Search⌘ K

Exploring Stack in GDB

Explore how to use GDB to examine the stack, inspect function call sequences, and analyze registers and memory during program execution. Understand how to set breakpoints, view the call stack with backtrace, and interpret disassembly for debugging C programs on x64 Linux.

Simple stack in GDB

To see the call stack in action, we’ll use a project called SimpleStack.

Source code

The source code of the stack is given below:

Simple stack

The code for the simple stack is:

C++
void func();
int main(int argc, char* argv[])
{
func();
return 0;
}

We define three functions func, func2, and func3 as below:

Method func

The code for func is:

C++
void func2();
void func()
{
func2();
}

Method func2

The code for func2 is:

C++
void func3();
void func2()
{
func3();
}

Method func3

The code for func3 is:

C++
void func3()
{
__asm__ volatile("int $0x03");
}

Compilation and execution of code:

We compile the files and load the executable into GDB:

gcc SimpleStack.c func.c func2.c func3.c -o SimpleStack
gdb ./SimpleStack

Note: You can practice all ...