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