1/*
2 * Copyright (C) 2009  Internet Systems Consortium, Inc. ("ISC")
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
15 */
16
17/* $Id: backtrace.h,v 1.2 2009/09/01 18:40:25 jinmei Exp $ */
18
19/*! \file isc/backtrace.h
20 * \brief provide a back trace of the running process to help debug problems.
21 *
22 * This module tries to get a back trace of the process using some platform
23 * dependent way when available.  It also manages an internal symbol table
24 * that maps function addresses used in the process to their textual symbols.
25 * This module is expected to be used to help debug when some fatal error
26 * happens.
27 *
28 * IMPORTANT NOTE: since the (major) intended use case of this module is
29 * dumping a back trace on a fatal error, normally followed by self termination,
30 * functions defined in this module generally doesn't employ assertion checks
31 * (if it did, a program bug could cause infinite recursive calls to a
32 * backtrace function).  These functions still perform minimal checks and return
33 * ISC_R_FAILURE if they detect an error, but the caller should therefore be
34 * very careful about the use of these functions, and generally discouraged to
35 * use them except in an exit path.  The exception is
36 * isc_backtrace_getsymbolfromindex(), which is expected to be used in a
37 * non-error-handling context and validates arguments with assertion checks.
38 */
39
40#ifndef ISC_BACKTRACE_H
41#define ISC_BACKTRACE_H 1
42
43/***
44 ***	Imports
45 ***/
46
47#include <isc/types.h>
48
49/***
50 *** Types
51 ***/
52struct isc_backtrace_symmap {
53	void		*addr;
54	const char	*symbol;
55};
56
57extern const int isc__backtrace_nsymbols;
58extern const isc_backtrace_symmap_t isc__backtrace_symtable[];
59
60/***
61 *** Functions
62 ***/
63
64ISC_LANG_BEGINDECLS
65isc_result_t
66isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes);
67/*%<
68 * Get a back trace of the running process above this function itself.  On
69 * success, addrs[i] will store the address of the call point of the i-th
70 * stack frame (addrs[0] is the caller of this function).  *nframes will store
71 * the total number of frames.
72 *
73 * Requires (note that these are not ensured by assertion checks, see above):
74 *
75 *\li	'addrs' is a valid array containing at least 'maxaddrs' void * entries.
76 *
77 *\li	'nframes' must be non NULL.
78 *
79 * Returns:
80 *
81 *\li	#ISC_R_SUCCESS
82 *\li	#ISC_R_FAILURE
83 *\li	#ISC_R_NOTFOUND
84 *\li	#ISC_R_NOTIMPLEMENTED
85 */
86
87isc_result_t
88isc_backtrace_getsymbolfromindex(int idx, const void **addrp,
89				 const char **symbolp);
90/*%<
91 * Returns the content of the internal symbol table of the given index.
92 * On success, *addrsp and *symbolp point to the address and the symbol of
93 * the 'index'th entry of the table, respectively.  If 'idx' is not in the
94 * range of the symbol table, ISC_R_RANGE will be returned.
95 *
96 * Requires
97 *
98 *\li	'addrp' must be non NULL && '*addrp' == NULL.
99 *
100 *\li	'symbolp' must be non NULL && '*symbolp' == NULL.
101 *
102 * Returns:
103 *
104 *\li	#ISC_R_SUCCESS
105 *\li	#ISC_R_RANGE
106 */
107
108isc_result_t
109isc_backtrace_getsymbol(const void *addr, const char **symbolp,
110			unsigned long *offsetp);
111/*%<
112 * Searches the internal symbol table for the symbol that most matches the
113 * given 'addr'.  On success, '*symbolp' will point to the name of function
114 * to which the address 'addr' belong, and '*offsetp' will store the offset
115 * from the function's entry address to 'addr'.
116 *
117 * Requires (note that these are not ensured by assertion checks, see above):
118 *
119 *\li	'symbolp' must be non NULL && '*symbolp' == NULL.
120 *
121 *\li	'offsetp' must be non NULL.
122 *
123 * Returns:
124 *
125 *\li	#ISC_R_SUCCESS
126 *\li	#ISC_R_FAILURE
127 *\li	#ISC_R_NOTFOUND
128 */
129ISC_LANG_ENDDECLS
130
131#endif	/* ISC_BACKTRACE_H */
132