17ec681f3Smrg/*
27ec681f3Smrg * Copyright (C) 2018 Stefan Schake <stschake@gmail.com>
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#include <backtrace/Backtrace.h>
257ec681f3Smrg
267ec681f3Smrg#include "util/u_debug.h"
277ec681f3Smrg#include "u_debug_stack.h"
287ec681f3Smrg#include "util/hash_table.h"
297ec681f3Smrg#include "os/os_thread.h"
307ec681f3Smrg
317ec681f3Smrgstatic hash_table *symbol_table;
327ec681f3Smrgstatic mtx_t table_mutex = _MTX_INITIALIZER_NP;
337ec681f3Smrg
347ec681f3Smrgstatic const char *
357ec681f3Smrgintern_symbol(const char *symbol)
367ec681f3Smrg{
377ec681f3Smrg   if (!symbol_table)
387ec681f3Smrg      symbol_table = _mesa_hash_table_create(NULL, NULL, _mesa_key_string_equal);
397ec681f3Smrg
407ec681f3Smrg   uint32_t hash = _mesa_hash_string(symbol);
417ec681f3Smrg   hash_entry *entry =
427ec681f3Smrg      _mesa_hash_table_search_pre_hashed(symbol_table, hash, symbol);
437ec681f3Smrg   if (!entry)
447ec681f3Smrg      entry = _mesa_hash_table_insert_pre_hashed(symbol_table, hash, symbol, strdup(symbol));
457ec681f3Smrg
467ec681f3Smrg   return (const char *) entry->data;
477ec681f3Smrg}
487ec681f3Smrg
497ec681f3Smrgvoid
507ec681f3Smrgdebug_backtrace_capture(debug_stack_frame *backtrace,
517ec681f3Smrg                        unsigned start_frame,
527ec681f3Smrg                        unsigned nr_frames)
537ec681f3Smrg{
547ec681f3Smrg   Backtrace *bt;
557ec681f3Smrg
567ec681f3Smrg   if (!nr_frames)
577ec681f3Smrg      return;
587ec681f3Smrg
597ec681f3Smrg   bt = Backtrace::Create(BACKTRACE_CURRENT_PROCESS,
607ec681f3Smrg                          BACKTRACE_CURRENT_THREAD);
617ec681f3Smrg   if (bt == NULL) {
627ec681f3Smrg      for (unsigned i = 0; i < nr_frames; i++)
637ec681f3Smrg         backtrace[i].procname = NULL;
647ec681f3Smrg      return;
657ec681f3Smrg   }
667ec681f3Smrg
677ec681f3Smrg   /* Add one to exclude this call. Unwind already ignores itself. */
687ec681f3Smrg   bt->Unwind(start_frame + 1);
697ec681f3Smrg
707ec681f3Smrg   mtx_lock(&table_mutex);
717ec681f3Smrg
727ec681f3Smrg   for (unsigned i = 0; i < nr_frames; i++) {
737ec681f3Smrg      const backtrace_frame_data_t* frame = bt->GetFrame(i);
747ec681f3Smrg      if (frame) {
757ec681f3Smrg         backtrace[i].procname = intern_symbol(frame->func_name.c_str());
767ec681f3Smrg         backtrace[i].start_ip = frame->pc;
777ec681f3Smrg         backtrace[i].off = frame->func_offset;
787ec681f3Smrg         backtrace[i].map = intern_symbol(frame->map.Name().c_str());
797ec681f3Smrg         backtrace[i].map_off = frame->rel_pc;
807ec681f3Smrg      } else {
817ec681f3Smrg         backtrace[i].procname = NULL;
827ec681f3Smrg      }
837ec681f3Smrg   }
847ec681f3Smrg
857ec681f3Smrg   mtx_unlock(&table_mutex);
867ec681f3Smrg
877ec681f3Smrg   delete bt;
887ec681f3Smrg}
897ec681f3Smrg
907ec681f3Smrgvoid
917ec681f3Smrgdebug_backtrace_dump(const debug_stack_frame *backtrace,
927ec681f3Smrg                     unsigned nr_frames)
937ec681f3Smrg{
947ec681f3Smrg   for (unsigned i = 0; i < nr_frames; i++) {
957ec681f3Smrg      if (backtrace[i].procname)
967ec681f3Smrg         debug_printf(
977ec681f3Smrg            "%s(+0x%x)\t%012" PRIx64 ": %s+0x%x\n",
987ec681f3Smrg            backtrace[i].map,
997ec681f3Smrg            backtrace[i].map_off,
1007ec681f3Smrg            backtrace[i].start_ip,
1017ec681f3Smrg            backtrace[i].procname,
1027ec681f3Smrg            backtrace[i].off);
1037ec681f3Smrg   }
1047ec681f3Smrg}
1057ec681f3Smrg
1067ec681f3Smrgvoid
1077ec681f3Smrgdebug_backtrace_print(FILE *f,
1087ec681f3Smrg                      const debug_stack_frame *backtrace,
1097ec681f3Smrg                      unsigned nr_frames)
1107ec681f3Smrg{
1117ec681f3Smrg   for (unsigned i = 0; i < nr_frames; i++) {
1127ec681f3Smrg      if (backtrace[i].procname)
1137ec681f3Smrg         fprintf(f,
1147ec681f3Smrg                 "%s(+0x%x)\t%012" PRIx64 ": %s+0x%x\n",
1157ec681f3Smrg                 backtrace[i].map,
1167ec681f3Smrg                 backtrace[i].map_off,
1177ec681f3Smrg                 backtrace[i].start_ip,
1187ec681f3Smrg                 backtrace[i].procname,
1197ec681f3Smrg                 backtrace[i].off);
1207ec681f3Smrg   }
1217ec681f3Smrg}
122