21 #include <winpr/config.h>
22 #include <winpr/platform.h>
24 WINPR_PRAGMA_DIAG_PUSH
25 WINPR_PRAGMA_DIAG_IGNORED_RESERVED_ID_MACRO
26 WINPR_PRAGMA_DIAG_IGNORED_UNUSED_MACRO
28 #define __STDC_WANT_LIB_EXT1__ 1
36 #include <winpr/crt.h>
37 #include <winpr/string.h>
39 #if defined(USE_EXECINFO)
40 #include <execinfo/debug.h>
43 #if defined(USE_UNWIND)
44 #include <unwind/debug.h>
47 #if defined(WINPR_HAVE_CORKSCREW)
48 #include <corkscrew/debug.h>
51 #if defined(_WIN32) || defined(_WIN64)
53 #include <windows/debug.h>
56 #include <winpr/wlog.h>
57 #include <winpr/debug.h>
60 #define MIN(a, b) (a) < (b) ? (a) : (b)
63 #define TAG "com.winpr.utils.debug"
67 WLog_Print(WLog_Get(TAG), WLOG_TRACE, __VA_ARGS__); \
72 WLog_Print(WLog_Get(TAG), WLOG_DEBUG, __VA_ARGS__); \
77 WLog_Print(WLog_Get(TAG), WLOG_INFO, __VA_ARGS__); \
82 WLog_Print(WLog_Get(TAG), WLOG_WARN, __VA_ARGS__); \
87 WLog_Print(WLog_Get(TAG), WLOG_ERROR, __VA_ARGS__); \
92 WLog_Print(WLog_Get(TAG), WLOG_FATAL, __VA_ARGS__); \
95 static const char* support_msg =
"Invalid stacktrace buffer! check if platform is supported!";
97 void winpr_backtrace_free(
void* buffer)
102 #if defined(USE_UNWIND)
103 winpr_unwind_backtrace_free(buffer);
104 #elif defined(USE_EXECINFO)
105 winpr_execinfo_backtrace_free(buffer);
106 #elif defined(WINPR_HAVE_CORKSCREW)
107 winpr_corkscrew_backtrace_free(buffer);
108 #elif defined(_WIN32) || defined(_WIN64)
109 winpr_win_backtrace_free(buffer);
116 void* winpr_backtrace(DWORD size)
118 #if defined(USE_UNWIND)
119 return winpr_unwind_backtrace(size);
120 #elif defined(USE_EXECINFO)
121 return winpr_execinfo_backtrace(size);
122 #elif defined(WINPR_HAVE_CORKSCREW)
123 return winpr_corkscrew_backtrace(size);
124 #elif (defined(_WIN32) || defined(_WIN64)) && !defined(_UWP)
125 return winpr_win_backtrace(size);
130 return _strdup(support_msg);
134 char** winpr_backtrace_symbols(
void* buffer,
size_t* used)
145 #if defined(USE_UNWIND)
146 return winpr_unwind_backtrace_symbols(buffer, used);
147 #elif defined(USE_EXECINFO)
148 return winpr_execinfo_backtrace_symbols(buffer, used);
149 #elif defined(WINPR_HAVE_CORKSCREW)
150 return winpr_corkscrew_backtrace_symbols(buffer, used);
151 #elif (defined(_WIN32) || defined(_WIN64)) && !defined(_UWP)
152 return winpr_win_backtrace_symbols(buffer, used);
162 size_t len = strlen(support_msg);
163 char* ppmsg = calloc(
sizeof(
char*) + len + 1,
sizeof(
char));
166 char** msgptr = (
char**)ppmsg;
167 char* msg = &ppmsg[
sizeof(
char*)];
170 strncpy(msg, support_msg, len);
176 void winpr_backtrace_symbols_fd(
void* buffer,
int fd)
184 #if defined(USE_EXECINFO) && !defined(USE_UNWIND)
185 winpr_execinfo_backtrace_symbols_fd(buffer, fd);
186 #elif !defined(ANDROID)
189 char** lines = winpr_backtrace_symbols(buffer, &used);
194 for (
size_t i = 0; i < used; i++)
195 (
void)_write(fd, lines[i], (
unsigned)strnlen(lines[i], UINT32_MAX));
203 void winpr_log_backtrace(
const char* tag, DWORD level, DWORD size)
205 winpr_log_backtrace_ex(WLog_Get(tag), level, size);
208 void winpr_log_backtrace_ex(wLog* log, DWORD level, DWORD size)
212 void* stack = winpr_backtrace(20);
216 WLog_Print(log, WLOG_ERROR,
"winpr_backtrace failed!\n");
220 msg = winpr_backtrace_symbols(stack, &used);
224 for (
size_t x = 0; x < used; x++)
225 WLog_Print(log, level,
"%" PRIuz
": %s", x, msg[x]);
230 winpr_backtrace_free(stack);
233 char* winpr_strerror(INT32 dw,
char* dmsg,
size_t size)
235 #ifdef __STDC_LIB_EXT1__
236 (void)strerror_s(dw, dmsg, size);
237 #elif defined(WINPR_HAVE_STRERROR_R)
238 (void)strerror_r(dw, dmsg, size);
240 (void)_snprintf(dmsg, size,
"%s", strerror(dw));