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

    <tr>
        <th>Summary</th>
        <td>
            [lldb-dap] Invalid variable reference ids
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          Anthony-Eid
      </td>
    </tr>
</table>

<pre>
    # Summary

It seems like lldb-dap reuses variable reference numbers across scopes during the same suspended state. For example, if a client sends two scope requests with different frame_ids, lldb-dap uses the same variable reference numbers for different variables across scopes. 

## Example 

```json
{
  "type": "request",
  "seq": 11,
  "command": "scopes",
  "arguments": {
    "frameId": 524289
  }
}
{
  "type": "request",
  "seq": 10,
  "command": "scopes",
  "arguments": {
    "frameId": 524290
  }
}
{
  "body": {
    "scopes": [
      {
        "expensive": false,
        "name": "Locals",
        "namedVariables": 1,
        "presentationHint": "locals",
        "variablesReference": 1
      },
      {
        "expensive": false,
        "name": "Globals",
        "namedVariables": 0,
        "variablesReference": 2
      },
      {
        "expensive": false,
        "name": "Registers",
        "namedVariables": 3,
        "presentationHint": "registers",
        "variablesReference": 3
      }
    ]
  },
  "command": "scopes",
  "request_seq": 9,
  "seq": 0,
  "success": true,
  "type": "response"
},
{
  "body": {
    "scopes": [
      {
        "expensive": false,
        "name": "Locals",
        "namedVariables": 0,
        "presentationHint": "locals",
        "variablesReference": 1
      },
      {
        "expensive": false,
        "name": "Globals",
        "namedVariables": 0,
        "variablesReference": 2
      },
      {
        "expensive": false,
        "name": "Registers",
        "namedVariables": 3,
        "presentationHint": "registers",
        "variablesReference": 3
      }
    ]
  },
  "command": "scopes",
  "request_seq": 12,
  "seq": 0,
  "success": true,
  "type": "response"
}
```

This goes against the DAP specifications in regard to the lifetime of object references, because references become invalid before the program exits its suspended state.


## Additional Information 

I’m one of the people working on creating a client-side debug adapter implementation for the Zed code editor and noticed this bug occurring when requesting variable information. This is preventing Zed from getting valid variable information during debugging. I used the below simple program to get the above log message

compiled with `gcc -g3 -o main.out`

```c
int main() {

    int x = 1;

    return x; // Breakpoint on this line to trigger a stop event
}
```

I installed lldb-dap through Pacman on Arch Linux and am using lldb version 18.1.18. 

## Log files
[vscode-lldb-dap-logs.txt](https://github.com/user-attachments/files/18118198/vscode-lldb-dap-logs.txt)
[zed-lldb-dap-logs.txt](https://github.com/user-attachments/files/18118199/zed-lldb-dap-logs.txt)


Please let me know if there's additional information I could provide to help fix this. I'll also be willing to collaborate with someone to come up with a patch, but I don't have any exposure to the code base so I would really appreciate help. 

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsV8GO47gR_Rr6UrAhUfbYOvjgXo8TA3MYbIIccllQZEniDkVqWZTtztcHpCzb09vd2EG2DwEWUKMtslj1qviKehREurGIW7Z6Yqv9TAyhdX67s6F19nn-WatZ5dTzlvEC_jF0nfDPLNuxbHcMQIgdgdHfEIxR1VyJHjwOhAQn4bWoDILHGj1aiWCHrkJPIKR3REDS9UigBq9tA6FFINEh0EA9WoUKKIiACzg4D3gRXW-Q8Z9A1yBAGo02xreKIJzd6Aw8_jYgBYKzDi0oXafQAWovOvxFK4oObkgTzlvcdwDXzj84mwxfJLKAsSyMF7FUn0fE0-CnbHx-JWfj-_qJZTsAxnl47pFxzopdfLtmEAf4T5MJ4W9Xizx_GJau64RV98UjkO_XCt8MHdpAk9k1cppMdTlOHlZ8yTfluHC9Tyj3_wvW7IOxltl7WCNpX_NzDxxnVtMMPFhdLfHSoyV9mlKuhSGc4N6srOgeavLFSWEe0_rOTv1r4s5Uo9-Z9R4JbRBBO_t3bcPdtXnT9Y2RP0_Uvbm_J7d_XPan5Po346ofSTb748D5hwL_GRtNAf0fh178wD7597y_mXHxXcZXtq72N4b_YCtde_OXezuWrzbpY4_SICXSlHLwAz5Mvmx96p2lNDL2XrL8f2u_3zPyr_b7q_0-pP3y1z-Sf1r_P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