2

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?

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Rick
  • 33
  • 2
  • We are talking about 'call stack' not 'data stack', right? – Aubin Feb 18 '17 at 08:45
  • 4
    Possible duplicate of [print call stack in C or C++](http://stackoverflow.com/questions/3899870/print-call-stack-in-c-or-c) – Aubin Feb 18 '17 at 08:46
  • Yes Aubin, call stack indeed. Thank you for the information and I've just tried it. But the backtrace seems to only show the functions being called. 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. – Rick Feb 18 '17 at 16:16
  • @Rick: please **edit your question** instead of commenting it.... – Basile Starynkevitch Feb 18 '17 at 18:04

2 Answers2

4

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.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • This solves the problems I was having and gives a really nice explanation. Thank you very much – Rick Feb 19 '17 at 05:38
2

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
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • 1
    @BasileStarynkevitch He is *asking* how to use a debugger to examine state (just not at crash point). Note GDB tag on the question. – Employed Russian Feb 18 '17 at 22:46