preparing for tmux and gdb

This commit is contained in:
Ciro Santilli
2018-08-28 00:21:22 +01:00
parent 9484b43942
commit f46c7470e8
3 changed files with 110 additions and 103 deletions

View File

@@ -1,9 +1,11 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse import argparse
import base64
import imp import imp
import subprocess import subprocess
import os import os
import shlex
import sys import sys
this = sys.modules[__name__] this = sys.modules[__name__]
@@ -27,6 +29,9 @@ this = sys.modules[__name__]
# printf "exit_status $?\n" >> "$results_file" # printf "exit_status $?\n" >> "$results_file"
#) #)
def base64_encode(string):
return base64.b64encode(string.encode()).decode()
def get_argparse(**kwargs): def get_argparse(**kwargs):
""" """
Return an argument parser with common arguments set. Return an argument parser with common arguments set.
@@ -104,6 +109,12 @@ around when you checkout between branches.
parser.set_defaults(**this.configs) parser.set_defaults(**this.configs)
return parser return parser
def print_cmd(cmd):
out = []
for arg in cmd:
out.extend([shlex.quote(arg), ' \\\n'])
print(''.join(out))
def setup(parser): def setup(parser):
""" """
Parse the command line arguments, and setup several variables based on them. Parse the command line arguments, and setup several variables based on them.
@@ -138,6 +149,7 @@ def setup(parser):
this.qemu_executable = os.path.join(this.qemu_variant_dir, '{}-softmmu'.format(args.arch), 'qemu-system-{}'.format(args.arch)) this.qemu_executable = os.path.join(this.qemu_variant_dir, '{}-softmmu'.format(args.arch), 'qemu-system-{}'.format(args.arch))
this.qemu_guest_custom_dir = os.path.join(this.build_dir, 'qemu-custom') this.qemu_guest_custom_dir = os.path.join(this.build_dir, 'qemu-custom')
this.host_dir = os.path.join(this.buildroot_out_dir, 'host') this.host_dir = os.path.join(this.buildroot_out_dir, 'host')
this.host_bin_dir = os.path.join(this.host_dir, 'usr', 'bin')
this.images_dir = os.path.join(this.buildroot_out_dir, 'images') this.images_dir = os.path.join(this.buildroot_out_dir, 'images')
this.ext2_file = os.path.join(this.images_dir, 'rootfs.ext2') this.ext2_file = os.path.join(this.images_dir, 'rootfs.ext2')
this.qcow2_file = os.path.join(this.images_dir, 'rootfs.ext2.qcow2') this.qcow2_file = os.path.join(this.images_dir, 'rootfs.ext2.qcow2')

27
run
View File

@@ -1,17 +1,14 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import base64
import common
import os import os
import shlex import shlex
import subprocess import subprocess
import sys import sys
import common
# Argparse. # Argparse.
parser = common.get_argparse( parser = common.get_argparse(description='Run Linux on an emulator')
description='Run Linux on an emulator'
)
init_group = parser.add_mutually_exclusive_group() init_group = parser.add_mutually_exclusive_group()
kvm_group = parser.add_mutually_exclusive_group() kvm_group = parser.add_mutually_exclusive_group()
parser.add_argument( parser.add_argument(
@@ -176,7 +173,7 @@ else:
if args.debug: if args.debug:
extra_qemu_args.append('-S') extra_qemu_args.append('-S')
if args.kernel_cli_extra_after_dash_base64 is not None: if args.kernel_cli_extra_after_dash_base64 is not None:
kernel_cli_extra_after_dash += ' lkmc_eval_base64="{}"'.format(base64.b64encode(args.kernel_cli_extra_after_dash_base64)) kernel_cli_extra_after_dash += ' lkmc_eval_base64="{}"'.format(common.base64_encode(args.kernel_cli_extra_after_dash_base64))
if args.kernel_cli_extra_after_dash is not None: if args.kernel_cli_extra_after_dash is not None:
kernel_cli_extra_after_dash += ' {}'.format(args.kernel_cli_extra_after_dash) kernel_cli_extra_after_dash += ' {}'.format(args.kernel_cli_extra_after_dash)
if args.kgdb: if args.kgdb:
@@ -195,13 +192,13 @@ if args.eval is not None:
else: else:
initarg = 'init' initarg = 'init'
kernel_cli_extra += ' {}=/eval_base64.sh'.format(initarg) kernel_cli_extra += ' {}=/eval_base64.sh'.format(initarg)
kernel_cli_extra_after_dash += ' lkmc_eval="{}"'.format(base64.b64encode(args.eval)) kernel_cli_extra_after_dash += ' lkmc_eval="{}"'.format(common.base64_encode(args.eval))
if not args.graphic: if not args.graphic:
if args.arch == 'x86_64': if args.arch == 'x86_64':
kernel_cli_extra += ' console=ttyS0' kernel_cli_extra += ' console=ttyS0'
extra_qemu_args.append('-nographic') extra_qemu_args.append('-nographic')
if kernel_cli_extra_after_dash: if kernel_cli_extra_after_dash:
kernel_cli_extra += " - {}".format(kernel_cli_extra_after_dash) kernel_cli_extra += " -{}".format(kernel_cli_extra_after_dash)
# A dummy value that is already turned on by default and does not produce large output, # 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. # just to prevent QEMU from emitting a warning that '' is not valid.
@@ -361,13 +358,6 @@ else:
virtio_gpu_pci virtio_gpu_pci
) )
cmd += extra_emulator_args
print(' \\\n'.join(cmd))
try:
subprocess.Popen(cmd, env=env).wait()
except KeyboardInterrupt:
pass
#if args.tmux: #if args.tmux:
# if args.gem5: # if args.gem5:
# eval "./tmu 'sleep 2;./gem5-shell -n ${common_run_id} ${tmux_args};'" # eval "./tmu 'sleep 2;./gem5-shell -n ${common_run_id} ${tmux_args};'"
@@ -377,6 +367,11 @@ except KeyboardInterrupt:
# extra_emulator_args="${extra_emulator_args}${@} \\ # extra_emulator_args="${extra_emulator_args}${@} \\
#" #"
#fi #fi
cmd += extra_emulator_args
common.print_cmd(cmd)
subprocess.Popen(cmd, env=env).wait()
#cmd="time \\ #cmd="time \\
#${cmd}${extra_emulator_args}" #${cmd}${extra_emulator_args}"
#if [ -z "$debug_vm" ]; then #if [ -z "$debug_vm" ]; then

174
rungdb
View File

@@ -1,87 +1,87 @@
#!/usr/bin/env bash #!/usr/bin/env python3
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common"
after= import os
before= import shlex
lx_symbols="-ex 'lx-symbols ../kernel_module-1.0/' \\ import signal
" import subprocess
kgdb=false
docontinue=true import common
while getopts "A:a:b:CgkL:n:X${common_getopts_flags}" OPT; do
case "$OPT" in parser = common.get_argparse(description='Connect with GDB to an emulator to debug Linux itself')
A) parser.add_argument(
after="$OPTARG" '-A', '--after', default='',
;; help='Pass extra arguments to GDB, to be appended after all other arguments'
b) )
before="$OPTARG" parser.add_argument(
;; '-b', '--before', default='',
C) help='Pass extra arguments to GDB to be prepended before any of the arguments passed by this script'
# No Continue. )
docontinue=false parser.add_argument(
;; '-C', '--no-continue', default=False, action='store_true',
k) help="Don't run continue after connecting"
kgdb=true )
;; parser.add_argument(
X) '-k', '--kgdb', default=False, action='store_true'
lx_symbols= )
;; parser.add_argument(
?) '-X', '--no-lxsymbols', default=False, action='store_true'
common_getopts_case "$OPT" )
;; parser.add_argument(
esac 'break_at', nargs='?',
done help='Extra options to append at the end of the emulator command line'
shift "$(($OPTIND - 1))" )
if [ "$#" -gt 0 ]; then args = common.setup(parser)
brk="-ex 'break ${1}' \\ after = shlex.split(args.after)
" before = shlex.split(args.before)
shift if args.no_lxsymbols:
else lx_symbols = []
brk= else:
fi lx_symbols = ['-ex', 'lx-symbols ../kernel_module-1.0/']
common_setup if args.break_at is not None:
gdb="${common_host_dir}/usr/bin/${common_arch}-linux-gdb \\ break_at = ['-ex', 'break {}'.format(args.break_at)]
${before}" else:
if "$kgdb"; then break_at = []
cmd="\ cmd = (
${gdb}\ [
-q \\ os.path.join(common.host_bin_dir,
-ex 'add-auto-load-safe-path $(pwd)' \\ '{}-linux-gdb'.format(args.arch))
-ex 'file vmlinux' \\ ] +
-ex 'target remote localhost:${common_gdb_port}' \\ before +
" [
else '-q',
# ## lx-symbols '-ex', 'add-auto-load-safe-path {}'.format(common.linux_variant_dir),
# '-ex', 'file {}'.format(common.vmlinux),
# ### lx-symbols after continue '-ex', 'target remote localhost:{}'.format(common.gdb_port),
# ]
# lx symbols must be run after continue. )
# if not args.kgdb:
# running it immediately after the connect on the bootloader leads to failure, cmd.extend(break_at)
# likely because kernel structure on which it depends are not yet available. if not args.no_continue:
# # ## lx-symbols
# With this setup, continue runs, and lx-symbols only runs when a break happens, #
# either by hitting the breakpoint, or by entering Ctrl + C. # ### lx-symbols after continue
# #
# Sure, if the user sets a break on a raw address of the bootloader, # lx symbols must be run after continue.
# problems will still arise, but let's think about that some other time. #
# # running it immediately after the connect on the bootloader leads to failure,
# ### lx-symbols autoload # likely because kernel structure on which it depends are not yet available.
# #
# The lx-symbols commands gets loaded through the file vmlinux-gdb.py # With this setup, continue runs, and lx-symbols only runs when a break happens,
# which gets put on the kernel build root when python debugging scripts are enabled. # either by hitting the breakpoint, or by entering Ctrl + C.
cmd="\ #
${gdb}\ # Sure, if the user sets a break on a raw address of the bootloader,
-q \\ # problems will still arise, but let's think about that some other time.
-ex 'add-auto-load-safe-path $(pwd)' \\ #
-ex 'file vmlinux' \\ # ### lx-symbols autoload
-ex 'target remote localhost:${common_gdb_port}' \\ #
${brk}\ # The lx-symbols commands gets loaded through the file vmlinux-gdb.py
" # which gets put on the kernel build root when python debugging scripts are enabled.
fi cmd.extend(['-ex', 'continue'] + lx_symbols)
if "$docontinue"; then cmd.extend(after)
cmd="${cmd}\ common.print_cmd(cmd)
-ex continue \\
${lx_symbols}\ # TODO eeval
" # "${common.root_dir}/eeval" "$cmd $after" "${common.run_dir}/rungdb.sh"
fi def signal_handler(sig, frame): pass
"${common_root_dir}/eeval" "cd '${common_linux_variant_dir}' && \\ signal.signal(signal.SIGINT, signal_handler)
$cmd $after" "${common_run_dir}/rungdb.sh" subprocess.Popen(cmd, cwd=common.linux_variant_dir).wait()