1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
|
"""
Test scripted frame provider functionality.
"""
import os
import lldb
import lldbsuite.test.lldbplatformutil as lldbplatformutil
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import TestBase
from lldbsuite.test import lldbutil
@skipIf(oslist=["linux"], archs=["arm$"])
class ScriptedFrameProviderTestCase(TestBase):
NO_DEBUG_INFO_TESTCASE = True
def setUp(self):
TestBase.setUp(self)
self.source = "main.cpp"
def test_replace_all_frames(self):
"""Test that we can replace the entire stack."""
self.build()
target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
self, "Break here", lldb.SBFileSpec(self.source), only_one_thread=False
)
# Import the test frame provider.
script_path = os.path.join(self.getSourceDir(), "test_frame_providers.py")
self.runCmd("command script import " + script_path)
# Attach the Replace provider.
error = lldb.SBError()
provider_id = target.RegisterScriptedFrameProvider(
"test_frame_providers.ReplaceFrameProvider",
lldb.SBStructuredData(),
error,
)
self.assertTrue(error.Success(), f"Failed to register provider: {error}")
self.assertNotEqual(provider_id, 0, "Provider ID should be non-zero")
# Verify we have exactly 3 synthetic frames.
self.assertEqual(thread.GetNumFrames(), 3, "Should have 3 synthetic frames")
# Verify frame indices and PCs (dictionary-based frames don't have custom function names).
frame0 = thread.GetFrameAtIndex(0)
self.assertIsNotNone(frame0)
self.assertEqual(frame0.GetPC(), 0x1000)
frame1 = thread.GetFrameAtIndex(1)
self.assertIsNotNone(frame1)
self.assertIn("thread_func", frame1.GetFunctionName())
frame2 = thread.GetFrameAtIndex(2)
self.assertIsNotNone(frame2)
self.assertEqual(frame2.GetPC(), 0x3000)
def test_prepend_frames(self):
"""Test that we can add frames before real stack."""
self.build()
target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
self, "Break here", lldb.SBFileSpec(self.source), only_one_thread=False
)
# Get original frame count and PC.
original_frame_count = thread.GetNumFrames()
self.assertGreaterEqual(
original_frame_count, 2, "Should have at least 2 real frames"
)
# Import and attach Prepend provider.
script_path = os.path.join(self.getSourceDir(), "test_frame_providers.py")
self.runCmd("command script import " + script_path)
error = lldb.SBError()
provider_id = target.RegisterScriptedFrameProvider(
"test_frame_providers.PrependFrameProvider",
lldb.SBStructuredData(),
error,
)
self.assertTrue(error.Success(), f"Failed to register provider: {error}")
self.assertNotEqual(provider_id, 0, "Provider ID should be non-zero")
# Verify we have 2 more frames.
new_frame_count = thread.GetNumFrames()
self.assertEqual(new_frame_count, original_frame_count + 2)
# Verify first 2 frames are synthetic (check PCs, not function names).
frame0 = thread.GetFrameAtIndex(0)
self.assertEqual(frame0.GetPC(), 0x9000)
frame1 = thread.GetFrameAtIndex(1)
self.assertEqual(frame1.GetPC(), 0xA000)
# Verify frame 2 is the original real frame 0.
frame2 = thread.GetFrameAtIndex(2)
self.assertIn("thread_func", frame2.GetFunctionName())
def test_append_frames(self):
"""Test that we can add frames after real stack."""
self.build()
target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
self, "Break here", lldb.SBFileSpec(self.source), only_one_thread=False
)
# Get original frame count.
original_frame_count = thread.GetNumFrames()
# Import and attach Append provider.
script_path = os.path.join(self.getSourceDir(), "test_frame_providers.py")
self.runCmd("command script import " + script_path)
error = lldb.SBError()
provider_id = target.RegisterScriptedFrameProvider(
"test_frame_providers.AppendFrameProvider",
lldb.SBStructuredData(),
error,
)
self.assertTrue(error.Success(), f"Failed to register provider: {error}")
self.assertNotEqual(provider_id, 0, "Provider ID should be non-zero")
# Verify we have 1 more frame.
new_frame_count = thread.GetNumFrames()
self.assertEqual(new_frame_count, original_frame_count + 1)
# Verify first frames are still real.
frame0 = thread.GetFrameAtIndex(0)
self.assertIn("thread_func", frame0.GetFunctionName())
frame_n_plus_1 = thread.GetFrameAtIndex(new_frame_count - 1)
self.assertEqual(frame_n_plus_1.GetPC(), 0x10)
def test_scripted_frame_objects(self):
"""Test that provider can return ScriptedFrame objects."""
self.build()
target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
self, "Break here", lldb.SBFileSpec(self.source), only_one_thread=False
)
# Import the provider that returns ScriptedFrame objects.
script_path = os.path.join(self.getSourceDir(), "test_frame_providers.py")
self.runCmd("command script import " + script_path)
error = lldb.SBError()
provider_id = target.RegisterScriptedFrameProvider(
"test_frame_providers.ScriptedFrameObjectProvider",
lldb.SBStructuredData(),
error,
)
self.assertTrue(error.Success(), f"Failed to register provider: {error}")
self.assertNotEqual(provider_id, 0, "Provider ID should be non-zero")
# Verify we have 5 frames.
self.assertEqual(
thread.GetNumFrames(), 5, "Should have 5 custom scripted frames"
)
# Verify frame properties from CustomScriptedFrame.
frame0 = thread.GetFrameAtIndex(0)
self.assertIsNotNone(frame0)
self.assertEqual(frame0.GetFunctionName(), "custom_scripted_frame_0")
self.assertEqual(frame0.GetPC(), 0x5000)
self.assertTrue(frame0.IsSynthetic(), "Frame should be marked as synthetic")
frame1 = thread.GetFrameAtIndex(1)
self.assertIsNotNone(frame1)
self.assertEqual(frame1.GetPC(), 0x6000)
frame2 = thread.GetFrameAtIndex(2)
self.assertIsNotNone(frame2)
self.assertEqual(frame2.GetFunctionName(), "custom_scripted_frame_2")
self.assertEqual(frame2.GetPC(), 0x7000)
self.assertTrue(frame2.IsSynthetic(), "Frame should be marked as synthetic")
def test_applies_to_thread(self):
"""Test that applies_to_thread filters which threads get the provider."""
self.build()
target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
self, "Break here", lldb.SBFileSpec(self.source), only_one_thread=False
)
# We should have at least 2 threads (worker threads) at the breakpoint.
num_threads = process.GetNumThreads()
self.assertGreaterEqual(
num_threads, 2, "Should have at least 2 threads at breakpoint"
)
# Import the test frame provider.
script_path = os.path.join(self.getSourceDir(), "test_frame_providers.py")
self.runCmd("command script import " + script_path)
# Collect original thread info before applying provider.
thread_info = {}
for i in range(num_threads):
t = process.GetThreadAtIndex(i)
thread_info[t.GetIndexID()] = {
"frame_count": t.GetNumFrames(),
"pc": t.GetFrameAtIndex(0).GetPC(),
}
# Register the ThreadFilterFrameProvider which only applies to thread ID 1.
error = lldb.SBError()
provider_id = target.RegisterScriptedFrameProvider(
"test_frame_providers.ThreadFilterFrameProvider",
lldb.SBStructuredData(),
error,
)
self.assertTrue(error.Success(), f"Failed to register provider: {error}")
self.assertNotEqual(provider_id, 0, "Provider ID should be non-zero")
# Check each thread.
thread_id_1_found = False
# On ARM32, FixCodeAddress clears bit 0, so synthetic PCs get modified.
is_arm_32bit = lldbplatformutil.getArchitecture() == "arm"
expected_synthetic_pc = 0xFFFE if is_arm_32bit else 0xFFFF
for i in range(num_threads):
t = process.GetThreadAtIndex(i)
thread_id = t.GetIndexID()
if thread_id == 1:
# Thread with ID 1 should have synthetic frame.
thread_id_1_found = True
self.assertEqual(
t.GetNumFrames(),
1,
f"Thread with ID 1 should have 1 synthetic frame",
)
self.assertEqual(
t.GetFrameAtIndex(0).GetPC(),
expected_synthetic_pc,
f"Thread with ID 1 should have synthetic PC {expected_synthetic_pc:#x}",
)
else:
# Other threads should keep their original frames.
self.assertEqual(
t.GetNumFrames(),
thread_info[thread_id]["frame_count"],
f"Thread with ID {thread_id} should not be affected by provider",
)
self.assertEqual(
t.GetFrameAtIndex(0).GetPC(),
thread_info[thread_id]["pc"],
f"Thread with ID {thread_id} should have its original PC",
)
# We should have found at least one thread with ID 1.
self.assertTrue(
thread_id_1_found,
"Should have found a thread with ID 1 to test filtering",
)
def test_remove_frame_provider_by_id(self):
"""Test that RemoveScriptedFrameProvider removes a specific provider by ID."""
self.build()
target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
self, "Break here", lldb.SBFileSpec(self.source), only_one_thread=False
)
# Import the test frame providers.
script_path = os.path.join(self.getSourceDir(), "test_frame_providers.py")
self.runCmd("command script import " + script_path)
# Get original frame count.
original_frame_count = thread.GetNumFrames()
original_pc = thread.GetFrameAtIndex(0).GetPC()
# Register the first provider and get its ID.
error = lldb.SBError()
provider_id_1 = target.RegisterScriptedFrameProvider(
"test_frame_providers.ReplaceFrameProvider",
lldb.SBStructuredData(),
error,
)
self.assertTrue(error.Success(), f"Failed to register provider 1: {error}")
# Verify first provider is active (3 synthetic frames).
self.assertEqual(thread.GetNumFrames(), 3, "Should have 3 synthetic frames")
self.assertEqual(
thread.GetFrameAtIndex(0).GetPC(), 0x1000, "Should have first provider's PC"
)
# Register a second provider and get its ID.
provider_id_2 = target.RegisterScriptedFrameProvider(
"test_frame_providers.PrependFrameProvider",
lldb.SBStructuredData(),
error,
)
self.assertTrue(error.Success(), f"Failed to register provider 2: {error}")
# Verify IDs are different
self.assertNotEqual(
provider_id_1, provider_id_2, "Provider IDs should be unique"
)
# Now remove the first provider by ID
result = target.RemoveScriptedFrameProvider(provider_id_1)
self.assertSuccess(
result, f"Should successfully remove provider with ID {provider_id_1}"
)
# After removing the first provider, the second provider should still be
# active. The PrependFrameProvider adds 2 frames before the real stack.
# Since ReplaceFrameProvider had 3 frames, and we removed it, we should now
# have the original frames (from real stack) with PrependFrameProvider applied.
new_frame_count = thread.GetNumFrames()
self.assertEqual(
new_frame_count,
original_frame_count + 2,
"Should have original frames + 2 prepended frames",
)
# First two frames should be from PrependFrameProvider.
self.assertEqual(
thread.GetFrameAtIndex(0).GetPC(),
0x9000,
"First frame should be from PrependFrameProvider",
)
self.assertEqual(
thread.GetFrameAtIndex(1).GetPC(),
0xA000,
"Second frame should be from PrependFrameProvider",
)
# Remove the second provider.
result = target.RemoveScriptedFrameProvider(provider_id_2)
self.assertSuccess(
result, f"Should successfully remove provider with ID {provider_id_2}"
)
# After removing both providers, frames should be back to original.
self.assertEqual(
thread.GetNumFrames(),
original_frame_count,
"Should restore original frame count",
)
self.assertEqual(
thread.GetFrameAtIndex(0).GetPC(),
original_pc,
"Should restore original PC",
)
# Try to remove a provider that doesn't exist.
result = target.RemoveScriptedFrameProvider(999999)
self.assertTrue(result.Fail(), "Should fail to remove non-existent provider")
def test_circular_dependency_fix(self):
"""Test that accessing input_frames in __init__ doesn't cause circular dependency.
This test verifies the fix for the circular dependency issue where:
1. Thread::GetStackFrameList() creates the frame provider
2. Provider's __init__ accesses input_frames and calls methods on frames
3. SBFrame methods trigger ExecutionContextRef::GetFrameSP()
4. Before the fix: GetFrameSP() would call Thread::GetStackFrameList() again -> circular dependency!
5. After the fix: GetFrameSP() uses the remembered frame list -> no circular dependency
The fix works by:
- StackFrame stores m_frame_list_wp (weak pointer to originating list)
- ExecutionContextRef stores m_frame_list_wp when created from a frame
- ExecutionContextRef::GetFrameSP() tries the remembered list first before asking the thread
"""
self.build()
target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
self, "Break here", lldb.SBFileSpec(self.source), only_one_thread=False
)
# Get original frame count and PC.
original_frame_count = thread.GetNumFrames()
original_pc = thread.GetFrameAtIndex(0).GetPC()
self.assertGreaterEqual(
original_frame_count, 2, "Should have at least 2 real frames"
)
# Import the provider that accesses input frames in __init__.
script_path = os.path.join(self.getSourceDir(), "test_frame_providers.py")
self.runCmd("command script import " + script_path)
# Register the CircularDependencyTestProvider.
# Before the fix, this would crash or hang due to circular dependency.
error = lldb.SBError()
provider_id = target.RegisterScriptedFrameProvider(
"test_frame_providers.CircularDependencyTestProvider",
lldb.SBStructuredData(),
error,
)
# If we get here without crashing, the fix is working!
self.assertTrue(error.Success(), f"Failed to register provider: {error}")
self.assertNotEqual(provider_id, 0, "Provider ID should be non-zero")
# Verify the provider worked correctly,
# Should have 1 synthetic frame + all original frames.
new_frame_count = thread.GetNumFrames()
self.assertEqual(
new_frame_count,
original_frame_count + 1,
"Should have original frames + 1 synthetic frame",
)
# On ARM32, FixCodeAddress clears bit 0, so synthetic PCs get modified.
is_arm_32bit = lldbplatformutil.getArchitecture() == "arm"
expected_synthetic_pc = 0xDEADBEEE if is_arm_32bit else 0xDEADBEEF
# First frame should be synthetic.
frame0 = thread.GetFrameAtIndex(0)
self.assertIsNotNone(frame0)
self.assertEqual(
frame0.GetPC(),
expected_synthetic_pc,
f"First frame should be synthetic frame with PC {expected_synthetic_pc:#x}",
)
# Second frame should be the original first frame.
frame1 = thread.GetFrameAtIndex(1)
self.assertIsNotNone(frame1)
self.assertEqual(
frame1.GetPC(),
original_pc,
"Second frame should be original first frame",
)
# Verify we can still call methods on frames (no circular dependency!).
for i in range(min(3, new_frame_count)):
frame = thread.GetFrameAtIndex(i)
self.assertIsNotNone(frame)
# These calls should not trigger circular dependency.
pc = frame.GetPC()
self.assertNotEqual(pc, 0, f"Frame {i} should have valid PC")
def test_python_source_frames(self):
"""Test that frames can point to Python source files and display properly."""
self.build()
target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
self, "Break here", lldb.SBFileSpec(self.source), only_one_thread=False
)
# Get original frame count.
original_frame_count = thread.GetNumFrames()
self.assertGreaterEqual(
original_frame_count, 2, "Should have at least 2 real frames"
)
# Import the provider.
script_path = os.path.join(self.getSourceDir(), "test_frame_providers.py")
self.runCmd("command script import " + script_path)
# Register the PythonSourceFrameProvider.
error = lldb.SBError()
provider_id = target.RegisterScriptedFrameProvider(
"test_frame_providers.PythonSourceFrameProvider",
lldb.SBStructuredData(),
error,
)
self.assertTrue(error.Success(), f"Failed to register provider: {error}")
self.assertNotEqual(provider_id, 0, "Provider ID should be non-zero")
# Verify we have 3 more frames (Python frames).
new_frame_count = thread.GetNumFrames()
self.assertEqual(
new_frame_count,
original_frame_count + 3,
"Should have original frames + 3 Python frames",
)
# Verify first three frames are Python source frames.
frame0 = thread.GetFrameAtIndex(0)
self.assertIsNotNone(frame0)
self.assertEqual(
frame0.GetFunctionName(),
"compute_fibonacci",
"First frame should be compute_fibonacci",
)
self.assertTrue(frame0.IsSynthetic(), "Frame should be marked as synthetic")
# PC-less frames should show invalid address and not crash.
self.assertEqual(
frame0.GetPC(),
lldb.LLDB_INVALID_ADDRESS,
"PC-less frame should have LLDB_INVALID_ADDRESS",
)
self.assertEqual(
frame0.GetFP(),
lldb.LLDB_INVALID_ADDRESS,
"PC-less frame FP should return LLDB_INVALID_ADDRESS",
)
self.assertEqual(
frame0.GetSP(),
lldb.LLDB_INVALID_ADDRESS,
"PC-less frame SP should return LLDB_INVALID_ADDRESS",
)
self.assertEqual(
frame0.GetCFA(),
0,
"PC-less frame CFA should return 0",
)
frame1 = thread.GetFrameAtIndex(1)
self.assertIsNotNone(frame1)
self.assertEqual(
frame1.GetFunctionName(),
"process_data",
"Second frame should be process_data",
)
self.assertTrue(frame1.IsSynthetic(), "Frame should be marked as synthetic")
frame2 = thread.GetFrameAtIndex(2)
self.assertIsNotNone(frame2)
self.assertEqual(frame2.GetFunctionName(), "main", "Third frame should be main")
self.assertTrue(frame2.IsSynthetic(), "Frame should be marked as synthetic")
# Verify line entry information is present.
line_entry0 = frame0.GetLineEntry()
self.assertTrue(line_entry0.IsValid(), "Frame 0 should have a valid line entry")
self.assertEqual(line_entry0.GetLine(), 7, "Frame 0 should point to line 7")
file_spec0 = line_entry0.GetFileSpec()
self.assertTrue(file_spec0.IsValid(), "Frame 0 should have valid file spec")
self.assertEqual(
file_spec0.GetFilename(),
"python_helper.py",
"Frame 0 should point to python_helper.py",
)
line_entry1 = frame1.GetLineEntry()
self.assertTrue(line_entry1.IsValid(), "Frame 1 should have a valid line entry")
self.assertEqual(line_entry1.GetLine(), 16, "Frame 1 should point to line 16")
line_entry2 = frame2.GetLineEntry()
self.assertTrue(line_entry2.IsValid(), "Frame 2 should have a valid line entry")
self.assertEqual(line_entry2.GetLine(), 27, "Frame 2 should point to line 27")
# Verify the frames display properly in backtrace.
# This tests that PC-less frames don't show 0xffffffffffffffff.
self.runCmd("bt")
output = self.res.GetOutput()
# Should show function names.
self.assertIn("compute_fibonacci", output)
self.assertIn("process_data", output)
self.assertIn("main", output)
# Should show Python file.
self.assertIn("python_helper.py", output)
# Should show line numbers.
self.assertIn(":7", output) # compute_fibonacci line.
self.assertIn(":16", output) # process_data line.
self.assertIn(":27", output) # main line.
# Should NOT show invalid address (0xffffffffffffffff).
self.assertNotIn("0xffffffffffffffff", output.lower())
# Verify frame 3 is the original real frame 0.
frame3 = thread.GetFrameAtIndex(3)
self.assertIsNotNone(frame3)
self.assertIn("thread_func", frame3.GetFunctionName())
|