]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
kresctl debug: allow paths for '--gdb' and add existence checks
authorOto Šťáva <oto.stava@nic.cz>
Thu, 5 Oct 2023 13:49:00 +0000 (15:49 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Tue, 3 Dec 2024 10:49:59 +0000 (11:49 +0100)
python/knot_resolver/client/commands/debug.py

index 421e0edb76109d08f89c6ce4919e78fb8311728d..3350fda59fca7a1544838e0a7d3cf488aa3bf45d 100644 (file)
@@ -2,6 +2,7 @@ import argparse
 import json
 import os
 import sys
+from pathlib import Path
 from typing import List, Optional, Tuple, Type
 
 from knot_resolver.client.command import Command, CommandArgs, CompWords, register_command
@@ -45,7 +46,7 @@ class DebugCommand(Command):
             "--gdb",
             help="GDB command (may be a command on PATH, or an absolute path)",
             type=str,
-            default="gdb",
+            default=None,
         )
         return debug, DebugCommand
 
@@ -54,8 +55,24 @@ class DebugCommand(Command):
         return {}
 
     def run(self, args: CommandArgs) -> None:  # noqa: PLR0912, PLR0915
-        gdb_cmd = str(which.which(self.gdb))
-        sudo_cmd = str(which.which("sudo"))
+        if self.gdb is None:
+            try:
+                gdb_cmd = str(which.which("gdb"))
+            except RuntimeError:
+                print("Could not find 'gdb' in $PATH. Is GDB installed?", file=sys.stderr)
+                sys.exit(1)
+        elif "/" not in self.gdb:
+            try:
+                gdb_cmd = str(which.which(self.gdb))
+            except RuntimeError:
+                print(f"Could not find '{self.gdb}' in $PATH.", file=sys.stderr)
+                sys.exit(1)
+        else:
+            gdb_cmd_path = Path(self.gdb).absolute()
+            if not gdb_cmd_path.exists():
+                print(f"Could not find '{self.gdb}'.", file=sys.stderr)
+                sys.exit(1)
+            gdb_cmd = str(gdb_cmd_path)
 
         response = request(args.socket, "GET", f"processes/{self.proc_type}")
         if response.status != 200:
@@ -79,12 +96,18 @@ class DebugCommand(Command):
 
         # Put `sudo --` at the beginning of the command.
         if self.sudo:
+            try:
+                sudo_cmd = str(which.which("sudo"))
+            except RuntimeError:
+                print("Could not find 'sudo' in $PATH. Is sudo installed?", file=sys.stderr)
+                sys.exit(1)
             exec_args.extend([sudo_cmd, "--"])
 
-        # Attach GDB to processes - the first process gets passed as a regular `--pid` argument to GDB, the
-        # rest are attached using the `add-inferior` and `attach` GDB commands. This way, we are now debugging
-        # multiple processes.
+        # Attach GDB to processes - the processes are attached using the `add-inferior` and `attach` GDB
+        # commands. This way, we can debug multiple processes.
         exec_args.extend([gdb_cmd])
+        exec_args.extend(["-init-eval-command", "set detach-on-fork off"])
+        exec_args.extend(["-init-eval-command", "set schedule-multiple on"])
         exec_args.extend(["-init-eval-command", f'attach {procs[0]["pid"]}'])
         inferior = 2
         for proc in procs[1:]: