Skip to content

Commit 8894f2f

Browse files
authored
Add support for executing a script prior to starting GDB. (#15)
This facilitates the preparation of the context in which the target executables run. The context might be crucial for the expected behavior of the analyzed executables.
1 parent 4c09f65 commit 8894f2f

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

debuggers/gdb/gdb_mi_driver.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class GDBMiDebugger(Driver):
1616

1717
gdb_instances = None
1818

19-
def __init__(self, base_args, regression_args):
20-
self.base_gdb_instance = IDDGdbController()
21-
self.regressed_gdb_instance = IDDGdbController()
19+
def __init__(self, base_args, base_script_file_path, regression_args, regression_script_file_path):
20+
self.base_gdb_instance = IDDGdbController(base_script_file_path)
21+
self.regressed_gdb_instance = IDDGdbController(regression_script_file_path)
2222

2323
self.gdb_instances = { 'base': self.base_gdb_instance, 'regressed': self.regressed_gdb_instance }
2424

debuggers/gdb/idd_gdb_controller.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import logging
22
import subprocess
3-
import os
4-
from distutils.spawn import find_executable
5-
from typing import Union, List, Optional
3+
64
from pygdbmi.gdbcontroller import GdbController
75
from pygdbmi.IoManager import IoManager
86
from pygdbmi.constants import (
@@ -14,16 +12,25 @@
1412
logger = logging.getLogger(__name__)
1513

1614
class IDDGdbController(GdbController):
15+
script_file_path = None
16+
17+
def __init__(self, script_file_path = None):
18+
self.script_file_path = script_file_path
19+
super().__init__( None, DEFAULT_TIME_TO_CHECK_FOR_ADDITIONAL_OUTPUT_SEC)
20+
1721
def spawn_new_gdb_subprocess(self) -> int:
1822
if self.gdb_process:
1923
logger.debug(
2024
"Killing current gdb subprocess (pid %d)" % self.gdb_process.pid
2125
)
2226
self.exit()
2327

24-
logger.debug(f'Launching gdb: {" ".join(self.command)}')
28+
if self.script_file_path:
29+
logger.debug(f'Configuring to run bash script: {self.script_file_path} before starting GDB')
30+
# The modified command will source the script and then run gdb
31+
self.command = ["/bin/bash", "-c", f"source {self.script_file_path} && {' '.join(self.command)}"]
2532

26-
my_env = os.environ.copy()
33+
logger.debug(f'Launching gdb: {" ".join(self.command)}')
2734

2835
# Use pipes to the standard streams
2936
self.gdb_process = subprocess.Popen(
@@ -32,8 +39,7 @@ def spawn_new_gdb_subprocess(self) -> int:
3239
stdout=subprocess.PIPE,
3340
stdin=subprocess.PIPE,
3441
stderr=subprocess.PIPE,
35-
bufsize=0,
36-
env=my_env
42+
bufsize=0
3743
)
3844

3945
self.io_manager = IoManager(

idd.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ async def execute_debugger_command(self, event: Input.Changed) -> None:
200200
parser = argparse.ArgumentParser(description='Diff Debug for simple debugging!')
201201
parser.add_argument('-c','--comparator', help='Choose a comparator', default='gdb')
202202
parser.add_argument('-ba','--base-args', help='Base executable args', default='[]', nargs='+')
203+
parser.add_argument('-bs','--base-script-path', help='Base preliminary script file path', default='[]', nargs='+')
203204
parser.add_argument('-ra','--regression-args', help='Regression executable args', default='[]', nargs='+')
205+
parser.add_argument('-rs','--regression-script-path', help='Regression prelimminary script file path', default='[]', nargs='+')
204206
parser.add_argument('-r','--remote_host', help='The host of the remote server', default='localhost')
205207
parser.add_argument('-p','--platform', help='The platform of the remote server: macosx, linux', default='linux')
206208
parser.add_argument('-t','--triple', help='The target triple: x86_64-apple-macosx, x86_64-gnu-linux', default='x86_64-gnu-linux')
@@ -209,12 +211,14 @@ async def execute_debugger_command(self, event: Input.Changed) -> None:
209211

210212
comperator = args['comparator']
211213
ba = ' '.join(args['base_args'])
214+
bs = ' '.join(args['base_script_path'])
212215
ra = ' '.join(args['regression_args'])
216+
rs = ' '.join(args['regression_script_path'])
213217

214218
if comperator == 'gdb':
215219
from debuggers.gdb.gdb_mi_driver import GDBMiDebugger
216220

217-
Debugger = GDBMiDebugger(ba, ra)
221+
Debugger = GDBMiDebugger(ba, bs, ra, rs)
218222
elif comperator == 'lldb':
219223
from debuggers.lldb.lldb_driver import LLDBDebugger
220224

0 commit comments

Comments
 (0)