2017-07-17 14:51:26 +00:00
|
|
|
'use strict';
|
|
|
|
|
2020-01-02 13:27:54 +00:00
|
|
|
const {
|
2020-11-18 09:53:25 +00:00
|
|
|
SafeSet,
|
2020-01-02 13:27:54 +00:00
|
|
|
} = primordials;
|
|
|
|
|
2024-04-21 16:53:08 +00:00
|
|
|
let hook;
|
|
|
|
let config;
|
|
|
|
|
2018-05-07 02:25:26 +00:00
|
|
|
function lazyHookCreation() {
|
2019-01-10 15:32:05 +00:00
|
|
|
const inspector = internalBinding('inspector');
|
2018-05-07 02:25:26 +00:00
|
|
|
const { createHook } = require('async_hooks');
|
2018-10-08 23:10:07 +00:00
|
|
|
config = internalBinding('config');
|
2018-05-07 02:25:26 +00:00
|
|
|
|
|
|
|
hook = createHook({
|
|
|
|
init(asyncId, type, triggerAsyncId, resource) {
|
2017-07-17 14:51:26 +00:00
|
|
|
// It's difficult to tell which tasks will be recurring and which won't,
|
|
|
|
// therefore we mark all tasks as recurring. Based on the discussion
|
|
|
|
// in https://github.com/nodejs/node/pull/13870#discussion_r124515293,
|
|
|
|
// this should be fine as long as we call asyncTaskCanceled() too.
|
2018-05-07 02:25:26 +00:00
|
|
|
const recurring = true;
|
|
|
|
if (type === 'PROMISE')
|
|
|
|
this.promiseIds.add(asyncId);
|
|
|
|
else
|
|
|
|
inspector.asyncTaskScheduled(type, asyncId, recurring);
|
|
|
|
},
|
2017-07-17 14:51:26 +00:00
|
|
|
|
2018-05-07 02:25:26 +00:00
|
|
|
before(asyncId) {
|
|
|
|
if (this.promiseIds.has(asyncId))
|
|
|
|
return;
|
|
|
|
inspector.asyncTaskStarted(asyncId);
|
|
|
|
},
|
2017-07-17 14:51:26 +00:00
|
|
|
|
2018-05-07 02:25:26 +00:00
|
|
|
after(asyncId) {
|
|
|
|
if (this.promiseIds.has(asyncId))
|
|
|
|
return;
|
|
|
|
inspector.asyncTaskFinished(asyncId);
|
|
|
|
},
|
2017-07-17 14:51:26 +00:00
|
|
|
|
2018-05-07 02:25:26 +00:00
|
|
|
destroy(asyncId) {
|
|
|
|
if (this.promiseIds.has(asyncId))
|
|
|
|
return this.promiseIds.delete(asyncId);
|
|
|
|
inspector.asyncTaskCanceled(asyncId);
|
|
|
|
},
|
|
|
|
});
|
2017-07-17 14:51:26 +00:00
|
|
|
|
2020-11-18 09:53:25 +00:00
|
|
|
hook.promiseIds = new SafeSet();
|
2018-05-07 02:25:26 +00:00
|
|
|
}
|
2017-11-18 15:37:07 +00:00
|
|
|
|
2017-07-17 14:51:26 +00:00
|
|
|
function enable() {
|
2018-05-07 02:25:26 +00:00
|
|
|
if (hook === undefined) lazyHookCreation();
|
2017-07-17 14:51:26 +00:00
|
|
|
if (config.bits < 64) {
|
|
|
|
// V8 Inspector stores task ids as (void*) pointers.
|
|
|
|
// async_hooks store ids as 64bit numbers.
|
|
|
|
// As a result, we cannot reliably translate async_hook ids to V8 async_task
|
|
|
|
// ids on 32bit platforms.
|
|
|
|
process.emitWarning(
|
|
|
|
'Warning: Async stack traces in debugger are not available ' +
|
|
|
|
`on ${config.bits}bit platforms. The feature is disabled.`,
|
|
|
|
{
|
|
|
|
code: 'INSPECTOR_ASYNC_STACK_TRACES_NOT_AVAILABLE',
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
hook.enable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function disable() {
|
2018-05-07 02:25:26 +00:00
|
|
|
if (hook === undefined) lazyHookCreation();
|
2017-07-17 14:51:26 +00:00
|
|
|
hook.disable();
|
|
|
|
}
|
|
|
|
|
2019-01-10 15:32:05 +00:00
|
|
|
module.exports = {
|
|
|
|
enable,
|
2023-02-26 10:34:02 +00:00
|
|
|
disable,
|
2017-07-17 14:51:26 +00:00
|
|
|
};
|