mirror of
https://github.com/nodejs/node.git
synced 2026-10-10 18:59:49 -04:00
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>
101 lines
2.1 KiB
JavaScript
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,
|
|
};
|