diff options
author | legendecas <legendecas@gmail.com> | 2019-11-17 15:02:01 +0300 |
---|---|---|
committer | Myles Borins <mylesborins@google.com> | 2019-12-18 02:21:12 +0300 |
commit | d8ce9a0e234660d268f60fb55b156aebfaf29310 (patch) | |
tree | b34d2b4ac4f2089f28c36d0ba2dcca9493853c87 /src | |
parent | 510edead69d2ea3288ee7e76e352d937cd997c30 (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 bf3420f826f..f3d71c61c09 100644 --- a/src/env.cc +++ b/src/env.cc @@ -940,6 +940,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; |