Skip to content

Commit 184b1ad

Browse files
ability to attach to a process using pid (#30)
* remove redundant flush outputs * running raw commands in parallel * ability to attach to a process using pid * removing additional whitespace Fixes #28
1 parent 32547b6 commit 184b1ad

File tree

2 files changed

+50
-36
lines changed

2 files changed

+50
-36
lines changed

debuggers/gdb/gdb_mi_driver.py

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,22 @@ class GDBMiDebugger(Driver):
1616

1717
gdb_instances = None
1818

19-
def __init__(self, base_args, base_script_file_path, regression_args, regression_script_file_path):
19+
def __init__(self, base_args, base_script_file_path, regression_args, regression_script_file_path,
20+
base_pid=None, regression_pid=None):
2021
self.base_gdb_instance = create_IDDGdbController(base_script_file_path)
2122
self.regressed_gdb_instance = create_IDDGdbController(regression_script_file_path)
2223

2324
self.gdb_instances = { 'base': self.base_gdb_instance, 'regressed': self.regressed_gdb_instance }
2425

25-
self.run_single_raw_command('file ' + base_args, 'base')
26-
self.run_single_raw_command('file ' + regression_args, 'regressed')
26+
if base_pid is None:
27+
self.run_single_raw_command('file ' + base_args, 'base')
28+
else:
29+
self.run_single_raw_command('attach ' + base_pid, 'base')
30+
31+
if regression_pid is None:
32+
self.run_single_raw_command('file ' + regression_args, 'regressed')
33+
else:
34+
self.run_single_raw_command('attach ' + regression_pid, 'regressed')
2735

2836
dirname = os.path.dirname(__file__)
2937
self.run_parallel_raw_command("source " + os.path.join(dirname, "gdb_commands.py"))
@@ -35,23 +43,11 @@ def run_parallel_command(self, command):
3543

3644
# wait till base is done
3745
raw_result = self.base_gdb_instance.recv()
38-
39-
# make sure all output is flushed
40-
# time.sleep(.005)
41-
self.base_gdb_instance.send((("",), {"timeout_sec": 60}))
42-
raw_result += self.base_gdb_instance.recv()
43-
4446
# parse output (base)
4547
base_response = self.parse_command_output(raw_result)
4648

4749
# wait till regression is done
4850
raw_result = self.regressed_gdb_instance.recv()
49-
50-
# make sure all output is flushed
51-
# time.sleep(.005)
52-
self.regressed_gdb_instance.send((("",), {"timeout_sec": 60}))
53-
raw_result += self.regressed_gdb_instance.recv()
54-
5551
# parse output regression
5652
regressed_response = self.parse_command_output(raw_result)
5753

@@ -72,12 +68,7 @@ def run_single_command(self, command, version):
7268

7369
self.gdb_instances[version].send(((" {command}\n".format(command = command),), {"timeout_sec": 60}))
7470
raw_result = self.gdb_instances[version].recv()
75-
76-
# make sure all output is flushed
77-
# time.sleep(.005)
78-
self.gdb_instances[version].send((("",), {"timeout_sec": 60}))
79-
raw_result += self.gdb_instances[version].recv()
80-
71+
8172
return self.parse_command_output(raw_result)
8273

8374
def run_single_special_command(self, command, version):
@@ -87,10 +78,6 @@ def run_single_special_command(self, command, version):
8778
self.gdb_instances[version].send(((" {command}\n".format(command = command),), {"timeout_sec": 60}))
8879
raw_result = self.gdb_instances[version].recv()
8980

90-
# flush output
91-
self.gdb_instances[version].send((("",), {"timeout_sec": 60}))
92-
raw_result += self.gdb_instances[version].recv()
93-
9481
return self.parse_special_command_output(raw_result)
9582

9683
def parse_special_command_output(self, raw_result):
@@ -155,20 +142,26 @@ def get_current_registers(self):
155142
return { "base" : base_stack_frame, "regressed" : regression_stack_frame }
156143

157144
def run_parallel_raw_command(self, command):
158-
base_result = str(self.run_single_raw_command(command, "base"))
159-
regression_result = str(self.run_single_raw_command(command, "regressed"))
145+
self.base_gdb_instance.send((("{command}\n".format(command = command),), {"timeout_sec": 60}))
146+
self.regressed_gdb_instance.send((("{command}\n".format(command = command),), {"timeout_sec": 60}))
147+
148+
raw_result = self.base_gdb_instance.recv()
149+
base_result = str(self.parse_raw_command_output(raw_result))
150+
raw_result = self.regressed_gdb_instance.recv()
151+
regression_result = str(self.parse_raw_command_output(raw_result))
160152

161153
return { "base": base_result, "regressed": regression_result }
162154

163-
def run_single_raw_command(self, command, version):
155+
def parse_raw_command_output(self, raw_result):
164156
result = []
165-
self.gdb_instances[version].send((("{command}\n".format(command = command),), {"timeout_sec": 60}))
166-
raw_result = self.gdb_instances[version].recv()
167-
168157
for item in raw_result:
169158
result.append(str(item))
170-
171159
return result
172160

161+
def run_single_raw_command(self, command, version):
162+
self.gdb_instances[version].send((("{command}\n".format(command = command),), {"timeout_sec": 60}))
163+
raw_result = self.gdb_instances[version].recv()
164+
return self.parse_raw_command_output(raw_result)
165+
173166
def terminate(self):
174167
terminate_all_IDDGdbController()

idd.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,11 @@ async def execute_debugger_command(self, event: Input.Changed) -> None:
311311

312312
parser = argparse.ArgumentParser(description='Diff Debug for simple debugging!')
313313
parser.add_argument('-c','--comparator', help='Choose a comparator', default='gdb')
314-
parser.add_argument('-ba','--base-args', help='Base executable args', default='[]', nargs='+')
314+
parser.add_argument('-ba','--base-args', help='Base executable args', default="", nargs='+')
315+
parser.add_argument('-bpid','--base-processid', help='Base process ID', default=None)
315316
parser.add_argument('-bs','--base-script-path', help='Base preliminary script file path', default=None, nargs='+')
316-
parser.add_argument('-ra','--regression-args', help='Regression executable args', default='[]', nargs='+')
317+
parser.add_argument('-ra','--regression-args', help='Regression executable args', default="", nargs='+')
318+
parser.add_argument('-rpid','--regression-processid', help='Regression process ID', default=None)
317319
parser.add_argument('-rs','--regression-script-path', help='Regression preliminary script file path', default=None, nargs='+')
318320
parser.add_argument('-r','--remote_host', help='The host of the remote server', default='localhost')
319321
parser.add_argument('-p','--platform', help='The platform of the remote server: macosx, linux', default='linux')
@@ -325,22 +327,41 @@ async def execute_debugger_command(self, event: Input.Changed) -> None:
325327

326328
comparator = args['comparator']
327329
ba = ' '.join(args['base_args'])
330+
bpid = args['base_processid']
328331
bs = ' '.join(args['base_script_path']) if args['base_script_path'] is not None else None
329332
ra = ' '.join(args['regression_args'])
333+
rpid = args['regression_processid']
330334
rs = ' '.join(args['regression_script_path']) if args["regression_script_path"] is not None else None
331335

332336
if comparator == 'gdb':
333337
from debuggers.gdb.gdb_mi_driver import GDBMiDebugger
334338

335-
Debugger = GDBMiDebugger(ba, bs, ra, rs)
339+
if ba != "" and bpid is not None:
340+
raise Exception("Both executable and process ID given for base. This is not possible")
341+
if ra != "" and rpid is not None:
342+
raise Exception("Both executable and process ID given for regression. This is not possible")
343+
344+
if ba == "":
345+
if ra == "":
346+
Debugger = GDBMiDebugger(ba, bs, ra, rs, base_pid=bpid, regression_pid=rpid)
347+
else:
348+
Debugger = GDBMiDebugger(ba, bs, ra, rs, base_pid=bpid)
349+
else:
350+
if ra == "":
351+
Debugger = GDBMiDebugger(ba, bs, ra, rs, regression_pid=rpid)
352+
else:
353+
Debugger = GDBMiDebugger(ba, bs, ra, rs)
354+
336355
elif comparator == 'lldb':
337356
from debuggers.lldb.lldb_driver import LLDBDebugger
338357

358+
if ba == "" or ra == "":
359+
raise Exception("LLDB can only be used by launching executable and executable is not provided")
339360
Debugger = LLDBDebugger(ba, ra)
340361
else:
341362
sys.exit("Invalid comparator set")
342363

343364
disable_registers = args["disable_registers"]
344365
disable_assembly = args["disable_assembly"]
345366
dd = DiffDebug(disable_assembly, disable_registers)
346-
dd.run()
367+
dd.run()

0 commit comments

Comments
 (0)