aboutsummaryrefslogtreecommitdiff
path: root/ndk-gdb.py
diff options
context:
space:
mode:
authorJosh Gao <jmgao@google.com>2015-09-08 15:21:52 -0700
committerJosh Gao <jmgao@google.com>2015-09-16 11:20:10 -0700
commit9fcbff71c5b2c7fe03109aea5428d2caf35eb0be (patch)
treeb08df916333bceec710253875b297f6c1dcfe58c /ndk-gdb.py
parentac4ab93dd7c69498e82470566c14a4b7816c234b (diff)
downloadndk-9fcbff71c5b2c7fe03109aea5428d2caf35eb0be.tar.gz
[ndk-gdb.py] Reuse the result of adb shell ps.
Remove another adb shell roundtrip. Bug: http://b/23793246 Change-Id: Idf1dccddb8c5ed8cb1327fadfffbfbf0822232e2
Diffstat (limited to 'ndk-gdb.py')
-rwxr-xr-xndk-gdb.py69
1 files changed, 37 insertions, 32 deletions
diff --git a/ndk-gdb.py b/ndk-gdb.py
index 95ffaf658..877252878 100755
--- a/ndk-gdb.py
+++ b/ndk-gdb.py
@@ -440,21 +440,17 @@ def adb_var_shell(args, log_command=False):
def adb_var_shell2(args, log_command=False):
return _adb_var_shell(args, redirect_stderr=True, log_command=log_command)
-# Return the PID of a given package or program, or 0 if it doesn't run
-# $1: Package name ("com.example.hellojni") or program name ("/lib/gdbserver")
-# Out: PID number, or 0 if not running
-#
-def get_pid_of(package_name):
- '''
- Some custom ROMs use busybox instead of toolbox for ps.
- Without -w, busybox truncates the output, and very long
- package names like com.exampleisverylongtoolongbyfar.plasma
- exceed the limit.
- '''
+def get_processes():
+ '''Return a dict from process name to list of running PIDs on the device.'''
+
+ # Some custom ROMs use busybox instead of toolbox for ps. Without -w,
+ # busybox truncates the output, and very long package names like
+ # com.exampleisverylongtoolongbyfar.plasma exceed the limit.
+ #
+ # Perform the check for this on the device to avoid an adb roundtrip
+ # Some devices might not have readlink or which, so we need to handle
+ # this as well.
- # Perform the check on the device to avoid an adb roundtrip
- # Some devices might not have readlink or which, so we need to check for
- # them.
ps_script = '''
if [ ! -x /system/bin/readlink -o ! -x /system/bin/which ]; then
ps;
@@ -467,6 +463,10 @@ def get_pid_of(package_name):
ps_script = " ".join([line.strip() for line in ps_script.splitlines()])
retcode, output = adb_cmd(False, ['shell', ps_script])
+ if retcode != 0:
+ raise RuntimeException("Failed to get ps output")
+
+ processes = dict()
output = output.replace('\r', '').splitlines()
columns = output.pop(0).split()
try:
@@ -475,9 +475,15 @@ def get_pid_of(package_name):
PID_column = 1
while output:
columns = output.pop().split()
- if columns.pop() == package_name:
- return 0,int(columns[PID_column])
- return 1,0
+ process_name = columns[-1]
+ pid = int(columns[PID_column])
+ if process_name in processes:
+ processes[process_name].append(pid)
+ else:
+ processes[process_name] = [pid]
+
+ return processes
+
def extract_package_name(xmlfile):
'''
@@ -727,10 +733,10 @@ The target device is running API level %d!''' % (API_LEVEL))
#
adb_cmd(True, ['shell', 'sleep', '%f' % (DELAY)], log_command=True)
+ processes = get_processes()
+
# Find the PID of the application being run
- retcode,PID = get_pid_of(PACKAGE_NAME)
- log('Found running PID: %d' % (PID))
- if retcode or PID == 0:
+ if PACKAGE_NAME not in processes:
if OPTION_LAUNCH:
error('''Could not extract PID of application on device/emulator.
Weird, this probably means one of these:
@@ -744,28 +750,27 @@ The target device is running API level %d!''' % (API_LEVEL))
Are you sure the application is already started?
Consider using --start or --launch=<name> if not.''')
+ if len(processes[PACKAGE_NAME]) > 1:
+ error('Multiple processes found for %s: %s' % (PACKAGE_NAME, processes[PACKAGE_NAME]))
+
+ pid = processes[PACKAGE_NAME][0]
+ log('Found running PID: %d' % (pid))
+
# Check that there is no other instance of gdbserver running
- retcode,GDBSERVER_PID = get_pid_of('lib/gdbserver')
- if not retcode and not GDBSERVER_PID == 0:
+ if 'lib/gdbserver' in processes:
if not OPTION_FORCE:
error('Another debug session running, Use --force to kill it.')
log('Killing existing debugging session')
- adb_cmd(False, ['shell', 'kill -9 %s' % (GDBSERVER_PID)])
+ for gdbserver_pid in processes['lib/gdbserver']:
+ adb_cmd(False, ['shell', 'kill -9 %s' % (gdbserver_pid)])
# Launch gdbserver now
DEBUG_SOCKET = 'debug-socket'
adb_cmd(False,
- ['shell', 'run-as', PACKAGE_NAME, device_gdbserver, '+%s' % (DEBUG_SOCKET), '--attach', str(PID)],
+ ['shell', 'run-as', PACKAGE_NAME, device_gdbserver, '+%s' % (DEBUG_SOCKET), '--attach', str(pid)],
log_command=True, adb_trace=True, background=True)
log('Launched gdbserver succesfully.')
-# Make sure gdbserver was launched - debug check.
-# adb_var_shell(['sleep', '0.1'], log_command=False)
-# retcode,GDBSERVER_PID = get_pid_of('lib/gdbserver')
-# if retcode or GDBSERVER_PID == 0:
-# error('Could not launch gdbserver on the device?')
-# log('Launched gdbserver succesfully (PID=%s)' % (GDBSERVER_PID))
-
# Setup network redirection
log('Setup network redirection')
retcode,_ = adb_cmd(False,
@@ -808,7 +813,7 @@ The target device is running API level %d!''' % (API_LEVEL))
if (OPTION_START != None or OPTION_LAUNCH != None) and len(OPTION_WAIT):
log('Set up JDB connection, using jdb command: %s' % JDB_CMD)
retcode,_ = adb_cmd(False,
- ['forward', 'tcp:%d' % (JDB_PORT), 'jdwp:%d' % (PID)],
+ ['forward', 'tcp:%d' % (JDB_PORT), 'jdwp:%d' % (pid)],
log_command=True)
time.sleep(1.0)
if retcode: