2
2
import json
3
3
import os
4
4
import sys
5
+ from pathlib import Path
5
6
from typing import List , Optional , Tuple , Type
6
7
7
8
from knot_resolver_manager .cli .command import Command , CommandArgs , CompWords , register_command
@@ -46,7 +47,7 @@ def register_args_subparser(
46
47
"--gdb" ,
47
48
help = "GDB command (may be a command on PATH, or an absolute path)" ,
48
49
type = str ,
49
- default = "gdb" ,
50
+ default = None ,
50
51
)
51
52
return debug , DebugCommand
52
53
@@ -55,8 +56,24 @@ def completion(args: List[str], parser: argparse.ArgumentParser) -> CompWords:
55
56
return {}
56
57
57
58
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 )
60
77
61
78
response = request (args .socket , "GET" , f"processes/{ self .proc_type } " )
62
79
if response .status != 200 :
@@ -80,12 +97,18 @@ def run(self, args: CommandArgs) -> None:
80
97
81
98
# Put `sudo --` at the beginning of the command.
82
99
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 )
83
105
exec_args .extend ([sudo_cmd , "--" ])
84
106
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.
88
109
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" ])
89
112
exec_args .extend (["-init-eval-command" , f'attach { procs [0 ]["pid" ]} ' ])
90
113
inferior = 2
91
114
for proc in procs [1 :]:
0 commit comments