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

    <tr>
        <th>Summary</th>
        <td>
            "Just My Code" support for LLDB
        </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 issue is to gather information for **potentially** supporting the "Just My Code" feature in lldb.

This is a Microsoft Debugger feature announced here: https://devblogs.microsoft.com/cppblog/announcing-jmc-stepping-in-visual-studio/

The clang code generation for it was implemented for ELF in:
https://discourse.llvm.org/t/rfc-just-my-code-stepping-for-non-msvc-debuggers/60279
https://reviews.llvm.org/D119910

And I presume was/is used with another downstream debugger.

This inserts calls to a `__CheckForDebuggerJustMyCode` function at the start of every compiled function. There is a default implementation provided to prevent a linker error if the C runtime (I assume) doesn't have one. The default implementation is just:
```
000000000000004c <__CheckForDebuggerJustMyCode>:
      4c: d65f03c0      ret
```
The signature for this function is:
```
void __CheckForDebuggerJustMyCode(const char* flag);
```
There is a global variable inserted that contains the value 1 by default. 1 meaning that it is your code. Code will pass the address of that variable to that function. This means (I think) that a debugger could change that global byte to a 0 to mark a file as not your code at runtime.

The only real implementation I've seen is from MSVC. The basic idea is:
```
if the flag is 1
        then go to some location that the debugger has placed a breakpoint on
else return
```
I am not sure if the break is placed exactly there, or how it is placed. You could arrange for a global symbol
that the debugger could break on. This symbol being present would also tell the debugger if jmc is present in the process.

The idea is that when you shouldn't stop, the code just runs through the function and returns. When you should, it will
end up at the break location and you go into the debugger.

After you've hit the break location you step out once, into the user function, then show the user
that you've stopped. So that they never see this intermediate function in the callstack.

What I am unsure about is does this feature only work/work best when all code is compiled with jwc?

Microsoft's debugger has configuration files for what paths you want to ignore. A change to those files does not
require a rebuild, this is why I think it is modifying these global vars each time that config updates.

https://learn.microsoft.com/en-us/visualstudio/debugger/just-my-code?view=vs-2022#BKMK_CPP_Customize_call_stack_behavior

If you decide at runtime that some of the jmc compatible code isn't yours, set those global vars to 0 and the jmc
function returns without breaking. Which suggests that you can only add to "your code" if the code in question is compiled with jmc. If the code is not jmc compatible it can never be added, unless you ignore jmc completely and fall back to filtering the steps normally.

Which seems to be what the pre-jmc version of the feature did in Microsoft's debugger.
(GDB can also do this https://sourceware.org/gdb/download/onlinedocs/gdb/Skipping-Over-Functions-and-Files.html)

Which has the disadvantage of going into the debugger every time, which is what the jmc function aims to avoid.
For someone who is almost never debugging system code though, they won't hit that fallback.

Based on that conjecture the work would be:
* An implementation of __CheckForDebuggerJustMyCode somewhere (compiler-rt?). Containing some unique symbol for the debugger to find.
* Some flag to link that in with the compiled code and/or documentation to do that manually.
* Teaching lldb how to detect the presence of jmc and find the place to break in __CheckForDebuggerJustMyCode.
* Adding a setting to globally ignore jmc.
* Adding the logic to do step ins using the jmc break.
* Adding setting(s) to remove from "your code".
* Adding a fallback to existing filtering if the code you want to add is not jmc compatible.
  (not sure that we have those filters either)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8WF1v4rrW_jXhZgkEBtpywQVtN6_67hmdLc3obJ2ryrFXEk8dO-MP2Jxff7RsE6DtTDUaBInX57Oe9STce9UaxG21fqzWzxMeQ2fd9pkflPw2KPGGIUxqK0_b753yoLyPCMpDsNDy0KEDZRrreh6UNdBYBxXbVWw32IAmKK71Kf8APg6DdUGZFkKHUDH2_9EH-HqCJyuxYgwa5CE6BGVAa1nPqvlzNd_l_4t34PBVCWe9bQI8Yx3bFt14kBtjoxEooUOH1XIHXQiDr5a7iu0rtpd4qLVt_aw_25gJ21dsL4aBLlRsX0wo005_9GLqAw4DfVFmelA-cj31IUplyeBNeAhCc9OCsBKhRYPuUhIV4Mg9qH7Q2KMJKNPPf3zZgzIUXbLxLlblhY3O40zrQz-zjqILFdu7Rkx_RB-m_WlKzi4xNtZNjTXT3h_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