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