Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import argparse | 6 import argparse |
| 7 import os.path | 7 import os.path |
| 8 import requests | 8 import requests |
| 9 import subprocess | 9 import subprocess |
| 10 import sys | 10 import sys |
| 11 import tempfile | |
|
ppi
2015/06/24 15:14:08
add a blank line
etiennej
2015/06/29 15:41:43
Done.
| |
| 12 from android_gdb.install_remote_file_reader import install_binary | |
| 11 | 13 |
| 12 _MOJO_DEBUGGER_PORT = 7777 | 14 _MOJO_DEBUGGER_PORT = 7777 |
| 13 | 15 |
| 14 | 16 |
| 17 def _GetDirAbove(dirname): | |
| 18 """Returns the directory "above" this file containing |dirname|.""" | |
| 19 path = os.path.abspath(__file__) | |
| 20 while True: | |
| 21 path, _ = os.path.split(path) | |
| 22 assert path | |
| 23 if dirname in os.listdir(path): | |
| 24 return path | |
| 25 | |
| 26 | |
| 15 def _send_request(request, payload=None): | 27 def _send_request(request, payload=None): |
| 16 """Sends a request to mojo:debugger.""" | 28 """Sends a request to mojo:debugger.""" |
| 17 try: | 29 try: |
| 18 url = 'http://localhost:%s/%s' % (_MOJO_DEBUGGER_PORT, request) | 30 url = 'http://localhost:%s/%s' % (_MOJO_DEBUGGER_PORT, request) |
| 19 if payload: | 31 if payload: |
| 20 return requests.post(url, payload) | 32 return requests.post(url, payload) |
| 21 else: | 33 else: |
| 22 return requests.get(url) | 34 return requests.get(url) |
| 23 except requests.exceptions.ConnectionError: | 35 except requests.exceptions.ConnectionError: |
| 24 print 'Failed to connect to mojo:debugger, make sure the shell is running.' | 36 print 'Failed to connect to mojo:debugger, make sure the shell is running.' |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 stack.wait() | 137 stack.wait() |
| 126 | 138 |
| 127 if logcat.returncode: | 139 if logcat.returncode: |
| 128 print 'adb logcat failed, make sure the device is connected and available' | 140 print 'adb logcat failed, make sure the device is connected and available' |
| 129 return logcat.returncode | 141 return logcat.returncode |
| 130 if stack.returncode: | 142 if stack.returncode: |
| 131 return stack.returncode | 143 return stack.returncode |
| 132 return 0 | 144 return 0 |
| 133 | 145 |
| 134 | 146 |
| 147 def _device_gdb(args): | |
| 148 """Run GDB on an instance of Mojo Shell on an android device.""" | |
| 149 if args.ndk_dir: | |
| 150 ndk_dir = args.ndk_dir | |
| 151 else: | |
| 152 ndk_dir = os.path.join(_GetDirAbove('third_party'), 'third_party', | |
|
ppi
2015/06/24 15:14:08
We already have similar logic in devtools, in `sta
etiennej
2015/06/29 15:41:42
Added a TODO.
| |
| 153 'android_tools', 'ndk') | |
| 154 install_args = {} | |
| 155 if args.gsutil_dir: | |
| 156 install_args['gsutil'] = os.path.join(args.gsutil_dir, 'gsutil') | |
| 157 else: | |
| 158 install_args['gsutil'] = os.path.join( | |
| 159 _GetDirAbove('depot_tools'), 'depot_tools', 'third_party', 'gsutil', | |
| 160 'gsutil') | |
| 161 if args.adb_path: | |
| 162 install_args['adb'] = args.adb_path | |
| 163 install_binary(**install_args) | |
| 164 | |
| 165 gdb_path = os.path.join( | |
| 166 ndk_dir, | |
| 167 'toolchains', | |
| 168 # TODO(etiennej): Always select the most recent toolchain? | |
| 169 'arm-linux-androideabi-4.9', | |
| 170 'prebuilt', | |
| 171 # TODO(etiennej): DEPS mac NDK and use it on macs. | |
| 172 'linux-x86_64', | |
| 173 'bin', | |
| 174 'arm-linux-androideabi-gdb') | |
| 175 python_gdb_script_path = os.path.join(os.path.dirname(__file__), | |
| 176 'android_gdb', 'session.py') | |
| 177 # We need to pass some commands to GDB at startup | |
|
ppi
2015/06/24 15:14:08
End with a period.
etiennej
2015/06/29 15:41:43
Done.
| |
| 178 gdb_setup_commands = tempfile.NamedTemporaryFile() | |
| 179 gdb_setup_commands.write('source ' + python_gdb_script_path + '\n') | |
| 180 debug_session_arguments = {} | |
| 181 if args.build_dir: | |
| 182 debug_session_arguments["build_directory"] = args.build_dir | |
| 183 if args.package_name: | |
| 184 debug_session_arguments["package_name"] = args.package_name | |
| 185 if args.third_party_dir: | |
| 186 debug_session_arguments["third_party_dir"] = args.third_party_dir | |
| 187 debug_session_arguments = ', '.join([k + '=' + string_escape(v) for | |
|
ppi
2015/06/24 15:14:08
Where string_escape is defined or imported?
etiennej
2015/06/29 15:41:43
Done.
| |
| 188 k, v in debug_session_arguments.items()]) | |
| 189 gdb_setup_commands.write('py d = DebugSession('+debug_session_arguments+')\n') | |
|
ppi
2015/06/24 15:14:08
Add spaced around "+"es.
etiennej
2015/06/29 15:41:43
Done.
| |
| 190 gdb_setup_commands.write('py d.start_debug()\n') | |
| 191 gdb_setup_commands.flush() | |
| 192 gdb_proc = subprocess.Popen([gdb_path, '-x', gdb_setup_commands.name], | |
| 193 stdin=sys.stdin, | |
| 194 stdout=sys.stdout, | |
| 195 stderr=sys.stderr) | |
| 196 gdb_proc.wait() | |
| 197 | |
| 198 | |
| 135 def _add_device_command(subparsers): | 199 def _add_device_command(subparsers): |
| 136 """Sets up the parser for the 'device' command.""" | 200 """Sets up the parser for the 'device' command.""" |
| 137 device_parser = subparsers.add_parser('device', | 201 device_parser = subparsers.add_parser('device', |
| 138 help='interact with the Android device (requires adb in PATH or passing ' | 202 help='interact with the Android device (requires adb in PATH or passing ' |
| 139 '--adb-path)') | 203 '--adb-path)') |
| 140 device_parser.add_argument('--adb-path', type=str, | 204 device_parser.add_argument('--adb-path', type=str, |
| 141 help='path to the adb tool from the Android SDK') | 205 help='path to the adb tool from the Android SDK (optional)') |
| 206 device_parser.add_argument('--ndk-dir', type=str, | |
| 207 help='path to the directory containing the Android NDK') | |
| 208 device_parser.add_argument('--build-dir', type=str, | |
| 209 help='path to the build directory') | |
| 142 device_subparser = device_parser.add_subparsers( | 210 device_subparser = device_parser.add_subparsers( |
| 143 help='the command to run') | 211 help='the command to run') |
| 144 | 212 |
| 145 device_stack_parser = device_subparser.add_parser('stack', | 213 device_stack_parser = device_subparser.add_parser('stack', |
| 146 help='symbolize the crash stacktraces from the device log') | 214 help='symbolize the crash stacktraces from the device log') |
| 147 device_stack_parser.add_argument('--build-dir', type=str, | |
| 148 help='path to the build directory') | |
| 149 device_stack_parser.add_argument('--ndk-dir', type=str, | |
| 150 help='path to the directory containing the Android NDK') | |
| 151 device_stack_parser.set_defaults(func=_device_stack) | 215 device_stack_parser.set_defaults(func=_device_stack) |
| 152 | 216 |
| 217 device_gdb_parser = device_subparser.add_parser('gdb', | |
| 218 help='debug a shell instance currently running on a device') | |
|
ppi
2015/06/24 15:14:08
"debug" doesn't say much. Please clarify that we a
etiennej
2015/06/29 15:41:43
Done.
| |
| 219 device_gdb_parser.add_argument('--third_party_dir', type=str, | |
| 220 help='Path to a directory containing third party libraries') | |
| 221 device_gdb_parser.add_argument('--gsutil_dir', type=str, | |
| 222 help='Path to a directory containing gsutil') | |
| 223 device_gdb_parser.add_argument('--package_name', type=str, | |
| 224 help='Name of the Mojo Shell android package to debug') | |
| 225 device_gdb_parser.set_defaults(func=_device_gdb) | |
|
qsr
2015/06/24 15:29:01
Hum. It would be good if that worked also for desk
etiennej
2015/06/29 15:41:43
My understanding is that ppi@ wanted to have all a
| |
| 226 | |
| 153 | 227 |
| 154 def main(): | 228 def main(): |
| 155 parser = argparse.ArgumentParser(description='Command-line interface for ' | 229 parser = argparse.ArgumentParser(description='Command-line interface for ' |
| 156 'mojo:debugger') | 230 'mojo:debugger') |
| 157 subparsers = parser.add_subparsers(help='the tool to run') | 231 subparsers = parser.add_subparsers(help='the tool to run') |
| 158 _add_device_command(subparsers) | 232 _add_device_command(subparsers) |
| 159 _add_tracing_command(subparsers) | 233 _add_tracing_command(subparsers) |
| 160 _add_wm_command(subparsers) | 234 _add_wm_command(subparsers) |
| 161 | 235 |
| 162 args = parser.parse_args() | 236 args = parser.parse_args() |
| 163 return args.func(args) | 237 return args.func(args) |
| 164 | 238 |
| 165 if __name__ == '__main__': | 239 if __name__ == '__main__': |
| 166 sys.exit(main()) | 240 sys.exit(main()) |
| OLD | NEW |