| | vinyl-cache/lib/libvinyl/vbt.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2006 Verdens Gang AS |
| 2 |
|
* Copyright (c) 2006-2022 Varnish Software AS |
| 3 |
|
* All rights reserved. |
| 4 |
|
* |
| 5 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
| 6 |
|
* Author: Guillaume Quintard <guillaume@varnish-software.com> |
| 7 |
|
* Author: Dridi Boukelmoune <dridi.boukelmoune@gmail.com> |
| 8 |
|
* |
| 9 |
|
* Redistribution and use in source and binary forms, with or without |
| 10 |
|
* modification, are permitted provided that the following conditions |
| 11 |
|
* are met: |
| 12 |
|
* 1. Redistributions of source code must retain the above copyright |
| 13 |
|
* notice, this list of conditions and the following disclaimer. |
| 14 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 15 |
|
* notice, this list of conditions and the following disclaimer in the |
| 16 |
|
* documentation and/or other materials provided with the distribution. |
| 17 |
|
* |
| 18 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 19 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 22 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 |
|
* SUCH DAMAGE. |
| 29 |
|
* |
| 30 |
|
* We tend to print back-traces when there is a fatal error, so the VBT code |
| 31 |
|
* should avoid assertions. |
| 32 |
|
*/ |
| 33 |
|
|
| 34 |
|
#include "config.h" |
| 35 |
|
|
| 36 |
|
#include <stdlib.h> |
| 37 |
|
#include <stdio.h> |
| 38 |
|
#include <string.h> |
| 39 |
|
|
| 40 |
|
#ifdef WITH_UNWIND |
| 41 |
|
# include <libunwind.h> |
| 42 |
|
#endif |
| 43 |
|
|
| 44 |
|
#ifdef HAVE_EXECINFO_H |
| 45 |
|
# include <execinfo.h> |
| 46 |
|
#endif |
| 47 |
|
|
| 48 |
|
#include "vdef.h" |
| 49 |
|
#include "vas.h" |
| 50 |
|
#include "vbt.h" |
| 51 |
|
#include "vsb.h" |
| 52 |
|
|
| 53 |
|
#include "miniobj.h" |
| 54 |
|
|
| 55 |
|
#ifdef WITH_UNWIND |
| 56 |
|
static int |
| 57 |
33 |
vbt_unwind(struct vsb *vsb) |
| 58 |
|
{ |
| 59 |
|
unw_cursor_t cursor; unw_context_t uc; |
| 60 |
|
unw_word_t ip, sp; |
| 61 |
|
unw_word_t offp; |
| 62 |
|
char fname[1024]; |
| 63 |
|
const char *sep; |
| 64 |
|
int ret; |
| 65 |
|
|
| 66 |
33 |
ret = unw_getcontext(&uc); |
| 67 |
33 |
if (ret != 0) { |
| 68 |
0 |
VSB_printf(vsb, "Backtrace not available " |
| 69 |
0 |
"(unw_getcontext returned %d)\n", ret); |
| 70 |
0 |
return (-1); |
| 71 |
|
} |
| 72 |
33 |
ret = unw_init_local(&cursor, &uc); |
| 73 |
33 |
if (ret != 0) { |
| 74 |
0 |
VSB_printf(vsb, "Backtrace not available " |
| 75 |
0 |
"(unw_init_local returned %d)\n", ret); |
| 76 |
0 |
return (-1); |
| 77 |
|
} |
| 78 |
678 |
while (unw_step(&cursor) > 0) { |
| 79 |
645 |
fname[0] = '\0'; |
| 80 |
645 |
sep = ""; |
| 81 |
645 |
if (!unw_get_reg(&cursor, UNW_REG_IP, &ip)) { |
| 82 |
645 |
VSB_printf(vsb, "ip=0x%lx", (long) ip); |
| 83 |
645 |
sep = " "; |
| 84 |
645 |
} |
| 85 |
645 |
if (!unw_get_reg(&cursor, UNW_REG_SP, &sp)) { |
| 86 |
645 |
VSB_printf(vsb, "%ssp=0x%lx", sep, (long) sp); |
| 87 |
645 |
sep = " "; |
| 88 |
645 |
} |
| 89 |
645 |
if (!unw_get_proc_name(&cursor, fname, sizeof(fname), &offp)) { |
| 90 |
1158 |
VSB_printf(vsb, "%s<%s+0x%lx>", |
| 91 |
579 |
sep, fname[0] ? fname : "<unknown>", (long)offp); |
| 92 |
579 |
} |
| 93 |
645 |
VSB_putc(vsb, '\n'); |
| 94 |
|
} |
| 95 |
|
|
| 96 |
33 |
return (0); |
| 97 |
33 |
} |
| 98 |
|
#endif |
| 99 |
|
|
| 100 |
|
#ifdef HAVE_EXECINFO_H |
| 101 |
|
# define BACKTRACE_LEVELS 20 |
| 102 |
|
|
| 103 |
|
static void |
| 104 |
0 |
vbt_execinfo(struct vsb *vsb) |
| 105 |
|
{ |
| 106 |
|
void *array[BACKTRACE_LEVELS]; |
| 107 |
|
size_t size; |
| 108 |
|
size_t i; |
| 109 |
|
char **strings; |
| 110 |
|
char *p; |
| 111 |
|
char buf[32]; |
| 112 |
|
|
| 113 |
0 |
size = backtrace (array, BACKTRACE_LEVELS); |
| 114 |
0 |
if (size > BACKTRACE_LEVELS) { |
| 115 |
0 |
VSB_printf(vsb, "Backtrace not available (ret=%zu)\n", size); |
| 116 |
0 |
return; |
| 117 |
|
} |
| 118 |
0 |
for (i = 0; i < size; i++) { |
| 119 |
0 |
bprintf(buf, "%p", array[i]); |
| 120 |
0 |
VSB_printf(vsb, "%s: ", buf); |
| 121 |
0 |
strings = backtrace_symbols(&array[i], 1); |
| 122 |
0 |
if (strings == NULL || strings[0] == NULL) { |
| 123 |
0 |
VSB_cat(vsb, "(?)"); |
| 124 |
0 |
} else { |
| 125 |
0 |
p = strings[0]; |
| 126 |
0 |
if (!memcmp(buf, p, vstrlen(buf))) { |
| 127 |
0 |
p += vstrlen(buf); |
| 128 |
0 |
if (*p == ':') |
| 129 |
0 |
p++; |
| 130 |
0 |
while (*p == ' ') |
| 131 |
0 |
p++; |
| 132 |
0 |
} |
| 133 |
0 |
VSB_cat(vsb, p); |
| 134 |
|
} |
| 135 |
0 |
VSB_cat(vsb, "\n"); |
| 136 |
0 |
free(strings); |
| 137 |
0 |
} |
| 138 |
0 |
} |
| 139 |
|
#endif |
| 140 |
|
|
| 141 |
|
void |
| 142 |
33 |
VBT_format(struct vsb *vsb) |
| 143 |
|
{ |
| 144 |
|
|
| 145 |
33 |
if (!VALID_OBJ(vsb, VSB_MAGIC)) |
| 146 |
0 |
return; |
| 147 |
|
#ifdef WITH_UNWIND |
| 148 |
33 |
if (!vbt_unwind(vsb)) |
| 149 |
33 |
return; |
| 150 |
|
# ifdef HAVE_EXECINFO_H |
| 151 |
0 |
VSB_cat(vsb, "Falling back to execinfo backtrace\n"); |
| 152 |
|
# endif |
| 153 |
|
#endif |
| 154 |
|
|
| 155 |
|
#ifdef HAVE_EXECINFO_H |
| 156 |
0 |
vbt_execinfo(vsb); |
| 157 |
|
#endif |
| 158 |
33 |
} |
| 159 |
|
|
| 160 |
|
int |
| 161 |
0 |
VBT_dump(size_t len, char buf[len]) |
| 162 |
|
{ |
| 163 |
|
struct vsb vsb[1]; |
| 164 |
|
|
| 165 |
0 |
if (VSB_init(vsb, buf, len) == NULL) |
| 166 |
0 |
return (-1); |
| 167 |
|
|
| 168 |
0 |
VSB_cat(vsb, "Backtrace:\n"); |
| 169 |
0 |
VSB_indent(vsb, 2); |
| 170 |
0 |
VBT_format(vsb); |
| 171 |
0 |
VSB_indent(vsb, -2); |
| 172 |
|
|
| 173 |
0 |
return (0); |
| 174 |
0 |
} |