How to get a SystemVerilog backtrace programmatically?

When debugging simulations, oftentimes I get an error from a generic error handler method, but I have no idea who called that error handler. It would be very useful to dump the backtrace in the error handler method to find out where the error is coming from. Any way to accomplish this in SystemVerilog? This is a standard feature of many other languages.

Note: I understand this is a poorly designed testbench. However, I need tools in my toolbox when dealing with other people’s code.

Questa implements a $stacktrace; system task that prints the current call stack from where it was called from. Other tools may implement similar commands, but even if they don’t, you can always set a breakpoint in the generic error handler code that executes a simulator command that displays the current call stack, and then continues.

In reply to dave_59:

Questa implements a $stacktrace; system task that prints the current call stack from where it was called from. Other tools may implement similar commands, but even if they don’t, you can always set a breakpoint in the generic error handler code that executes a simulator command that displays the current call stack, and then continues.

Hi Dave,

Can you please let me know how to use the $stacktrace function?
If possible please provide an example it would be very helpful.

Thanks,
Kamal.

In reply to kamal kumar balaga:

Note, since this was originally posted, $stacktrace has been added to the upcoming IEEE P1800-2023 LRM

When called as a task, $stacktrace displays the call stack information. When called as a function, $stacktrace returns a string containing the call stack information. The content of the call stack information is implementation dependent.
For example:

initial begin
string trace;
$stacktrace;         // Call stack information is displayed
trace = $stacktrace; // Call stack information is in 'trace' variable
// for later processing
...
end
1 Like