From b038ad91f5a49a5d5bb2c4050b03b54026a60119 Mon Sep 17 00:00:00 2001 From: legendecas Date: Mon, 19 Aug 2019 21:03:08 +0800 Subject: [PATCH] src,lib: make ^C print a JS stack trace If terminating the process with ctrl-c / SIGINT, prints a JS stacktrace leading up to the currently executing code. The feature would be enabled under option `--trace-sigint`. Conditions of no stacktrace on sigint: - has (an) active sigint listener(s); - main thread is idle (i.e. uv polling), a message instead of stacktrace would be printed. PR-URL: https://github.com/nodejs/node/pull/29207 Reviewed-By: Anna Henningsen Reviewed-By: Matteo Collina Reviewed-By: Ben Noordhuis Reviewed-By: Joyee Cheung Reviewed-By: Christopher Hiller Reviewed-By: Rich Trott --- doc/api/cli.md | 8 + doc/node.1 | 2 + lib/internal/bootstrap/pre_execution.js | 13 ++ lib/internal/watchdog.js | 59 +++++++ node.gyp | 1 + src/async_wrap.h | 1 + src/memory_tracker-inl.h | 14 ++ src/memory_tracker.h | 7 + src/node_binding.cc | 1 + src/node_options.cc | 5 + src/node_options.h | 1 + src/node_watchdog.cc | 144 +++++++++++++++++- src/node_watchdog.h | 54 ++++++- test/pseudo-tty/test-trace-sigint-disabled.js | 39 +++++ .../pseudo-tty/test-trace-sigint-disabled.out | 0 test/pseudo-tty/test-trace-sigint-on-idle.js | 30 ++++ test/pseudo-tty/test-trace-sigint-on-idle.out | 1 + test/pseudo-tty/test-trace-sigint.js | 30 ++++ test/pseudo-tty/test-trace-sigint.out | 9 ++ test/sequential/test-async-wrap-getasyncid.js | 1 + 20 files changed, 405 insertions(+), 15 deletions(-) create mode 100644 lib/internal/watchdog.js create mode 100644 test/pseudo-tty/test-trace-sigint-disabled.js create mode 100644 test/pseudo-tty/test-trace-sigint-disabled.out create mode 100644 test/pseudo-tty/test-trace-sigint-on-idle.js create mode 100644 test/pseudo-tty/test-trace-sigint-on-idle.out create mode 100644 test/pseudo-tty/test-trace-sigint.js create mode 100644 test/pseudo-tty/test-trace-sigint.out diff --git a/doc/api/cli.md b/doc/api/cli.md index d323e24f3ecf96..a64c426c2515cf 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -821,6 +821,13 @@ added: v12.16.0 Prints a stack trace whenever an environment is exited proactively, i.e. invoking `process.exit()`. +### `--trace-sigint` + + +Prints a stack trace on SIGINT. + ### `--trace-sync-io`