Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame^] | 1 | # Copyright 2014 The ChromiumOS Authors |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [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 | """Connect to a DUT in firmware via remote GDB, install custom GDB commands.""" |
| 6 | |
| 7 | import errno |
Raul E Rangel | eb8aadc | 2018-05-17 09:10:27 -0600 | [diff] [blame] | 8 | import glob |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 9 | import logging |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 10 | import os |
| 11 | import re |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 12 | import socket |
| 13 | import time |
| 14 | |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 15 | from chromite.third_party.pyelftools.elftools.elf.elffile import ELFFile |
| 16 | |
Mike Frysinger | 06a51c8 | 2021-04-06 11:39:17 -0400 | [diff] [blame] | 17 | from chromite.lib import build_target_lib |
Aviv Keshet | b7519e1 | 2016-10-04 00:50:00 -0700 | [diff] [blame] | 18 | from chromite.lib import constants |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 19 | from chromite.lib import cros_build_lib |
| 20 | from chromite.lib import timeout_util |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 21 | |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 22 | |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 23 | # Need to do this before Servo import |
| 24 | cros_build_lib.AssertInsideChroot() |
| 25 | |
Mike Frysinger | 92bdef5 | 2019-08-21 21:05:13 -0400 | [diff] [blame] | 26 | # pylint: disable=import-error,wrong-import-position |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 27 | from servo import client |
Ruben Rodriguez Buchillon | 15bdd45 | 2018-10-09 08:31:41 +0800 | [diff] [blame] | 28 | from servo import servo_parsing |
Aseda Aboagye | 6e21d3f | 2016-10-21 11:37:56 -0700 | [diff] [blame] | 29 | from servo import terminal_freezer |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 30 | |
| 31 | |
Mike Frysinger | 92bdef5 | 2019-08-21 21:05:13 -0400 | [diff] [blame] | 32 | # pylint: enable=import-error,wrong-import-position |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 33 | |
Ralph Nathan | 91874ca | 2015-03-19 13:29:41 -0700 | [diff] [blame] | 34 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 35 | _SRC_ROOT = os.path.join(constants.CHROOT_SOURCE_ROOT, "src") |
| 36 | _SRC_DC = os.path.join(_SRC_ROOT, "platform/depthcharge") |
| 37 | _SRC_VB = os.path.join(_SRC_ROOT, "platform/vboot_reference") |
| 38 | _SRC_LP = os.path.join(_SRC_ROOT, "third_party/coreboot/payloads/libpayload") |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 39 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 40 | _PTRN_GDB = b"Ready for GDB connection" |
Douglas Anderson | 5a1056b | 2021-08-26 14:10:08 -0700 | [diff] [blame] | 41 | _PTRN_BOARD = ( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 42 | b"Starting(?: read-only| read/write)? depthcharge on ([A-Za-z_]+)..." |
| 43 | ) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 44 | |
| 45 | |
Julius Werner | 149fe78 | 2018-10-10 15:18:14 -0700 | [diff] [blame] | 46 | def GetGdbForElf(elf): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | """Return the correct C compiler prefix for the target ELF file.""" |
| 48 | with open(elf, "rb") as fp: |
| 49 | return { |
| 50 | "EM_386": "x86_64-cros-linux-gnu-gdb", |
| 51 | "EM_X86_64": "x86_64-cros-linux-gnu-gdb", |
| 52 | "EM_ARM": "armv7a-cros-linux-gnueabihf-gdb", |
| 53 | "EM_AARCH64": "aarch64-cros-linux-gnu-gdb", |
| 54 | }[ELFFile(fp).header.e_machine] |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 55 | |
| 56 | |
| 57 | def ParseArgs(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 58 | """Parse and validate command line arguments.""" |
| 59 | description = "Debug depthcharge using GDB" |
| 60 | parser = servo_parsing.ServodClientParser(description=description) |
| 61 | parser.add_argument( |
| 62 | "-b", "--board", help="The board overlay name (auto-detect by default)" |
| 63 | ) |
| 64 | parser.add_argument( |
| 65 | "-c", |
| 66 | "--cgdb", |
| 67 | action="store_true", |
| 68 | help="Use cgdb curses interface rather than plain gdb", |
| 69 | ) |
| 70 | parser.add_argument( |
| 71 | "-y", |
| 72 | "--symbols", |
| 73 | help="Root directory or complete path to symbolized ELF " |
| 74 | "(defaults to /build/<BOARD>/firmware)", |
| 75 | ) |
| 76 | parser.add_argument( |
| 77 | "-r", |
| 78 | "--reboot", |
| 79 | choices=["yes", "no", "auto"], |
| 80 | help="Reboot the DUT before connect (default: reboot if " |
| 81 | "the remote and is unreachable)", |
| 82 | default="auto", |
| 83 | ) |
| 84 | parser.add_argument( |
| 85 | "-e", |
| 86 | "--execute", |
| 87 | action="append", |
| 88 | default=[], |
| 89 | help="GDB command to run after connect (can be supplied " |
| 90 | "multiple times)", |
| 91 | ) |
| 92 | parser.add_argument( |
| 93 | "-t", "--tty", help="TTY file to connect to (defaults to cpu_uart_pty)" |
| 94 | ) |
| 95 | opts = parser.parse_args(argv) |
Ruben Rodriguez Buchillon | d7c65bd | 2018-12-10 10:10:19 +0800 | [diff] [blame] | 96 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 97 | return opts |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 98 | |
| 99 | |
Julius Werner | 193c914 | 2018-05-09 16:25:38 -0700 | [diff] [blame] | 100 | def FindSymbols(firmware_dir, board): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 101 | """Find the symbolized depthcharge ELF (may be supplied by -y flag).""" |
Raul E Rangel | eb8aadc | 2018-05-17 09:10:27 -0600 | [diff] [blame] | 102 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 103 | # Allow overriding the file directly just in case our detection screws up. |
| 104 | if firmware_dir and firmware_dir.endswith(".elf"): |
| 105 | return firmware_dir |
Raul E Rangel | eb8aadc | 2018-05-17 09:10:27 -0600 | [diff] [blame] | 106 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 107 | if not firmware_dir: |
| 108 | # Unified builds have the format |
| 109 | # /build/<board|family>/firmware/<build_target|model>/. The board in |
| 110 | # depthcharge corresponds to the build_target in unified builds. For this |
| 111 | # reason we need to glob all boards to find the correct build_target. |
| 112 | unified_build_dirs = glob.glob("/build/*/firmware/%s" % board) |
| 113 | if len(unified_build_dirs) == 1: |
| 114 | firmware_dir = unified_build_dirs[0] |
| 115 | elif len(unified_build_dirs) > 1: |
| 116 | raise ValueError( |
| 117 | "Multiple boards were found (%s). Use -y to specify manually" |
| 118 | % (", ".join(unified_build_dirs)) |
| 119 | ) |
Raul E Rangel | eb8aadc | 2018-05-17 09:10:27 -0600 | [diff] [blame] | 120 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 121 | if not firmware_dir: |
| 122 | firmware_dir = os.path.join( |
| 123 | build_target_lib.get_default_sysroot_path(board), "firmware" |
| 124 | ) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 125 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 126 | # Very old firmware you might still find on GoldenEye had dev.ro.elf. |
| 127 | basenames = ["dev.elf", "dev.ro.elf"] |
| 128 | for basename in basenames: |
| 129 | path = os.path.join(firmware_dir, "depthcharge", basename) |
| 130 | if not os.path.exists(path): |
| 131 | path = os.path.join(firmware_dir, basename) |
| 132 | if os.path.exists(path): |
| 133 | logging.warning( |
| 134 | "Auto-detected symbol file at %s... make sure that this" |
| 135 | " matches the image on your DUT!", |
| 136 | path, |
| 137 | ) |
| 138 | return path |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 139 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 140 | raise ValueError( |
| 141 | "Could not find depthcharge symbol file (dev.elf)! " |
| 142 | "(You can use -s to supply it manually.)" |
| 143 | ) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 144 | |
| 145 | |
| 146 | # TODO(jwerner): Fine tune |wait| delay or maybe even make it configurable if |
| 147 | # this causes problems due to load on the host. The callers where this is |
| 148 | # critical should all have their own timeouts now, though, so it's questionable |
| 149 | # whether the delay here is even needed at all anymore. |
| 150 | def ReadAll(fd, wait=0.03): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 151 | """Read from |fd| until no more data has come for at least |wait| seconds.""" |
| 152 | data = [] |
| 153 | try: |
| 154 | while True: |
| 155 | time.sleep(wait) |
| 156 | new_data = os.read(fd, 4096) |
| 157 | if not new_data: |
| 158 | break |
| 159 | data.append(new_data) |
| 160 | except OSError as e: |
| 161 | if e.errno != errno.EAGAIN: |
| 162 | raise |
| 163 | data = b"".join(data) |
| 164 | if data: |
| 165 | logging.debug(data.decode("ascii", errors="replace")) |
| 166 | return data |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 167 | |
| 168 | |
| 169 | def GdbChecksum(message): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 170 | """Calculate a remote-GDB style checksum.""" |
| 171 | chksum = sum(message) |
| 172 | return (b"%.2x" % chksum)[-2:] |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 173 | |
| 174 | |
| 175 | def TestConnection(fd): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 176 | """Return True iff there is a resposive GDB stub on the other end of 'fd'.""" |
| 177 | cmd = b"vUnknownCommand" |
| 178 | for _ in range(3): |
| 179 | os.write(fd, b"$%s#%s\n" % (cmd, GdbChecksum(cmd))) |
| 180 | reply = ReadAll(fd) |
| 181 | if b"+$#00" in reply: |
| 182 | os.write(fd, b"+") |
| 183 | logging.info( |
| 184 | "TestConnection: Could successfully connect to remote end." |
| 185 | ) |
| 186 | return True |
| 187 | logging.info("TestConnection: Remote end does not respond.") |
| 188 | return False |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 189 | |
| 190 | |
| 191 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 192 | opts = ParseArgs(argv) |
| 193 | servo = client.ServoClient(host=opts.host, port=opts.port) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 194 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 195 | if not opts.tty: |
| 196 | try: |
| 197 | opts.tty = servo.get("cpu_uart_pty") |
| 198 | except (client.ServoClientError, socket.error): |
| 199 | logging.error( |
| 200 | "Cannot auto-detect TTY file without servod. Use the --tty " |
| 201 | "option." |
| 202 | ) |
| 203 | raise |
| 204 | with terminal_freezer.TerminalFreezer(opts.tty): |
| 205 | fd = os.open(opts.tty, os.O_RDWR | os.O_NONBLOCK) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 206 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 207 | data = ReadAll(fd) |
| 208 | if opts.reboot == "auto": |
| 209 | if TestConnection(fd): |
| 210 | opts.reboot = "no" |
| 211 | else: |
| 212 | opts.reboot = "yes" |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 213 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 214 | if opts.reboot == "yes": |
| 215 | logging.info("Rebooting DUT...") |
| 216 | try: |
| 217 | servo.set("warm_reset", "on") |
| 218 | time.sleep(0.1) |
| 219 | servo.set("warm_reset", "off") |
| 220 | except (client.ServoClientError, socket.error): |
| 221 | logging.error( |
| 222 | "Cannot reboot without a Servo board. You have to boot " |
| 223 | "into developer mode and press CTRL+G manually before " |
| 224 | "running fwgdb." |
| 225 | ) |
| 226 | raise |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 227 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 228 | # Throw away old data to avoid confusion from messages before the reboot |
| 229 | data = b"" |
| 230 | msg = "Could not reboot into depthcharge!" |
| 231 | with timeout_util.Timeout(10, msg): |
| 232 | while not re.search(_PTRN_BOARD, data): |
| 233 | data += ReadAll(fd) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 234 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 235 | msg = ( |
| 236 | "Could not enter GDB mode with CTRL+G! " |
| 237 | '(Confirm that you flashed an "image.dev.bin" image to this DUT, ' |
| 238 | "and that you have GBB_FLAG_FORCE_DEV_SWITCH_ON (0x8) set.)" |
| 239 | ) |
| 240 | with timeout_util.Timeout(5, msg): |
| 241 | while not re.search(_PTRN_GDB, data): |
| 242 | # Some delay to avoid spamming the console too hard while not being |
| 243 | # long enough to cause a user-visible slowdown. |
| 244 | time.sleep(0.5) |
| 245 | # Send a CTRL+G to tell depthcharge to trap into GDB. |
| 246 | logging.debug("[Ctrl+G]") |
| 247 | os.write(fd, b"\x07") |
| 248 | data += ReadAll(fd) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 249 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 250 | if not opts.board: |
| 251 | matches = re.findall(_PTRN_BOARD, data) |
| 252 | if not matches: |
| 253 | raise ValueError( |
| 254 | "Could not auto-detect board! Please use -b option." |
| 255 | ) |
| 256 | opts.board = matches[-1] |
| 257 | logging.info( |
| 258 | "Auto-detected board as %s from DUT console output.", opts.board |
| 259 | ) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 260 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 261 | if not TestConnection(fd): |
| 262 | raise IOError( |
| 263 | "Could not connect to remote end! Confirm that your DUT is " |
| 264 | "running in GDB mode on %s." % opts.tty |
| 265 | ) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 266 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 267 | # Eat up leftover data or it will spill back to terminal |
| 268 | ReadAll(fd) |
| 269 | os.close(fd) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 270 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 271 | opts.execute.insert(0, "target remote %s" % opts.tty) |
| 272 | ex_args = sum([["--ex", cmd] for cmd in opts.execute], []) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 273 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 274 | elf = FindSymbols(opts.symbols, opts.board) |
| 275 | gdb_cmd = GetGdbForElf(elf) |
Raul E Rangel | 72f3271 | 2018-05-29 11:42:59 -0600 | [diff] [blame] | 276 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 277 | gdb_args = [ |
| 278 | "--symbols", |
| 279 | elf, |
| 280 | "--directory", |
| 281 | _SRC_DC, |
| 282 | "--directory", |
| 283 | _SRC_VB, |
| 284 | "--directory", |
| 285 | _SRC_LP, |
| 286 | ] + ex_args |
Raul E Rangel | 72f3271 | 2018-05-29 11:42:59 -0600 | [diff] [blame] | 287 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 288 | if opts.cgdb: |
| 289 | full_cmd = ["cgdb", "-d", gdb_cmd, "--"] + gdb_args |
| 290 | else: |
| 291 | full_cmd = [gdb_cmd] + gdb_args |
Raul E Rangel | 72f3271 | 2018-05-29 11:42:59 -0600 | [diff] [blame] | 292 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 293 | logging.info("Launching GDB...") |
| 294 | cros_build_lib.run( |
| 295 | full_cmd, ignore_sigint=True, debug_level=logging.WARNING |
| 296 | ) |