1 /*
2 * Copyright (C) 2018 Stefan Schake <[email protected]>
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 "u_debug_stack.h"
25
26 #if WITH_LIBBACKTRACE
27
28 #include <backtrace/Backtrace.h>
29
30 #include "util/simple_mtx.h"
31 #include "util/u_debug.h"
32 #include "util/hash_table.h"
33 #include "util/u_thread.h"
34
35 static hash_table *symbol_table;
36 static simple_mtx_t table_mutex = SIMPLE_MTX_INITIALIZER;
37
38 static const char *
intern_symbol(const char * symbol)39 intern_symbol(const char *symbol)
40 {
41 if (!symbol_table)
42 symbol_table = _mesa_hash_table_create(NULL, NULL, _mesa_key_string_equal);
43
44 uint32_t hash = _mesa_hash_string(symbol);
45 hash_entry *entry =
46 _mesa_hash_table_search_pre_hashed(symbol_table, hash, symbol);
47 if (!entry)
48 entry = _mesa_hash_table_insert_pre_hashed(symbol_table, hash, symbol, strdup(symbol));
49
50 return (const char *) entry->data;
51 }
52
53 void
debug_backtrace_capture(debug_stack_frame * backtrace,unsigned start_frame,unsigned nr_frames)54 debug_backtrace_capture(debug_stack_frame *backtrace,
55 unsigned start_frame,
56 unsigned nr_frames)
57 {
58 Backtrace *bt;
59
60 if (!nr_frames)
61 return;
62
63 bt = Backtrace::Create(BACKTRACE_CURRENT_PROCESS,
64 BACKTRACE_CURRENT_THREAD);
65 if (bt == NULL) {
66 for (unsigned i = 0; i < nr_frames; i++)
67 backtrace[i].procname = NULL;
68 return;
69 }
70
71 /* Add one to exclude this call. Unwind already ignores itself. */
72 bt->Unwind(start_frame + 1);
73
74 simple_mtx_lock(&table_mutex);
75
76 for (unsigned i = 0; i < nr_frames; i++) {
77 const backtrace_frame_data_t* frame = bt->GetFrame(i);
78 if (frame) {
79 backtrace[i].procname = intern_symbol(frame->func_name.c_str());
80 backtrace[i].start_ip = frame->pc;
81 backtrace[i].off = frame->func_offset;
82 backtrace[i].map = intern_symbol(frame->map.Name().c_str());
83 backtrace[i].map_off = frame->rel_pc;
84 } else {
85 backtrace[i].procname = NULL;
86 }
87 }
88
89 simple_mtx_unlock(&table_mutex);
90
91 delete bt;
92 }
93
94 void
debug_backtrace_dump(const debug_stack_frame * backtrace,unsigned nr_frames)95 debug_backtrace_dump(const debug_stack_frame *backtrace,
96 unsigned nr_frames)
97 {
98 for (unsigned i = 0; i < nr_frames; i++) {
99 if (backtrace[i].procname)
100 debug_printf(
101 "%s(+0x%x)\t%012" PRIx64 ": %s+0x%x\n",
102 backtrace[i].map,
103 backtrace[i].map_off,
104 backtrace[i].start_ip,
105 backtrace[i].procname,
106 backtrace[i].off);
107 }
108 }
109
110 void
debug_backtrace_print(FILE * f,const debug_stack_frame * backtrace,unsigned nr_frames)111 debug_backtrace_print(FILE *f,
112 const debug_stack_frame *backtrace,
113 unsigned nr_frames)
114 {
115 for (unsigned i = 0; i < nr_frames; i++) {
116 if (backtrace[i].procname)
117 fprintf(f,
118 "%s(+0x%x)\t%012" PRIx64 ": %s+0x%x\n",
119 backtrace[i].map,
120 backtrace[i].map_off,
121 backtrace[i].start_ip,
122 backtrace[i].procname,
123 backtrace[i].off);
124 }
125 }
126
127 #else
128
129 void
debug_backtrace_capture(debug_stack_frame * backtrace,unsigned start_frame,unsigned nr_frames)130 debug_backtrace_capture(debug_stack_frame *backtrace,
131 unsigned start_frame,
132 unsigned nr_frames)
133 {
134 }
135
136 void
debug_backtrace_dump(const debug_stack_frame * backtrace,unsigned nr_frames)137 debug_backtrace_dump(const debug_stack_frame *backtrace,
138 unsigned nr_frames)
139 {
140 }
141
142 void
debug_backtrace_print(FILE * f,const debug_stack_frame * backtrace,unsigned nr_frames)143 debug_backtrace_print(FILE *f,
144 const debug_stack_frame *backtrace,
145 unsigned nr_frames)
146 {
147 }
148
149 #endif // WITH_LIBBACKTRACE