Files
node/lib/trace_events.js
Shelley Vohr c636b05c9e trace_events: fix abort when Node.js does not own the V8 platform
Requiring `node:trace_events` and calling `createTracing()` aborted
the process, and `getEnabledCategories()` dereferenced a null pointer,
when Node.js runs on an embedder's own platform
(`kNoInitializeNodeV8Platform`): no `tracing::Agent` exists then, and
`lib/trace_events.js` only checked the compile-time `hasTracing` flag
and `ownsProcessState` before handing the null agent to the binding.

Have the binding report whether an agent exists and throw the existing
`ERR_TRACE_EVENTS_UNAVAILABLE` when it does not, as for a
`--without-v8-platform` build.

Refs: https://github.com/nodejs/node/pull/19803
Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65954
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2026-09-18 15:23:28 +00:00

101 lines
2.1 KiB
JavaScript

'use strict';
const {
ArrayPrototypeJoin,
SafeSet,
} = primordials;
const { hasTracing } = internalBinding('config');
const kMaxTracingCount = 10;
const {
ERR_TRACE_EVENTS_CATEGORY_REQUIRED,
ERR_TRACE_EVENTS_UNAVAILABLE,
} = require('internal/errors').codes;
const { ownsProcessState } = require('internal/worker');
const {
CategorySet,
getEnabledCategories,
hasAgent,
} = internalBinding('trace_events');
if (!hasTracing || !ownsProcessState || !hasAgent())
throw new ERR_TRACE_EVENTS_UNAVAILABLE();
const { customInspectSymbol } = require('internal/util');
const { format } = require('internal/util/inspect');
const {
validateObject,
validateStringArray,
} = require('internal/validators');
const enabledTracingObjects = new SafeSet();
class Tracing {
#handle;
#categories;
#enabled = false;
constructor(categories) {
this.#handle = new CategorySet(categories);
this.#categories = categories;
}
enable() {
if (!this.#enabled) {
this.#enabled = true;
this.#handle.enable();
enabledTracingObjects.add(this);
if (enabledTracingObjects.size > kMaxTracingCount) {
process.emitWarning(
'Possible trace_events memory leak detected. There are more than ' +
`${kMaxTracingCount} enabled Tracing objects.`,
);
}
}
}
disable() {
if (this.#enabled) {
this.#enabled = false;
this.#handle.disable();
enabledTracingObjects.delete(this);
}
}
get enabled() {
return this.#enabled;
}
get categories() {
return ArrayPrototypeJoin(this.#categories, ',');
}
[customInspectSymbol](depth, opts) {
if (typeof depth === 'number' && depth < 0)
return this;
const obj = {
enabled: this.enabled,
categories: this.categories,
};
return `Tracing ${format(obj)}`;
}
}
function createTracing(options) {
validateObject(options, 'options');
validateStringArray(options.categories, 'options.categories');
if (options.categories.length <= 0)
throw new ERR_TRACE_EVENTS_CATEGORY_REQUIRED();
return new Tracing(options.categories);
}
module.exports = {
createTracing,
getEnabledCategories,
};