the stack frame

23
The Stack The Stack F F rame rame An Ivaylo Marinkov presentation [email protected]

Upload: ivo-marinkov

Post on 25-Dec-2014

1.896 views

Category:

Education


2 download

DESCRIPTION

A presentation about the stack frame by Ivaylo Marinkov http://tsarstva.bg

TRANSCRIPT

Page 1: The Stack Frame

The Stack The Stack FFramerame

An Ivaylo Marinkov [email protected]

Page 2: The Stack Frame

Call StackCall Stack

A A stackstack data structure storing data structure storing information of a computer program's information of a computer program's active subroutines.active subroutines.

Whilst important for the software's Whilst important for the software's proper functioning, its details are proper functioning, its details are usually hidden and usage is automated usually hidden and usage is automated in high-level programming languages.in high-level programming languages.

Page 3: The Stack Frame

Call Stack PurposeCall Stack Purpose

Keeping record of the point to which Keeping record of the point to which each active subroutine should return each active subroutine should return control when finished.control when finished.

Page 4: The Stack Frame

Active SubroutineActive SubroutineA routine that has been called but A routine that has been called but has not finished execution yet. has not finished execution yet. Afterwords, control should be Afterwords, control should be returned to the point where the call returned to the point where the call has been made. has been made.

Routines may be nested to any level Routines may be nested to any level and recursion is possibe – hence the and recursion is possibe – hence the stack structure.stack structure.

Page 5: The Stack Frame

For example, the For example, the DrawSquareDrawSquare subroutine calls the subroutine calls the DrawLineDrawLine subroutine from four different subroutine from four different places. places. DrawLineDrawLine must know where must know where to return once completed. to return once completed.

This is accomplished by pushing the This is accomplished by pushing the address following the call address following the call instruction – the instruction – the return addressreturn address – – onto the stack with each call.onto the stack with each call.

Page 6: The Stack Frame

Call Stack Inner WorkingsCall Stack Inner WorkingsThe caller The caller pushespushes the return address onto the return address onto the stack (the stack (windingwinding))..

The called subroutine, when it finishes, The called subroutine, when it finishes, popspops the return address off the stack and the return address off the stack and transfers control to it (transfers control to it (unwindingunwinding))..

If a called subroutine calls on yet another If a called subroutine calls on yet another subroutine, it will push another address subroutine, it will push another address onto the stack, and so on, with the onto the stack, and so on, with the information stacking up and unstacking as information stacking up and unstacking as the program dictates.the program dictates.

Page 7: The Stack Frame

Should pushing consume all the Should pushing consume all the space allocated for the call stack, an space allocated for the call stack, an error called error called stack overflow stack overflow will will occur. occur.

There is usually a single call stack There is usually a single call stack associated with each process thread. associated with each process thread. However, the program may create However, the program may create additional call stacks for tasks such as additional call stacks for tasks such as signal-hadlingsignal-hadling or or cooperative cooperative multitaskingmultitasking..

Page 8: The Stack Frame

Additional Call Stack FunctionsAdditional Call Stack Functions

Local data storage – keeping local-scope variable values.

Parameter passing – storage for values passed by calling code.

Evalution stack – in some cases operands for logical and arithmetic operations may be stored in the call stack.

Current instance pointer – for this pointer in object-oriented languages.

Page 9: The Stack Frame

StructureStructureA call stack is composed of stack frames, machine and application banary interface-dependant data structures containing subroutine state information.

Page 10: The Stack Frame

The Stack FrameThe Stack Frame

Generally speaking, a Generally speaking, a procedure's stack frame contains procedure's stack frame contains all the information necessary to all the information necessary to save and restore the state of the save and restore the state of the procedure.procedure.

Page 11: The Stack Frame

Strictly speaking, it is only necessary Strictly speaking, it is only necessary for the calling program and the for the calling program and the called procedure to agree on the called procedure to agree on the structure of the stack frame for each structure of the stack frame for each procedure call.procedure call.

However, the specification of a However, the specification of a calling convention facilitates the use calling convention facilitates the use of procedure libraries by defining of procedure libraries by defining the structure of the stack frame the structure of the stack frame uniformly for all procedure calls. uniformly for all procedure calls.

Page 12: The Stack Frame

Calling convention used in the MIPS architecture stack frame

The frame pointer is stored in register $30, also known as $fp. A stack frame consists of the memory on the stack between the frame pointer and the stack pointer.

Three steps are necessary to call a procedure.

Page 13: The Stack Frame

1.1. Pass the arguments.Pass the arguments. The first four arguments are passed in registers $a0-$a3. The remaining arguments are pushed onto the stack.

2.2. Save the caller-saved registers.Save the caller-saved registers. This includes registers $t0-$t9, if they contain live values at the call site.

3.3. Execute a Execute a jaljal instruction. instruction. The endless MIPS cycle

Page 14: The Stack Frame

1.1. Pass the arguments.Pass the arguments. The first four arguments are passed in registers $a0-$a3. The remaining arguments are pushed onto the stack.

2.2. Save the caller-saved registers.Save the caller-saved registers. This includes registers $t0-$t9, if they contain live values at the call site.

3.3. Execute a Execute a jaljal instruction. instruction. The endless MIPS cycle

Page 15: The Stack Frame

Within the called routine, the following steps Within the called routine, the following steps are necessary:are necessary:

1.1. Establish the stackEstablish the stack frame by subtracting frame by subtracting the frame size from the stack pointer.the frame size from the stack pointer.

2.2. Save the callee-saved registersSave the callee-saved registers in the in the frame. Registerframe. Register $fp$fp is always saved. Registeris always saved. Register $ra$ra and registersand registers $a0$a0--$a3$a3 need to be saved if need to be saved if they are in use and the routine itself makes they are in use and the routine itself makes calls. Any of the registerscalls. Any of the registers $s0$s0- - $s7$s7 that are that are used by the callee need to be saved.used by the callee need to be saved.

3.3. Establish the frame pointerEstablish the frame pointer by adding the by adding the stack frame size to the address instack frame size to the address in $sp$sp. .

Page 16: The Stack Frame

To return from a call, a function places the To return from a call, a function places the returned value intoreturned value into $v0$v0 and executes the and executes the following steps:following steps:

1.1. Restore any callee-saved registersRestore any callee-saved registers that that were saved upon entry.were saved upon entry.

2.2. Pop the stack framePop the stack frame by subtracting the by subtracting the frame size fromframe size from $sp$sp..

3.3. ReturnReturn by jumping to the address in by jumping to the address in registerregister $ra$ra. .

Page 17: The Stack Frame

DebuggingDebugging

The purpose of a debugger such The purpose of a debugger such as GDB (as GDB (gnu.org/software/gdbgnu.org/software/gdb) is ) is to allow you to see what is going to allow you to see what is going on “inside” another program on “inside” another program while it executes--or what while it executes--or what another program was doing at another program was doing at the moment it crashed.the moment it crashed.

Page 18: The Stack Frame

In PracticeIn PracticeAn illustration to viewing the call stack using GDB.

1.1. Compile your program with the -g option, like cc -g -o p1 p1.c

Page 19: The Stack Frame

2.2. Navigate to your program's directory Navigate to your program's directory and run GDB:and run GDB: gdb gdb your_programyour_program

If all went fine, you will land on a If all went fine, you will land on a command prompt.command prompt.

Page 20: The Stack Frame

3.3. Then install some breakpoints using the Then install some breakpoints using the breakbreak command: command: break break function_namefunction_name

4.4. NowNow runrun. The program will proceed . The program will proceed until the first breakpoint.until the first breakpoint.

Page 21: The Stack Frame

You can select a frame using theYou can select a frame using the frame frame nn command and view frame information command and view frame information using theusing the info frame info frame n n command. command.

Some of the details this command Some of the details this command displays are the addresses of the frame, displays are the addresses of the frame, the next frame down (called by this the next frame down (called by this frame) and the next frame up (caller of frame) and the next frame up (caller of this frame).this frame).

Page 22: The Stack Frame

View the call stack using theView the call stack using the backtracebacktrace command. A backtrace is a summary of command. A backtrace is a summary of how your program got where it is. It how your program got where it is. It shows one line per frame, for many shows one line per frame, for many frames, starting with the one currently frames, starting with the one currently in execution (frame zero), followed by its in execution (frame zero), followed by its caller (frame one), and on up the stack.caller (frame one), and on up the stack.

Page 23: The Stack Frame

ResourcesResources

https://en.wikipedia.org/wiki/Call_stackhttp://www.cs.uaf.edu/~cs301/notes/Chapter9/node11.htmlhttp://chortle.ccsu.edu/assemblytutorial/Chapter-26/ass26_4.htmlhttp://www.chemie.fu-berlin.de/chemnet/use/info/gdb/http://www.freebsd.org/doc/en/books/developers-handbook/debugging.htmlhttps://www.gnu.org/software/gdb/Background images from various sources.

This presentation was created using only free and open source software including the Ubuntu Linux operating system, LibreOffice, Mozilla Firefox, Geany, GDB and

KolourPaint.