Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.

Commit 00506f7

Browse files
author
Jan Krems
committed
feat: backtrace/bt
1 parent 8823c60 commit 00506f7

4 files changed

Lines changed: 80 additions & 4 deletions

File tree

examples/backtrace.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
const { exports: moduleScoped } = module;
3+
4+
function topFn(a, b = false) {
5+
const l1 = a;
6+
let t = typeof l1;
7+
var v = t.length;
8+
debugger;
9+
return b || t || v || moduleScoped;
10+
}
11+
12+
class Ctor {
13+
constructor(options) {
14+
this.options = options;
15+
}
16+
17+
m() {
18+
const mLocal = this.options;
19+
topFn(this);
20+
return mLocal;
21+
}
22+
}
23+
24+
(function () {
25+
const theOptions = { x: 42 };
26+
const arr = [theOptions];
27+
arr.forEach(options => {
28+
const obj = new Ctor(options);
29+
return obj.m();
30+
});
31+
}());

lib/internal/inspect-repl.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const SHORTCUTS = {
3131
next: 'n',
3232
step: 's',
3333
out: 'o',
34-
// backtrace: 'bt',
34+
backtrace: 'bt',
3535
setBreakpoint: 'sb',
3636
// clearBreakpoint: 'cb',
3737
// pause_: 'pause',
@@ -116,7 +116,7 @@ function createRepl(inspector) {
116116
const watchedExpressions = [];
117117
const knownBreakpoints = [];
118118

119-
// let currentBacktrace;
119+
let currentBacktrace;
120120
let selectedFrame;
121121

122122
const print = inspector.print.bind(inspector);
@@ -148,6 +148,16 @@ function createRepl(inspector) {
148148
}
149149
}
150150

151+
function printBacktrace() {
152+
currentBacktrace.forEach((callFrame, idx) => {
153+
const { location: { scriptId, lineNumber, columnNumber }, functionName } = callFrame;
154+
const script = knownScripts[scriptId];
155+
const relativeUrl = (script && getRelativePath(script && script.url)) || '<unknown>';
156+
const name = functionName || '(anonymous)';
157+
print(`#${idx} ${name} ${relativeUrl}:${lineNumber + 1}:${columnNumber}`);
158+
});
159+
}
160+
151161
function watchers() {
152162
return Promise.resolve(watchedExpressions);
153163
}
@@ -297,7 +307,7 @@ function createRepl(inspector) {
297307

298308
Debugger.on('paused', ({ callFrames, reason /* , hitBreakpoints */ }) => {
299309
// Save execution context's data
300-
// currentBacktrace = callFrames;
310+
currentBacktrace = callFrames;
301311
selectedFrame = callFrames[0];
302312
const { scriptId, lineNumber } = selectedFrame.location;
303313

@@ -343,6 +353,10 @@ function createRepl(inspector) {
343353
return Debugger.pause();
344354
},
345355

356+
get backtrace() {
357+
return printBacktrace();
358+
},
359+
346360
setBreakpoint,
347361
list,
348362
});

test/cli/backtrace.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
const tap = require('tap');
3+
4+
const startCLI = require('./start-cli');
5+
6+
tap.test('display and navigate backtrace', (t) => {
7+
const cli = startCLI(['examples/backtrace.js']);
8+
9+
function onFatal(error) {
10+
cli.quit();
11+
throw error;
12+
}
13+
14+
return cli.waitFor(/break/)
15+
.then(() => cli.waitForPrompt())
16+
.then(() => cli.stepCommand('c'))
17+
.then(() => cli.command('bt'))
18+
.then(() => {
19+
t.match(cli.output, '#0 topFn examples/backtrace.js:8:2');
20+
})
21+
.then(() => cli.command('backtrace'))
22+
.then(() => {
23+
t.match(cli.output, '#0 topFn examples/backtrace.js:8:2');
24+
})
25+
.then(() => cli.quit())
26+
.then(null, onFatal);
27+
});

test/cli/start-cli.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function startCLI(args) {
1919

2020
function getOutput() {
2121
return outputBuffer.join('').toString()
22-
.replace(/^[^\n]*[\b]/mg, '\n');
22+
.replace(/^[^\n]*?[\b]/mg, '');
2323
}
2424

2525
child.stdout.setEncoding('utf8');
@@ -80,6 +80,10 @@ function startCLI(args) {
8080
return getOutput();
8181
},
8282

83+
get rawOutput() {
84+
return outputBuffer.join('').toString();
85+
},
86+
8387
parseSourceLines() {
8488
return getOutput().split('\n')
8589
.map((line) => line.match(/(?:\*|>)?\s*(\d+)/))

0 commit comments

Comments
 (0)