blob: 6019a0862e6af26907fd666f56ce44ba817659ba [file] [log] [blame]
David 'Digit' Turnera08d6052010-04-16 12:45:33 -07001#!/bin/sh
2#
3# Copyright (C) 2010 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18# This wrapper script is used to launch a native debugging session
19# on a given NDK application. The application must be debuggable, i.e.
20# its android:debuggable attribute must be set to 'true' in the
21# <application> element of its manifest.
22#
23# See docs/NDK-GDB.TXT for usage description. Essentially, you just
24# need to launch ndk-gdb from your application project directory
25# after doing ndk-build && ant install && <start-application-on-device>
26#
27. `dirname $0`/build/core/ndk-common.sh
28
29force_32bit_binaries
30
31find_program ADB_CMD adb
32ADB_FLAGS=
33
34AWK_CMD=awk
35
36DEBUG_PORT=5039
37
38PARAMETERS=
39OPTION_HELP=no
40OPTION_PROJECT=
41OPTION_FORCE=no
42OPTION_ADB=
43OPTION_EXEC=
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -070044OPTION_START=no
45OPTION_LAUNCH=
46OPTION_LAUNCH_LIST=no
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070047
48check_parameter ()
49{
50 if [ -z "$2" ]; then
51 echo "ERROR: Missing parameter after option '$1'"
52 exit 1
53 fi
54}
55
56check_adb_flags ()
57{
58 if [ -n "$ADB_FLAGS" ] ; then
59 echo "ERROR: Only one of -e, -d or -s <serial> can be used at the same time!"
60 exit 1
61 fi
62}
63
64get_build_var ()
65{
66 if [ -z "$GNUMAKE" ] ; then
67 GNUMAKE=make
68 fi
69 $GNUMAKE --no-print-dir -f $ANDROID_NDK_ROOT/build/core/build-local.mk -C $PROJECT DUMP_$1
70}
71
72get_build_var_for_abi ()
73{
74 if [ -z "$GNUMAKE" ] ; then
75 GNUMAKE=make
76 fi
77 $GNUMAKE --no-print-dir -f $ANDROID_NDK_ROOT/build/core/build-local.mk -C $PROJECT DUMP_$1 APP_ABI=$2
78}
79
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -070080# Used to run an awk script on the manifest
81run_awk_manifest_script ()
82{
83 $AWK_CMD -f $AWK_SCRIPTS/$1 $PROJECT/$MANIFEST
84}
85
David 'Digit' Turner47eff9f2010-06-03 11:57:49 -070086if [ "$HOST_OS" = "cygwin" ] ; then
87# Return native path representation from cygwin one
88# $1: a cygwin-compatible path (e.g. /cygdrive/c/some/thing)
89# Return: path in host windows representation, e.g. C:/some/thing
90#
91# We use mixed mode (i.e. / as the directory separator) because
92# all the tools we use recognize it properly, and it avoids lots
93# of escaping nonsense associated with "\"
94#
95native_path ()
96{
97 cygpath -m $1
98}
99else # HOST_OS != windows
100native_path ()
101{
102 echo "$1"
103}
104fi # HOST_OS != windows
105
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700106VERBOSE=no
107while [ -n "$1" ]; do
108 opt="$1"
109 optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
110 case "$opt" in
111 --help|-h|-\?)
112 OPTION_HELP=yes
113 ;;
114 --verbose)
115 VERBOSE=yes
116 ;;
117 -s)
118 check_parameter $1 $2
119 check_adb_flags
120 ADB_FLAGS=" -s $2"
121 shift
122 ;;
123 -s*)
124 check_adb_flags
125 optarg=`expr -- "$opt" : '-s\(.*\)'`
126 ADB_FLAGS=" -s $optarg"
127 ;;
128 -p)
129 check_parameter $1 $2
130 OPTION_PROJECT="$2"
131 shift
132 ;;
133 -p*)
134 optarg=`expr -- "$opt" : '-p\(.*\)'`
135 OPTION_PROJECT="$optarg"
136 ;;
137 --exec=*)
138 OPTION_EXEC="$optarg"
139 ;;
140 -x)
141 check_parameter $1 $2
142 OPTION_EXEC="$2"
143 shift
144 ;;
145 -x*)
146 optarg=`expr -- "$opt" : '-x\(.*\)'`
147 OPTION_EXEC="$optarg"
148 ;;
149 -e)
150 check_adb_flags
151 ADB_FLAGS=" -e"
152 ;;
153 -d)
154 check_adb_flags
155 ADB_FLAGS=" -d"
156 ;;
157 --adb=*) # specify ADB command
158 OPTION_ADB="$optarg"
159 ;;
160 --awk=*)
161 AWK_CMD="$optarg"
162 ;;
163 --project=*)
164 OPTION_PROJECT="$optarg"
165 ;;
166 --port=*)
167 DEBUG_PORT="$optarg"
168 ;;
169 --force)
170 OPTION_FORCE="yes"
171 ;;
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700172 --launch-list)
173 OPTION_LAUNCH_LIST="yes"
174 ;;
175 --launch=*)
176 OPTION_LAUNCH="$optarg"
177 ;;
178 --start)
179 OPTION_START=yes
180 ;;
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700181 -*) # unknown options
182 echo "ERROR: Unknown option '$opt', use --help for list of valid ones."
183 exit 1
184 ;;
185 *) # Simply record parameter
186 if [ -z "$PARAMETERS" ] ; then
187 PARAMETERS="$opt"
188 else
189 PARAMETERS="$PARAMETERS $opt"
190 fi
191 ;;
192 esac
193 shift
194done
195
196if [ "$OPTION_HELP" = "yes" ] ; then
197 echo "Usage: $PROGNAME [options]"
198 echo ""
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700199 echo "Setup a gdb debugging session for your Android NDK application."
200 echo "Read $$NDK/docs/NDK-GDB.TXT for complete usage instructions."
201 echo ""
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700202 echo "Valid options:"
203 echo ""
204 echo " --help|-h|-? Print this help"
205 echo " --verbose Enable verbose mode"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700206 echo " --force Kill existing debug session if it exists"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700207 echo " --start Launch application instead of attaching to existing one"
208 echo " --launch=<name> Same as --start, but specify activity name (see below)"
209 echo " --launch-list List all launchable activity names from manifest"
210 echo " --project=<path> Specify application project path"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700211 echo " -p <path> Same as --project=<path>"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700212 echo " --port=<port> Use tcp:localhost:<port> to communicate with gdbserver [$DEBUG_PORT]"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700213 echo " --exec=<file> Execute gdb initialization commands in <file> after connection"
214 echo " -x <file> Same as --exec=<file>"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700215 echo " --adb=<file> Use specific adb command [$ADB_CMD]"
216 echo " --awk=<file> Use specific awk command [$AWK_CMD]"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700217 echo " -e Connect to single emulator instance"
218 echo " -d Connect to single target device"
219 echo " -s <serial> Connect to specific emulator or device"
220 echo ""
221 exit 0
222fi
223
224log "Android NDK installation path: $ANDROID_NDK_ROOT"
225
226if [ -n "$OPTION_EXEC" ] ; then
227 if [ ! -f "$OPTION_EXEC" ]; then
228 echo "ERROR: Invalid initialization file: $OPTION_EXEC"
229 exit 1
230 fi
231fi
232
233# Check ADB tool version
234if [ -n "$OPTION_ADB" ] ; then
235 ADB_CMD="$OPTION_ADB"
236 log "Using specific adb command: $ADB_CMD"
237else
238 if [ -z "$ADB_CMD" ] ; then
239 echo "ERROR: The 'adb' tool is not in your path."
240 echo " You can change your PATH variable, or use"
241 echo " --adb=<executable> to point to a valid one."
242 exit 1
243 fi
244 log "Using default adb command: $ADB_CMD"
245fi
246
247ADB_VERSION=`$ADB_CMD version`
248if [ $? != 0 ] ; then
249 echo "ERROR: Could not run ADB with: $ADB_CMD"
250 exit 1
251fi
252log "ADB version found: $ADB_VERSION"
253
254ADB_CMD="${ADB_CMD}${ADB_FLAGS}"
255log "Using final ADB command: '$ADB_CMD'"
256
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700257adb_shell ()
258{
259 # Run an adb shell command and return its output.
260 #
261 # We need to filter weird control characters like \r that are
262 # included in the output.
263 #
264 $ADB_CMD shell $@ | sed -e 's![[:cntrl:]]!!g'
265}
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700266
267# Check the awk tool
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700268AWK_SCRIPTS=$ANDROID_NDK_ROOT/build/awk
269AWK_TEST=`$AWK_CMD -f $AWK_SCRIPTS/check-awk.awk`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700270if [ $? != 0 ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700271 echo "ERROR: Could not run '$AWK_CMD' command. Do you have it installed properly?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700272 exit 1
273fi
274if [ "$AWK_TEST" != "Pass" ] ; then
275 echo "ERROR: Your version of 'awk' is obsolete. Please use --awk=<file> to point to Nawk or Gawk!"
276 exit 1
277fi
278
279# Name of the manifest file
280MANIFEST=AndroidManifest.xml
281
282# Find the root of the application project.
283if [ -n "$OPTION_PROJECT" ] ; then
284 PROJECT=$OPTION_PROJECT
285 log "Using specified project path: $PROJECT"
286 if [ ! -d "$PROJECT" ] ; then
287 echo "ERROR: Your --project option does not point to a directory!"
288 exit 1
289 fi
290 if [ ! -f "$PROJECT/$MANIFEST" ] ; then
291 echo "ERROR: Your --project does not point to an Android project path!"
292 echo " It is missing a $MANIFEST file."
293 exit 1
294 fi
295else
296 # Assume we are in the project directory
297 if [ -f "$MANIFEST" ] ; then
298 PROJECT=.
299 else
300 PROJECT=
301 CURDIR=`pwd`
302 while [ "$CURDIR" != "/" ] ; do
303 if [ -f "$CURDIR/$MANIFEST" ] ; then
304 PROJECT="$CURDIR"
305 break
306 fi
307 CURDIR=`dirname $CURDIR`
308 done
309 if [ -z "$PROJECT" ] ; then
310 echo "ERROR: Launch this script from an application project directory, or use --project=<path>."
311 exit 1
312 fi
313 fi
314 log "Using auto-detected project path: $PROJECT"
315fi
316
317# Extract the package name from the manifest
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700318PACKAGE_NAME=`run_awk_manifest_script extract-package-name.awk`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700319log "Found package name: $PACKAGE_NAME"
320if [ $? != 0 -o "$PACKAGE_NAME" = "<none>" ] ; then
321 echo "ERROR: Could not extract package name from $PROJECT/$MANIFEST."
322 echo " Please check that the file is well-formed!"
323 exit 1
324fi
325
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700326# If --launch-list is used, list all launchable activities, and be done with it
327if [ "$OPTION_LAUNCH_LIST" = "yes" ] ; then
328 log "Extracting list of launchable activities from manifest:"
329 run_awk_manifest_script extract-launchable.awk
330 exit 0
331fi
332
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700333APP_ABIS=`get_build_var APP_ABI`
334log "ABIs targetted by application: $APP_ABIS"
335
336# Check the ADB command, and that we can connect to the device/emulator
337ADB_TEST=`$ADB_CMD shell ls`
338if [ $? != 0 ] ; then
339 echo "ERROR: Could not connect to device or emulator!"
340 echo " Please check that an emulator is running or a device is connected"
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700341 echo " through USB to this machine. You can use -e, -d and -s <serial>"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700342 echo " in case of multiple ones."
343 exit 1
344fi
345
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700346# Check that the device is running Froyo (API Level 8) or higher
347#
348API_LEVEL=`adb_shell getprop ro.build.version.sdk`
349if [ $? != 0 -o -z "$API_LEVEL" ] ; then
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200350 echo "ERROR: Could not find target device's supported API level!"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700351 echo "ndk-gdb will only work if your device is running Android 2.2 or higher."
352 exit 1
353fi
David 'Digit' Turner54be4862010-06-03 11:57:49 -0700354log "Device API Level: $API_LEVEL"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700355if [ "$API_LEVEL" -lt "8" ] ; then
356 echo "ERROR: ndk-gdb requires a target device running Android 2.2 (API level 8) or higher."
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200357 echo "The target device is running API level $API_LEVEL!"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700358 exit 1
359fi
360
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700361# Get the target device's supported ABI(s)
362# And check that they are supported by the application
363#
364COMPAT_ABI=none
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700365CPU_ABI=`adb_shell getprop ro.product.cpu.abi`
David 'Digit' Turner47eff9f2010-06-03 11:57:49 -0700366for ABI in $APP_ABIS; do
367 if [ "$ABI" = "$CPU_ABI" ] ; then
368 COMPAT_ABI=$CPU_ABI
369 break
370 fi
371done
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700372
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700373CPU_ABI2=`adb_shell getprop ro.product.cpu.abi2`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700374if [ -z "$CPU_ABI2" ] ; then
375 log "Device CPU ABI: $CPU_ABI"
376else
377 log "Device CPU ABIs: $CPU_ABI $CPU_ABI2"
David 'Digit' Turner47eff9f2010-06-03 11:57:49 -0700378 if [ "$COMPAT_ABI" = "none" ] ; then
379 for ABI in $APP_ABIS; do
380 if [ "$ABI" = "$CPU_ABI2" ] ; then
381 COMPAT_ABI=$CPU_ABI2
382 break
383 fi
384 done
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700385 fi
386fi
387if [ "$COMPAT_ABI" = none ] ; then
388 echo "ERROR: The device does not support the application's targetted CPU ABIs!"
389 if [ "$CPU_ABI2" = "$CPU_ABI" ] ; then
390 CPU_ABI2=
391 fi
392 echo " Device supports: $CPU_ABI $CPU_ABI2"
393 echo " Package supports: $APP_ABIS"
394 exit 1
395fi
396log "Compatible device ABI: $COMPAT_ABI"
397
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200398# Check that the application is debuggable, or nothing will work
399DEBUGGABLE=`run_awk_manifest_script extract-debuggable.awk`
400log "Found debuggable flag: $DEBUGGABLE"
401if [ $? != 0 -o "$DEBUGGABLE" != "true" ] ; then
402 # If gdbserver exists, then we built with 'ndk-build NDK_DEBUG=1' and it's
403 # ok to not have android:debuggable set to true in the original manifest.
404 # However, if this is not the case, then complain!!
405 if [ -f $PROJECT/libs/$COMPAT_ABI/gdbserver ] ; then
406 log "Found gdbserver under libs/$COMPAT_ABI, assuming app was built with NDK_DEBUG=1"
407 else
408 echo "ERROR: Package $PACKAGE_NAME is not debuggable ! You can fix that in two ways:"
409 echo ""
410 echo " - Rebuilt with the NDK_DEBUG=1 option when calling 'ndk-build'."
411 echo ""
412 echo " - Modify your manifest to set android:debuggable attribute to \"true\","
413 echo " then rebuild normally."
414 echo ""
415 echo "After one of these, re-install to the device!"
416 exit 1
417 fi
418else
419 # DEBUGGABLE is true in the manifest. Let's check that the user didn't change the
420 # debuggable flag in the manifest without calling ndk-build afterwards.
421 if [ ! -f $PROJECT/libs/$COMPAT_ABI/gdbserver ] ; then
422 echo "ERROR: Could not find gdbserver binary under $PROJECT/libs/$COMPAT_ABI"
423 echo " This usually means you modified your AndroidManifest.xml to set"
424 echo " the android:debuggable flag to 'true' but did not rebuild the"
425 echo " native binaries. Please call 'ndk-build' to do so,"
426 echo " *then* re-install to the device!"
427 exit 1
428 fi
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700429fi
430
431# Let's check that 'gdbserver' is properly installed on the device too. If this
432# is not the case, the user didn't install the proper package after rebuilding.
433#
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700434DEVICE_GDBSERVER=`adb_shell ls /data/data/$PACKAGE_NAME/lib/gdbserver`
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700435log "Found device gdbserver: $DEVICE_GDBSERVER"
436if pattern_match "No such file or directory" "$DEVICE_GDBSERVER" ] ; then
437 echo "ERROR: Non-debuggable application installed on the target device."
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200438 echo " Please re-install the debuggable version!"
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700439 exit 1
440fi
441
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700442# Get information from the build system
443GDBSETUP_INIT=`get_build_var_for_abi NDK_APP_GDBSETUP $COMPAT_ABI`
444log "Using gdb setup init: $GDBSETUP_INIT"
445
446TOOLCHAIN_PREFIX=`get_build_var_for_abi TOOLCHAIN_PREFIX $COMPAT_ABI`
447log "Using toolchain prefix: $TOOLCHAIN_PREFIX"
448
449APP_OUT=`get_build_var_for_abi TARGET_OUT $COMPAT_ABI`
450log "Using app out directory: $APP_OUT"
451
452# Find the <dataDir> of the package on the device
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700453DATA_DIR=`adb_shell run-as $PACKAGE_NAME /system/bin/sh -c pwd`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700454log "Found data directory: '$DATA_DIR'"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700455if [ $? != 0 -o -z "$DATA_DIR" ] ; then
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700456 echo "ERROR: Could not extract package's data directory. Are you sure that"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700457 echo " your installed application is debuggable?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700458 exit 1
459fi
460
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700461# Launch the activity if needed
462if [ -n "$OPTION_START" ] ; then
463 # If --launch is used, ignore --start, otherwise extract the first
464 # launchable activity name from the manifest and use it as if --launch=<name>
465 # was used instead.
466 #
467 if [ -z "$OPTION_LAUNCH" ] ; then
468 OPTION_LAUNCH=`run_awk_manifest_script extract-launchable.awk | sed 2q`
469 if [ $? != 0 ] ; then
470 echo "ERROR: Could not extract name of launchable activity from manifest!"
471 echo " Try to use --launch=<name> directly instead as a work-around."
472 exit 1
473 fi
474 log "Found first launchable activity: $OPTION_LAUNCH"
475 if [ -z "$OPTION_LAUNCH" ] ; then
476 echo "ERROR: It seems that your Application does not have any launchable activity!"
477 echo " Please fix your manifest file and rebuild/re-install your application."
478 exit 1
479 fi
480 fi
481fi
482
483if [ -n "$OPTION_LAUNCH" ] ; then
484 log "Launching activity: $PACKAGE_NAME/$OPTION_LAUNCH"
485 run $ADB_CMD shell am start -n $PACKAGE_NAME/$OPTION_LAUNCH
486 if [ $? != 0 ] ; then
487 echo "ERROR: Could not launch specified activity: $OPTION_LAUNCH"
488 echo " Use --launch-list to dump a list of valid values."
489 exit 1
490 fi
491 # Sleep a bit, it sometimes take one second to start properly
492 # Note that we use the 'sleep' command on the device here.
493 run $ADB_CMD shell sleep 1
494fi
495
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700496# Find the PID of the application being run
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700497PID=`$ADB_CMD shell ps | $AWK_CMD -f $AWK_SCRIPTS/extract-pid.awk -v PACKAGE=$PACKAGE_NAME`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700498log "Found running PID: $PID"
499if [ $? != 0 -o "$PID" = "0" ] ; then
500 echo "ERROR: Could not extract PID of application on device/emulator."
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700501 if [ -n "$OPTION_LAUNCH" ] ; then
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700502 echo " Weird, this probably means one of these:"
503 echo ""
504 echo " - The installed package does not match your current manifest."
505 echo " - The application process was terminated."
506 echo ""
507 echo " Try using the --verbose option and look at its output for details."
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700508 else
509 echo " Are you sure the application is already started?"
510 echo " Consider using --start or --launch=<name> if not."
511 fi
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700512 exit 1
513fi
514
515# Check that there is no other instance of gdbserver running
516GDBSERVER_PS=`$ADB_CMD shell ps | grep lib/gdbserver`
517if [ -n "$GDBSERVER_PS" ] ; then
518 if [ "$OPTION_FORCE" = "no" ] ; then
519 echo "ERROR: Another debug session running, Use --force to kill it."
520 exit 1
521 fi
522 log "Killing existing debugging session"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700523 GDBSERVER_PID=`echo $GDBSERVER_PS | $AWK_CMD -f $AWK_SCRIPTS/extract-pid.awk -v PACKAGE=lib/gdbserver`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700524 if [ $GDBSERVER_PID != 0 ] ; then
525 run $ADB_CMD shell kill -9 $GDBSERVER_PID
526 fi
527fi
528
529# Launch gdbserver now
530DEBUG_SOCKET=debug-socket
531run $ADB_CMD shell run-as $PACKAGE_NAME lib/gdbserver +$DEBUG_SOCKET --attach $PID &
532if [ $? != 0 ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700533 echo "ERROR: Could not launch gdbserver on the device?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700534 exit 1
535fi
536log "Launched gdbserver succesfully."
537
538# Setup network redirection
539log "Setup network redirection"
540run $ADB_CMD forward tcp:$DEBUG_PORT localfilesystem:$DATA_DIR/$DEBUG_SOCKET
541if [ $? != 0 ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700542 echo "ERROR: Could not setup network redirection to gdbserver?"
543 echo " Maybe using --port=<port> to use a different TCP port might help?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700544 exit 1
545fi
546
547# Get the app_server binary from the device
548APP_PROCESS=$APP_OUT/app_process
David 'Digit' Turner47eff9f2010-06-03 11:57:49 -0700549run $ADB_CMD pull /system/bin/app_process `native_path $APP_PROCESS`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700550log "Pulled $APP_BINARY from device/emulator."
551
552# Now launch the appropriate gdb client with the right init commands
553#
554GDBCLIENT=${TOOLCHAIN_PREFIX}gdb
555GDBSETUP=$APP_OUT/gdb.setup
556cp -f $GDBSETUP_INIT $GDBSETUP
557echo "target remote :$DEBUG_PORT" >> $GDBSETUP
558if [ -n "$OPTION_EXEC" ] ; then
559 cat $OPTION_EXEC >> $GDBSETUP
560fi
561$GDBCLIENT -x $GDBSETUP -e $APP_PROCESS