Skip to content

Commit 8341898

Browse files
report: add --report-disable-network option
New option `--report-disable-network`, also available as `report.disableNetwork`, enables the user to disable networking interfaces in their diagnostic report. On some systems, this can cause the report to take minutes to generate so this option can be used to optimize that. fixes: #46060
1 parent 68885d5 commit 8341898

File tree

7 files changed

+106
-9
lines changed

7 files changed

+106
-9
lines changed

lib/internal/process/report.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ const report = {
6060
validateBoolean(b, 'compact');
6161
nr.setCompact(b);
6262
},
63+
get disableNetwork() {
64+
return nr.getDisableNetwork();
65+
},
66+
set disableNetwork(b) {
67+
validateBoolean(b, 'disableNetwork');
68+
nr.setDisableNetwork(b);
69+
},
6370
get signal() {
6471
return nr.getSignal();
6572
},

src/node_options.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,11 @@ PerProcessOptionsParser::PerProcessOptionsParser(
950950
"generate diagnostic report on fatal (internal) errors",
951951
&PerProcessOptions::report_on_fatalerror,
952952
kAllowedInEnvvar);
953+
AddOption("--report-disable-network",
954+
"disable network interface diagnostics."
955+
" (default: false)",
956+
&PerProcessOptions::report_disable_network,
957+
kAllowedInEnvvar);
953958

954959
#ifdef NODE_HAVE_I18N_SUPPORT
955960
AddOption("--icu-data-dir",

src/node_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ class PerProcessOptions : public Options {
306306
bool report_compact = false;
307307
std::string report_directory;
308308
std::string report_filename;
309+
bool report_disable_network = false;
309310

310311
// TODO(addaleax): Some of these could probably be per-Environment.
311312
std::string use_largepages = "off";

src/node_report.cc

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ static void WriteNodeReport(Isolate* isolate,
6161
const std::string& filename,
6262
std::ostream& out,
6363
Local<Value> error,
64-
bool compact);
65-
static void PrintVersionInformation(JSONWriter* writer);
64+
bool compact,
65+
bool disable_network);
66+
static void PrintVersionInformation(JSONWriter* writer, bool disable_network);
6667
static void PrintJavaScriptErrorStack(JSONWriter* writer,
6768
Isolate* isolate,
6869
Local<Value> error,
@@ -93,7 +94,8 @@ static void WriteNodeReport(Isolate* isolate,
9394
const std::string& filename,
9495
std::ostream& out,
9596
Local<Value> error,
96-
bool compact) {
97+
bool compact,
98+
bool disable_network) {
9799
// Obtain the current time and the pid.
98100
TIME_TYPE tm_struct;
99101
DiagnosticFilename::LocalTime(&tm_struct);
@@ -174,7 +176,7 @@ static void WriteNodeReport(Isolate* isolate,
174176
}
175177

176178
// Report Node.js and OS version information
177-
PrintVersionInformation(&writer);
179+
PrintVersionInformation(&writer, disable_network);
178180
writer.json_objectend();
179181

180182
if (isolate != nullptr) {
@@ -256,7 +258,7 @@ static void WriteNodeReport(Isolate* isolate,
256258
}
257259

258260
// Report Node.js version, OS version and machine information.
259-
static void PrintVersionInformation(JSONWriter* writer) {
261+
static void PrintVersionInformation(JSONWriter* writer, bool disable_network) {
260262
std::ostringstream buf;
261263
// Report Node version
262264
buf << "v" << NODE_VERSION_STRING;
@@ -300,7 +302,8 @@ static void PrintVersionInformation(JSONWriter* writer) {
300302
}
301303

302304
PrintCpuInfo(writer);
303-
PrintNetworkInterfaceInfo(writer);
305+
if (!disable_network)
306+
PrintNetworkInterfaceInfo(writer);
304307

305308
char host[UV_MAXHOSTNAMESIZE];
306309
size_t host_size = sizeof(host);
@@ -917,8 +920,14 @@ std::string TriggerNodeReport(Isolate* isolate,
917920
compact = per_process::cli_options->report_compact;
918921
}
919922

923+
bool disable_network;
924+
{
925+
Mutex::ScopedLock lock(per_process::cli_options_mutex);
926+
disable_network = per_process::cli_options->report_disable_network;
927+
}
928+
920929
report::WriteNodeReport(
921-
isolate, env, message, trigger, filename, *outstream, error, compact);
930+
isolate, env, message, trigger, filename, *outstream, error, compact, disable_network);
922931

923932
// Do not close stdout/stderr, only close files we opened.
924933
if (outfile.is_open()) {
@@ -969,8 +978,13 @@ void GetNodeReport(Isolate* isolate,
969978
if (isolate != nullptr) {
970979
env = Environment::GetCurrent(isolate);
971980
}
981+
bool disable_network;
982+
{
983+
Mutex::ScopedLock lock(per_process::cli_options_mutex);
984+
disable_network = per_process::cli_options->report_disable_network;
985+
}
972986
report::WriteNodeReport(
973-
isolate, env, message, trigger, "", out, error, false);
987+
isolate, env, message, trigger, "", out, error, false, disable_network);
974988
}
975989

976990
// External function to trigger a report, writing to a supplied stream.
@@ -983,8 +997,13 @@ void GetNodeReport(Environment* env,
983997
if (env != nullptr) {
984998
isolate = env->isolate();
985999
}
1000+
bool disable_network;
1001+
{
1002+
Mutex::ScopedLock lock(per_process::cli_options_mutex);
1003+
disable_network = per_process::cli_options->report_disable_network;
1004+
}
9861005
report::WriteNodeReport(
987-
isolate, env, message, trigger, "", out, error, false);
1006+
isolate, env, message, trigger, "", out, error, false, disable_network);
9881007
}
9891008

9901009
} // namespace node

src/node_report_module.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ static void SetCompact(const FunctionCallbackInfo<Value>& info) {
8484
per_process::cli_options->report_compact = compact;
8585
}
8686

87+
static void GetDisableNetwork(const FunctionCallbackInfo<Value>& info) {
88+
Mutex::ScopedLock lock(per_process::cli_options_mutex);
89+
info.GetReturnValue().Set(per_process::cli_options->report_disable_network);
90+
}
91+
92+
static void SetDisableNetwork(const FunctionCallbackInfo<Value>& info) {
93+
Mutex::ScopedLock lock(per_process::cli_options_mutex);
94+
Environment* env = Environment::GetCurrent(info);
95+
Isolate* isolate = env->isolate();
96+
bool disable_network = info[0]->ToBoolean(isolate)->Value();
97+
per_process::cli_options->report_disable_network = disable_network;
98+
}
99+
87100
static void GetDirectory(const FunctionCallbackInfo<Value>& info) {
88101
Mutex::ScopedLock lock(per_process::cli_options_mutex);
89102
Environment* env = Environment::GetCurrent(info);
@@ -174,6 +187,8 @@ static void Initialize(Local<Object> exports,
174187
SetMethod(context, exports, "getReport", GetReport);
175188
SetMethod(context, exports, "getCompact", GetCompact);
176189
SetMethod(context, exports, "setCompact", SetCompact);
190+
SetMethod(context, exports, "getDisableNetwork", GetDisableNetwork);
191+
SetMethod(context, exports, "setDisableNetwork", SetDisableNetwork);
177192
SetMethod(context, exports, "getDirectory", GetDirectory);
178193
SetMethod(context, exports, "setDirectory", SetDirectory);
179194
SetMethod(context, exports, "getFilename", GetFilename);
@@ -200,6 +215,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
200215
registry->Register(GetReport);
201216
registry->Register(GetCompact);
202217
registry->Register(SetCompact);
218+
registry->Register(GetDisableNetwork);
219+
registry->Register(SetDisableNetwork);
203220
registry->Register(GetDirectory);
204221
registry->Register(SetDirectory);
205222
registry->Register(GetFilename);

test/report/test-report-config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ assert.throws(() => {
6666
}, { code: 'ERR_INVALID_ARG_TYPE' });
6767
assert.strictEqual(process.report.compact, true);
6868

69+
// Verify that process.report.reportDisableNetwork behaves properly.
70+
assert.strictEqual(process.report.disableNetwork, true);
71+
process.report.disableNetwork = false;
72+
assert.strictEqual(process.report.disableNetwork, false);
73+
process.report.disableNetwork = true;
74+
assert.strictEqual(process.report.disableNetwork, true);
75+
assert.throws(() => {
76+
process.report.disableNetwork = {};
77+
}, { code: 'ERR_INVALID_ARG_TYPE' });
78+
assert.strictEqual(process.report.disableNetwork, true);
79+
6980
if (!common.isWindows) {
7081
// Verify that process.report.signal behaves properly.
7182
assert.strictEqual(process.report.signal, 'SIGUSR2');
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
const assert = require('node:assert/strict');
3+
const { spawnSync } = require('node:child_process');
4+
const tmpdir = require('../common/tmpdir');
5+
const { describe, it, before } = require('node:test');
6+
const fs = require('node:fs');
7+
const helper = require('../common/report');
8+
9+
function validate(pid) {
10+
const reports = helper.findReports(pid, tmpdir.path);
11+
assert.equal(reports.length, 1);
12+
let report = fs.readFileSync(reports[0], { encoding: 'utf8' });
13+
report = JSON.parse(report);
14+
assert.equal(report.header.networkInterfaces, undefined);
15+
fs.unlinkSync(reports[0]);
16+
}
17+
18+
describe('report disable network option', () => {
19+
before(() => {
20+
tmpdir.refresh();
21+
process.report.directory = tmpdir.path;
22+
});
23+
24+
it('should be configurable with --report-disable-network', () => {
25+
const args = ['--report-disable-network', '-e', 'process.report.writeReport()'];
26+
const child = spawnSync(process.execPath, args, { cwd: tmpdir.path });
27+
assert.equal(child.status, 0);
28+
assert.equal(child.signal, null);
29+
validate(child.pid);
30+
});
31+
32+
it('should be configurable with report.disableNetwork', () => {
33+
process.report.disableNetwork = true;
34+
process.report.writeReport();
35+
validate(process.pid);
36+
});
37+
});

0 commit comments

Comments
 (0)