Dump stack trace using lldb.
(lldb_path: str, pid: int, test_name: str)
| 84 | |
| 85 | |
| 86 | def _dump_with_lldb(lldb_path: str, pid: int, test_name: str) -> Optional[str]: |
| 87 | """Dump stack trace using lldb.""" |
| 88 | # Create lldb command script |
| 89 | with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".lldb") as f: |
| 90 | # Write lldb commands |
| 91 | f.write("# Attach to process and dump all thread stacks\n") |
| 92 | f.write("settings set target.process.stop-on-exec false\n") |
| 93 | f.write("settings set target.process.stop-on-sharedlibrary-events false\n") |
| 94 | f.write("settings set target.x86-disassembly-flavor intel\n") |
| 95 | f.write("\n") |
| 96 | f.write("# Get backtrace for all threads\n") |
| 97 | f.write("thread backtrace all\n") |
| 98 | f.write("\n") |
| 99 | f.write("# Show thread info\n") |
| 100 | f.write("thread list\n") |
| 101 | f.write("\n") |
| 102 | f.write("# Quit\n") |
| 103 | f.write("quit\n") |
| 104 | script_path = f.name |
| 105 | |
| 106 | try: |
| 107 | # Run lldb with the script |
| 108 | cmd = [ |
| 109 | lldb_path, |
| 110 | "--batch", |
| 111 | "--source", |
| 112 | script_path, |
| 113 | "--attach-pid", |
| 114 | str(pid), |
| 115 | ] |
| 116 | |
| 117 | ts_print(f"Running: {' '.join(cmd)}") |
| 118 | |
| 119 | result = RunningProcess.run( |
| 120 | cmd, |
| 121 | cwd=None, |
| 122 | check=False, |
| 123 | timeout=30, # 30 second timeout for debugger |
| 124 | ) |
| 125 | |
| 126 | output = result.stdout |
| 127 | |
| 128 | # Clean up script |
| 129 | os.unlink(script_path) |
| 130 | |
| 131 | if output and output.strip(): |
| 132 | return output |
| 133 | else: |
| 134 | return "Debugger attached but produced no output" |
| 135 | |
| 136 | except RuntimeError as e: |
| 137 | if "timeout" in str(e).lower(): |
| 138 | ts_print("⏱️ Debugger timed out after 30 seconds") |
| 139 | else: |
| 140 | ts_print(f"Debugger error: {e}") |
| 141 | try: |
| 142 | os.unlink(script_path) |
| 143 | except KeyboardInterrupt as ki: |
no test coverage detected