From 2ce8b974a09cccd9378b57e43130f67ea57e739f Mon Sep 17 00:00:00 2001 From: theanarkh Date: Sun, 24 Dec 2023 00:35:27 +0800 Subject: [PATCH] src: fix unix abstract socket path for trace event PR-URL: https://github.com/nodejs/node/pull/50858 Reviewed-By: James M Snell --- src/pipe_wrap.cc | 8 +++- .../test-trace-events-net-abstract-socket.js | 43 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-trace-events-net-abstract-socket.js diff --git a/src/pipe_wrap.cc b/src/pipe_wrap.cc index e6f1f38120db71..3f7cf26709e57f 100644 --- a/src/pipe_wrap.cc +++ b/src/pipe_wrap.cc @@ -230,11 +230,15 @@ void PipeWrap::Connect(const FunctionCallbackInfo& args) { if (err) { delete req_wrap; } else { - TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(TRACING_CATEGORY_NODE2(net, native), + const char* path_type = (*name)[0] == '\0' ? "abstract socket" : "file"; + const char* pipe_path = (*name)[0] == '\0' ? (*name) + 1 : *name; + TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(TRACING_CATEGORY_NODE2(net, native), "connect", req_wrap, + "path_type", + path_type, "pipe_path", - TRACE_STR_COPY(*name)); + TRACE_STR_COPY(pipe_path)); } args.GetReturnValue().Set(err); diff --git a/test/parallel/test-trace-events-net-abstract-socket.js b/test/parallel/test-trace-events-net-abstract-socket.js new file mode 100644 index 00000000000000..d2e1546743c958 --- /dev/null +++ b/test/parallel/test-trace-events-net-abstract-socket.js @@ -0,0 +1,43 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const cp = require('child_process'); +const fs = require('fs'); +const tmpdir = require('../common/tmpdir'); + +if (!common.isLinux) common.skip(); + +const CODE = ` + const net = require('net'); + net.connect('${common.PIPE}').on('error', () => {}); + net.connect('\\0${common.PIPE}').on('error', () => {}); +`; + +tmpdir.refresh(); +const FILE_NAME = tmpdir.resolve('node_trace.1.log'); + +const proc = cp.spawn(process.execPath, + [ '--trace-events-enabled', + '--trace-event-categories', 'node.net.native', + '-e', CODE ], + { cwd: tmpdir.path }); + +proc.once('exit', common.mustCall(() => { + assert(fs.existsSync(FILE_NAME)); + fs.readFile(FILE_NAME, common.mustCall((err, data) => { + const traces = JSON.parse(data.toString()).traceEvents; + assert(traces.length > 0); + let count = 0; + traces.forEach((trace) => { + if (trace.cat === 'node,node.net,node.net.native' && + trace.name === 'connect') { + count++; + if (trace.ph === 'b') { + assert.ok(!!trace.args.path_type); + assert.ok(!!trace.args.pipe_path); + } + } + }); + assert.strictEqual(count, 4); + })); +}));