Skip to content

Commit 2e3ca1b

Browse files
RafaelGSSruyadorno
authored andcommitted
src: add cli option to preserve env vars on dr
PR-URL: #55697 Backport-PR-URL: #56055 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Richard Lau <rlau@redhat.com>
1 parent 552a182 commit 2e3ca1b

File tree

12 files changed

+199
-14
lines changed

12 files changed

+199
-14
lines changed

doc/api/cli.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,6 +2049,15 @@ Enables report to be generated upon receiving the specified (or predefined)
20492049
signal to the running Node.js process. The signal to trigger the report is
20502050
specified through `--report-signal`.
20512051

2052+
### `--report-exclude-env`
2053+
2054+
<!-- YAML
2055+
added: REPLACEME
2056+
-->
2057+
2058+
When `--report-exclude-env` is passed the diagnostic report generated will not
2059+
contain the `environmentVariables` data.
2060+
20522061
### `--report-signal=signal`
20532062

20542063
<!-- YAML
@@ -3136,6 +3145,7 @@ one is included in the list below.
31363145
* `--redirect-warnings`
31373146
* `--report-compact`
31383147
* `--report-dir`, `--report-directory`
3148+
* `--report-exclude-env`
31393149
* `--report-exclude-network`
31403150
* `--report-filename`
31413151
* `--report-on-fatalerror`

doc/api/process.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3478,6 +3478,16 @@ const { report } = require('node:process');
34783478
console.log(`Report on exception: ${report.reportOnUncaughtException}`);
34793479
```
34803480
3481+
### `process.report.excludeEnv`
3482+
3483+
<!-- YAML
3484+
added: REPLACEME
3485+
-->
3486+
3487+
* {boolean}
3488+
3489+
If `true`, a diagnostic report is generated without the environment variables.
3490+
34813491
### `process.report.signal`
34823492
34833493
<!-- YAML

doc/api/report.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
<!-- YAML
1212
changes:
13+
- version: REPLACEME
14+
pr-url: https://github.com/nodejs/node/pull/55697
15+
description: Added `--report-exclude-env` option for excluding environment variables from report generation.
1316
- version: v22.0.0
1417
pr-url: https://github.com/nodejs/node/pull/51645
1518
description: Added `--report-exclude-network` option for excluding networking operations that can slow down report generation in some cases.
@@ -507,6 +510,10 @@ meaning of `SIGUSR2` for the said purposes.
507510
in `libuv.*.(remote|local)Endpoint.host` from the diagnostic report.
508511
By default this is not set and the network interfaces are included.
509512

513+
* `--report-exclude-env` Exclude `environmentVariables` from the
514+
diagnostic report. By default this is not set and the environment
515+
variables are included.
516+
510517
A report can also be triggered via an API call from a JavaScript application:
511518

512519
```js

lib/internal/process/report.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ const report = {
105105

106106
nr.setReportOnUncaughtException(trigger);
107107
},
108+
get excludeEnv() {
109+
return nr.getExcludeEnv();
110+
},
111+
set excludeEnv(b) {
112+
validateBoolean(b, 'excludeEnv');
113+
nr.setExcludeEnv(b);
114+
},
108115
};
109116

110117
function addSignalHandler(sig) {

src/env-inl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,10 @@ inline bool Environment::is_in_heapsnapshot_heap_limit_callback() const {
915915
return is_in_heapsnapshot_heap_limit_callback_;
916916
}
917917

918+
inline bool Environment::report_exclude_env() const {
919+
return options_->report_exclude_env;
920+
}
921+
918922
inline void Environment::AddHeapSnapshotNearHeapLimitCallback() {
919923
DCHECK(!heapsnapshot_near_heap_limit_callback_added_);
920924
heapsnapshot_near_heap_limit_callback_added_ = true;

src/env.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,8 @@ class Environment final : public MemoryRetainer {
10481048
inline void set_heap_snapshot_near_heap_limit(uint32_t limit);
10491049
inline bool is_in_heapsnapshot_heap_limit_callback() const;
10501050

1051+
inline bool report_exclude_env() const;
1052+
10511053
inline void AddHeapSnapshotNearHeapLimitCallback();
10521054

10531055
inline void RemoveHeapSnapshotNearHeapLimitCallback(size_t heap_limit);

src/node_options.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,11 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
895895
&EnvironmentOptions::tls_max_v1_3,
896896
kAllowedInEnvvar);
897897

898+
AddOption("--report-exclude-env",
899+
"Exclude environment variables when generating report"
900+
" (default: false)",
901+
&EnvironmentOptions::report_exclude_env,
902+
kAllowedInEnvvar);
898903
AddOption("--report-exclude-network",
899904
"exclude network interface diagnostics."
900905
" (default: false)",

src/node_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ class EnvironmentOptions : public Options {
251251

252252
std::vector<std::string> user_argv;
253253

254+
bool report_exclude_env = false;
254255
bool report_exclude_network = false;
255256

256257
inline DebugOptions* get_debug_options() { return &debug_options_; }

src/node_report.cc

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ static void WriteNodeReport(Isolate* isolate,
6161
std::ostream& out,
6262
Local<Value> error,
6363
bool compact,
64-
bool exclude_network = false);
64+
bool exclude_network = false,
65+
bool exclude_env = false);
6566
static void PrintVersionInformation(JSONWriter* writer,
6667
bool exclude_network = false);
6768
static void PrintJavaScriptErrorStack(JSONWriter* writer,
@@ -78,6 +79,7 @@ static void PrintJavaScriptErrorProperties(JSONWriter* writer,
7879
static void PrintNativeStack(JSONWriter* writer);
7980
static void PrintResourceUsage(JSONWriter* writer);
8081
static void PrintGCStatistics(JSONWriter* writer, Isolate* isolate);
82+
static void PrintEnvironmentVariables(JSONWriter* writer);
8183
static void PrintSystemInformation(JSONWriter* writer);
8284
static void PrintLoadedLibraries(JSONWriter* writer);
8385
static void PrintComponentVersions(JSONWriter* writer);
@@ -95,7 +97,8 @@ static void WriteNodeReport(Isolate* isolate,
9597
std::ostream& out,
9698
Local<Value> error,
9799
bool compact,
98-
bool exclude_network) {
100+
bool exclude_network,
101+
bool exclude_env) {
99102
// Obtain the current time and the pid.
100103
TIME_TYPE tm_struct;
101104
DiagnosticFilename::LocalTime(&tm_struct);
@@ -251,6 +254,9 @@ static void WriteNodeReport(Isolate* isolate,
251254
writer.json_arrayend();
252255

253256
// Report operating system information
257+
if (exclude_env == false) {
258+
PrintEnvironmentVariables(&writer);
259+
}
254260
PrintSystemInformation(&writer);
255261

256262
writer.json_objectend();
@@ -696,8 +702,7 @@ static void PrintResourceUsage(JSONWriter* writer) {
696702
#endif // RUSAGE_THREAD
697703
}
698704

699-
// Report operating system information.
700-
static void PrintSystemInformation(JSONWriter* writer) {
705+
static void PrintEnvironmentVariables(JSONWriter* writer) {
701706
uv_env_item_t* envitems;
702707
int envcount;
703708
int r;
@@ -717,7 +722,10 @@ static void PrintSystemInformation(JSONWriter* writer) {
717722
}
718723

719724
writer->json_objectend();
725+
}
720726

727+
// Report operating system information.
728+
static void PrintSystemInformation(JSONWriter* writer) {
721729
#ifndef _WIN32
722730
static struct {
723731
const char* description;
@@ -917,6 +925,10 @@ std::string TriggerNodeReport(Isolate* isolate,
917925
bool exclude_network = env != nullptr ? env->options()->report_exclude_network
918926
: per_process::cli_options->per_isolate
919927
->per_env->report_exclude_network;
928+
bool exclude_env =
929+
env != nullptr
930+
? env->report_exclude_env()
931+
: per_process::cli_options->per_isolate->per_env->report_exclude_env;
920932

921933
report::WriteNodeReport(isolate,
922934
env,
@@ -926,7 +938,8 @@ std::string TriggerNodeReport(Isolate* isolate,
926938
*outstream,
927939
error,
928940
compact,
929-
exclude_network);
941+
exclude_network,
942+
exclude_env);
930943

931944
// Do not close stdout/stderr, only close files we opened.
932945
if (outfile.is_open()) {
@@ -980,8 +993,20 @@ void GetNodeReport(Isolate* isolate,
980993
bool exclude_network = env != nullptr ? env->options()->report_exclude_network
981994
: per_process::cli_options->per_isolate
982995
->per_env->report_exclude_network;
983-
report::WriteNodeReport(
984-
isolate, env, message, trigger, "", out, error, false, exclude_network);
996+
bool exclude_env =
997+
env != nullptr
998+
? env->report_exclude_env()
999+
: per_process::cli_options->per_isolate->per_env->report_exclude_env;
1000+
report::WriteNodeReport(isolate,
1001+
env,
1002+
message,
1003+
trigger,
1004+
"",
1005+
out,
1006+
error,
1007+
false,
1008+
exclude_network,
1009+
exclude_env);
9851010
}
9861011

9871012
// External function to trigger a report, writing to a supplied stream.
@@ -997,8 +1022,20 @@ void GetNodeReport(Environment* env,
9971022
bool exclude_network = env != nullptr ? env->options()->report_exclude_network
9981023
: per_process::cli_options->per_isolate
9991024
->per_env->report_exclude_network;
1000-
report::WriteNodeReport(
1001-
isolate, env, message, trigger, "", out, error, false, exclude_network);
1025+
bool exclude_env =
1026+
env != nullptr
1027+
? env->report_exclude_env()
1028+
: per_process::cli_options->per_isolate->per_env->report_exclude_env;
1029+
report::WriteNodeReport(isolate,
1030+
env,
1031+
message,
1032+
trigger,
1033+
"",
1034+
out,
1035+
error,
1036+
false,
1037+
exclude_network,
1038+
exclude_env);
10021039
}
10031040

10041041
} // namespace node

src/node_report_module.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ static void SetExcludeNetwork(const FunctionCallbackInfo<Value>& info) {
9595
env->options()->report_exclude_network = info[0]->IsTrue();
9696
}
9797

98+
static void GetExcludeEnv(const FunctionCallbackInfo<Value>& info) {
99+
Environment* env = Environment::GetCurrent(info);
100+
info.GetReturnValue().Set(env->report_exclude_env());
101+
}
102+
103+
static void SetExcludeEnv(const FunctionCallbackInfo<Value>& info) {
104+
Environment* env = Environment::GetCurrent(info);
105+
CHECK(info[0]->IsBoolean());
106+
env->options()->report_exclude_env = info[0]->IsTrue();
107+
}
108+
98109
static void GetDirectory(const FunctionCallbackInfo<Value>& info) {
99110
Mutex::ScopedLock lock(per_process::cli_options_mutex);
100111
Environment* env = Environment::GetCurrent(info);
@@ -187,6 +198,8 @@ static void Initialize(Local<Object> exports,
187198
SetMethod(context, exports, "setCompact", SetCompact);
188199
SetMethod(context, exports, "getExcludeNetwork", GetExcludeNetwork);
189200
SetMethod(context, exports, "setExcludeNetwork", SetExcludeNetwork);
201+
SetMethod(context, exports, "getExcludeEnv", GetExcludeEnv);
202+
SetMethod(context, exports, "setExcludeEnv", SetExcludeEnv);
190203
SetMethod(context, exports, "getDirectory", GetDirectory);
191204
SetMethod(context, exports, "setDirectory", SetDirectory);
192205
SetMethod(context, exports, "getFilename", GetFilename);
@@ -215,6 +228,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
215228
registry->Register(SetCompact);
216229
registry->Register(GetExcludeNetwork);
217230
registry->Register(SetExcludeNetwork);
231+
registry->Register(GetExcludeEnv);
232+
registry->Register(SetExcludeEnv);
218233
registry->Register(GetDirectory);
219234
registry->Register(SetDirectory);
220235
registry->Register(GetFilename);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy