Skip to content

Conversation

@SlynxCZ
Copy link
Contributor

@SlynxCZ SlynxCZ commented Jun 15, 2025

Summary

This PR adds structured callback trace registration to CounterStrikeSharp by capturing the actual user-level stack frame that registered a delegate.

This feature by itself does not change runtime behavior or add logging — it’s purely structural and performance-neutral. It prepares the internal architecture for advanced debugging and crash diagnostics without introducing risk to existing systems.


Why

Old output (completely unhelpful):

[23:40:30.477] CSSharp: Native 5df2e20d was invoked on a non-main thread

This gives no information about which plugin or part of the code triggered it.

New trace (fully traceable via my external MM plugin):

[CALLBACK TRACE] name='MyPlugin+<>c__DisplayClass7_0.<OnMapStart>b__0' callerStack(top)='MyPlugin.OnMapStart @ MyPlugin.cs:42'

This reveals exactly which delegate was registered and where — essential for diagnosing crashes, thread violations, or slow callbacks.


Implementation

  • Adds a filtered StackTrace(2, true) inside FunctionReference.CreateWrappedCallback()
  • Skips internal frames (e.g. CounterStrikeSharp, SafeExecutor, compiler-generated closure types)
  • Passes the first meaningful frame to RegisterCallbackTrace(...)
  • Does not log anything directly — avoids console spam or runtime overhead

This is a backend-only improvement that enables external tooling support.


AcceleratorCSS Integration

This structural change enables native integration with AcceleratorCSS, a Metamod plugin I’ve developed, which hooks into RegisterCallbackTrace().

Once this PR is merged:

  • AcceleratorCSS will be fully compatible with all Linux builds of CSS
  • It will log the last N registered callbacks and write them to disk on crash (along with console history and map info)
  • It dramatically improves post-mortem debugging, especially for SIGSEGV or thread-related crashes
  • Windows support is also planned in future updates

The plugin is finished and will be released immediately after this PR is merged.


Let me know if you prefer the trace logic behind a config flag, or extracted into a utility. Otherwise, this is backward-safe and ready to merge.

@SlynxCZ SlynxCZ requested a review from roflmuffin as a code owner June 15, 2025 18:44
@SlynxCZ
Copy link
Contributor Author

SlynxCZ commented Jun 15, 2025

I actaully forgot to remove the configs/addons/counterstrikesharp/configs/core.json before creating pull request, this is not required in my functionaly, so i can change it later, also i forgot to say that i added some sigs & offsets that weren't there before, they are:

  • GetHammerUniqueId
  • ChangeSubclass
  • CAttributeList_SetOrAddAttributeValueByName
  • CBaseModelEntity_SetBodygroup

@SlynxCZ
Copy link
Contributor Author

SlynxCZ commented Jun 16, 2025

All requested changes have been applied:

  • Reverted unrelated formatting in event_manager.cpp
  • Cleaned up unused sigs in gamedata.json
  • Fixed Execute() logic to allow global listeners
  • Ensured FreeEvent() is only called when the pointer is valid

Let me know if you’d prefer anything split into a separate PR or adjusted further.
Thanks again for the feedback!

{
if (!IsContextSafe())
{
ScriptContext().ThrowNativeError("ScriptCallback::Execute aborted due to invalid context");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, in what scenarios have you encountered that the context is not safe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As i remember, sometimes in newer css versions when was warmup and player which was alive changed team, where plugin with eventplayerspawn opened instantly any menu, it stopped recognizing inputs from OnKeyPress excluding the next, previous and close actions. This fixed it for me.

@roflmuffin
Copy link
Owner

LGTM, might need to fix the clang-format issues

@SlynxCZ
Copy link
Contributor Author

SlynxCZ commented Jun 17, 2025

LGTM, might need to fix the clang-format issues

Fixed the clang-format complaint from CI. Good to go now!

@roflmuffin roflmuffin enabled auto-merge (squash) June 20, 2025 12:23
@roflmuffin roflmuffin merged commit 6f66316 into roflmuffin:main Jun 20, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants