-
Notifications
You must be signed in to change notification settings - Fork 5k
App crashes with an output "Trace/Breakpoint Trap" on Linux when a P/Invoke callback is called from a native library if the dotnet debugger is attached. #104459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Tagging subscribers to this area: @tommcdon |
Hi @walterlv! Thanks for reporting this bug!
Do you know if this issue reproduces on Windows? |
Ahh nevermind this question as the repro is very specific to linux. Do you know if the callback/debugging issue is specific to the libpulse API (e.g. does a standalone repo that uses callback from C++ to C# on Linux reproduce the issue)? I am curious if there is something specific to libpulse that is causing the problem, for example a difference in calling convention, etc... |
@tommcdon I can repro this issues by @walterlv 's repo in my linux system. And I can sure it's not the libpulse bug, because I can repro this issues with https://github.com/Haltroy/CefGlue I can not reproduce on Windows because I fail to run the libpulse on Windows... I mean I do not know if it can be reproduced on Windows. |
Thanks to my friend @kkwpsv, he helped me to find out more information about this issue. @tommcdon This issue is quite different from #102767:
Let's see more details here.
Then,
The stack traces are shown as follows: [UnmanagedCallersOnly]
private static unsafe void Callback(byte* sourceId, int isEnabled, byte level,
long matchAnyKeywords, long matchAllKeywords, Interop.Advapi32.EVENT_FILTER_DESCRIPTOR* filterData, void* callbackContext)
{
EventPipeEventProvider _this = (EventPipeEventProvider)GCHandle.FromIntPtr((IntPtr)callbackContext).Target!;
if (_this._eventProvider.TryGetTarget(out EventProvider? target))
{
_this.ProviderCallback(target, sourceId, isEnabled, level, matchAnyKeywords, matchAllKeywords, filterData);
}
} |
We haven't been able to repro the exact issue from your repros yet, but the If the dotnet debugger is hitting the same EventPipeEventProvider Callback issue, then there is a partial fix already merged through #106040 and a second PR #106156 that is open |
We used the latest version of the C# extension in VS Code
Yes - the daily builds from https://github.com/dotnet/sdk/blob/main/documentation/package-table.md contain the fix. |
@tommcdon I test again with |
Seems like the same problem I'm seeing here: microsoft/DockerTools#444 |
@jwilliamsonveeam Sorry, the microsoft/DockerTools#444 is too long, I'm afraid I'm missing out on important information. |
@lindexi I updated my last comment with a small self contained example of a program that fails with a sigtrap in the native c code callback. |
I've run @walterlv's reproducer (Walterlv.Issues.TraceBreakpointTrap) and reproduced the issue as well. I've been debugging a similar issue where the scenario is as follows:
Using @walterlv's reproducer as a base, I've modified it with these changes and managed to avoid the crash. The output from my execution is as follows: $ ./artifacts/bin/Walterlv.Issues.TraceBreakpointTrap/debug/TraceBreakpointTrapDemo --skip-attach
### Trace/Breakpoint Trap issue on .NET debugger ###
Context state changed: 1
If you want to debug this demo using other debuggers (e.g. GDB, LLDB), you can use the following options:
--sleep <seconds> Sleep for a while before attaching debugger.
--skip-attach Skip attaching debugger and run directly.
Please attach a dotnet debugger and use 'Set next statement'.
Context state changed: 2
Context state changed: 3
Context state changed: 4
Context state changed: 5
Issue may not be reproduced. Exit. In the output, changes 1 to 4 are from before the debugger is attached. Once the debug is attached, change 5 is printed but there's no crash. Additionally, in my own (non-shareable) projects, I've been able to use a C debugger (lldb or gdb) to manually call the callback (through a function pointer) directly from the debugger. This led to the C# application throwing the following error:
This error is seemingly thrown here, but I don't have a fine understanding of the dotnet runtime. |
I think this may have revealed the culprit. The thing is that .NET runtime only handles signals when the thread those occurred on are known to the runtime. That means that they were either created by the runtime or called into the runtime. If the debugger sets the breakpoint on the
This code is for NativeAOT, in coreclr, the error comes from here: runtime/src/coreclr/vm/dllimportcallback.cpp Lines 187 to 196 in 008ee9f
|
@janvorli Hello and thanks for your input! I'll be reviewing the However, I'd like to point out that the yet-unregistered thread is receiving a |
https://github.com/jwilliamsonveeam/TimerCallBackDemo |
The debugger can set some breakpoints on its own for its internal purposes. @tommcdon would most likely know if it can be the case here. |
@janvorli If the debugger is setting its own breakpoint (e.g. on managed-to-unmanaged transitions) and then reaching it before the thread is properly registered with the .NET runtime (e.g. on the first .NET interaction of a thread), then the @tommcdon Could you please confirm if my assumption is correct? |
Sorry to be that person, but has there been any update on this? I'm not sure if @tommcdon could reproduce the issue and verify if this is an issue with .NET, or if there's a workaround. |
Edit: It was unrelated, and due to a clash with the Go runtime in the library.
|
golang libraries are not supported with .NET runtime in the same process: https://learn.microsoft.com/en-us/dotnet/standard/native-interop/abi-support#golang . It can result in hard to diagnose crashes like the one you have observed. |
I tested with a C stub and there is no longer any issue with the debugger and callback, so you're correct, it must be a clash between the runtimes and the debugger. Thanks for the clarification, and apologies for having polluted the issue with an irrelevant case, I'll edit the comment. |
Description
Note: Not all native callbacks cause this issue so I've written a minimal reproducible example below.
Reproduction Steps
Minimal reproducible example 1:
Reproducible example 2:
Expected behavior
The app should not crash when the dotnet debugger is attached.
Actual behavior
The app crashes with an output "Trace/Breakpoint Trap".
Regression?
I've only tested this on .NET 8.0.302
Known Workarounds
I've found several workarounds:
Note:
Debugger.IsAttached
property cannot detect the native debugger so I added alternative options--sleep <seconds>
and--skip-attach
for the minimal reproducible example above.Configuration
I didn't find any environment that doesn't have this issue.
Other information
dotnet tool install -g dotnet-sos
dotnet sos install
ulimit -c unlimited
echo "0x3F"> /proc/<pid>/coredump_filter
after the process starts and the pid is known.Trace/Breakpoint Trap (core dumped)
.lldb --core core TraceBreakpointTrapDemo
The text was updated successfully, but these errors were encountered: