Skip to content

Add a way to generate callstack trace message #7148

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

SeungheonOh
Copy link
Collaborator

@SeungheonOh SeungheonOh commented Jun 16, 2025

closes #7146. Add plugin option to generate callstack trace message. Currently this won't handle cases when a function has PlutusTx.Trace.callStack and that function is called in multiple places. It will just use callstack of the first occurance :/ (working on it. more detail on first commit message).

I redid everything, now it will have correct callstack, but with some execution cost overhead. I'm not too unhappy about this since callstack in general is almost exclusively used for debug scripts anyways.

Example
-- SillyCalss.hs
class SillyClass a where
  sillyThing :: a -> Integer

instance SillyClass Integer where
  sillyThing = traceError callStack

-- Demo.hs
rob :: Integer -> Integer -> Integer
rob x = sillyThing @Integer

-- entrypoint
tom :: Integer -> Integer
tom = rob (x PTx.+ 20)
tom:demo/Demo.hs:44:1-44:3
\-rob:demo/Demo.hs:32:1-32:3
\-$fSillyClassInteger
\-$fSillyClassInteger_$csillyThing

Added `generate-callstack` option, which, when turned on, will replace every
occurances of `PlutusTx.Trace.callStack` with the callstack message at its
relative position.

This is "rudimentary" because it won't correctly handled when `PlutusTx.Trace.callStack`
is called inside of function, call it `bob`, which gets called in multiple locations.
It will use callstack when `bob` was called the first time and use that exact callstack
in any subsequent calls of `bob` which can be very misleading. This, however, is the best
we can get without needing to pass around callstack values at UPLC level or generating
multiple copies of `bob` for all of its unique relative positions.
Copy link
Contributor

github-actions bot commented Jun 16, 2025

PR Preview Action v1.6.1

🚀 View preview at
https://IntersectMBO.github.io/plutus/pr-preview/pr-7148/

Built to branch gh-pages at 2025-06-17 20:25 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@SeungheonOh
Copy link
Collaborator Author

Figured out a way to do "real" callstack, with some runtime overhead. This will handle something like this correctly:

silly, silly2, silly3, silly4 :: Integer -> Integer
silly x = silly2 x
silly2 x = silly3 x
silly3 x = silly4 x
silly4 x = bob

bob, rob, cob :: Integer
bob = traceError callStack
rob = traceError callStack
cob = traceError callStack

tom :: Integer -> Integer
tom 1 = silly 1
tom 2 = rob
tom 3 = cob
tom _ = 42
[||tom 1||]
tom:demo/Demo.hs:62:1-62:3
\-silly:demo/Demo.hs:51:1-51:5
\-silly2:demo/Demo.hs:52:1-52:6
\-silly3:demo/Demo.hs:53:1-53:6
\-silly4:demo/Demo.hs:54:1-54:6
\-bob:demo/Demo.hs:57:1-57:3

[||tom 2||]
tom:demo/Demo.hs:62:1-62:3
\-rob:demo/Demo.hs:58:1-58:3

[||tom 3||]
tom:demo/Demo.hs:62:1-62:3
\-cob:demo/Demo.hs:59:1-59:3

Comment on lines +630 to +631
-- FIXME: Variables will miss span information when the module they are defined
-- in is loaded from cache instead of getting compiled.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is there a workaround for this?

\-nestedLinear:test/Plugin/CallStack/Spec.hs:63:1-63:12
\-nestedLinear2:test/Plugin/CallStack/Spec.hs:64:1-64:13
\-nestedLinear3:test/Plugin/CallStack/Spec.hs:65:1-65:13
\-nestedLinear4:test/Plugin/CallStack/Spec.hs:66:1-66:13
Copy link
Collaborator Author

@SeungheonOh SeungheonOh Jun 17, 2025

Choose a reason for hiding this comment

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

The span provided points to the definition of executed function instead of location of invocation. Is this fine?

... I think it's fine

@@ -0,0 +1,4 @@
### Added
- `generate-callstack` option: When enabled, the plugin replaces every occurrence of `PlutusTx.Trace.callStack` with a `BuiltinString` containing the callstack at that relative point. Enabling this option incurs execution-cost overhead: it adds an extra callstack argument to all user-defined functions, and every time they are invoked it requires having extra application node for providing the current callstack. When `gennerate-callstack` is not enabled, `PlutusTx.Trace.callStack` is string "<CallStack>".
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- `generate-callstack` option: When enabled, the plugin replaces every occurrence of `PlutusTx.Trace.callStack` with a `BuiltinString` containing the callstack at that relative point. Enabling this option incurs execution-cost overhead: it adds an extra callstack argument to all user-defined functions, and every time they are invoked it requires having extra application node for providing the current callstack. When `gennerate-callstack` is not enabled, `PlutusTx.Trace.callStack` is string "<CallStack>".
- `generate-callstack` option: When enabled, the plugin replaces every occurrence of `PlutusTx.Trace.callStack` with a `BuiltinString` containing the callstack at that relative point. Enabling this option incurs execution-cost overhead: it adds an extra callstack argument to all user-defined functions, and every time they are invoked it requires having extra application node for providing the current callstack. When `generate-callstack` is not enabled, `PlutusTx.Trace.callStack` is string "<CallStack>".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add bind tracing mode
2 participants