blob: f78ef2b9a84326bca3e6756d1fc27d0897add651 [file] [log] [blame]
Josh Gao043bad72015-09-22 11:43:08 -07001#!/usr/bin/env python
2#
3# Copyright (C) 2015 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
18import adb
19import argparse
Alex Light92476652019-01-17 11:18:48 -080020import json
Josh Gao043bad72015-09-22 11:43:08 -070021import logging
22import os
Ryan Prichard5d1c3cb2019-06-04 16:35:02 -070023import posixpath
Elliott Hughes89e1ecf2017-06-30 14:03:32 -070024import re
Ryan Prichard5d1c3cb2019-06-04 16:35:02 -070025import shutil
Josh Gao043bad72015-09-22 11:43:08 -070026import subprocess
27import sys
Ryan Prichard5d1c3cb2019-06-04 16:35:02 -070028import tempfile
Alex Light92476652019-01-17 11:18:48 -080029import textwrap
Josh Gao043bad72015-09-22 11:43:08 -070030
31# Shared functions across gdbclient.py and ndk-gdb.py.
32import gdbrunner
33
Ryan Prichard5d1c3cb2019-06-04 16:35:02 -070034g_temp_dirs = []
35
Haibo Huange194fce2020-01-06 14:40:27 -080036
37def read_toolchain_config(root):
38 """Finds out current toolchain path and version."""
39 def get_value(str):
40 return str[str.index('"') + 1:str.rindex('"')]
41
42 config_path = os.path.join(root, 'build', 'soong', 'cc', 'config',
43 'global.go')
44 with open(config_path) as f:
45 contents = f.readlines()
46 clang_base = ""
47 clang_version = ""
48 for line in contents:
49 line = line.strip()
50 if line.startswith('ClangDefaultBase'):
51 clang_base = get_value(line)
52 elif line.startswith('ClangDefaultVersion'):
53 clang_version = get_value(line)
54 return (clang_base, clang_version)
55
56
Josh Gao043bad72015-09-22 11:43:08 -070057def get_gdbserver_path(root, arch):
Haibo Huang931cd0b2019-03-26 13:57:32 -070058 path = "{}/prebuilts/misc/gdbserver/android-{}/gdbserver{}"
Josh Gao043bad72015-09-22 11:43:08 -070059 if arch.endswith("64"):
Haibo Huang931cd0b2019-03-26 13:57:32 -070060 return path.format(root, arch, "64")
Josh Gao043bad72015-09-22 11:43:08 -070061 else:
Haibo Huang931cd0b2019-03-26 13:57:32 -070062 return path.format(root, arch, "")
Josh Gao043bad72015-09-22 11:43:08 -070063
64
Haibo Huange194fce2020-01-06 14:40:27 -080065def get_lldb_server_path(root, clang_base, clang_version, arch):
66 arch = {
67 'arm': 'arm',
68 'arm64': 'aarch64',
69 'x86': 'i386',
70 'x86_64': 'x86_64',
71 }[arch]
72 return os.path.join(root, clang_base, "linux-x86",
73 clang_version, "runtimes_ndk_cxx", arch, "lldb-server")
74
75
Elliott Hughes89e1ecf2017-06-30 14:03:32 -070076def get_tracer_pid(device, pid):
77 if pid is None:
78 return 0
79
80 line, _ = device.shell(["grep", "-e", "^TracerPid:", "/proc/{}/status".format(pid)])
81 tracer_pid = re.sub('TracerPid:\t(.*)\n', r'\1', line)
82 return int(tracer_pid)
83
84
Josh Gao043bad72015-09-22 11:43:08 -070085def parse_args():
86 parser = gdbrunner.ArgumentParser()
87
88 group = parser.add_argument_group(title="attach target")
89 group = group.add_mutually_exclusive_group(required=True)
90 group.add_argument(
91 "-p", dest="target_pid", metavar="PID", type=int,
92 help="attach to a process with specified PID")
93 group.add_argument(
94 "-n", dest="target_name", metavar="NAME",
95 help="attach to a process with specified name")
96 group.add_argument(
97 "-r", dest="run_cmd", metavar="CMD", nargs=argparse.REMAINDER,
98 help="run a binary on the device, with args")
99
100 parser.add_argument(
101 "--port", nargs="?", default="5039",
Elliott Hughes89e1ecf2017-06-30 14:03:32 -0700102 help="override the port used on the host [default: 5039]")
Josh Gao043bad72015-09-22 11:43:08 -0700103 parser.add_argument(
104 "--user", nargs="?", default="root",
105 help="user to run commands as on the device [default: root]")
Alex Light92476652019-01-17 11:18:48 -0800106 parser.add_argument(
107 "--setup-forwarding", default=None, choices=["gdb", "vscode"],
108 help=("Setup the gdbserver and port forwarding. Prints commands or " +
109 ".vscode/launch.json configuration needed to connect the debugging " +
110 "client to the server."))
Josh Gao043bad72015-09-22 11:43:08 -0700111
Haibo Huange194fce2020-01-06 14:40:27 -0800112 lldb_group = parser.add_mutually_exclusive_group()
113 lldb_group.add_argument("--lldb", action="store_true", help="Use lldb.")
114 lldb_group.add_argument("--no-lldb", action="store_true", help="Do not use lldb.")
115
Peter Collingbourne63bf1082018-12-19 20:51:42 -0800116 parser.add_argument(
117 "--env", nargs=1, action="append", metavar="VAR=VALUE",
118 help="set environment variable when running a binary")
119
Josh Gao043bad72015-09-22 11:43:08 -0700120 return parser.parse_args()
121
122
Elliott Hughes1a2f12d2017-06-02 13:15:59 -0700123def verify_device(root, device):
Josh Gao466e2892017-07-13 15:39:05 -0700124 name = device.get_prop("ro.product.name")
125 target_device = os.environ["TARGET_PRODUCT"]
126 if target_device != name:
127 msg = "TARGET_PRODUCT ({}) does not match attached device ({})"
128 sys.exit(msg.format(target_device, name))
Josh Gao043bad72015-09-22 11:43:08 -0700129
130
131def get_remote_pid(device, process_name):
132 processes = gdbrunner.get_processes(device)
133 if process_name not in processes:
134 msg = "failed to find running process {}".format(process_name)
135 sys.exit(msg)
136 pids = processes[process_name]
137 if len(pids) > 1:
138 msg = "multiple processes match '{}': {}".format(process_name, pids)
139 sys.exit(msg)
140
141 # Fetch the binary using the PID later.
142 return pids[0]
143
144
Ryan Prichard5d1c3cb2019-06-04 16:35:02 -0700145def make_temp_dir(prefix):
146 global g_temp_dirs
147 result = tempfile.mkdtemp(prefix='gdbclient-linker-')
148 g_temp_dirs.append(result)
149 return result
150
151
152def ensure_linker(device, sysroot, interp):
153 """Ensure that the device's linker exists on the host.
154
155 PT_INTERP is usually /system/bin/linker[64], but on the device, that file is
156 a symlink to /apex/com.android.runtime/bin/linker[64]. The symbolized linker
157 binary on the host is located in ${sysroot}/apex, not in ${sysroot}/system,
158 so add the ${sysroot}/apex path to the solib search path.
159
160 PT_INTERP will be /system/bin/bootstrap/linker[64] for executables using the
161 non-APEX/bootstrap linker. No search path modification is needed.
162
163 For a tapas build, only an unbundled app is built, and there is no linker in
164 ${sysroot} at all, so copy the linker from the device.
165
166 Returns:
167 A directory to add to the soinfo search path or None if no directory
168 needs to be added.
169 """
170
171 # Static executables have no interpreter.
172 if interp is None:
173 return None
174
175 # gdb will search for the linker using the PT_INTERP path. First try to find
176 # it in the sysroot.
177 local_path = os.path.join(sysroot, interp.lstrip("/"))
178 if os.path.exists(local_path):
179 return None
180
181 # If the linker on the device is a symlink, search for the symlink's target
182 # in the sysroot directory.
183 interp_real, _ = device.shell(["realpath", interp])
184 interp_real = interp_real.strip()
185 local_path = os.path.join(sysroot, interp_real.lstrip("/"))
186 if os.path.exists(local_path):
187 if posixpath.basename(interp) == posixpath.basename(interp_real):
188 # Add the interpreter's directory to the search path.
189 return os.path.dirname(local_path)
190 else:
191 # If PT_INTERP is linker_asan[64], but the sysroot file is
192 # linker[64], then copy the local file to the name gdb expects.
193 result = make_temp_dir('gdbclient-linker-')
194 shutil.copy(local_path, os.path.join(result, posixpath.basename(interp)))
195 return result
196
197 # Pull the system linker.
198 result = make_temp_dir('gdbclient-linker-')
199 device.pull(interp, os.path.join(result, posixpath.basename(interp)))
200 return result
Josh Gao043bad72015-09-22 11:43:08 -0700201
202
David Pursell639d1c42015-10-20 15:38:32 -0700203def handle_switches(args, sysroot):
Josh Gao043bad72015-09-22 11:43:08 -0700204 """Fetch the targeted binary and determine how to attach gdb.
205
206 Args:
207 args: Parsed arguments.
208 sysroot: Local sysroot path.
209
210 Returns:
211 (binary_file, attach_pid, run_cmd).
212 Precisely one of attach_pid or run_cmd will be None.
213 """
214
215 device = args.device
216 binary_file = None
217 pid = None
218 run_cmd = None
219
Josh Gao057c2732017-05-24 15:55:50 -0700220 args.su_cmd = ["su", args.user] if args.user else []
221
Josh Gao043bad72015-09-22 11:43:08 -0700222 if args.target_pid:
223 # Fetch the binary using the PID later.
224 pid = args.target_pid
225 elif args.target_name:
226 # Fetch the binary using the PID later.
227 pid = get_remote_pid(device, args.target_name)
228 elif args.run_cmd:
229 if not args.run_cmd[0]:
230 sys.exit("empty command passed to -r")
Josh Gao043bad72015-09-22 11:43:08 -0700231 run_cmd = args.run_cmd
Kevin Rocard258c89e2017-07-12 18:21:29 -0700232 if not run_cmd[0].startswith("/"):
233 try:
234 run_cmd[0] = gdbrunner.find_executable_path(device, args.run_cmd[0],
235 run_as_cmd=args.su_cmd)
236 except RuntimeError:
237 sys.exit("Could not find executable '{}' passed to -r, "
238 "please provide an absolute path.".format(args.run_cmd[0]))
239
David Pursell639d1c42015-10-20 15:38:32 -0700240 binary_file, local = gdbrunner.find_file(device, run_cmd[0], sysroot,
Josh Gao057c2732017-05-24 15:55:50 -0700241 run_as_cmd=args.su_cmd)
Josh Gao043bad72015-09-22 11:43:08 -0700242 if binary_file is None:
243 assert pid is not None
244 try:
David Pursell639d1c42015-10-20 15:38:32 -0700245 binary_file, local = gdbrunner.find_binary(device, pid, sysroot,
Josh Gao057c2732017-05-24 15:55:50 -0700246 run_as_cmd=args.su_cmd)
Josh Gao043bad72015-09-22 11:43:08 -0700247 except adb.ShellError:
248 sys.exit("failed to pull binary for PID {}".format(pid))
249
David Pursell639d1c42015-10-20 15:38:32 -0700250 if not local:
251 logging.warning("Couldn't find local unstripped executable in {},"
252 " symbols may not be available.".format(sysroot))
253
Josh Gao043bad72015-09-22 11:43:08 -0700254 return (binary_file, pid, run_cmd)
255
Alex Light92476652019-01-17 11:18:48 -0800256def generate_vscode_script(gdbpath, root, sysroot, binary_name, port, dalvik_gdb_script, solib_search_path):
257 # TODO It would be nice if we didn't need to copy this or run the
258 # gdbclient.py program manually. Doing this would probably require
259 # writing a vscode extension or modifying an existing one.
260 res = {
261 "name": "(gdbclient.py) Attach {} (port: {})".format(binary_name.split("/")[-1], port),
262 "type": "cppdbg",
263 "request": "launch", # Needed for gdbserver.
264 "cwd": root,
265 "program": binary_name,
266 "MIMode": "gdb",
267 "miDebuggerServerAddress": "localhost:{}".format(port),
268 "miDebuggerPath": gdbpath,
269 "setupCommands": [
270 {
271 # Required for vscode.
272 "description": "Enable pretty-printing for gdb",
273 "text": "-enable-pretty-printing",
274 "ignoreFailures": True,
275 },
276 {
277 "description": "gdb command: dir",
278 "text": "-environment-directory {}".format(root),
279 "ignoreFailures": False
280 },
281 {
282 "description": "gdb command: set solib-search-path",
283 "text": "-gdb-set solib-search-path {}".format(":".join(solib_search_path)),
284 "ignoreFailures": False
285 },
286 {
287 "description": "gdb command: set solib-absolute-prefix",
288 "text": "-gdb-set solib-absolute-prefix {}".format(sysroot),
289 "ignoreFailures": False
290 },
291 ]
292 }
293 if dalvik_gdb_script:
294 res["setupCommands"].append({
295 "description": "gdb command: source art commands",
296 "text": "-interpreter-exec console \"source {}\"".format(dalvik_gdb_script),
297 "ignoreFailures": False,
298 })
299 return json.dumps(res, indent=4)
Josh Gao466e2892017-07-13 15:39:05 -0700300
Alex Light92476652019-01-17 11:18:48 -0800301def generate_gdb_script(root, sysroot, binary_name, port, dalvik_gdb_script, solib_search_path, connect_timeout):
Josh Gao043bad72015-09-22 11:43:08 -0700302 solib_search_path = ":".join(solib_search_path)
303
304 gdb_commands = ""
Alex Light92476652019-01-17 11:18:48 -0800305 gdb_commands += "file '{}'\n".format(binary_name)
Josh Gao19f18ce2015-10-22 16:08:13 -0700306 gdb_commands += "directory '{}'\n".format(root)
Josh Gao043bad72015-09-22 11:43:08 -0700307 gdb_commands += "set solib-absolute-prefix {}\n".format(sysroot)
308 gdb_commands += "set solib-search-path {}\n".format(solib_search_path)
Alex Light92476652019-01-17 11:18:48 -0800309 if dalvik_gdb_script:
Josh Gao043bad72015-09-22 11:43:08 -0700310 gdb_commands += "source {}\n".format(dalvik_gdb_script)
311
David Pursell320f8812015-10-05 14:22:10 -0700312 # Try to connect for a few seconds, sometimes the device gdbserver takes
313 # a little bit to come up, especially on emulators.
314 gdb_commands += """
315python
316
317def target_remote_with_retry(target, timeout_seconds):
318 import time
319 end_time = time.time() + timeout_seconds
320 while True:
321 try:
Dan Albertd124bc72018-06-26 11:15:16 -0700322 gdb.execute("target extended-remote " + target)
David Pursell320f8812015-10-05 14:22:10 -0700323 return True
324 except gdb.error as e:
325 time_left = end_time - time.time()
326 if time_left < 0 or time_left > timeout_seconds:
327 print("Error: unable to connect to device.")
328 print(e)
329 return False
330 time.sleep(min(0.25, time_left))
331
332target_remote_with_retry(':{}', {})
333
334end
335""".format(port, connect_timeout)
336
Josh Gao043bad72015-09-22 11:43:08 -0700337 return gdb_commands
338
Haibo Huange194fce2020-01-06 14:40:27 -0800339
Haibo Huang07e17072020-05-12 16:51:29 -0700340def generate_lldb_script(root, sysroot, binary_name, port, solib_search_path):
Haibo Huange194fce2020-01-06 14:40:27 -0800341 commands = []
342 commands.append(
343 'settings append target.exec-search-paths {}'.format(' '.join(solib_search_path)))
344
345 commands.append('target create {}'.format(binary_name))
Haibo Huang07e17072020-05-12 16:51:29 -0700346 commands.append("settings set target.source-map '' '{}'".format(root))
Haibo Huange194fce2020-01-06 14:40:27 -0800347 commands.append('target modules search-paths add / {}/'.format(sysroot))
348 commands.append('gdb-remote {}'.format(port))
349 return '\n'.join(commands)
350
351
352def generate_setup_script(debugger_path, sysroot, linker_search_dir, binary_file, is64bit, port, debugger, connect_timeout=5):
Alex Light92476652019-01-17 11:18:48 -0800353 # Generate a setup script.
354 # TODO: Detect the zygote and run 'art-on' automatically.
355 root = os.environ["ANDROID_BUILD_TOP"]
356 symbols_dir = os.path.join(sysroot, "system", "lib64" if is64bit else "lib")
357 vendor_dir = os.path.join(sysroot, "vendor", "lib64" if is64bit else "lib")
358
359 solib_search_path = []
360 symbols_paths = ["", "hw", "ssl/engines", "drm", "egl", "soundfx"]
361 vendor_paths = ["", "hw", "egl"]
362 solib_search_path += [os.path.join(symbols_dir, x) for x in symbols_paths]
363 solib_search_path += [os.path.join(vendor_dir, x) for x in vendor_paths]
Ryan Prichard5d1c3cb2019-06-04 16:35:02 -0700364 if linker_search_dir is not None:
365 solib_search_path += [linker_search_dir]
Alex Light92476652019-01-17 11:18:48 -0800366
367 dalvik_gdb_script = os.path.join(root, "development", "scripts", "gdb", "dalvik.gdb")
368 if not os.path.exists(dalvik_gdb_script):
369 logging.warning(("couldn't find {} - ART debugging options will not " +
370 "be available").format(dalvik_gdb_script))
371 dalvik_gdb_script = None
372
373 if debugger == "vscode":
374 return generate_vscode_script(
Haibo Huange194fce2020-01-06 14:40:27 -0800375 debugger_path, root, sysroot, binary_file.name, port, dalvik_gdb_script, solib_search_path)
Alex Light92476652019-01-17 11:18:48 -0800376 elif debugger == "gdb":
377 return generate_gdb_script(root, sysroot, binary_file.name, port, dalvik_gdb_script, solib_search_path, connect_timeout)
Haibo Huange194fce2020-01-06 14:40:27 -0800378 elif debugger == 'lldb':
379 return generate_lldb_script(
Haibo Huang07e17072020-05-12 16:51:29 -0700380 root, sysroot, binary_file.name, port, solib_search_path)
Alex Light92476652019-01-17 11:18:48 -0800381 else:
382 raise Exception("Unknown debugger type " + debugger)
383
Josh Gao043bad72015-09-22 11:43:08 -0700384
Ryan Prichard5d1c3cb2019-06-04 16:35:02 -0700385def do_main():
Josh Gao466e2892017-07-13 15:39:05 -0700386 required_env = ["ANDROID_BUILD_TOP",
387 "ANDROID_PRODUCT_OUT", "TARGET_PRODUCT"]
388 for env in required_env:
389 if env not in os.environ:
390 sys.exit(
391 "Environment variable '{}' not defined, have you run lunch?".format(env))
392
Josh Gao043bad72015-09-22 11:43:08 -0700393 args = parse_args()
394 device = args.device
Josh Gao44b84a82015-10-28 11:57:37 -0700395
396 if device is None:
397 sys.exit("ERROR: Failed to find device.")
398
Josh Gao043bad72015-09-22 11:43:08 -0700399 root = os.environ["ANDROID_BUILD_TOP"]
Josh Gao466e2892017-07-13 15:39:05 -0700400 sysroot = os.path.join(os.environ["ANDROID_PRODUCT_OUT"], "symbols")
Josh Gao043bad72015-09-22 11:43:08 -0700401
402 # Make sure the environment matches the attached device.
Elliott Hughes1a2f12d2017-06-02 13:15:59 -0700403 verify_device(root, device)
Josh Gao043bad72015-09-22 11:43:08 -0700404
405 debug_socket = "/data/local/tmp/debug_socket"
406 pid = None
407 run_cmd = None
408
409 # Fetch binary for -p, -n.
David Pursell639d1c42015-10-20 15:38:32 -0700410 binary_file, pid, run_cmd = handle_switches(args, sysroot)
Josh Gao043bad72015-09-22 11:43:08 -0700411
412 with binary_file:
Ryan Prichard5d1c3cb2019-06-04 16:35:02 -0700413 if sys.platform.startswith("linux"):
414 platform_name = "linux-x86"
415 elif sys.platform.startswith("darwin"):
416 platform_name = "darwin-x86"
417 else:
418 sys.exit("Unknown platform: {}".format(sys.platform))
419
Josh Gao043bad72015-09-22 11:43:08 -0700420 arch = gdbrunner.get_binary_arch(binary_file)
421 is64bit = arch.endswith("64")
422
423 # Make sure we have the linker
Haibo Huange194fce2020-01-06 14:40:27 -0800424 clang_base, clang_version = read_toolchain_config(root)
425 toolchain_path = os.path.join(root, clang_base, platform_name,
426 clang_version)
427 llvm_readobj_path = os.path.join(toolchain_path, "bin", "llvm-readobj")
Ryan Prichard5d1c3cb2019-06-04 16:35:02 -0700428 interp = gdbrunner.get_binary_interp(binary_file.name, llvm_readobj_path)
429 linker_search_dir = ensure_linker(device, sysroot, interp)
Josh Gao043bad72015-09-22 11:43:08 -0700430
Elliott Hughes89e1ecf2017-06-30 14:03:32 -0700431 tracer_pid = get_tracer_pid(device, pid)
Haibo Huange194fce2020-01-06 14:40:27 -0800432 use_lldb = args.lldb
Elliott Hughes89e1ecf2017-06-30 14:03:32 -0700433 if tracer_pid == 0:
Peter Collingbourne63bf1082018-12-19 20:51:42 -0800434 cmd_prefix = args.su_cmd
435 if args.env:
436 cmd_prefix += ['env'] + [v[0] for v in args.env]
437
Elliott Hughes89e1ecf2017-06-30 14:03:32 -0700438 # Start gdbserver.
Haibo Huange194fce2020-01-06 14:40:27 -0800439 if use_lldb:
440 server_local_path = get_lldb_server_path(
441 root, clang_base, clang_version, arch)
442 server_remote_path = "/data/local/tmp/{}-lldb-server".format(
443 arch)
444 else:
445 server_local_path = get_gdbserver_path(root, arch)
446 server_remote_path = "/data/local/tmp/{}-gdbserver".format(
447 arch)
Elliott Hughes89e1ecf2017-06-30 14:03:32 -0700448 gdbrunner.start_gdbserver(
Haibo Huange194fce2020-01-06 14:40:27 -0800449 device, server_local_path, server_remote_path,
Elliott Hughes89e1ecf2017-06-30 14:03:32 -0700450 target_pid=pid, run_cmd=run_cmd, debug_socket=debug_socket,
Haibo Huange194fce2020-01-06 14:40:27 -0800451 port=args.port, run_as_cmd=cmd_prefix, lldb=use_lldb)
Elliott Hughes89e1ecf2017-06-30 14:03:32 -0700452 else:
Haibo Huange194fce2020-01-06 14:40:27 -0800453 print(
454 "Connecting to tracing pid {} using local port {}".format(
455 tracer_pid, args.port))
Elliott Hughes89e1ecf2017-06-30 14:03:32 -0700456 gdbrunner.forward_gdbserver_port(device, local=args.port,
457 remote="tcp:{}".format(args.port))
Josh Gao043bad72015-09-22 11:43:08 -0700458
Haibo Huange194fce2020-01-06 14:40:27 -0800459 if use_lldb:
460 debugger_path = os.path.join(toolchain_path, "bin", "lldb")
461 debugger = 'lldb'
462 else:
463 debugger_path = os.path.join(
464 root, "prebuilts", "gdb", platform_name, "bin", "gdb")
465 debugger = args.setup_forwarding or "gdb"
466
Alex Light92476652019-01-17 11:18:48 -0800467 # Generate a gdb script.
Haibo Huange194fce2020-01-06 14:40:27 -0800468 setup_commands = generate_setup_script(debugger_path=debugger_path,
Alex Light92476652019-01-17 11:18:48 -0800469 sysroot=sysroot,
Ryan Prichard5d1c3cb2019-06-04 16:35:02 -0700470 linker_search_dir=linker_search_dir,
Alex Light92476652019-01-17 11:18:48 -0800471 binary_file=binary_file,
472 is64bit=is64bit,
473 port=args.port,
Haibo Huange194fce2020-01-06 14:40:27 -0800474 debugger=debugger)
Josh Gao043bad72015-09-22 11:43:08 -0700475
Haibo Huange194fce2020-01-06 14:40:27 -0800476 if use_lldb or not args.setup_forwarding:
Alex Light92476652019-01-17 11:18:48 -0800477 # Print a newline to separate our messages from the GDB session.
478 print("")
David Pursell639d1c42015-10-20 15:38:32 -0700479
Alex Light92476652019-01-17 11:18:48 -0800480 # Start gdb.
Haibo Huange194fce2020-01-06 14:40:27 -0800481 gdbrunner.start_gdb(debugger_path, setup_commands, lldb=use_lldb)
Alex Light92476652019-01-17 11:18:48 -0800482 else:
483 print("")
Haibo Huange194fce2020-01-06 14:40:27 -0800484 print(setup_commands)
Alex Light92476652019-01-17 11:18:48 -0800485 print("")
486 if args.setup_forwarding == "vscode":
Haibo Huange194fce2020-01-06 14:40:27 -0800487 print(textwrap.dedent("""
Alex Light92476652019-01-17 11:18:48 -0800488 Paste the above json into .vscode/launch.json and start the debugger as
489 normal. Press enter in this terminal once debugging is finished to shutdown
Haibo Huange194fce2020-01-06 14:40:27 -0800490 the gdbserver and close all the ports."""))
Alex Light92476652019-01-17 11:18:48 -0800491 else:
Haibo Huange194fce2020-01-06 14:40:27 -0800492 print(textwrap.dedent("""
Alex Light92476652019-01-17 11:18:48 -0800493 Paste the above gdb commands into the gdb frontend to setup the gdbserver
494 connection. Press enter in this terminal once debugging is finished to
Haibo Huange194fce2020-01-06 14:40:27 -0800495 shutdown the gdbserver and close all the ports."""))
Alex Light92476652019-01-17 11:18:48 -0800496 print("")
497 raw_input("Press enter to shutdown gdbserver")
Josh Gao043bad72015-09-22 11:43:08 -0700498
Ryan Prichard5d1c3cb2019-06-04 16:35:02 -0700499
500def main():
501 try:
502 do_main()
503 finally:
504 global g_temp_dirs
505 for temp_dir in g_temp_dirs:
506 shutil.rmtree(temp_dir)
507
508
Josh Gao043bad72015-09-22 11:43:08 -0700509if __name__ == "__main__":
510 main()