Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games.
2 : *
3 : * Permission is hereby granted, free of charge, to any person obtaining
4 : * a copy of this software and associated documentation files (the
5 : * "Software"), to deal in the Software without restriction, including
6 : * without limitation the rights to use, copy, modify, merge, publish,
7 : * distribute, sublicense, and/or sell copies of the Software, and to
8 : * permit persons to whom the Software is furnished to do so, subject to
9 : * the following conditions:
10 : *
11 : * The above copyright notice and this permission notice shall be included
12 : * in all copies or substantial portions of the Software.
13 : *
14 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 : */
22 :
23 : // Note: This used to use BFD to get more useful debugging information.
24 : // (See SVN r8270 if you want that code.)
25 : // That requires binutils at runtime, which hits some bugs and library versioning
26 : // issues on some Linux distros.
27 : // The debugging info reported by this code with BFD wasn't especially useful,
28 : // and it's easy enough to get people to run in gdb if we want a proper backtrace.
29 : // So we now go with the simple approach of not using BFD.
30 :
31 : #include "precompiled.h"
32 :
33 : #include "lib/debug.h"
34 :
35 : #include "lib/code_annotation.h"
36 : #include "lib/posix/posix_types.h"
37 : #include "lib/secure_crt.h"
38 : #include "lib/status.h"
39 : #include "lib/sysdep/os.h"
40 :
41 : #include <algorithm>
42 : #include <cstdio>
43 : #include <cstdlib>
44 : #include <cstring>
45 : #include <cwchar>
46 : #include <string>
47 : #include <sys/syscall.h>
48 :
49 : #if OS_ANDROID
50 :
51 : // Android NDK doesn't support backtrace()
52 : // TODO: use unwind.h or similar?
53 :
54 : void* debug_GetCaller(void* /*context*/, const wchar_t* /*lastFuncToSkip*/)
55 : {
56 : return NULL;
57 : }
58 :
59 : Status debug_DumpStack(wchar_t* /*buf*/, size_t /*max_chars*/, void* /*context*/,
60 : const wchar_t* /*lastFuncToSkip*/)
61 : {
62 : return ERR::NOT_SUPPORTED;
63 : }
64 :
65 : Status debug_ResolveSymbol(void* /*ptr_of_interest*/, wchar_t* /*sym_name*/, wchar_t* /*file*/,
66 : int* /*line*/)
67 : {
68 : return ERR::NOT_SUPPORTED;
69 : }
70 :
71 : #else
72 :
73 : #include <execinfo.h>
74 :
75 0 : void* debug_GetCaller(void* /*context*/, const wchar_t* /*lastFuncToSkip*/)
76 : {
77 : // bt[0] == this function
78 : // bt[1] == our caller
79 : // bt[2] == the first caller they are interested in
80 : // HACK: we currently don't support lastFuncToSkip (would require debug information),
81 : // instead just returning the caller of the function calling us
82 0 : void *bt[3];
83 0 : int bt_size = backtrace(bt, 3);
84 0 : if (bt_size < 3)
85 : return NULL;
86 0 : return bt[2];
87 : }
88 :
89 0 : Status debug_DumpStack(wchar_t* buf, size_t max_chars, void* /*context*/, const wchar_t* /*lastFuncToSkip*/)
90 : {
91 0 : static const size_t N_FRAMES = 16;
92 0 : void *bt[N_FRAMES];
93 0 : int bt_size = 0;
94 0 : wchar_t *bufpos = buf;
95 0 : wchar_t *bufend = buf + max_chars;
96 :
97 0 : bt_size = backtrace(bt, ARRAY_SIZE(bt));
98 :
99 : // Assumed max length of a single print-out
100 : static const size_t MAX_OUT_CHARS = 1024;
101 :
102 0 : for (size_t i = 0; (int)i < bt_size && bufpos+MAX_OUT_CHARS < bufend; i++)
103 : {
104 0 : wchar_t file[DEBUG_FILE_CHARS];
105 0 : wchar_t symbol[DEBUG_SYMBOL_CHARS];
106 0 : int line;
107 0 : int len;
108 :
109 0 : if (debug_ResolveSymbol(bt[i], symbol, file, &line) == 0)
110 : {
111 0 : if (file[0])
112 0 : len = swprintf(bufpos, MAX_OUT_CHARS, L"(%p) %ls:%d %ls\n", bt[i], file, line, symbol);
113 : else
114 0 : len = swprintf(bufpos, MAX_OUT_CHARS, L"(%p) %ls\n", bt[i], symbol);
115 : }
116 : else
117 : {
118 0 : len = swprintf(bufpos, MAX_OUT_CHARS, L"(%p)\n", bt[i]);
119 : }
120 :
121 0 : if (len < 0)
122 : {
123 : // MAX_OUT_CHARS exceeded, realistically this was caused by some
124 : // mindbogglingly long symbol name... replace the end with an
125 : // ellipsis and a newline
126 0 : memcpy(&bufpos[MAX_OUT_CHARS-6], L"...\n", 5*sizeof(wchar_t));
127 0 : len = MAX_OUT_CHARS;
128 : }
129 :
130 0 : bufpos += len;
131 : }
132 :
133 0 : return INFO::OK;
134 : }
135 :
136 0 : Status debug_ResolveSymbol(void* ptr_of_interest, wchar_t* sym_name, wchar_t* file, int* line)
137 : {
138 0 : if (sym_name)
139 0 : *sym_name = 0;
140 0 : if (file)
141 0 : *file = 0;
142 0 : if (line)
143 0 : *line = 0;
144 :
145 0 : char** symbols = backtrace_symbols(&ptr_of_interest, 1);
146 0 : if (symbols)
147 : {
148 0 : swprintf_s(sym_name, DEBUG_SYMBOL_CHARS, L"%hs", symbols[0]);
149 0 : free(symbols);
150 :
151 : // (Note that this will usually return a pretty useless string,
152 : // because we compile with -fvisibility=hidden and there won't be
153 : // any exposed symbols for backtrace_symbols to report.)
154 :
155 0 : return INFO::OK;
156 : }
157 : else
158 : {
159 : return ERR::FAIL;
160 : }
161 : }
162 :
163 : #endif
164 :
165 : /* This is basically a reimplementation of the pthread_setname_np() glibc
166 : * function, to make it work on every Linux distribution, no matter the libc
167 : * used.
168 : *
169 : * The thread name limit is 16 (including the terminating null byte) on Linux,
170 : * so we have to cut the provided name at 15 bytes.
171 : *
172 : * This API exists since Linux 2.6.33, on older kernels the user will just not
173 : * get thread names.
174 : */
175 3 : void debug_SetThreadName(const char* name)
176 : {
177 3 : constexpr size_t MAX_THREAD_NAME_LEN = 15;
178 3 : constexpr size_t MAX_FILE_PATH_LEN = 32;
179 :
180 3 : char pathname[MAX_FILE_PATH_LEN];
181 3 : pid_t tid = syscall(SYS_gettid);
182 3 : snprintf(pathname, MAX_FILE_PATH_LEN, "/proc/self/task/%d/comm", tid);
183 :
184 3 : FILE* comm = fopen(pathname, "w");
185 3 : if (!comm)
186 0 : return;
187 :
188 3 : std::string limited_name(name, std::min(strlen(name), MAX_THREAD_NAME_LEN));
189 3 : fputs(limited_name.c_str(), comm);
190 3 : fclose(comm);
191 3 : }
|