Skip to content

Instantly share code, notes, and snippets.

@soarez
Created January 30, 2013 01:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soarez/4669761 to your computer and use it in GitHub Desktop.
Save soarez/4669761 to your computer and use it in GitHub Desktop.
Count stack frames
#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;
}
program: main.c
gcc -Wall -O0 -masm=intel -m32 -g -o program main.c
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