Chapter 8.Examining the StackWhen your program has stopped, the first thing you need to know is where it stopped and how it gotthere.Each time your program performs a function call, information about the call is generated. That in-formation includes the location of the call in your program, the arguments of the call, and the localvariables of the function being called. The information is saved in a block of data called a stack frame.The stack frames are allocated in a region of memory called the call stack.When your program stops, the gdb commands for examining the stack allow you to see all of thisinformation.One of the stack frames is selected by gdb and many gdb commands refer implicitly to the selectedframe. In particular, whenever you ask gdb for the value of a variable in your program, the valueis found in the selected frame. There are special gdb commands to select whichever frame you areinterested in. Refer to Section 8.3 Selecting a frame.When your program stops, gdb automatically selects the currently executing frame and describes itbriefly, similar to the frame command (refer to Section 8.4 Information about a frame).8.1. Stack framesThe call stack is divided up into contiguous pieces called stack frames, or frames for short; each frameis the data associated with one call to one function. The frame contains the arguments given to thefunction, the function’s local variables, and the address at which the function is executing.When your program is started, the stack has only one frame, that of the function main. This is calledthe initial frame or the outermost frame. Each time a function is called, a new frame is made. Eachtime a function returns, the frame for that function invocation is eliminated. If a function is recursive,there can be many frames for the same function. The frame for the function in which execution isactually occurring is called the innermost frame. This is the most recently created of all the stackframes that still exist.Inside your program, stack frames are identified by their addresses. A stack frame consists of manybytes, each of which has its own address; each kind of computer has a convention for choosing onebyte whose address serves as the address of the frame. Usually this address is kept in a register calledthe frame pointer register while execution is going on in that frame.gdb assigns numbers to all existing stack frames, starting with zero for the innermost frame, one forthe frame that called it, and so on upward. These numbers do not really exist in your program; theyare assigned by gdb to give you a way of designating stack frames in gdb commands.Some compilers provide a way to compile functions so that they operate without stack frames. (Forexample, the gcc option-fomit-frame-pointergenerates functions without a frame.) This is occasionally done with heavily used library functionsto save the frame setup time. gdb has limited facilities for dealing with these function invocations. Ifthe innermost function invocation has no stack frame, gdb nevertheless regards it as though it had aseparate frame, which is numbered zero as usual, allowing correct tracing of the function call chain.However, gdb has no provision for frameless functions elsewhere in the stack.