blob: e4beb2854781c6e3a06cac899e0ce7283059fdaf [file] [log] [blame]
Luca Clementi327064b2013-07-23 00:11:35 -07001/*
2 * Copyright (c) 2013 Luca Clementi <luca.clementi@gmail.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "defs.h"
28#include <limits.h>
29#include <libunwind-ptrace.h>
30
Masatake YAMATO4e121e52014-04-16 15:33:05 +090031#define DPRINTF(F, A, ...) if (debug_flag) fprintf(stderr, " [unwind(" A ")] " F "\n", __VA_ARGS__)
32
Luca Clementi327064b2013-07-23 00:11:35 -070033/*
34 * Кeep a sorted array of cache entries,
35 * so that we can binary search through it.
36 */
37struct mmap_cache_t {
38 /**
39 * example entry:
40 * 7fabbb09b000-7fabbb09f000 r--p 00179000 fc:00 1180246 /lib/libc-2.11.1.so
41 *
42 * start_addr is 0x7fabbb09b000
43 * end_addr is 0x7fabbb09f000
44 * mmap_offset is 0x179000
45 * binary_filename is "/lib/libc-2.11.1.so"
46 */
47 unsigned long start_addr;
48 unsigned long end_addr;
49 unsigned long mmap_offset;
50 char* binary_filename;
51};
52
Masatake YAMATO2d534da2014-04-16 15:33:04 +090053/*
54 * Type used in stacktrace walker
55 */
56typedef void (*call_action_fn)(void *data,
57 char *binary_filename,
58 char *symbol_name,
59 unw_word_t function_off_set,
60 unsigned long true_offset);
61typedef void (*error_action_fn)(void *data,
62 const char *error,
63 unsigned long true_offset);
64
65
Luca Clementi327064b2013-07-23 00:11:35 -070066static unw_addr_space_t libunwind_as;
67
68void
Masatake YAMATO61413922014-04-16 15:33:02 +090069unwind_init(void)
Luca Clementi327064b2013-07-23 00:11:35 -070070{
71 libunwind_as = unw_create_addr_space(&_UPT_accessors, 0);
72 if (!libunwind_as)
73 error_msg_and_die("failed to create address space for stack tracing");
74}
75
76void
Masatake YAMATO61413922014-04-16 15:33:02 +090077unwind_tcb_init(struct tcb *tcp)
Luca Clementi327064b2013-07-23 00:11:35 -070078{
79 tcp->libunwind_ui = _UPT_create(tcp->pid);
80 if (!tcp->libunwind_ui)
81 die_out_of_memory();
82}
83
84void
Masatake YAMATO61413922014-04-16 15:33:02 +090085unwind_tcb_fin(struct tcb *tcp)
Luca Clementi327064b2013-07-23 00:11:35 -070086{
Masatake YAMATO61413922014-04-16 15:33:02 +090087 unwind_cache_invalidate(tcp);
Luca Clementi327064b2013-07-23 00:11:35 -070088 _UPT_destroy(tcp->libunwind_ui);
89 tcp->libunwind_ui = NULL;
90}
91
92/*
93 * caching of /proc/ID/maps for each process to speed up stack tracing
94 *
95 * The cache must be refreshed after some syscall: mmap, mprotect, munmap, execve
96 */
Masatake YAMATOb65042f2014-04-16 15:33:00 +090097static void
Luca Clementi327064b2013-07-23 00:11:35 -070098alloc_mmap_cache(struct tcb* tcp)
99{
100 unsigned long start_addr, end_addr, mmap_offset;
101 char filename[sizeof ("/proc/0123456789/maps")];
102 char buffer[PATH_MAX + 80];
103 char binary_path[PATH_MAX];
104 struct mmap_cache_t *cur_entry, *prev_entry;
105 /* start with a small dynamically-allocated array and then expand it */
106 size_t cur_array_size = 10;
107 struct mmap_cache_t *cache_head;
108 FILE *fp;
109
110 sprintf(filename, "/proc/%d/maps", tcp->pid);
111 fp = fopen(filename, "r");
112 if (!fp) {
113 perror_msg("fopen: %s", filename);
114 return;
115 }
116
117 cache_head = calloc(cur_array_size, sizeof(*cache_head));
118 if (!cache_head)
119 die_out_of_memory();
120
121 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
122 binary_path[0] = '\0'; // 'reset' it just to be paranoid
123
124 sscanf(buffer, "%lx-%lx %*c%*c%*c%*c %lx %*x:%*x %*d %[^\n]",
125 &start_addr, &end_addr, &mmap_offset, binary_path);
126
127 /* ignore special 'fake files' like "[vdso]", "[heap]", "[stack]", */
128 if (binary_path[0] == '[') {
129 continue;
130 }
131
132 if (binary_path[0] == '\0') {
133 continue;
134 }
135
136 if (end_addr < start_addr)
137 perror_msg_and_die("%s: unrecognized maps file format",
138 filename);
139
140 cur_entry = &cache_head[tcp->mmap_cache_size];
141 cur_entry->start_addr = start_addr;
142 cur_entry->end_addr = end_addr;
143 cur_entry->mmap_offset = mmap_offset;
144 cur_entry->binary_filename = strdup(binary_path);
145
146 /*
147 * sanity check to make sure that we're storing
148 * non-overlapping regions in ascending order
149 */
150 if (tcp->mmap_cache_size > 0) {
151 prev_entry = &cache_head[tcp->mmap_cache_size - 1];
152 if (prev_entry->start_addr >= cur_entry->start_addr)
153 perror_msg_and_die("Overlaying memory region in %s",
154 filename);
155 if (prev_entry->end_addr > cur_entry->start_addr)
156 perror_msg_and_die("Overlaying memory region in %s",
157 filename);
158 }
159 tcp->mmap_cache_size++;
160
161 /* resize doubling its size */
162 if (tcp->mmap_cache_size >= cur_array_size) {
163 cur_array_size *= 2;
164 cache_head = realloc(cache_head, cur_array_size * sizeof(*cache_head));
165 if (!cache_head)
166 die_out_of_memory();
167 }
168 }
169 fclose(fp);
170 tcp->mmap_cache = cache_head;
171}
172
173/* deleting the cache */
174void
Masatake YAMATO61413922014-04-16 15:33:02 +0900175unwind_cache_invalidate(struct tcb* tcp)
Luca Clementi327064b2013-07-23 00:11:35 -0700176{
177 unsigned int i;
178 for (i = 0; i < tcp->mmap_cache_size; i++) {
179 free(tcp->mmap_cache[i].binary_filename);
180 tcp->mmap_cache[i].binary_filename = NULL;
181 }
182 free(tcp->mmap_cache);
183 tcp->mmap_cache = NULL;
184 tcp->mmap_cache_size = 0;
185}
186
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900187/*
188 * walking the stack
189 */
190static void
191stacktrace_walk(struct tcb *tcp,
192 call_action_fn call_action,
193 error_action_fn error_action,
194 void *data)
Luca Clementi327064b2013-07-23 00:11:35 -0700195{
196 unw_word_t ip;
197 unw_cursor_t cursor;
198 unw_word_t function_off_set;
199 int stack_depth = 0, ret_val;
200 /* these are used for the binary search through the mmap_chace */
201 unsigned int lower, upper, mid;
202 size_t symbol_name_size = 40;
203 char * symbol_name;
204 struct mmap_cache_t* cur_mmap_cache;
205 unsigned long true_offset;
206
207 if (!tcp->mmap_cache)
208 alloc_mmap_cache(tcp);
209 if (!tcp->mmap_cache || !tcp->mmap_cache_size)
210 return;
211
212 symbol_name = malloc(symbol_name_size);
213 if (!symbol_name)
214 die_out_of_memory();
215
216 if (unw_init_remote(&cursor, libunwind_as, tcp->libunwind_ui) < 0)
217 perror_msg_and_die("Can't initiate libunwind");
218
219 do {
220 /* looping on the stack frame */
221 if (unw_get_reg(&cursor, UNW_REG_IP, &ip) < 0) {
222 perror_msg("Can't walk the stack of process %d", tcp->pid);
223 break;
224 }
225
226 lower = 0;
227 upper = tcp->mmap_cache_size - 1;
228
229 while (lower <= upper) {
230 /* find the mmap_cache and print the stack frame */
231 mid = (upper + lower) / 2;
232 cur_mmap_cache = &tcp->mmap_cache[mid];
233
234 if (ip >= cur_mmap_cache->start_addr &&
235 ip < cur_mmap_cache->end_addr) {
236 for (;;) {
237 symbol_name[0] = '\0';
238 ret_val = unw_get_proc_name(&cursor, symbol_name,
239 symbol_name_size, &function_off_set);
240 if (ret_val != -UNW_ENOMEM)
241 break;
242 symbol_name_size *= 2;
243 symbol_name = realloc(symbol_name, symbol_name_size);
244 if (!symbol_name)
245 die_out_of_memory();
246 }
247
248 true_offset = ip - cur_mmap_cache->start_addr +
249 cur_mmap_cache->mmap_offset;
250 if (symbol_name[0]) {
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900251 call_action(data,
252 cur_mmap_cache->binary_filename,
253 symbol_name,
254 function_off_set,
255 true_offset);
Luca Clementi327064b2013-07-23 00:11:35 -0700256 } else {
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900257 call_action(data,
258 cur_mmap_cache->binary_filename,
259 symbol_name,
260 0,
261 true_offset);
Luca Clementi327064b2013-07-23 00:11:35 -0700262 }
Luca Clementi327064b2013-07-23 00:11:35 -0700263 break; /* stack frame printed */
264 }
265 else if (mid == 0) {
266 /*
267 * there is a bug in libunwind >= 1.0
268 * after a set_tid_address syscall
269 * unw_get_reg returns IP == 0
270 */
271 if(ip)
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900272 error_action(data,
273 "backtracing_error", 0);
Luca Clementi327064b2013-07-23 00:11:35 -0700274 goto ret;
275 }
276 else if (ip < cur_mmap_cache->start_addr)
Masatake YAMATOb4a2de82014-04-16 15:32:59 +0900277 upper = mid;
Luca Clementi327064b2013-07-23 00:11:35 -0700278 else
279 lower = mid + 1;
280
281 }
282 if (lower > upper) {
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900283 error_action(data,
284 "backtracing_error", ip);
Luca Clementi327064b2013-07-23 00:11:35 -0700285 goto ret;
286 }
287
288 ret_val = unw_step(&cursor);
289
290 if (++stack_depth > 255) {
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900291 error_action(data,
292 "too many stack frames", 0);
Luca Clementi327064b2013-07-23 00:11:35 -0700293 break;
294 }
295 } while (ret_val > 0);
296ret:
297 free(symbol_name);
298}
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900299
300/*
301 * printing an entry in stack
302 */
303/*
304 * we want to keep the format used by backtrace_symbols from the glibc
305 *
306 * ./a.out() [0x40063d]
307 * ./a.out() [0x4006bb]
308 * ./a.out() [0x4006c6]
309 * /lib64/libc.so.6(__libc_start_main+0xed) [0x7fa2f8a5976d]
310 * ./a.out() [0x400569]
311 */
312#define STACK_ENTRY_SYMBOL_FMT \
313 " > %s(%s+0x%lx) [0x%lx]\n", \
314 binary_filename, \
315 symbol_name, \
316 function_off_set, \
317 true_offset
318#define STACK_ENTRY_NOSYMBOL_FMT \
319 " > %s() [0x%lx]\n", \
320 binary_filename, true_offset
321#define STACK_ENTRY_BUG_FMT \
322 " > BUG IN %s\n"
323#define STACK_ENTRY_ERROR_WITH_OFFSET_FMT \
324 " > %s [0x%lx]\n", error, true_offset
325#define STACK_ENTRY_ERROR_FMT \
326 " > %s\n", error
327
328static void
329print_call_cb(void *dummy,
330 char *binary_filename,
331 char *symbol_name,
332 unw_word_t function_off_set,
333 unsigned long true_offset)
334{
335 if (symbol_name)
336 tprintf(STACK_ENTRY_SYMBOL_FMT);
337 else if (binary_filename)
338 tprintf(STACK_ENTRY_NOSYMBOL_FMT);
339 else
340 tprintf(STACK_ENTRY_BUG_FMT, __FUNCTION__);
341
342 line_ended();
343}
344
345static void
346print_error_cb(void *dummy,
347 const char *error,
348 unsigned long true_offset)
349{
350 if (true_offset)
351 tprintf(STACK_ENTRY_ERROR_WITH_OFFSET_FMT);
352 else
353 tprintf(STACK_ENTRY_ERROR_FMT);
354
355 line_ended();
356}
357
358/*
359 * printing stack
360 */
361void
362unwind_print_stacktrace(struct tcb* tcp)
363{
364 stacktrace_walk(tcp, print_call_cb, print_error_cb, NULL);
365}