-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug
executable file
·75 lines (64 loc) · 3.66 KB
/
debug
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
# Thanks to https://stackoverflow.com/a/65442462 and https://stackoverflow.com/a/32960272
if [ "$#" -eq 0 ]; then
printf "Usage: $0 [filename] [any other nasm args].\nEx: $0 bootstrap-asm.asm -DDVORAK"
printf "\n\nThe debugger will automatically break at the start of the code after"
printf "\nthe bootloader. Unfortunately I couldn't get debug symbols to work, so"
printf "\nyou'll have to manually compare the disassembly with the source code."
printf "\n\nWhen you set breakpoints use the addresses listed in the disassembly"
printf "\noutput. e.g. \`b *0x7E00\`. If you just type \`b\` it won't put the"
printf "\nbreakpoint on the right address because it won't include the cs register"
printf "\nvalue. To break on the current address use \`b *\$rip\`. You can also"
printf "\nstep through and look at jump/call addresses in the disassembly to find"
printf "\nthe addresses of your labels to set breakpoints on.\n"
exit 1
fi
# This is a fix to make qemu not force the gdb architecture back to 32 bit x86
mkdir -p gdbconf
if ! stat gdbconf/i386-32bit.xml >/dev/null 2>&1; then
echo "Setting up a patch to fix the disassembly..."
echo '<?xml version="1.0"?><!DOCTYPE target SYSTEM "gdb-target.dtd"><target><architecture>i8086</architecture><xi:include href="i386-32bit.xml"/></target>' > gdbconf/target.xml
wget -nc https://raw.githubusercontent.com/qemu/qemu/master/gdb-xml/i386-32bit.xml -O gdbconf/i386-32bit.xml
fi
# This is a nice config that provides the automatic disassembly and memory
# printing and several nice functions
# Mostly use stepi and stepo (skip a function call) from this when you debug
GDB_REAL_CONF=gdbconf/gdbinit_real_mode.txt
if ! stat $GDB_REAL_CONF >/dev/null 2>&1; then
echo "Downloading a gdb config to make this nicer... (provides auto disassembly, functions: stepo, find_in_mem, etc)"
wget -nc https://raw.githubusercontent.com/mhugo/gdb_init_real_mode/843e3b530971aeed9cfa1d12f585132e0d651c15/gdbinit_real_mode.txt -O $GDB_REAL_CONF
# Patch in using the above work-around
sed -i '/set architecture i8086/a\
set tdesc filename gdbconf/target.xml' $GDB_REAL_CONF
# Fix the stepo address, not sure why this is wasn't right already...
sed -i 's/set $_nextaddress = $eip + $offset + $noffset/set $_nextaddress = $rip + $offset + $noffset/' $GDB_REAL_CONF
# Also tbreak isn't working for us so we have to clear it in stepo
sed -i '/# else we just single step/i\
clear *$_nextaddress' $GDB_REAL_CONF
fi
file="${@: -1}"
name="${file%%.*}"
binfile="bin/$name.bin"
elffile="bin/$name.o.elf"
bit16file="bin/$name.bits.asm"
mkdir -p `dirname $binfile`
if ! nasm $file -f bin -o $binfile ${@:1:$(($#-1))}; then
exit $?
fi
# Add "bits 16" as the first line which is only needed when making an elf file
sed -e '1ibits 16\' $file > $bit16file
# Make the debug symbols elf file. The extensions is needed for 16 bit to work.
nasm $bit16file -f elf32 -F dwarf -g -w+gnu-elf-extensions -o $elffile ${@:1:$(($#-1))}
rm $bit16file
if ! which qemu-system-i386 > /dev/null; then
printf "\nYou need to install qemu-system-i386 (qemu-arch-extra package in some distributions) to use the debugger\n"
exit 127
fi
qemu-system-i386 $QEMU_ARGS -s -S -drive file=$binfile,format=raw,if=ide &
# Note: If you turn off the offset for the add-symbol-file command then gdb can
# load the source code line numbers correctly but the fancy real-mode-gdb config
# doesn't work because it actually understand the offset correctly.
gdb -ix $GDB_REAL_CONF \
-ex 'target remote localhost:1234' \
-ex "add-symbol-file $elffile -s .text 0x7c00" \
-ex 'b start_' -ex 'c' -ex 'delete breakpoints' # break at the start of the code after the bootloader