1 /*
2 * Copyright (C) 2018 Stefan Schake <stschake@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <backtrace/Backtrace.h>
25
26 #include "util/u_debug.h"
27 #include "u_debug_stack.h"
28 #include "util/hash_table.h"
29 #include "os/os_thread.h"
30
31 static hash_table *symbol_table;
32 static mtx_t table_mutex = _MTX_INITIALIZER_NP;
33
34 static const char *
intern_symbol(const char * symbol)35 intern_symbol(const char *symbol)
36 {
37 if (!symbol_table)
38 symbol_table = _mesa_hash_table_create(NULL, NULL, _mesa_key_string_equal);
39
40 uint32_t hash = _mesa_hash_string(symbol);
41 hash_entry *entry =
42 _mesa_hash_table_search_pre_hashed(symbol_table, hash, symbol);
43 if (!entry)
44 entry = _mesa_hash_table_insert_pre_hashed(symbol_table, hash, symbol, strdup(symbol));
45
46 return (const char *) entry->data;
47 }
48
49 void
debug_backtrace_capture(debug_stack_frame * backtrace,unsigned start_frame,unsigned nr_frames)50 debug_backtrace_capture(debug_stack_frame *backtrace,
51 unsigned start_frame,
52 unsigned nr_frames)
53 {
54 Backtrace *bt;
55
56 if (!nr_frames)
57 return;
58
59 bt = Backtrace::Create(BACKTRACE_CURRENT_PROCESS,
60 BACKTRACE_CURRENT_THREAD);
61 if (bt == NULL) {
62 for (unsigned i = 0; i < nr_frames; i++)
63 backtrace[i].procname = NULL;
64 return;
65 }
66
67 /* Add one to exclude this call. Unwind already ignores itself. */
68 bt->Unwind(start_frame + 1);
69
70 mtx_lock(&table_mutex);
71
72 for (unsigned i = 0; i < nr_frames; i++) {
73 const backtrace_frame_data_t* frame = bt->GetFrame(i);
74 if (frame) {
75 backtrace[i].procname = intern_symbol(frame->func_name.c_str());
76 backtrace[i].start_ip = frame->pc;
77 backtrace[i].off = frame->func_offset;
78 backtrace[i].map = intern_symbol(frame->map.Name().c_str());
79 backtrace[i].map_off = frame->rel_pc;
80 } else {
81 backtrace[i].procname = NULL;
82 }
83 }
84
85 mtx_unlock(&table_mutex);
86
87 delete bt;
88 }
89
90 void
debug_backtrace_dump(const debug_stack_frame * backtrace,unsigned nr_frames)91 debug_backtrace_dump(const debug_stack_frame *backtrace,
92 unsigned nr_frames)
93 {
94 for (unsigned i = 0; i < nr_frames; i++) {
95 if (backtrace[i].procname)
96 debug_printf(
97 "%s(+0x%x)\t%012" PRIx64 ": %s+0x%x\n",
98 backtrace[i].map,
99 backtrace[i].map_off,
100 backtrace[i].start_ip,
101 backtrace[i].procname,
102 backtrace[i].off);
103 }
104 }
105
106 void
debug_backtrace_print(FILE * f,const debug_stack_frame * backtrace,unsigned nr_frames)107 debug_backtrace_print(FILE *f,
108 const debug_stack_frame *backtrace,
109 unsigned nr_frames)
110 {
111 for (unsigned i = 0; i < nr_frames; i++) {
112 if (backtrace[i].procname)
113 fprintf(f,
114 "%s(+0x%x)\t%012" PRIx64 ": %s+0x%x\n",
115 backtrace[i].map,
116 backtrace[i].map_off,
117 backtrace[i].start_ip,
118 backtrace[i].procname,
119 backtrace[i].off);
120 }
121 }
122