diff options
author | legendecas <legendecas@gmail.com> | 2019-11-17 15:02:01 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-12-17 00:05:53 +0300 |
commit | 6859fcf6f7c0710508b2a1f461043547e8a85acc (patch) | |
tree | 48d417ae1d782815a2eed65f71f9a099a6d21cfa /src | |
parent | 024b6588313a0e32595317936ee279dec7a5a756 (diff) |
cli: add --trace-exit cli option
It could be convenient to trace abnormal exit of the Node.js processes
that printing stacktrace on each `process.exit` call with a cli option.
This also takes effects on worker threads.
PR-URL: https://github.com/nodejs/node/pull/30516
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/env.cc | 15 | ||||
-rw-r--r-- | src/node_options.cc | 4 | ||||
-rw-r--r-- | src/node_options.h | 1 |
3 files changed, 20 insertions, 0 deletions
diff --git a/src/env.cc b/src/env.cc index ed0c82cdeba..c816ea82d6a 100644 --- a/src/env.cc +++ b/src/env.cc @@ -937,6 +937,21 @@ void AsyncHooks::grow_async_ids_stack() { uv_key_t Environment::thread_local_env = {}; void Environment::Exit(int exit_code) { + if (options()->trace_exit) { + HandleScope handle_scope(isolate()); + + if (is_main_thread()) { + fprintf(stderr, "(node:%d) ", uv_os_getpid()); + } else { + fprintf(stderr, "(node:%d, thread:%llu) ", uv_os_getpid(), thread_id()); + } + + fprintf( + stderr, "WARNING: Exited the environment with code %d\n", exit_code); + PrintStackTrace( + isolate(), + StackTrace::CurrentStackTrace(isolate(), 10, StackTrace::kDetailed)); + } if (is_main_thread()) { stop_sub_worker_contexts(); DisposePlatform(); diff --git a/src/node_options.cc b/src/node_options.cc index 831540f993f..ff3bf9b4edd 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -480,6 +480,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { "show stack traces on deprecations", &EnvironmentOptions::trace_deprecation, kAllowedInEnvironment); + AddOption("--trace-exit", + "show stack trace when an environment exits", + &EnvironmentOptions::trace_exit, + kAllowedInEnvironment); AddOption("--trace-sync-io", "show stack trace when use of sync IO is detected after the " "first tick", diff --git a/src/node_options.h b/src/node_options.h index c3ef78fd5bc..633d5725369 100644 --- a/src/node_options.h +++ b/src/node_options.h @@ -141,6 +141,7 @@ class EnvironmentOptions : public Options { bool test_udp_no_try_send = false; bool throw_deprecation = false; bool trace_deprecation = false; + bool trace_exit = false; bool trace_sync_io = false; bool trace_tls = false; bool trace_uncaught = false; |