Is there any way to get what functions (recursively) are called in a given function?
For example, the function parseBlockExpr() from Zig code uses parseBlock(), then expectStatementRecoverable(), then expectStatement(), and it goes back to parseBlockExpr().
It sounds like you want a std.debug.captureStackTrace(). You can pass @returnAddress()
as the parameter, and you can just put the resulting StackTrace directly into a printer.
This is how e.g. the leak detection check in the debug allocator does it.
3 Likes
Works for me.
Example of using std.debug.captureStackTrace():
const std = @import("std");
const stdout = std.io.getStdOut().writer();
var address_buffer: [100]usize = undefined;
var trace1 = std.builtin.StackTrace {
.instruction_addresses = address_buffer[0..],
.index = 0,
};
fn f0(val: u8) void {
std.debug.captureStackTrace(null, &trace1);
if(val == 0){
return;
}
f1(val - 1);
}
fn f1(val: u8) void {
if(val == 0){
return;
}
f2(val - 1);
}
fn f2(val: u8) void {
if(val == 0){
return;
}
f0(val - 1);
}
pub fn main() !void {
f0(9);
std.debug.dumpStackTrace(trace1);
}