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: 268 288 93.1 %
Date: 2019-02-13 22:28:58 Branches: 48 82 58.5 %

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 <inttypes.h>
31
#endif
32
33
#include <fcntl.h>
34
#include <string.h>
35
#include <time.h>
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
namespace report {
51
using node::arraysize;
52
using node::Environment;
53
using node::Mutex;
54
using node::NativeSymbolDebuggingContext;
55
using node::PerIsolateOptions;
56
using v8::HeapSpaceStatistics;
57
using v8::HeapStatistics;
58
using v8::Isolate;
59
using v8::Local;
60
using v8::Number;
61
using v8::StackTrace;
62
using v8::String;
63
using v8::V8;
64
using v8::Value;
65
66
// Internal/static function declarations
67
static void WriteNodeReport(Isolate* isolate,
68
                            Environment* env,
69
                            const char* message,
70
                            const char* location,
71
                            const std::string& filename,
72
                            std::ostream& out,
73
                            Local<String> stackstr,
74
                            TIME_TYPE* time);
75
static void PrintVersionInformation(JSONWriter* writer);
76
static void PrintJavaScriptStack(JSONWriter* writer,
77
                                 Isolate* isolate,
78
                                 Local<String> stackstr,
79
                                 const char* location);
80
static void PrintNativeStack(JSONWriter* writer);
81
#ifndef _WIN32
82
static void PrintResourceUsage(JSONWriter* writer);
83
#endif
84
static void PrintGCStatistics(JSONWriter* writer, Isolate* isolate);
85
static void PrintSystemInformation(JSONWriter* writer);
86
static void PrintLoadedLibraries(JSONWriter* writer);
87
static void PrintComponentVersions(JSONWriter* writer);
88
static void PrintRelease(JSONWriter* writer);
89
static void LocalTime(TIME_TYPE* tm_struct);
90
91
// Global variables
92
static std::atomic_int seq = {0};  // sequence number for report filenames
93
94
// External function to trigger a report, writing to file.
95
// The 'name' parameter is in/out: an input filename is used
96
// if supplied, and the actual filename is returned.
97
4
std::string TriggerNodeReport(Isolate* isolate,
98
                              Environment* env,
99
                              const char* message,
100
                              const char* location,
101
                              std::string name,
102
                              Local<String> stackstr) {
103
4
  std::ostringstream oss;
104
8
  std::string filename;
105
8
  std::shared_ptr<PerIsolateOptions> options;
106
4
  if (env != nullptr) options = env->isolate_data()->options();
107
108
  // Obtain the current time and the pid (platform dependent)
109
  TIME_TYPE tm_struct;
110
4
  LocalTime(&tm_struct);
111
4
  uv_pid_t pid = uv_os_getpid();
112
  // Determine the required report filename. In order of priority:
113
  //   1) supplied on API 2) configured on startup 3) default generated
114
4
  if (!name.empty()) {
115
    // Filename was specified as API parameter, use that
116
    oss << name;
117

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

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

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

12942
}  // namespace report