Skip to content

Commit 897c94e

Browse files
committed
Installed exception handlers at top level for unexpected crashes
1 parent 33b61d5 commit 897c94e

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/backend/server.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,19 @@ import { quoteShellCmdLine } from '../common';
99
import { greenFormat } from '../frontend/ansi-helpers';
1010

1111
export let GdbPid = -1;
12+
13+
let ServerLogFilePath: string = null;
14+
export function getServerLogFilePath(): string {
15+
if (!ServerLogFilePath) {
16+
const tmpDirName = os.tmpdir();
17+
ServerLogFilePath = path.join(tmpDirName, 'cortex-debug-server.log');
18+
}
19+
return ServerLogFilePath;
20+
}
21+
1222
export function ServerConsoleLog(str: string, usePid?: number) {
1323
if (!str) { return; }
1424
try {
15-
const tmpDirName = os.tmpdir();
1625
const date = new Date();
1726
if (usePid) {
1827
GdbPid = usePid;
@@ -22,7 +31,7 @@ export function ServerConsoleLog(str: string, usePid?: number) {
2231
if (!str.endsWith('\n')) {
2332
str += '\n';
2433
}
25-
fs.appendFileSync(path.join(tmpDirName, 'cortex-debug-server-exiting.log'), str);
34+
fs.appendFileSync(getServerLogFilePath(), str);
2635
} catch (e) {
2736
console.log(e ? e.toString() : 'unknown exception?');
2837
}

src/gdb.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
createPortName, GenericCustomEvent, quoteShellCmdLine, toStringDecHexOctBin, ADAPTER_DEBUG_MODE, defSymbolFile, CTIAction, getPathRelative,
1919
SWOConfigureEvent, RTTCommonDecoderOpts
2020
} from './common';
21-
import { GDBServer, ServerConsoleLog } from './backend/server';
21+
import { GDBServer, getServerLogFilePath, ServerConsoleLog } from './backend/server';
2222
import { MINode } from './backend/mi_parse';
2323
import { expandValue, isExpandable } from './backend/gdb_expansion';
2424
import { GdbDisassembler } from './backend/disasm';
@@ -423,6 +423,9 @@ export class GDBDebugSession extends LoggingDebugSession {
423423
this.handleMsg('stdout',
424424
`Cortex-Debug: VSCode debugger extension version ${args.pvtVersion} git(${__COMMIT_HASH__}). `
425425
+ 'Usage info: https://github.com/Marus/cortex-debug#usage');
426+
if (this.args.showDevDebugOutput) {
427+
this.handleMsg('stderr', `INFO: A log of gdb-servers, gdb, debug adapter start/stop/pid info can be found in '${getServerLogFilePath()}'.\n`);
428+
}
426429

427430
this.setHWBreakpointInfo();
428431

@@ -3628,4 +3631,23 @@ function initTwoCharsToIntMap(): object {
36283631

36293632
const twoCharsToIntMap = initTwoCharsToIntMap();
36303633

3631-
LoggingDebugSession.run(GDBDebugSession);
3634+
process.on('uncaughtException', (err) => {
3635+
const msg = err && err.stack ? err.stack : (err.message ? err.message : 'unknown error');
3636+
console.error('cortex-debug: Caught exception:', msg);
3637+
ServerConsoleLog('Caught exception: ' + msg);
3638+
process.exit(1); // The process is in an unreliable state, so exit is recommended
3639+
});
3640+
3641+
process.on('unhandledRejection', (reason, promise) => {
3642+
const msg = 'cortex-debug: Unhandled Rejection: reason: ' + reason.toString() + ' promise: ' + promise.toString();
3643+
console.error(msg);
3644+
ServerConsoleLog(msg);
3645+
});
3646+
3647+
try {
3648+
LoggingDebugSession.run(GDBDebugSession);
3649+
} catch (error) {
3650+
console.error('cortex-debug: Error occurred while running GDBDebugSession:', error);
3651+
ServerConsoleLog('cortex-debug: Caught exception: ' + error.toString());
3652+
throw error;
3653+
}

0 commit comments

Comments
 (0)