| 1 |
"""Test SBThread.GetExtendedBacktraceThread API with queue debugging.""" |
| 2 |
|
| 3 |
import os |
| 4 |
import lldb |
| 5 |
from lldbsuite.test.decorators import * |
| 6 |
from lldbsuite.test.lldbtest import * |
| 7 |
from lldbsuite.test import lldbutil |
| 8 |
|
| 9 |
|
| 10 |
class TestExtendedBacktraceAPI(TestBase): |
| 11 |
NO_DEBUG_INFO_TESTCASE = True |
| 12 |
|
| 13 |
def setUp(self): |
| 14 |
TestBase.setUp(self) |
| 15 |
self.main_source = "main.m" |
| 16 |
|
| 17 |
@skipUnlessDarwin |
| 18 |
@add_test_categories(["objc", "pyapi"]) |
| 19 |
def test_extended_backtrace_thread_api(self): |
| 20 |
"""Test GetExtendedBacktraceThread with queue debugging.""" |
| 21 |
self.build() |
| 22 |
exe = self.getBuildArtifact("a.out") |
| 23 |
|
| 24 |
# Get Xcode developer directory path. |
| 25 |
# Try DEVELOPER_DIR environment variable first, then fall back to xcode-select. |
| 26 |
xcode_dev_path = os.environ.get("DEVELOPER_DIR") |
| 27 |
|
| 28 |
if not xcode_dev_path: |
| 29 |
import subprocess |
| 30 |
|
| 31 |
xcode_dev_path = ( |
| 32 |
subprocess.check_output(["xcode-select", "-p"]).decode("utf-8").strip() |
| 33 |
) |
| 34 |
|
| 35 |
# Check for libBacktraceRecording.dylib. |
| 36 |
libbtr_path = os.path.join( |
| 37 |
xcode_dev_path, "usr/lib/libBacktraceRecording.dylib" |
| 38 |
) |
| 39 |
|
| 40 |
self.assertTrue( |
| 41 |
os.path.isfile(libbtr_path), |
| 42 |
f"libBacktraceRecording.dylib is not present at {libbtr_path}", |
| 43 |
) |
| 44 |
|
| 45 |
self.assertTrue( |
| 46 |
os.path.isfile("/usr/lib/system/introspection/libdispatch.dylib"), |
| 47 |
"introspection libdispatch dylib not installed.", |
| 48 |
) |
| 49 |
|
| 50 |
# Create launch info with environment variables for libBacktraceRecording. |
| 51 |
launch_info = lldb.SBLaunchInfo(None) |
| 52 |
launch_info.SetWorkingDirectory(self.get_process_working_directory()) |
| 53 |
launch_info.SetEnvironmentEntries( |
| 54 |
[ |
| 55 |
f"DYLD_INSERT_LIBRARIES={libbtr_path}", |
| 56 |
"DYLD_LIBRARY_PATH=/usr/lib/system/introspection", |
| 57 |
], |
| 58 |
True, |
| 59 |
) |
| 60 |
|
| 61 |
# Launch the process and run to breakpoint. |
| 62 |
target, process, thread, bp = lldbutil.run_to_name_breakpoint( |
| 63 |
self, "do_work_level_5", launch_info=launch_info, bkpt_module="a.out" |
| 64 |
) |
| 65 |
|
| 66 |
self.assertTrue(target.IsValid(), VALID_TARGET) |
| 67 |
self.assertTrue(process.IsValid(), PROCESS_IS_VALID) |
| 68 |
self.assertTrue(thread.IsValid(), "Stopped thread is valid") |
| 69 |
self.assertTrue(bp.IsValid(), VALID_BREAKPOINT) |
| 70 |
|
| 71 |
# Call GetNumQueues to ensure queue information is loaded. |
| 72 |
num_queues = process.GetNumQueues() |
| 73 |
|
| 74 |
# Check that we can find the com.apple.main-thread queue. |
| 75 |
main_thread_queue_found = False |
| 76 |
for i in range(num_queues): |
| 77 |
queue = process.GetQueueAtIndex(i) |
| 78 |
if queue.GetName() == "com.apple.main-thread": |
| 79 |
main_thread_queue_found = True |
| 80 |
break |
| 81 |
|
| 82 |
# Verify we have at least 5 frames. |
| 83 |
self.assertGreaterEqual( |
| 84 |
thread.GetNumFrames(), |
| 85 |
5, |
| 86 |
"Thread should have at least 5 frames in backtrace", |
| 87 |
) |
| 88 |
|
| 89 |
# Get frame 2 BEFORE calling GetExtendedBacktraceThread. |
| 90 |
# This mimics what Xcode does - it has the frame objects ready. |
| 91 |
frame2 = thread.GetFrameAtIndex(2) |
| 92 |
self.assertTrue(frame2.IsValid(), "Frame 2 is valid") |
| 93 |
|
| 94 |
# Now test GetExtendedBacktraceThread. |
| 95 |
# This is the critical part - getting the extended backtrace calls into |
| 96 |
# libBacktraceRecording which does an inferior function call, and this |
| 97 |
# invalidates/clears the unwinder state. |
| 98 |
extended_thread = thread.GetExtendedBacktraceThread("libdispatch") |
| 99 |
|
| 100 |
# This should be valid since we injected libBacktraceRecording. |
| 101 |
self.assertTrue( |
| 102 |
extended_thread.IsValid(), |
| 103 |
"Extended backtrace thread for 'libdispatch' should be valid with libBacktraceRecording loaded", |
| 104 |
) |
| 105 |
|
| 106 |
# The extended thread should have frames. |
| 107 |
self.assertGreater( |
| 108 |
extended_thread.GetNumFrames(), |
| 109 |
0, |
| 110 |
"Extended backtrace thread should have at least one frame", |
| 111 |
) |
| 112 |
|
| 113 |
# Test frame 2 on the extended backtrace thread. |
| 114 |
self.assertGreater( |
| 115 |
extended_thread.GetNumFrames(), |
| 116 |
2, |
| 117 |
"Extended backtrace thread should have at least 3 frames to access frame 2", |
| 118 |
) |
| 119 |
|
| 120 |
extended_frame2 = extended_thread.GetFrameAtIndex(2) |
| 121 |
self.assertTrue(extended_frame2.IsValid(), "Extended thread frame 2 is valid") |
| 122 |
|
| 123 |
# NOW try to access variables from frame 2 of the ORIGINAL thread. |
| 124 |
# This is the key test - after GetExtendedBacktraceThread() has executed |
| 125 |
# an inferior function call, the unwinder state may be invalidated. |
| 126 |
# Xcode exhibits this bug where variables show "register fp is not available" |
| 127 |
# after extended backtrace retrieval. |
| 128 |
|
| 129 |
# Set frame 2 as the selected frame so expect_var_path works. |
| 130 |
thread.SetSelectedFrame(2) |
| 131 |
|
| 132 |
variables = frame2.GetVariables(False, True, False, True) |
| 133 |
self.assertGreater( |
| 134 |
variables.GetSize(), 0, "Frame 2 should have at least one variable" |
| 135 |
) |
| 136 |
|
| 137 |
# Test all variables in frame 2, like Xcode does. |
| 138 |
# Use expect_var_path to verify each variable is accessible without errors. |
| 139 |
for i in range(variables.GetSize()): |
| 140 |
var = variables.GetValueAtIndex(i) |
| 141 |
var_name = var.GetName() |
| 142 |
|
| 143 |
# This will fail if the variable contains "not available" or has errors. |
| 144 |
self.expect_var_path(var_name) |