GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_report.cc Lines: 258 269 95.9 %
Date: 2019-03-02 22:23:06 Branches: 51 78 65.4 %

Line Branch Exec Source
1
2
#include "node_report.h"
3
#include "ares.h"
4
#include "debug_utils.h"
5
#include "http_parser.h"
6
#include "nghttp2/nghttp2ver.h"
7
#include "node_internals.h"
8
#include "node_metadata.h"
9
#include "zlib.h"
10
11
#include <atomic>
12
#include <fstream>
13
14
#ifdef _WIN32
15
#include <Lm.h>
16
#include <Windows.h>
17
#include <dbghelp.h>
18
#include <process.h>
19
#include <psapi.h>
20
#include <tchar.h>
21
#include <cwctype>
22
#else
23
#include <sys/resource.h>
24
// Get the standard printf format macros for C99 stdint types.
25
#ifndef __STDC_FORMAT_MACROS
26
#define __STDC_FORMAT_MACROS
27
#endif
28
#include <cxxabi.h>
29
#include <dlfcn.h>
30
#include <cinttypes>
31
#endif
32
33
#include <fcntl.h>
34
#include <cstring>
35
#include <ctime>
36
#include <iomanip>
37
38
#ifndef _MSC_VER
39
#include <strings.h>
40
#endif
41
42
#ifdef __APPLE__
43
#include <mach-o/dyld.h>
44
#endif
45
46
#ifndef _WIN32
47
extern char** environ;
48
#endif
49
50
constexpr int NANOS_PER_SEC = 1000 * 1000 * 1000;
51
constexpr double SEC_PER_MICROS = 1e-6;
52
53
namespace report {
54
using node::arraysize;
55
using node::Environment;
56
using node::Mutex;
57
using node::NativeSymbolDebuggingContext;
58
using node::PerIsolateOptions;
59
using v8::HeapSpaceStatistics;
60
using v8::HeapStatistics;
61
using v8::Isolate;
62
using v8::Local;
63
using v8::Number;
64
using v8::StackTrace;
65
using v8::String;
66
using v8::V8;
67
using v8::Value;
68
69
// Internal/static function declarations
70
static void WriteNodeReport(Isolate* isolate,
71
                            Environment* env,
72
                            const char* message,
73
                            const char* location,
74
                            const std::string& filename,
75
                            std::ostream& out,
76
                            Local<String> stackstr,
77
                            TIME_TYPE* tm_struct);
78
static void PrintVersionInformation(JSONWriter* writer);
79
static void PrintJavaScriptStack(JSONWriter* writer,
80
                                 Isolate* isolate,
81
                                 Local<String> stackstr,
82
                                 const char* location);
83
static void PrintNativeStack(JSONWriter* writer);
84
#ifndef _WIN32
85
static void PrintResourceUsage(JSONWriter* writer);
86
#endif
87
static void PrintGCStatistics(JSONWriter* writer, Isolate* isolate);
88
static void PrintSystemInformation(JSONWriter* writer);
89
static void PrintLoadedLibraries(JSONWriter* writer);
90
static void PrintComponentVersions(JSONWriter* writer);
91
static void PrintRelease(JSONWriter* writer);
92
static void LocalTime(TIME_TYPE* tm_struct);
93
94
// Global variables
95
static std::atomic_int seq = {0};  // sequence number for report filenames
96
97
// External function to trigger a report, writing to file.
98
// The 'name' parameter is in/out: an input filename is used
99
// if supplied, and the actual filename is returned.
100
6
std::string TriggerNodeReport(Isolate* isolate,
101
                              Environment* env,
102
                              const char* message,
103
                              const char* location,
104
                              std::string name,
105
                              Local<String> stackstr) {
106
6
  std::ostringstream oss;
107
12
  std::string filename;
108
12
  std::shared_ptr<PerIsolateOptions> options;
109
6
  if (env != nullptr) options = env->isolate_data()->options();
110
111
  // Obtain the current time and the pid (platform dependent)
112
  TIME_TYPE tm_struct;
113
6
  LocalTime(&tm_struct);
114
  // Determine the required report filename. In order of priority:
115
  //   1) supplied on API 2) configured on startup 3) default generated
116
6
  if (!name.empty()) {
117
    // Filename was specified as API parameter, use that
118
2
    oss << name;
119

4
  } else if (env != nullptr && options->report_filename.length() > 0) {
120
    // File name was supplied via start-up option, use that
121
1
    oss << options->report_filename;
122
  } else {
123
    // Construct the report filename, with timestamp, pid and sequence number
124
3
    oss << "report";
125
#ifdef _WIN32
126
    oss << "." << std::setfill('0') << std::setw(4) << tm_struct.wYear;
127
    oss << std::setfill('0') << std::setw(2) << tm_struct.wMonth;
128
    oss << std::setfill('0') << std::setw(2) << tm_struct.wDay;
129
    oss << "." << std::setfill('0') << std::setw(2) << tm_struct.wHour;
130
    oss << std::setfill('0') << std::setw(2) << tm_struct.wMinute;
131
    oss << std::setfill('0') << std::setw(2) << tm_struct.wSecond;
132
#else  // UNIX, OSX
133
3
    oss << "." << std::setfill('0') << std::setw(4) << tm_struct.tm_year + 1900;
134
3
    oss << std::setfill('0') << std::setw(2) << tm_struct.tm_mon + 1;
135
3
    oss << std::setfill('0') << std::setw(2) << tm_struct.tm_mday;
136
3
    oss << "." << std::setfill('0') << std::setw(2) << tm_struct.tm_hour;
137
3
    oss << std::setfill('0') << std::setw(2) << tm_struct.tm_min;
138
3
    oss << std::setfill('0') << std::setw(2) << tm_struct.tm_sec;
139
#endif
140
3
    oss << "." << uv_os_getpid();
141
3
    oss << "." << std::setfill('0') << std::setw(3) << ++seq;
142
3
    oss << ".json";
143
  }
144
145
6
  filename = oss.str();
146
  // Open the report file stream for writing. Supports stdout/err,
147
  // user-specified or (default) generated name
148
12
  std::ofstream outfile;
149
  std::ostream* outstream;
150
6
  if (filename == "stdout") {
151
    outstream = &std::cout;
152
6
  } else if (filename == "stderr") {
153
    outstream = &std::cerr;
154
  } else {
155
    // Regular file. Append filename to directory path if one was specified
156

6
    if (env != nullptr && options->report_directory.length() > 0) {
157
5
      std::string pathname = options->report_directory;
158
5
      pathname += PATHSEP;
159
5
      pathname += filename;
160
5
      outfile.open(pathname, std::ios::out | std::ios::binary);
161
    } else {
162
1
      outfile.open(filename, std::ios::out | std::ios::binary);
163
    }
164
    // Check for errors on the file open
165
6
    if (!outfile.is_open()) {
166
      std::cerr << std::endl
167
                << "Failed to open Node.js report file: " << filename;
168
169
      if (env != nullptr && options->report_directory.length() > 0)
170
        std::cerr << " directory: " << options->report_directory;
171
172
      std::cerr << " (errno: " << errno << ")" << std::endl;
173
      return "";
174
    }
175
6
    outstream = &outfile;
176
177
6
    std::cerr << std::endl
178
6
              << "Writing Node.js report to file: " << filename << std::endl;
179
  }
180
181
  WriteNodeReport(isolate, env, message, location, filename, *outstream,
182
6
                  stackstr, &tm_struct);
183
184
  // Do not close stdout/stderr, only close files we opened.
185
6
  if (outfile.is_open()) {
186
6
    outfile.close();
187
  }
188
189
6
  std::cerr << "Node.js report completed" << std::endl;
190
12
  return filename;
191
}
192
193
// External function to trigger a report, writing to a supplied stream.
194
4
void GetNodeReport(Isolate* isolate,
195
                   Environment* env,
196
                   const char* message,
197
                   const char* location,
198
                   Local<String> stackstr,
199
                   std::ostream& out) {
200
  // Obtain the current time and the pid (platform dependent)
201
  TIME_TYPE tm_struct;
202
4
  LocalTime(&tm_struct);
203
  WriteNodeReport(
204
4
      isolate, env, message, location, "", out, stackstr, &tm_struct);
205
4
}
206
207
// Internal function to coordinate and write the various
208
// sections of the report to the supplied stream
209
10
static void WriteNodeReport(Isolate* isolate,
210
                            Environment* env,
211
                            const char* message,
212
                            const char* location,
213
                            const std::string& filename,
214
                            std::ostream& out,
215
                            Local<String> stackstr,
216
                            TIME_TYPE* tm_struct) {
217
10
  uv_pid_t pid = uv_os_getpid();
218
219
  // Save formatting for output stream.
220
10
  std::ios old_state(nullptr);
221
10
  old_state.copyfmt(out);
222
223
  // File stream opened OK, now start printing the report content:
224
  // the title and header information (event, filename, timestamp and pid)
225
226
10
  JSONWriter writer(out);
227
10
  writer.json_start();
228
10
  writer.json_objectstart("header");
229
230
10
  writer.json_keyvalue("event", message);
231
10
  writer.json_keyvalue("location", location);
232
10
  if (!filename.empty())
233
6
    writer.json_keyvalue("filename", filename);
234
  else
235
4
    writer.json_keyvalue("filename", JSONWriter::Null{});
236
237
  // Report dump event and module load date/time stamps
238
  char timebuf[64];
239
#ifdef _WIN32
240
  snprintf(timebuf,
241
           sizeof(timebuf),
242
           "%4d-%02d-%02dT%02d:%02d:%02dZ",
243
           tm_struct->wYear,
244
           tm_struct->wMonth,
245
           tm_struct->wDay,
246
           tm_struct->wHour,
247
           tm_struct->wMinute,
248
           tm_struct->wSecond);
249
  writer.json_keyvalue("dumpEventTime", timebuf);
250
#else  // UNIX, OSX
251
  snprintf(timebuf,
252
           sizeof(timebuf),
253
           "%4d-%02d-%02dT%02d:%02d:%02dZ",
254
           tm_struct->tm_year + 1900,
255
           tm_struct->tm_mon + 1,
256
           tm_struct->tm_mday,
257
           tm_struct->tm_hour,
258
           tm_struct->tm_min,
259
10
           tm_struct->tm_sec);
260
10
  writer.json_keyvalue("dumpEventTime", timebuf);
261
  struct timeval ts;
262
10
  gettimeofday(&ts, nullptr);
263
  writer.json_keyvalue("dumpEventTimeStamp",
264
10
                       std::to_string(ts.tv_sec * 1000 + ts.tv_usec / 1000));
265
#endif
266
  // Report native process ID
267
10
  writer.json_keyvalue("processId", pid);
268
269
  // Report out the command line.
270
10
  if (!node::per_process::cli_options->cmdline.empty()) {
271
10
    writer.json_arraystart("commandLine");
272
42
    for (std::string arg : node::per_process::cli_options->cmdline) {
273
32
      writer.json_element(arg);
274
32
    }
275
10
    writer.json_arrayend();
276
  }
277
278
  // Report Node.js and OS version information
279
10
  PrintVersionInformation(&writer);
280
10
  writer.json_objectend();
281
282
  // Report summary JavaScript stack backtrace
283
10
  PrintJavaScriptStack(&writer, isolate, stackstr, location);
284
285
  // Report native stack backtrace
286
10
  PrintNativeStack(&writer);
287
288
  // Report V8 Heap and Garbage Collector information
289
10
  PrintGCStatistics(&writer, isolate);
290
291
  // Report OS and current thread resource usage
292
#ifndef _WIN32
293
10
  PrintResourceUsage(&writer);
294
#endif
295
296
10
  writer.json_arraystart("libuv");
297
10
  if (env != nullptr) {
298
10
    uv_walk(env->event_loop(), WalkHandle, static_cast<void*>(&writer));
299
300
10
    writer.json_start();
301
10
    writer.json_keyvalue("type", "loop");
302
    writer.json_keyvalue("is_active",
303
10
        static_cast<bool>(uv_loop_alive(env->event_loop())));
304
    writer.json_keyvalue("address",
305
10
        ValueToHexString(reinterpret_cast<int64_t>(env->event_loop())));
306
10
    writer.json_end();
307
  }
308
309
10
  writer.json_arrayend();
310
311
  // Report operating system information
312
10
  PrintSystemInformation(&writer);
313
314
10
  writer.json_objectend();
315
316
  // Restore output stream formatting.
317
10
  out.copyfmt(old_state);
318
10
}
319
320
// Report Node.js version, OS version and machine information.
321
10
static void PrintVersionInformation(JSONWriter* writer) {
322
10
  std::ostringstream buf;
323
  // Report Node version
324
10
  buf << "v" << NODE_VERSION_STRING;
325
10
  writer->json_keyvalue("nodejsVersion", buf.str());
326
10
  buf.str("");
327
328
#ifndef _WIN32
329
  // Report compiler and runtime glibc versions where possible.
330
  const char* (*libc_version)();
331
  *(reinterpret_cast<void**>(&libc_version)) =
332
10
      dlsym(RTLD_DEFAULT, "gnu_get_libc_version");
333
10
  if (libc_version != nullptr)
334
10
    writer->json_keyvalue("glibcVersionRuntime", (*libc_version)());
335
#endif /* _WIN32 */
336
337
#ifdef __GLIBC__
338
10
  buf << __GLIBC__ << "." << __GLIBC_MINOR__;
339
10
  writer->json_keyvalue("glibcVersionCompiler", buf.str());
340
10
  buf.str("");
341
#endif
342
343
  // Report Process word size
344
10
  writer->json_keyvalue("wordSize", sizeof(void*) * 8);
345
10
  writer->json_keyvalue("arch", node::per_process::metadata.arch);
346
10
  writer->json_keyvalue("platform", node::per_process::metadata.platform);
347
348
  // Report deps component versions
349
10
  PrintComponentVersions(writer);
350
351
  // Report release metadata.
352
10
  PrintRelease(writer);
353
354
  // Report operating system and machine information
355
  uv_utsname_t os_info;
356
357
10
  if (uv_os_uname(&os_info) == 0) {
358
10
    writer->json_keyvalue("osName", os_info.sysname);
359
10
    writer->json_keyvalue("osRelease", os_info.release);
360
10
    writer->json_keyvalue("osVersion", os_info.version);
361
10
    writer->json_keyvalue("osMachine", os_info.machine);
362
  }
363
364
  char host[UV_MAXHOSTNAMESIZE];
365
10
  size_t host_size = sizeof(host);
366
367
10
  if (uv_os_gethostname(host, &host_size) == 0)
368
10
    writer->json_keyvalue("host", host);
369
10
}
370
371
// Report the JavaScript stack.
372
10
static void PrintJavaScriptStack(JSONWriter* writer,
373
                                 Isolate* isolate,
374
                                 Local<String> stackstr,
375
                                 const char* location) {
376
10
  writer->json_objectstart("javascriptStack");
377
378
10
  std::string ss;
379

20
  if ((!strcmp(location, "OnFatalError")) ||
380
10
      (!strcmp(location, "OnUserSignal"))) {
381
    ss = "No stack.\nUnavailable.\n";
382
  } else {
383
10
    String::Utf8Value sv(isolate, stackstr);
384
10
    ss = std::string(*sv, sv.length());
385
  }
386
10
  int line = ss.find("\n");
387
10
  if (line == -1) {
388
    writer->json_keyvalue("message", ss);
389
    writer->json_objectend();
390
  } else {
391
10
    std::string l = ss.substr(0, line);
392
10
    writer->json_keyvalue("message", l);
393
10
    writer->json_arraystart("stack");
394
10
    ss = ss.substr(line + 1);
395
10
    line = ss.find("\n");
396
92
    while (line != -1) {
397
72
      l = ss.substr(0, line);
398
360
      l.erase(l.begin(), std::find_if(l.begin(), l.end(), [](int ch) {
399
360
                return !std::iswspace(ch);
400
432
              }));
401
72
      writer->json_element(l);
402
72
      ss = ss.substr(line + 1);
403
72
      line = ss.find("\n");
404
10
    }
405
  }
406
10
  writer->json_arrayend();
407
10
  writer->json_objectend();
408
10
}
409
410
// Report a native stack backtrace
411
10
static void PrintNativeStack(JSONWriter* writer) {
412
10
  auto sym_ctx = NativeSymbolDebuggingContext::New();
413
  void* frames[256];
414
10
  const int size = sym_ctx->GetStackTrace(frames, arraysize(frames));
415
10
  writer->json_arraystart("nativeStack");
416
  int i;
417
80
  for (i = 1; i < size; i++) {
418
70
    void* frame = frames[i];
419
70
    writer->json_start();
420
    writer->json_keyvalue("pc",
421
70
                          ValueToHexString(reinterpret_cast<uintptr_t>(frame)));
422
70
    writer->json_keyvalue("symbol", sym_ctx->LookupSymbol(frame).Display());
423
70
    writer->json_end();
424
  }
425
10
  writer->json_arrayend();
426
10
}
427
428
// Report V8 JavaScript heap information.
429
// This uses the existing V8 HeapStatistics and HeapSpaceStatistics APIs.
430
// The isolate->GetGCStatistics(&heap_stats) internal V8 API could potentially
431
// provide some more useful information - the GC history and the handle counts
432
10
static void PrintGCStatistics(JSONWriter* writer, Isolate* isolate) {
433
10
  HeapStatistics v8_heap_stats;
434
10
  isolate->GetHeapStatistics(&v8_heap_stats);
435
10
  HeapSpaceStatistics v8_heap_space_stats;
436
437
10
  writer->json_objectstart("javascriptHeap");
438
10
  writer->json_keyvalue("totalMemory", v8_heap_stats.total_heap_size());
439
  writer->json_keyvalue("totalCommittedMemory",
440
10
                        v8_heap_stats.total_physical_size());
441
10
  writer->json_keyvalue("usedMemory", v8_heap_stats.used_heap_size());
442
  writer->json_keyvalue("availableMemory",
443
10
                        v8_heap_stats.total_available_size());
444
10
  writer->json_keyvalue("memoryLimit", v8_heap_stats.heap_size_limit());
445
446
10
  writer->json_objectstart("heapSpaces");
447
  // Loop through heap spaces
448
80
  for (size_t i = 0; i < isolate->NumberOfHeapSpaces(); i++) {
449
70
    isolate->GetHeapSpaceStatistics(&v8_heap_space_stats, i);
450
70
    writer->json_objectstart(v8_heap_space_stats.space_name());
451
70
    writer->json_keyvalue("memorySize", v8_heap_space_stats.space_size());
452
    writer->json_keyvalue(
453
        "committedMemory",
454
70
        v8_heap_space_stats.physical_space_size());
455
    writer->json_keyvalue(
456
        "capacity",
457
140
        v8_heap_space_stats.space_used_size() +
458
140
            v8_heap_space_stats.space_available_size());
459
70
    writer->json_keyvalue("used", v8_heap_space_stats.space_used_size());
460
    writer->json_keyvalue(
461
70
        "available", v8_heap_space_stats.space_available_size());
462
70
    writer->json_objectend();
463
  }
464
465
10
  writer->json_objectend();
466
10
  writer->json_objectend();
467
10
}
468
469
#ifndef _WIN32
470
// Report resource usage (Linux/OSX only).
471
10
static void PrintResourceUsage(JSONWriter* writer) {
472
  // Get process uptime in seconds
473
  uint64_t uptime =
474
10
      (uv_hrtime() - node::per_process::node_start_time) / (NANOS_PER_SEC);
475
10
  if (uptime == 0) uptime = 1;  // avoid division by zero.
476
477
  // Process and current thread usage statistics
478
  struct rusage stats;
479
10
  writer->json_objectstart("resourceUsage");
480
10
  if (getrusage(RUSAGE_SELF, &stats) == 0) {
481
    double user_cpu =
482
10
        stats.ru_utime.tv_sec + SEC_PER_MICROS * stats.ru_utime.tv_usec;
483
    double kernel_cpu =
484
10
        stats.ru_stime.tv_sec + SEC_PER_MICROS * stats.ru_stime.tv_usec;
485
10
    writer->json_keyvalue("userCpuSeconds", user_cpu);
486
10
    writer->json_keyvalue("kernelCpuSeconds", kernel_cpu);
487
10
    double cpu_abs = user_cpu + kernel_cpu;
488
10
    double cpu_percentage = (cpu_abs / uptime) * 100.0;
489
10
    writer->json_keyvalue("cpuConsumptionPercent", cpu_percentage);
490
10
    writer->json_keyvalue("maxRss", stats.ru_maxrss * 1024);
491
10
    writer->json_objectstart("pageFaults");
492
10
    writer->json_keyvalue("IORequired", stats.ru_majflt);
493
10
    writer->json_keyvalue("IONotRequired", stats.ru_minflt);
494
10
    writer->json_objectend();
495
10
    writer->json_objectstart("fsActivity");
496
10
    writer->json_keyvalue("reads", stats.ru_inblock);
497
10
    writer->json_keyvalue("writes", stats.ru_oublock);
498
10
    writer->json_objectend();
499
  }
500
10
  writer->json_objectend();
501
#ifdef RUSAGE_THREAD
502
10
  if (getrusage(RUSAGE_THREAD, &stats) == 0) {
503
10
    writer->json_objectstart("uvthreadResourceUsage");
504
    double user_cpu =
505
10
        stats.ru_utime.tv_sec + SEC_PER_MICROS * stats.ru_utime.tv_usec;
506
    double kernel_cpu =
507
10
        stats.ru_stime.tv_sec + SEC_PER_MICROS * stats.ru_stime.tv_usec;
508
10
    writer->json_keyvalue("userCpuSeconds", user_cpu);
509
10
    writer->json_keyvalue("kernelCpuSeconds", kernel_cpu);
510
10
    double cpu_abs = user_cpu + kernel_cpu;
511
10
    double cpu_percentage = (cpu_abs / uptime) * 100.0;
512
10
    writer->json_keyvalue("cpuConsumptionPercent", cpu_percentage);
513
10
    writer->json_objectstart("fsActivity");
514
10
    writer->json_keyvalue("reads", stats.ru_inblock);
515
10
    writer->json_keyvalue("writes", stats.ru_oublock);
516
10
    writer->json_objectend();
517
10
    writer->json_objectend();
518
  }
519
#endif
520
10
}
521
#endif
522
523
// Report operating system information.
524
10
static void PrintSystemInformation(JSONWriter* writer) {
525
#ifndef _WIN32
526
  static struct {
527
    const char* description;
528
    int id;
529
  } rlimit_strings[] = {
530
    {"core_file_size_blocks", RLIMIT_CORE},
531
    {"data_seg_size_kbytes", RLIMIT_DATA},
532
    {"file_size_blocks", RLIMIT_FSIZE},
533
#if !(defined(_AIX) || defined(__sun))
534
    {"max_locked_memory_bytes", RLIMIT_MEMLOCK},
535
#endif
536
#ifndef __sun
537
    {"max_memory_size_kbytes", RLIMIT_RSS},
538
#endif
539
    {"open_files", RLIMIT_NOFILE},
540
    {"stack_size_bytes", RLIMIT_STACK},
541
    {"cpu_time_seconds", RLIMIT_CPU},
542
#ifndef __sun
543
    {"max_user_processes", RLIMIT_NPROC},
544
#endif
545
    {"virtual_memory_kbytes", RLIMIT_AS}
546
  };
547
#endif  // _WIN32
548
10
  writer->json_objectstart("environmentVariables");
549
10
  Mutex::ScopedLock lock(node::per_process::env_var_mutex);
550
#ifdef _WIN32
551
  LPWSTR lpszVariable;
552
  LPWCH lpvEnv;
553
554
  // Get pointer to the environment block
555
  lpvEnv = GetEnvironmentStringsW();
556
  if (lpvEnv != nullptr) {
557
    // Variable strings are separated by null bytes,
558
    // and the block is terminated by a null byte.
559
    lpszVariable = reinterpret_cast<LPWSTR>(lpvEnv);
560
    while (*lpszVariable) {
561
      DWORD size = WideCharToMultiByte(
562
          CP_UTF8, 0, lpszVariable, -1, nullptr, 0, nullptr, nullptr);
563
      char* str = new char[size];
564
      WideCharToMultiByte(
565
          CP_UTF8, 0, lpszVariable, -1, str, size, nullptr, nullptr);
566
      std::string env(str);
567
      int sep = env.rfind("=");
568
      std::string key = env.substr(0, sep);
569
      std::string value = env.substr(sep + 1);
570
      writer->json_keyvalue(key, value);
571
      lpszVariable += lstrlenW(lpszVariable) + 1;
572
    }
573
    FreeEnvironmentStringsW(lpvEnv);
574
  }
575
  writer->json_objectend();
576
#else
577
20
  std::string pair;
578
640
  for (char** env = environ; *env != nullptr; ++env) {
579
630
    std::string pair(*env);
580
630
    int separator = pair.find('=');
581
1260
    std::string key = pair.substr(0, separator);
582
1260
    std::string str = pair.substr(separator + 1);
583
630
    writer->json_keyvalue(key, str);
584
630
  }
585
10
  writer->json_objectend();
586
587
10
  writer->json_objectstart("userLimits");
588
  struct rlimit limit;
589
20
  std::string soft, hard;
590
591
110
  for (size_t i = 0; i < arraysize(rlimit_strings); i++) {
592
100
    if (getrlimit(rlimit_strings[i].id, &limit) == 0) {
593
100
      writer->json_objectstart(rlimit_strings[i].description);
594
595
100
      if (limit.rlim_cur == RLIM_INFINITY)
596
60
        writer->json_keyvalue("soft", "unlimited");
597
      else
598
40
        writer->json_keyvalue("soft", limit.rlim_cur);
599
600
100
      if (limit.rlim_max == RLIM_INFINITY)
601
70
        writer->json_keyvalue("hard", "unlimited");
602
      else
603
30
        writer->json_keyvalue("hard", limit.rlim_max);
604
605
100
      writer->json_objectend();
606
    }
607
  }
608
10
  writer->json_objectend();
609
#endif
610
611
20
  PrintLoadedLibraries(writer);
612
10
}
613
614
// Report a list of loaded native libraries.
615
10
static void PrintLoadedLibraries(JSONWriter* writer) {
616
10
  writer->json_arraystart("sharedObjects");
617
  std::vector<std::string> modules =
618
10
      NativeSymbolDebuggingContext::GetLoadedLibraries();
619
10
  for (auto const& module_name : modules) writer->json_element(module_name);
620
10
  writer->json_arrayend();
621
10
}
622
623
// Obtain and report the node and subcomponent version strings.
624
10
static void PrintComponentVersions(JSONWriter* writer) {
625
10
  std::stringstream buf;
626
627
10
  writer->json_objectstart("componentVersions");
628
629
#define V(key)                                                                 \
630
  writer->json_keyvalue(#key, node::per_process::metadata.versions.key);
631
10
  NODE_VERSIONS_KEYS(V)
632
#undef V
633
634
10
  writer->json_objectend();
635
10
}
636
637
// Report runtime release information.
638
10
static void PrintRelease(JSONWriter* writer) {
639
10
  writer->json_objectstart("release");
640
10
  writer->json_keyvalue("name", node::per_process::metadata.release.name);
641
#if NODE_VERSION_IS_LTS
642
  writer->json_keyvalue("lts", node::per_process::metadata.release.lts);
643
#endif
644
645
#ifdef NODE_HAS_RELEASE_URLS
646
  writer->json_keyvalue("headersUrl",
647
                        node::per_process::metadata.release.headers_url);
648
  writer->json_keyvalue("sourceUrl",
649
                        node::per_process::metadata.release.source_url);
650
#ifdef _WIN32
651
  writer->json_keyvalue("libUrl", node::per_process::metadata.release.lib_url);
652
#endif  // _WIN32
653
#endif  // NODE_HAS_RELEASE_URLS
654
655
10
  writer->json_objectend();
656
10
}
657
658
10
static void LocalTime(TIME_TYPE* tm_struct) {
659
#ifdef _WIN32
660
  GetLocalTime(tm_struct);
661
#else  // UNIX, OSX
662
  struct timeval time_val;
663
10
  gettimeofday(&time_val, nullptr);
664
10
  localtime_r(&time_val.tv_sec, tm_struct);
665
#endif
666
10
}
667
668

12876
}  // namespace report