没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
This document describes how to analyze and retrieve the call stack for an executing program written in C or C++. The process can be quite complicated because of the unique features of certain processors and operating systems that are sometimes important when making the analysis. We have attempted to make things easier by providing several examples; after reading the general descriptions, we suggest you look at the examples and return to reread the descriptive sections in the context of an example
资源推荐
资源详情
资源评论
Call stack analysis
Document ID: N/A
Version: 1.0
Status: Draft
Distribution: Internal
raw.doc Page 1 of 47
1
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
2
Revision History
Date Version Author Summary
14nov0
6
0.1 Laurent Cruaud
Xavier Pouyollon
1’st Draft
28nov0
6
0.2 Francis Lynch Revised draft
Review List
Name Role
Notication List
Name Role
raw.doc Page 2 of 47
3
41
42
43
44
45
46
47
48
49
50
51
4
Table of Contents
1 Introduction........................................................4
2 The Structure of the Call Stack.....................................4
3 The Structure of the Call Frame.....................................5
4 Debugging Information To Assist Call Stack Analysis.................6
4.1 Notation conventions............................................9
4.2 Interpreting the .debug_frame information.......................9
4.3 How to retrieve a register value when it was not saved.........10
5 Call Stack Analysis in the Absence of Debugging Information........12
6 simpleC: Linux, ARM, C, GNU example................................12
7 simpleC: vxWorks, Pentium, C, GNU example..........................18
8 BallTest: VxWorks, PPC, C++, DIAB..................................25
9 ballTest: VxWorks, ARM, C++, vxWorks, GNU..........................31
10 ballTest: VxWorks, ARM, C++, vxWorks, Diab........................37
11 broken BallTest: VxWorks, PPC, C++, GNU...........................44
raw.doc Page 3 of 47
5
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
6
1 Introduction
This document describes how to analyze and retrieve the call stack for an executing
program written in C or C++. The process can be quite complicated because of the
unique features of certain processors and operating systems that are sometimes important
when making the analysis. We have attempted to make things easier by providing several
examples; after reading the general descriptions, we suggest you look at the examples and
return to reread the descriptive sections in the context of an example.
2 The Structure of the Call Stack
Because C and C++ are languages that require re-entrant and even possibly recursive
functions, any implementation must support local variables that are unique to an
invocation of a function. These local variables are stored in an area of memory called a
call frame
1
, along with other information needed to implement this instance of the
function call (usually including the return address to the function which called it). During
execution of a thread in a program, a function may call another function, which in turns
calls yet another function, and so on. The sequence of call frames which is built by a
succession of function calls is called the call stack because it represents a LIFO (last in,
first out) stack of function calls. We can represent the call stack with a diagram like this,
with each call frame identified by a level number that starts at 0 for the currently
executing function:
Level 0
Level 1
Level 2
...
Where Level2 calls Level1
Where Level1 calls Level0 (top most level, where you find the current pc)
For each call frame, we may want to know the following information:
1. The value of the program counter when the function was either stopped (in the
case of frame level 0) or suspended due to its making a function call (in the case
of all other frames).
2. The name of the function which created the call frame.
3. The return address for the frame.
4. The values of the arguments supplied to the function.
5. The values of the local variables in scope when the function was stopped or
suspended.
Notice that item 3 (the return address) for stack frame N is closely related to item 1 (the
program counter) for stack frame N+1, but they are not the same. For all stack frames
other than level 0, the program counter for that frame is the address of the instruction
1
¹ The call frame is sometimes also called an activation record.
raw.doc Page 4 of 47
7
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
106
107
108
109
110
111
8
9
which called the function, but the return address for the called function is the address of
the instruction following the function call instruction. On some machine architectures, it
can be difficult to determine the address of the instruction which called a function,
because all that is saved in the called frame is the return address; if a machine has
variable length instructions, it is difficult (in general) to work backwards from the return
address to find the previous instruction.
The depth of the call stack (i.e., the number of call frames on the call stack) depends on
how many nested function calls have occurred since the thread began execution. The
bottom of the call stack is the oldest call frame on the call stack, and represents the call
frame where execution began for the currently executing thread. For the main thread of
the process, this is usually a frame that is set up by an assembly language startup routine
that then (directly or indirectly) calls the “main” function of the program. For other
threads, it will be the stack frame set up by the operating system when the thread is
created via some operating system routine. Identifying the bottom frame of a call stack is
often difficult, because the normal conventions for storing return addresses do not apply
to such frames but there is generally no way for the stack analysis routines to know that;
as a result they may obtain a spurious return address that leads to one or more bogus stack
frames (i.e., frames that do not really exist). This is one area of the current call stack
analysis that might be improved by making calls into the TOS component to determine if
there are operating-system-dependent ways of identifying the bottom of the call stack.
A list of the call frames in a call stack is sometimes called a back trace, since it traces
backwards the sequence of functions which were called in order to reach this point in the
execution of the thread. To produce a back trace and allow the user to evaluate and
manipulate variables and arguments for older call frames, the DFW needs at least to find
the base address for each call frame; given that and a knowledge of how arguments and
local variables are stored relative to the call frame of a particular function (which is
obtained from the symbolic debugging information included in the ELF file), the DFW
can determine the values of most variables and arguments. There are two special cases
that add complications to this:
1. The return address is almost always stored somewhere in the call frame, and we
need to determine how to find it.
2. The compiler may have assigned some variables to registers, so we may need to
determine how to find the value of a register in a particular call frame.
Both of these cases will be discussed in much greater detail below.
3 The Structure of the Call Frame
Every machine architecture that provides an implementation of C or C++ must provide an
area of memory that can be used for the call stack during the execution of the generated
code. A given call frame is identified by an address of a block of memory on the stack.
This address is referred to as the canonical frame address, or CFA, for the called
function. The CFA for a particular frame is a logical address and may point to either the
raw.doc Page 5 of 47
10
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
11
剩余46页未读,继续阅读
资源评论
xiangxueqiu
- 粉丝: 0
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功