Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception support #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/lib/cdt-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export class CDTController {
// set, and before issuing further commands to the debugger
public proxyServer?: ChromeDevToolsProxyServer;
private scripts: Array<JerryMessageScriptParsed> = [];
private pendingException: boolean = false;
private pendingExceptionStr?: string;
private pendingBreakpoint?: Breakpoint;
// stores resolve and reject functions from promised evaluations
private evalResolvers: Array<PromiseFunctions> = [];
Expand All @@ -62,12 +64,23 @@ export class CDTController {
// this can happen before the proxy is connected
if (this.proxyServer) {
this.pendingBreakpoint = message.breakpoint;
this.pendingException = false;
this.protocolHandler!.requestBacktrace();
}
}

onExceptionHit(message: JerryMessageBreakpointHit, exception?: string) {
// this can happen before the proxy is connected
if (this.proxyServer) {
this.pendingBreakpoint = message.breakpoint;
this.pendingException = true;
this.pendingExceptionStr = exception;
this.protocolHandler!.requestBacktrace();
}
}

onBacktrace(backtrace: Array<Breakpoint>) {
this.sendPaused(this.pendingBreakpoint, backtrace);
this.sendPaused(this.pendingBreakpoint, backtrace, this.pendingException, this.pendingExceptionStr);
this.pendingBreakpoint = undefined;
}

Expand Down Expand Up @@ -215,10 +228,11 @@ export class CDTController {
}

// 'report' functions are events from Debugger to CDT
private sendPaused(breakpoint: Breakpoint | undefined, backtrace: Array<Breakpoint>) {
private sendPaused(breakpoint: Breakpoint | undefined, backtrace: Array<Breakpoint>,
isException: boolean, exception?: string) {
// Node uses 'Break on start' but this is not allowable in crdp.d.ts
const reason = breakpoint ? 'debugCommand' : 'other';
this.proxyServer!.sendPaused(breakpoint, backtrace, reason);
const reason = isException ? 'exception' : (breakpoint ? 'debugCommand' : 'other');
this.proxyServer!.sendPaused(breakpoint, backtrace, reason, exception);
}

/**
Expand Down
13 changes: 9 additions & 4 deletions src/lib/cdt-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import { Breakpoint } from './breakpoint';
import { onHttpRequest } from './cdt-proxy-http';
import { JerryMessageScriptParsed } from './protocol-handler';

export type PausedReason = 'exception' | 'debugCommand' | 'other';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the first | looks bolded, is it a format error of github or normal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do see that too... I think it's a font/resolution issue on our monitors. If I zoom in one click, the other one gets bolded. :)

Thanks for taking a look.

export type PauseForExceptions = 'none' | 'uncaught' | 'all';

export interface CDTDelegate {
requestScripts: () => void;
requestBreakpoint: () => void;
Expand Down Expand Up @@ -63,7 +66,7 @@ export class ChromeDevToolsProxyServer {
readonly uuid: string;
readonly jsfile: string;
private skipAllPauses: boolean = false;
private pauseOnExceptions: ('none' | 'uncaught' | 'all') = 'none';
private pauseOnExceptions: PauseForExceptions = 'none';
private asyncCallStackDepth: number = 0; // 0 is unlimited
private delegate: CDTDelegate;
private api: Crdp.CrdpServer;
Expand Down Expand Up @@ -186,7 +189,7 @@ export class ChromeDevToolsProxyServer {
* Sends Debugger.paused event for the current debugger location
*/
sendPaused(breakpoint: Breakpoint | undefined, backtrace: Array<Breakpoint>,
reason: 'exception' | 'debugCommand' | 'other') {
reason: PausedReason, exception?: string) {
const callFrames: Array<Crdp.Debugger.CallFrame> = [];
let nextFrameId = 0;
for (const bp of backtrace) {
Expand All @@ -209,10 +212,12 @@ export class ChromeDevToolsProxyServer {
hitBreakpoints.push(String(breakpoint.activeIndex));
}

const data = exception ? { message: exception } : undefined;
this.api.Debugger.emitPaused({
hitBreakpoints,
reason,
callFrames,
reason,
data,
hitBreakpoints,
});
}

Expand Down
35 changes: 27 additions & 8 deletions src/lib/protocol-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface JerryDebugProtocolDelegate {
onBacktrace?(backtrace: Array<Breakpoint>): void;
onBreakpointHit?(message: JerryMessageBreakpointHit): void;
onEvalResult?(subType: number, result: string): void;
onExceptionHit?(message: JerryMessageBreakpointHit, exception?: string): void;
onError?(code: number, message: string): void;
onResume?(): void;
onScriptParsed?(message: JerryMessageScriptParsed): void;
Expand Down Expand Up @@ -96,13 +97,14 @@ export class JerryDebugProtocolHandler {
private sourceNameData?: Uint8Array;
private functionName?: string;
private functionNameData?: Uint8Array;
private exceptionStr?: string;
private exceptionData?: Uint8Array;
private backtrace: Array<Breakpoint> = [];
private evalResultData?: Uint8Array;
private functions: FunctionMap = {};
private newFunctions: FunctionMap = {};
private backtrace: Array<Breakpoint> = [];

private nextScriptID: number = 1;
private exceptionData?: Uint8Array;
private evalsPending: number = 0;
private lastBreakpointHit?: Breakpoint;
private lastBreakpointExact: boolean = true;
Expand Down Expand Up @@ -131,6 +133,9 @@ export class JerryDebugProtocolHandler {
[SP.JERRY_DEBUGGER_FUNCTION_NAME_END]: this.onFunctionName,
[SP.JERRY_DEBUGGER_RELEASE_BYTE_CODE_CP]: this.onReleaseByteCodeCP,
[SP.JERRY_DEBUGGER_BREAKPOINT_HIT]: this.onBreakpointHit,
[SP.JERRY_DEBUGGER_EXCEPTION_HIT]: this.onBreakpointHit,
[SP.JERRY_DEBUGGER_EXCEPTION_STR]: this.onExceptionStr,
[SP.JERRY_DEBUGGER_EXCEPTION_STR_END]: this.onExceptionStr,
[SP.JERRY_DEBUGGER_BACKTRACE]: this.onBacktrace,
[SP.JERRY_DEBUGGER_BACKTRACE_END]: this.onBacktrace,
[SP.JERRY_DEBUGGER_EVAL_RESULT]: this.onEvalResult,
Expand Down Expand Up @@ -435,9 +440,8 @@ export class JerryDebugProtocolHandler {

if (data[0] === SP.JERRY_DEBUGGER_EXCEPTION_HIT) {
console.log('Exception throw detected');
if (this.exceptionData) {
console.log('Exception hint:', cesu8ToString(this.exceptionData));
this.exceptionData = undefined;
if (this.exceptionStr) {
console.log('Exception hint:', this.exceptionStr);
}
}

Expand All @@ -452,9 +456,24 @@ export class JerryDebugProtocolHandler {
const atAround = breakpointRef.exact ? 'at' : 'around';
console.log(`Stopped ${atAround} ${breakpointInfo}${breakpoint}`);

// TODO: handle exception case differently
if (this.delegate.onBreakpointHit) {
this.delegate.onBreakpointHit(breakpointRef);
if (data[0] === SP.JERRY_DEBUGGER_EXCEPTION_HIT) {
// TODO: handle exception case differently
if (this.delegate.onExceptionHit) {
this.delegate.onExceptionHit(breakpointRef, this.exceptionStr);
}
} else {
if (this.delegate.onBreakpointHit) {
this.delegate.onBreakpointHit(breakpointRef);
}
}
}

onExceptionStr(data: Uint8Array) {
this.logPacket('Exception Str');
this.exceptionData = assembleUint8Arrays(this.evalResultData, data);
if (data[0] === SP.JERRY_DEBUGGER_EXCEPTION_STR_END) {
this.exceptionStr = cesu8ToString(this.exceptionData);
this.exceptionData = undefined;
}
}

Expand Down