Skip to content

Commit 738b79d

Browse files
committed
add scripting ability and correspoonding test
1 parent 1aa8842 commit 738b79d

File tree

11 files changed

+128
-8
lines changed

11 files changed

+128
-8
lines changed

qiling/debugger/qdb/arch/arch.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,9 @@ def __init__(self):
1818
def arch_insn_size(self):
1919
return 4
2020

21+
@property
22+
def archbit(self):
23+
return 4
24+
2125
def read_insn(self, address: int):
2226
return self.read_mem(address, self.arch_insn_size)

qiling/debugger/qdb/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def try_read_pointer(self, address: int) -> Optional[bytes]:
7474
try to read pointer size of data from ql.mem
7575
"""
7676

77-
return self.try_read(address, self.arch_insn_size)
77+
return self.try_read(address, self.archbit)
7878

7979
def read_string(self, address: int) -> Optional[str]:
8080
"""

qiling/debugger/qdb/memory.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def parse(self, line: str):
112112
else:
113113
args = line.split()
114114

115-
rest = args[0] if len(args) == 1 else args
115+
rest = [args[0]] if len(args) == 1 else args
116116

117117
fmt = self.get_default_fmt
118118

@@ -134,10 +134,15 @@ def parse(self, line: str):
134134

135135
line = " ".join(line)
136136
# substitue register name with real value
137+
for each_reg in filter(lambda r: len(r) == 3, self.ql.reg.register_mapping):
138+
reg = f"${each_reg}"
139+
if reg in line:
140+
line = re.sub(f"\\{reg}", hex(self.ql.reg.read(each_reg)), line)
141+
137142
for each_reg in filter(lambda r: len(r) == 2, self.ql.reg.register_mapping):
138143
reg = f"${each_reg}"
139144
if reg in line:
140-
line = re.sub(f"\{reg}", hex(self.ql.reg.read(each_reg)), line)
145+
line = re.sub(f"\\{reg}", hex(self.ql.reg.read(each_reg)), line)
141146

142147

143148
ft, sz, ct = fmt

qiling/debugger/qdb/qdb.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from qiling.const import QL_ARCH, QL_VERBOSE
1212
from qiling.debugger import QlDebugger
1313

14-
from .utils import setup_context_render, setup_branch_predictor, SnapshotManager
14+
from .utils import setup_context_render, setup_branch_predictor, SnapshotManager, run_qdb_script
1515
from .memory import setup_memory_Manager
1616
from .misc import parse_int, Breakpoint, TempBreakpoint
1717
from .const import color
@@ -23,7 +23,7 @@ class QlQdb(cmd.Cmd, QlDebugger):
2323
The built-in debugger of Qiling Framework
2424
"""
2525

26-
def __init__(self, ql: Qiling, init_hook: str = "", rr: bool = False) -> None:
26+
def __init__(self, ql: Qiling, init_hook: str = "", rr: bool = False, script: str = "") -> None:
2727
"""
2828
@init_hook: the entry to be paused at
2929
@rr: record/replay debugging
@@ -32,6 +32,7 @@ def __init__(self, ql: Qiling, init_hook: str = "", rr: bool = False) -> None:
3232
self.ql = ql
3333
self.prompt = f"{color.BOLD}{color.RED}Qdb> {color.END}"
3434
self._saved_reg_dump = None
35+
self._script = script
3536
self.bp_list = {}
3637

3738
self.rr = SnapshotManager(ql) if rr else None
@@ -82,8 +83,11 @@ def bp_handler(ql, address, size, bp_list):
8283
else:
8384
self.init_state = self.ql.save()
8485

85-
self.do_context()
86-
self.interactive()
86+
if self._script:
87+
run_qdb_script(self, self._script)
88+
else:
89+
self.do_context()
90+
self.interactive()
8791

8892
@property
8993
def cur_addr(self) -> int:
@@ -354,6 +358,17 @@ def do_show(self, *args) -> None:
354358
if self.rr:
355359
qdb_print(QDB_MSG.INFO, f"Snapshots: {len([st for st in self.rr.layers if isinstance(st, self.rr.DiffedState)])}")
356360

361+
def do_script(self, filename: str) -> None:
362+
"""
363+
usage: script [filename]
364+
load a script for automate qdb funcitonality, execute qdb command line by line basically
365+
"""
366+
367+
if filename:
368+
run_qdb_script(self, filename)
369+
else:
370+
qdb_print(QDB_MSG.ERROR, "parameter filename must be specified")
371+
357372
def do_shell(self, *command) -> None:
358373
"""
359374
run python code
@@ -370,12 +385,15 @@ def do_quit(self, *args) -> bool:
370385
"""
371386

372387
self.ql.stop()
388+
if self._script:
389+
return True
373390
exit()
374391

375392
def do_EOF(self, *args) -> None:
376393
"""
377394
handle Ctrl+D
378395
"""
396+
379397
if input(f"{color.RED}[!] Are you sure about saying good bye ~ ? [Y/n]{color.END} ").strip() == "Y":
380398
self.do_quit()
381399

qiling/debugger/qdb/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ def setup_context_render(ql, predictor):
8080
QL_ARCH.MIPS: ContextRenderMIPS,
8181
}.get(ql.archtype)(ql, predictor)
8282

83+
def run_qdb_script(qdb, filename: str) -> None:
84+
with open(filename) as fd:
85+
for line in iter(fd.readline, ""):
86+
87+
# skip commented and empty line
88+
if line.startswith("#") or line == "\n":
89+
continue
90+
91+
cmd, arg, _ = qdb.parseline(line)
92+
func = getattr(qdb, f"do_{cmd}")
93+
if arg:
94+
func(arg)
95+
else:
96+
func()
97+
8398

8499
"""
85100

qiling/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,4 +587,4 @@ def verify_ret(ql, err):
587587
else:
588588
raise
589589
else:
590-
raise
590+
raise

tests/qdb_scripts/arm.qdb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# This line is demonstrate comment in qdb script
2+
3+
x/10wx 0x7ff3cee4
4+
x $sp
5+
x $sp + 0x10
6+
x/5i 0x047ba9e0
7+
b 0x047ba9ec
8+
c
9+
s
10+
n
11+
p
12+
p
13+
q

tests/qdb_scripts/mips32el.qdb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# This line is demonstrate comment in qdb script
2+
3+
x/10wx 0x7ff3cec0
4+
x $sp
5+
x $sp + 0x10
6+
x/5i 0x047bac40
7+
b 0x047bac50
8+
c
9+
s
10+
n
11+
p
12+
p
13+
q

tests/qdb_scripts/x86.qdb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This line is demonstrate comment in qdb script
2+
3+
x/4wx 0x7ff3cee0
4+
x $esp
5+
x $esp + 0x4
6+
x/5i 0x047bac70
7+
s
8+
n
9+
p
10+
p
11+
q

tests/test_onlinux.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ python3 ./test_android.py &&
1616
python3 ./test_mcu.py &&
1717
python3 ./test_evm.py &&
1818
python3 ./test_blob.py &&
19+
python3 ./test_qdb.py &&
1920
echo "Done Test"

0 commit comments

Comments
 (0)