Created
January 30, 2013 01:19
-
-
Save soarez/4669761 to your computer and use it in GitHub Desktop.
Count stack frames
This file contains 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 <stdio.h> | |
int count_stack_frames() { | |
void **ebp, **p; | |
int count; | |
asm(".intel_syntax noprefix"); | |
asm ("mov %0, ebp;" | |
:"=g"(ebp)); | |
for (p = ebp, count = 0; p; p = (void**)(*p)) | |
++count; | |
return count; | |
} | |
void level3() { | |
printf("level3() :: %d\n", count_stack_frames()); | |
} | |
void level2() { | |
printf("level2() :: %d\n", count_stack_frames()); | |
level3(); | |
} | |
void level1() { | |
printf("level1() :: %d\n", count_stack_frames()); | |
level2(); | |
} | |
int main(int argc, char * argv[]) { | |
printf("main() :: %d\n", count_stack_frames()); | |
level1(); | |
return 0; | |
} |
This file contains 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
program: main.c | |
gcc -Wall -O0 -masm=intel -m32 -g -o program main.c |
This file contains 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
main() :: 2 | |
level1() :: 3 | |
level2() :: 4 | |
level3() :: 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment