1#!/bin/sh
2
3##########################################################################
4# Copyright (c) 2007, 2008, 2009, 2010, 2013, ETH Zurich.
5# All rights reserved.
6#
7# This file is distributed under the terms in the attached LICENSE file.
8# If you do not find this file, copies can be found by writing to:
9# ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
10##########################################################################
11
12# Sanely starts the debugger with the simulator.
13
14QEMU_CMD=$1
15GDB=$2
16GDB_ARGS=$3
17SERIAL_OUTPUT=$4
18PORT=$((10000 + UID))
19
20if [ "${SERIAL_OUTPUT}" = "" ] ; then
21    # Assuming session is interactive. Use terminal for serial output because
22    # stdout does not work for daemonized qemu and output is lost. This likely
23    # only matters on ARM where there is no video driver at time of writing.
24    SERIAL_OUTPUT=`tty`
25fi
26
27PIDFILE=/tmp/qemu_debugsim_${USER}_${PORT}.pid
28if test -f $PIDFILE; then
29    if ps `cat $PIDFILE` >/dev/null; then
30	echo "Another QEMU already running (PID: `cat $PIDFILE` PIDFILE: $PIDFILE)"
31	exit 1
32    else
33	echo "Deleting stale lockfile $PIDFILE"
34	rm -f $PIDFILE
35    fi
36fi
37
38echo args = $GDB_ARGS
39
40cat > barrelfish_debug.gdb <<EOF
41# Connect to QEMU instance
42target remote localhost:$PORT
43EOF
44
45QEMU_INVOCATION="${QEMU_CMD} -serial $SERIAL_OUTPUT -gdb tcp::$PORT -S -daemonize -pidfile $PIDFILE"
46eval ${QEMU_INVOCATION}
47if [ $? -eq 0 ] ; then 
48    stty sane
49    trap '' SIGINT
50    ${GDB} -x barrelfish_debug.gdb ${GDB_ARGS}
51    PID=`cat ${PIDFILE}`
52    kill ${PID} > /dev/null || true
53    rm -f $PIDFILE
54else
55    echo Failed to launch qemu with:
56    echo "   ${QEMU_INVOCATION}"
57fi
58