@@ -85,35 +85,74 @@ def main():
8585 parser .add_argument ("--log-file" , default = "qemu-run.log" , help = "Path to save the QEMU output log. Defaults to 'qemu-run.log'." )
8686 parser .add_argument ("--timeout" , type = int , default = 2 , help = "Seconds of silence before assuming QEMU has hung or finished. Defaults to 2." )
8787 parser .add_argument ("--valgrind" , action = "store_true" , help = "Run with valgrind (native_sim only)." )
88- parser .add_argument ("--cores" , type = int , help = "Number of SMP cores for QEMU." )
88+ parser .add_argument ("--cores" , type = int , default = None , help = "Number of SMP cores to emulate in QEMU." )
8989 parser .add_argument ("--mtrace-log" , help = "Path to MTrace log file for ADSP ACE30." )
90- parser .add_argument ("--tcp-monitor" , type = int , help = "Expose QEMU monitor on local TCP port." )
90+ parser .add_argument ("--tcp-monitor" , type = int , nargs = "?" , const = 1025 , default = None , help = "Start the QEMU TCP monitor socket. Optionally specify the port (default: 1025) ." )
9191 parser .add_argument ("--qemu-d" , help = "Pass -d flags to QEMU." )
9292 parser .add_argument ("--exec-log" , help = "Pass -D log file to QEMU." )
9393 parser .add_argument ("--rebuild" , action = "store_true" , help = "Rebuild before running." )
94- parser .add_argument ("--test- ztest" , action = "store_true" , help = "Build and run with ZTest overlay." )
94+ parser .add_argument ("--ztest" , action = "store_true" , help = "Build and run with ZTest overlay." )
9595 parser .add_argument ("--test-fw-standard" , action = "store_true" , help = "Build and run standard FW with ZTest enabled." )
9696 parser .add_argument ("--interactive" , action = "store_true" , help = "Run QEMU directly in interactive mode (disables crash monitor)." )
9797 args = parser .parse_args ()
9898
99+ # Clean up old log files before starting
100+ for log_path in [args .mtrace_log , args .exec_log ]:
101+ if log_path and os .path .exists (log_path ):
102+ try :
103+ os .remove (log_path )
104+ print (f"[sof-qemu-run] Cleaned up old log: { log_path } " )
105+ except Exception as e :
106+ print (f"[sof-qemu-run] Warning: Could not delete { log_path } : { e } " )
107+
108+ extra_qemu_flags = []
109+ if args .cores :
110+ extra_qemu_flags .append (f"-smp { args .cores } " )
111+
112+ if args .tcp_monitor :
113+ extra_qemu_flags .append (f"-monitor tcp:localhost:{ args .tcp_monitor } ,server,nowait" )
114+
115+ if args .mtrace_log :
116+ # For ADSP boards, mtrace-file is a machine parameter.
117+ # We append it to a -machine flag. West will append this to its own flags.
118+ extra_qemu_flags .append (f"-machine adsp_ace30,mtrace-file={ args .mtrace_log } " )
119+ os .environ ["QEMU_ACE_MTRACE_FILE" ] = args .mtrace_log
120+ print (f"[sof-qemu-run] Setting QEMU_ACE_MTRACE_FILE: { args .mtrace_log } " )
121+
122+ if args .exec_log :
123+ extra_qemu_flags .append (f"-D { args .exec_log } " )
124+
125+ if args .qemu_d :
126+ extra_qemu_flags .append (f"-d { args .qemu_d } " )
127+
128+ if extra_qemu_flags :
129+ existing_flags = os .environ .get ("QEMU_EXTRA_FLAGS" , "" )
130+ os .environ ["QEMU_EXTRA_FLAGS" ] = f"{ existing_flags } { ' ' .join (extra_qemu_flags )} " .strip ()
131+ print (f"[sof-qemu-run] QEMU_EXTRA_FLAGS: { os .environ ['QEMU_EXTRA_FLAGS' ]} " )
132+
99133 # Make absolute path just in case
100134 build_dir = os .path .abspath (args .build_dir )
101135
102- # Board detection from .config
136+ print (f"Starting QEMU test runner (Build Dir: { args .build_dir } )..." )
137+
138+ west_path = shutil .which ("west" )
139+
140+ # Detect the board configuration from CMakeCache.txt
141+ is_native_sim = False
103142 board = "unknown"
104- config_path = os .path .join (build_dir , "zephyr" , ".config" )
105- if os .path .isfile (config_path ):
106- with open (config_path , "r" ) as f :
107- for line in f :
108- if line .startswith ("CONFIG_BOARD=" ):
109- board = line .split ("=" )[1 ].strip ().strip ('"' )
110- break
143+ cmake_cache = os .path .join (build_dir , "CMakeCache.txt" )
111144
112- # Detection for native_sim board
113- is_native_sim = "native_sim" in board
145+ if os .path .isfile (cmake_cache ):
146+ with open (cmake_cache , "r" ) as f :
147+ for line in f :
148+ if line .startswith ("CACHED_BOARD:STRING=" ) or line .startswith ("BOARD:STRING=" ):
149+ board = line .split ("=" , 1 )[1 ].strip ()
150+ if "native_sim" in board :
151+ is_native_sim = True
152+ break
114153
115- if args .test_ztest :
116- print ("\n \033 [32;1m[sof-qemu-run] ISOLATED ZTEST ENABLED: Only unit testing modules will be built and executed .\033 [0m" )
154+ if args .ztest :
155+ print ("\n \033 [32;1m[sof-qemu-run] ZTEST ENABLED: Mathematics and firmware testing configured .\033 [0m" )
117156 if args .rebuild :
118157 print ("\033 [32;1m[sof-qemu-run] Recompiling Zephyr firmware with testing overlays natively...\033 [0m" )
119158 # Inject standard rimage build directory directly into PATH so `west sign` mathematically authenticates Zephyr.elf into Zephyr.ri directly seamlessly.
@@ -322,7 +361,7 @@ def main():
322361 run_sof_crash_decode (build_dir , full_output )
323362 else :
324363 if is_native_sim :
325- print ("\n [sof-qemu-run] No crash detected." )
364+ print ("\n [sof-qemu-run] No crash detected. (Skipping QEMU monitor interaction for native_sim) " )
326365 else :
327366 print ("\n [sof-qemu-run] No crash detected. Interacting with QEMU Monitor to grab registers..." )
328367
0 commit comments