]> granicus.if.org Git - strace/blob - unwind.c
Implement PTRACE_SECCOMP_GET_METADATA ptrace request decoding
[strace] / unwind.c
1 /*
2  * Copyright (c) 2013 Luca Clementi <luca.clementi@gmail.com>
3  * Copyright (c) 2013-2018 The strace developers.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "defs.h"
29 #include "mmap_cache.h"
30 #include <libunwind-ptrace.h>
31
32 #ifdef USE_DEMANGLE
33 # if defined HAVE_DEMANGLE_H
34 #  include <demangle.h>
35 # elif defined HAVE_LIBIBERTY_DEMANGLE_H
36 #  include <libiberty/demangle.h>
37 # endif
38 #endif
39
40 /*
41  * Type used in stacktrace walker
42  */
43 typedef void (*call_action_fn)(void *data,
44                                const char *binary_filename,
45                                const char *symbol_name,
46                                unw_word_t function_offset,
47                                unsigned long true_offset);
48 typedef void (*error_action_fn)(void *data,
49                                 const char *error,
50                                 unsigned long true_offset);
51
52 /*
53  * Type used in stacktrace capturing
54  */
55 struct call_t {
56         struct call_t *next;
57         char *output_line;
58 };
59
60 struct queue_t {
61         struct call_t *tail;
62         struct call_t *head;
63 };
64
65 static void queue_print(struct queue_t *queue);
66
67 static unw_addr_space_t libunwind_as;
68
69 static const char asprintf_error_str[] = "???";
70
71 void
72 unwind_init(void)
73 {
74         libunwind_as = unw_create_addr_space(&_UPT_accessors, 0);
75         if (!libunwind_as)
76                 error_msg_and_die("failed to create address space for stack tracing");
77         unw_set_caching_policy(libunwind_as, UNW_CACHE_GLOBAL);
78         mmap_cache_enable();
79 }
80
81 void
82 unwind_tcb_init(struct tcb *tcp)
83 {
84         if (tcp->libunwind_ui)
85                 return;
86
87         tcp->libunwind_ui = _UPT_create(tcp->pid);
88         if (!tcp->libunwind_ui)
89                 perror_msg_and_die("_UPT_create");
90
91         tcp->queue = xmalloc(sizeof(*tcp->queue));
92         tcp->queue->head = NULL;
93         tcp->queue->tail = NULL;
94 }
95
96 void
97 unwind_tcb_fin(struct tcb *tcp)
98 {
99         queue_print(tcp->queue);
100         free(tcp->queue);
101         tcp->queue = NULL;
102
103         _UPT_destroy(tcp->libunwind_ui);
104         tcp->libunwind_ui = NULL;
105 }
106
107 static void
108 get_symbol_name(unw_cursor_t *cursor, char **name,
109                 size_t *size, unw_word_t *offset)
110 {
111         for (;;) {
112                 int rc = unw_get_proc_name(cursor, *name, *size, offset);
113                 if (rc == 0)
114                         break;
115                 if (rc != -UNW_ENOMEM) {
116                         **name = '\0';
117                         *offset = 0;
118                         break;
119                 }
120                 *name = xgrowarray(*name, size, 1);
121         }
122 }
123
124 static int
125 print_stack_frame(struct tcb *tcp,
126                   call_action_fn call_action,
127                   error_action_fn error_action,
128                   void *data,
129                   unw_cursor_t *cursor,
130                   char **symbol_name,
131                   size_t *symbol_name_size)
132 {
133         unw_word_t ip;
134         struct mmap_cache_t *cur_mmap_cache;
135
136         if (unw_get_reg(cursor, UNW_REG_IP, &ip) < 0) {
137                 perror_msg("Can't walk the stack of process %d", tcp->pid);
138                 return -1;
139         }
140
141         cur_mmap_cache = mmap_cache_search(tcp, ip);
142         if (cur_mmap_cache
143             /* ignore mappings that have no PROT_EXEC bit set */
144             && (cur_mmap_cache->protections & MMAP_CACHE_PROT_EXECUTABLE)) {
145                 unsigned long true_offset;
146                 unw_word_t function_offset;
147
148                 get_symbol_name(cursor, symbol_name, symbol_name_size,
149                                 &function_offset);
150                 true_offset = ip - cur_mmap_cache->start_addr +
151                         cur_mmap_cache->mmap_offset;
152 #ifdef USE_DEMANGLE
153                 char *demangled_name =
154                         cplus_demangle(*symbol_name,
155                                        DMGL_AUTO | DMGL_PARAMS);
156 #endif
157                 call_action(data,
158                             cur_mmap_cache->binary_filename,
159 #ifdef USE_DEMANGLE
160                             demangled_name ? demangled_name :
161 #endif
162                             *symbol_name,
163                             function_offset,
164                             true_offset);
165 #ifdef USE_DEMANGLE
166                 free(demangled_name);
167 #endif
168
169                 return 0;
170         }
171
172         /*
173          * there is a bug in libunwind >= 1.0
174          * after a set_tid_address syscall
175          * unw_get_reg returns IP == 0
176          */
177         if (ip)
178                 error_action(data, "unexpected_backtracing_error", ip);
179         return -1;
180 }
181
182 /*
183  * walking the stack
184  */
185 static void
186 stacktrace_walk(struct tcb *tcp,
187                 call_action_fn call_action,
188                 error_action_fn error_action,
189                 void *data)
190 {
191         char *symbol_name;
192         size_t symbol_name_size = 40;
193         unw_cursor_t cursor;
194         int stack_depth;
195
196         if (!tcp->mmap_cache)
197                 error_msg_and_die("bug: mmap_cache is NULL");
198         if (tcp->mmap_cache_size == 0)
199                 error_msg_and_die("bug: mmap_cache is empty");
200
201         symbol_name = xmalloc(symbol_name_size);
202
203         if (unw_init_remote(&cursor, libunwind_as, tcp->libunwind_ui) < 0)
204                 perror_msg_and_die("Can't initiate libunwind");
205
206         for (stack_depth = 0; stack_depth < 256; ++stack_depth) {
207                 if (print_stack_frame(tcp, call_action, error_action, data,
208                                 &cursor, &symbol_name, &symbol_name_size) < 0)
209                         break;
210                 if (unw_step(&cursor) <= 0)
211                         break;
212         }
213         if (stack_depth >= 256)
214                 error_action(data, "too many stack frames", 0);
215
216         free(symbol_name);
217 }
218
219 /*
220  * printing an entry in stack to stream or buffer
221  */
222 /*
223  * we want to keep the format used by backtrace_symbols from the glibc
224  *
225  * ./a.out() [0x40063d]
226  * ./a.out() [0x4006bb]
227  * ./a.out() [0x4006c6]
228  * /lib64/libc.so.6(__libc_start_main+0xed) [0x7fa2f8a5976d]
229  * ./a.out() [0x400569]
230  */
231 #define STACK_ENTRY_SYMBOL_FMT                  \
232         " > %s(%s+0x%lx) [0x%lx]\n",            \
233         binary_filename,                        \
234         symbol_name,                            \
235         (unsigned long) function_offset,        \
236         true_offset
237 #define STACK_ENTRY_NOSYMBOL_FMT                \
238         " > %s() [0x%lx]\n",                    \
239         binary_filename, true_offset
240 #define STACK_ENTRY_BUG_FMT                     \
241         " > BUG IN %s\n"
242 #define STACK_ENTRY_ERROR_WITH_OFFSET_FMT       \
243         " > %s [0x%lx]\n", error, true_offset
244 #define STACK_ENTRY_ERROR_FMT                   \
245         " > %s\n", error
246
247 static void
248 print_call_cb(void *dummy,
249               const char *binary_filename,
250               const char *symbol_name,
251               unw_word_t function_offset,
252               unsigned long true_offset)
253 {
254         if (symbol_name && (symbol_name[0] != '\0'))
255                 tprintf(STACK_ENTRY_SYMBOL_FMT);
256         else if (binary_filename)
257                 tprintf(STACK_ENTRY_NOSYMBOL_FMT);
258         else
259                 tprintf(STACK_ENTRY_BUG_FMT, __func__);
260
261         line_ended();
262 }
263
264 static void
265 print_error_cb(void *dummy,
266                const char *error,
267                unsigned long true_offset)
268 {
269         if (true_offset)
270                 tprintf(STACK_ENTRY_ERROR_WITH_OFFSET_FMT);
271         else
272                 tprintf(STACK_ENTRY_ERROR_FMT);
273
274         line_ended();
275 }
276
277 static char *
278 sprint_call_or_error(const char *binary_filename,
279                      const char *symbol_name,
280                      unw_word_t function_offset,
281                      unsigned long true_offset,
282                      const char *error)
283 {
284         char *output_line = NULL;
285         int n;
286
287         if (symbol_name)
288                 n = asprintf(&output_line, STACK_ENTRY_SYMBOL_FMT);
289         else if (binary_filename)
290                 n = asprintf(&output_line, STACK_ENTRY_NOSYMBOL_FMT);
291         else if (error)
292                 n = true_offset
293                         ? asprintf(&output_line, STACK_ENTRY_ERROR_WITH_OFFSET_FMT)
294                         : asprintf(&output_line, STACK_ENTRY_ERROR_FMT);
295         else
296                 n = asprintf(&output_line, STACK_ENTRY_BUG_FMT, __func__);
297
298         if (n < 0) {
299                 perror_func_msg("asprintf");
300                 output_line = (char *) asprintf_error_str;
301         }
302
303         return output_line;
304 }
305
306 /*
307  * queue manipulators
308  */
309 static void
310 queue_put(struct queue_t *queue,
311           const char *binary_filename,
312           const char *symbol_name,
313           unw_word_t function_offset,
314           unsigned long true_offset,
315           const char *error)
316 {
317         struct call_t *call;
318
319         call = xmalloc(sizeof(*call));
320         call->output_line = sprint_call_or_error(binary_filename,
321                                                  symbol_name,
322                                                  function_offset,
323                                                  true_offset,
324                                                  error);
325         call->next = NULL;
326
327         if (!queue->head) {
328                 queue->head = call;
329                 queue->tail = call;
330         } else {
331                 queue->tail->next = call;
332                 queue->tail = call;
333         }
334 }
335
336 static void
337 queue_put_call(void *queue,
338                const char *binary_filename,
339                const char *symbol_name,
340                unw_word_t function_offset,
341                unsigned long true_offset)
342 {
343         queue_put(queue,
344                   binary_filename,
345                   symbol_name,
346                   function_offset,
347                   true_offset,
348                   NULL);
349 }
350
351 static void
352 queue_put_error(void *queue,
353                 const char *error,
354                 unsigned long ip)
355 {
356         queue_put(queue, NULL, NULL, 0, ip, error);
357 }
358
359 static void
360 queue_print(struct queue_t *queue)
361 {
362         struct call_t *call, *tmp;
363
364         queue->tail = NULL;
365         call = queue->head;
366         queue->head = NULL;
367         while (call) {
368                 tmp = call;
369                 call = call->next;
370
371                 tprints(tmp->output_line);
372                 line_ended();
373
374                 if (tmp->output_line != asprintf_error_str)
375                         free(tmp->output_line);
376
377                 tmp->output_line = NULL;
378                 tmp->next = NULL;
379                 free(tmp);
380         }
381 }
382
383 /*
384  * printing stack
385  */
386 void
387 unwind_print_stacktrace(struct tcb *tcp)
388 {
389 #if SUPPORTED_PERSONALITIES > 1
390         if (tcp->currpers != DEFAULT_PERSONALITY) {
391                 /* disable stack trace */
392                 return;
393         }
394 #endif
395         if (tcp->queue->head) {
396                 debug_func_msg("head: tcp=%p, queue=%p", tcp, tcp->queue->head);
397                 queue_print(tcp->queue);
398         } else switch (mmap_cache_rebuild_if_invalid(tcp, __func__)) {
399                 case MMAP_CACHE_REBUILD_RENEWED:
400                         unw_flush_cache(libunwind_as, 0, 0);
401                         ATTRIBUTE_FALLTHROUGH;
402                 case MMAP_CACHE_REBUILD_READY:
403                         debug_func_msg("walk: tcp=%p, queue=%p", tcp, tcp->queue->head);
404                         stacktrace_walk(tcp, print_call_cb, print_error_cb, NULL);
405                         break;
406                 default:
407                         /* Do nothing */
408                         ;
409         }
410 }
411
412 /*
413  * capturing stack
414  */
415 void
416 unwind_capture_stacktrace(struct tcb *tcp)
417 {
418 #if SUPPORTED_PERSONALITIES > 1
419         if (tcp->currpers != DEFAULT_PERSONALITY) {
420                 /* disable stack trace */
421                 return;
422         }
423 #endif
424         if (tcp->queue->head)
425                 error_msg_and_die("bug: unprinted entries in queue");
426
427         switch (mmap_cache_rebuild_if_invalid(tcp, __func__)) {
428         case MMAP_CACHE_REBUILD_RENEWED:
429                 unw_flush_cache(libunwind_as, 0, 0);
430                 ATTRIBUTE_FALLTHROUGH;
431         case MMAP_CACHE_REBUILD_READY:
432                 stacktrace_walk(tcp, queue_put_call, queue_put_error,
433                                 tcp->queue);
434                 debug_func_msg("tcp=%p, queue=%p", tcp, tcp->queue->head);
435                 break;
436         default:
437                 /* Do nothing */
438                 ;
439         }
440 }