Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2014 The ChromiumOS Authors |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Wrapper for running gdb. |
| 6 | |
| 7 | This handles the fun details like running against the right sysroot, via |
| 8 | qemu, bind mounts, etc... |
| 9 | """ |
| 10 | |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 11 | import argparse |
| 12 | import contextlib |
| 13 | import errno |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 14 | import logging |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 15 | import os |
| 16 | import sys |
| 17 | import tempfile |
| 18 | |
Ben Pastene | 8d75497 | 2019-12-04 15:03:23 -0800 | [diff] [blame] | 19 | from chromite.cli.cros import cros_chrome_sdk |
Mike Frysinger | 06a51c8 | 2021-04-06 11:39:17 -0400 | [diff] [blame] | 20 | from chromite.lib import build_target_lib |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 21 | from chromite.lib import commandline |
Ben Pastene | 8d75497 | 2019-12-04 15:03:23 -0800 | [diff] [blame] | 22 | from chromite.lib import constants |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 23 | from chromite.lib import cros_build_lib |
| 24 | from chromite.lib import namespaces |
| 25 | from chromite.lib import osutils |
Ben Pastene | c228d49 | 2018-07-02 13:53:58 -0700 | [diff] [blame] | 26 | from chromite.lib import path_util |
cmtice | 932e0aa | 2015-02-27 11:49:12 -0800 | [diff] [blame] | 27 | from chromite.lib import qemu |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 28 | from chromite.lib import remote_access |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 29 | from chromite.lib import retry_util |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 30 | from chromite.lib import toolchain |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 31 | |
Mike Frysinger | 1c76d4c | 2020-02-08 23:35:29 -0500 | [diff] [blame] | 32 | |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 33 | class GdbException(Exception): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 34 | """Base exception for this module.""" |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 35 | |
| 36 | |
| 37 | class GdbBadRemoteDeviceError(GdbException): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 38 | """Raised when remote device does not exist or is not responding.""" |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 39 | |
| 40 | |
| 41 | class GdbMissingSysrootError(GdbException): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 42 | """Raised when path to sysroot cannot be found in chroot.""" |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 43 | |
| 44 | |
| 45 | class GdbMissingInferiorError(GdbException): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 46 | """Raised when the binary to be debugged cannot be found.""" |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 47 | |
| 48 | |
| 49 | class GdbMissingDebuggerError(GdbException): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 50 | """Raised when cannot find correct version of debugger.""" |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 51 | |
| 52 | |
| 53 | class GdbCannotFindRemoteProcessError(GdbException): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 54 | """Raised when cannot find requested executing process on remote device.""" |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 55 | |
| 56 | |
| 57 | class GdbUnableToStartGdbserverError(GdbException): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 58 | """Raised when error occurs trying to start gdbserver on remote device.""" |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 59 | |
| 60 | |
| 61 | class GdbTooManyPidsError(GdbException): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | """Raised when more than one matching pid is found running on device.""" |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 63 | |
| 64 | |
| 65 | class GdbEarlyExitError(GdbException): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 66 | """Raised when user requests to exit early.""" |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 67 | |
| 68 | |
| 69 | class GdbCannotDetectBoardError(GdbException): |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 70 | """Board isn't specified and can't be automatically determined.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 71 | |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 72 | |
Yunlian Jiang | fdd7e08 | 2018-05-21 16:30:49 -0700 | [diff] [blame] | 73 | class GdbSimpleChromeBinaryError(GdbException): |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 74 | """None or multiple chrome binaries are under out_${board} dir.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 75 | |
cmtice | 932e0aa | 2015-02-27 11:49:12 -0800 | [diff] [blame] | 76 | |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 77 | class BoardSpecificGdb(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 78 | """Framework for running gdb.""" |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 79 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 80 | _BIND_MOUNT_PATHS = ("dev", "dev/pts", "proc", "mnt/host/source", "sys") |
| 81 | _GDB = "/usr/bin/gdb" |
| 82 | _EXTRA_SSH_SETTINGS = { |
| 83 | "CheckHostIP": "no", |
| 84 | "BatchMode": "yes", |
| 85 | "LogLevel": "QUIET", |
| 86 | } |
| 87 | _MISSING_DEBUG_INFO_MSG = """ |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 88 | %(inf_cmd)s is stripped and %(debug_file)s does not exist on your local machine. |
| 89 | The debug symbols for that package may not be installed. To install the debug |
| 90 | symbols for %(package)s only, run: |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 91 | |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 92 | cros_install_debug_syms --board=%(board)s %(package)s |
| 93 | |
| 94 | To install the debug symbols for all available packages, run: |
| 95 | |
| 96 | cros_install_debug_syms --board=%(board)s --all""" |
chromeos-ci-prod | c1e6131 | 2023-04-27 19:46:48 -0700 | [diff] [blame] | 97 | _ASH_CHROME_REMOTE_BIN = "/opt/google/chrome/chrome" |
| 98 | _LACROS_CHROME_REMOTE_BIN = "/usr/local/lacros-chrome/chrome" |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 99 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 100 | def __init__( |
| 101 | self, |
| 102 | board, |
| 103 | gdb_args, |
| 104 | inf_cmd, |
| 105 | inf_args, |
| 106 | remote, |
| 107 | pid, |
| 108 | remote_process_name, |
| 109 | cgdb_flag, |
| 110 | ping, |
| 111 | binary, |
Marc Grimme | 5441aba | 2023-04-14 16:56:36 +0200 | [diff] [blame] | 112 | gdb_binary, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 113 | ): |
| 114 | self.board = board |
| 115 | self.sysroot = None |
| 116 | self.prompt = "(gdb) " |
| 117 | self.inf_cmd = inf_cmd |
chromeos-ci-prod | c1e6131 | 2023-04-27 19:46:48 -0700 | [diff] [blame] | 118 | if not self.inf_cmd: |
| 119 | if remote_process_name.startswith("lacros-"): |
| 120 | self.inf_cmd = self._LACROS_CHROME_REMOTE_BIN |
| 121 | else: |
| 122 | self.inf_cmd = self._ASH_CHROME_REMOTE_BIN |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 123 | self.run_as_root = False |
| 124 | self.gdb_args = gdb_args |
| 125 | self.inf_args = inf_args |
| 126 | self.remote = remote.hostname if remote else None |
chromeos-ci-prod | c1e6131 | 2023-04-27 19:46:48 -0700 | [diff] [blame] | 127 | # strip off the lacros- or ash- specifics |
| 128 | self.remote_process_name = remote_process_name.lstrip("lacros-").lstrip( |
| 129 | "ash-" |
| 130 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 131 | self.pid = pid |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 132 | # Port used for sending ssh commands to DUT. |
| 133 | self.remote_port = remote.port if remote else None |
| 134 | # Port for communicating between gdb & gdbserver. |
| 135 | self.gdbserver_port = remote_access.GetUnusedPort() |
| 136 | self.ssh_settings = remote_access.CompileSSHConnectSettings( |
| 137 | **self._EXTRA_SSH_SETTINGS |
| 138 | ) |
| 139 | self.cgdb = cgdb_flag |
| 140 | self.framework = "auto" |
| 141 | self.qemu = None |
| 142 | self.device = None |
Marc Grimme | 5441aba | 2023-04-14 16:56:36 +0200 | [diff] [blame] | 143 | self.cross_gdb = gdb_binary |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 144 | self.ping = ping |
| 145 | self.binary = binary |
| 146 | self.in_chroot = None |
| 147 | self.chrome_path = None |
| 148 | self.sdk_path = None |
Yunlian Jiang | fdd7e08 | 2018-05-21 16:30:49 -0700 | [diff] [blame] | 149 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 150 | def IsInChroot(self): |
| 151 | """Decide whether we are in chroot or chrome-sdk.""" |
| 152 | return os.path.exists("/mnt/host/source/chromite/") |
Yunlian Jiang | fdd7e08 | 2018-05-21 16:30:49 -0700 | [diff] [blame] | 153 | |
chromeos-ci-prod | c1e6131 | 2023-04-27 19:46:48 -0700 | [diff] [blame] | 154 | def IsLacros(self): |
Trent Apted | 380d1c6 | 2023-05-16 09:19:31 +1000 | [diff] [blame] | 155 | """Whether the Lacros chrome binary is in use. |
| 156 | |
| 157 | The --attach option specifies the type of if you want to attach to |
| 158 | browser, renderer or gpu-process. Prefixed with either lacros- or ash-. |
| 159 | You can specify which browser you want to attach to. Default is ash. |
| 160 | """ |
chromeos-ci-prod | c1e6131 | 2023-04-27 19:46:48 -0700 | [diff] [blame] | 161 | return self.inf_cmd == self._LACROS_CHROME_REMOTE_BIN |
| 162 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 163 | def SimpleChromeGdb(self): |
| 164 | """Get the name of the cross gdb based on board name.""" |
| 165 | bin_path = cros_chrome_sdk.SDKFetcher.GetCachePath( |
| 166 | cros_chrome_sdk.SDKFetcher.TARGET_TOOLCHAIN_KEY, |
| 167 | self.sdk_path, |
| 168 | self.board, |
| 169 | ) |
| 170 | bin_path = os.path.join(bin_path, "bin") |
| 171 | for f in os.listdir(bin_path): |
| 172 | if f.endswith("gdb"): |
| 173 | return os.path.join(bin_path, f) |
| 174 | raise GdbMissingDebuggerError( |
| 175 | "Cannot find cross gdb for %s." % self.board |
| 176 | ) |
Yunlian Jiang | fdd7e08 | 2018-05-21 16:30:49 -0700 | [diff] [blame] | 177 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 178 | def SimpleChromeSysroot(self): |
| 179 | """Get the sysroot in simple chrome.""" |
| 180 | sysroot = cros_chrome_sdk.SDKFetcher.GetCachePath( |
| 181 | constants.CHROME_SYSROOT_TAR, self.sdk_path, self.board |
| 182 | ) |
| 183 | if not sysroot: |
| 184 | raise GdbMissingSysrootError( |
| 185 | "Cannot find sysroot for %s at %s" % (self.board, self.sdk_path) |
| 186 | ) |
| 187 | return sysroot |
Yunlian Jiang | fdd7e08 | 2018-05-21 16:30:49 -0700 | [diff] [blame] | 188 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 189 | def GetSimpleChromeBinary(self): |
| 190 | """Get path to the binary in simple chrome.""" |
| 191 | if self.binary: |
| 192 | return self.binary |
Yunlian Jiang | fdd7e08 | 2018-05-21 16:30:49 -0700 | [diff] [blame] | 193 | |
chromeos-ci-prod | c1e6131 | 2023-04-27 19:46:48 -0700 | [diff] [blame] | 194 | output_dir = ( |
| 195 | os.path.join(self.chrome_path, "src", f"out_{self.board}_lacros") |
| 196 | if self.IsLacros() |
| 197 | else os.path.join(self.chrome_path, "src", f"out_{self.board}") |
| 198 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 199 | target_binary = None |
| 200 | binary_name = os.path.basename(self.inf_cmd) |
| 201 | for root, _, files in os.walk(output_dir): |
| 202 | for f in files: |
| 203 | if f == binary_name: |
| 204 | if target_binary is None: |
| 205 | target_binary = os.path.join(root, f) |
| 206 | else: |
| 207 | raise GdbSimpleChromeBinaryError( |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 208 | "There are multiple %s under %s. Please specify " |
| 209 | "the path to the binary via --binary" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 210 | % (binary_name, output_dir) |
| 211 | ) |
| 212 | if target_binary is None: |
Yunlian Jiang | fdd7e08 | 2018-05-21 16:30:49 -0700 | [diff] [blame] | 213 | raise GdbSimpleChromeBinaryError( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 214 | "There is no %s under %s." % (binary_name, output_dir) |
| 215 | ) |
| 216 | return target_binary |
cmtice | 932e0aa | 2015-02-27 11:49:12 -0800 | [diff] [blame] | 217 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 218 | def VerifyAndFinishInitialization(self, device): |
| 219 | """Verify files/processes exist and flags are correct.""" |
| 220 | if not self.board: |
| 221 | if self.remote: |
| 222 | self.board = cros_build_lib.GetBoard( |
| 223 | device_board=device.board, |
| 224 | override_board=self.board, |
| 225 | strict=True, |
| 226 | ) |
| 227 | else: |
| 228 | raise GdbCannotDetectBoardError( |
| 229 | "Cannot determine which board to use. " |
| 230 | "Please specify the with --board flag." |
| 231 | ) |
| 232 | self.in_chroot = self.IsInChroot() |
| 233 | self.prompt = "(%s-gdb) " % self.board |
| 234 | if self.in_chroot: |
| 235 | self.sysroot = build_target_lib.get_default_sysroot_path(self.board) |
| 236 | self.inf_cmd = self.RemoveSysrootPrefix(self.inf_cmd) |
Marc Grimme | 5441aba | 2023-04-14 16:56:36 +0200 | [diff] [blame] | 237 | if not self.cross_gdb: |
| 238 | self.cross_gdb = self.GetCrossGdb() |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 239 | else: |
| 240 | self.chrome_path = os.path.realpath( |
| 241 | os.path.join( |
| 242 | os.path.dirname(os.path.realpath(__file__)), "../../../.." |
| 243 | ) |
| 244 | ) |
| 245 | self.sdk_path = path_util.FindCacheDir() |
| 246 | self.sysroot = self.SimpleChromeSysroot() |
Marc Grimme | 5441aba | 2023-04-14 16:56:36 +0200 | [diff] [blame] | 247 | if not self.cross_gdb: |
| 248 | self.cross_gdb = self.SimpleChromeGdb() |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 249 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 250 | if self.remote: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 251 | # If given remote process name, find pid & inf_cmd on remote device. |
| 252 | if self.remote_process_name or self.pid: |
| 253 | self._FindRemoteProcess(device) |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 254 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 255 | # Verify that sysroot is valid (exists). |
| 256 | if not os.path.isdir(self.sysroot): |
| 257 | raise GdbMissingSysrootError( |
| 258 | "Sysroot does not exist: %s" % self.sysroot |
| 259 | ) |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 260 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 261 | self.device = device |
| 262 | if not self.in_chroot: |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 263 | return |
| 264 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 265 | sysroot_inf_cmd = "" |
| 266 | if self.inf_cmd: |
| 267 | sysroot_inf_cmd = os.path.join( |
| 268 | self.sysroot, self.inf_cmd.lstrip("/") |
| 269 | ) |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 270 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 271 | # Verify that inf_cmd, if given, exists. |
| 272 | if sysroot_inf_cmd and not os.path.exists(sysroot_inf_cmd): |
| 273 | raise GdbMissingInferiorError( |
| 274 | "Cannot find file %s (in sysroot)." % sysroot_inf_cmd |
| 275 | ) |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 276 | |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 277 | # Check to see if inf_cmd is stripped, and if so, check to see if debug |
| 278 | # file exists. If not, tell user and give them the option of quitting & |
| 279 | # getting the debug info. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 280 | if sysroot_inf_cmd: |
| 281 | stripped_info = cros_build_lib.run( |
| 282 | ["file", sysroot_inf_cmd], capture_output=True, encoding="utf-8" |
| 283 | ).stdout |
| 284 | if " not stripped" not in stripped_info: |
| 285 | debug_file = os.path.join( |
| 286 | self.sysroot, "usr/lib/debug", self.inf_cmd.lstrip("/") |
| 287 | ) |
| 288 | debug_file += ".debug" |
| 289 | if not os.path.exists(debug_file): |
| 290 | equery = "equery-%s" % self.board |
| 291 | package = cros_build_lib.run( |
| 292 | [equery, "-q", "b", self.inf_cmd], |
| 293 | capture_output=True, |
| 294 | encoding="utf-8", |
| 295 | ).stdout |
| 296 | # pylint: disable=logging-not-lazy |
| 297 | logging.info( |
| 298 | self._MISSING_DEBUG_INFO_MSG |
| 299 | % { |
| 300 | "board": self.board, |
| 301 | "inf_cmd": self.inf_cmd, |
| 302 | "package": package, |
| 303 | "debug_file": debug_file, |
| 304 | } |
| 305 | ) |
| 306 | answer = cros_build_lib.BooleanPrompt() |
| 307 | if not answer: |
| 308 | raise GdbEarlyExitError( |
| 309 | "Exiting early, at user request." |
| 310 | ) |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 311 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 312 | # Set up qemu, if appropriate. |
| 313 | qemu_arch = qemu.Qemu.DetectArch(self._GDB, self.sysroot) |
| 314 | if qemu_arch is None: |
| 315 | self.framework = "ldso" |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 316 | else: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 317 | self.framework = "qemu" |
| 318 | self.qemu = qemu.Qemu(self.sysroot, arch=qemu_arch) |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 319 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 320 | if self.remote: |
| 321 | # Verify cgdb flag info. |
| 322 | if self.cgdb: |
| 323 | if osutils.Which("cgdb") is None: |
| 324 | raise GdbMissingDebuggerError( |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 325 | "Cannot find cgdb. Please install cgdb first." |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 326 | ) |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 327 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 328 | def RemoveSysrootPrefix(self, path): |
| 329 | """Returns the given path with any sysroot prefix removed.""" |
| 330 | # If the sysroot is /, then the paths are already normalized. |
| 331 | if self.sysroot != "/" and path.startswith(self.sysroot): |
| 332 | path = path.replace(self.sysroot, "", 1) |
| 333 | return path |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 334 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 335 | @staticmethod |
| 336 | def GetNonRootAccount(): |
| 337 | """Return details about the non-root account we want to use. |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 338 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 339 | Returns: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 340 | A tuple of (username, uid, gid, home). |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 341 | """ |
| 342 | return ( |
| 343 | os.environ.get("SUDO_USER", "nobody"), |
| 344 | int(os.environ.get("SUDO_UID", "65534")), |
| 345 | int(os.environ.get("SUDO_GID", "65534")), |
| 346 | # Should we find a better home? |
| 347 | "/tmp/portage", |
| 348 | ) |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 349 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 350 | @staticmethod |
| 351 | @contextlib.contextmanager |
| 352 | def LockDb(db): |
| 353 | """Lock an account database. |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 354 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 355 | We use the same algorithm as shadow/user.eclass. This way we don't race |
| 356 | and corrupt things in parallel. |
| 357 | """ |
| 358 | lock = "%s.lock" % db |
| 359 | _, tmplock = tempfile.mkstemp(prefix="%s.platform." % lock) |
Raul E Rangel | 746c45d | 2018-05-09 09:27:31 -0600 | [diff] [blame] | 360 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 361 | # First try forever to grab the lock. |
| 362 | retry = lambda e: e.errno == errno.EEXIST |
| 363 | # Retry quickly at first, but slow down over time. |
| 364 | try: |
| 365 | retry_util.GenericRetry( |
| 366 | retry, 60, os.link, tmplock, lock, sleep=0.1 |
| 367 | ) |
| 368 | except Exception as e: |
| 369 | raise Exception("Could not grab lock %s. %s" % (lock, e)) |
Raul E Rangel | 746c45d | 2018-05-09 09:27:31 -0600 | [diff] [blame] | 370 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 371 | # Yield while holding the lock, but try to clean it no matter what. |
| 372 | try: |
| 373 | os.unlink(tmplock) |
| 374 | yield lock |
| 375 | finally: |
| 376 | os.unlink(lock) |
Raul E Rangel | 746c45d | 2018-05-09 09:27:31 -0600 | [diff] [blame] | 377 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 378 | def SetupUser(self): |
| 379 | """Propogate the user name<->id mapping from outside the chroot. |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 380 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 381 | Some unittests use getpwnam($USER), as does bash. If the account |
| 382 | is not registered in the sysroot, they get back errors. |
| 383 | """ |
| 384 | MAGIC_GECOS = ( |
| 385 | "Added by your friendly platform test helper; do not modify" |
| 386 | ) |
| 387 | # This is kept in sync with what sdk_lib/make_chroot.sh generates. |
| 388 | SDK_GECOS = "ChromeOS Developer" |
| 389 | |
| 390 | user, uid, gid, home = self.GetNonRootAccount() |
| 391 | if user == "nobody": |
| 392 | return |
| 393 | |
| 394 | passwd_db = os.path.join(self.sysroot, "etc", "passwd") |
| 395 | with self.LockDb(passwd_db): |
| 396 | data = osutils.ReadFile(passwd_db) |
| 397 | accts = data.splitlines() |
| 398 | for acct in accts: |
| 399 | passwd = acct.split(":") |
| 400 | if passwd[0] == user: |
| 401 | # Did the sdk make this account? |
| 402 | if passwd[4] == SDK_GECOS: |
| 403 | # Don't modify it (see below) since we didn't create it. |
| 404 | return |
| 405 | |
| 406 | # Did we make this account? |
| 407 | if passwd[4] != MAGIC_GECOS: |
| 408 | raise RuntimeError( |
| 409 | "your passwd db (%s) has unmanaged acct %s" |
| 410 | % (passwd_db, user) |
| 411 | ) |
| 412 | |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 413 | # Maybe we should see if it needs to be updated? Like if |
| 414 | # they changed UIDs? But we don't really check that |
| 415 | # elsewhere ... |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 416 | return |
| 417 | |
| 418 | acct = ( |
| 419 | "%(name)s:x:%(uid)s:%(gid)s:%(gecos)s:%(homedir)s:%(shell)s" |
| 420 | % { |
| 421 | "name": user, |
| 422 | "uid": uid, |
| 423 | "gid": gid, |
| 424 | "gecos": MAGIC_GECOS, |
| 425 | "homedir": home, |
| 426 | "shell": "/bin/bash", |
| 427 | } |
| 428 | ) |
Mike Frysinger | 31fdddd | 2023-02-24 15:50:55 -0500 | [diff] [blame] | 429 | with open(passwd_db, "a", encoding="utf-8") as f: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 430 | if data[-1] != "\n": |
| 431 | f.write("\n") |
| 432 | f.write("%s\n" % acct) |
| 433 | |
| 434 | def _FindRemoteProcess(self, device): |
| 435 | """Find a named process (or a pid) running on a remote device.""" |
| 436 | if not self.remote_process_name and not self.pid: |
| 437 | return |
| 438 | |
| 439 | if self.remote_process_name: |
Trent Apted | 380d1c6 | 2023-05-16 09:19:31 +1000 | [diff] [blame] | 440 | # Look for a process with the specified name on the remote device; |
| 441 | # if found, get its pid. Strip off the lacros- or ash- part. |
chromeos-ci-prod | c1e6131 | 2023-04-27 19:46:48 -0700 | [diff] [blame] | 442 | pname = self.remote_process_name.lstrip("lacros-").lstrip("ash-") |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 443 | if pname == "browser": |
chromeos-ci-prod | c1e6131 | 2023-04-27 19:46:48 -0700 | [diff] [blame] | 444 | all_chrome_pids = set(device.GetRunningPids(self.inf_cmd)) |
| 445 | non_main_chrome_pids = set( |
| 446 | device.GetRunningPids("%s.+type=" % self.inf_cmd) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 447 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 448 | pids = list(all_chrome_pids - non_main_chrome_pids) |
Alex Klein | 6493053 | 2023-04-17 12:20:52 -0600 | [diff] [blame] | 449 | elif pname in ("renderer", "gpu-process"): |
chromeos-ci-prod | c1e6131 | 2023-04-27 19:46:48 -0700 | [diff] [blame] | 450 | pids = device.GetRunningPids( |
| 451 | "%s.+type=%s" % (self.inf_cmd, pname) |
| 452 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 453 | else: |
| 454 | pids = device.GetRunningPids(pname) |
| 455 | |
| 456 | if pids: |
| 457 | if len(pids) == 1: |
| 458 | self.pid = pids[0] |
| 459 | else: |
| 460 | raise GdbTooManyPidsError( |
| 461 | "Multiple pids found for %s process: %s. " |
| 462 | "You must specify the correct pid." |
| 463 | % (pname, repr(pids)) |
| 464 | ) |
| 465 | else: |
| 466 | raise GdbCannotFindRemoteProcessError( |
| 467 | 'Cannot find pid for "%s" on %s' % (pname, self.remote) |
| 468 | ) |
| 469 | |
| 470 | # Find full path for process, from pid (and verify pid). |
| 471 | command = [ |
| 472 | "readlink", |
| 473 | "-e", |
| 474 | "/proc/%s/exe" % self.pid, |
Yunlian Jiang | e51d6a5 | 2018-06-18 14:02:11 -0700 | [diff] [blame] | 475 | ] |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 476 | try: |
| 477 | res = device.run(command, capture_output=True) |
| 478 | if res.returncode == 0: |
| 479 | self.inf_cmd = res.stdout.rstrip("\n") |
| 480 | except cros_build_lib.RunCommandError: |
| 481 | raise GdbCannotFindRemoteProcessError( |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 482 | "Unable to find name of process with pid %s on %s" |
| 483 | % (self.pid, self.remote) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 484 | ) |
Raul E Rangel | 746c45d | 2018-05-09 09:27:31 -0600 | [diff] [blame] | 485 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 486 | def GetCrossGdb(self): |
| 487 | """Find the appropriate cross-version of gdb for the board.""" |
| 488 | toolchains = toolchain.GetToolchainsForBoard(self.board) |
| 489 | tc = list(toolchain.FilterToolchains(toolchains, "default", True)) |
| 490 | cross_gdb = tc[0] + "-gdb" |
| 491 | if not osutils.Which(cross_gdb): |
| 492 | raise GdbMissingDebuggerError( |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 493 | "Cannot find %s; do you need to run setup_board?" % cross_gdb |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 494 | ) |
| 495 | return cross_gdb |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 496 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 497 | def GetGdbInitCommands(self, inferior_cmd, device=None): |
Alex Klein | 8b44453 | 2023-04-11 16:35:24 -0600 | [diff] [blame] | 498 | """Generate list of commands with which to initialize a gdb session.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 499 | gdb_init_commands = [] |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 500 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 501 | if self.remote: |
| 502 | sysroot_var = self.sysroot |
| 503 | else: |
| 504 | sysroot_var = "/" |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 505 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 506 | gdb_init_commands = [ |
| 507 | "set sysroot %s" % sysroot_var, |
| 508 | "set prompt %s" % self.prompt, |
| 509 | ] |
| 510 | if self.in_chroot: |
| 511 | gdb_init_commands += [ |
| 512 | "set solib-absolute-prefix %s" % sysroot_var, |
| 513 | "set solib-search-path %s" % sysroot_var, |
| 514 | "set debug-file-directory %s/usr/lib/debug" % sysroot_var, |
| 515 | ] |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 516 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 517 | if device: |
Mike Frysinger | c0780a6 | 2022-08-29 04:41:56 -0400 | [diff] [blame] | 518 | ssh_cmd = device.agent.GetSSHCommand(self.ssh_settings) |
Raul E Rangel | abd74fe | 2018-04-26 09:17:41 -0600 | [diff] [blame] | 519 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 520 | ssh_cmd.extend(["--", "gdbserver"]) |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 521 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 522 | if self.pid: |
| 523 | ssh_cmd.extend(["--attach", "stdio", str(self.pid)]) |
| 524 | target_type = "remote" |
| 525 | elif inferior_cmd: |
| 526 | ssh_cmd.extend(["-", inferior_cmd]) |
| 527 | ssh_cmd.extend(self.inf_args) |
| 528 | target_type = "remote" |
| 529 | else: |
| 530 | ssh_cmd.extend(["--multi", "stdio"]) |
| 531 | target_type = "extended-remote" |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 532 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 533 | ssh_cmd = cros_build_lib.CmdToStr(ssh_cmd) |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 534 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 535 | if self.in_chroot: |
| 536 | if inferior_cmd: |
| 537 | gdb_init_commands.append( |
| 538 | "file %s" |
| 539 | % os.path.join(sysroot_var, inferior_cmd.lstrip(os.sep)) |
| 540 | ) |
| 541 | else: |
| 542 | binary = self.GetSimpleChromeBinary() |
| 543 | gdb_init_commands += [ |
| 544 | "set debug-file-directory %s" % os.path.dirname(binary), |
| 545 | "file %s" % binary, |
| 546 | ] |
cmtice | 932e0aa | 2015-02-27 11:49:12 -0800 | [diff] [blame] | 547 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 548 | gdb_init_commands.append("target %s | %s" % (target_type, ssh_cmd)) |
| 549 | else: |
| 550 | if inferior_cmd: |
| 551 | gdb_init_commands.append("file %s " % inferior_cmd) |
| 552 | gdb_init_commands.append( |
| 553 | "set args %s" % " ".join(self.inf_args) |
| 554 | ) |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 555 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 556 | return gdb_init_commands |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 557 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 558 | def RunRemote(self): |
| 559 | """Handle remote debugging, via gdbserver & cross debugger.""" |
| 560 | with remote_access.ChromiumOSDeviceHandler( |
| 561 | self.remote, |
| 562 | port=self.remote_port, |
| 563 | connect_settings=self.ssh_settings, |
| 564 | ping=self.ping, |
| 565 | ) as device: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 566 | self.VerifyAndFinishInitialization(device) |
| 567 | gdb_cmd = self.cross_gdb |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 568 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 569 | gdb_commands = self.GetGdbInitCommands(self.inf_cmd, device) |
| 570 | gdb_args = ["--quiet"] + [ |
| 571 | "--eval-command=%s" % x for x in gdb_commands |
| 572 | ] |
| 573 | gdb_args += self.gdb_args |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 574 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 575 | if self.cgdb: |
| 576 | gdb_args = ["-d", gdb_cmd, "--"] + gdb_args |
| 577 | gdb_cmd = "cgdb" |
cmtice | 932e0aa | 2015-02-27 11:49:12 -0800 | [diff] [blame] | 578 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 579 | cros_build_lib.run( |
| 580 | [gdb_cmd] + gdb_args, |
| 581 | ignore_sigint=True, |
| 582 | print_cmd=True, |
| 583 | cwd=self.sysroot, |
| 584 | ) |
| 585 | |
| 586 | def Run(self): |
| 587 | """Runs the debugger in a proper environment (e.g. qemu).""" |
| 588 | |
| 589 | self.VerifyAndFinishInitialization(None) |
| 590 | self.SetupUser() |
| 591 | if self.framework == "qemu": |
| 592 | self.qemu.Install(self.sysroot) |
| 593 | self.qemu.RegisterBinfmt() |
| 594 | |
| 595 | for mount in self._BIND_MOUNT_PATHS: |
| 596 | path = os.path.join(self.sysroot, mount) |
| 597 | osutils.SafeMakedirs(path) |
| 598 | osutils.Mount("/" + mount, path, "none", osutils.MS_BIND) |
| 599 | |
| 600 | gdb_cmd = self._GDB |
| 601 | inferior_cmd = self.inf_cmd |
| 602 | |
| 603 | gdb_argv = self.gdb_args[:] |
| 604 | if gdb_argv: |
| 605 | gdb_argv[0] = self.RemoveSysrootPrefix(gdb_argv[0]) |
| 606 | # Some programs expect to find data files via $CWD, so doing a chroot |
| 607 | # and dropping them into / would make them fail. |
| 608 | cwd = self.RemoveSysrootPrefix(os.getcwd()) |
| 609 | |
| 610 | os.chroot(self.sysroot) |
| 611 | os.chdir(cwd) |
| 612 | # The TERM the user is leveraging might not exist in the sysroot. |
| 613 | # Force a reasonable default that supports standard color sequences. |
| 614 | os.environ["TERM"] = "ansi" |
| 615 | # Some progs want this like bash else they get super confused. |
| 616 | os.environ["PWD"] = cwd |
| 617 | if not self.run_as_root: |
| 618 | _, uid, gid, home = self.GetNonRootAccount() |
| 619 | os.setgid(gid) |
| 620 | os.setuid(uid) |
| 621 | os.environ["HOME"] = home |
| 622 | |
| 623 | gdb_commands = self.GetGdbInitCommands(inferior_cmd) |
| 624 | |
| 625 | gdb_args = [gdb_cmd, "--quiet"] + [ |
| 626 | "--eval-command=%s" % x for x in gdb_commands |
| 627 | ] |
| 628 | gdb_args += self.gdb_args |
| 629 | |
| 630 | os.execvp(gdb_cmd, gdb_args) |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 631 | |
| 632 | |
cmtice | 932e0aa | 2015-02-27 11:49:12 -0800 | [diff] [blame] | 633 | def _ReExecuteIfNeeded(argv, ns_net=False, ns_pid=False): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 634 | """Re-execute gdb as root. |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 635 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 636 | We often need to do things as root, so make sure we're that. Like chroot |
| 637 | for proper library environment or do bind mounts. |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 638 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 639 | Also unshare the mount namespace so as to ensure that doing bind mounts for |
| 640 | tests don't leak out to the normal chroot. Also unshare the UTS namespace |
| 641 | so changes to `hostname` do not impact the host. |
| 642 | """ |
| 643 | if osutils.IsNonRootUser(): |
| 644 | cmd = ["sudo", "-E", "--"] + argv |
| 645 | os.execvp(cmd[0], cmd) |
| 646 | else: |
| 647 | namespaces.SimpleUnshare(net=ns_net, pid=ns_pid) |
cmtice | 932e0aa | 2015-02-27 11:49:12 -0800 | [diff] [blame] | 648 | |
| 649 | |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 650 | def FindInferior(arg_list): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 651 | """Look for the name of the inferior (to be debugged) in arg list.""" |
cmtice | 932e0aa | 2015-02-27 11:49:12 -0800 | [diff] [blame] | 652 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 653 | program_name = "" |
| 654 | new_list = [] |
| 655 | for item in arg_list: |
| 656 | if item[0] == "-": |
| 657 | new_list.append(item) |
| 658 | elif not program_name: |
| 659 | program_name = item |
| 660 | else: |
| 661 | raise RuntimeError( |
| 662 | "Found multiple program names: %s %s" % (program_name, item) |
| 663 | ) |
cmtice | 932e0aa | 2015-02-27 11:49:12 -0800 | [diff] [blame] | 664 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 665 | return program_name, new_list |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 666 | |
| 667 | |
| 668 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 669 | parser = commandline.ArgumentParser(description=__doc__) |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 670 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 671 | parser.add_argument("--board", default=None, help="board to debug for") |
| 672 | parser.add_argument( |
Marc Grimme | 5441aba | 2023-04-14 16:56:36 +0200 | [diff] [blame] | 673 | "--gdb", |
| 674 | type="file_exists", |
| 675 | help="Path to gdb binary. Default is that gdb binary is auto detected.", |
| 676 | ) |
| 677 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 678 | "-g", |
Mike Frysinger | 3152f6b | 2023-04-26 23:47:48 -0400 | [diff] [blame] | 679 | "--gdb-args", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 680 | action="append", |
| 681 | default=[], |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 682 | help=( |
| 683 | "Arguments to gdb itself. If multiple arguments are" |
| 684 | " passed, each argument needs a separate '-g' flag." |
| 685 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 686 | ) |
Mike Frysinger | 3152f6b | 2023-04-26 23:47:48 -0400 | [diff] [blame] | 687 | # TODO(build): Delete by Jan 2024. |
| 688 | parser.add_argument( |
| 689 | "--gdb_args", |
| 690 | action="append", |
| 691 | deprecated="Use --gdb-args instead", |
| 692 | help=argparse.SUPPRESS, |
| 693 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 694 | parser.add_argument( |
| 695 | "--remote", |
| 696 | default=None, |
| 697 | type=commandline.DeviceParser(commandline.DEVICE_SCHEME_SSH), |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 698 | help=( |
| 699 | "Remote device on which to run the binary. Use" |
| 700 | ' "--remote=localhost:9222" to debug in a ChromeOS image in an' |
| 701 | " already running local virtual machine." |
| 702 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 703 | ) |
| 704 | parser.add_argument( |
| 705 | "--pid", |
| 706 | default="", |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 707 | help=( |
| 708 | "Process ID of the (already) running process on the" |
| 709 | " remote device to which to attach." |
| 710 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 711 | ) |
Mike Frysinger | 5c70278 | 2023-04-17 16:20:24 -0400 | [diff] [blame] | 712 | # TODO(build): Delete in Jan 2024. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 713 | parser.add_argument( |
| 714 | "--remote_pid", |
| 715 | dest="pid", |
Mike Frysinger | 5c70278 | 2023-04-17 16:20:24 -0400 | [diff] [blame] | 716 | help=argparse.SUPPRESS, |
| 717 | deprecated="Use --pid instead.", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 718 | ) |
| 719 | parser.add_argument( |
| 720 | "--no-ping", |
| 721 | dest="ping", |
| 722 | default=True, |
| 723 | action="store_false", |
| 724 | help="Do not ping remote before attempting to connect.", |
| 725 | ) |
| 726 | parser.add_argument( |
| 727 | "--attach", |
| 728 | dest="attach_name", |
| 729 | default="", |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 730 | help=( |
| 731 | "Name of existing process to which to attach, on remote device" |
| 732 | " (remote debugging only).Options are [browser, renderer, gpu-" |
| 733 | 'process] and can be prefixed with either "ash-" or "lacros-".' |
| 734 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 735 | ) |
| 736 | parser.add_argument( |
| 737 | "--cgdb", |
| 738 | default=False, |
| 739 | action="store_true", |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 740 | help=( |
| 741 | "Use cgdb curses interface rather than plain gdb." |
| 742 | "This option is only valid for remote debugging." |
| 743 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 744 | ) |
| 745 | parser.add_argument( |
| 746 | "inf_args", |
| 747 | nargs=argparse.REMAINDER, |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 748 | help=( |
| 749 | "Arguments for gdb to pass to the program being" |
| 750 | " debugged. These are positional and must come at the end" |
| 751 | " of the command line. This will not work if attaching" |
| 752 | " to an already running program." |
| 753 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 754 | ) |
| 755 | parser.add_argument( |
| 756 | "--binary", |
| 757 | default="", |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 758 | help=( |
| 759 | "full path to the binary being debugged." |
| 760 | " This is only useful for simple chrome." |
| 761 | " An example is --binary /home/out_falco/chrome." |
| 762 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 763 | ) |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 764 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 765 | options = parser.parse_args(argv) |
| 766 | options.Freeze() |
cmtice | b70801a | 2014-12-11 14:29:34 -0800 | [diff] [blame] | 767 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 768 | gdb_args = [] |
| 769 | inf_args = [] |
| 770 | inf_cmd = "" |
cmtice | 932e0aa | 2015-02-27 11:49:12 -0800 | [diff] [blame] | 771 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 772 | if options.inf_args: |
| 773 | inf_cmd = options.inf_args[0] |
| 774 | inf_args = options.inf_args[1:] |
cmtice | 932e0aa | 2015-02-27 11:49:12 -0800 | [diff] [blame] | 775 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 776 | if options.gdb_args: |
| 777 | gdb_args = options.gdb_args |
cmtice | 932e0aa | 2015-02-27 11:49:12 -0800 | [diff] [blame] | 778 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 779 | if inf_cmd: |
| 780 | fname = os.path.join( |
| 781 | build_target_lib.get_default_sysroot_path(options.board), |
| 782 | inf_cmd.lstrip("/"), |
| 783 | ) |
| 784 | if not os.path.exists(fname): |
Alex Klein | df8ee50 | 2022-10-18 09:48:15 -0600 | [diff] [blame] | 785 | cros_build_lib.Die("Cannot find program %s.", fname) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 786 | else: |
| 787 | if inf_args: |
| 788 | parser.error("Cannot specify arguments without a program.") |
cmtice | 932e0aa | 2015-02-27 11:49:12 -0800 | [diff] [blame] | 789 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 790 | if inf_args and (options.pid or options.attach_name): |
| 791 | parser.error( |
| 792 | "Cannot pass arguments to an already" |
| 793 | " running process (--remote-pid or --attach)." |
| 794 | ) |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 795 | |
chromeos-ci-prod | c1e6131 | 2023-04-27 19:46:48 -0700 | [diff] [blame] | 796 | if not options.remote: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 797 | if options.cgdb: |
| 798 | parser.error( |
| 799 | "--cgdb option can only be used with remote debugging." |
| 800 | ) |
| 801 | if options.pid: |
| 802 | parser.error( |
| 803 | "Must specify a remote device (--remote) if you want " |
| 804 | "to attach to a remote pid." |
| 805 | ) |
| 806 | if options.attach_name: |
| 807 | parser.error( |
| 808 | "Must specify remote device (--remote) when using" |
| 809 | " --attach option." |
| 810 | ) |
| 811 | if options.binary: |
| 812 | if not os.path.exists(options.binary): |
| 813 | parser.error("%s does not exist." % options.binary) |
cmtice | f23cb13 | 2015-04-10 15:13:00 -0700 | [diff] [blame] | 814 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 815 | # Once we've finished checking args, make sure we're root. |
| 816 | if not options.remote: |
| 817 | _ReExecuteIfNeeded([sys.argv[0]] + argv) |
| 818 | |
| 819 | gdb = BoardSpecificGdb( |
| 820 | options.board, |
| 821 | gdb_args, |
| 822 | inf_cmd, |
| 823 | inf_args, |
| 824 | options.remote, |
| 825 | options.pid, |
| 826 | options.attach_name, |
| 827 | options.cgdb, |
| 828 | options.ping, |
| 829 | options.binary, |
Marc Grimme | 5441aba | 2023-04-14 16:56:36 +0200 | [diff] [blame] | 830 | options.gdb, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 831 | ) |
| 832 | |
| 833 | try: |
| 834 | if options.remote: |
| 835 | gdb.RunRemote() |
| 836 | else: |
| 837 | gdb.Run() |
| 838 | |
| 839 | except GdbException as e: |
| 840 | if options.debug: |
| 841 | raise |
| 842 | else: |
Trent Apted | 593c074 | 2023-05-05 03:50:20 +0000 | [diff] [blame] | 843 | # TODO(b/236161656): Fix. |
| 844 | # pylint: disable-next=raising-bad-type |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 845 | raise cros_build_lib.Die(str(e)) |