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 31static hash_table *backtrace_table; 32static mtx_t table_mutex = _MTX_INITIALIZER_NP; 33 34void 35debug_backtrace_capture(debug_stack_frame *mesa_backtrace, 36 unsigned start_frame, 37 unsigned nr_frames) 38{ 39 hash_entry *backtrace_entry; 40 Backtrace *backtrace; 41 pid_t tid = gettid(); 42 43 if (!nr_frames) 44 return; 45 46 /* We keep an Android Backtrace handler around for each thread */ 47 mtx_lock(&table_mutex); 48 if (!backtrace_table) 49 backtrace_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer, 50 _mesa_key_pointer_equal); 51 52 backtrace_entry = _mesa_hash_table_search(backtrace_table, (void*) (uintptr_t)tid); 53 if (!backtrace_entry) { 54 backtrace = Backtrace::Create(getpid(), tid); 55 _mesa_hash_table_insert(backtrace_table, (void*) (uintptr_t)tid, backtrace); 56 } else { 57 backtrace = (Backtrace *) backtrace_entry->data; 58 } 59 mtx_unlock(&table_mutex); 60 61 /* Add one to exclude this call. Unwind already ignores itself. */ 62 backtrace->Unwind(start_frame + 1); 63 64 /* Store the Backtrace handler in the first mesa frame for reference. 65 * Unwind will generally return less frames than nr_frames specified 66 * but we have no good way of storing the real count otherwise. 67 * The Backtrace handler only stores the results until the next Unwind, 68 * but that is how u_debug_stack is used anyway. 69 */ 70 mesa_backtrace->function = backtrace; 71} 72 73void 74debug_backtrace_dump(const debug_stack_frame *mesa_backtrace, 75 unsigned nr_frames) 76{ 77 Backtrace *backtrace = (Backtrace *) mesa_backtrace->function; 78 size_t i; 79 80 if (!nr_frames) 81 return; 82 83 if (nr_frames > backtrace->NumFrames()) 84 nr_frames = backtrace->NumFrames(); 85 for (i = 0; i < nr_frames; i++) { 86 /* There is no prescribed format and this isn't interpreted further, 87 * so we simply use the default Android format. 88 */ 89 const std::string& frame_line = backtrace->FormatFrameData(i); 90 debug_printf("%s\n", frame_line.c_str()); 91 } 92} 93 94void 95debug_backtrace_print(FILE *f, 96 const debug_stack_frame *mesa_backtrace, 97 unsigned nr_frames) 98{ 99 Backtrace *backtrace = (Backtrace *) mesa_backtrace->function; 100 size_t i; 101 102 if (!nr_frames) 103 return; 104 105 if (nr_frames > backtrace->NumFrames()) 106 nr_frames = backtrace->NumFrames(); 107 for (i = 0; i < nr_frames; i++) { 108 const std::string& frame_line = backtrace->FormatFrameData(i); 109 fprintf(f, "%s\n", frame_line.c_str()); 110 } 111} 112