11cb0ef41Sopenharmony_ci#include "node_report.h"
21cb0ef41Sopenharmony_ci#include "debug_utils-inl.h"
31cb0ef41Sopenharmony_ci#include "diagnosticfilename-inl.h"
41cb0ef41Sopenharmony_ci#include "env-inl.h"
51cb0ef41Sopenharmony_ci#include "json_utils.h"
61cb0ef41Sopenharmony_ci#include "node_internals.h"
71cb0ef41Sopenharmony_ci#include "node_metadata.h"
81cb0ef41Sopenharmony_ci#include "node_mutex.h"
91cb0ef41Sopenharmony_ci#include "node_worker.h"
101cb0ef41Sopenharmony_ci#include "util.h"
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci#ifdef _WIN32
131cb0ef41Sopenharmony_ci#include <Windows.h>
141cb0ef41Sopenharmony_ci#else  // !_WIN32
151cb0ef41Sopenharmony_ci#include <cxxabi.h>
161cb0ef41Sopenharmony_ci#include <sys/resource.h>
171cb0ef41Sopenharmony_ci#include <dlfcn.h>
181cb0ef41Sopenharmony_ci#endif
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci#include <iostream>
211cb0ef41Sopenharmony_ci#include <cstring>
221cb0ef41Sopenharmony_ci#include <ctime>
231cb0ef41Sopenharmony_ci#include <cwctype>
241cb0ef41Sopenharmony_ci#include <fstream>
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciconstexpr int NODE_REPORT_VERSION = 3;
271cb0ef41Sopenharmony_ciconstexpr int NANOS_PER_SEC = 1000 * 1000 * 1000;
281cb0ef41Sopenharmony_ciconstexpr double SEC_PER_MICROS = 1e-6;
291cb0ef41Sopenharmony_ciconstexpr int MAX_FRAME_COUNT = 10;
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_cinamespace node {
321cb0ef41Sopenharmony_ciusing node::worker::Worker;
331cb0ef41Sopenharmony_ciusing v8::Array;
341cb0ef41Sopenharmony_ciusing v8::Context;
351cb0ef41Sopenharmony_ciusing v8::HandleScope;
361cb0ef41Sopenharmony_ciusing v8::HeapSpaceStatistics;
371cb0ef41Sopenharmony_ciusing v8::HeapStatistics;
381cb0ef41Sopenharmony_ciusing v8::Isolate;
391cb0ef41Sopenharmony_ciusing v8::Just;
401cb0ef41Sopenharmony_ciusing v8::Local;
411cb0ef41Sopenharmony_ciusing v8::Maybe;
421cb0ef41Sopenharmony_ciusing v8::MaybeLocal;
431cb0ef41Sopenharmony_ciusing v8::Nothing;
441cb0ef41Sopenharmony_ciusing v8::Object;
451cb0ef41Sopenharmony_ciusing v8::RegisterState;
461cb0ef41Sopenharmony_ciusing v8::SampleInfo;
471cb0ef41Sopenharmony_ciusing v8::StackFrame;
481cb0ef41Sopenharmony_ciusing v8::StackTrace;
491cb0ef41Sopenharmony_ciusing v8::String;
501cb0ef41Sopenharmony_ciusing v8::TryCatch;
511cb0ef41Sopenharmony_ciusing v8::V8;
521cb0ef41Sopenharmony_ciusing v8::Value;
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_cinamespace report {
551cb0ef41Sopenharmony_ci// Internal/static function declarations
561cb0ef41Sopenharmony_cistatic void WriteNodeReport(Isolate* isolate,
571cb0ef41Sopenharmony_ci                            Environment* env,
581cb0ef41Sopenharmony_ci                            const char* message,
591cb0ef41Sopenharmony_ci                            const char* trigger,
601cb0ef41Sopenharmony_ci                            const std::string& filename,
611cb0ef41Sopenharmony_ci                            std::ostream& out,
621cb0ef41Sopenharmony_ci                            Local<Value> error,
631cb0ef41Sopenharmony_ci                            bool compact);
641cb0ef41Sopenharmony_cistatic void PrintVersionInformation(JSONWriter* writer);
651cb0ef41Sopenharmony_cistatic void PrintJavaScriptErrorStack(JSONWriter* writer,
661cb0ef41Sopenharmony_ci                                      Isolate* isolate,
671cb0ef41Sopenharmony_ci                                      Local<Value> error,
681cb0ef41Sopenharmony_ci                                      const char* trigger);
691cb0ef41Sopenharmony_cistatic void PrintEmptyJavaScriptStack(JSONWriter* writer);
701cb0ef41Sopenharmony_cistatic void PrintJavaScriptStack(JSONWriter* writer,
711cb0ef41Sopenharmony_ci                                 Isolate* isolate,
721cb0ef41Sopenharmony_ci                                 const char* trigger);
731cb0ef41Sopenharmony_cistatic void PrintJavaScriptErrorProperties(JSONWriter* writer,
741cb0ef41Sopenharmony_ci                                           Isolate* isolate,
751cb0ef41Sopenharmony_ci                                           Local<Value> error);
761cb0ef41Sopenharmony_cistatic void PrintNativeStack(JSONWriter* writer);
771cb0ef41Sopenharmony_cistatic void PrintResourceUsage(JSONWriter* writer);
781cb0ef41Sopenharmony_cistatic void PrintGCStatistics(JSONWriter* writer, Isolate* isolate);
791cb0ef41Sopenharmony_cistatic void PrintSystemInformation(JSONWriter* writer);
801cb0ef41Sopenharmony_cistatic void PrintLoadedLibraries(JSONWriter* writer);
811cb0ef41Sopenharmony_cistatic void PrintComponentVersions(JSONWriter* writer);
821cb0ef41Sopenharmony_cistatic void PrintRelease(JSONWriter* writer);
831cb0ef41Sopenharmony_cistatic void PrintCpuInfo(JSONWriter* writer);
841cb0ef41Sopenharmony_cistatic void PrintNetworkInterfaceInfo(JSONWriter* writer);
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci// Internal function to coordinate and write the various
871cb0ef41Sopenharmony_ci// sections of the report to the supplied stream
881cb0ef41Sopenharmony_cistatic void WriteNodeReport(Isolate* isolate,
891cb0ef41Sopenharmony_ci                            Environment* env,
901cb0ef41Sopenharmony_ci                            const char* message,
911cb0ef41Sopenharmony_ci                            const char* trigger,
921cb0ef41Sopenharmony_ci                            const std::string& filename,
931cb0ef41Sopenharmony_ci                            std::ostream& out,
941cb0ef41Sopenharmony_ci                            Local<Value> error,
951cb0ef41Sopenharmony_ci                            bool compact) {
961cb0ef41Sopenharmony_ci  // Obtain the current time and the pid.
971cb0ef41Sopenharmony_ci  TIME_TYPE tm_struct;
981cb0ef41Sopenharmony_ci  DiagnosticFilename::LocalTime(&tm_struct);
991cb0ef41Sopenharmony_ci  uv_pid_t pid = uv_os_getpid();
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci  // Save formatting for output stream.
1021cb0ef41Sopenharmony_ci  std::ios old_state(nullptr);
1031cb0ef41Sopenharmony_ci  old_state.copyfmt(out);
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci  // File stream opened OK, now start printing the report content:
1061cb0ef41Sopenharmony_ci  // the title and header information (event, filename, timestamp and pid)
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci  JSONWriter writer(out, compact);
1091cb0ef41Sopenharmony_ci  writer.json_start();
1101cb0ef41Sopenharmony_ci  writer.json_objectstart("header");
1111cb0ef41Sopenharmony_ci  writer.json_keyvalue("reportVersion", NODE_REPORT_VERSION);
1121cb0ef41Sopenharmony_ci  writer.json_keyvalue("event", message);
1131cb0ef41Sopenharmony_ci  writer.json_keyvalue("trigger", trigger);
1141cb0ef41Sopenharmony_ci  if (!filename.empty())
1151cb0ef41Sopenharmony_ci    writer.json_keyvalue("filename", filename);
1161cb0ef41Sopenharmony_ci  else
1171cb0ef41Sopenharmony_ci    writer.json_keyvalue("filename", JSONWriter::Null{});
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci  // Report dump event and module load date/time stamps
1201cb0ef41Sopenharmony_ci  char timebuf[64];
1211cb0ef41Sopenharmony_ci#ifdef _WIN32
1221cb0ef41Sopenharmony_ci  snprintf(timebuf,
1231cb0ef41Sopenharmony_ci           sizeof(timebuf),
1241cb0ef41Sopenharmony_ci           "%4d-%02d-%02dT%02d:%02d:%02dZ",
1251cb0ef41Sopenharmony_ci           tm_struct.wYear,
1261cb0ef41Sopenharmony_ci           tm_struct.wMonth,
1271cb0ef41Sopenharmony_ci           tm_struct.wDay,
1281cb0ef41Sopenharmony_ci           tm_struct.wHour,
1291cb0ef41Sopenharmony_ci           tm_struct.wMinute,
1301cb0ef41Sopenharmony_ci           tm_struct.wSecond);
1311cb0ef41Sopenharmony_ci  writer.json_keyvalue("dumpEventTime", timebuf);
1321cb0ef41Sopenharmony_ci#else  // UNIX, OSX
1331cb0ef41Sopenharmony_ci  snprintf(timebuf,
1341cb0ef41Sopenharmony_ci           sizeof(timebuf),
1351cb0ef41Sopenharmony_ci           "%4d-%02d-%02dT%02d:%02d:%02dZ",
1361cb0ef41Sopenharmony_ci           tm_struct.tm_year + 1900,
1371cb0ef41Sopenharmony_ci           tm_struct.tm_mon + 1,
1381cb0ef41Sopenharmony_ci           tm_struct.tm_mday,
1391cb0ef41Sopenharmony_ci           tm_struct.tm_hour,
1401cb0ef41Sopenharmony_ci           tm_struct.tm_min,
1411cb0ef41Sopenharmony_ci           tm_struct.tm_sec);
1421cb0ef41Sopenharmony_ci  writer.json_keyvalue("dumpEventTime", timebuf);
1431cb0ef41Sopenharmony_ci#endif
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci  uv_timeval64_t ts;
1461cb0ef41Sopenharmony_ci  if (uv_gettimeofday(&ts) == 0) {
1471cb0ef41Sopenharmony_ci    writer.json_keyvalue("dumpEventTimeStamp",
1481cb0ef41Sopenharmony_ci                         std::to_string(ts.tv_sec * 1000 + ts.tv_usec / 1000));
1491cb0ef41Sopenharmony_ci  }
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci  // Report native process ID
1521cb0ef41Sopenharmony_ci  writer.json_keyvalue("processId", pid);
1531cb0ef41Sopenharmony_ci  if (env != nullptr)
1541cb0ef41Sopenharmony_ci    writer.json_keyvalue("threadId", env->thread_id());
1551cb0ef41Sopenharmony_ci  else
1561cb0ef41Sopenharmony_ci    writer.json_keyvalue("threadId", JSONWriter::Null{});
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci  {
1591cb0ef41Sopenharmony_ci    // Report the process cwd.
1601cb0ef41Sopenharmony_ci    char buf[PATH_MAX_BYTES];
1611cb0ef41Sopenharmony_ci    size_t cwd_size = sizeof(buf);
1621cb0ef41Sopenharmony_ci    if (uv_cwd(buf, &cwd_size) == 0)
1631cb0ef41Sopenharmony_ci      writer.json_keyvalue("cwd", buf);
1641cb0ef41Sopenharmony_ci  }
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_ci  // Report out the command line.
1671cb0ef41Sopenharmony_ci  if (!per_process::cli_options->cmdline.empty()) {
1681cb0ef41Sopenharmony_ci    writer.json_arraystart("commandLine");
1691cb0ef41Sopenharmony_ci    for (const std::string& arg : per_process::cli_options->cmdline) {
1701cb0ef41Sopenharmony_ci      writer.json_element(arg);
1711cb0ef41Sopenharmony_ci    }
1721cb0ef41Sopenharmony_ci    writer.json_arrayend();
1731cb0ef41Sopenharmony_ci  }
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci  // Report Node.js and OS version information
1761cb0ef41Sopenharmony_ci  PrintVersionInformation(&writer);
1771cb0ef41Sopenharmony_ci  writer.json_objectend();
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ci  if (isolate != nullptr) {
1801cb0ef41Sopenharmony_ci    writer.json_objectstart("javascriptStack");
1811cb0ef41Sopenharmony_ci    // Report summary JavaScript error stack backtrace
1821cb0ef41Sopenharmony_ci    PrintJavaScriptErrorStack(&writer, isolate, error, trigger);
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci    writer.json_objectend();  // the end of 'javascriptStack'
1851cb0ef41Sopenharmony_ci
1861cb0ef41Sopenharmony_ci    // Report V8 Heap and Garbage Collector information
1871cb0ef41Sopenharmony_ci    PrintGCStatistics(&writer, isolate);
1881cb0ef41Sopenharmony_ci  } else {
1891cb0ef41Sopenharmony_ci    writer.json_objectstart("javascriptStack");
1901cb0ef41Sopenharmony_ci    PrintEmptyJavaScriptStack(&writer);
1911cb0ef41Sopenharmony_ci    writer.json_objectend();  // the end of 'javascriptStack'
1921cb0ef41Sopenharmony_ci  }
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_ci  // Report native stack backtrace
1951cb0ef41Sopenharmony_ci  PrintNativeStack(&writer);
1961cb0ef41Sopenharmony_ci
1971cb0ef41Sopenharmony_ci  // Report OS and current thread resource usage
1981cb0ef41Sopenharmony_ci  PrintResourceUsage(&writer);
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci  writer.json_arraystart("libuv");
2011cb0ef41Sopenharmony_ci  if (env != nullptr) {
2021cb0ef41Sopenharmony_ci    uv_walk(env->event_loop(), WalkHandle, static_cast<void*>(&writer));
2031cb0ef41Sopenharmony_ci
2041cb0ef41Sopenharmony_ci    writer.json_start();
2051cb0ef41Sopenharmony_ci    writer.json_keyvalue("type", "loop");
2061cb0ef41Sopenharmony_ci    writer.json_keyvalue("is_active",
2071cb0ef41Sopenharmony_ci        static_cast<bool>(uv_loop_alive(env->event_loop())));
2081cb0ef41Sopenharmony_ci    writer.json_keyvalue("address",
2091cb0ef41Sopenharmony_ci        ValueToHexString(reinterpret_cast<int64_t>(env->event_loop())));
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ci    // Report Event loop idle time
2121cb0ef41Sopenharmony_ci    uint64_t idle_time = uv_metrics_idle_time(env->event_loop());
2131cb0ef41Sopenharmony_ci    writer.json_keyvalue("loopIdleTimeSeconds", 1.0 * idle_time / 1e9);
2141cb0ef41Sopenharmony_ci    writer.json_end();
2151cb0ef41Sopenharmony_ci  }
2161cb0ef41Sopenharmony_ci
2171cb0ef41Sopenharmony_ci  writer.json_arrayend();
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_ci  writer.json_arraystart("workers");
2201cb0ef41Sopenharmony_ci  if (env != nullptr) {
2211cb0ef41Sopenharmony_ci    Mutex workers_mutex;
2221cb0ef41Sopenharmony_ci    ConditionVariable notify;
2231cb0ef41Sopenharmony_ci    std::vector<std::string> worker_infos;
2241cb0ef41Sopenharmony_ci    size_t expected_results = 0;
2251cb0ef41Sopenharmony_ci
2261cb0ef41Sopenharmony_ci    env->ForEachWorker([&](Worker* w) {
2271cb0ef41Sopenharmony_ci      expected_results += w->RequestInterrupt([&](Environment* env) {
2281cb0ef41Sopenharmony_ci        std::ostringstream os;
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_ci        GetNodeReport(
2311cb0ef41Sopenharmony_ci            env, "Worker thread subreport", trigger, Local<Value>(), os);
2321cb0ef41Sopenharmony_ci
2331cb0ef41Sopenharmony_ci        Mutex::ScopedLock lock(workers_mutex);
2341cb0ef41Sopenharmony_ci        worker_infos.emplace_back(os.str());
2351cb0ef41Sopenharmony_ci        notify.Signal(lock);
2361cb0ef41Sopenharmony_ci      });
2371cb0ef41Sopenharmony_ci    });
2381cb0ef41Sopenharmony_ci
2391cb0ef41Sopenharmony_ci    Mutex::ScopedLock lock(workers_mutex);
2401cb0ef41Sopenharmony_ci    worker_infos.reserve(expected_results);
2411cb0ef41Sopenharmony_ci    while (worker_infos.size() < expected_results)
2421cb0ef41Sopenharmony_ci      notify.Wait(lock);
2431cb0ef41Sopenharmony_ci    for (const std::string& worker_info : worker_infos)
2441cb0ef41Sopenharmony_ci      writer.json_element(JSONWriter::ForeignJSON { worker_info });
2451cb0ef41Sopenharmony_ci  }
2461cb0ef41Sopenharmony_ci  writer.json_arrayend();
2471cb0ef41Sopenharmony_ci
2481cb0ef41Sopenharmony_ci  // Report operating system information
2491cb0ef41Sopenharmony_ci  PrintSystemInformation(&writer);
2501cb0ef41Sopenharmony_ci
2511cb0ef41Sopenharmony_ci  writer.json_objectend();
2521cb0ef41Sopenharmony_ci
2531cb0ef41Sopenharmony_ci  // Restore output stream formatting.
2541cb0ef41Sopenharmony_ci  out.copyfmt(old_state);
2551cb0ef41Sopenharmony_ci}
2561cb0ef41Sopenharmony_ci
2571cb0ef41Sopenharmony_ci// Report Node.js version, OS version and machine information.
2581cb0ef41Sopenharmony_cistatic void PrintVersionInformation(JSONWriter* writer) {
2591cb0ef41Sopenharmony_ci  std::ostringstream buf;
2601cb0ef41Sopenharmony_ci  // Report Node version
2611cb0ef41Sopenharmony_ci  buf << "v" << NODE_VERSION_STRING;
2621cb0ef41Sopenharmony_ci  writer->json_keyvalue("nodejsVersion", buf.str());
2631cb0ef41Sopenharmony_ci  buf.str("");
2641cb0ef41Sopenharmony_ci
2651cb0ef41Sopenharmony_ci#ifndef _WIN32
2661cb0ef41Sopenharmony_ci  // Report compiler and runtime glibc versions where possible.
2671cb0ef41Sopenharmony_ci  const char* (*libc_version)();
2681cb0ef41Sopenharmony_ci  *(reinterpret_cast<void**>(&libc_version)) =
2691cb0ef41Sopenharmony_ci      dlsym(RTLD_DEFAULT, "gnu_get_libc_version");
2701cb0ef41Sopenharmony_ci  if (libc_version != nullptr)
2711cb0ef41Sopenharmony_ci    writer->json_keyvalue("glibcVersionRuntime", (*libc_version)());
2721cb0ef41Sopenharmony_ci#endif /* _WIN32 */
2731cb0ef41Sopenharmony_ci
2741cb0ef41Sopenharmony_ci#ifdef __GLIBC__
2751cb0ef41Sopenharmony_ci  buf << __GLIBC__ << "." << __GLIBC_MINOR__;
2761cb0ef41Sopenharmony_ci  writer->json_keyvalue("glibcVersionCompiler", buf.str());
2771cb0ef41Sopenharmony_ci  buf.str("");
2781cb0ef41Sopenharmony_ci#endif
2791cb0ef41Sopenharmony_ci
2801cb0ef41Sopenharmony_ci  // Report Process word size
2811cb0ef41Sopenharmony_ci  writer->json_keyvalue("wordSize", sizeof(void*) * 8);
2821cb0ef41Sopenharmony_ci  writer->json_keyvalue("arch", per_process::metadata.arch);
2831cb0ef41Sopenharmony_ci  writer->json_keyvalue("platform", per_process::metadata.platform);
2841cb0ef41Sopenharmony_ci
2851cb0ef41Sopenharmony_ci  // Report deps component versions
2861cb0ef41Sopenharmony_ci  PrintComponentVersions(writer);
2871cb0ef41Sopenharmony_ci
2881cb0ef41Sopenharmony_ci  // Report release metadata.
2891cb0ef41Sopenharmony_ci  PrintRelease(writer);
2901cb0ef41Sopenharmony_ci
2911cb0ef41Sopenharmony_ci  // Report operating system and machine information
2921cb0ef41Sopenharmony_ci  uv_utsname_t os_info;
2931cb0ef41Sopenharmony_ci
2941cb0ef41Sopenharmony_ci  if (uv_os_uname(&os_info) == 0) {
2951cb0ef41Sopenharmony_ci    writer->json_keyvalue("osName", os_info.sysname);
2961cb0ef41Sopenharmony_ci    writer->json_keyvalue("osRelease", os_info.release);
2971cb0ef41Sopenharmony_ci    writer->json_keyvalue("osVersion", os_info.version);
2981cb0ef41Sopenharmony_ci    writer->json_keyvalue("osMachine", os_info.machine);
2991cb0ef41Sopenharmony_ci  }
3001cb0ef41Sopenharmony_ci
3011cb0ef41Sopenharmony_ci  PrintCpuInfo(writer);
3021cb0ef41Sopenharmony_ci  PrintNetworkInterfaceInfo(writer);
3031cb0ef41Sopenharmony_ci
3041cb0ef41Sopenharmony_ci  char host[UV_MAXHOSTNAMESIZE];
3051cb0ef41Sopenharmony_ci  size_t host_size = sizeof(host);
3061cb0ef41Sopenharmony_ci
3071cb0ef41Sopenharmony_ci  if (uv_os_gethostname(host, &host_size) == 0)
3081cb0ef41Sopenharmony_ci    writer->json_keyvalue("host", host);
3091cb0ef41Sopenharmony_ci}
3101cb0ef41Sopenharmony_ci
3111cb0ef41Sopenharmony_ci// Report CPU info
3121cb0ef41Sopenharmony_cistatic void PrintCpuInfo(JSONWriter* writer) {
3131cb0ef41Sopenharmony_ci  uv_cpu_info_t* cpu_info;
3141cb0ef41Sopenharmony_ci  int count;
3151cb0ef41Sopenharmony_ci  if (uv_cpu_info(&cpu_info, &count) == 0) {
3161cb0ef41Sopenharmony_ci    writer->json_arraystart("cpus");
3171cb0ef41Sopenharmony_ci    for (int i = 0; i < count; i++) {
3181cb0ef41Sopenharmony_ci      writer->json_start();
3191cb0ef41Sopenharmony_ci      writer->json_keyvalue("model", cpu_info[i].model);
3201cb0ef41Sopenharmony_ci      writer->json_keyvalue("speed", cpu_info[i].speed);
3211cb0ef41Sopenharmony_ci      writer->json_keyvalue("user", cpu_info[i].cpu_times.user);
3221cb0ef41Sopenharmony_ci      writer->json_keyvalue("nice", cpu_info[i].cpu_times.nice);
3231cb0ef41Sopenharmony_ci      writer->json_keyvalue("sys", cpu_info[i].cpu_times.sys);
3241cb0ef41Sopenharmony_ci      writer->json_keyvalue("idle", cpu_info[i].cpu_times.idle);
3251cb0ef41Sopenharmony_ci      writer->json_keyvalue("irq", cpu_info[i].cpu_times.irq);
3261cb0ef41Sopenharmony_ci      writer->json_end();
3271cb0ef41Sopenharmony_ci    }
3281cb0ef41Sopenharmony_ci    writer->json_arrayend();
3291cb0ef41Sopenharmony_ci    uv_free_cpu_info(cpu_info, count);
3301cb0ef41Sopenharmony_ci  }
3311cb0ef41Sopenharmony_ci}
3321cb0ef41Sopenharmony_ci
3331cb0ef41Sopenharmony_cistatic void PrintNetworkInterfaceInfo(JSONWriter* writer) {
3341cb0ef41Sopenharmony_ci  uv_interface_address_t* interfaces;
3351cb0ef41Sopenharmony_ci  char ip[INET6_ADDRSTRLEN];
3361cb0ef41Sopenharmony_ci  char netmask[INET6_ADDRSTRLEN];
3371cb0ef41Sopenharmony_ci  char mac[18];
3381cb0ef41Sopenharmony_ci  int count;
3391cb0ef41Sopenharmony_ci
3401cb0ef41Sopenharmony_ci  if (uv_interface_addresses(&interfaces, &count) == 0) {
3411cb0ef41Sopenharmony_ci    writer->json_arraystart("networkInterfaces");
3421cb0ef41Sopenharmony_ci
3431cb0ef41Sopenharmony_ci    for (int i = 0; i < count; i++) {
3441cb0ef41Sopenharmony_ci      writer->json_start();
3451cb0ef41Sopenharmony_ci      writer->json_keyvalue("name", interfaces[i].name);
3461cb0ef41Sopenharmony_ci      writer->json_keyvalue("internal", !!interfaces[i].is_internal);
3471cb0ef41Sopenharmony_ci      snprintf(mac,
3481cb0ef41Sopenharmony_ci               sizeof(mac),
3491cb0ef41Sopenharmony_ci               "%02x:%02x:%02x:%02x:%02x:%02x",
3501cb0ef41Sopenharmony_ci               static_cast<unsigned char>(interfaces[i].phys_addr[0]),
3511cb0ef41Sopenharmony_ci               static_cast<unsigned char>(interfaces[i].phys_addr[1]),
3521cb0ef41Sopenharmony_ci               static_cast<unsigned char>(interfaces[i].phys_addr[2]),
3531cb0ef41Sopenharmony_ci               static_cast<unsigned char>(interfaces[i].phys_addr[3]),
3541cb0ef41Sopenharmony_ci               static_cast<unsigned char>(interfaces[i].phys_addr[4]),
3551cb0ef41Sopenharmony_ci               static_cast<unsigned char>(interfaces[i].phys_addr[5]));
3561cb0ef41Sopenharmony_ci      writer->json_keyvalue("mac", mac);
3571cb0ef41Sopenharmony_ci
3581cb0ef41Sopenharmony_ci      if (interfaces[i].address.address4.sin_family == AF_INET) {
3591cb0ef41Sopenharmony_ci        uv_ip4_name(&interfaces[i].address.address4, ip, sizeof(ip));
3601cb0ef41Sopenharmony_ci        uv_ip4_name(&interfaces[i].netmask.netmask4, netmask, sizeof(netmask));
3611cb0ef41Sopenharmony_ci        writer->json_keyvalue("address", ip);
3621cb0ef41Sopenharmony_ci        writer->json_keyvalue("netmask", netmask);
3631cb0ef41Sopenharmony_ci        writer->json_keyvalue("family", "IPv4");
3641cb0ef41Sopenharmony_ci      } else if (interfaces[i].address.address4.sin_family == AF_INET6) {
3651cb0ef41Sopenharmony_ci        uv_ip6_name(&interfaces[i].address.address6, ip, sizeof(ip));
3661cb0ef41Sopenharmony_ci        uv_ip6_name(&interfaces[i].netmask.netmask6, netmask, sizeof(netmask));
3671cb0ef41Sopenharmony_ci        writer->json_keyvalue("address", ip);
3681cb0ef41Sopenharmony_ci        writer->json_keyvalue("netmask", netmask);
3691cb0ef41Sopenharmony_ci        writer->json_keyvalue("family", "IPv6");
3701cb0ef41Sopenharmony_ci        writer->json_keyvalue("scopeid",
3711cb0ef41Sopenharmony_ci                              interfaces[i].address.address6.sin6_scope_id);
3721cb0ef41Sopenharmony_ci      } else {
3731cb0ef41Sopenharmony_ci        writer->json_keyvalue("family", "unknown");
3741cb0ef41Sopenharmony_ci      }
3751cb0ef41Sopenharmony_ci
3761cb0ef41Sopenharmony_ci      writer->json_end();
3771cb0ef41Sopenharmony_ci    }
3781cb0ef41Sopenharmony_ci
3791cb0ef41Sopenharmony_ci    writer->json_arrayend();
3801cb0ef41Sopenharmony_ci    uv_free_interface_addresses(interfaces, count);
3811cb0ef41Sopenharmony_ci  }
3821cb0ef41Sopenharmony_ci}
3831cb0ef41Sopenharmony_ci
3841cb0ef41Sopenharmony_cistatic void PrintJavaScriptErrorProperties(JSONWriter* writer,
3851cb0ef41Sopenharmony_ci                                           Isolate* isolate,
3861cb0ef41Sopenharmony_ci                                           Local<Value> error) {
3871cb0ef41Sopenharmony_ci  writer->json_objectstart("errorProperties");
3881cb0ef41Sopenharmony_ci  if (!error.IsEmpty() && error->IsObject()) {
3891cb0ef41Sopenharmony_ci    TryCatch try_catch(isolate);
3901cb0ef41Sopenharmony_ci    Local<Object> error_obj = error.As<Object>();
3911cb0ef41Sopenharmony_ci    Local<Context> context = error_obj->GetIsolate()->GetCurrentContext();
3921cb0ef41Sopenharmony_ci    Local<Array> keys;
3931cb0ef41Sopenharmony_ci    if (!error_obj->GetOwnPropertyNames(context).ToLocal(&keys)) {
3941cb0ef41Sopenharmony_ci      return writer->json_objectend();  // the end of 'errorProperties'
3951cb0ef41Sopenharmony_ci    }
3961cb0ef41Sopenharmony_ci    uint32_t keys_length = keys->Length();
3971cb0ef41Sopenharmony_ci    for (uint32_t i = 0; i < keys_length; i++) {
3981cb0ef41Sopenharmony_ci      Local<Value> key;
3991cb0ef41Sopenharmony_ci      if (!keys->Get(context, i).ToLocal(&key) || !key->IsString()) {
4001cb0ef41Sopenharmony_ci        continue;
4011cb0ef41Sopenharmony_ci      }
4021cb0ef41Sopenharmony_ci      Local<Value> value;
4031cb0ef41Sopenharmony_ci      Local<String> value_string;
4041cb0ef41Sopenharmony_ci      if (!error_obj->Get(context, key).ToLocal(&value) ||
4051cb0ef41Sopenharmony_ci          !value->ToString(context).ToLocal(&value_string)) {
4061cb0ef41Sopenharmony_ci        continue;
4071cb0ef41Sopenharmony_ci      }
4081cb0ef41Sopenharmony_ci      node::Utf8Value k(isolate, key);
4091cb0ef41Sopenharmony_ci      if (k == "stack" || k == "message") continue;
4101cb0ef41Sopenharmony_ci      node::Utf8Value v(isolate, value_string);
4111cb0ef41Sopenharmony_ci      writer->json_keyvalue(k.ToStringView(), v.ToStringView());
4121cb0ef41Sopenharmony_ci    }
4131cb0ef41Sopenharmony_ci  }
4141cb0ef41Sopenharmony_ci  writer->json_objectend();  // the end of 'errorProperties'
4151cb0ef41Sopenharmony_ci}
4161cb0ef41Sopenharmony_ci
4171cb0ef41Sopenharmony_cistatic Maybe<std::string> ErrorToString(Isolate* isolate,
4181cb0ef41Sopenharmony_ci                                        Local<Context> context,
4191cb0ef41Sopenharmony_ci                                        Local<Value> error) {
4201cb0ef41Sopenharmony_ci  if (error.IsEmpty()) {
4211cb0ef41Sopenharmony_ci    return Nothing<std::string>();
4221cb0ef41Sopenharmony_ci  }
4231cb0ef41Sopenharmony_ci
4241cb0ef41Sopenharmony_ci  MaybeLocal<String> maybe_str;
4251cb0ef41Sopenharmony_ci  // `ToString` is not available to Symbols.
4261cb0ef41Sopenharmony_ci  if (error->IsSymbol()) {
4271cb0ef41Sopenharmony_ci    maybe_str = error.As<v8::Symbol>()->ToDetailString(context);
4281cb0ef41Sopenharmony_ci  } else if (!error->IsObject()) {
4291cb0ef41Sopenharmony_ci    maybe_str = error->ToString(context);
4301cb0ef41Sopenharmony_ci  } else if (error->IsObject()) {
4311cb0ef41Sopenharmony_ci    MaybeLocal<Value> stack = error.As<Object>()->Get(
4321cb0ef41Sopenharmony_ci        context, FIXED_ONE_BYTE_STRING(isolate, "stack"));
4331cb0ef41Sopenharmony_ci    if (!stack.IsEmpty() && stack.ToLocalChecked()->IsString()) {
4341cb0ef41Sopenharmony_ci      maybe_str = stack.ToLocalChecked().As<String>();
4351cb0ef41Sopenharmony_ci    }
4361cb0ef41Sopenharmony_ci  }
4371cb0ef41Sopenharmony_ci
4381cb0ef41Sopenharmony_ci  Local<String> js_str;
4391cb0ef41Sopenharmony_ci  if (!maybe_str.ToLocal(&js_str)) {
4401cb0ef41Sopenharmony_ci    return Nothing<std::string>();
4411cb0ef41Sopenharmony_ci  }
4421cb0ef41Sopenharmony_ci  String::Utf8Value sv(isolate, js_str);
4431cb0ef41Sopenharmony_ci  return Just<>(std::string(*sv, sv.length()));
4441cb0ef41Sopenharmony_ci}
4451cb0ef41Sopenharmony_ci
4461cb0ef41Sopenharmony_cistatic void PrintEmptyJavaScriptStack(JSONWriter* writer) {
4471cb0ef41Sopenharmony_ci  writer->json_keyvalue("message", "No stack.");
4481cb0ef41Sopenharmony_ci  writer->json_arraystart("stack");
4491cb0ef41Sopenharmony_ci  writer->json_element("Unavailable.");
4501cb0ef41Sopenharmony_ci  writer->json_arrayend();
4511cb0ef41Sopenharmony_ci
4521cb0ef41Sopenharmony_ci  writer->json_objectstart("errorProperties");
4531cb0ef41Sopenharmony_ci  writer->json_objectend();
4541cb0ef41Sopenharmony_ci}
4551cb0ef41Sopenharmony_ci
4561cb0ef41Sopenharmony_ci// Do our best to report the JavaScript stack without calling into JavaScript.
4571cb0ef41Sopenharmony_cistatic void PrintJavaScriptStack(JSONWriter* writer,
4581cb0ef41Sopenharmony_ci                                 Isolate* isolate,
4591cb0ef41Sopenharmony_ci                                 const char* trigger) {
4601cb0ef41Sopenharmony_ci  // Can not capture the stacktrace when the isolate is in a OOM state or no
4611cb0ef41Sopenharmony_ci  // context is entered.
4621cb0ef41Sopenharmony_ci  if (!strcmp(trigger, "OOMError") || !isolate->InContext()) {
4631cb0ef41Sopenharmony_ci    PrintEmptyJavaScriptStack(writer);
4641cb0ef41Sopenharmony_ci    return;
4651cb0ef41Sopenharmony_ci  }
4661cb0ef41Sopenharmony_ci
4671cb0ef41Sopenharmony_ci  HandleScope scope(isolate);
4681cb0ef41Sopenharmony_ci  RegisterState state;
4691cb0ef41Sopenharmony_ci  state.pc = nullptr;
4701cb0ef41Sopenharmony_ci  state.fp = &state;
4711cb0ef41Sopenharmony_ci  state.sp = &state;
4721cb0ef41Sopenharmony_ci
4731cb0ef41Sopenharmony_ci  // in-out params
4741cb0ef41Sopenharmony_ci  SampleInfo info;
4751cb0ef41Sopenharmony_ci  void* samples[MAX_FRAME_COUNT];
4761cb0ef41Sopenharmony_ci  isolate->GetStackSample(state, samples, MAX_FRAME_COUNT, &info);
4771cb0ef41Sopenharmony_ci
4781cb0ef41Sopenharmony_ci  constexpr StackTrace::StackTraceOptions stack_trace_options =
4791cb0ef41Sopenharmony_ci      static_cast<StackTrace::StackTraceOptions>(
4801cb0ef41Sopenharmony_ci          StackTrace::kDetailed |
4811cb0ef41Sopenharmony_ci          StackTrace::kExposeFramesAcrossSecurityOrigins);
4821cb0ef41Sopenharmony_ci  Local<StackTrace> stack = StackTrace::CurrentStackTrace(
4831cb0ef41Sopenharmony_ci      isolate, MAX_FRAME_COUNT, stack_trace_options);
4841cb0ef41Sopenharmony_ci
4851cb0ef41Sopenharmony_ci  if (stack->GetFrameCount() == 0) {
4861cb0ef41Sopenharmony_ci    PrintEmptyJavaScriptStack(writer);
4871cb0ef41Sopenharmony_ci    return;
4881cb0ef41Sopenharmony_ci  }
4891cb0ef41Sopenharmony_ci
4901cb0ef41Sopenharmony_ci  writer->json_keyvalue("message", trigger);
4911cb0ef41Sopenharmony_ci  writer->json_arraystart("stack");
4921cb0ef41Sopenharmony_ci  for (int i = 0; i < stack->GetFrameCount(); i++) {
4931cb0ef41Sopenharmony_ci    Local<StackFrame> frame = stack->GetFrame(isolate, i);
4941cb0ef41Sopenharmony_ci
4951cb0ef41Sopenharmony_ci    Utf8Value function_name(isolate, frame->GetFunctionName());
4961cb0ef41Sopenharmony_ci    Utf8Value script_name(isolate, frame->GetScriptName());
4971cb0ef41Sopenharmony_ci    const int line_number = frame->GetLineNumber();
4981cb0ef41Sopenharmony_ci    const int column = frame->GetColumn();
4991cb0ef41Sopenharmony_ci
5001cb0ef41Sopenharmony_ci    std::string stack_line = SPrintF(
5011cb0ef41Sopenharmony_ci        "at %s (%s:%d:%d)", *function_name, *script_name, line_number, column);
5021cb0ef41Sopenharmony_ci    writer->json_element(stack_line);
5031cb0ef41Sopenharmony_ci  }
5041cb0ef41Sopenharmony_ci  writer->json_arrayend();
5051cb0ef41Sopenharmony_ci  writer->json_objectstart("errorProperties");
5061cb0ef41Sopenharmony_ci  writer->json_objectend();
5071cb0ef41Sopenharmony_ci}
5081cb0ef41Sopenharmony_ci
5091cb0ef41Sopenharmony_ci// Report the JavaScript stack.
5101cb0ef41Sopenharmony_cistatic void PrintJavaScriptErrorStack(JSONWriter* writer,
5111cb0ef41Sopenharmony_ci                                      Isolate* isolate,
5121cb0ef41Sopenharmony_ci                                      Local<Value> error,
5131cb0ef41Sopenharmony_ci                                      const char* trigger) {
5141cb0ef41Sopenharmony_ci  if (error.IsEmpty()) {
5151cb0ef41Sopenharmony_ci    return PrintJavaScriptStack(writer, isolate, trigger);
5161cb0ef41Sopenharmony_ci  }
5171cb0ef41Sopenharmony_ci
5181cb0ef41Sopenharmony_ci  TryCatch try_catch(isolate);
5191cb0ef41Sopenharmony_ci  HandleScope scope(isolate);
5201cb0ef41Sopenharmony_ci  Local<Context> context = isolate->GetCurrentContext();
5211cb0ef41Sopenharmony_ci  std::string ss = "";
5221cb0ef41Sopenharmony_ci  if (!ErrorToString(isolate, context, error).To(&ss)) {
5231cb0ef41Sopenharmony_ci    PrintEmptyJavaScriptStack(writer);
5241cb0ef41Sopenharmony_ci    return;
5251cb0ef41Sopenharmony_ci  }
5261cb0ef41Sopenharmony_ci
5271cb0ef41Sopenharmony_ci  int line = ss.find('\n');
5281cb0ef41Sopenharmony_ci  if (line == -1) {
5291cb0ef41Sopenharmony_ci    writer->json_keyvalue("message", ss);
5301cb0ef41Sopenharmony_ci  } else {
5311cb0ef41Sopenharmony_ci    std::string l = ss.substr(0, line);
5321cb0ef41Sopenharmony_ci    writer->json_keyvalue("message", l);
5331cb0ef41Sopenharmony_ci    writer->json_arraystart("stack");
5341cb0ef41Sopenharmony_ci    ss = ss.substr(line + 1);
5351cb0ef41Sopenharmony_ci    line = ss.find('\n');
5361cb0ef41Sopenharmony_ci    while (line != -1) {
5371cb0ef41Sopenharmony_ci      l = ss.substr(0, line);
5381cb0ef41Sopenharmony_ci      l.erase(l.begin(), std::find_if(l.begin(), l.end(), [](int ch) {
5391cb0ef41Sopenharmony_ci                return !std::iswspace(ch);
5401cb0ef41Sopenharmony_ci              }));
5411cb0ef41Sopenharmony_ci      writer->json_element(l);
5421cb0ef41Sopenharmony_ci      ss = ss.substr(line + 1);
5431cb0ef41Sopenharmony_ci      line = ss.find('\n');
5441cb0ef41Sopenharmony_ci    }
5451cb0ef41Sopenharmony_ci    writer->json_arrayend();
5461cb0ef41Sopenharmony_ci  }
5471cb0ef41Sopenharmony_ci
5481cb0ef41Sopenharmony_ci  // Report summary JavaScript error properties backtrace
5491cb0ef41Sopenharmony_ci  PrintJavaScriptErrorProperties(writer, isolate, error);
5501cb0ef41Sopenharmony_ci}
5511cb0ef41Sopenharmony_ci
5521cb0ef41Sopenharmony_ci// Report a native stack backtrace
5531cb0ef41Sopenharmony_cistatic void PrintNativeStack(JSONWriter* writer) {
5541cb0ef41Sopenharmony_ci  auto sym_ctx = NativeSymbolDebuggingContext::New();
5551cb0ef41Sopenharmony_ci  void* frames[256];
5561cb0ef41Sopenharmony_ci  const int size = sym_ctx->GetStackTrace(frames, arraysize(frames));
5571cb0ef41Sopenharmony_ci  writer->json_arraystart("nativeStack");
5581cb0ef41Sopenharmony_ci  int i;
5591cb0ef41Sopenharmony_ci  for (i = 1; i < size; i++) {
5601cb0ef41Sopenharmony_ci    void* frame = frames[i];
5611cb0ef41Sopenharmony_ci    writer->json_start();
5621cb0ef41Sopenharmony_ci    writer->json_keyvalue("pc",
5631cb0ef41Sopenharmony_ci                          ValueToHexString(reinterpret_cast<uintptr_t>(frame)));
5641cb0ef41Sopenharmony_ci    writer->json_keyvalue("symbol", sym_ctx->LookupSymbol(frame).Display());
5651cb0ef41Sopenharmony_ci    writer->json_end();
5661cb0ef41Sopenharmony_ci  }
5671cb0ef41Sopenharmony_ci  writer->json_arrayend();
5681cb0ef41Sopenharmony_ci}
5691cb0ef41Sopenharmony_ci
5701cb0ef41Sopenharmony_ci// Report V8 JavaScript heap information.
5711cb0ef41Sopenharmony_ci// This uses the existing V8 HeapStatistics and HeapSpaceStatistics APIs.
5721cb0ef41Sopenharmony_ci// The isolate->GetGCStatistics(&heap_stats) internal V8 API could potentially
5731cb0ef41Sopenharmony_ci// provide some more useful information - the GC history and the handle counts
5741cb0ef41Sopenharmony_cistatic void PrintGCStatistics(JSONWriter* writer, Isolate* isolate) {
5751cb0ef41Sopenharmony_ci  HeapStatistics v8_heap_stats;
5761cb0ef41Sopenharmony_ci  isolate->GetHeapStatistics(&v8_heap_stats);
5771cb0ef41Sopenharmony_ci  HeapSpaceStatistics v8_heap_space_stats;
5781cb0ef41Sopenharmony_ci
5791cb0ef41Sopenharmony_ci  writer->json_objectstart("javascriptHeap");
5801cb0ef41Sopenharmony_ci  writer->json_keyvalue("totalMemory", v8_heap_stats.total_heap_size());
5811cb0ef41Sopenharmony_ci  writer->json_keyvalue("executableMemory",
5821cb0ef41Sopenharmony_ci                        v8_heap_stats.total_heap_size_executable());
5831cb0ef41Sopenharmony_ci  writer->json_keyvalue("totalCommittedMemory",
5841cb0ef41Sopenharmony_ci                        v8_heap_stats.total_physical_size());
5851cb0ef41Sopenharmony_ci  writer->json_keyvalue("availableMemory",
5861cb0ef41Sopenharmony_ci                        v8_heap_stats.total_available_size());
5871cb0ef41Sopenharmony_ci  writer->json_keyvalue("totalGlobalHandlesMemory",
5881cb0ef41Sopenharmony_ci                        v8_heap_stats.total_global_handles_size());
5891cb0ef41Sopenharmony_ci  writer->json_keyvalue("usedGlobalHandlesMemory",
5901cb0ef41Sopenharmony_ci                        v8_heap_stats.used_global_handles_size());
5911cb0ef41Sopenharmony_ci  writer->json_keyvalue("usedMemory", v8_heap_stats.used_heap_size());
5921cb0ef41Sopenharmony_ci  writer->json_keyvalue("memoryLimit", v8_heap_stats.heap_size_limit());
5931cb0ef41Sopenharmony_ci  writer->json_keyvalue("mallocedMemory", v8_heap_stats.malloced_memory());
5941cb0ef41Sopenharmony_ci  writer->json_keyvalue("externalMemory", v8_heap_stats.external_memory());
5951cb0ef41Sopenharmony_ci  writer->json_keyvalue("peakMallocedMemory",
5961cb0ef41Sopenharmony_ci                        v8_heap_stats.peak_malloced_memory());
5971cb0ef41Sopenharmony_ci  writer->json_keyvalue("nativeContextCount",
5981cb0ef41Sopenharmony_ci                        v8_heap_stats.number_of_native_contexts());
5991cb0ef41Sopenharmony_ci  writer->json_keyvalue("detachedContextCount",
6001cb0ef41Sopenharmony_ci                        v8_heap_stats.number_of_detached_contexts());
6011cb0ef41Sopenharmony_ci  writer->json_keyvalue("doesZapGarbage", v8_heap_stats.does_zap_garbage());
6021cb0ef41Sopenharmony_ci
6031cb0ef41Sopenharmony_ci  writer->json_objectstart("heapSpaces");
6041cb0ef41Sopenharmony_ci  // Loop through heap spaces
6051cb0ef41Sopenharmony_ci  for (size_t i = 0; i < isolate->NumberOfHeapSpaces(); i++) {
6061cb0ef41Sopenharmony_ci    isolate->GetHeapSpaceStatistics(&v8_heap_space_stats, i);
6071cb0ef41Sopenharmony_ci    writer->json_objectstart(v8_heap_space_stats.space_name());
6081cb0ef41Sopenharmony_ci    writer->json_keyvalue("memorySize", v8_heap_space_stats.space_size());
6091cb0ef41Sopenharmony_ci    writer->json_keyvalue(
6101cb0ef41Sopenharmony_ci        "committedMemory",
6111cb0ef41Sopenharmony_ci        v8_heap_space_stats.physical_space_size());
6121cb0ef41Sopenharmony_ci    writer->json_keyvalue(
6131cb0ef41Sopenharmony_ci        "capacity",
6141cb0ef41Sopenharmony_ci        v8_heap_space_stats.space_used_size() +
6151cb0ef41Sopenharmony_ci            v8_heap_space_stats.space_available_size());
6161cb0ef41Sopenharmony_ci    writer->json_keyvalue("used", v8_heap_space_stats.space_used_size());
6171cb0ef41Sopenharmony_ci    writer->json_keyvalue(
6181cb0ef41Sopenharmony_ci        "available", v8_heap_space_stats.space_available_size());
6191cb0ef41Sopenharmony_ci    writer->json_objectend();
6201cb0ef41Sopenharmony_ci  }
6211cb0ef41Sopenharmony_ci
6221cb0ef41Sopenharmony_ci  writer->json_objectend();
6231cb0ef41Sopenharmony_ci  writer->json_objectend();
6241cb0ef41Sopenharmony_ci}
6251cb0ef41Sopenharmony_ci
6261cb0ef41Sopenharmony_cistatic void PrintResourceUsage(JSONWriter* writer) {
6271cb0ef41Sopenharmony_ci  // Get process uptime in seconds
6281cb0ef41Sopenharmony_ci  uint64_t uptime =
6291cb0ef41Sopenharmony_ci      (uv_hrtime() - per_process::node_start_time) / (NANOS_PER_SEC);
6301cb0ef41Sopenharmony_ci  if (uptime == 0) uptime = 1;  // avoid division by zero.
6311cb0ef41Sopenharmony_ci
6321cb0ef41Sopenharmony_ci  // Process and current thread usage statistics
6331cb0ef41Sopenharmony_ci  uv_rusage_t rusage;
6341cb0ef41Sopenharmony_ci  writer->json_objectstart("resourceUsage");
6351cb0ef41Sopenharmony_ci
6361cb0ef41Sopenharmony_ci  uint64_t free_memory = uv_get_free_memory();
6371cb0ef41Sopenharmony_ci  uint64_t total_memory = uv_get_total_memory();
6381cb0ef41Sopenharmony_ci
6391cb0ef41Sopenharmony_ci  writer->json_keyvalue("free_memory", free_memory);
6401cb0ef41Sopenharmony_ci  writer->json_keyvalue("total_memory", total_memory);
6411cb0ef41Sopenharmony_ci
6421cb0ef41Sopenharmony_ci  size_t rss;
6431cb0ef41Sopenharmony_ci  int err = uv_resident_set_memory(&rss);
6441cb0ef41Sopenharmony_ci  if (!err) {
6451cb0ef41Sopenharmony_ci    writer->json_keyvalue("rss", rss);
6461cb0ef41Sopenharmony_ci  }
6471cb0ef41Sopenharmony_ci
6481cb0ef41Sopenharmony_ci  uint64_t constrained_memory = uv_get_constrained_memory();
6491cb0ef41Sopenharmony_ci  if (constrained_memory) {
6501cb0ef41Sopenharmony_ci    writer->json_keyvalue("constrained_memory", constrained_memory);
6511cb0ef41Sopenharmony_ci  }
6521cb0ef41Sopenharmony_ci
6531cb0ef41Sopenharmony_ci  // See GuessMemoryAvailableToTheProcess
6541cb0ef41Sopenharmony_ci  if (!err && constrained_memory && constrained_memory >= rss) {
6551cb0ef41Sopenharmony_ci    uint64_t available_memory = constrained_memory - rss;
6561cb0ef41Sopenharmony_ci    writer->json_keyvalue("available_memory", available_memory);
6571cb0ef41Sopenharmony_ci  } else {
6581cb0ef41Sopenharmony_ci    writer->json_keyvalue("available_memory", free_memory);
6591cb0ef41Sopenharmony_ci  }
6601cb0ef41Sopenharmony_ci
6611cb0ef41Sopenharmony_ci  if (uv_getrusage(&rusage) == 0) {
6621cb0ef41Sopenharmony_ci    double user_cpu =
6631cb0ef41Sopenharmony_ci        rusage.ru_utime.tv_sec + SEC_PER_MICROS * rusage.ru_utime.tv_usec;
6641cb0ef41Sopenharmony_ci    double kernel_cpu =
6651cb0ef41Sopenharmony_ci        rusage.ru_stime.tv_sec + SEC_PER_MICROS * rusage.ru_stime.tv_usec;
6661cb0ef41Sopenharmony_ci    writer->json_keyvalue("userCpuSeconds", user_cpu);
6671cb0ef41Sopenharmony_ci    writer->json_keyvalue("kernelCpuSeconds", kernel_cpu);
6681cb0ef41Sopenharmony_ci    double cpu_abs = user_cpu + kernel_cpu;
6691cb0ef41Sopenharmony_ci    double cpu_percentage = (cpu_abs / uptime) * 100.0;
6701cb0ef41Sopenharmony_ci    double user_cpu_percentage = (user_cpu / uptime) * 100.0;
6711cb0ef41Sopenharmony_ci    double kernel_cpu_percentage = (kernel_cpu / uptime) * 100.0;
6721cb0ef41Sopenharmony_ci    writer->json_keyvalue("cpuConsumptionPercent", cpu_percentage);
6731cb0ef41Sopenharmony_ci    writer->json_keyvalue("userCpuConsumptionPercent", user_cpu_percentage);
6741cb0ef41Sopenharmony_ci    writer->json_keyvalue("kernelCpuConsumptionPercent", kernel_cpu_percentage);
6751cb0ef41Sopenharmony_ci    writer->json_keyvalue("maxRss", rusage.ru_maxrss * 1024);
6761cb0ef41Sopenharmony_ci    writer->json_objectstart("pageFaults");
6771cb0ef41Sopenharmony_ci    writer->json_keyvalue("IORequired", rusage.ru_majflt);
6781cb0ef41Sopenharmony_ci    writer->json_keyvalue("IONotRequired", rusage.ru_minflt);
6791cb0ef41Sopenharmony_ci    writer->json_objectend();
6801cb0ef41Sopenharmony_ci    writer->json_objectstart("fsActivity");
6811cb0ef41Sopenharmony_ci    writer->json_keyvalue("reads", rusage.ru_inblock);
6821cb0ef41Sopenharmony_ci    writer->json_keyvalue("writes", rusage.ru_oublock);
6831cb0ef41Sopenharmony_ci    writer->json_objectend();
6841cb0ef41Sopenharmony_ci  }
6851cb0ef41Sopenharmony_ci  writer->json_objectend();
6861cb0ef41Sopenharmony_ci#ifdef RUSAGE_THREAD
6871cb0ef41Sopenharmony_ci  struct rusage stats;
6881cb0ef41Sopenharmony_ci  if (getrusage(RUSAGE_THREAD, &stats) == 0) {
6891cb0ef41Sopenharmony_ci    writer->json_objectstart("uvthreadResourceUsage");
6901cb0ef41Sopenharmony_ci    double user_cpu =
6911cb0ef41Sopenharmony_ci        stats.ru_utime.tv_sec + SEC_PER_MICROS * stats.ru_utime.tv_usec;
6921cb0ef41Sopenharmony_ci    double kernel_cpu =
6931cb0ef41Sopenharmony_ci        stats.ru_stime.tv_sec + SEC_PER_MICROS * stats.ru_stime.tv_usec;
6941cb0ef41Sopenharmony_ci    writer->json_keyvalue("userCpuSeconds", user_cpu);
6951cb0ef41Sopenharmony_ci    writer->json_keyvalue("kernelCpuSeconds", kernel_cpu);
6961cb0ef41Sopenharmony_ci    double cpu_abs = user_cpu + kernel_cpu;
6971cb0ef41Sopenharmony_ci    double cpu_percentage = (cpu_abs / uptime) * 100.0;
6981cb0ef41Sopenharmony_ci    double user_cpu_percentage = (user_cpu / uptime) * 100.0;
6991cb0ef41Sopenharmony_ci    double kernel_cpu_percentage = (kernel_cpu / uptime) * 100.0;
7001cb0ef41Sopenharmony_ci    writer->json_keyvalue("cpuConsumptionPercent", cpu_percentage);
7011cb0ef41Sopenharmony_ci    writer->json_keyvalue("userCpuConsumptionPercent", user_cpu_percentage);
7021cb0ef41Sopenharmony_ci    writer->json_keyvalue("kernelCpuConsumptionPercent", kernel_cpu_percentage);
7031cb0ef41Sopenharmony_ci    writer->json_objectstart("fsActivity");
7041cb0ef41Sopenharmony_ci    writer->json_keyvalue("reads", stats.ru_inblock);
7051cb0ef41Sopenharmony_ci    writer->json_keyvalue("writes", stats.ru_oublock);
7061cb0ef41Sopenharmony_ci    writer->json_objectend();
7071cb0ef41Sopenharmony_ci    writer->json_objectend();
7081cb0ef41Sopenharmony_ci  }
7091cb0ef41Sopenharmony_ci#endif  // RUSAGE_THREAD
7101cb0ef41Sopenharmony_ci}
7111cb0ef41Sopenharmony_ci
7121cb0ef41Sopenharmony_ci// Report operating system information.
7131cb0ef41Sopenharmony_cistatic void PrintSystemInformation(JSONWriter* writer) {
7141cb0ef41Sopenharmony_ci  uv_env_item_t* envitems;
7151cb0ef41Sopenharmony_ci  int envcount;
7161cb0ef41Sopenharmony_ci  int r;
7171cb0ef41Sopenharmony_ci
7181cb0ef41Sopenharmony_ci  writer->json_objectstart("environmentVariables");
7191cb0ef41Sopenharmony_ci
7201cb0ef41Sopenharmony_ci  {
7211cb0ef41Sopenharmony_ci    Mutex::ScopedLock lock(per_process::env_var_mutex);
7221cb0ef41Sopenharmony_ci    r = uv_os_environ(&envitems, &envcount);
7231cb0ef41Sopenharmony_ci  }
7241cb0ef41Sopenharmony_ci
7251cb0ef41Sopenharmony_ci  if (r == 0) {
7261cb0ef41Sopenharmony_ci    for (int i = 0; i < envcount; i++)
7271cb0ef41Sopenharmony_ci      writer->json_keyvalue(envitems[i].name, envitems[i].value);
7281cb0ef41Sopenharmony_ci
7291cb0ef41Sopenharmony_ci    uv_os_free_environ(envitems, envcount);
7301cb0ef41Sopenharmony_ci  }
7311cb0ef41Sopenharmony_ci
7321cb0ef41Sopenharmony_ci  writer->json_objectend();
7331cb0ef41Sopenharmony_ci
7341cb0ef41Sopenharmony_ci#ifndef _WIN32
7351cb0ef41Sopenharmony_ci  static struct {
7361cb0ef41Sopenharmony_ci    const char* description;
7371cb0ef41Sopenharmony_ci    int id;
7381cb0ef41Sopenharmony_ci  } rlimit_strings[] = {
7391cb0ef41Sopenharmony_ci    {"core_file_size_blocks", RLIMIT_CORE},
7401cb0ef41Sopenharmony_ci    {"data_seg_size_kbytes", RLIMIT_DATA},
7411cb0ef41Sopenharmony_ci    {"file_size_blocks", RLIMIT_FSIZE},
7421cb0ef41Sopenharmony_ci#if !(defined(_AIX) || defined(__sun))
7431cb0ef41Sopenharmony_ci    {"max_locked_memory_bytes", RLIMIT_MEMLOCK},
7441cb0ef41Sopenharmony_ci#endif
7451cb0ef41Sopenharmony_ci#ifndef __sun
7461cb0ef41Sopenharmony_ci    {"max_memory_size_kbytes", RLIMIT_RSS},
7471cb0ef41Sopenharmony_ci#endif
7481cb0ef41Sopenharmony_ci    {"open_files", RLIMIT_NOFILE},
7491cb0ef41Sopenharmony_ci    {"stack_size_bytes", RLIMIT_STACK},
7501cb0ef41Sopenharmony_ci    {"cpu_time_seconds", RLIMIT_CPU},
7511cb0ef41Sopenharmony_ci#ifndef __sun
7521cb0ef41Sopenharmony_ci    {"max_user_processes", RLIMIT_NPROC},
7531cb0ef41Sopenharmony_ci#endif
7541cb0ef41Sopenharmony_ci#ifndef __OpenBSD__
7551cb0ef41Sopenharmony_ci    {"virtual_memory_kbytes", RLIMIT_AS}
7561cb0ef41Sopenharmony_ci#endif
7571cb0ef41Sopenharmony_ci  };
7581cb0ef41Sopenharmony_ci
7591cb0ef41Sopenharmony_ci  writer->json_objectstart("userLimits");
7601cb0ef41Sopenharmony_ci  struct rlimit limit;
7611cb0ef41Sopenharmony_ci  std::string soft, hard;
7621cb0ef41Sopenharmony_ci
7631cb0ef41Sopenharmony_ci  for (size_t i = 0; i < arraysize(rlimit_strings); i++) {
7641cb0ef41Sopenharmony_ci    if (getrlimit(rlimit_strings[i].id, &limit) == 0) {
7651cb0ef41Sopenharmony_ci      writer->json_objectstart(rlimit_strings[i].description);
7661cb0ef41Sopenharmony_ci
7671cb0ef41Sopenharmony_ci      if (limit.rlim_cur == RLIM_INFINITY)
7681cb0ef41Sopenharmony_ci        writer->json_keyvalue("soft", "unlimited");
7691cb0ef41Sopenharmony_ci      else
7701cb0ef41Sopenharmony_ci        writer->json_keyvalue("soft", limit.rlim_cur);
7711cb0ef41Sopenharmony_ci
7721cb0ef41Sopenharmony_ci      if (limit.rlim_max == RLIM_INFINITY)
7731cb0ef41Sopenharmony_ci        writer->json_keyvalue("hard", "unlimited");
7741cb0ef41Sopenharmony_ci      else
7751cb0ef41Sopenharmony_ci        writer->json_keyvalue("hard", limit.rlim_max);
7761cb0ef41Sopenharmony_ci
7771cb0ef41Sopenharmony_ci      writer->json_objectend();
7781cb0ef41Sopenharmony_ci    }
7791cb0ef41Sopenharmony_ci  }
7801cb0ef41Sopenharmony_ci  writer->json_objectend();
7811cb0ef41Sopenharmony_ci#endif  // _WIN32
7821cb0ef41Sopenharmony_ci
7831cb0ef41Sopenharmony_ci  PrintLoadedLibraries(writer);
7841cb0ef41Sopenharmony_ci}
7851cb0ef41Sopenharmony_ci
7861cb0ef41Sopenharmony_ci// Report a list of loaded native libraries.
7871cb0ef41Sopenharmony_cistatic void PrintLoadedLibraries(JSONWriter* writer) {
7881cb0ef41Sopenharmony_ci  writer->json_arraystart("sharedObjects");
7891cb0ef41Sopenharmony_ci  std::vector<std::string> modules =
7901cb0ef41Sopenharmony_ci      NativeSymbolDebuggingContext::GetLoadedLibraries();
7911cb0ef41Sopenharmony_ci  for (auto const& module_name : modules) writer->json_element(module_name);
7921cb0ef41Sopenharmony_ci  writer->json_arrayend();
7931cb0ef41Sopenharmony_ci}
7941cb0ef41Sopenharmony_ci
7951cb0ef41Sopenharmony_ci// Obtain and report the node and subcomponent version strings.
7961cb0ef41Sopenharmony_cistatic void PrintComponentVersions(JSONWriter* writer) {
7971cb0ef41Sopenharmony_ci  std::stringstream buf;
7981cb0ef41Sopenharmony_ci
7991cb0ef41Sopenharmony_ci  writer->json_objectstart("componentVersions");
8001cb0ef41Sopenharmony_ci
8011cb0ef41Sopenharmony_ci#define V(key) +1
8021cb0ef41Sopenharmony_ci  std::pair<std::string_view, std::string_view>
8031cb0ef41Sopenharmony_ci      versions_array[NODE_VERSIONS_KEYS(V)];
8041cb0ef41Sopenharmony_ci#undef V
8051cb0ef41Sopenharmony_ci  auto* slot = &versions_array[0];
8061cb0ef41Sopenharmony_ci
8071cb0ef41Sopenharmony_ci#define V(key)                                                                 \
8081cb0ef41Sopenharmony_ci  do {                                                                         \
8091cb0ef41Sopenharmony_ci    *slot++ = std::pair<std::string_view, std::string_view>(                   \
8101cb0ef41Sopenharmony_ci        #key, per_process::metadata.versions.key);                             \
8111cb0ef41Sopenharmony_ci  } while (0);
8121cb0ef41Sopenharmony_ci  NODE_VERSIONS_KEYS(V)
8131cb0ef41Sopenharmony_ci#undef V
8141cb0ef41Sopenharmony_ci
8151cb0ef41Sopenharmony_ci  std::sort(&versions_array[0],
8161cb0ef41Sopenharmony_ci            &versions_array[arraysize(versions_array)],
8171cb0ef41Sopenharmony_ci            [](auto& a, auto& b) { return a.first < b.first; });
8181cb0ef41Sopenharmony_ci
8191cb0ef41Sopenharmony_ci  for (const auto& version : versions_array) {
8201cb0ef41Sopenharmony_ci    writer->json_keyvalue(version.first, version.second);
8211cb0ef41Sopenharmony_ci  }
8221cb0ef41Sopenharmony_ci
8231cb0ef41Sopenharmony_ci  writer->json_objectend();
8241cb0ef41Sopenharmony_ci}
8251cb0ef41Sopenharmony_ci
8261cb0ef41Sopenharmony_ci// Report runtime release information.
8271cb0ef41Sopenharmony_cistatic void PrintRelease(JSONWriter* writer) {
8281cb0ef41Sopenharmony_ci  writer->json_objectstart("release");
8291cb0ef41Sopenharmony_ci  writer->json_keyvalue("name", per_process::metadata.release.name);
8301cb0ef41Sopenharmony_ci#if NODE_VERSION_IS_LTS
8311cb0ef41Sopenharmony_ci  writer->json_keyvalue("lts", per_process::metadata.release.lts);
8321cb0ef41Sopenharmony_ci#endif
8331cb0ef41Sopenharmony_ci
8341cb0ef41Sopenharmony_ci#ifdef NODE_HAS_RELEASE_URLS
8351cb0ef41Sopenharmony_ci  writer->json_keyvalue("headersUrl",
8361cb0ef41Sopenharmony_ci                        per_process::metadata.release.headers_url);
8371cb0ef41Sopenharmony_ci  writer->json_keyvalue("sourceUrl", per_process::metadata.release.source_url);
8381cb0ef41Sopenharmony_ci#ifdef _WIN32
8391cb0ef41Sopenharmony_ci  writer->json_keyvalue("libUrl", per_process::metadata.release.lib_url);
8401cb0ef41Sopenharmony_ci#endif  // _WIN32
8411cb0ef41Sopenharmony_ci#endif  // NODE_HAS_RELEASE_URLS
8421cb0ef41Sopenharmony_ci
8431cb0ef41Sopenharmony_ci  writer->json_objectend();
8441cb0ef41Sopenharmony_ci}
8451cb0ef41Sopenharmony_ci
8461cb0ef41Sopenharmony_ci}  // namespace report
8471cb0ef41Sopenharmony_ci
8481cb0ef41Sopenharmony_cistd::string TriggerNodeReport(Isolate* isolate,
8491cb0ef41Sopenharmony_ci                              Environment* env,
8501cb0ef41Sopenharmony_ci                              const char* message,
8511cb0ef41Sopenharmony_ci                              const char* trigger,
8521cb0ef41Sopenharmony_ci                              const std::string& name,
8531cb0ef41Sopenharmony_ci                              Local<Value> error) {
8541cb0ef41Sopenharmony_ci  std::string filename;
8551cb0ef41Sopenharmony_ci
8561cb0ef41Sopenharmony_ci  // Determine the required report filename. In order of priority:
8571cb0ef41Sopenharmony_ci  //   1) supplied on API 2) configured on startup 3) default generated
8581cb0ef41Sopenharmony_ci  if (!name.empty()) {
8591cb0ef41Sopenharmony_ci    // Filename was specified as API parameter.
8601cb0ef41Sopenharmony_ci    filename = name;
8611cb0ef41Sopenharmony_ci  } else {
8621cb0ef41Sopenharmony_ci    std::string report_filename;
8631cb0ef41Sopenharmony_ci    {
8641cb0ef41Sopenharmony_ci      Mutex::ScopedLock lock(per_process::cli_options_mutex);
8651cb0ef41Sopenharmony_ci      report_filename = per_process::cli_options->report_filename;
8661cb0ef41Sopenharmony_ci    }
8671cb0ef41Sopenharmony_ci    if (report_filename.length() > 0) {
8681cb0ef41Sopenharmony_ci      // File name was supplied via start-up option.
8691cb0ef41Sopenharmony_ci      filename = report_filename;
8701cb0ef41Sopenharmony_ci    } else {
8711cb0ef41Sopenharmony_ci      filename = *DiagnosticFilename(
8721cb0ef41Sopenharmony_ci          env != nullptr ? env->thread_id() : 0, "report", "json");
8731cb0ef41Sopenharmony_ci    }
8741cb0ef41Sopenharmony_ci  }
8751cb0ef41Sopenharmony_ci
8761cb0ef41Sopenharmony_ci  // Open the report file stream for writing. Supports stdout/err,
8771cb0ef41Sopenharmony_ci  // user-specified or (default) generated name
8781cb0ef41Sopenharmony_ci  std::ofstream outfile;
8791cb0ef41Sopenharmony_ci  std::ostream* outstream;
8801cb0ef41Sopenharmony_ci  if (filename == "stdout") {
8811cb0ef41Sopenharmony_ci    outstream = &std::cout;
8821cb0ef41Sopenharmony_ci  } else if (filename == "stderr") {
8831cb0ef41Sopenharmony_ci    outstream = &std::cerr;
8841cb0ef41Sopenharmony_ci  } else {
8851cb0ef41Sopenharmony_ci    std::string report_directory;
8861cb0ef41Sopenharmony_ci    {
8871cb0ef41Sopenharmony_ci      Mutex::ScopedLock lock(per_process::cli_options_mutex);
8881cb0ef41Sopenharmony_ci      report_directory = per_process::cli_options->report_directory;
8891cb0ef41Sopenharmony_ci    }
8901cb0ef41Sopenharmony_ci    // Regular file. Append filename to directory path if one was specified
8911cb0ef41Sopenharmony_ci    if (report_directory.length() > 0) {
8921cb0ef41Sopenharmony_ci      std::string pathname = report_directory;
8931cb0ef41Sopenharmony_ci      pathname += kPathSeparator;
8941cb0ef41Sopenharmony_ci      pathname += filename;
8951cb0ef41Sopenharmony_ci      outfile.open(pathname, std::ios::out | std::ios::binary);
8961cb0ef41Sopenharmony_ci    } else {
8971cb0ef41Sopenharmony_ci      outfile.open(filename, std::ios::out | std::ios::binary);
8981cb0ef41Sopenharmony_ci    }
8991cb0ef41Sopenharmony_ci    // Check for errors on the file open
9001cb0ef41Sopenharmony_ci    if (!outfile.is_open()) {
9011cb0ef41Sopenharmony_ci      std::cerr << "\nFailed to open Node.js report file: " << filename;
9021cb0ef41Sopenharmony_ci
9031cb0ef41Sopenharmony_ci      if (report_directory.length() > 0)
9041cb0ef41Sopenharmony_ci        std::cerr << " directory: " << report_directory;
9051cb0ef41Sopenharmony_ci
9061cb0ef41Sopenharmony_ci      std::cerr << " (errno: " << errno << ")" << std::endl;
9071cb0ef41Sopenharmony_ci      return "";
9081cb0ef41Sopenharmony_ci    }
9091cb0ef41Sopenharmony_ci    outstream = &outfile;
9101cb0ef41Sopenharmony_ci    std::cerr << "\nWriting Node.js report to file: " << filename;
9111cb0ef41Sopenharmony_ci  }
9121cb0ef41Sopenharmony_ci
9131cb0ef41Sopenharmony_ci  bool compact;
9141cb0ef41Sopenharmony_ci  {
9151cb0ef41Sopenharmony_ci    Mutex::ScopedLock lock(per_process::cli_options_mutex);
9161cb0ef41Sopenharmony_ci    compact = per_process::cli_options->report_compact;
9171cb0ef41Sopenharmony_ci  }
9181cb0ef41Sopenharmony_ci
9191cb0ef41Sopenharmony_ci  report::WriteNodeReport(
9201cb0ef41Sopenharmony_ci      isolate, env, message, trigger, filename, *outstream, error, compact);
9211cb0ef41Sopenharmony_ci
9221cb0ef41Sopenharmony_ci  // Do not close stdout/stderr, only close files we opened.
9231cb0ef41Sopenharmony_ci  if (outfile.is_open()) {
9241cb0ef41Sopenharmony_ci    outfile.close();
9251cb0ef41Sopenharmony_ci  }
9261cb0ef41Sopenharmony_ci
9271cb0ef41Sopenharmony_ci  // Do not mix JSON and free-form text on stderr.
9281cb0ef41Sopenharmony_ci  if (filename != "stderr") {
9291cb0ef41Sopenharmony_ci    std::cerr << "\nNode.js report completed" << std::endl;
9301cb0ef41Sopenharmony_ci  }
9311cb0ef41Sopenharmony_ci  return filename;
9321cb0ef41Sopenharmony_ci}
9331cb0ef41Sopenharmony_ci
9341cb0ef41Sopenharmony_ci// External function to trigger a report, writing to file.
9351cb0ef41Sopenharmony_cistd::string TriggerNodeReport(Isolate* isolate,
9361cb0ef41Sopenharmony_ci                              const char* message,
9371cb0ef41Sopenharmony_ci                              const char* trigger,
9381cb0ef41Sopenharmony_ci                              const std::string& name,
9391cb0ef41Sopenharmony_ci                              Local<Value> error) {
9401cb0ef41Sopenharmony_ci  Environment* env = nullptr;
9411cb0ef41Sopenharmony_ci  if (isolate != nullptr) {
9421cb0ef41Sopenharmony_ci    env = Environment::GetCurrent(isolate);
9431cb0ef41Sopenharmony_ci  }
9441cb0ef41Sopenharmony_ci  return TriggerNodeReport(isolate, env, message, trigger, name, error);
9451cb0ef41Sopenharmony_ci}
9461cb0ef41Sopenharmony_ci
9471cb0ef41Sopenharmony_ci// External function to trigger a report, writing to file.
9481cb0ef41Sopenharmony_cistd::string TriggerNodeReport(Environment* env,
9491cb0ef41Sopenharmony_ci                              const char* message,
9501cb0ef41Sopenharmony_ci                              const char* trigger,
9511cb0ef41Sopenharmony_ci                              const std::string& name,
9521cb0ef41Sopenharmony_ci                              Local<Value> error) {
9531cb0ef41Sopenharmony_ci  return TriggerNodeReport(env != nullptr ? env->isolate() : nullptr,
9541cb0ef41Sopenharmony_ci                           env,
9551cb0ef41Sopenharmony_ci                           message,
9561cb0ef41Sopenharmony_ci                           trigger,
9571cb0ef41Sopenharmony_ci                           name,
9581cb0ef41Sopenharmony_ci                           error);
9591cb0ef41Sopenharmony_ci}
9601cb0ef41Sopenharmony_ci
9611cb0ef41Sopenharmony_ci// External function to trigger a report, writing to a supplied stream.
9621cb0ef41Sopenharmony_civoid GetNodeReport(Isolate* isolate,
9631cb0ef41Sopenharmony_ci                   const char* message,
9641cb0ef41Sopenharmony_ci                   const char* trigger,
9651cb0ef41Sopenharmony_ci                   Local<Value> error,
9661cb0ef41Sopenharmony_ci                   std::ostream& out) {
9671cb0ef41Sopenharmony_ci  Environment* env = nullptr;
9681cb0ef41Sopenharmony_ci  if (isolate != nullptr) {
9691cb0ef41Sopenharmony_ci    env = Environment::GetCurrent(isolate);
9701cb0ef41Sopenharmony_ci  }
9711cb0ef41Sopenharmony_ci  report::WriteNodeReport(
9721cb0ef41Sopenharmony_ci      isolate, env, message, trigger, "", out, error, false);
9731cb0ef41Sopenharmony_ci}
9741cb0ef41Sopenharmony_ci
9751cb0ef41Sopenharmony_ci// External function to trigger a report, writing to a supplied stream.
9761cb0ef41Sopenharmony_civoid GetNodeReport(Environment* env,
9771cb0ef41Sopenharmony_ci                   const char* message,
9781cb0ef41Sopenharmony_ci                   const char* trigger,
9791cb0ef41Sopenharmony_ci                   Local<Value> error,
9801cb0ef41Sopenharmony_ci                   std::ostream& out) {
9811cb0ef41Sopenharmony_ci  Isolate* isolate = nullptr;
9821cb0ef41Sopenharmony_ci  if (env != nullptr) {
9831cb0ef41Sopenharmony_ci    isolate = env->isolate();
9841cb0ef41Sopenharmony_ci  }
9851cb0ef41Sopenharmony_ci  report::WriteNodeReport(
9861cb0ef41Sopenharmony_ci      isolate, env, message, trigger, "", out, error, false);
9871cb0ef41Sopenharmony_ci}
9881cb0ef41Sopenharmony_ci
9891cb0ef41Sopenharmony_ci}  // namespace node
990