summaryrefslogtreecommitdiff
path: root/src/gallium/auxiliary/util/u_debug_stack_android.cpp
blob: 11e2d3962b5ba251512ea5fdac369fe621baaed6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright (C) 2018 Stefan Schake <stschake@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <backtrace/Backtrace.h>

#include "u_debug.h"
#include "u_debug_stack.h"
#include "util/hash_table.h"
#include "os/os_thread.h"

static hash_table *backtrace_table;
static mtx_t table_mutex = _MTX_INITIALIZER_NP;

void
debug_backtrace_capture(debug_stack_frame *mesa_backtrace,
                        unsigned start_frame,
                        unsigned nr_frames)
{
   hash_entry *backtrace_entry;
   Backtrace *backtrace;
   pid_t tid = gettid();

   if (!nr_frames)
      return;

   /* We keep an Android Backtrace handler around for each thread */
   mtx_lock(&table_mutex);
   if (!backtrace_table)
      backtrace_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
                                                _mesa_key_pointer_equal);

   backtrace_entry = _mesa_hash_table_search(backtrace_table, (void*)(uintptr_t) tid);
   if (!backtrace_entry) {
      backtrace = Backtrace::Create(getpid(), tid);
      _mesa_hash_table_insert(backtrace_table, (void*)(uintptr_t) tid, backtrace);
   } else {
      backtrace = (Backtrace *) backtrace_entry->data;
   }
   mtx_unlock(&table_mutex);

   /* Add one to exclude this call. Unwind already ignores itself. */
   backtrace->Unwind(start_frame + 1);

   /* Store the Backtrace handler in the first mesa frame for reference.
    * Unwind will generally return less frames than nr_frames specified
    * but we have no good way of storing the real count otherwise.
    * The Backtrace handler only stores the results until the next Unwind,
    * but that is how u_debug_stack is used anyway.
    */
   mesa_backtrace->function = backtrace;
}

void
debug_backtrace_dump(const debug_stack_frame *mesa_backtrace,
                     unsigned nr_frames)
{
   Backtrace *backtrace = (Backtrace *) mesa_backtrace->function;
   size_t i;

   if (!nr_frames)
      return;

   if (nr_frames > backtrace->NumFrames())
      nr_frames = backtrace->NumFrames();
   for (i = 0; i < nr_frames; i++) {
      /* There is no prescribed format and this isn't interpreted further,
       * so we simply use the default Android format.
       */
      const std::string& frame_line = backtrace->FormatFrameData(i);
      debug_printf("%s\n", frame_line.c_str());
   }
}

void
debug_backtrace_print(FILE *f,
                      const debug_stack_frame *mesa_backtrace,
                      unsigned nr_frames)
{
   Backtrace *backtrace = (Backtrace *) mesa_backtrace->function;
   size_t i;

   if (!nr_frames)
      return;

   if (nr_frames > backtrace->NumFrames())
      nr_frames = backtrace->NumFrames();
   for (i = 0; i < nr_frames; i++) {
      const std::string& frame_line = backtrace->FormatFrameData(i);
      fprintf(f, "%s\n", frame_line.c_str());
   }
}