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 |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame^] | 110 | # depthcharge corresponds to the build_target in unified builds. For |
| 111 | # this reason we need to glob all boards to find the correct |
| 112 | # build_target. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 113 | unified_build_dirs = glob.glob("/build/*/firmware/%s" % board) |
| 114 | if len(unified_build_dirs) == 1: |
| 115 | firmware_dir = unified_build_dirs[0] |
| 116 | elif len(unified_build_dirs) > 1: |
| 117 | raise ValueError( |
| 118 | "Multiple boards were found (%s). Use -y to specify manually" |
| 119 | % (", ".join(unified_build_dirs)) |
| 120 | ) |
Raul E Rangel | eb8aadc | 2018-05-17 09:10:27 -0600 | [diff] [blame] | 121 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 122 | if not firmware_dir: |
| 123 | firmware_dir = os.path.join( |
| 124 | build_target_lib.get_default_sysroot_path(board), "firmware" |
| 125 | ) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 126 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 127 | # Very old firmware you might still find on GoldenEye had dev.ro.elf. |
| 128 | basenames = ["dev.elf", "dev.ro.elf"] |
| 129 | for basename in basenames: |
| 130 | path = os.path.join(firmware_dir, "depthcharge", basename) |
| 131 | if not os.path.exists(path): |
| 132 | path = os.path.join(firmware_dir, basename) |
| 133 | if os.path.exists(path): |
| 134 | logging.warning( |
| 135 | "Auto-detected symbol file at %s... make sure that this" |
| 136 | " matches the image on your DUT!", |
| 137 | path, |
| 138 | ) |
| 139 | return path |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 140 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 141 | raise ValueError( |
| 142 | "Could not find depthcharge symbol file (dev.elf)! " |
Julius Werner | 3b893e8 | 2023-03-06 18:33:37 -0800 | [diff] [blame] | 143 | "(You can use -y to supply it manually.)" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 144 | ) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 145 | |
| 146 | |
| 147 | # TODO(jwerner): Fine tune |wait| delay or maybe even make it configurable if |
| 148 | # this causes problems due to load on the host. The callers where this is |
| 149 | # critical should all have their own timeouts now, though, so it's questionable |
| 150 | # whether the delay here is even needed at all anymore. |
| 151 | def ReadAll(fd, wait=0.03): |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame^] | 152 | """Read from |fd| until no data has come for at least |wait| seconds.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 153 | data = [] |
| 154 | try: |
| 155 | while True: |
| 156 | time.sleep(wait) |
| 157 | new_data = os.read(fd, 4096) |
| 158 | if not new_data: |
| 159 | break |
| 160 | data.append(new_data) |
| 161 | except OSError as e: |
| 162 | if e.errno != errno.EAGAIN: |
| 163 | raise |
| 164 | data = b"".join(data) |
| 165 | if data: |
| 166 | logging.debug(data.decode("ascii", errors="replace")) |
| 167 | return data |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 168 | |
| 169 | |
| 170 | def GdbChecksum(message): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 171 | """Calculate a remote-GDB style checksum.""" |
| 172 | chksum = sum(message) |
| 173 | return (b"%.2x" % chksum)[-2:] |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 174 | |
| 175 | |
| 176 | def TestConnection(fd): |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame^] | 177 | """Return True if there's a responsive GDB stub on the other end of |fd|.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 178 | cmd = b"vUnknownCommand" |
| 179 | for _ in range(3): |
| 180 | os.write(fd, b"$%s#%s\n" % (cmd, GdbChecksum(cmd))) |
| 181 | reply = ReadAll(fd) |
| 182 | if b"+$#00" in reply: |
| 183 | os.write(fd, b"+") |
| 184 | logging.info( |
| 185 | "TestConnection: Could successfully connect to remote end." |
| 186 | ) |
| 187 | return True |
| 188 | logging.info("TestConnection: Remote end does not respond.") |
| 189 | return False |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 190 | |
| 191 | |
| 192 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 193 | opts = ParseArgs(argv) |
| 194 | servo = client.ServoClient(host=opts.host, port=opts.port) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 195 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 196 | if not opts.tty: |
| 197 | try: |
| 198 | opts.tty = servo.get("cpu_uart_pty") |
| 199 | except (client.ServoClientError, socket.error): |
| 200 | logging.error( |
| 201 | "Cannot auto-detect TTY file without servod. Use the --tty " |
| 202 | "option." |
| 203 | ) |
| 204 | raise |
| 205 | with terminal_freezer.TerminalFreezer(opts.tty): |
| 206 | fd = os.open(opts.tty, os.O_RDWR | os.O_NONBLOCK) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 207 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 208 | data = ReadAll(fd) |
| 209 | if opts.reboot == "auto": |
| 210 | if TestConnection(fd): |
| 211 | opts.reboot = "no" |
| 212 | else: |
| 213 | opts.reboot = "yes" |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 214 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 215 | if opts.reboot == "yes": |
| 216 | logging.info("Rebooting DUT...") |
| 217 | try: |
| 218 | servo.set("warm_reset", "on") |
| 219 | time.sleep(0.1) |
| 220 | servo.set("warm_reset", "off") |
| 221 | except (client.ServoClientError, socket.error): |
| 222 | logging.error( |
| 223 | "Cannot reboot without a Servo board. You have to boot " |
| 224 | "into developer mode and press CTRL+G manually before " |
| 225 | "running fwgdb." |
| 226 | ) |
| 227 | raise |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 228 | |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame^] | 229 | # Throw away old data to avoid confusion from messages before the |
| 230 | # reboot. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 231 | data = b"" |
| 232 | msg = "Could not reboot into depthcharge!" |
| 233 | with timeout_util.Timeout(10, msg): |
| 234 | while not re.search(_PTRN_BOARD, data): |
| 235 | data += ReadAll(fd) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 236 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 237 | msg = ( |
| 238 | "Could not enter GDB mode with CTRL+G! " |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame^] | 239 | '(Confirm that you flashed an "image.dev.bin" image to this ' |
| 240 | "DUT, and that you have GBB_FLAG_FORCE_DEV_SWITCH_ON (0x8) " |
| 241 | "set.)" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 242 | ) |
| 243 | with timeout_util.Timeout(5, msg): |
| 244 | while not re.search(_PTRN_GDB, data): |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame^] | 245 | # Some delay to avoid spamming the console too hard while |
| 246 | # not being long enough to cause a user-visible slowdown. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 247 | time.sleep(0.5) |
| 248 | # Send a CTRL+G to tell depthcharge to trap into GDB. |
| 249 | logging.debug("[Ctrl+G]") |
| 250 | os.write(fd, b"\x07") |
| 251 | data += ReadAll(fd) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 252 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 253 | if not opts.board: |
| 254 | matches = re.findall(_PTRN_BOARD, data) |
| 255 | if not matches: |
| 256 | raise ValueError( |
| 257 | "Could not auto-detect board! Please use -b option." |
| 258 | ) |
Julius Werner | 3b893e8 | 2023-03-06 18:33:37 -0800 | [diff] [blame] | 259 | opts.board = matches[-1].decode().lower() |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 260 | logging.info( |
| 261 | "Auto-detected board as %s from DUT console output.", opts.board |
| 262 | ) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 263 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 264 | if not TestConnection(fd): |
| 265 | raise IOError( |
| 266 | "Could not connect to remote end! Confirm that your DUT is " |
| 267 | "running in GDB mode on %s." % opts.tty |
| 268 | ) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 269 | |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame^] | 270 | # Eat up leftover data or it will spill back to terminal. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 271 | ReadAll(fd) |
| 272 | os.close(fd) |
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 | opts.execute.insert(0, "target remote %s" % opts.tty) |
| 275 | ex_args = sum([["--ex", cmd] for cmd in opts.execute], []) |
Julius Werner | 634ccac | 2014-06-24 13:59:18 -0700 | [diff] [blame] | 276 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 277 | elf = FindSymbols(opts.symbols, opts.board) |
| 278 | gdb_cmd = GetGdbForElf(elf) |
Raul E Rangel | 72f3271 | 2018-05-29 11:42:59 -0600 | [diff] [blame] | 279 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 280 | gdb_args = [ |
| 281 | "--symbols", |
| 282 | elf, |
| 283 | "--directory", |
| 284 | _SRC_DC, |
| 285 | "--directory", |
| 286 | _SRC_VB, |
| 287 | "--directory", |
| 288 | _SRC_LP, |
| 289 | ] + ex_args |
Raul E Rangel | 72f3271 | 2018-05-29 11:42:59 -0600 | [diff] [blame] | 290 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 291 | if opts.cgdb: |
| 292 | full_cmd = ["cgdb", "-d", gdb_cmd, "--"] + gdb_args |
| 293 | else: |
| 294 | full_cmd = [gdb_cmd] + gdb_args |
Raul E Rangel | 72f3271 | 2018-05-29 11:42:59 -0600 | [diff] [blame] | 295 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 296 | logging.info("Launching GDB...") |
| 297 | cros_build_lib.run( |
| 298 | full_cmd, ignore_sigint=True, debug_level=logging.WARNING |
| 299 | ) |