diff --git a/common.py b/common.py index 052aa1f..b515b89 100644 --- a/common.py +++ b/common.py @@ -135,10 +135,15 @@ around when you checkout between branches. parser.set_defaults(**defaults) return parser -def print_cmd(cmd, cmd_file=None): +def print_cmd(cmd, cmd_file=None, extra_env=None): + if extra_env is None: + extra_env = {} + newline_separator = ' \\\n' out = [] + for key in extra_env: + out.extend(['{}={}'.format(shlex.quote(key), shlex.quote(extra_env[key])), newline_separator]) for arg in cmd: - out.extend([shlex.quote(arg), ' \\\n']) + out.extend([shlex.quote(arg), newline_separator]) out = ''.join(out) print(out) if cmd_file is not None: @@ -148,7 +153,7 @@ def print_cmd(cmd, cmd_file=None): st = os.stat(cmd_file) os.chmod(cmd_file, st.st_mode | stat.S_IXUSR) -def run_cmd(cmd, cmd_file=None, out_file=None, **kwargs): +def run_cmd(cmd, cmd_file=None, out_file=None, extra_env=None, **kwargs): """ Run a command. Write the command to stdout before running it. @@ -159,19 +164,23 @@ def run_cmd(cmd, cmd_file=None, out_file=None, **kwargs): - cmd_file is not None, write the command to the given file - out_file is not None, write the stdout and stderr of the command to the given file """ - if out_file: + if out_file is not None: stdout=subprocess.PIPE stderr=subprocess.STDOUT else: stdout=None stderr=None - print_cmd(cmd, cmd_file) + if extra_env is None: + extra_env = {} + env = os.environ.copy() + env.update(extra_env) + print_cmd(cmd, cmd_file, extra_env=extra_env) # Otherwise Ctrl + C gives: # - ugly Python stack trace for gem5 (QEMU takes over terminal and is fine). # - kills Python, and that then kills GDB: https://stackoverflow.com/questions/19807134/does-python-always-raise-an-exception-if-you-do-ctrlc-when-a-subprocess-is-exec signal.signal(signal.SIGINT, signal.SIG_IGN) # https://stackoverflow.com/questions/15535240/python-popen-write-to-stdout-and-log-file-simultaneously/52090802#52090802 - with subprocess.Popen(cmd, stdout=stdout, stderr=stderr, **kwargs) as proc: + with subprocess.Popen(cmd, stdout=stdout, stderr=stderr, env=env, **kwargs) as proc: if out_file is not None: with open(out_file, 'bw') as logfile: while True: diff --git a/run b/run index cb21f05..d5f4f76 100755 --- a/run +++ b/run @@ -163,7 +163,6 @@ args = common.setup(parser) kernel_cli_extra = 'console_msg_format=syslog nokaslr norandmaps panic=-1 printk.devkmsg=on printk.time=y' if args.kernel_cli_extra is not None: kernel_cli_extra += ' {}'.format(args.kernel_cli_extra) -env = os.environ.copy() kernel_cli_extra_after_dash = '' extra_emulator_args = args.extra_emulator_args.copy() extra_qemu_args = [] @@ -200,6 +199,7 @@ if not args.graphic: extra_qemu_args.append('-nographic') if kernel_cli_extra_after_dash: kernel_cli_extra += " -{}".format(kernel_cli_extra_after_dash) +extra_env = {} # A dummy value that is already turned on by default and does not produce large output, # just to prevent QEMU from emitting a warning that '' is not valid. @@ -210,7 +210,7 @@ if args.gem5: gem5_exe_args = shlex.split(args.gem5_exe_args) if args.trace is not None: gem5_exe_args.append('--debug-flags={}'.format(args.trace)) - env['M5_PATH'] = common.gem5_system_dir + extra_env['M5_PATH'] = common.gem5_system_dir cmd = ( debug_vm + [ @@ -378,7 +378,7 @@ if debug_vm: out_file = None else: out_file = common.termout_file -returncode = common.run_cmd(cmd, cmd_file=common.run_cmd_file, out_file=out_file, env=env) +returncode = common.run_cmd(cmd, cmd_file=common.run_cmd_file, out_file=out_file, extra_env=extra_env) if returncode != 0: common.error('simulator exited with status != 0') # Check if guest panicked.