Skip to content

Commit 6f0e43b

Browse files
robertsipkayichoi
authored andcommitted
Support delayed stack trace loading
This capability of the Debug Adapter Protocol supports the delayed loading of parts of the stack. It requires that both the 'startFrame' and 'levels' arguments and the 'totalFrames' result of the 'StackTrace' requests are supported. It helps to avoid duplicated top stack frame and limited number of frames in the CALLSTACK pane (how many can be transmitted in one message). IoT.js-VSCode-DCO-1.0-Signed-off-by: Daniella Barsony [email protected] IoT.js-VSCode-DCO-1.0-Signed-off-by: Robert Sipka [email protected]
1 parent 354da80 commit 6f0e43b

File tree

4 files changed

+53
-34
lines changed

4 files changed

+53
-34
lines changed

src/IotjsDebugger.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { IAttachRequestArguments, SourceSendingOptions, TemporaryBreakpoint } fr
2828
import { JerryDebuggerClient, JerryDebuggerOptions } from './JerryDebuggerClient';
2929
import {
3030
JerryDebugProtocolDelegate, JerryDebugProtocolHandler, JerryMessageScriptParsed, JerryEvalResult,
31-
JerryMessageExceptionHit, JerryMessageBreakpointHit
31+
JerryMessageExceptionHit, JerryMessageBreakpointHit, JerryBacktraceResult
3232
} from './JerryProtocolHandler';
3333
import { EVAL_RESULT_SUBTYPE, CLIENT as CLIENT_PACKAGE } from './JerryProtocolConstants';
3434
import { Breakpoint } from './JerryBreakpoints';
@@ -76,7 +76,7 @@ class IotjsDebugSession extends DebugSession {
7676
response.body.supportsEvaluateForHovers = false;
7777
response.body.supportsStepBack = false;
7878
response.body.supportsRestartRequest = false;
79-
response.body.supportsDelayedStackTraceLoading = false;
79+
response.body.supportsDelayedStackTraceLoading = true;
8080

8181
this._sourceSendingOptions = <SourceSendingOptions>{
8282
contextReset: false,
@@ -353,18 +353,19 @@ class IotjsDebugSession extends DebugSession {
353353
response: DebugProtocol.StackTraceResponse, args: DebugProtocol.StackTraceArguments
354354
): Promise<void> {
355355
try {
356-
const backtrace = await this._protocolhandler.requestBacktrace();
357-
const stk = backtrace.map((f, i) => new StackFrame(
358-
1000 + i,
359-
f.func.name || 'global',
360-
this.pathToSource(`${this._args.localRoot}/${this.pathToBasename(f.func.sourceName)}`),
361-
f.line,
362-
f.func.column
363-
)
356+
const backtraceData: JerryBacktraceResult = await this._protocolhandler.requestBacktrace(args.startFrame,
357+
args.levels);
358+
const stk = backtraceData.backtrace.map((f, i) => new StackFrame(
359+
1000 + i,
360+
f.func.name || 'global',
361+
this.pathToSource(`${this._args.localRoot}/${this.pathToBasename(f.func.sourceName)}`),
362+
f.line,
363+
f.func.column)
364364
);
365365

366366
response.body = {
367367
stackFrames: stk,
368+
totalFrames: backtraceData.totalFrames
368369
};
369370

370371
this.sendResponse(response);

src/JerryProtocolConstants.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
'use strict';
1818

1919
// Expected JerryScript debugger protocol version.
20-
export const JERRY_DEBUGGER_VERSION = 3;
20+
export const JERRY_DEBUGGER_VERSION = 5;
2121

2222
// Packages sent from the server to the client.
2323
export enum SERVER {
@@ -40,13 +40,14 @@ export enum SERVER {
4040
JERRY_DEBUGGER_EXCEPTION_HIT = 17,
4141
JERRY_DEBUGGER_EXCEPTION_STR = 18,
4242
JERRY_DEBUGGER_EXCEPTION_STR_END = 19,
43-
JERRY_DEBUGGER_BACKTRACE = 20,
44-
JERRY_DEBUGGER_BACKTRACE_END = 21,
45-
JERRY_DEBUGGER_EVAL_RESULT = 22,
46-
JERRY_DEBUGGER_EVAL_RESULT_END = 23,
47-
JERRY_DEBUGGER_WAIT_FOR_SOURCE = 24,
48-
JERRY_DEBUGGER_OUTPUT_RESULT = 25,
49-
JERRY_DEBUGGER_OUTPUT_RESULT_END = 26
43+
JERRY_DEBUGGER_BACKTRACE_TOTAL = 20,
44+
JERRY_DEBUGGER_BACKTRACE = 21,
45+
JERRY_DEBUGGER_BACKTRACE_END = 22,
46+
JERRY_DEBUGGER_EVAL_RESULT = 23,
47+
JERRY_DEBUGGER_EVAL_RESULT_END = 24,
48+
JERRY_DEBUGGER_WAIT_FOR_SOURCE = 25,
49+
JERRY_DEBUGGER_OUTPUT_RESULT = 26,
50+
JERRY_DEBUGGER_OUTPUT_RESULT_END = 27
5051
}
5152

5253
// Subtypes of eval.

src/JerryProtocolHandler.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface ParserStackFrame {
4444
}
4545

4646
export interface JerryDebugProtocolDelegate {
47-
onBacktrace?(backtrace: Array<Breakpoint>): void;
47+
onBacktrace?(backtrace: JerryBacktraceResult): void;
4848
onBreakpointHit?(message: JerryMessageBreakpointHit, stopType: string): void;
4949
onExceptionHit?(message: JerryMessageExceptionHit): void;
5050
onEvalResult?(subType: number, result: string): void;
@@ -85,6 +85,11 @@ interface ProtocolFunctionMap {
8585
[type: number]: (data: Uint8Array) => void;
8686
}
8787

88+
export interface JerryBacktraceResult {
89+
totalFrames: number;
90+
backtrace: Array<Breakpoint>;
91+
}
92+
8893
interface FunctionMap {
8994
[cp: string]: ParsedFunction;
9095
}
@@ -143,7 +148,7 @@ export class JerryDebugProtocolHandler {
143148
private evalResultData?: Uint8Array;
144149
private functions: FunctionMap = {};
145150
private newFunctions: FunctionMap = {};
146-
private backtrace: Array<Breakpoint> = [];
151+
private backtraceData: JerryBacktraceResult = {totalFrames : 0, backtrace: []};
147152

148153
private nextScriptID: number = 1;
149154
private exceptionData?: Uint8Array;
@@ -187,6 +192,7 @@ export class JerryDebugProtocolHandler {
187192
[SP.SERVER.JERRY_DEBUGGER_EXCEPTION_HIT]: this.onBreakpointHit,
188193
[SP.SERVER.JERRY_DEBUGGER_EXCEPTION_STR]: this.onExceptionStr,
189194
[SP.SERVER.JERRY_DEBUGGER_EXCEPTION_STR_END]: this.onExceptionStr,
195+
[SP.SERVER.JERRY_DEBUGGER_BACKTRACE_TOTAL]: this.onBacktrace,
190196
[SP.SERVER.JERRY_DEBUGGER_BACKTRACE]: this.onBacktrace,
191197
[SP.SERVER.JERRY_DEBUGGER_BACKTRACE_END]: this.onBacktrace,
192198
[SP.SERVER.JERRY_DEBUGGER_EVAL_RESULT]: this.onEvalResult,
@@ -547,25 +553,26 @@ export class JerryDebugProtocolHandler {
547553
this.lastStopType = null;
548554
}
549555

550-
public onBacktrace(data: Uint8Array): Breakpoint[] {
556+
public onBacktrace(data: Uint8Array): JerryBacktraceResult {
551557
this.logPacket('Backtrace');
552-
for (let i = 1; i < data.byteLength; i += this.byteConfig.cpointerSize + 4) {
553-
const breakpointData = this.decodeMessage('CI', data, i);
554-
this.backtrace.push(this.getBreakpoint(breakpointData).breakpoint);
558+
559+
if (data[0] === SP.SERVER.JERRY_DEBUGGER_BACKTRACE_TOTAL) {
560+
this.backtraceData.totalFrames = this.decodeMessage('I', data, 1);
561+
this.backtraceData.backtrace = [];
562+
} else {
563+
for (let i = 1; i < data.byteLength; i += this.byteConfig.cpointerSize + 4) {
564+
const breakpointData = this.decodeMessage('CI', data, i);
565+
this.backtraceData.backtrace.push(this.getBreakpoint(breakpointData).breakpoint);
566+
}
555567
}
556568

557569
if (data[0] === SP.SERVER.JERRY_DEBUGGER_BACKTRACE_END) {
558570
if (this.delegate.onBacktrace) {
559-
this.delegate.onBacktrace(this.backtrace);
571+
this.delegate.onBacktrace(this.backtraceData);
560572
}
561-
562-
const bt = this.backtrace;
563-
this.backtrace = [];
564-
565-
return bt;
566573
}
567574

568-
return [];
575+
return this.backtraceData;
569576
}
570577

571578
public onEvalResult(data: Uint8Array): JerryEvalResult {
@@ -741,11 +748,21 @@ export class JerryDebugProtocolHandler {
741748
]));
742749
}
743750

744-
public requestBacktrace(): Promise<any> {
751+
public requestBacktrace(start?: number, levels?: number): Promise<any> {
752+
if (start === undefined)
753+
start = 0;
754+
if (levels === undefined)
755+
levels = 0;
756+
745757
if (!this.lastBreakpointHit) {
746758
return Promise.reject(new Error('backtrace not allowed while app running'));
747759
}
748-
return this.sendRequest(encodeMessage(this.byteConfig, 'BI', [SP.CLIENT.JERRY_DEBUGGER_GET_BACKTRACE, 0]));
760+
761+
return this.sendRequest(encodeMessage(this.byteConfig, 'BIIB',
762+
[SP.CLIENT.JERRY_DEBUGGER_GET_BACKTRACE,
763+
start,
764+
start + levels,
765+
1]));
749766
}
750767

751768
logPacket(description: string, ignorable: boolean = false) {

src/test/JerryProtocolHandler.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ suite('JerryProtocolHandler', () => {
342342

343343
test('aborts when unhandled message sent', () => {
344344
delegate.onError.resetHistory();
345-
const array = Uint8Array.from([SP.SERVER.JERRY_DEBUGGER_CONFIGURATION, 200, 4, 1, 3]);
345+
const array = Uint8Array.from([SP.SERVER.JERRY_DEBUGGER_CONFIGURATION, 200, 4, 1, 5]);
346346
handler.onMessage(array);
347347
assert(delegate.onError.notCalled);
348348
array[0] = 255;

0 commit comments

Comments
 (0)