<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/96877>96877</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Cannot read registers using commands placed in a .lldbinit file
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            lldb
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          DavidSpickett
      </td>
    </tr>
</table>

<pre>
    This is adapted from a report received on Discord where someone was trying to debug a Linux kernel image and wanted to relocate a file using the value stored in a register as the offset.

I reproduced this with a simpler setup and I think it's down to behaviour differences for commands inside a `.lldbinit` file versus being in a sourced file using `-s` or entered into the interactive prompt.

The commands are:
```
$ cat .lldbinit
target create /tmp/test.o
b main
run
process status
register read sp
expr `$sp`
target modules load --file /tmp/test.o .text `$sp`
```

What I expect to happen is we run to main and are able to read `sp` then use it to relocate the text section. This works fine if you source the file with `-s`:
```
$ ./bin/lldb -s lldb-commands
(lldb) command source -s 0 'lldb-commands'
Executing commands in '/home/david.spickett/build-llvm-aarch64/lldb-commands'.
(lldb) target create /tmp/test.o
Current executable set to '/tmp/test.o' (aarch64).
(lldb) b main
Breakpoint 1: where = test.o`main at test.c:2:10, address = 0x000000000000071c
(lldb) run
Process 56857 launched: '/tmp/test.o' (aarch64)
Process 56857 stopped
* thread #1, name = 'test.o', stop reason = breakpoint 1.1
    frame #0: 0x0000aaaaaaaaa71c test.o`main at test.c:2:10
   1    int main() {
-> 2 return 0;
   3    }
(lldb) process status
Process 56857 stopped
* thread #1, name = 'test.o', stop reason = breakpoint 1.1
 frame #0: 0x0000aaaaaaaaa71c test.o`main at test.c:2:10
   1    int main() {
-> 2      return 0;
   3    }
(lldb) register read sp
      sp = 0x0000fffffffff200
(lldb) expr `$sp`
(long) $1 = 281474976707072
(lldb) target modules load --file /tmp/test.o .text `$sp`
section '.text' loaded at 0xfffffffff200
```
It does not work if you put the same commands in a `.lldbinit` file:
```
$ ./bin/lldb
Current executable set to '/tmp/test.o' (aarch64).
Breakpoint 1: where = test.o`main at test.c:2:10, address = 0x000000000000071c
Process 56955 launched: '/tmp/test.o' (aarch64)
Process 56955 stopped
error: Command requires a process which is currently stopped.
error: <user expression 0>:1:1: use of undeclared identifier '$sp'
    1 | $sp
      | ^
error: invalid load address string '$sp'
Process 56955 stopped
* thread #1, name = 'test.o', stop reason = signal SIGSEGV: address not mapped to object (fault address: 0x5b0)
    frame #0: 0xffffffffffffffff
```
We even print that the target has stopped but then we say it isn't. So I suspect that the stop event does not propagate during the time a `.lldbinit` is running.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8V0lv4zgT_TX0pWCBphdZBx-yuRHgO3xAGtNnSixZnEikhoud_PtBUZLjJJ6eHvRMC922w-Xx1fZYkt7rg0HcsfUtW9_PZAyNdbt7edTqqdfVM4YwK6163X1ttAftQSrZB1RQO9uBBIe9dQEcVqiPqMAauNe-sk7BqUGH4G2H1iCcpIfgXrU5QLCgsIwHkPA_beILPKMz2ILu5AFBGgUnaeiMYMFhaysZECTUukWIPiE0CEfZRgQfrEMF2iQuB-0DOqCjGgRb1x5Dxvg94zfD5yMRdlbFiuDJpJMODUjwuutbdOAxxD5xeKR58ww6MJF7UPZkiFCJjTxqGx0oXdfo0FToobYOKtt10igP2nitiDHb8KxtVamNDmzDBwuO6Hz0UCIZknh7Gx3xuTCQbfjc0xbrAE3AwcZgk12aBmQV9BGhd7br39v4tcE3LtIhW44TbMPHf8OfYgWVDPBGMQ0H6Q4YoHJIbmdiH7qePtGHzA5LSuikNsNvF8cfvbMVeg8-yBD9ODlFxKFU4PthFF96RxYysfL9mc54bmdVbNFDa6WC-Tz55AMJyAK-hM8IH-1Ln98aGeAR8KXHKlAAG9n3aCiVTwgupqCSOSno0iHIssUh9aSiQ9IJ5HgD0SPo8C4vKSCJjscqaGsySJVysu7ZQ60Ngq7h1cYxyml9Miol3hTn78UoY2Jf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