Created
August 10, 2022 08:12
-
-
Save eggfly/8a5880cf77984f7d40b4ba49c5f545cd to your computer and use it in GitHub Desktop.
A demo of __builtin_frame_address()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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