1*eabc0478Schristos /* $NetBSD: backtrace.c,v 1.2 2024/08/18 20:47:14 christos Exp $ */ 2897be3a4Schristos 3897be3a4Schristos /* 4897be3a4Schristos * Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC") 5897be3a4Schristos * 6897be3a4Schristos * Permission to use, copy, modify, and/or distribute this software for any 7897be3a4Schristos * purpose with or without fee is hereby granted, provided that the above 8897be3a4Schristos * copyright notice and this permission notice appear in all copies. 9897be3a4Schristos * 10897be3a4Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 11897be3a4Schristos * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 12897be3a4Schristos * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 13897be3a4Schristos * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 14897be3a4Schristos * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 15897be3a4Schristos * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 16897be3a4Schristos * PERFORMANCE OF THIS SOFTWARE. 17897be3a4Schristos */ 18897be3a4Schristos 19897be3a4Schristos /* Id: backtrace.c,v 1.3 2009/09/02 23:48:02 tbox Exp */ 20897be3a4Schristos 21897be3a4Schristos /*! \file */ 22897be3a4Schristos 23897be3a4Schristos #include "config.h" 24897be3a4Schristos 25897be3a4Schristos #include <string.h> 26897be3a4Schristos #include <stdlib.h> 27897be3a4Schristos #ifdef HAVE_LIBCTRACE 28897be3a4Schristos #include <execinfo.h> 29897be3a4Schristos #endif 30897be3a4Schristos 31897be3a4Schristos #include <isc/backtrace.h> 32897be3a4Schristos #include <isc/result.h> 33897be3a4Schristos #include <isc/util.h> 34897be3a4Schristos 35897be3a4Schristos #ifdef ISC_PLATFORM_USEBACKTRACE 36897be3a4Schristos /* 37897be3a4Schristos * Getting a back trace of a running process is tricky and highly platform 38897be3a4Schristos * dependent. Our current approach is as follows: 39897be3a4Schristos * 1. If the system library supports the "backtrace()" function, use it. 40897be3a4Schristos * 2. Otherwise, if the compiler is gcc and the architecture is x86_64 or IA64, 41897be3a4Schristos * then use gcc's (hidden) Unwind_Backtrace() function. Note that this 42897be3a4Schristos * function doesn't work for C programs on many other architectures. 43897be3a4Schristos * 3. Otherwise, if the architecture x86 or x86_64, try to unwind the stack 44897be3a4Schristos * frame following frame pointers. This assumes the executable binary 45897be3a4Schristos * compiled with frame pointers; this is not always true for x86_64 (rather, 46897be3a4Schristos * compiler optimizations often disable frame pointers). The validation 47897be3a4Schristos * checks in getnextframeptr() hopefully rejects bogus values stored in 48897be3a4Schristos * the RBP register in such a case. If the backtrace function itself crashes 49897be3a4Schristos * due to this problem, the whole package should be rebuilt with 50897be3a4Schristos * --disable-backtrace. 51897be3a4Schristos */ 52897be3a4Schristos #ifdef HAVE_LIBCTRACE 53897be3a4Schristos #define BACKTRACE_LIBC 54897be3a4Schristos #elif defined(__GNUC__) && (defined(__x86_64__) || defined(__ia64__)) 55897be3a4Schristos #define BACKTRACE_GCC 56897be3a4Schristos #elif defined(__x86_64__) || defined(__i386__) 57897be3a4Schristos #define BACKTRACE_X86STACK 58897be3a4Schristos #else 59897be3a4Schristos #define BACKTRACE_DISABLED 60897be3a4Schristos #endif /* HAVE_LIBCTRACE */ 61897be3a4Schristos #else /* !ISC_PLATFORM_USEBACKTRACE */ 62897be3a4Schristos #define BACKTRACE_DISABLED 63897be3a4Schristos #endif /* ISC_PLATFORM_USEBACKTRACE */ 64897be3a4Schristos 65897be3a4Schristos #ifdef BACKTRACE_LIBC 66897be3a4Schristos isc_result_t 67897be3a4Schristos isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) { 68897be3a4Schristos int n; 69897be3a4Schristos 70897be3a4Schristos /* 71897be3a4Schristos * Validate the arguments: intentionally avoid using REQUIRE(). 72897be3a4Schristos * See notes in backtrace.h. 73897be3a4Schristos */ 74897be3a4Schristos if (addrs == NULL || nframes == NULL) 75897be3a4Schristos return (ISC_R_FAILURE); 76897be3a4Schristos 77897be3a4Schristos /* 78897be3a4Schristos * backtrace(3) includes this function itself in the address array, 79897be3a4Schristos * which should be eliminated from the returned sequence. 80897be3a4Schristos */ 81897be3a4Schristos n = backtrace(addrs, maxaddrs); 82897be3a4Schristos if (n < 2) 83897be3a4Schristos return (ISC_R_NOTFOUND); 84897be3a4Schristos n--; 85897be3a4Schristos memmove(addrs, &addrs[1], sizeof(void *) * n); 86897be3a4Schristos *nframes = n; 87897be3a4Schristos return (ISC_R_SUCCESS); 88897be3a4Schristos } 89897be3a4Schristos #elif defined(BACKTRACE_GCC) 90897be3a4Schristos extern int _Unwind_Backtrace(void* fn, void* a); 91897be3a4Schristos extern void* _Unwind_GetIP(void* ctx); 92897be3a4Schristos 93897be3a4Schristos typedef struct { 94897be3a4Schristos void **result; 95897be3a4Schristos int max_depth; 96897be3a4Schristos int skip_count; 97897be3a4Schristos int count; 98897be3a4Schristos } trace_arg_t; 99897be3a4Schristos 100897be3a4Schristos static int 101897be3a4Schristos btcallback(void *uc, void *opq) { 102897be3a4Schristos trace_arg_t *arg = (trace_arg_t *)opq; 103897be3a4Schristos 104897be3a4Schristos if (arg->skip_count > 0) 105897be3a4Schristos arg->skip_count--; 106897be3a4Schristos else 107897be3a4Schristos arg->result[arg->count++] = (void *)_Unwind_GetIP(uc); 108897be3a4Schristos if (arg->count == arg->max_depth) 109897be3a4Schristos return (5); /* _URC_END_OF_STACK */ 110897be3a4Schristos 111897be3a4Schristos return (0); /* _URC_NO_REASON */ 112897be3a4Schristos } 113897be3a4Schristos 114897be3a4Schristos isc_result_t 115897be3a4Schristos isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) { 116897be3a4Schristos trace_arg_t arg; 117897be3a4Schristos 118897be3a4Schristos /* Argument validation: see above. */ 119897be3a4Schristos if (addrs == NULL || nframes == NULL) 120897be3a4Schristos return (ISC_R_FAILURE); 121897be3a4Schristos 122897be3a4Schristos arg.skip_count = 1; 123897be3a4Schristos arg.result = addrs; 124897be3a4Schristos arg.max_depth = maxaddrs; 125897be3a4Schristos arg.count = 0; 126897be3a4Schristos _Unwind_Backtrace(btcallback, &arg); 127897be3a4Schristos 128897be3a4Schristos *nframes = arg.count; 129897be3a4Schristos 130897be3a4Schristos return (ISC_R_SUCCESS); 131897be3a4Schristos } 132897be3a4Schristos #elif defined(BACKTRACE_X86STACK) 133897be3a4Schristos #ifdef __x86_64__ 134897be3a4Schristos static unsigned long 135897be3a4Schristos getrbp() { 136897be3a4Schristos __asm("movq %rbp, %rax\n"); 137897be3a4Schristos } 138897be3a4Schristos #endif 139897be3a4Schristos 140897be3a4Schristos static void ** 141897be3a4Schristos getnextframeptr(void **sp) { 142897be3a4Schristos void **newsp = (void **)*sp; 143897be3a4Schristos 144897be3a4Schristos /* 145897be3a4Schristos * Perform sanity check for the new frame pointer, derived from 146897be3a4Schristos * google glog. This can actually be bogus depending on compiler. 147897be3a4Schristos */ 148897be3a4Schristos 149897be3a4Schristos /* prohibit the stack frames from growing downwards */ 150897be3a4Schristos if (newsp <= sp) 151897be3a4Schristos return (NULL); 152897be3a4Schristos 153897be3a4Schristos /* A heuristics to reject "too large" frame: this actually happened. */ 154897be3a4Schristos if ((char *)newsp - (char *)sp > 100000) 155897be3a4Schristos return (NULL); 156897be3a4Schristos 157897be3a4Schristos /* 158897be3a4Schristos * Not sure if other checks used in glog are needed at this moment. 159897be3a4Schristos * For our purposes we don't have to consider non-contiguous frames, 160897be3a4Schristos * for example. 161897be3a4Schristos */ 162897be3a4Schristos 163897be3a4Schristos return (newsp); 164897be3a4Schristos } 165897be3a4Schristos 166897be3a4Schristos isc_result_t 167897be3a4Schristos isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) { 168897be3a4Schristos int i = 0; 169897be3a4Schristos void **sp; 170897be3a4Schristos 171897be3a4Schristos /* Argument validation: see above. */ 172897be3a4Schristos if (addrs == NULL || nframes == NULL) 173897be3a4Schristos return (ISC_R_FAILURE); 174897be3a4Schristos 175897be3a4Schristos #ifdef __x86_64__ 176897be3a4Schristos sp = (void **)getrbp(); 177897be3a4Schristos if (sp == NULL) 178897be3a4Schristos return (ISC_R_NOTFOUND); 179897be3a4Schristos /* 180897be3a4Schristos * sp is the frame ptr of this function itself due to the call to 181897be3a4Schristos * getrbp(), so need to unwind one frame for consistency. 182897be3a4Schristos */ 183897be3a4Schristos sp = getnextframeptr(sp); 184897be3a4Schristos #else 185897be3a4Schristos /* 186897be3a4Schristos * i386: the frame pointer is stored 2 words below the address for the 187897be3a4Schristos * first argument. Note that the body of this function cannot be 188897be3a4Schristos * inlined since it depends on the address of the function argument. 189897be3a4Schristos */ 190897be3a4Schristos sp = (void **)&addrs - 2; 191897be3a4Schristos #endif 192897be3a4Schristos 193897be3a4Schristos while (sp != NULL && i < maxaddrs) { 194897be3a4Schristos addrs[i++] = *(sp + 1); 195897be3a4Schristos sp = getnextframeptr(sp); 196897be3a4Schristos } 197897be3a4Schristos 198897be3a4Schristos *nframes = i; 199897be3a4Schristos 200897be3a4Schristos return (ISC_R_SUCCESS); 201897be3a4Schristos } 202897be3a4Schristos #elif defined(BACKTRACE_DISABLED) 203897be3a4Schristos isc_result_t 204897be3a4Schristos isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) { 205897be3a4Schristos /* Argument validation: see above. */ 206897be3a4Schristos if (addrs == NULL || nframes == NULL) 207897be3a4Schristos return (ISC_R_FAILURE); 208897be3a4Schristos 209897be3a4Schristos UNUSED(maxaddrs); 210897be3a4Schristos 211897be3a4Schristos return (ISC_R_NOTIMPLEMENTED); 212897be3a4Schristos } 213897be3a4Schristos #endif 214897be3a4Schristos 215897be3a4Schristos isc_result_t 216897be3a4Schristos isc_backtrace_getsymbolfromindex(int idx, const void **addrp, 217897be3a4Schristos const char **symbolp) 218897be3a4Schristos { 219897be3a4Schristos REQUIRE(addrp != NULL && *addrp == NULL); 220897be3a4Schristos REQUIRE(symbolp != NULL && *symbolp == NULL); 221897be3a4Schristos 222897be3a4Schristos if (idx < 0 || idx >= isc__backtrace_nsymbols) 223897be3a4Schristos return (ISC_R_RANGE); 224897be3a4Schristos 225897be3a4Schristos *addrp = isc__backtrace_symtable[idx].addr; 226897be3a4Schristos *symbolp = isc__backtrace_symtable[idx].symbol; 227897be3a4Schristos return (ISC_R_SUCCESS); 228897be3a4Schristos } 229897be3a4Schristos 230897be3a4Schristos static int 231897be3a4Schristos symtbl_compare(const void *addr, const void *entryarg) { 232897be3a4Schristos const isc_backtrace_symmap_t *entry = entryarg; 233897be3a4Schristos const isc_backtrace_symmap_t *end = 234897be3a4Schristos &isc__backtrace_symtable[isc__backtrace_nsymbols - 1]; 235897be3a4Schristos 236897be3a4Schristos if (isc__backtrace_nsymbols == 1 || entry == end) { 237897be3a4Schristos if (addr >= entry->addr) { 238897be3a4Schristos /* 239897be3a4Schristos * If addr is equal to or larger than that of the last 240897be3a4Schristos * entry of the table, we cannot be sure if this is 241897be3a4Schristos * within a valid range so we consider it valid. 242897be3a4Schristos */ 243897be3a4Schristos return (0); 244897be3a4Schristos } 245897be3a4Schristos return (-1); 246897be3a4Schristos } 247897be3a4Schristos 248897be3a4Schristos /* entry + 1 is a valid entry from now on. */ 249897be3a4Schristos if (addr < entry->addr) 250897be3a4Schristos return (-1); 251897be3a4Schristos else if (addr >= (entry + 1)->addr) 252897be3a4Schristos return (1); 253897be3a4Schristos return (0); 254897be3a4Schristos } 255897be3a4Schristos 256897be3a4Schristos isc_result_t 257897be3a4Schristos isc_backtrace_getsymbol(const void *addr, const char **symbolp, 258897be3a4Schristos unsigned long *offsetp) 259897be3a4Schristos { 260897be3a4Schristos isc_result_t result = ISC_R_SUCCESS; 261897be3a4Schristos isc_backtrace_symmap_t *found; 262897be3a4Schristos 263897be3a4Schristos /* 264897be3a4Schristos * Validate the arguments: intentionally avoid using REQUIRE(). 265897be3a4Schristos * See notes in backtrace.h. 266897be3a4Schristos */ 267897be3a4Schristos if (symbolp == NULL || *symbolp != NULL || offsetp == NULL) 268897be3a4Schristos return (ISC_R_FAILURE); 269897be3a4Schristos 270897be3a4Schristos if (isc__backtrace_nsymbols < 1) 271897be3a4Schristos return (ISC_R_NOTFOUND); 272897be3a4Schristos 273897be3a4Schristos /* 274897be3a4Schristos * Search the table for the entry that meets: 275897be3a4Schristos * entry.addr <= addr < next_entry.addr. 276897be3a4Schristos */ 277897be3a4Schristos found = bsearch(addr, isc__backtrace_symtable, isc__backtrace_nsymbols, 278897be3a4Schristos sizeof(isc__backtrace_symtable[0]), symtbl_compare); 279897be3a4Schristos if (found == NULL) 280897be3a4Schristos result = ISC_R_NOTFOUND; 281897be3a4Schristos else { 282897be3a4Schristos *symbolp = found->symbol; 283897be3a4Schristos *offsetp = (u_long)((const char *)addr - (char *)found->addr); 284897be3a4Schristos } 285897be3a4Schristos 286897be3a4Schristos return (result); 287897be3a4Schristos } 288