From d559072646d27418211a7e1d0605940ef4ec7581 Mon Sep 17 00:00:00 2001 From: Simon Sobisch Date: Wed, 7 Feb 2024 08:57:29 +0000 Subject: [PATCH 1/2] check for configured debugger before start to provide a nicer error message --- CHANGELOG.md | 7 +++++++ src/gdb.ts | 14 ++++++++++++-- src/lldb.ts | 14 ++++++++++++-- src/mago.ts | 14 ++++++++++++-- src/mibase.ts | 20 ++++++++++++++++---- 5 files changed, 59 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab0c2613..6e858b26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,13 @@ Versioning]. [keep a changelog]: https://keepachangelog.com/en/1.0.0 [semantic versioning]: https://semver.org/spec/v2.0.0.html +## Unreleased + +### Added + +- check for configured debugger before start to provide a nicer error message + ([@GitMensch]) + ## [0.27.0] - 2024-02-07 ### Added diff --git a/src/gdb.ts b/src/gdb.ts index 3c0357bf..642ca97d 100644 --- a/src/gdb.ts +++ b/src/gdb.ts @@ -53,7 +53,12 @@ class GDBDebugSession extends MI2DebugSession { } protected launchRequest(response: DebugProtocol.LaunchResponse, args: LaunchRequestArguments): void { - this.miDebugger = new MI2(args.gdbpath || "gdb", ["-q", "--interpreter=mi2"], args.debugger_args, args.env); + const dbgCommand = args.gdbpath || "gdb"; + if (this.checkCommand(dbgCommand)) { + this.sendErrorResponse(response, 104, `Configured debugger ${dbgCommand} not found.`); + return; + } + this.miDebugger = new MI2(dbgCommand, ["-q", "--interpreter=mi2"], args.debugger_args, args.env); this.setPathSubstitutions(args.pathSubstitutions); this.initDebugger(); this.quit = false; @@ -94,7 +99,12 @@ class GDBDebugSession extends MI2DebugSession { } protected attachRequest(response: DebugProtocol.AttachResponse, args: AttachRequestArguments): void { - this.miDebugger = new MI2(args.gdbpath || "gdb", ["-q", "--interpreter=mi2"], args.debugger_args, args.env); + const dbgCommand = args.gdbpath || "gdb"; + if (this.checkCommand(dbgCommand)) { + this.sendErrorResponse(response, 104, `Configured debugger ${dbgCommand} not found.`); + return; + } + this.miDebugger = new MI2(dbgCommand, ["-q", "--interpreter=mi2"], args.debugger_args, args.env); this.setPathSubstitutions(args.pathSubstitutions); this.initDebugger(); this.quit = false; diff --git a/src/lldb.ts b/src/lldb.ts index 79b7e709..49fa0215 100644 --- a/src/lldb.ts +++ b/src/lldb.ts @@ -48,7 +48,12 @@ class LLDBDebugSession extends MI2DebugSession { } protected launchRequest(response: DebugProtocol.LaunchResponse, args: LaunchRequestArguments): void { - this.miDebugger = new MI2_LLDB(args.lldbmipath || "lldb-mi", [], args.debugger_args, args.env); + const dbgCommand = args.lldbmipath || "lldb-mi"; + if (this.checkCommand(dbgCommand)) { + this.sendErrorResponse(response, 104, `Configured debugger ${dbgCommand} not found.`); + return; + } + this.miDebugger = new MI2_LLDB(dbgCommand, [], args.debugger_args, args.env); this.setPathSubstitutions(args.pathSubstitutions); this.initDebugger(); this.quit = false; @@ -89,7 +94,12 @@ class LLDBDebugSession extends MI2DebugSession { } protected attachRequest(response: DebugProtocol.AttachResponse, args: AttachRequestArguments): void { - this.miDebugger = new MI2_LLDB(args.lldbmipath || "lldb-mi", [], args.debugger_args, args.env); + const dbgCommand = args.lldbmipath || "lldb-mi"; + if (this.checkCommand(dbgCommand)) { + this.sendErrorResponse(response, 104, `Configured debugger ${dbgCommand} not found.`); + return; + } + this.miDebugger = new MI2_LLDB(dbgCommand, [], args.debugger_args, args.env); this.setPathSubstitutions(args.pathSubstitutions); this.initDebugger(); this.quit = false; diff --git a/src/mago.ts b/src/mago.ts index d9d8fd0d..09345c7d 100644 --- a/src/mago.ts +++ b/src/mago.ts @@ -50,7 +50,12 @@ class MagoDebugSession extends MI2DebugSession { } protected launchRequest(response: DebugProtocol.LaunchResponse, args: LaunchRequestArguments): void { - this.miDebugger = new MI2_Mago(args.magomipath || "mago-mi", ["-q"], args.debugger_args, args.env); + const dbgCommand = args.magomipath || "mago-mi"; + if (this.checkCommand(dbgCommand)) { + this.sendErrorResponse(response, 104, `Configured debugger ${dbgCommand} not found.`); + return; + } + this.miDebugger = new MI2_Mago(dbgCommand, ["-q"], args.debugger_args, args.env); this.initDebugger(); this.quit = false; this.attached = false; @@ -69,7 +74,12 @@ class MagoDebugSession extends MI2DebugSession { } protected attachRequest(response: DebugProtocol.AttachResponse, args: AttachRequestArguments): void { - this.miDebugger = new MI2_Mago(args.magomipath || "mago-mi", [], args.debugger_args, args.env); + const dbgCommand = args.magomipath || "mago-mi"; + if (this.checkCommand(dbgCommand)) { + this.sendErrorResponse(response, 104, `Configured debugger ${dbgCommand} not found.`); + return; + } + this.miDebugger = new MI2_Mago(dbgCommand, ["-q"], args.debugger_args, args.env); this.initDebugger(); this.quit = false; this.attached = true; diff --git a/src/mibase.ts b/src/mibase.ts index 55cfca01..a3230068 100644 --- a/src/mibase.ts +++ b/src/mibase.ts @@ -5,6 +5,7 @@ import { Breakpoint, IBackend, Variable, VariableObject, ValuesFormattingMode, M import { MINode } from './backend/mi_parse'; import { expandValue, isExpandable } from './backend/gdb_expansion'; import { MI2 } from './backend/mi2/mi2'; +import { execSync } from 'child_process'; import * as systemPath from "path"; import * as net from "net"; import * as os from "os"; @@ -17,10 +18,10 @@ class ExtendedVariable { } class VariableScope { - constructor (public readonly name: string, public readonly threadId: number, public readonly level: number) { + constructor(public readonly name: string, public readonly threadId: number, public readonly level: number) { } - public static variableName (handle: number, name: string): string { + public static variableName(handle: number, name: string): string { return `var_${handle}_${name}`; } } @@ -92,6 +93,17 @@ export class MI2DebugSession extends DebugSession { } } + // verifies that the specified command can be executed + protected checkCommand(debuggerName: string): boolean { + try { + const command = process.platform === 'win32' ? 'where' : 'command -v'; + execSync(`${command} ${scriptName}`, { stdio: 'ignore' }); + return true; + } catch (error) { + return false; + } + } + protected setValuesFormattingMode(mode: ValuesFormattingMode) { switch (mode) { case "disabled": @@ -728,7 +740,7 @@ export class MI2DebugSession extends DebugSession { id: 1, label: args.source.name, column: args.column, - line : args.line + line: args.line }] }; this.sendResponse(response); @@ -743,7 +755,7 @@ export class MI2DebugSession extends DebugSession { protected setSourceFileMap(configMap: { [index: string]: string }, fallbackGDB: string, fallbackIDE: string): void { if (configMap === undefined) { - this.sourceFileMap = new SourceFileMap({[fallbackGDB]: fallbackIDE}); + this.sourceFileMap = new SourceFileMap({ [fallbackGDB]: fallbackIDE }); } else { this.sourceFileMap = new SourceFileMap(configMap, fallbackGDB); } From 8c926ca1c3d2d4f397cb817cec4d367648e98af9 Mon Sep 17 00:00:00 2001 From: Simon Sobisch Date: Wed, 7 Feb 2024 13:13:49 +0000 Subject: [PATCH 2/2] docs: minor changelog update --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e858b26..efb978f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ Versioning]. - Added registers view ([@nomtats]) #242 - Enabled breakpoints inside `riscv` files ([@William-An]) #404 +[0.27.0]: https://github.com/WebFreak001/code-debug/compare/v0.26.1...v0.27.0 + ## [0.26.1] - 2022-12-31 ### Fixed