Skip to content

Instantly share code, notes, and snippets.

@soarez soarez/Makefile
Created Jan 30, 2013

Embed
What would you like to do?
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
You can’t perform that action at this time.