1- # include " node_report.h "
1+ // Copyright 2017 Nodereport
22#include < nan.h>
3+ #include " node_report.h"
34
45namespace nodereport {
56
@@ -25,7 +26,7 @@ static unsigned int nodereport_verbose = 0;
2526#ifdef _WIN32 // signal trigger not supported on Windows
2627static unsigned int nodereport_signal = 0 ;
2728#else // trigger signal supported on Unix platforms and OSX
28- static unsigned int nodereport_signal = SIGUSR2; // default signal is SIGUSR2
29+ static unsigned int nodereport_signal = SIGUSR2; // default signal is SIGUSR2
2930static int report_signal = 0 ; // atomic for signal handling in progress
3031static uv_sem_t report_semaphore; // semaphore for hand-off to watchdog
3132static uv_async_t nodereport_trigger_async; // async handle for event loop
@@ -61,7 +62,8 @@ NAN_METHOD(TriggerReport) {
6162 }
6263 }
6364 if (nodereport_events & NR_APICALL) {
64- TriggerNodeReport (isolate, kJavaScript , " JavaScript API" , __func__, filename);
65+ TriggerNodeReport (isolate, kJavaScript , " JavaScript API" , __func__,
66+ filename);
6567 // Return value is the NodeReport filename
6668 info.GetReturnValue ().Set (Nan::New (filename).ToLocalChecked ());
6769 }
@@ -74,30 +76,38 @@ NAN_METHOD(TriggerReport) {
7476NAN_METHOD (SetEvents) {
7577 Nan::Utf8String parameter (info[0 ]);
7678 v8::Isolate* isolate = info.GetIsolate ();
77- unsigned int previous_events = nodereport_events; // save previous settings
79+ unsigned int previous_events = nodereport_events; // save previous settings
7880 nodereport_events = ProcessNodeReportEvents (*parameter);
7981
8082 // If NodeReport newly requested for fatalerror, set up the V8 call-back
81- if ((nodereport_events & NR_FATALERROR) && (error_hook_initialised == false )) {
83+ if ((nodereport_events & NR_FATALERROR) &&
84+ (error_hook_initialised == false )) {
8285 isolate->SetFatalErrorHandler (OnFatalError);
8386 error_hook_initialised = true ;
8487 }
8588
86- // If NodeReport newly requested for exceptions, tell V8 to capture stack trace and set up the callback
87- if ((nodereport_events & NR_EXCEPTION) && (exception_hook_initialised == false )) {
88- isolate->SetCaptureStackTraceForUncaughtExceptions (true , 32 , v8::StackTrace::kDetailed );
89- // The hook for uncaught exception won't get called unless the --abort_on_uncaught_exception option is set
90- v8::V8::SetFlagsFromString (" --abort_on_uncaught_exception" , sizeof (" --abort_on_uncaught_exception" )-1 );
89+ // If NodeReport newly requested for exceptions, tell V8 to capture stack
90+ // trace and set up the callback
91+ if ((nodereport_events & NR_EXCEPTION) &&
92+ (exception_hook_initialised == false )) {
93+ isolate->SetCaptureStackTraceForUncaughtExceptions (true , 32 ,
94+ v8::StackTrace::kDetailed );
95+ // The hook for uncaught exception won't get called unless the
96+ // --abort_on_uncaught_exception option is set
97+ v8::V8::SetFlagsFromString (" --abort_on_uncaught_exception" ,
98+ sizeof (" --abort_on_uncaught_exception" )-1 );
9199 isolate->SetAbortOnUncaughtExceptionCallback (OnUncaughtException);
92100 exception_hook_initialised = true ;
93101 }
94102
95103#ifndef _WIN32
96- // If NodeReport newly requested on external user signal set up watchdog thread and handler
104+ // If NodeReport newly requested on external user signal set up watchdog
105+ // thread and handler
97106 if ((nodereport_events & NR_SIGNAL) && (signal_thread_initialised == false )) {
98107 SetupSignalHandler ();
99108 }
100- // If NodeReport no longer required on external user signal, reset the OS signal handler
109+ // If NodeReport no longer required on external user signal, reset the OS
110+ // signal handler
101111 if (!(nodereport_events & NR_SIGNAL) && (previous_events & NR_SIGNAL)) {
102112 RestoreSignalHandler (nodereport_signal, &saved_sa);
103113 }
@@ -106,11 +116,13 @@ NAN_METHOD(SetEvents) {
106116NAN_METHOD (SetSignal) {
107117#ifndef _WIN32
108118 Nan::Utf8String parameter (info[0 ]);
109- unsigned int previous_signal = nodereport_signal; // save previous setting
119+ unsigned int previous_signal = nodereport_signal; // save previous setting
110120 nodereport_signal = ProcessNodeReportSignal (*parameter);
111121
112- // If signal event active and selected signal has changed, switch the OS signal handler
113- if ((nodereport_events & NR_SIGNAL) && (nodereport_signal != previous_signal)) {
122+ // If signal event active and selected signal has changed, switch the OS
123+ // signal handler
124+ if ((nodereport_events & NR_SIGNAL) &&
125+ (nodereport_signal != previous_signal)) {
114126 RestoreSignalHandler (previous_signal, &saved_sa);
115127 RegisterSignalHandler (nodereport_signal, SignalDump, &saved_sa);
116128 }
@@ -141,7 +153,8 @@ static void OnFatalError(const char* location, const char* message) {
141153 }
142154 // Trigger NodeReport if requested
143155 if (nodereport_events & NR_FATALERROR) {
144- TriggerNodeReport (Isolate::GetCurrent (), kFatalError , message, location, nullptr );
156+ TriggerNodeReport (Isolate::GetCurrent (), kFatalError , message, location,
157+ nullptr );
145158 }
146159 fflush (stderr);
147160 raise (SIGABRT);
@@ -152,8 +165,10 @@ bool OnUncaughtException(v8::Isolate* isolate) {
152165 if (nodereport_events & NR_EXCEPTION) {
153166 TriggerNodeReport (isolate, kException , " exception" , __func__, nullptr );
154167 }
155- if ((commandline_string.find (" abort-on-uncaught-exception" ) != std::string::npos) ||
156- (commandline_string.find (" abort_on_uncaught_exception" ) != std::string::npos)) {
168+ if ((commandline_string.find (" abort-on-uncaught-exception" )
169+ != std::string::npos) ||
170+ (commandline_string.find (" abort_on_uncaught_exception" )
171+ != std::string::npos)) {
157172 return true ; // abort required
158173 }
159174 // On V8 versions earlier than 5.4 we need to print the exception backtrace
@@ -177,7 +192,7 @@ bool OnUncaughtException(v8::Isolate* isolate) {
177192#ifdef _WIN32
178193static void PrintStackFromStackTrace (Isolate* isolate, FILE* fp) {
179194 Local<StackTrace> stack = StackTrace::CurrentStackTrace (isolate, 255 ,
180- StackTrace::kDetailed );
195+ StackTrace::kDetailed );
181196 // Print the JavaScript function name and source information for each frame
182197 for (int i = 0 ; i < stack->GetFrameCount (); i++) {
183198 Local<StackFrame> frame = stack->GetFrame (i);
@@ -190,13 +205,15 @@ static void PrintStackFromStackTrace(Isolate* isolate, FILE* fp) {
190205 if (frame->GetScriptId () == Message::kNoScriptIdInfo ) {
191206 fprintf (fp, " at [eval]:%i:%i\n " , line_number, column);
192207 } else {
193- fprintf (fp, " at [eval] (%s:%i:%i)\n " , *script_name, line_number, column);
208+ fprintf (fp, " at [eval] (%s:%i:%i)\n " , *script_name, line_number,
209+ column);
194210 }
195211 } else {
196212 if (fn_name_s.length () == 0 ) {
197213 fprintf (fp, " %s:%i:%i\n " , *script_name, line_number, column);
198214 } else {
199- fprintf (fp, " %s (%s:%i:%i)\n " , *fn_name_s, *script_name, line_number, column);
215+ fprintf (fp, " %s (%s:%i:%i)\n " , *fn_name_s, *script_name, line_number,
216+ column);
200217 }
201218 }
202219 }
@@ -206,11 +223,13 @@ static void PrintStackFromStackTrace(Isolate* isolate, FILE* fp) {
206223static void SignalDumpInterruptCallback (Isolate* isolate, void * data) {
207224 if (report_signal != 0 ) {
208225 if (nodereport_verbose) {
209- fprintf (stdout, " nodereport: SignalDumpInterruptCallback handling signal\n " );
226+ fprintf (stdout, " nodereport: SignalDumpInterruptCallback handling"
227+ " signal\n " );
210228 }
211229 if (nodereport_events & NR_SIGNAL) {
212230 if (nodereport_verbose) {
213- fprintf (stdout, " nodereport: SignalDumpInterruptCallback triggering NodeReport\n " );
231+ fprintf (stdout, " nodereport: SignalDumpInterruptCallback triggering"
232+ " NodeReport\n " );
214233 }
215234 TriggerNodeReport (isolate, kSignal_JS ,
216235 node::signo_string (report_signal), __func__, nullptr );
@@ -225,7 +244,8 @@ static void SignalDumpAsyncCallback(uv_async_t* handle) {
225244 }
226245 if (nodereport_events & NR_SIGNAL) {
227246 if (nodereport_verbose) {
228- fprintf (stdout, " nodereport: SignalDumpAsyncCallback triggering NodeReport\n " );
247+ fprintf (stdout, " nodereport: SignalDumpAsyncCallback triggering"
248+ " NodeReport\n " );
229249 }
230250 TriggerNodeReport (Isolate::GetCurrent (), kSignal_UV ,
231251 node::signo_string (report_signal), __func__, nullptr );
@@ -283,7 +303,8 @@ static int StartWatchdogThread(void* (*thread_main)(void* unused)) {
283303 pthread_sigmask (SIG_SETMASK, &sigmask, nullptr );
284304 pthread_attr_destroy (&attr);
285305 if (err != 0 ) {
286- fprintf (stderr, " nodereport: StartWatchdogThread pthread_create() failed: %s\n " , strerror (err));
306+ fprintf (stderr, " nodereport: StartWatchdogThread pthread_create() failed:"
307+ " %s\n " , strerror (err));
287308 fflush (stderr);
288309 return -err;
289310 }
@@ -295,7 +316,8 @@ inline void* ReportSignalThreadMain(void* unused) {
295316 for (;;) {
296317 uv_sem_wait (&report_semaphore);
297318 if (nodereport_verbose) {
298- fprintf (stdout, " nodereport: signal %s received\n " , node::signo_string (report_signal));
319+ fprintf (stdout, " nodereport: signal %s received\n " ,
320+ node::signo_string (report_signal));
299321 }
300322 uv_mutex_lock (&node_isolate_mutex);
301323 if (auto isolate = node_isolate) {
@@ -313,20 +335,27 @@ inline void* ReportSignalThreadMain(void* unused) {
313335static void SetupSignalHandler () {
314336 int rc = uv_sem_init (&report_semaphore, 0 );
315337 if (rc != 0 ) {
316- fprintf (stderr, " nodereport: initialization failed, uv_sem_init() returned %d\n " , rc);
317- Nan::ThrowError (" nodereport: initialization failed, uv_sem_init() returned error\n " );
338+ fprintf (stderr, " nodereport: initialization failed, uv_sem_init()"
339+ " returned %d\n " , rc);
340+ Nan::ThrowError (" nodereport: initialization failed, uv_sem_init()"
341+ " returned error\n " );
318342 }
319343 rc = uv_mutex_init (&node_isolate_mutex);
320344 if (rc != 0 ) {
321- fprintf (stderr, " nodereport: initialization failed, uv_mutex_init() returned %d\n " , rc);
322- Nan::ThrowError (" nodereport: initialization failed, uv_mutex_init() returned error\n " );
345+ fprintf (stderr, " nodereport: initialization failed, uv_mutex_init()"
346+ " returned %d\n " , rc);
347+ Nan::ThrowError (" nodereport: initialization failed, uv_mutex_init()"
348+ " returned error\n " );
323349 }
324350
325351 if (StartWatchdogThread (ReportSignalThreadMain) == 0 ) {
326- rc = uv_async_init (uv_default_loop (), &nodereport_trigger_async, SignalDumpAsyncCallback);
352+ rc = uv_async_init (uv_default_loop (), &nodereport_trigger_async,
353+ SignalDumpAsyncCallback);
327354 if (rc != 0 ) {
328- fprintf (stderr, " nodereport: initialization failed, uv_async_init() returned %d\n " , rc);
329- Nan::ThrowError (" nodereport: initialization failed, uv_async_init() returned error\n " );
355+ fprintf (stderr, " nodereport: initialization failed, uv_async_init()"
356+ " returned %d\n " , rc);
357+ Nan::ThrowError (" nodereport: initialization failed, uv_async_init()"
358+ " returned error\n " );
330359 }
331360 uv_unref (reinterpret_cast <uv_handle_t *>(&nodereport_trigger_async));
332361 RegisterSignalHandler (nodereport_signal, SignalDump, &saved_sa);
@@ -374,17 +403,22 @@ void Initialize(v8::Local<v8::Object> exports) {
374403 error_hook_initialised = true ;
375404 }
376405
377- // If NodeReport requested for exceptions, tell V8 to capture stack trace and set up the callback
406+ // If NodeReport requested for exceptions, tell V8 to capture stack trace and
407+ // set up the callback
378408 if (nodereport_events & NR_EXCEPTION) {
379- isolate->SetCaptureStackTraceForUncaughtExceptions (true , 32 , v8::StackTrace::kDetailed );
380- // The hook for uncaught exception won't get called unless the --abort_on_uncaught_exception option is set
381- v8::V8::SetFlagsFromString (" --abort_on_uncaught_exception" , sizeof (" --abort_on_uncaught_exception" )-1 );
409+ isolate->SetCaptureStackTraceForUncaughtExceptions (true , 32 ,
410+ v8::StackTrace::kDetailed );
411+ // The hook for uncaught exception won't get called unless the
412+ // --abort_on_uncaught_exception option is set
413+ v8::V8::SetFlagsFromString (" --abort_on_uncaught_exception" ,
414+ sizeof (" --abort_on_uncaught_exception" )-1 );
382415 isolate->SetAbortOnUncaughtExceptionCallback (OnUncaughtException);
383416 exception_hook_initialised = true ;
384417 }
385418
386419#ifndef _WIN32
387- // If NodeReport requested on external user signal set up watchdog thread and callbacks
420+ // If NodeReport requested on external user signal set up watchdog thread
421+ // and callbacks
388422 if (nodereport_events & NR_SIGNAL) {
389423 SetupSignalHandler ();
390424 }
@@ -408,7 +442,8 @@ void Initialize(v8::Local<v8::Object> exports) {
408442 fprintf (stdout, " nodereport: initialization complete, event flags: %#x\n " ,
409443 nodereport_events);
410444#else
411- fprintf (stdout, " nodereport: initialization complete, event flags: %#x signal flag: %#x\n " ,
445+ fprintf (stdout, " nodereport: initialization complete, event flags: %#x"
446+ " signal flag: %#x\n " ,
412447 nodereport_events, nodereport_signal);
413448#endif
414449 }
@@ -417,4 +452,3 @@ void Initialize(v8::Local<v8::Object> exports) {
417452NODE_MODULE (nodereport, Initialize)
418453
419454} // namespace nodereport
420-
0 commit comments