Skip to content

Instantly share code, notes, and snippets.

@eggfly
Created August 10, 2022 08:12
Show Gist options
  • Save eggfly/8a5880cf77984f7d40b4ba49c5f545cd to your computer and use it in GitHub Desktop.
Save eggfly/8a5880cf77984f7d40b4ba49c5f545cd to your computer and use it in GitHub Desktop.
A demo of __builtin_frame_address()
#include <iostream>
void my_stack_trace(int max_depth) {
uintptr_t frame = (uintptr_t) __builtin_frame_address(0);
uintptr_t next;
while (max_depth--) {
next = *(uintptr_t *) frame;
frame = next;
std::cout << (void *) next << std::endl;
}
}
void test(int max_depth) {
if (max_depth != 0) [[unlikely]] {
test(max_depth - 1);
} else {
my_stack_trace(20);
return;
}
}
int main() {
test(10);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment