Skip to content

Commit 9ecddaa

Browse files
authored
Merge pull request #401 from WebFreak001/check-debugger
check for configured debugger before start to provide a nicer error message
2 parents 9fb2875 + 8c926ca commit 9ecddaa

File tree

5 files changed

+61
-10
lines changed

5 files changed

+61
-10
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,22 @@ Versioning].
88
[keep a changelog]: https://keepachangelog.com/en/1.0.0
99
[semantic versioning]: https://semver.org/spec/v2.0.0.html
1010

11+
## Unreleased
12+
13+
### Added
14+
15+
- check for configured debugger before start to provide a nicer error message
16+
([@GitMensch])
17+
1118
## [0.27.0] - 2024-02-07
1219

1320
### Added
1421

1522
- Added registers view ([@nomtats]) #242
1623
- Enabled breakpoints inside `riscv` files ([@William-An]) #404
1724

25+
[0.27.0]: https://github.com/WebFreak001/code-debug/compare/v0.26.1...v0.27.0
26+
1827
## [0.26.1] - 2022-12-31
1928

2029
### Fixed

src/gdb.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ class GDBDebugSession extends MI2DebugSession {
5353
}
5454

5555
protected launchRequest(response: DebugProtocol.LaunchResponse, args: LaunchRequestArguments): void {
56-
this.miDebugger = new MI2(args.gdbpath || "gdb", ["-q", "--interpreter=mi2"], args.debugger_args, args.env);
56+
const dbgCommand = args.gdbpath || "gdb";
57+
if (this.checkCommand(dbgCommand)) {
58+
this.sendErrorResponse(response, 104, `Configured debugger ${dbgCommand} not found.`);
59+
return;
60+
}
61+
this.miDebugger = new MI2(dbgCommand, ["-q", "--interpreter=mi2"], args.debugger_args, args.env);
5762
this.setPathSubstitutions(args.pathSubstitutions);
5863
this.initDebugger();
5964
this.quit = false;
@@ -94,7 +99,12 @@ class GDBDebugSession extends MI2DebugSession {
9499
}
95100

96101
protected attachRequest(response: DebugProtocol.AttachResponse, args: AttachRequestArguments): void {
97-
this.miDebugger = new MI2(args.gdbpath || "gdb", ["-q", "--interpreter=mi2"], args.debugger_args, args.env);
102+
const dbgCommand = args.gdbpath || "gdb";
103+
if (this.checkCommand(dbgCommand)) {
104+
this.sendErrorResponse(response, 104, `Configured debugger ${dbgCommand} not found.`);
105+
return;
106+
}
107+
this.miDebugger = new MI2(dbgCommand, ["-q", "--interpreter=mi2"], args.debugger_args, args.env);
98108
this.setPathSubstitutions(args.pathSubstitutions);
99109
this.initDebugger();
100110
this.quit = false;

src/lldb.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ class LLDBDebugSession extends MI2DebugSession {
4848
}
4949

5050
protected launchRequest(response: DebugProtocol.LaunchResponse, args: LaunchRequestArguments): void {
51-
this.miDebugger = new MI2_LLDB(args.lldbmipath || "lldb-mi", [], args.debugger_args, args.env);
51+
const dbgCommand = args.lldbmipath || "lldb-mi";
52+
if (this.checkCommand(dbgCommand)) {
53+
this.sendErrorResponse(response, 104, `Configured debugger ${dbgCommand} not found.`);
54+
return;
55+
}
56+
this.miDebugger = new MI2_LLDB(dbgCommand, [], args.debugger_args, args.env);
5257
this.setPathSubstitutions(args.pathSubstitutions);
5358
this.initDebugger();
5459
this.quit = false;
@@ -89,7 +94,12 @@ class LLDBDebugSession extends MI2DebugSession {
8994
}
9095

9196
protected attachRequest(response: DebugProtocol.AttachResponse, args: AttachRequestArguments): void {
92-
this.miDebugger = new MI2_LLDB(args.lldbmipath || "lldb-mi", [], args.debugger_args, args.env);
97+
const dbgCommand = args.lldbmipath || "lldb-mi";
98+
if (this.checkCommand(dbgCommand)) {
99+
this.sendErrorResponse(response, 104, `Configured debugger ${dbgCommand} not found.`);
100+
return;
101+
}
102+
this.miDebugger = new MI2_LLDB(dbgCommand, [], args.debugger_args, args.env);
93103
this.setPathSubstitutions(args.pathSubstitutions);
94104
this.initDebugger();
95105
this.quit = false;

src/mago.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ class MagoDebugSession extends MI2DebugSession {
5050
}
5151

5252
protected launchRequest(response: DebugProtocol.LaunchResponse, args: LaunchRequestArguments): void {
53-
this.miDebugger = new MI2_Mago(args.magomipath || "mago-mi", ["-q"], args.debugger_args, args.env);
53+
const dbgCommand = args.magomipath || "mago-mi";
54+
if (this.checkCommand(dbgCommand)) {
55+
this.sendErrorResponse(response, 104, `Configured debugger ${dbgCommand} not found.`);
56+
return;
57+
}
58+
this.miDebugger = new MI2_Mago(dbgCommand, ["-q"], args.debugger_args, args.env);
5459
this.initDebugger();
5560
this.quit = false;
5661
this.attached = false;
@@ -69,7 +74,12 @@ class MagoDebugSession extends MI2DebugSession {
6974
}
7075

7176
protected attachRequest(response: DebugProtocol.AttachResponse, args: AttachRequestArguments): void {
72-
this.miDebugger = new MI2_Mago(args.magomipath || "mago-mi", [], args.debugger_args, args.env);
77+
const dbgCommand = args.magomipath || "mago-mi";
78+
if (this.checkCommand(dbgCommand)) {
79+
this.sendErrorResponse(response, 104, `Configured debugger ${dbgCommand} not found.`);
80+
return;
81+
}
82+
this.miDebugger = new MI2_Mago(dbgCommand, ["-q"], args.debugger_args, args.env);
7383
this.initDebugger();
7484
this.quit = false;
7585
this.attached = true;

src/mibase.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Breakpoint, IBackend, Variable, VariableObject, ValuesFormattingMode, M
55
import { MINode } from './backend/mi_parse';
66
import { expandValue, isExpandable } from './backend/gdb_expansion';
77
import { MI2 } from './backend/mi2/mi2';
8+
import { execSync } from 'child_process';
89
import * as systemPath from "path";
910
import * as net from "net";
1011
import * as os from "os";
@@ -17,10 +18,10 @@ class ExtendedVariable {
1718
}
1819

1920
class VariableScope {
20-
constructor (public readonly name: string, public readonly threadId: number, public readonly level: number) {
21+
constructor(public readonly name: string, public readonly threadId: number, public readonly level: number) {
2122
}
2223

23-
public static variableName (handle: number, name: string): string {
24+
public static variableName(handle: number, name: string): string {
2425
return `var_${handle}_${name}`;
2526
}
2627
}
@@ -92,6 +93,17 @@ export class MI2DebugSession extends DebugSession {
9293
}
9394
}
9495

96+
// verifies that the specified command can be executed
97+
protected checkCommand(debuggerName: string): boolean {
98+
try {
99+
const command = process.platform === 'win32' ? 'where' : 'command -v';
100+
execSync(`${command} ${scriptName}`, { stdio: 'ignore' });
101+
return true;
102+
} catch (error) {
103+
return false;
104+
}
105+
}
106+
95107
protected setValuesFormattingMode(mode: ValuesFormattingMode) {
96108
switch (mode) {
97109
case "disabled":
@@ -728,7 +740,7 @@ export class MI2DebugSession extends DebugSession {
728740
id: 1,
729741
label: args.source.name,
730742
column: args.column,
731-
line : args.line
743+
line: args.line
732744
}]
733745
};
734746
this.sendResponse(response);
@@ -743,7 +755,7 @@ export class MI2DebugSession extends DebugSession {
743755

744756
protected setSourceFileMap(configMap: { [index: string]: string }, fallbackGDB: string, fallbackIDE: string): void {
745757
if (configMap === undefined) {
746-
this.sourceFileMap = new SourceFileMap({[fallbackGDB]: fallbackIDE});
758+
this.sourceFileMap = new SourceFileMap({ [fallbackGDB]: fallbackIDE });
747759
} else {
748760
this.sourceFileMap = new SourceFileMap(configMap, fallbackGDB);
749761
}

0 commit comments

Comments
 (0)