From 8a50cefdcc9ce3cc8fb754c388fe840c8aa1fca6 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 29 Oct 2024 18:17:58 +0100 Subject: [PATCH 1/5] 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. --- doc/api/cli.md | 41 ++++++++++++ src/debug_utils.cc | 4 +- src/debug_utils.h | 4 +- src/env.cc | 15 +++-- src/node.cc | 2 +- src/node_credentials.cc | 38 ++++++++--- src/node_env_var.cc | 74 ++++++++++++++++++++- 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 | 85 ++++++++++++++++++++++++ test/parallel/test-trace-env.js | 89 ++++++++++++++++++++++++++ 19 files changed, 372 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 d232c34230276a..7a459f4677aa5e 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -2588,6 +2588,44 @@ added: v0.8.0 Print stack traces for deprecations. +### `--trace-env` + + + +Print reads and writes to environment variables. + +The following accesses will be printed: + +* 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)`. + +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` -Print reads and writes to environment variables. - -The following accesses will be printed: +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"`. @@ -2607,6 +2606,8 @@ The following accesses will be printed: * 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`.