File: MoreBacktrace.h

package info (click to toggle)
openmpi 1.6.5-9.1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 91,628 kB
  • ctags: 44,305
  • sloc: ansic: 408,966; cpp: 44,454; sh: 27,828; makefile: 10,486; asm: 3,882; python: 1,239; lex: 805; perl: 549; csh: 253; fortran: 232; f90: 126; tcl: 12
file content (274 lines) | stat: -rw-r--r-- 10,593 bytes parent folder | download | duplicates (5)
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
106
107
108
109
110
111
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
	File:		MoreBacktrace.h

	Contains:	Code for generating backtraces.

	Written by:	DTS

	Copyright:	Copyright (c) 2006 by Apple Computer, Inc., All Rights Reserved.

	Disclaimer:	IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
				("Apple") in consideration of your agreement to the following terms, and your
				use, installation, modification or redistribution of this Apple software
				constitutes acceptance of these terms.  If you do not agree with these terms,
				please do not use, install, modify or redistribute this Apple software.

				In consideration of your agreement to abide by the following terms, and subject
				to these terms, Apple grants you a personal, non-exclusive license, under Apples
				copyrights in this original Apple software (the "Apple Software"), to use,
				reproduce, modify and redistribute the Apple Software, with or without
				modifications, in source and/or binary forms; provided that if you redistribute
				the Apple Software in its entirety and without modifications, you must retain
				this notice and the following text and disclaimers in all such redistributions of
				the Apple Software.  Neither the name, trademarks, service marks or logos of
				Apple Computer, Inc. may be used to endorse or promote products derived from the
				Apple Software without specific prior written permission from Apple.  Except as
				expressly stated in this notice, no other rights or licenses, express or implied,
				are granted by Apple herein, including but not limited to any patent rights that
				may be infringed by your derivative works or by other works in which the Apple
				Software may be incorporated.

				The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
				WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
				WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
				PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
				COMBINATION WITH YOUR PRODUCTS.

				IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
				CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
				GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
				ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
				OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
				(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
				ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

	Change History (most recent first):

$Log: MoreBacktrace.h,v $
Revision 1.4  2006/01/22 22:46:48  eskimo1
Complete rewrite to account for architecture independence.

Revision 1.3  2003/04/09 22:48:15  eskimo1
Added comments.

Revision 1.2  2003/04/09 21:58:18  eskimo1
Removed the "uncertain PC" flag.  I may need to bring it back later, but for now I never use it so there's no point declaring it.

Revision 1.1  2003/04/04 15:03:08  eskimo1
First checked in.  This code still has bugs, but I've written enough code that checking in is a good idea.


*/

#pragma once

/////////////////////////////////////////////////////////////////

// MoreIsBetter Setup

#include "MoreSetup.h"

// Mac OS Interfaces

// Put <mach/mach.h> inside extern "C" guards for the C++ build 
// because the Mach header files don't always have them.

#if defined(__cplusplus)
	extern "C" {
#endif

#include <mach/mach.h>

#if defined(__cplusplus)
	}
#endif

#include <stdint.h>

/////////////////////////////////////////////////////////////////

#ifdef __cplusplus
extern "C" {
#endif

/////////////////////////////////////////////////////////////////

#if TARGET_RT_MAC_CFM
	#error MoreBacktrace no longer supports CFM builds.
#endif

/////////////////////////////////////////////////////////////////

/*	Overview
	--------
	This module implements a number of backtrace routines. All 
	of the routines are implemented in terms of a common core. 
	The code is structured in a very generic way.  For example, if 
	you were running on a version of Mach that support inter-machine 
	messaging, it would be feasible to do a backtrace of a PowerPC 
	program from program executing a completely different instruction 
	set architecture (ISA).
	
	IMPORTANT:
	The current code does not support cross-architecture backtraces 
	because of technical limitations (specifically, I haven't had 
	time to implement cross-architecture symbol translation, which 
	is required for cross-architecture backtraces to work properly; 
	see the comments for InitSigTrampAddress in the implementation 
	file for the details).
	
	Backtraces are inherently processor-specific.  Internal to this 
	module is a ISA later than adapts the backtrace for various ISA. 
	Currently it supports PowerPC (32-bit), PowerPC (64-bit), and 
	Intel (32-bit).
	
	If you're curious about how stack frames work for each ISA, check 
	out the comments in the implementation file.  The comments in the 
	header focus on how you use these routines.
*/

/*	Changes Since Previous Version
	------------------------------
	If you used the previous version of this code, please note the 
	following changes:
	
	o Everything has changed (-:
	
	I completely rewrote this code to support multiple architectures. 
	By including support for PowerPC (64-bit), I was forced to 
	eliminate my dependencies on CoreServices (which isn't available 
	to PowerPC (64-bit) on Mac OS X 10.4.x), which means now I depend 
	solely on the System framework.  Also, because I had to support 
	Intel, which requires Mach-O, I decided to drop support for CFM. 
	My theory is that anyone who wants to adopt the new version of this 
	module is doing so because they're porting to Intel, and those folks 
	have to leave their CFM build behind.
	
	The good news is that the new implementation is very similar 
	in spirit to the old, and it should be very easy for you to 
	adopt the new code.
*/

// Within this module, I treat all addresses as 64-bit.  That way 
// the code doesn't change for 64-bit and 32-bit architectures.  
//
// Note:
// I could have made this type scale with the current pointer size, 
// but my eventual goal is to support cross-architecture backtraces, 
// which means I wanted a size that's at least as big as the 'largest' 
// architecture I support.

typedef uint64_t MoreBTAddr;

// The following flags provide information about a specific frame in 
// a backtrace.
//
// IMPORTANT:
// kMoreBTFrameBadMask is set for the last frame in the backtrace 
// (where we've run off the end of the stack), but it can also be set 
// for intermediate frames (where we've detected a frameless leaf 
// routine, either at the top of the stack or as part of crossing 
// a signal frame).

typedef int MoreBTFlags;
enum {
	kMoreBTFrameBadMask      = 0x0001,	// this frame pointer is bad
	kMoreBTPCBadMask         = 0x0002,	// this PC is bad
	kMoreBTSignalHandlerMask = 0x0004	// this frame is a signal handler
};

// The end result of a backtrace is an array of MoreBTFrame 
// structures describing a particular frame in the backtrace.  
//
// Note:
// The PC points to the code that's using the frame.  It is not 
// the return address for that code.  On architectures where the 
// frame holds the return address (Intel, but not PowerPC), I do 
// the appropriate corrections.

struct MoreBTFrame {
	MoreBTAddr		pc;					// PC for this function invocation
	MoreBTAddr		fp;					// frame pointer for this function invocation
	MoreBTFlags		flags;				// various flags, see above
};
typedef struct MoreBTFrame MoreBTFrame;

/*	Common Parameters
	-----------------
	All of the backtrace routines accept certain common parameters.
	
	  o function result -- This is an errno-style error code.
	
	  o stackBottom and stackTop -- These define the extent of the stack 
		which you are tracing.  If this information isn't handy, supply 
		0 for both.  Supplying meaningful values can reduce the number 
		of bogus frames reported if the stack is corrupt.
	
	  o frameArray and frameArrayCount -- These define an array of stack 
		frames that the routines fill out.  You can supply NULL and 0 
		(respectively) if you're not interested in getting the actual 
		frame data (typically you do this to get the count of the number 
		of frames via frameCount).  The routines do not fail if this 
		buffer is exhausted.  Instead they simply return as many frames 
		as they can and continue tracing, returning an accurate value 
		for frameCount.
		
	  o frameCountPtr -- You can use this to get back an accurate count of 
		the number of frames in the stack.  If you're not interested 
		in this information, you can pass NULL.
	
	IMPORTANT:
	Because of the above, on return, *frameCountPtr can be larger than 
	frameArrayCount.
	
	The following assertions apply to these common parameters.
	
	o On entry, stackBottom and stackTop must both be zero, or stackBottom 
	  must be strictly less than stackTop.
	
	o On entry, if frameArrayCount is not zero, frameArray must be not NULL.
	
	o On entry, frameCountPtr must not be NULL.
	
	o On return, *frameCountPtr will be the number of frames that were 
	  found.  On success, this is likely to be a meaningful number.  On 
	  error, this just indicates how far we got.
	  
	o On return, if frameArrayCount is not NULL, frame data has been placed 
	  into the frame array.  The number of valid entries is 
	  min(frameArray, *frameArrayCount).
*/

extern int MoreBacktraceMachSelf(
	MoreBTAddr		stackBottom, 
	MoreBTAddr		stackTop,
	MoreBTFrame *	frameArray, 
	size_t			frameArrayCount, 
	size_t *		frameCountPtr
);
	// Does a backtrace of the current thread.  All of the parameters 
	// are described by the "Common Parameters" section above.

extern int MoreBacktraceMachThread(
	task_t			task, 
	thread_t		thread,
	MoreBTAddr		stackBottom, 
	MoreBTAddr		stackTop,
	MoreBTFrame *	frameArray, 
	size_t			frameArrayCount, 
	size_t *		frameCountPtr
);
	// Does a backtrace of a particular thread within a particular tasks. 
	// The common parameters (stackBottom and later) are described by the 
	// "Common Parameters" section above.  The task parameter must a 
	// task control port; for a thread in the current task, use 
	// mach_task_self to get this value.  The thread parameter is the thread 
	// to sample.  It's generally inappropriate to use mach_thread_self 
	// (call MoreBacktraceMachSelf instead), but it does work.
	//
	// On entry, task must not be MACH_PORT_NULL.
	// On entry, thread must not be MACH_PORT_NULL.

#ifdef __cplusplus
}
#endif