]> git.cworth.org Git - vogl/blob - src/libbacktrace/btrace.h
Initial vogl checkin
[vogl] / src / libbacktrace / btrace.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 #ifndef BTRACE_H
27 #define BTRACE_H
28
29 #include <stdint.h>
30 #include <string.h>
31
32 #include "vogl_dynamic_string.h"
33
34 namespace vogl
35 {
36     class json_node;
37 }
38
39 struct btrace_info
40 {
41     uintptr_t addr;
42     uintptr_t offset;
43     const char *module;   // Guaranteed to be !NULL.
44     const char *function; //
45     const char *filename; //
46     int linenumber;
47     char demangled_func_buf[512];
48 };
49
50 /*
51  * Walk up the stack getting the instruction pointers and stuffing them into your 
52  *  array. We will skip the first <addrs_to_skip> count of addrs, and we will return
53  *  the count of addrs we found. You can pass the addrs to btrace_resolve_addr() to
54  *  get modules, function names, etc.
55  */
56 int btrace_get(uintptr_t *addrs, size_t count_addrs, uint32_t addrs_to_skip);
57
58 /*
59  * Get symbol information given an instruction pointer.
60  */
61 #define BTRACE_RESOLVE_ADDR_GET_FILENAME   0x00000001
62 #define BTRACE_RESOLVE_ADDR_DEMANGLE_FUNC  0x00000002
63 bool btrace_resolve_addr(btrace_info *info, uintptr_t addr, uint32_t flags);
64
65 /*
66  * Get debug filename (or NULL if not found).
67  */
68 const char *btrace_get_debug_filename(const char *filename);
69
70 /*
71  * Walk up the stack until we find a module that isn't the same as the one we're in.
72  *  Return the full path string to that module. Will return NULL if nothing is found.
73  */
74 const char *btrace_get_calling_module();
75 const char *btrace_get_current_module();
76
77 /*
78  * Called when a new module is dlopen'd.
79  */
80 void btrace_dlopen_notify(const char *filename);
81
82 /*
83  * Return information about the current machine.
84  */
85 bool btrace_get_machine_info(vogl::json_node *machine_info);
86
87 /*
88  * Set information about current gl context.
89  */
90 struct btrace_glinfo
91 {
92     vogl::dynamic_string version_str;
93     vogl::dynamic_string glsl_version_str;
94     vogl::dynamic_string vendor_str;
95     vogl::dynamic_string renderer_str;
96 };
97 btrace_glinfo& btrace_get_glinfo();
98
99 int btrace_dump();
100 const char *btrace_demangle_function(const char *name, char *buffer, size_t buflen);
101
102 struct btrace_module_info
103 {
104     //$ TODO mikesart: need an ID number in here. This will get incremented
105     // everytime we have an address conflict. All stack traces should get this
106     // ID so they know which module they should get symbols from.
107     uintptr_t base_address;
108     uint32_t address_size;
109     struct backtrace_state *backtrace_state;
110     const char *filename;
111     int uuid_len;
112     uint8_t uuid[20];
113     int is_exe;
114 };
115
116 /*
117  * Explicitly add a module to the module list. Used for symbol resolving.
118  */
119 bool btrace_dlopen_add_module(const btrace_module_info &module_info);
120
121 /*
122  * Uuid helper functions.
123  */
124 int btrace_uuid_str_to_uuid(uint8_t uuid[20], const char *uuid_str);
125 void btrace_uuid_to_str(char uuid_str[41], const uint8_t *uuid, int len);
126
127 inline bool
128 operator <(const btrace_module_info &info1, const btrace_module_info &info2)
129 {
130     if (info1.base_address < info2.base_address)
131         return true;
132     if(info1.base_address == info2.base_address)
133     {
134         if(info1.address_size < info2.address_size)
135             return true;
136         if (info1.address_size == info2.address_size)
137         {
138             if (strcmp(info1.filename, info2.filename) < 0)
139                 return false;
140         }
141     }
142     return false;
143 }
144
145 inline bool
146 operator ==(const btrace_module_info &info1, const btrace_module_info &info2)
147 {
148     return (info1.base_address == info2.base_address) &&
149            (info1.address_size == info2.address_size) &&
150             !strcmp(info1.filename, info2.filename);
151 }
152
153 #endif // BTRACE_H