Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: add --title command line argument, trace event #21477

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,13 @@ added: v0.11.14

Throw errors for deprecations.

### `--title=title`
<!-- YAML
added: REPLACEME
-->

Set `process.title` on startup.

### `--tls-cipher-list=list`
<!-- YAML
added: v4.0.0
Expand Down Expand Up @@ -532,6 +539,7 @@ Node options that are allowed are:
- `--redirect-warnings`
- `--require`, `-r`
- `--throw-deprecation`
- `--title`
- `--tls-cipher-list`
- `--trace-deprecation`
- `--trace-event-categories`
Expand Down
3 changes: 3 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ instead of printing to stderr.
.It Fl -throw-deprecation
Throw errors for deprecations.
.
.It Fl -title Ns = Ns Ar title
Specify process.title on startup.
.
.It Fl -tls-cipher-list Ns = Ns Ar list
Specify an alternative default TLS cipher list.
Requires Node.js to be built with crypto support. (Default)
Expand Down
19 changes: 19 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ std::string config_warning_file; // NOLINT(runtime/string)
// that is used by lib/internal/bootstrap/node.js
bool config_expose_internals = false;

std::string config_process_title; // NOLINT(runtime/string)

bool v8_initialized = false;

bool linux_at_secure = false;
Expand Down Expand Up @@ -1655,6 +1657,8 @@ static void ProcessTitleSetter(Local<Name> property,
Local<Value> value,
const PropertyCallbackInfo<void>& info) {
node::Utf8Value title(info.GetIsolate(), value);
TRACE_EVENT_METADATA1("__metadata", "process_name", "name",
TRACE_STR_COPY(*title));
uv_set_process_title(*title);
}

Expand Down Expand Up @@ -2506,6 +2510,7 @@ static void PrintHelp() {
" write warnings to file instead of\n"
" stderr\n"
" --throw-deprecation throw an exception on deprecations\n"
" --title=title the process title to use on start up\n"
#if HAVE_OPENSSL
" --tls-cipher-list=val use an alternative default TLS cipher "
"list\n"
Expand Down Expand Up @@ -2643,6 +2648,7 @@ static void CheckIfAllowedInEnv(const char* exe, bool is_env,
"--redirect-warnings",
"--require",
"--throw-deprecation",
"--title",
"--tls-cipher-list",
"--trace-deprecation",
"--trace-event-categories",
Expand Down Expand Up @@ -2813,6 +2819,8 @@ static void ParseArgs(int* argc,
} else if (strncmp(arg, "--security-revert=", 18) == 0) {
const char* cve = arg + 18;
Revert(cve);
} else if (strncmp(arg, "--title=", 8) == 0) {
config_process_title = arg + 8;
} else if (strcmp(arg, "--preserve-symlinks") == 0) {
config_preserve_symlinks = true;
} else if (strcmp(arg, "--preserve-symlinks-main") == 0) {
Expand Down Expand Up @@ -3304,6 +3312,10 @@ void Init(int* argc,

ProcessArgv(argc, argv, exec_argc, exec_argv);

// Set the process.title immediately after processing argv if --title is set.
if (!config_process_title.empty())
uv_set_process_title(config_process_title.c_str());

#if defined(NODE_HAVE_I18N_SUPPORT)
// If the parameter isn't given, use the env variable.
if (icu_data_dir.empty())
Expand Down Expand Up @@ -3528,6 +3540,13 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
Environment env(isolate_data, context, v8_platform.GetTracingAgent());
env.Start(argc, argv, exec_argc, exec_argv, v8_is_profiling);

char name_buffer[512];
if (uv_get_process_title(name_buffer, sizeof(name_buffer)) == 0) {
// Only emit the metadata event if the title can be retrieved successfully.
// Ignore it otherwise.
TRACE_EVENT_METADATA1("__metadata", "process_name", "name",
TRACE_STR_COPY(name_buffer));
}
TRACE_EVENT_METADATA1("__metadata", "version", "node", NODE_VERSION_STRING);
TRACE_EVENT_METADATA1("__metadata", "thread_name", "name",
"JavaScriptMainThread");
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-process-title-cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Flags: --title=foo
'use strict';

const common = require('../common');

if (common.isSunOS)
common.skip(`Unsupported platform [${process.platform}]`);

const assert = require('assert');

// Verifies that the --title=foo command line flag set the process
// title on startup.
assert.strictEqual(process.title, 'foo');
13 changes: 12 additions & 1 deletion test/parallel/test-trace-events-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ if (!common.isMainThread)
common.skip('process.chdir is not available in Workers');

const CODE =
'setTimeout(() => { for (var i = 0; i < 100000; i++) { "test" + i } }, 1)';
'setTimeout(() => { for (var i = 0; i < 100000; i++) { "test" + i } }, 1);' +
'process.title = "foo"';
const FILE_NAME = 'node_trace.1.log';

const tmpdir = require('../common/tmpdir');
Expand All @@ -17,6 +18,7 @@ process.chdir(tmpdir.path);

const proc = cp.spawn(process.execPath,
[ '--trace-event-categories', 'node.perf.usertiming',
'--title=bar',
'-e', CODE ]);
proc.once('exit', common.mustCall(() => {
assert(common.fileExists(FILE_NAME));
Expand All @@ -32,5 +34,14 @@ proc.once('exit', common.mustCall(() => {
assert(traces.some((trace) =>
trace.cat === '__metadata' && trace.name === 'version' &&
trace.args.node === process.versions.node));
if (!common.isSunOS) {
// Changing process.title is currently unsupported on SunOS/SmartOS
assert(traces.some((trace) =>
trace.cat === '__metadata' && trace.name === 'process_name' &&
trace.args.name === 'foo'));
assert(traces.some((trace) =>
trace.cat === '__metadata' && trace.name === 'process_name' &&
trace.args.name === 'bar'));
}
}));
}));