Skip to content

Commit c16cd8c

Browse files
tdusnokiyichoi
authored andcommitted
Add exception handling to onBreakpointHit function (#26)
IoT.js-Debug-DCO-1.0-Signed-off-by: Tibor Dusnoki [email protected]
1 parent 53854d4 commit c16cd8c

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

src/IotjsDebugger.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import * as Path from 'path';
2626
import { IAttachRequestArguments } from './IotjsDebuggerInterfaces';
2727
import { JerryDebuggerClient, JerryDebuggerOptions } from './JerryDebuggerClient';
2828
import {
29-
JerryDebugProtocolDelegate, JerryDebugProtocolHandler, JerryMessageScriptParsed, JerryEvalResult
29+
JerryDebugProtocolDelegate, JerryDebugProtocolHandler, JerryMessageScriptParsed, JerryEvalResult,
30+
JerryMessageExceptionHit
3031
} from './JerryProtocolHandler';
3132
import { EVAL_RESULT_SUBTYPE } from './JerryProtocolConstants';
3233

@@ -116,6 +117,11 @@ class IotjsDebugSession extends LoggingDebugSession {
116117
this.sendEvent(new StoppedEvent('breakpoint', IotjsDebugSession.THREAD_ID));
117118
};
118119

120+
const onExceptionHit = (data: JerryMessageExceptionHit) => {
121+
this.log('onExceptionHit');
122+
this.sendEvent(new StoppedEvent('exception', IotjsDebugSession.THREAD_ID, data.message));
123+
};
124+
119125
const onResume = () => {
120126
this.log('onResume');
121127

@@ -156,6 +162,7 @@ class IotjsDebugSession extends LoggingDebugSession {
156162

157163
const protocolDelegate = <JerryDebugProtocolDelegate>{
158164
onBreakpointHit,
165+
onExceptionHit,
159166
onResume,
160167
onScriptParsed,
161168
onWaitForSource

src/JerryProtocolHandler.ts

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export interface ParserStackFrame {
4545
export interface JerryDebugProtocolDelegate {
4646
onBacktrace?(backtrace: Array<Breakpoint>): void;
4747
onBreakpointHit?(message: JerryMessageBreakpointHit): void;
48+
onExceptionHit?(message: JerryMessageExceptionHit): void;
4849
onEvalResult?(subType: number, result: string): void;
4950
onError?(code: number, message: string): void;
5051
onResume?(): void;
@@ -68,6 +69,12 @@ export interface JerryMessageBreakpointHit {
6869
exact: boolean;
6970
}
7071

72+
export interface JerryMessageExceptionHit {
73+
breakpoint: Breakpoint;
74+
exact: boolean;
75+
message: string;
76+
}
77+
7178
export interface JerryEvalResult {
7279
subtype: number;
7380
value: string;
@@ -135,6 +142,7 @@ export class JerryDebugProtocolHandler {
135142

136143
private nextScriptID: number = 1;
137144
private exceptionData?: Uint8Array;
145+
private exceptionString?: string;
138146
private evalsPending: number = 0;
139147
private lastBreakpointHit?: Breakpoint;
140148
private lastBreakpointExact: boolean = true;
@@ -169,6 +177,9 @@ export class JerryDebugProtocolHandler {
169177
[SP.SERVER.JERRY_DEBUGGER_FUNCTION_NAME_END]: this.onFunctionName,
170178
[SP.SERVER.JERRY_DEBUGGER_RELEASE_BYTE_CODE_CP]: this.onReleaseByteCodeCP,
171179
[SP.SERVER.JERRY_DEBUGGER_BREAKPOINT_HIT]: this.onBreakpointHit,
180+
[SP.SERVER.JERRY_DEBUGGER_EXCEPTION_HIT]: this.onBreakpointHit,
181+
[SP.SERVER.JERRY_DEBUGGER_EXCEPTION_STR]: this.onExceptionStr,
182+
[SP.SERVER.JERRY_DEBUGGER_EXCEPTION_STR_END]: this.onExceptionStr,
172183
[SP.SERVER.JERRY_DEBUGGER_BACKTRACE]: this.onBacktrace,
173184
[SP.SERVER.JERRY_DEBUGGER_BACKTRACE_END]: this.onBacktrace,
174185
[SP.SERVER.JERRY_DEBUGGER_EVAL_RESULT]: this.onEvalResult,
@@ -476,14 +487,6 @@ export class JerryDebugProtocolHandler {
476487
const breakpointRef = this.getBreakpoint(breakpointData);
477488
const breakpoint = breakpointRef.breakpoint;
478489

479-
if (data[0] === SP.SERVER.JERRY_DEBUGGER_EXCEPTION_HIT) {
480-
this.log('Exception throw detected');
481-
if (this.exceptionData) {
482-
this.log(`Exception hint: ${cesu8ToString(this.exceptionData)}`);
483-
this.exceptionData = undefined;
484-
}
485-
}
486-
487490
this.lastBreakpointHit = breakpoint;
488491
this.lastBreakpointExact = breakpointRef.exact;
489492

@@ -495,7 +498,23 @@ export class JerryDebugProtocolHandler {
495498
const atAround = breakpointRef.exact ? 'at' : 'around';
496499
this.log(`Stopped ${atAround} ${breakpointInfo}${breakpoint}`);
497500

498-
// TODO: handle exception case differently
501+
if (data[0] === SP.SERVER.JERRY_DEBUGGER_EXCEPTION_HIT) {
502+
this.log('Exception throw detected');
503+
if (this.exceptionString) {
504+
this.log(`Exception hint: ${this.exceptionString}`);
505+
}
506+
507+
if (this.delegate.onExceptionHit) {
508+
this.delegate.onExceptionHit({
509+
'breakpoint': breakpoint,
510+
'exact': breakpointRef.exact,
511+
'message': this.exceptionString,
512+
});
513+
this.exceptionString = undefined;
514+
return;
515+
}
516+
}
517+
499518
if (this.delegate.onBreakpointHit) {
500519
this.delegate.onBreakpointHit(breakpointRef);
501520
}
@@ -775,4 +794,13 @@ export class JerryDebugProtocolHandler {
775794
if (!simple) this.currentRequest = request;
776795
return true;
777796
}
797+
798+
onExceptionStr(data: Uint8Array) {
799+
this.logPacket('onExceptionStr');
800+
this.exceptionData = assembleUint8Arrays(this.exceptionData, data);
801+
if (data[0] === SP.SERVER.JERRY_DEBUGGER_EXCEPTION_STR_END) {
802+
this.exceptionString = cesu8ToString(this.exceptionData);
803+
this.exceptionData = undefined;
804+
}
805+
}
778806
}

0 commit comments

Comments
 (0)