From 816d37a18724f4ade8b592d2f5b3e19b97cf32ee Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 27 Nov 2024 10:44:16 +0100 Subject: [PATCH] cli: implement --trace-env and --trace-env-[js|native]-stack This implements --trace-env, --trace-env-js-stack and --trace-env-native-stack CLI options which can be used to find out what environment variables are accessed and where they are accessed. PR-URL: https://github.com/nodejs/node/pull/55604 Reviewed-By: Chengzhong Wu Reviewed-By: James M Snell --- doc/api/cli.md | 42 +++++++++++ src/debug_utils.cc | 4 +- src/debug_utils.h | 4 +- src/env.cc | 15 ++-- src/node.cc | 2 +- src/node_credentials.cc | 33 ++++++--- src/node_env_var.cc | 62 +++++++++++++++- src/node_internals.h | 7 +- src/node_options.cc | 18 +++++ src/node_options.h | 3 + src/path.cc | 2 +- test/fixtures/process-env/define.js | 6 ++ test/fixtures/process-env/delete.js | 1 + test/fixtures/process-env/enumerate.js | 3 + test/fixtures/process-env/get.js | 2 + test/fixtures/process-env/query.js | 3 + test/fixtures/process-env/set.js | 1 + test/parallel/test-trace-env-stack.js | 84 ++++++++++++++++++++++ test/parallel/test-trace-env.js | 99 ++++++++++++++++++++++++++ 19 files changed, 365 insertions(+), 26 deletions(-) create mode 100644 test/fixtures/process-env/define.js create mode 100644 test/fixtures/process-env/delete.js create mode 100644 test/fixtures/process-env/enumerate.js create mode 100644 test/fixtures/process-env/get.js create mode 100644 test/fixtures/process-env/query.js create mode 100644 test/fixtures/process-env/set.js create mode 100644 test/parallel/test-trace-env-stack.js create mode 100644 test/parallel/test-trace-env.js diff --git a/doc/api/cli.md b/doc/api/cli.md index c41fcb075db645..11f1e4cb3cde36 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -2572,6 +2572,45 @@ added: v0.8.0 Print stack traces for deprecations. +### `--trace-env` + + + +Print information about any access to environment variables done in the current Node.js +instance to stderr, including: + +* The environment variable reads that Node.js does internally. +* Writes in the form of `process.env.KEY = "SOME VALUE"`. +* Reads in the form of `process.env.KEY`. +* Definitions in the form of `Object.defineProperty(process.env, 'KEY', {...})`. +* Queries in the form of `Object.hasOwn(process.env, 'KEY')`, + `process.env.hasOwnProperty('KEY')` or `'KEY' in process.env`. +* Deletions in the form of `delete process.env.KEY`. +* Enumerations inf the form of `...process.env` or `Object.keys(process.env)`. + +Only the names of the environment variables being accessed are printed. The values are not printed. + +To print the stack trace of the access, use `--trace-env-js-stack` and/or +`--trace-env-native-stack`. + +### `--trace-env-js-stack` + + + +In addition to what `--trace-env` does, this prints the JavaScript stack trace of the access. + +### `--trace-env-native-stack` + + + +In addition to what `--trace-env` does, this prints the native stack trace of the access. + ### `--trace-event-categories`