Runs gdb in --batch mode with the additional arguments given by *args. Returns its (stdout, stderr) decoded from utf-8 using the replace handler.
(*args, exitcode=0, check=True, **env_vars)
| 31 | GDB_VERSION = (0, 0) |
| 32 | |
| 33 | def run_gdb(*args, exitcode=0, check=True, **env_vars): |
| 34 | """Runs gdb in --batch mode with the additional arguments given by *args. |
| 35 | |
| 36 | Returns its (stdout, stderr) decoded from utf-8 using the replace handler. |
| 37 | """ |
| 38 | env = clean_environment() |
| 39 | if env_vars: |
| 40 | env.update(env_vars) |
| 41 | |
| 42 | cmd = [GDB_PROGRAM, |
| 43 | # Batch mode: Exit after processing all the command files |
| 44 | # specified with -x/--command |
| 45 | '--batch', |
| 46 | # -nx: Do not execute commands from any .gdbinit initialization |
| 47 | # files (gh-66384) |
| 48 | '-nx'] |
| 49 | if GDB_VERSION >= (7, 4): |
| 50 | cmd.extend(('--init-eval-command', |
| 51 | f'add-auto-load-safe-path {CHECKOUT_HOOK_PATH}')) |
| 52 | cmd.extend(args) |
| 53 | |
| 54 | proc = subprocess.run( |
| 55 | cmd, |
| 56 | # Redirect stdin to prevent gdb from messing with the terminal settings |
| 57 | stdin=subprocess.PIPE, |
| 58 | stdout=subprocess.PIPE, |
| 59 | stderr=subprocess.PIPE, |
| 60 | encoding="utf8", errors="backslashreplace", |
| 61 | env=env) |
| 62 | |
| 63 | stdout = proc.stdout |
| 64 | stderr = proc.stderr |
| 65 | if check and proc.returncode != exitcode: |
| 66 | cmd_text = shlex.join(cmd) |
| 67 | raise Exception(f"{cmd_text} failed with exit code {proc.returncode}, " |
| 68 | f"expected exit code {exitcode}:\n" |
| 69 | f"stdout={stdout!r}\n" |
| 70 | f"stderr={stderr!r}") |
| 71 | |
| 72 | return (stdout, stderr) |
| 73 | |
| 74 | |
| 75 | def get_gdb_version(): |
searching dependent graphs…