Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
'src/node_main.cc',
'src/node_os.cc',
'src/node_revert.cc',
'src/node_report.cc',
Copy link
Contributor

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

Copy link
Author

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

'src/node_util.cc',
'src/node_v8.cc',
'src/node_stat_watcher.cc',
Expand Down Expand Up @@ -179,6 +180,7 @@
'src/node_watchdog.h',
'src/node_wrap.h',
'src/node_revert.h',
'src/node_report.h',
'src/node_i18n.h',
'src/pipe_wrap.h',
'src/tty_wrap.h',
Expand Down
129 changes: 115 additions & 14 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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, ...) {
Expand Down Expand Up @@ -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);
}
}


Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it safe using fprintf in a signal handler?

Copy link
Author

Choose a reason for hiding this comment

The 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.
It is very handy to issue a message at that point, I may be able to use write() instead.


// 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
Expand All @@ -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.)
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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__


Expand Down Expand Up @@ -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)
Expand Down
Loading