diff --git a/src/node.cc b/src/node.cc index 0db313fd36b..4fb793705bd 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1665,6 +1665,8 @@ static void ProcessTitleSetter(Local property, Local value, const PropertyCallbackInfo& info) { node::Utf8Value title(info.GetIsolate(), value); + TRACE_EVENT_METADATA1("__metadata", "process_name", "name", + TRACE_STR_COPY(*title)); uv_set_process_title(*title); } @@ -3525,6 +3527,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"); diff --git a/test/parallel/test-trace-events-metadata.js b/test/parallel/test-trace-events-metadata.js index 440aa00a9c2..7f9ccc3c737 100644 --- a/test/parallel/test-trace-events-metadata.js +++ b/test/parallel/test-trace-events-metadata.js @@ -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'); @@ -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)); @@ -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')); + } })); }));