1111from qiling .const import QL_ARCH , QL_VERBOSE
1212from 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
1515from .memory import setup_memory_Manager
1616from .misc import parse_int , Breakpoint , TempBreakpoint
1717from .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
0 commit comments