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