-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackFrame.c
More file actions
105 lines (84 loc) · 4.37 KB
/
StackFrame.c
File metadata and controls
105 lines (84 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* StackFrame.c
*
* Source file for StackFrame module that provides functionality relating to
* stack frames and printing out stack frame data.
*
*/
#include <stdio.h>
#include "StackFrame.h"
#include <stdlib.h>
unsigned long getBasePointer() {
/** Caller's base pointer.*/
unsigned long basePointer;
/**
* The following inline assembly obtains the caller's base pointer which is the value held at getBasePointer()'s base pointer held at 0(%rbp).
*/
asm("movq 0(%%rbp), %0;" : "=r" (basePointer));
return basePointer;
}
unsigned long getReturnAddress() {
/** Caller's return address.*/
unsigned long returnAddress;
/**
* The following inline assembly code copies the caller's base pointer into the temporary register %rax.
* The second instruction the uses RIP Relative Addressing Mode to obtain the caller's return address held at 8 bytes above its base pointer (rbp).
*/
asm("movq 0(%%rbp), %%rax; movq 8(%%rax), %0;" : "=r" (returnAddress));
return returnAddress;
}
void printStackFrameData(unsigned long basePointer, unsigned long previousBasePointer) {
/** Size of the frame delimited by the two base pointers*/
int frameSize;
frameSize = previousBasePointer - basePointer;
for (int offset = ZERO; offset < frameSize; offset += BYTES_PER_LINE) {
/** String containing the 64bit number held at the memory address given by (basePointer+offset) in hexadecimal.*/
char* numberInHex = malloc(DEFAULT_STRING_LENGTH + ONE);
/** Points to the unsigne long variable containing the 64bit number held at the memory address given by (basePointer+offset) in decimal.*/
unsigned long *ptr_numberInDec = malloc(__SIZEOF_LONG__);
/**
* This x86-64 instruction copy the 64bit number stored at the memory address (basePointer+offset) in the C variable ptr_numberInDec.
*/
asm("movq 0(%1), %0;" : "=r" (*ptr_numberInDec) : "r" (basePointer + offset));
/** Stores the 64 bit number as a 16 digit hexadecimal string in numberInHex.*/
sprintf(numberInHex, "%016lx", *ptr_numberInDec);
/**
* Print line in the following format:
* {memory address}: {64bit number in hexadecimal} {64bit number's 8 individual bytes as separate hexadecimal numbers}
*/
printf("%016lx: %016lx --", basePointer+offset, *ptr_numberInDec);
for (int i = DEFAULT_STRING_LENGTH - ONE; i > ZERO; i -= TWO) {
printf(" %c%c", numberInHex[i - ONE], numberInHex[i]);
}
/** If the memory address is the base pointer, prints out a dashed line to indicate end/beginning of Stack Frame.*/
if (offset == ZERO) { printf("\n-------------");}
printf("\n");
free(numberInHex);
free(ptr_numberInDec);
}
}
void printStackFrames(int number) {
/** At initialization: holds the base pointer of printStackFrames.*/
unsigned long basePointer;
/** Points to the value (memory address) stored in C variable basePointer.*/
unsigned long *ptr_basePointer = &basePointer;
*ptr_basePointer = getBasePointer();
/** At initialization: holds the base pointer of printStackFrames' caller.*/
unsigned long previousBasePointer;
/** Points to the value (memory address) stored in previousBasePointer.*/
unsigned long *ptr_previousBasePointer = &previousBasePointer;
/** Initializes previousBasePointer.*/
asm("movq 0(%%rbp), %0;" : "=r" (*ptr_previousBasePointer) : "r" (*ptr_basePointer));
/**
* This for loop prints the given number of stack frames starting from the caller's stack frame.
*
* It prints the stack frame data between the lowest memory address (base pointer) and the highest memory address (previous base pointer).
* Then, it replaces the current base pointer by the value of the previous base pointer.
* And updates the previous base pointer by gettting the previous previous base pointer using inline assembler.
*/
for (int j = ZERO; j < (number + ONE); j++) {
printStackFrameData(*ptr_basePointer, *ptr_previousBasePointer); // Prints the Stack Frame Data
*ptr_basePointer = *ptr_previousBasePointer; // sets the current Base Pointer as the Previous Base Pointer
asm("movq 0(%1), %0;" : "=r" (*ptr_previousBasePointer) : "r" (*ptr_basePointer)); // gets the previous previous Base Pointer to be the new previous base pointer.
}
}