Rick
Rick

Reputation: 33

How can I show the content of stack in C?

I have taken some online courses introducing the use of stack and they are nice but rather theoretical. Therefore, I've been trying to understand it through showing the stack of some simple C program. I've found several methods of doing it and tried using gdb (backtrace...).

However, I am only able to show stack information when bugs occur. I am wondering if there is a way to just show the stack even if the program is correctly run?

Upvotes: 2

Views: 2054

Answers (2)

In theory, you cannot be sure that there is any stack.

For example, a compiler could inline every function call. Or it could do some whole program static analysis and find out that no stack is required. Or the compiler did optimize some calls as tail calls.

More realistically, local variables inside some functions could all go in registers (when optimizing).

In practice, you could run your program in a debugger (like gdb) and stop the program (e.g. with Ctrl C in your gdb session), then run the backtrace (or bt) command of gdb and examine the stack of the debugged process.


(the information below is advanced; don't get confused if you are a newbie; and be very careful about what is doable at compile time, from inside your program, or from your debugger)

If you want to access the call stack programmatically inside a program on Linux (with GNU libc) you might use the backtrace functions (they are not standard, and might not work, in particular with strong optimizations). Or even Ian Taylor libbacktrace (then better compile all your code with -g since libbacktrace uses debug information in DWARF format).

In a comment you added:

I am looking for a way of showing the detail information in the stack such as the value of parameters, variables, SP and so on.

This is not possible in general (at runtime, from inside your program). Variables and parameters are only known to the compiler (and are forgotten at runtime). In the machine code you only have memory locations and registers (and perhaps stack frames, which might get lost with the -fomit-stack-pointer compile option....). Also, you often would compile your C code with some optimizations, and then it is likely that some variables don't sit on the stack (but only in registers).

Notice that C has no introspection, or reflection, no explicit continuations, and is not an homoiconic language.

Upvotes: 4

Employed Russian
Employed Russian

Reputation: 213887

I am wondering if there is a way to just show the stack even if the program is correctly run?

Yes: you can examine stack at any arbitrary point in your program's execution: just set a breakpoint on instruction of interest, or single-step the entire program.

For example:

(gdb) break main
(gdb) run

... program stops after main prolog
(gdb) where  # examine stack
(gdb) stepi  # execute one instruction
(gdb) where
(gdb) stepi
... repeat until you reach syscall SYS_exit, or until you are too bored to continue

Upvotes: 2

Related Questions