mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-26 11:41:35 +01:00
run_cmd: factor out logging of run and rungdb
This commit is contained in:
40
common.py
40
common.py
@@ -7,6 +7,7 @@ import re
|
|||||||
import subprocess
|
import subprocess
|
||||||
import os
|
import os
|
||||||
import shlex
|
import shlex
|
||||||
|
import signal
|
||||||
import stat
|
import stat
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
@@ -144,6 +145,43 @@ def print_cmd(cmd, cmd_file=None):
|
|||||||
st = os.stat(cmd_file)
|
st = os.stat(cmd_file)
|
||||||
os.chmod(cmd_file, st.st_mode | stat.S_IXUSR)
|
os.chmod(cmd_file, st.st_mode | stat.S_IXUSR)
|
||||||
|
|
||||||
|
def run_cmd(cmd, cmd_file=None, out_file=None, **kwargs):
|
||||||
|
"""
|
||||||
|
Run a command. Write the command to stdout before running it.
|
||||||
|
|
||||||
|
Wait until the command finishes execution.
|
||||||
|
|
||||||
|
If:
|
||||||
|
|
||||||
|
- 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:
|
||||||
|
stdout=subprocess.PIPE
|
||||||
|
stderr=subprocess.STDOUT
|
||||||
|
else:
|
||||||
|
stdout=None
|
||||||
|
stderr=None
|
||||||
|
print_cmd(cmd, cmd_file)
|
||||||
|
# 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:
|
||||||
|
if out_file is not None:
|
||||||
|
with open(out_file, 'bw') as logfile:
|
||||||
|
while True:
|
||||||
|
byte = proc.stdout.read(1)
|
||||||
|
if byte:
|
||||||
|
sys.stdout.buffer.write(byte)
|
||||||
|
sys.stdout.flush()
|
||||||
|
logfile.write(byte)
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
||||||
|
return proc.returncode
|
||||||
|
|
||||||
def setup(parser, **extra_args):
|
def setup(parser, **extra_args):
|
||||||
"""
|
"""
|
||||||
Parse the command line arguments, and setup several variables based on them.
|
Parse the command line arguments, and setup several variables based on them.
|
||||||
@@ -208,7 +246,7 @@ def setup(parser, **extra_args):
|
|||||||
this.executable = this.qemu_executable
|
this.executable = this.qemu_executable
|
||||||
this.run_dir = this.qemu_run_dir
|
this.run_dir = this.qemu_run_dir
|
||||||
this.termout_file = this.qemu_termout_file
|
this.termout_file = this.qemu_termout_file
|
||||||
this.cmd_file = os.path.join(this.run_dir, 'cmd.sh')
|
this.run_cmd_file = os.path.join(this.run_dir, 'cmd.sh')
|
||||||
if args.arch == 'arm':
|
if args.arch == 'arm':
|
||||||
this.linux_image = os.path.join('arch', 'arm', 'boot', 'zImage')
|
this.linux_image = os.path.join('arch', 'arm', 'boot', 'zImage')
|
||||||
elif args.arch == 'aarch64':
|
elif args.arch == 'aarch64':
|
||||||
|
|||||||
@@ -10,4 +10,4 @@ parser = common.get_argparse(
|
|||||||
argparse_args={'description':'Connect a terminal to a running gem5 instance'}
|
argparse_args={'description':'Connect a terminal to a running gem5 instance'}
|
||||||
)
|
)
|
||||||
args = common.setup(parser)
|
args = common.setup(parser)
|
||||||
sys.exit(subprocess.Popen([str(common.gem5_m5term), 'localhost', str(common.gem5_telnet_port)]).wait())
|
sys.exit(common.run_cmd([str(common.gem5_m5term), 'localhost', str(common.gem5_telnet_port)]))
|
||||||
|
|||||||
29
run
29
run
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import shlex
|
import shlex
|
||||||
import signal
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
@@ -375,30 +374,12 @@ if args.tmux:
|
|||||||
])
|
])
|
||||||
|
|
||||||
cmd += extra_emulator_args
|
cmd += extra_emulator_args
|
||||||
do_tee = not debug_vm
|
if debug_vm:
|
||||||
if do_tee:
|
out_file = None
|
||||||
stdout=subprocess.PIPE
|
|
||||||
stderr=subprocess.STDOUT
|
|
||||||
else:
|
else:
|
||||||
stdout=None
|
out_file = common.termout_file
|
||||||
stderr=None
|
returncode = common.run_cmd(cmd, cmd_file=common.run_cmd_file, out_file=out_file, env=env)
|
||||||
common.print_cmd(cmd, common.cmd_file)
|
if returncode != 0:
|
||||||
# Otherwise Ctrl + C gives an ugly Python stack trace for gem5 (QEMU takes over terminal and is fine).
|
|
||||||
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, env=env) as proc:
|
|
||||||
if do_tee:
|
|
||||||
with open(common.termout_file, 'bw') as logfile:
|
|
||||||
while True:
|
|
||||||
byte = proc.stdout.read(1)
|
|
||||||
if byte:
|
|
||||||
sys.stdout.buffer.write(byte)
|
|
||||||
sys.stdout.flush()
|
|
||||||
logfile.write(byte)
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
|
||||||
if proc.returncode != 0:
|
|
||||||
common.error('simulator exited with status != 0')
|
common.error('simulator exited with status != 0')
|
||||||
# Check if guest panicked.
|
# Check if guest panicked.
|
||||||
if args.gem5:
|
if args.gem5:
|
||||||
|
|||||||
11
rungdb
11
rungdb
@@ -79,13 +79,4 @@ if not args.no_continue:
|
|||||||
# which gets put on the kernel build root when python debugging scripts are enabled.
|
# which gets put on the kernel build root when python debugging scripts are enabled.
|
||||||
cmd.extend(['-ex', 'continue'] + lx_symbols)
|
cmd.extend(['-ex', 'continue'] + lx_symbols)
|
||||||
cmd.extend(after)
|
cmd.extend(after)
|
||||||
common.print_cmd(cmd)
|
sys.exit(common.run_cmd(cmd, cmd_file=os.path.join(common.run_dir, 'rungdb.sh'), cwd=common.linux_variant_dir))
|
||||||
|
|
||||||
# TODO eeval
|
|
||||||
# "${common.root_dir}/eeval" "$cmd $after" "${common.run_dir}/rungdb.sh"
|
|
||||||
|
|
||||||
# Required, otherwise Ctrl + C kills Python and that 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, lambda *args: None)
|
|
||||||
|
|
||||||
sys.exit(subprocess.Popen(cmd, cwd=common.linux_variant_dir).wait())
|
|
||||||
|
|||||||
Reference in New Issue
Block a user