]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_backtrace.cpp
Initial vogl checkin
[vogl] / src / voglcore / vogl_backtrace.cpp
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 // File vogl_backtrace.cpp
28 #include "vogl_backtrace.h"
29
30 #if VOGL_USE_LINUX_API
31 #define USE_LIBBACKTRACE 0
32
33 #if USE_LIBBACKTRACE
34 #include "backtrace.h"
35 #else
36 #include <linux/unistd.h>
37 #include <execinfo.h>
38 #endif
39 #endif
40
41 namespace vogl
42 {
43 #if VOGL_USE_LINUX_API
44
45 #if !USE_LIBBACKTRACE
46     bool get_printable_backtrace(dynamic_string_array &string_vec)
47     {
48         string_vec.resize(0);
49
50         const uint BUF_SIZE = 256;
51         void *buffer[BUF_SIZE];
52
53         int nptrs = backtrace(buffer, BUF_SIZE);
54         if (!nptrs)
55             return false;
56
57         char **strings = backtrace_symbols(buffer, nptrs);
58         if (!strings)
59             return false;
60
61         string_vec.resize(nptrs);
62
63         for (int i = 0; i < nptrs; i++)
64             string_vec[i].set(strings[i] ? strings[i] : "?");
65
66         free(strings);
67
68         return true;
69     }
70 #else
71     bool get_printable_backtrace(dynamic_string_array &string_vec)
72     {
73         string_vec.resize(0);
74
75         const int N = 128;
76         uintptr_t pcs[N];
77         int n = backtrace_simple_get_pcs(pcs, N);
78         if (!n)
79             return false;
80
81         VOGL_ASSERT(n <= N);
82         if (n > N)
83             return false;
84
85         string_vec.reserve(n);
86
87         for (int i = 0; i < n; i++)
88         {
89             stackframe_info info;
90             utils::zero_object(info);
91             if (backtrace_simple_resolve_pc(&info, pcs[i]))
92             {
93                 string_vec.enlarge(1)->format("%u %s(%i), PC: 0x%llX, Ofs: 0x%llX, Mod: %s, Filename: \"%s\"", i,
94                                               info.function ? demangle(info.function).get_ptr() : "?",
95                                               info.linenumber,
96                                               (uint64_t)info.pc, (uint64_t)info.offset,
97                                               info.module ? info.module : "?",
98                                               info.filename ? info.filename : "?");
99             }
100             else
101             {
102                 string_vec.push_back("?");
103             }
104         }
105
106         return true;
107     }
108 #endif // !USE_LIBBACKTRACE
109
110 #else  // !VOGL_USE_LINUX_API
111
112     bool get_printable_backtrace(dynamic_string_array &strings)
113     {
114         strings.resize(0);
115
116         console::debug("TODO: %s\n", VOGL_FUNCTION_NAME);
117
118         return false;
119     }
120 #endif // VOGL_USE_LINUX_API
121
122 } // namespace vogl