Skip to content

Commit 79c1f64

Browse files
author
Oto Šťáva
committed
kresctl debug: allow paths for '--gdb' and add existence checks
1 parent 6c9f52a commit 79c1f64

File tree

1 file changed

+29
-6
lines changed
  • manager/knot_resolver_manager/cli/cmd

1 file changed

+29
-6
lines changed

manager/knot_resolver_manager/cli/cmd/debug.py

+29-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import os
44
import sys
5+
from pathlib import Path
56
from typing import List, Optional, Tuple, Type
67

78
from knot_resolver_manager.cli.command import Command, CommandArgs, CompWords, register_command
@@ -46,7 +47,7 @@ def register_args_subparser(
4647
"--gdb",
4748
help="GDB command (may be a command on PATH, or an absolute path)",
4849
type=str,
49-
default="gdb",
50+
default=None,
5051
)
5152
return debug, DebugCommand
5253

@@ -55,8 +56,24 @@ def completion(args: List[str], parser: argparse.ArgumentParser) -> CompWords:
5556
return {}
5657

5758
def run(self, args: CommandArgs) -> None:
58-
gdb_cmd = str(which.which(self.gdb))
59-
sudo_cmd = str(which.which("sudo"))
59+
if self.gdb is None:
60+
try:
61+
gdb_cmd = str(which.which("gdb"))
62+
except RuntimeError:
63+
print("Could not find 'gdb' in $PATH. Is GDB installed?", file=sys.stderr)
64+
sys.exit(1)
65+
elif "/" not in self.gdb:
66+
try:
67+
gdb_cmd = str(which.which(self.gdb))
68+
except RuntimeError:
69+
print(f"Could not find '{self.gdb}' in $PATH.", file=sys.stderr)
70+
sys.exit(1)
71+
else:
72+
gdb_cmd_path = Path(self.gdb).absolute()
73+
if not gdb_cmd_path.exists():
74+
print(f"Could not find '{self.gdb}'.", file=sys.stderr)
75+
sys.exit(1)
76+
gdb_cmd = str(gdb_cmd_path)
6077

6178
response = request(args.socket, "GET", f"processes/{self.proc_type}")
6279
if response.status != 200:
@@ -80,12 +97,18 @@ def run(self, args: CommandArgs) -> None:
8097

8198
# Put `sudo --` at the beginning of the command.
8299
if self.sudo:
100+
try:
101+
sudo_cmd = str(which.which("sudo"))
102+
except RuntimeError:
103+
print("Could not find 'sudo' in $PATH. Is sudo installed?", file=sys.stderr)
104+
sys.exit(1)
83105
exec_args.extend([sudo_cmd, "--"])
84106

85-
# Attach GDB to processes - the first process gets passed as a regular `--pid` argument to GDB, the
86-
# rest are attached using the `add-inferior` and `attach` GDB commands. This way, we are now debugging
87-
# multiple processes.
107+
# Attach GDB to processes - the processes are attached using the `add-inferior` and `attach` GDB
108+
# commands. This way, we can debug multiple processes.
88109
exec_args.extend([gdb_cmd])
110+
exec_args.extend(["-init-eval-command", "set detach-on-fork off"])
111+
exec_args.extend(["-init-eval-command", "set schedule-multiple on"])
89112
exec_args.extend(["-init-eval-command", f'attach {procs[0]["pid"]}'])
90113
inferior = 2
91114
for proc in procs[1:]:

0 commit comments

Comments
 (0)