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