Issue 176717
Summary [LLDB] error: Couldn't apply _expression_ side effects: couldn't dematerialize register x0 without a stack frame in Python script context
Labels new issue
Assignees
Reporter Halle
    Greetings,

I am filing this as a bug as advised in the LLVM [discourse](https://discourse.llvm.org/t/error-couldnt-apply-_expression_-side-effects-couldnt-dematerialize-register-x0-without-a-stack-frame-in-python-script-context/89501/5), so please forgive the appearance of a (third šŸ˜…) cross-post.

In macOS, I am experiencing an unexpected outcome in a Python script context where I would like to `po` arm64 register `x0` in a state that is known to be paused:

```python
 print("Attempting to po register x0 in a known-stopped state via HandleCommand")
    debugger.SetAsync(False)
    process = exe_ctx.process
    if process:
        state = process.GetState()
        print(f"process is ${process}")
        if state == lldb.eStateStopped:
            print("process is stopped")
            thread = process.GetSelectedThread()
            if thread:
                print(f"thread is ${thread}")
                frame = thread.GetSelectedFrame()
                if frame:
                    print(f"frame is ${frame}")
                    symbol = frame.GetSymbol();
                    if symbol:
                        print(f"symbol is ${symbol}")
                        print("Trying po without explicit thread and frame select in command context")
                        lldb.debugger.HandleCommand("po $x0")                           
                        print("Trying po with explicit thread and frame select in command context")                        
                        lldb.debugger.HandleCommand(f"thread select {thread.GetIndexID()}")
                        lldb.debugger.HandleCommand(f"frame select {frame.GetFrameID()}")                       
                        lldb.debugger.HandleCommand("po $x0")                                                      
                    else:
                        print("no symbol")                             
                else:
                    print("no frame") 
            else:
                print("no thread") 
        else:
             print(f"state is not stopped, it is ${state}")                            
    else:
        print("no process")
    print("Done attempting to po register x0 in a known-stopped state via HandleCommand")
```

This usually (but not 100% deterministically) prints the error `Couldn’t apply _expression_ side effects : couldn’t dematerialize register x0 without a stack frame`. Sometimes the error is `Couldn’t materialize: couldn’t read the value of register x0ļæ¼` followed by `error: errored out in DoExecute, couldn’t PrepareToExecuteJITExpression`.

When the script completes shortly after this attempt, it is always possible to successfully `po $x0` interactively in `lldb`. I believe I have also seen the Python function on very rare occasions perform the `po` successfully.

`po` (or another _expression_ printing variation such as `p`) is my goal, because I want to obtain  Objective-C and Swift object descriptions when they exist, so it isn't helpful to me to work with the raw register read results, however, using `re r x0` or reading the register via the Python API `FindRegister(ā€œx0ā€)` works in this same context 100% of the time, so the issue is not related to the register not actually being readable in context.

Here are three examples of the output showing first the script output and then the result of my just typing the `po` into paused lldb after the function has exited.

```bash
Attempting to po register x0 in a known-stopped state via HandleCommand
process is $SBProcess: pid = 86947, state = stopped, threads = 4, executable = MyApp
process is stopped
thread is $thread #1: tid = 0xd64c3, 0x0000000100abc690 MyApp`sub_10002c690, queue = 'com.apple.main-thread', stop reason = breakpoint 1.823
frame is $frame #0: 0x0000000100abc690 MyApp`sub_10002c690
symbol is $id = {0x00000000}, range = [0x000000010002c690-0x000000010002c6c8), name="sub_10002c690"
frame #0: 0x0000000100abc690 MyApp`sub_10002c690
MyApp`sub_10002c690:
->  0x100abc690 <+0>:  ret    
    0x100abc694 <+4>:  stp    x20, x19, [sp, #-0x20]!
    0x100abc698 <+8>:  stp    x29, x30, [sp, #0x10]
    0x100abc69c <+12>: add    x29, sp, #0x10
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.823
    frame #0: 0x0000000100abc690 MyApp`sub_10002c690
MyApp`sub_10002c690:
->  0x100abc690 <+0>:  ret    
    0x100abc694 <+4>:  stp    x20, x19, [sp, #-0x20]!
    0x100abc698 <+8>:  stp    x29, x30, [sp, #0x10]
    0x100abc69c <+12>: add    x29, sp, #0x10
error: Couldn't apply _expression_ side effects : couldn't dematerialize register x0 without a stack frame
Done attempting to po register x0 in a known-stopped state via HandleCommand

(lldb) po $x0
4306298192

Attempting to po register x0 in a known-stopped state via HandleCommand
process is $SBProcess: pid = 86947, state = stopped, threads = 4, executable = MyApp
process is stopped
thread is $thread #1: tid = 0xd64c3, 0x0000000100abbc50 MyApp`sub_10002bc50, queue = 'com.apple.main-thread', stop reason = breakpoint 1.777
frame is $frame #0: 0x0000000100abbc50 MyApp`sub_10002bc50
symbol is $id = {0x00000000}, range = [0x000000010002bc50-0x000000010002bc60), name="sub_10002bc50"
frame #0: 0x0000000100abbc50 MyApp`sub_10002bc50
MyApp`sub_10002bc50:
->  0x100abbc50 <+0>:  adrp   x0, 19
    0x100abbc54 <+4>:  add    x0, x0, #0x340
    0x100abbc58 <+8>:  mov    x1, #0x0 ; =0 
    0x100abbc5c <+12>: ret    
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.777
    frame #0: 0x0000000100abbc50 MyApp`sub_10002bc50
MyApp`sub_10002bc50:
->  0x100abbc50 <+0>:  adrp   x0, 19
    0x100abbc54 <+4>:  add    x0, x0, #0x340
    0x100abbc58 <+8>:  mov    x1, #0x0 ; =0 
    0x100abbc5c <+12>: ret    
error: Couldn't apply _expression_ side effects : couldn't dematerialize register x0 without a stack frame
Done attempting to po register x0 in a known-stopped state via HandleCommand

(lldb) po $x0
255

Attempting to po register x0 in a known-stopped state via HandleCommand
process is $SBProcess: pid = 86947, state = stopped, threads = 4, executable = MyApp
process is stopped
thread is $thread #1: tid = 0xd64c3, 0x0000000100a941c8 MyApp`sub_1000041c8, queue = 'com.apple.main-thread', stop reason = breakpoint 1.4
frame is $frame #0: 0x0000000100a941c8 MyApp`sub_1000041c8
symbol is $id = {0x00000000}, range = [0x00000001000041c8-0x000000010000420c), name="sub_1000041c8"
frame #0: 0x0000000100a941c8 MyApp`sub_1000041c8
MyApp`sub_1000041c8:
->  0x100a941c8 <+0>:  stp    x20, x19, [sp, #-0x20]!
    0x100a941cc <+4>:  stp    x29, x30, [sp, #0x10]
    0x100a941d0 <+8>:  add    x29, sp, #0x10
    0x100a941d4 <+12>: mov    x19, x0
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.4
    frame #0: 0x0000000100a941c8 MyApp`sub_1000041c8
MyApp`sub_1000041c8:
->  0x100a941c8 <+0>:  stp    x20, x19, [sp, #-0x20]!
    0x100a941cc <+4>:  stp    x29, x30, [sp, #0x10]
    0x100a941d0 <+8>:  add    x29, sp, #0x10
    0x100a941d4 <+12>: mov    x19, x0
error: Couldn't materialize: couldn't read the value of register x0
error: errored out in DoExecute, couldn't PrepareToExecuteJITExpression
Done attempting to po register x0 in a known-stopped state via HandleCommand

(lldb) po $x0
4306314120
```

I have also tried simply calling `lldb.debugger.HandleCommand("thread select 1")` and `lldb.debugger.HandleCommand("frame select 0")` to guarantee the context (or so I thought), and I’ve tried calling the command using `CommandInterpreter` instead of directly in `debugger.HandleCommand()`. I’ve also tried checking the thread and frame for validity before proceeding. So, I am inclined to agree that it is a bug.

My LLDB version is the one at `/usr/bin/lldb` (not via Xcode, but the Xcode version does it too) and it’s `lldb-1700.0.9.502`, `Apple Swift version 6.1.2 (swiftlang-6.1.2.1.2 clang-1700.0.13.5)`

This is occurring with optimized, stripped arm64, but I don’t think that’s the issue since the `po` succeeds interactively. The device the executable is running on is remote, mentioning just in case it matters. I see one other report of this error in [this post](https://bryce.co/lldb-remove-randomness/) but otherwise it seems to be extremely rare.

Thanks for any assistance!
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to