1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
/* Copyright (c) 2006-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "str.h"
#include "backtrace-string.h"
#define MAX_STACK_SIZE 30
#define BACKTRACE_SKIP_PREFIX "backtrace_"
#if defined(HAVE_LIBUNWIND)
#include <libunwind.h>
static int backtrace_append_unwind(string_t *str)
{
size_t str_orig_size = str_len(str);
char proc_name[256];
int ret;
unsigned int fp = 0;
unw_cursor_t c;
unw_context_t ctx;
unw_proc_info_t pip;
bool success = FALSE;
if ((ret = unw_getcontext(&ctx)) != 0) {
str_printfa(str, "unw_getcontext() failed: %d", ret);
return -1;
}
if ((ret = unw_init_local(&c, &ctx)) != 0) {
str_printfa(str, "unw_init_local() failed: %d", ret);
return -1;
}
do {
str_printfa(str, "#%d ", fp);
if ((ret = unw_get_proc_info(&c, &pip)) != 0) {
str_printfa(str, "[unw_get_proc_info_failed(): %d]", ret);
} else if (pip.start_ip == 0 || pip.end_ip == 0) {
str_append(str, "[no start/end information]");
} else if ((ret = unw_get_proc_name(&c, proc_name, sizeof(proc_name), 0)) != 0 &&
ret != UNW_ENOMEM) {
str_printfa(str, "[unw_get_proc_name() failed: %d]", ret);
} else if (!success && str_begins(proc_name, BACKTRACE_SKIP_PREFIX)) {
str_truncate(str, str_orig_size);
continue;
} else {
str_append_max(str, proc_name, sizeof(proc_name));
str_printfa(str, "[0x%08zx]", pip.start_ip);
success = TRUE;
}
str_append(str, " -> ");
fp++;
} while ((ret = unw_step(&c)) > 0);
/* remove ' -> ' */
if (str->used > 4)
str_truncate(str, str->used - 4);
return ret == 0 && success ? 0 : -1;
}
#endif
#if defined(HAVE_BACKTRACE_SYMBOLS) && defined(HAVE_EXECINFO_H)
/* Linux */
#include <execinfo.h>
static int backtrace_append_libc(string_t *str)
{
size_t str_orig_size = str_len(str);
void *stack[MAX_STACK_SIZE];
char **strings;
int ret, i;
ret = backtrace(stack, N_ELEMENTS(stack));
if (ret <= 0)
return -1;
strings = backtrace_symbols(stack, ret);
for (i = 0; i < ret; i++) {
if (str_len(str) > str_orig_size)
str_append(str, " -> ");
if (strings == NULL) {
/* out of memory case */
str_printfa(str, "0x%p", stack[i]);
} else if (str_len(str) != str_orig_size ||
!str_begins(strings[i], BACKTRACE_SKIP_PREFIX))
str_append(str, strings[i]);
}
free(strings);
return 0;
}
#elif defined(HAVE_WALKCONTEXT) && defined(HAVE_UCONTEXT_H)
/* Solaris */
#include <ucontext.h>
struct walk_context {
string_t *str;
unsigned int pos;
};
static int walk_callback(uintptr_t ptr, int signo ATTR_UNUSED,
void *context)
{
struct walk_context *ctx = context;
if (ctx->pos > 0)
str_append(ctx->str, " -> ");
str_printfa(ctx->str, "0x%p", (void *)ptr);
ctx->pos++;
return 0;
}
static int backtrace_append_libc(string_t *str)
{
ucontext_t uc;
struct walk_context ctx;
if (getcontext(&uc) < 0)
return -1;
ctx.str = str;
ctx.pos = 0;
walkcontext(&uc, walk_callback, &ctx);
return 0;
}
#else
static int backtrace_append_libc(string_t *str ATTR_UNUSED)
{
return -1;
}
#endif
int backtrace_append(string_t *str)
{
#if defined(HAVE_LIBUNWIND)
size_t orig_len = str_len(str);
if (backtrace_append_unwind(str) == 0)
return 0;
/* failed to get useful backtrace. libc's own method is likely
better. */
str_truncate(str, orig_len);
#endif
return backtrace_append_libc(str);
}
int backtrace_get(const char **backtrace_r)
{
string_t *str;
str = t_str_new(512);
if (backtrace_append(str) < 0)
return -1;
*backtrace_r = str_c(str);
return 0;
}
|