varnish-cache/lib/libvarnish/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
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
        ret = unw_getcontext(&uc);
67
        if (ret != 0) {
68
                VSB_printf(vsb, "Backtrace not available "
69
                    "(unw_getcontext returned %d)\n", ret);
70
                return (-1);
71
        }
72
        ret = unw_init_local(&cursor, &uc);
73
        if (ret != 0) {
74
                VSB_printf(vsb, "Backtrace not available "
75
                    "(unw_init_local returned %d)\n", ret);
76
                return (-1);
77
        }
78
        while (unw_step(&cursor) > 0) {
79
                fname[0] = '\0';
80
                sep = "";
81
                if (!unw_get_reg(&cursor, UNW_REG_IP, &ip)) {
82
                        VSB_printf(vsb, "ip=0x%lx", (long) ip);
83
                        sep = " ";
84
                }
85
                if (!unw_get_reg(&cursor, UNW_REG_SP, &sp)) {
86
                        VSB_printf(vsb, "%ssp=0x%lx", sep, (long) sp);
87
                        sep = " ";
88
                }
89
                if (!unw_get_proc_name(&cursor, fname, sizeof(fname), &offp)) {
90
                        VSB_printf(vsb, "%s<%s+0x%lx>",
91
                            sep, fname[0] ? fname : "<unknown>", (long)offp);
92
                }
93
                VSB_putc(vsb, '\n');
94
        }
95
96
        return (0);
97
}
98
#endif
99
100
#ifdef HAVE_EXECINFO_H
101
#  define BACKTRACE_LEVELS      20
102
103
static void
104 516
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 516
        size = backtrace (array, BACKTRACE_LEVELS);
114 516
        if (size > BACKTRACE_LEVELS) {
115 0
                VSB_printf(vsb, "Backtrace not available (ret=%zu)\n", size);
116 0
                return;
117
        }
118 9263
        for (i = 0; i < size; i++) {
119 8747
                bprintf(buf, "%p", array[i]);
120 8747
                VSB_printf(vsb, "%s: ", buf);
121 8747
                strings = backtrace_symbols(&array[i], 1);
122 8747
                if (strings == NULL || strings[0] == NULL) {
123 0
                        VSB_cat(vsb, "(?)");
124 0
                } else {
125 8747
                        p = strings[0];
126 8747
                        if (!memcmp(buf, p, strlen(buf))) {
127 8747
                                p += strlen(buf);
128 8747
                                if (*p == ':')
129 0
                                        p++;
130 17494
                                while (*p == ' ')
131 8747
                                        p++;
132 8747
                        }
133 8747
                        VSB_cat(vsb, p);
134
                }
135 8747
                VSB_cat(vsb, "\n");
136 8747
                free(strings);
137 8747
        }
138 516
}
139
#endif
140
141
void
142 516
VBT_format(struct vsb *vsb)
143
{
144
145 516
        if (!VALID_OBJ(vsb, VSB_MAGIC))
146 0
                return;
147
#ifdef WITH_UNWIND
148
        if (!vbt_unwind(vsb))
149
                return;
150
#  ifdef HAVE_EXECINFO_H
151
        VSB_cat(vsb, "Falling back to execinfo backtrace\n");
152
#  endif
153
#endif
154
155
#ifdef HAVE_EXECINFO_H
156 516
        vbt_execinfo(vsb);
157
#endif
158 516
}