-
Notifications
You must be signed in to change notification settings - Fork 30.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
src: diagnostic NodeReport initial implementation #7242
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
#include "node_version.h" | ||
#include "node_internals.h" | ||
#include "node_revert.h" | ||
#include "node_report.h" | ||
|
||
#if defined HAVE_PERFCTR | ||
#include "node_counters.h" | ||
|
@@ -151,6 +152,8 @@ static node_module* modpending; | |
static node_module* modlist_builtin; | ||
static node_module* modlist_linked; | ||
static node_module* modlist_addon; | ||
static unsigned int nodereport_events = 0; | ||
static int nodereport_signal = 0; | ||
|
||
#if defined(NODE_HAVE_I18N_SUPPORT) | ||
// Path to ICU data (for i18n / Intl) | ||
|
@@ -186,6 +189,8 @@ static v8::Platform* default_platform; | |
|
||
#ifdef __POSIX__ | ||
static uv_sem_t debug_semaphore; | ||
static uv_sem_t report_semaphore; | ||
static uv_async_t nodereport_trigger_async; | ||
#endif | ||
|
||
static void PrintErrorString(const char* format, ...) { | ||
|
@@ -1596,6 +1601,12 @@ static void ReportException(Environment* env, | |
} | ||
|
||
fflush(stderr); | ||
|
||
// Trigger NodeReport if required | ||
if (nodereport_events & NR_EXCEPTION) { | ||
TriggerNodeReport(env->isolate(), kException, | ||
"Uncaught exception", "node::ReportException", message); | ||
} | ||
} | ||
|
||
|
||
|
@@ -2336,6 +2347,10 @@ static void OnFatalError(const char* location, const char* message) { | |
} else { | ||
PrintErrorString("FATAL ERROR: %s\n", message); | ||
} | ||
// Trigger NodeReport if required | ||
if (nodereport_events & NR_FATALERROR) { | ||
TriggerNodeReport(Isolate::GetCurrent(), kFatalError, message, location); | ||
} | ||
fflush(stderr); | ||
ABORT(); | ||
} | ||
|
@@ -3226,6 +3241,55 @@ static void SignalExit(int signo) { | |
raise(signo); | ||
} | ||
|
||
#ifdef __POSIX__ | ||
// Callbacks for triggering NodeReport on external signal, run on event loop | ||
static void SignalDumpInterruptCallback(Isolate *isolate, void *data) { | ||
if (nodereport_signal != 0) { | ||
TriggerNodeReport(Isolate::GetCurrent(), kSignal_JS, | ||
signo_string(*(static_cast<int *>(data))), | ||
"node::SignalDumpInterruptCallback()"); | ||
nodereport_signal = 0; | ||
} | ||
} | ||
static void SignalDumpAsyncCallback(uv_async_t* handle) { | ||
if (nodereport_signal != 0) { | ||
size_t signo_data = reinterpret_cast<size_t>(handle->data); | ||
TriggerNodeReport(Isolate::GetCurrent(), kSignal_UV, | ||
signo_string(static_cast<int>(signo_data)), | ||
"node::SignalDumpAsyncCallback()"); | ||
nodereport_signal = 0; | ||
} | ||
} | ||
|
||
// Raw signal handler for triggering a NodeReport - runs on an arbitrary thread | ||
static void SignalDump(int signo) { | ||
// Check atomic for NodeReport already pending, storing the signal number | ||
if (__sync_val_compare_and_swap(&nodereport_signal, 0, signo) == 0) { | ||
uv_sem_post(&report_semaphore); // Hand-off to watchdog thread | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it safe using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @santigimeno good point, fprintf() is not on the safe list for signal handlers. I will remove those calls. |
||
|
||
// Watchdog thread implementation for signal-triggered NodeReport | ||
inline void* ReportSignalThreadMain(void* unused) { | ||
for (;;) { | ||
uv_sem_wait(&report_semaphore); | ||
fprintf(stderr, "Signal %s received, triggering NodeReport\n", | ||
signo_string(nodereport_signal)); | ||
uv_mutex_lock(&node_isolate_mutex); | ||
if (auto isolate = node_isolate) { | ||
// Request interrupt callback for running Javascript code | ||
isolate->RequestInterrupt(SignalDumpInterruptCallback, | ||
&nodereport_signal); | ||
// Event loop may be idle, so also request an async callback | ||
size_t signo_data = static_cast<size_t>(nodereport_signal); | ||
nodereport_trigger_async.data = reinterpret_cast<void *>(signo_data); | ||
uv_async_send(&nodereport_trigger_async); | ||
} | ||
uv_mutex_unlock(&node_isolate_mutex); | ||
} | ||
return nullptr; | ||
} | ||
#endif | ||
|
||
// Most of the time, it's best to use `console.error` to write | ||
// to the process.stderr stream. However, in some cases, such as | ||
|
@@ -3246,6 +3310,12 @@ void LoadEnvironment(Environment* env) { | |
env->isolate()->SetFatalErrorHandler(node::OnFatalError); | ||
env->isolate()->AddMessageListener(OnMessage); | ||
|
||
// If NodeReport requested for exceptions, tell V8 to capture stack trace | ||
if (nodereport_events & NR_EXCEPTION) { | ||
env->isolate()->SetCaptureStackTraceForUncaughtExceptions(true, 32, | ||
v8::StackTrace::kDetailed); | ||
} | ||
|
||
// Compile, execute the src/node.js file. (Which was included as static C | ||
// string in node_natives.h. 'native_node' is the string containing that | ||
// source code.) | ||
|
@@ -3408,6 +3478,12 @@ static void PrintHelp() { | |
#endif | ||
" --preserve-symlinks preserve symbolic links when resolving\n" | ||
" and caching modules.\n" | ||
#endif | ||
" --nodereport-events= trigger NodeReport on specified events:\n" | ||
#ifdef _WIN32 | ||
" exception+fatalerror\n" | ||
#else | ||
" exception+fatalerror+sigusr2+sigquit\n" | ||
#endif | ||
"\n" | ||
"Environment variables:\n" | ||
|
@@ -3566,6 +3642,9 @@ static void ParseArgs(int* argc, | |
} else if (strcmp(arg, "--expose-internals") == 0 || | ||
strcmp(arg, "--expose_internals") == 0) { | ||
// consumed in js | ||
} else if (strncmp(arg, "--nodereport-events=", 20) == 0) { | ||
const char* events = arg + 20; | ||
nodereport_events = ProcessNodeReportArgs(events); | ||
} else { | ||
// V8 option. Pass through as-is. | ||
new_v8_argv[new_v8_argc] = arg; | ||
|
@@ -3745,12 +3824,7 @@ inline void* DebugSignalThreadMain(void* unused) { | |
return nullptr; | ||
} | ||
|
||
|
||
static int RegisterDebugSignalHandler() { | ||
// Start a watchdog thread for calling v8::Debug::DebugBreak() because | ||
// it's not safe to call directly from the signal handler, it can | ||
// deadlock with the thread it interrupts. | ||
CHECK_EQ(0, uv_sem_init(&debug_semaphore, 0)); | ||
static int StartWatchdogThread(void *(*thread_main) (void* unused)) { | ||
pthread_attr_t attr; | ||
CHECK_EQ(0, pthread_attr_init(&attr)); | ||
// Don't shrink the thread's stack on FreeBSD. Said platform decided to | ||
|
@@ -3765,23 +3839,33 @@ static int RegisterDebugSignalHandler() { | |
CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, &sigmask)); | ||
pthread_t thread; | ||
const int err = | ||
pthread_create(&thread, &attr, DebugSignalThreadMain, nullptr); | ||
pthread_create(&thread, &attr, thread_main, nullptr); | ||
CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, nullptr)); | ||
CHECK_EQ(0, pthread_attr_destroy(&attr)); | ||
if (err != 0) { | ||
fprintf(stderr, "node[%d]: pthread_create: %s\n", getpid(), strerror(err)); | ||
fflush(stderr); | ||
// Leave SIGUSR1 blocked. We don't install a signal handler, | ||
// receiving the signal would terminate the process. | ||
return -err; | ||
} | ||
RegisterSignalHandler(SIGUSR1, EnableDebugSignalHandler); | ||
// Unblock SIGUSR1. A pending SIGUSR1 signal will now be delivered. | ||
sigemptyset(&sigmask); | ||
sigaddset(&sigmask, SIGUSR1); | ||
CHECK_EQ(0, pthread_sigmask(SIG_UNBLOCK, &sigmask, nullptr)); | ||
return 0; | ||
} | ||
|
||
static int RegisterDebugSignalHandler() { | ||
// Start a watchdog thread for calling v8::Debug::DebugBreak() because | ||
// it's not safe to call directly from the signal handler, it can | ||
// deadlock with the thread it interrupts. | ||
CHECK_EQ(0, uv_sem_init(&debug_semaphore, 0)); | ||
const int err = StartWatchdogThread(DebugSignalThreadMain); | ||
if (err == 0) { | ||
RegisterSignalHandler(SIGUSR1, EnableDebugSignalHandler); | ||
// Unblock SIGUSR1. A pending SIGUSR1 signal will now be delivered. | ||
sigset_t sigmask; | ||
sigemptyset(&sigmask); | ||
sigaddset(&sigmask, SIGUSR1); | ||
CHECK_EQ(0, pthread_sigmask(SIG_UNBLOCK, &sigmask, nullptr)); | ||
} | ||
return err; | ||
} | ||
#endif // __POSIX__ | ||
|
||
|
||
|
@@ -4065,6 +4149,23 @@ void Init(int* argc, | |
if (v8_is_profiling) { | ||
uv_loop_configure(uv_default_loop(), UV_LOOP_BLOCK_SIGNAL, SIGPROF); | ||
} | ||
|
||
// Setup for NodeReport requested on external user signal | ||
if (nodereport_events & NR_SIGQUIT || nodereport_events & NR_SIGUSR2) { | ||
CHECK_EQ(0, uv_sem_init(&report_semaphore, 0)); | ||
if (StartWatchdogThread(ReportSignalThreadMain) == 0) { | ||
uv_async_init(uv_default_loop(), &nodereport_trigger_async, | ||
SignalDumpAsyncCallback); | ||
uv_unref(reinterpret_cast<uv_handle_t*>(&nodereport_trigger_async)); | ||
// Now register the external signal handlers, as requested | ||
if (nodereport_events & NR_SIGQUIT) { | ||
RegisterSignalHandler(SIGQUIT, SignalDump, false); | ||
} | ||
if (nodereport_events & NR_SIGUSR2) { | ||
RegisterSignalHandler(SIGUSR2, SignalDump, false); | ||
} | ||
} | ||
} | ||
#endif | ||
|
||
#if defined(NODE_HAVE_I18N_SUPPORT) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you also add the header file down below? It makes it easier when using Xcode or other IDEs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@evanlucas ah right, will do, thanks