forked from mozilla-b2g/B2G
-
Notifications
You must be signed in to change notification settings - Fork 2
/
run-gdb.sh
executable file
·142 lines (120 loc) · 4.44 KB
/
run-gdb.sh
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/bash
#set -x
get_pid_by_name() {
echo $($ADB shell "toolbox ps '$1' | (read header; read user pid rest; echo -n \$pid)")
}
SCRIPT_NAME=$(basename $0)
. load-config.sh
ADB=${ADB:-adb}
if [ ! -f "`which \"$ADB\"`" ]; then
ADB=out/host/`uname -s | tr "[[:upper:]]" "[[:lower:]]"`-x86/bin/adb
fi
echo "ADB Location: " $ADB
case $DEVICE in
generic_x86)
TARGET_ARCH=x86
TARGET_TRIPLE=i686-linux-android
;;
*)
TARGET_ARCH=arm
TARGET_TRIPLE=arm-linux-androideabi
;;
esac
HOST_OS=$(uname -s | tr "[[:upper:]]" "[[:lower:]]")-x86
if [ -z "${GDB}" ]; then
if [ -d prebuilt ]; then
GDB=prebuilt/${HOST_OS}/toolchain/${TARGET_TRIPLE}-4.4.x/bin/${TARGET_TRIPLE}-gdb
elif [ -d prebuilts ]; then
GDB=prebuilts/gcc/${HOST_OS}/${TARGET_ARCH}/${TARGET_TRIPLE}-4.7/bin/${TARGET_TRIPLE}-gdb
PYTHON_DIR=prebuilts/python/${HOST_OS}/2.7.5
if [ -d $PYTHON_DIR ]; then
export PYTHONHOME=$PYTHON_DIR
fi
else
echo "Not sure where gdb is located. Override using GDB= or fix the script."
exit 1
fi
fi
B2G_BIN=/system/b2g/b2g
GDBINIT=/tmp/b2g.gdbinit.$(whoami).$$
GONK_OBJDIR=out/target/product/$DEVICE
SYMDIR=$GONK_OBJDIR/symbols
GDBSERVER_PID=$(get_pid_by_name gdbserver)
GDB_PORT=$((10000 + $(id -u) % 50000))
if [ "$1" = "vgdb" -a -n "$2" ] ; then
GDB_PORT="$2"
elif [ "$1" = "attach" -a -n "$2" ] ; then
B2G_PID=$2
if [ -z "$($ADB ls /proc/$B2G_PID)" ] ; then
ATTACH_TARGET=$B2G_PID
B2G_PID=$(get_pid_by_name "$B2G_PID")
if [ -z "$B2G_PID" ] ; then
echo Error: PID $ATTACH_TARGET is invalid
exit 1;
fi
echo "Found $ATTACH_TARGET PID: $B2G_PID"
fi
GDB_PORT=$((10000 + ($B2G_PID + $(id -u)) % 50000))
# cmdline is null separated
B2G_BIN=$($ADB shell cat /proc/$B2G_PID/cmdline | tr '\0' '\n' | head -1)
else
B2G_PID=$(get_pid_by_name b2g)
fi
for p in $GDBSERVER_PID ; do
$ADB shell cat /proc/$p/cmdline | grep -q :$GDB_PORT && ( \
echo ..killing gdbserver pid $p
$ADB shell kill $p
) || echo ..ignoring gdbserver pid $p
done
$ADB forward tcp:$GDB_PORT tcp:$GDB_PORT
if [ "$1" = "attach" ]; then
if [ -z "$B2G_PID" ]; then
echo Error: No PID to attach to. B2G not running?
exit 1
fi
$ADB shell gdbserver :$GDB_PORT --attach $B2G_PID &
elif [ "$1" != "vgdb" ]; then
if [ -n "$1" ]; then
B2G_BIN=$1
shift
fi
[ -n "$MOZ_PROFILER_STARTUP" ] && GDBSERVER_ENV="$GDBSERVER_ENV MOZ_PROFILER_STARTUP=$MOZ_PROFILER_STARTUP "
[ -n "$MOZ_DEBUG_CHILD_PROCESS" ] && GDBSERVER_ENV="$GDBSERVER_ENV MOZ_DEBUG_CHILD_PROCESS=$MOZ_DEBUG_CHILD_PROCESS "
[ -n "$MOZ_DEBUG_APP_PROCESS" ] && GDBSERVER_ENV="$GDBSERVER_ENV MOZ_DEBUG_APP_PROCESS='$MOZ_DEBUG_APP_PROCESS' "
[ -n "$MOZ_IPC_MESSAGE_LOG" ] && GDBSERVER_ENV="$GDBSERVER_ENV MOZ_IPC_MESSAGE_LOG=$MOZ_IPC_MESSAGE_LOG "
[ -n "$B2G_PID" ] && $ADB shell kill $B2G_PID
[ "$B2G_BIN" = "/system/b2g/b2g" ] && $ADB shell stop b2g
if [ "$($ADB shell "if [ -f /system/b2g/libdmd.so ]; then echo 1; fi")" != "" ]; then
echo ""
echo "Using DMD."
echo ""
dmd="1"
ld_preload_extra="/system/b2g/libdmd.so"
fi
$ADB shell DMD=$dmd LD_LIBRARY_PATH=/system/b2g LD_PRELOAD=\"$ld_preload_extra /system/b2g/libmozglue.so\" TMPDIR=/data/local/tmp $GDBSERVER_ENV gdbserver --multi :$GDB_PORT $B2G_BIN $@ &
fi
sleep 1
echo "set solib-absolute-prefix $SYMDIR" > $GDBINIT
echo "handle SIGPIPE nostop" >> $GDBINIT
echo "set solib-search-path $GECKO_OBJDIR/dist/bin:$SYMDIR/system/lib:$SYMDIR/system/lib/hw:$SYMDIR/system/lib/egl:$SYMDIR/system/bin:$GONK_OBJDIR/system/lib:$GONK_OBJDIR/system/lib/egl:$GONK_OBJDIR/system/lib/hw:$GONK_OBJDIR/system/vendor/lib:$GONK_OBJDIR/system/vendor/lib/hw:$GONK_OBJDIR/system/vendor/lib/egl" >> $GDBINIT
if [ "$1" == "vgdb" ] ; then
echo "target remote :$GDB_PORT" >> $GDBINIT
else
echo "target extended-remote :$GDB_PORT" >> $GDBINIT
fi
PROG=$GECKO_OBJDIR/dist/bin/$(basename $B2G_BIN)
[ -f $PROG ] || PROG=${SYMDIR}${B2G_BIN}
if [[ "$-" == *x* ]]; then
# Since we got here, set -x was enabled near the top of the file. print
# out the contents of of the gdbinit file.
echo "----- Start of $GDBINIT -----"
cat $GDBINIT
echo "----- End of $GDBINIT -----"
fi
if [ "$SCRIPT_NAME" == "run-ddd.sh" ]; then
echo "ddd --debugger \"$GDB -x $GDBINIT\" $PROG"
ddd --debugger "$GDB -x $GDBINIT" $PROG
else
echo $GDB -x $GDBINIT $PROG
$GDB -x $GDBINIT $PROG
fi