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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
/**
* WinPR: Windows Portable Runtime
* Debugging Utils
*
* Copyright 2014 Armin Novak <armin.novak@thincast.com>
* Copyright 2014 Thincast Technologies GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <winpr/config.h>
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <winpr/crt.h>
#include <winpr/string.h>
#if defined(USE_EXECINFO)
#include <execinfo/debug.h>
#endif
#if defined(USE_UNWIND)
#include <unwind/debug.h>
#endif
#if defined(WINPR_HAVE_CORKSCREW)
#include <corkscrew/debug.h>
#endif
#if defined(_WIN32) || defined(_WIN64)
#include <io.h>
#include <windows/debug.h>
#endif
#include <winpr/wlog.h>
#include <winpr/debug.h>
#ifndef MIN
#define MIN(a, b) (a) < (b) ? (a) : (b)
#endif
#define TAG "com.winpr.utils.debug"
#define LOGT(...) \
do \
{ \
WLog_Print(WLog_Get(TAG), WLOG_TRACE, __VA_ARGS__); \
} while (0)
#define LOGD(...) \
do \
{ \
WLog_Print(WLog_Get(TAG), WLOG_DEBUG, __VA_ARGS__); \
} while (0)
#define LOGI(...) \
do \
{ \
WLog_Print(WLog_Get(TAG), WLOG_INFO, __VA_ARGS__); \
} while (0)
#define LOGW(...) \
do \
{ \
WLog_Print(WLog_Get(TAG), WLOG_WARN, __VA_ARGS__); \
} while (0)
#define LOGE(...) \
do \
{ \
WLog_Print(WLog_Get(TAG), WLOG_ERROR, __VA_ARGS__); \
} while (0)
#define LOGF(...) \
do \
{ \
WLog_Print(WLog_Get(TAG), WLOG_FATAL, __VA_ARGS__); \
} while (0)
static const char* support_msg = "Invalid stacktrace buffer! check if platform is supported!";
void winpr_backtrace_free(void* buffer)
{
if (!buffer)
return;
#if defined(USE_UNWIND)
winpr_unwind_backtrace_free(buffer);
#elif defined(USE_EXECINFO)
winpr_execinfo_backtrace_free(buffer);
#elif defined(WINPR_HAVE_CORKSCREW)
winpr_corkscrew_backtrace_free(buffer);
#elif defined(_WIN32) || defined(_WIN64)
winpr_win_backtrace_free(buffer);
#else
free(buffer);
LOGF(support_msg);
#endif
}
void* winpr_backtrace(DWORD size)
{
#if defined(USE_UNWIND)
return winpr_unwind_backtrace(size);
#elif defined(USE_EXECINFO)
return winpr_execinfo_backtrace(size);
#elif defined(WINPR_HAVE_CORKSCREW)
return winpr_corkscrew_backtrace(size);
#elif (defined(_WIN32) || defined(_WIN64)) && !defined(_UWP)
return winpr_win_backtrace(size);
#else
LOGF(support_msg);
/* return a non NULL buffer to allow the backtrace function familiy to succeed without failing
*/
return _strdup(support_msg);
#endif
}
char** winpr_backtrace_symbols(void* buffer, size_t* used)
{
if (used)
*used = 0;
if (!buffer)
{
LOGF(support_msg);
return NULL;
}
#if defined(USE_UNWIND)
return winpr_unwind_backtrace_symbols(buffer, used);
#elif defined(USE_EXECINFO)
return winpr_execinfo_backtrace_symbols(buffer, used);
#elif defined(WINPR_HAVE_CORKSCREW)
return winpr_corkscrew_backtrace_symbols(buffer, used);
#elif (defined(_WIN32) || defined(_WIN64)) && !defined(_UWP)
return winpr_win_backtrace_symbols(buffer, used);
#else
LOGF(support_msg);
/* We return a char** on heap that is compatible with free:
*
* 1. We allocate sizeof(char*) + strlen + 1 bytes.
* 2. The first sizeof(char*) bytes contain the pointer to the string following the pointer.
* 3. The at data + sizeof(char*) contains the actual string
*/
size_t len = strlen(support_msg);
char* ppmsg = calloc(sizeof(char*) + len + 1, sizeof(char));
if (!ppmsg)
return NULL;
char** msgptr = (char**)ppmsg;
char* msg = &ppmsg[sizeof(char*)];
*msgptr = msg;
strncpy(msg, support_msg, len);
*used = 1;
return ppmsg;
#endif
}
void winpr_backtrace_symbols_fd(void* buffer, int fd)
{
if (!buffer)
{
LOGF(support_msg);
return;
}
#if defined(USE_EXECINFO) && !defined(USE_UNWIND)
winpr_execinfo_backtrace_symbols_fd(buffer, fd);
#elif !defined(ANDROID)
{
size_t used = 0;
char** lines = winpr_backtrace_symbols(buffer, &used);
if (!lines)
return;
for (size_t i = 0; i < used; i++)
_write(fd, lines[i], (unsigned)strnlen(lines[i], UINT32_MAX));
free(lines);
}
#else
LOGF(support_msg);
#endif
}
void winpr_log_backtrace(const char* tag, DWORD level, DWORD size)
{
winpr_log_backtrace_ex(WLog_Get(tag), level, size);
}
void winpr_log_backtrace_ex(wLog* log, DWORD level, DWORD size)
{
size_t used = 0;
char** msg = NULL;
void* stack = winpr_backtrace(20);
if (!stack)
{
WLog_Print(log, WLOG_ERROR, "winpr_backtrace failed!\n");
goto fail;
}
msg = winpr_backtrace_symbols(stack, &used);
if (msg)
{
for (size_t x = 0; x < used; x++)
WLog_Print(log, level, "%" PRIuz ": %s", x, msg[x]);
}
free(msg);
fail:
winpr_backtrace_free(stack);
}
char* winpr_strerror(DWORD dw, char* dmsg, size_t size)
{
#ifdef __STDC_LIB_EXT1__
strerror_s(dw, dmsg, size);
#elif defined(WINPR_HAVE_STRERROR_R)
strerror_r(dw, dmsg, size);
#else
_snprintf(dmsg, size, "%s", strerror(dw));
#endif
return dmsg;
}
|