This repository has been archived by the owner on Jun 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
utilities.cc
680 lines (645 loc) · 20.5 KB
/
utilities.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
#include "node_report.h"
#ifdef __APPLE__
#include <crt_externs.h> // _NSGetArgv() and _NSGetArgc()
#endif
#ifdef __sun
#include <procfs.h> // psinfo_t structure
#endif
#ifdef _AIX
#include <sys/procfs.h> // psinfo_t structure
#endif
namespace nodereport {
/*******************************************************************************
* Function to process node-report config: selection of trigger events.
******************************************************************************/
unsigned int ProcessNodeReportEvents(const char* args) {
// Parse the supplied event types
unsigned int event_flags = 0;
const char* cursor = args;
while (*cursor != '\0') {
if (!strncmp(cursor, "exception", sizeof("exception") - 1)) {
event_flags |= NR_EXCEPTION;
cursor += sizeof("exception") - 1;
} else if (!strncmp(cursor, "fatalerror", sizeof("fatalerror") - 1)) {
event_flags |= NR_FATALERROR;
cursor += sizeof("fatalerror") - 1;
} else if (!strncmp(cursor, "signal", sizeof("signal") - 1)) {
event_flags |= NR_SIGNAL;
cursor += sizeof("signal") - 1;
} else if (!strncmp(cursor, "apicall", sizeof("apicall") - 1)) {
event_flags |= NR_APICALL;
cursor += sizeof("apicall") - 1;
} else {
std::cerr << "Unrecognised argument for node-report events option: " << cursor << "\n";
return 0;
}
if (*cursor == '+') {
cursor++; // Hop over the '+' separator
}
}
return event_flags;
}
/*******************************************************************************
* Function to process node-report config: selection of trigger signal.
******************************************************************************/
unsigned int ProcessNodeReportSignal(const char* args) {
#ifdef _WIN32
return 0; // no-op on Windows
#else
if (strlen(args) == 0) {
std::cerr << "Missing argument for node-report signal option\n";
} else {
// Parse the supplied switch
if (!strncmp(args, "SIGUSR2", sizeof("SIGUSR2") - 1)) {
return SIGUSR2;
} else if (!strncmp(args, "SIGQUIT", sizeof("SIGQUIT") - 1)) {
return SIGQUIT;
} else {
std::cerr << "Unrecognised argument for node-report signal option: "<< args << "\n";
}
}
return SIGUSR2; // Default signal is SIGUSR2
#endif
}
/*******************************************************************************
* Function to process node-report config: specification of report file name.
******************************************************************************/
void ProcessNodeReportFileName(const char* args) {
if (strlen(args) == 0) {
std::cerr << "Missing argument for node-report filename option\n";
return;
}
if (strlen(args) > NR_MAXNAME) {
std::cerr << "Supplied node-report filename too long (max " << NR_MAXNAME << " characters)\n";
return;
}
snprintf(report_filename, sizeof(report_filename), "%s", args);
}
/*******************************************************************************
* Function to process node-report config: specification of report directory.
******************************************************************************/
void ProcessNodeReportDirectory(const char* args) {
if (strlen(args) == 0) {
std::cerr << "Missing argument for node-report directory option\n";
return;
}
if (strlen(args) > NR_MAXPATH) {
std::cerr << "Supplied node-report directory path too long (max " << NR_MAXPATH << " characters)\n";
return;
}
snprintf(report_directory, sizeof(report_directory), "%s", args);
}
/*******************************************************************************
* Function to process node-report config: verbose mode switch.
******************************************************************************/
unsigned int ProcessNodeReportVerboseSwitch(const char* args) {
if (strlen(args) == 0) {
std::cerr << "Missing argument for node-report verbose switch option\n";
return 0;
}
// Parse the supplied switch
if (!strncmp(args, "yes", sizeof("yes") - 1) || !strncmp(args, "true", sizeof("true") - 1)) {
return 1;
} else if (!strncmp(args, "no", sizeof("no") - 1) || !strncmp(args, "false", sizeof("false") - 1)) {
return 0;
} else {
std::cerr << "Unrecognised argument for node-report verbose switch option: " << args << "\n";
}
return 0; // Default is verbose mode off
}
/*******************************************************************************
* Function to save the node and subcomponent version strings. This is called
* during node-report module initialisation.
*******************************************************************************/
void SetVersionString(Isolate* isolate) {
// Catch anything thrown and gracefully return
Nan::TryCatch trycatch;
version_string = UNKNOWN_NODEVERSION_STRING;
// Retrieve the process object
v8::Local<v8::String> process_prop;
if (!Nan::New<v8::String>("process").ToLocal(&process_prop)) return;
v8::Local<v8::Object> global_obj = isolate->GetCurrentContext()->Global();
v8::Local<v8::Value> process_value;
if (!Nan::Get(global_obj, process_prop).ToLocal(&process_value)) return;
if (!process_value->IsObject()) return;
v8::Local<v8::Object> process_obj = process_value.As<v8::Object>();
// Get process.version
v8::Local<v8::String> version_prop;
if (!Nan::New<v8::String>("version").ToLocal(&version_prop)) return;
v8::Local<v8::Value> version;
if (!Nan::Get(process_obj, version_prop).ToLocal(&version)) return;
// e.g. Node.js version: v6.9.1
if (version->IsString()) {
Nan::Utf8String node_version(version);
version_string = "Node.js version: ";
version_string += *node_version;
version_string += "\n";
}
// Get process.versions
v8::Local<v8::String> versions_prop;
if (!Nan::New<v8::String>("versions").ToLocal(&versions_prop)) return;
v8::Local<v8::Value> versions_value;
if (!Nan::Get(process_obj, versions_prop).ToLocal(&versions_value)) return;
if (!versions_value->IsObject()) return;
v8::Local<v8::Object> versions_obj = versions_value.As<v8::Object>();
// Get component names and versions from process.versions
v8::Local<v8::Array> components;
if (!Nan::GetOwnPropertyNames(versions_obj).ToLocal(&components)) return;
v8::Local<v8::Object> components_obj = components.As<v8::Object>();
std::string comp_versions = "(";
size_t wrap = 0;
uint32_t total_components = (*components)->Length();
for (uint32_t i = 0; i < total_components; i++) {
v8::Local<v8::Value> name_value;
if (!Nan::Get(components_obj, i).ToLocal(&name_value)) continue;
v8::Local<v8::Value> version_value;
if (!Nan::Get(versions_obj, name_value).ToLocal(&version_value)) continue;
Nan::Utf8String component_name(name_value);
Nan::Utf8String component_version(version_value);
if (*component_name == nullptr || *component_version == nullptr) continue;
if (!strcmp("node", *component_name)) {
// Put the Node.js version on the first line, if we didn't already have it
if (version_string == UNKNOWN_NODEVERSION_STRING) {
version_string = "Node.js version: v";
version_string += *component_version;
version_string += "\n";
}
} else {
// Other component versions follow, comma separated, wrapped at 80 characters
std::string comp_version_string = *component_name;
comp_version_string += ": ";
comp_version_string += *component_version;
if (wrap == 0) {
wrap = comp_version_string.length();
} else {
wrap += comp_version_string.length() + 2; // includes separator
if (wrap > 80) {
comp_versions += ",\n ";
wrap = comp_version_string.length();
} else {
comp_versions += ", ";
}
}
comp_versions += comp_version_string;
}
}
version_string += comp_versions + ")\n";
}
/*******************************************************************************
* Function to save the node-report module load time value. This is called
* during node-report module initialisation.
*******************************************************************************/
void SetLoadTime() {
#ifdef _WIN32
GetLocalTime(&loadtime_tm_struct);
#else // UNIX, OSX
struct timeval time_val;
gettimeofday(&time_val, nullptr);
localtime_r(&time_val.tv_sec, &loadtime_tm_struct);
#endif
time(&load_time);
}
/*******************************************************************************
* Function to save the process command line. This is called during node-report
* module initialisation.
*******************************************************************************/
void SetCommandLine() {
#ifdef __linux__
// Read the command line from /proc/self/cmdline
char buf[64];
FILE* cmdline_fd = fopen("/proc/self/cmdline", "r");
if (cmdline_fd == nullptr) {
return;
}
commandline_string = "";
int bytesread = fread(buf, 1, sizeof(buf), cmdline_fd);
while (bytesread > 0) {
for (int i = 0; i < bytesread; i++) {
// Arguments are null separated.
if (buf[i] == '\0') {
commandline_string += " ";
} else {
commandline_string += buf[i];
}
}
bytesread = fread(buf, 1, sizeof(buf), cmdline_fd);
}
fclose(cmdline_fd);
#elif __APPLE__
char **argv = *_NSGetArgv();
int argc = *_NSGetArgc();
commandline_string = "";
std::string separator = "";
for (int i = 0; i < argc; i++) {
commandline_string += separator + argv[i];
separator = " ";
}
#elif defined(_AIX) || defined(__sun)
// Read the command line from /proc/self/cmdline
char procbuf[64];
snprintf(procbuf, sizeof(procbuf), "/proc/%d/psinfo", getpid());
FILE* psinfo_fd = fopen(procbuf, "r");
if (psinfo_fd == nullptr) {
return;
}
psinfo_t info;
int bytesread = fread(&info, 1, sizeof(psinfo_t), psinfo_fd);
fclose(psinfo_fd);
if (bytesread == sizeof(psinfo_t)) {
commandline_string = "";
std::string separator = "";
#ifdef _AIX
char **argv = *((char ***) info.pr_argv);
#else
char **argv = ((char **) info.pr_argv);
#endif
for (uint32_t i = 0; i < info.pr_argc && argv[i] != nullptr; i++) {
commandline_string += separator + argv[i];
separator = " ";
}
}
#elif _WIN32
commandline_string = GetCommandLine();
#endif
}
/*******************************************************************************
* Utility function to format libuv socket information.
*******************************************************************************/
void reportEndpoints(uv_handle_t* h, std::ostringstream& out) {
struct sockaddr_storage addr_storage;
struct sockaddr* addr = (sockaddr*)&addr_storage;
char hostbuf[NI_MAXHOST];
char portbuf[NI_MAXSERV];
uv_any_handle* handle = (uv_any_handle*)h;
int addr_size = sizeof(addr_storage);
int rc = -1;
switch (h->type) {
case UV_UDP: {
rc = uv_udp_getsockname(&(handle->udp), addr, &addr_size);
break;
}
case UV_TCP: {
rc = uv_tcp_getsockname(&(handle->tcp), addr, &addr_size);
break;
}
default: break;
}
if (rc == 0) {
// getnameinfo will format host and port and handle IPv4/IPv6.
rc = getnameinfo(addr, addr_size, hostbuf, sizeof(hostbuf), portbuf,
sizeof(portbuf), NI_NUMERICSERV);
if (rc == 0) {
out << std::string(hostbuf) << ":" << std::string(portbuf);
}
if (h->type == UV_TCP) {
// Get the remote end of the connection.
rc = uv_tcp_getpeername(&(handle->tcp), addr, &addr_size);
if (rc == 0) {
rc = getnameinfo(addr, addr_size, hostbuf, sizeof(hostbuf), portbuf,
sizeof(portbuf), NI_NUMERICSERV);
if (rc == 0) {
out << " connected to ";
out << std::string(hostbuf) << ":" << std::string(portbuf);
}
} else if (rc == UV_ENOTCONN) {
out << " (not connected)";
}
}
}
}
/*******************************************************************************
* Utility function to format libuv path information.
*******************************************************************************/
void reportPath(uv_handle_t* h, std::ostringstream& out) {
char *buffer = nullptr;
int rc = -1;
size_t size = 0;
uv_any_handle* handle = (uv_any_handle*)h;
// First call to get required buffer size.
switch (h->type) {
case UV_FS_EVENT: {
rc = uv_fs_event_getpath(&(handle->fs_event), buffer, &size);
break;
}
case UV_FS_POLL: {
rc = uv_fs_poll_getpath(&(handle->fs_poll), buffer, &size);
break;
}
default: break;
}
if (rc == UV_ENOBUFS) {
buffer = static_cast<char *>(malloc(size));
switch (h->type) {
case UV_FS_EVENT: {
rc = uv_fs_event_getpath(&(handle->fs_event), buffer, &size);
break;
}
case UV_FS_POLL: {
rc = uv_fs_poll_getpath(&(handle->fs_poll), buffer, &size);
break;
}
default: break;
}
if (rc == 0) {
// buffer is not null terminated.
std::string name(buffer, size);
out << "filename: " << name;
}
free(buffer);
}
}
/*******************************************************************************
* Utility function to walk libuv handles.
*******************************************************************************/
void walkHandle(uv_handle_t* h, void* arg) {
std::string type;
std::ostringstream data;
std::ostream* out = reinterpret_cast<std::ostream*>(arg);
uv_any_handle* handle = (uv_any_handle*)h;
// List all the types so we get a compile warning if we've missed one,
// (using default: supresses the compiler warning).
switch (h->type) {
case UV_UNKNOWN_HANDLE: type = "unknown"; break;
case UV_ASYNC: type = "async"; break;
case UV_CHECK: type = "check"; break;
case UV_FS_EVENT: {
type = "fs_event";
reportPath(h, data);
break;
}
case UV_FS_POLL: {
type = "fs_poll";
reportPath(h, data);
break;
}
case UV_HANDLE: type = "handle"; break;
case UV_IDLE: type = "idle"; break;
case UV_NAMED_PIPE: type = "pipe"; break;
case UV_POLL: type = "poll"; break;
case UV_PREPARE: type = "prepare"; break;
case UV_PROCESS: {
type = "process";
data << "pid: " << handle->process.pid;
break;
}
case UV_STREAM: type = "stream"; break;
case UV_TCP: {
type = "tcp";
reportEndpoints(h, data);
break;
}
case UV_TIMER: {
// TODO timeout/due is not actually public however it is present
// in all current versions of libuv. Once uv_timer_get_timeout is
// in a supported level of libuv we should test for it with dlsym
// and use it instead, in case timeout moves in the future.
//
// On Windows in libuv 1.22 and later the `due` member was renamed
// to `timeout` for consistency with the other platforms.
#if defined(_WIN32) && (UV_VERSION_HEX < ((1 << 16) | (22 << 8)))
uint64_t due = handle->timer.due;
#else
uint64_t due = handle->timer.timeout;
#endif
uint64_t now = uv_now(handle->timer.loop);
type = "timer";
data << "repeat: " << uv_timer_get_repeat(&(handle->timer));
if (due > now) {
data << ", timeout in: " << (due - now) << " ms";
} else {
data << ", timeout expired: " << (now - due) << " ms ago";
}
break;
}
case UV_TTY: {
int height, width, rc;
type = "tty";
rc = uv_tty_get_winsize(&(handle->tty), &width, &height);
if (rc == 0) {
data << "width: " << width << ", height: " << height;
}
break;
}
case UV_UDP: {
type = "udp";
reportEndpoints(h, data);
break;
}
case UV_SIGNAL: {
// SIGWINCH is used by libuv so always appears.
// See http://docs.libuv.org/en/v1.x/signal.html
type = "signal";
data << "signum: " << handle->signal.signum
<< " (" << SignoString(handle->signal.signum) << ")";
break;
}
case UV_FILE: type = "file"; break;
// We shouldn't see "max" type
case UV_HANDLE_TYPE_MAX : type = "max"; break;
}
if (h->type == UV_TCP || h->type == UV_UDP
#ifndef _WIN32
|| h->type == UV_NAMED_PIPE
#endif
) {
// These *must* be 0 or libuv will set the buffer sizes to the non-zero
// values they contain.
int send_size = 0;
int recv_size = 0;
if (h->type == UV_TCP || h->type == UV_UDP) {
data << ", ";
}
uv_send_buffer_size(h, &send_size);
uv_recv_buffer_size(h, &recv_size);
data << "send buffer size: " << send_size
<< ", recv buffer size: " << recv_size;
}
if (h->type == UV_TCP || h->type == UV_NAMED_PIPE || h->type == UV_TTY ||
h->type == UV_UDP || h->type == UV_POLL) {
uv_os_fd_t fd_v;
uv_os_fd_t* fd = &fd_v;
int rc = uv_fileno(h, fd);
// uv_os_fd_t is an int on Unix and HANDLE on Windows.
#ifndef _WIN32
if (rc == 0) {
switch (fd_v) {
case 0:
data << ", stdin"; break;
case 1:
data << ", stdout"; break;
case 2:
data << ", stderr"; break;
default:
data << ", file descriptor: " << static_cast<int>(fd_v);
break;
}
}
#endif
}
if (h->type == UV_TCP || h->type == UV_NAMED_PIPE || h->type == UV_TTY) {
data << ", write queue size: "
<< handle->stream.write_queue_size;
data << (uv_is_readable(&handle->stream) ? ", readable" : "")
<< (uv_is_writable(&handle->stream) ? ", writable": "");
}
*out << std::left << "[" << (uv_has_ref(h) ? 'R' : '-')
<< (uv_is_active(h) ? 'A' : '-') << "] " << std::setw(10) << type
<< std::internal << std::setw(2 + 2 * sizeof(void*));
char prev_fill = out->fill('0');
*out << static_cast<void*>(h) << std::left;
out->fill(prev_fill);
*out << " " << std::left << data.str() << std::endl;
}
/*******************************************************************************
* Utility function to print out integer values with commas for readability.
******************************************************************************/
void WriteInteger(std::ostream& out, size_t value) {
int thousandsStack[8]; // Sufficient for max 64-bit number
int stackTop = 0;
int i;
char buf[64];
size_t workingValue = value;
do {
thousandsStack[stackTop++] = workingValue % 1000;
workingValue /= 1000;
} while (workingValue != 0);
for (i = stackTop-1; i >= 0; i--) {
if (i == (stackTop-1)) {
out << thousandsStack[i];
} else {
snprintf(buf, sizeof(buf), "%03u", thousandsStack[i]);
out << buf;
}
if (i > 0) {
out << ",";
}
}
}
/*******************************************************************************
* Utility function to convert a numeric signal number to a string.
*
* This code has been copied from node/src/node.cc function signo_string().
******************************************************************************/
const char *SignoString(int signo) {
#define SIGNO_CASE(e) case e: return #e;
switch (signo) {
#ifdef SIGHUP
SIGNO_CASE(SIGHUP);
#endif
#ifdef SIGINT
SIGNO_CASE(SIGINT);
#endif
#ifdef SIGQUIT
SIGNO_CASE(SIGQUIT);
#endif
#ifdef SIGILL
SIGNO_CASE(SIGILL);
#endif
#ifdef SIGTRAP
SIGNO_CASE(SIGTRAP);
#endif
#ifdef SIGABRT
SIGNO_CASE(SIGABRT);
#endif
#ifdef SIGIOT
# if SIGABRT != SIGIOT
SIGNO_CASE(SIGIOT);
# endif
#endif
#ifdef SIGBUS
SIGNO_CASE(SIGBUS);
#endif
#ifdef SIGFPE
SIGNO_CASE(SIGFPE);
#endif
#ifdef SIGKILL
SIGNO_CASE(SIGKILL);
#endif
#ifdef SIGUSR1
SIGNO_CASE(SIGUSR1);
#endif
#ifdef SIGSEGV
SIGNO_CASE(SIGSEGV);
#endif
#ifdef SIGUSR2
SIGNO_CASE(SIGUSR2);
#endif
#ifdef SIGPIPE
SIGNO_CASE(SIGPIPE);
#endif
#ifdef SIGALRM
SIGNO_CASE(SIGALRM);
#endif
SIGNO_CASE(SIGTERM);
#ifdef SIGCHLD
SIGNO_CASE(SIGCHLD);
#endif
#ifdef SIGSTKFLT
SIGNO_CASE(SIGSTKFLT);
#endif
#ifdef SIGCONT
SIGNO_CASE(SIGCONT);
#endif
#ifdef SIGSTOP
SIGNO_CASE(SIGSTOP);
#endif
#ifdef SIGTSTP
SIGNO_CASE(SIGTSTP);
#endif
#ifdef SIGBREAK
SIGNO_CASE(SIGBREAK);
#endif
#ifdef SIGTTIN
SIGNO_CASE(SIGTTIN);
#endif
#ifdef SIGTTOU
SIGNO_CASE(SIGTTOU);
#endif
#ifdef SIGURG
SIGNO_CASE(SIGURG);
#endif
#ifdef SIGXCPU
SIGNO_CASE(SIGXCPU);
#endif
#ifdef SIGXFSZ
SIGNO_CASE(SIGXFSZ);
#endif
#ifdef SIGVTALRM
SIGNO_CASE(SIGVTALRM);
#endif
#ifdef SIGPROF
SIGNO_CASE(SIGPROF);
#endif
#ifdef SIGWINCH
SIGNO_CASE(SIGWINCH);
#endif
#ifdef SIGIO
SIGNO_CASE(SIGIO);
#endif
#ifdef SIGPOLL
#if SIGPOLL != SIGIO
SIGNO_CASE(SIGPOLL);
#endif
#endif
#ifdef SIGLOST
#if SIGLOST != SIGABRT
SIGNO_CASE(SIGLOST);
#endif
#endif
#ifdef SIGPWR
#if SIGPWR != SIGLOST
SIGNO_CASE(SIGPWR);
#endif
#endif
#ifdef SIGINFO
#if !defined(SIGPWR) || SIGINFO != SIGPWR
SIGNO_CASE(SIGINFO);
#endif
#endif
#ifdef SIGSYS
SIGNO_CASE(SIGSYS);
#endif
default: return "unknown";
}
}
} // namespace nodereport