Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(202)

Side by Side Diff: build/android/adb_gdb

Issue 850793004: Update from https://crrev.com/311346 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « build/android/PRESUBMIT.py ('k') | build/android/pylib/base/base_test_result.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/bin/bash 1 #!/bin/bash
2 # 2 #
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 3 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be 4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file. 5 # found in the LICENSE file.
6 # 6 #
7 7
8 # A generic script used to attach to a running Chromium process and 8 # A generic script used to attach to a running Chromium process and
9 # debug it. Most users should not use this directly, but one of the 9 # debug it. Most users should not use this directly, but one of the
10 # wrapper scripts like adb_gdb_content_shell 10 # wrapper scripts like adb_gdb_content_shell
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 \$CHROMIUM_SRC/<out>/Debug/lib/ (used by Ninja builds) 255 \$CHROMIUM_SRC/<out>/Debug/lib/ (used by Ninja builds)
256 \$CHROMIUM_SRC/<out>/Release/lib.target/ (used by Make builds) 256 \$CHROMIUM_SRC/<out>/Release/lib.target/ (used by Make builds)
257 \$CHROMIUM_SRC/<out>/Debug/lib.target/ (used by Make builds) 257 \$CHROMIUM_SRC/<out>/Debug/lib.target/ (used by Make builds)
258 258
259 Where <out> is 'out' by default, unless the --out=<name> option is used or 259 Where <out> is 'out' by default, unless the --out=<name> option is used or
260 the CHROMIUM_OUT_DIR environment variable is defined. 260 the CHROMIUM_OUT_DIR environment variable is defined.
261 261
262 You can restrict this search by using --release or --debug to specify the 262 You can restrict this search by using --release or --debug to specify the
263 build type, or simply use --symbol-dir=<path> to specify the file manually. 263 build type, or simply use --symbol-dir=<path> to specify the file manually.
264 264
265 The script tries to extract the target architecture from your GYP_DEFINES, 265 The script tries to extract the target architecture from your target device,
266 but if this fails, will default to 'arm'. Use --target-arch=<name> to force 266 but if this fails, will default to 'arm'. Use --target-arch=<name> to force
267 its value. 267 its value.
268 268
269 Otherwise, the script will complain, but you can use the --gdbserver, 269 Otherwise, the script will complain, but you can use the --gdbserver,
270 --gdb and --symbol-lib options to specify everything manually. 270 --gdb and --symbol-lib options to specify everything manually.
271 271
272 An alternative to --gdb=<file> is to use --toollchain=<path> to specify 272 An alternative to --gdb=<file> is to use --toollchain=<path> to specify
273 the path to the host target-specific cross-toolchain. 273 the path to the host target-specific cross-toolchain.
274 274
275 You will also need the 'adb' tool in your path. Otherwise, use the --adb 275 You will also need the 'adb' tool in your path. Otherwise, use the --adb
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 if [ ! -f "$NDK_DIR/ndk-build" ]; then 347 if [ ! -f "$NDK_DIR/ndk-build" ]; then
348 panic "Not a valid NDK directory: $NDK_DIR" 348 panic "Not a valid NDK directory: $NDK_DIR"
349 fi 349 fi
350 ANDROID_NDK_ROOT=$NDK_DIR 350 ANDROID_NDK_ROOT=$NDK_DIR
351 fi 351 fi
352 352
353 if [ "$GDBINIT" -a ! -f "$GDBINIT" ]; then 353 if [ "$GDBINIT" -a ! -f "$GDBINIT" ]; then
354 panic "Unknown --script file: $GDBINIT" 354 panic "Unknown --script file: $GDBINIT"
355 fi 355 fi
356 356
357 # Find the target architecture from our $GYP_DEFINES 357 # Check that ADB is in our path
358 if [ -z "$ADB" ]; then
359 ADB=$(which adb 2>/dev/null)
360 if [ -z "$ADB" ]; then
361 panic "Can't find 'adb' tool in your path. Install it or use \
362 --adb=<file>"
363 fi
364 log "Auto-config: --adb=$ADB"
365 fi
366
367 # Check that it works minimally
368 ADB_VERSION=$($ADB version 2>/dev/null)
369 echo "$ADB_VERSION" | fgrep -q -e "Android Debug Bridge"
370 if [ $? != 0 ]; then
371 panic "Your 'adb' tool seems invalid, use --adb=<file> to specify a \
372 different one: $ADB"
373 fi
374
375 # If there are more than one device connected, and ANDROID_SERIAL is not
376 # defined, print an error message.
377 NUM_DEVICES_PLUS2=$($ADB devices 2>/dev/null | wc -l)
378 if [ "$NUM_DEVICES_PLUS2" -lt 3 -a -z "$ANDROID_SERIAL" ]; then
379 echo "ERROR: There is more than one Android device connected to ADB."
380 echo "Please define ANDROID_SERIAL to specify which one to use."
381 exit 1
382 fi
383
384 # Run a command through adb shell, strip the extra \r from the output
385 # and return the correct status code to detect failures. This assumes
386 # that the adb shell command prints a final \n to stdout.
387 # $1+: command to run
388 # Out: command's stdout
389 # Return: command's status
390 # Note: the command's stderr is lost
391 adb_shell () {
392 local TMPOUT="$(mktemp)"
393 local LASTLINE RET
394 local ADB=${ADB:-adb}
395
396 # The weird sed rule is to strip the final \r on each output line
397 # Since 'adb shell' never returns the command's proper exit/status code,
398 # we force it to print it as '%%<status>' in the temporary output file,
399 # which we will later strip from it.
400 $ADB shell $@ ";" echo "%%\$?" 2>/dev/null | \
401 sed -e 's![[:cntrl:]]!!g' > $TMPOUT
402 # Get last line in log, which contains the exit code from the command
403 LASTLINE=$(sed -e '$!d' $TMPOUT)
404 # Extract the status code from the end of the line, which must
405 # be '%%<code>'.
406 RET=$(echo "$LASTLINE" | \
407 awk '{ if (match($0, "%%[0-9]+$")) { print substr($0,RSTART+2); } }')
408 # Remove the status code from the last line. Note that this may result
409 # in an empty line.
410 LASTLINE=$(echo "$LASTLINE" | \
411 awk '{ if (match($0, "%%[0-9]+$")) { print substr($0,1,RSTART-1); } }')
412 # The output itself: all lines except the status code.
413 sed -e '$d' $TMPOUT && printf "%s" "$LASTLINE"
414 # Remove temp file.
415 rm -f $TMPOUT
416 # Exit with the appropriate status.
417 return $RET
418 }
419
420 # Find the target architecture from the target device.
358 # This returns an NDK-compatible architecture name. 421 # This returns an NDK-compatible architecture name.
359 # out: NDK Architecture name, or empty string. 422 # out: NDK Architecture name, or empty string.
360 get_gyp_target_arch () { 423 get_gyp_target_arch () {
361 local ARCH=$(echo $GYP_DEFINES | tr ' ' '\n' | grep '^target_arch=' |\ 424 local ARCH=$(adb_shell getprop ro.product.cpu.abi)
362 cut -d= -f2)
363 case $ARCH in 425 case $ARCH in
364 ia32|i?86|x86) echo "x86";; 426 mips|x86|x86_64) echo "$ARCH";;
365 mips|arm|arm64|x86_64) echo "$ARCH";; 427 arm64*) echo "arm64";;
428 arm*) echo "arm";;
366 *) echo ""; 429 *) echo "";
367 esac 430 esac
368 } 431 }
369 432
370 if [ -z "$TARGET_ARCH" ]; then 433 if [ -z "$TARGET_ARCH" ]; then
371 TARGET_ARCH=$(get_gyp_target_arch) 434 TARGET_ARCH=$(get_gyp_target_arch)
372 if [ -z "$TARGET_ARCH" ]; then 435 if [ -z "$TARGET_ARCH" ]; then
373 TARGET_ARCH=arm 436 TARGET_ARCH=arm
374 fi 437 fi
375 else 438 else
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 # 635 #
573 if [ -z "$GDBSERVER" ]; then 636 if [ -z "$GDBSERVER" ]; then
574 GDBSERVER=$(get_ndk_gdbserver "$ANDROID_NDK_ROOT" "$TARGET_ARCH") 637 GDBSERVER=$(get_ndk_gdbserver "$ANDROID_NDK_ROOT" "$TARGET_ARCH")
575 if [ -z "$GDBSERVER" ]; then 638 if [ -z "$GDBSERVER" ]; then
576 panic "Can't find NDK gdbserver binary. use --gdbserver to specify \ 639 panic "Can't find NDK gdbserver binary. use --gdbserver to specify \
577 valid one!" 640 valid one!"
578 fi 641 fi
579 log "Auto-config: --gdbserver=$GDBSERVER" 642 log "Auto-config: --gdbserver=$GDBSERVER"
580 fi 643 fi
581 644
582
583
584 # Check that ADB is in our path
585 if [ -z "$ADB" ]; then
586 ADB=$(which adb 2>/dev/null)
587 if [ -z "$ADB" ]; then
588 panic "Can't find 'adb' tool in your path. Install it or use \
589 --adb=<file>"
590 fi
591 log "Auto-config: --adb=$ADB"
592 fi
593
594 # Check that it works minimally
595 ADB_VERSION=$($ADB version 2>/dev/null)
596 echo "$ADB_VERSION" | fgrep -q -e "Android Debug Bridge"
597 if [ $? != 0 ]; then
598 panic "Your 'adb' tool seems invalid, use --adb=<file> to specify a \
599 different one: $ADB"
600 fi
601
602 # If there are more than one device connected, and ANDROID_SERIAL is not
603 # defined, print an error message.
604 NUM_DEVICES_PLUS2=$($ADB devices 2>/dev/null | wc -l)
605 if [ "$NUM_DEVICES_PLUS2" -lt 3 -a -z "$ANDROID_SERIAL" ]; then
606 echo "ERROR: There is more than one Android device connected to ADB."
607 echo "Please define ANDROID_SERIAL to specify which one to use."
608 exit 1
609 fi
610
611 # A unique ID for this script's session. This needs to be the same in all 645 # A unique ID for this script's session. This needs to be the same in all
612 # sub-shell commands we're going to launch, so take the PID of the launcher 646 # sub-shell commands we're going to launch, so take the PID of the launcher
613 # process. 647 # process.
614 TMP_ID=$$ 648 TMP_ID=$$
615 649
616 # Temporary directory, will get cleaned up on exit. 650 # Temporary directory, will get cleaned up on exit.
617 TMPDIR=/tmp/$USER-adb-gdb-tmp-$TMP_ID 651 TMPDIR=/tmp/$USER-adb-gdb-tmp-$TMP_ID
618 mkdir -p "$TMPDIR" && rm -rf "$TMPDIR"/* 652 mkdir -p "$TMPDIR" && rm -rf "$TMPDIR"/*
619 653
620 GDBSERVER_PIDFILE="$TMPDIR"/gdbserver-$TMP_ID.pid 654 GDBSERVER_PIDFILE="$TMPDIR"/gdbserver-$TMP_ID.pid
621 655
622 # Run a command through adb shell, strip the extra \r from the output
623 # and return the correct status code to detect failures. This assumes
624 # that the adb shell command prints a final \n to stdout.
625 # $1+: command to run
626 # Out: command's stdout
627 # Return: command's status
628 # Note: the command's stderr is lost
629 adb_shell () {
630 local TMPOUT="$(mktemp)"
631 local LASTLINE RET
632 local ADB=${ADB:-adb}
633
634 # The weird sed rule is to strip the final \r on each output line
635 # Since 'adb shell' never returns the command's proper exit/status code,
636 # we force it to print it as '%%<status>' in the temporary output file,
637 # which we will later strip from it.
638 $ADB shell $@ ";" echo "%%\$?" 2>/dev/null | \
639 sed -e 's![[:cntrl:]]!!g' > $TMPOUT
640 # Get last line in log, which contains the exit code from the command
641 LASTLINE=$(sed -e '$!d' $TMPOUT)
642 # Extract the status code from the end of the line, which must
643 # be '%%<code>'.
644 RET=$(echo "$LASTLINE" | \
645 awk '{ if (match($0, "%%[0-9]+$")) { print substr($0,RSTART+2); } }')
646 # Remove the status code from the last line. Note that this may result
647 # in an empty line.
648 LASTLINE=$(echo "$LASTLINE" | \
649 awk '{ if (match($0, "%%[0-9]+$")) { print substr($0,1,RSTART-1); } }')
650 # The output itself: all lines except the status code.
651 sed -e '$d' $TMPOUT && printf "%s" "$LASTLINE"
652 # Remove temp file.
653 rm -f $TMPOUT
654 # Exit with the appropriate status.
655 return $RET
656 }
657
658 # If --force is specified, try to kill any gdbserver process started by the 656 # If --force is specified, try to kill any gdbserver process started by the
659 # same user on the device. Normally, these are killed automatically by the 657 # same user on the device. Normally, these are killed automatically by the
660 # script on exit, but there are a few corner cases where this would still 658 # script on exit, but there are a few corner cases where this would still
661 # be needed. 659 # be needed.
662 if [ "$FORCE" ]; then 660 if [ "$FORCE" ]; then
663 GDBSERVER_PIDS=$(adb_shell ps | awk '$9 ~ /gdbserver/ { print $2; }') 661 GDBSERVER_PIDS=$(adb_shell ps | awk '$9 ~ /gdbserver/ { print $2; }')
664 for GDB_PID in $GDBSERVER_PIDS; do 662 for GDB_PID in $GDBSERVER_PIDS; do
665 log "Killing previous gdbserver (PID=$GDB_PID)" 663 log "Killing previous gdbserver (PID=$GDB_PID)"
666 adb_shell kill -9 $GDB_PID 664 adb_shell kill -9 $GDB_PID
667 done 665 done
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
1005 1003
1006 if [ "$VERBOSE" -gt 0 ]; then 1004 if [ "$VERBOSE" -gt 0 ]; then
1007 echo "### START $COMMANDS" 1005 echo "### START $COMMANDS"
1008 cat $COMMANDS 1006 cat $COMMANDS
1009 echo "### END $COMMANDS" 1007 echo "### END $COMMANDS"
1010 fi 1008 fi
1011 1009
1012 log "Launching gdb client: $GDB $GDB_ARGS -x $COMMANDS" 1010 log "Launching gdb client: $GDB $GDB_ARGS -x $COMMANDS"
1013 $GDB $GDB_ARGS -x $COMMANDS && 1011 $GDB $GDB_ARGS -x $COMMANDS &&
1014 rm -f "$GDBSERVER_PIDFILE" 1012 rm -f "$GDBSERVER_PIDFILE"
OLDNEW
« no previous file with comments | « build/android/PRESUBMIT.py ('k') | build/android/pylib/base/base_test_result.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698