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