2014-08-14 12:36:50 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
2014-08-14 12:42:10 +04:00
|
|
|
* Debugging Utils
|
2014-08-14 12:36:50 +04:00
|
|
|
*
|
2014-08-14 12:42:10 +04:00
|
|
|
* Copyright 2014 Armin Novak <armin.novak@thincast.com>
|
|
|
|
* Copyright 2014 Thincast Technologies GmbH
|
2014-08-14 12:36:50 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 12:08:00 +03:00
|
|
|
#include <winpr/config.h>
|
2014-08-14 12:36:50 +04:00
|
|
|
|
2014-09-17 19:59:06 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
2022-04-29 14:34:39 +03:00
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
2015-07-03 14:26:15 +03:00
|
|
|
#include <winpr/string.h>
|
2014-09-17 19:59:06 +04:00
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#if defined(WINPR_HAVE_EXECINFO_H)
|
2022-04-29 12:38:46 +03:00
|
|
|
#include <execinfo/debug.h>
|
2014-08-14 12:36:50 +04:00
|
|
|
#endif
|
|
|
|
|
2022-05-04 16:11:04 +03:00
|
|
|
#if defined(USE_UNWIND)
|
2022-04-29 12:28:49 +03:00
|
|
|
#include <unwind/debug.h>
|
|
|
|
#endif
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#if defined(WINPR_HAVE_CORKSCREW)
|
2022-04-29 12:56:35 +03:00
|
|
|
#include <corkscrew/debug.h>
|
2014-08-14 12:36:50 +04:00
|
|
|
#endif
|
|
|
|
|
2014-09-17 19:24:12 +04:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
2022-07-14 10:26:54 +03:00
|
|
|
#include <io.h>
|
2022-04-29 12:51:18 +03:00
|
|
|
#include <windows/debug.h>
|
2014-09-17 19:24:12 +04:00
|
|
|
#endif
|
|
|
|
|
2014-08-14 12:36:50 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/wlog.h>
|
|
|
|
#include <winpr/debug.h>
|
|
|
|
|
2021-11-25 10:35:47 +03:00
|
|
|
#ifndef MIN
|
2021-06-16 15:43:07 +03:00
|
|
|
#define MIN(a, b) (a) < (b) ? (a) : (b)
|
2021-11-25 10:35:47 +03:00
|
|
|
#endif
|
|
|
|
|
2014-08-14 12:36:50 +04:00
|
|
|
#define TAG "com.winpr.utils.debug"
|
2019-11-06 17:24:51 +03:00
|
|
|
#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)
|
2014-08-14 12:36:50 +04:00
|
|
|
|
2018-12-17 11:09:02 +03:00
|
|
|
static const char* support_msg = "Invalid stacktrace buffer! check if platform is supported!";
|
2014-08-14 12:36:50 +04:00
|
|
|
|
2015-05-21 20:25:35 +03:00
|
|
|
void winpr_backtrace_free(void* buffer)
|
2014-08-14 12:36:50 +04:00
|
|
|
{
|
|
|
|
if (!buffer)
|
|
|
|
return;
|
|
|
|
|
2022-05-04 16:11:04 +03:00
|
|
|
#if defined(USE_UNWIND)
|
2022-04-29 12:28:49 +03:00
|
|
|
winpr_unwind_backtrace_free(buffer);
|
2023-01-10 16:30:23 +03:00
|
|
|
#elif defined(WINPR_HAVE_EXECINFO_H)
|
2022-04-29 12:38:46 +03:00
|
|
|
winpr_execinfo_backtrace_free(buffer);
|
2023-01-10 16:30:23 +03:00
|
|
|
#elif defined(WINPR_HAVE_CORKSCREW)
|
2022-04-29 12:56:35 +03:00
|
|
|
winpr_corkscrew_backtrace_free(buffer);
|
2014-09-17 19:24:12 +04:00
|
|
|
#elif defined(_WIN32) || defined(_WIN64)
|
2022-04-29 12:51:18 +03:00
|
|
|
winpr_win_backtrace_free(buffer);
|
2014-08-14 12:36:50 +04:00
|
|
|
#else
|
|
|
|
LOGF(support_msg);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-05-21 20:25:35 +03:00
|
|
|
void* winpr_backtrace(DWORD size)
|
2014-08-14 12:36:50 +04:00
|
|
|
{
|
2022-05-04 16:11:04 +03:00
|
|
|
#if defined(USE_UNWIND)
|
2022-04-29 12:28:49 +03:00
|
|
|
return winpr_unwind_backtrace(size);
|
2023-01-10 16:30:23 +03:00
|
|
|
#elif defined(WINPR_HAVE_EXECINFO_H)
|
2022-04-29 12:38:46 +03:00
|
|
|
return winpr_execinfo_backtrace(size);
|
2023-01-10 16:30:23 +03:00
|
|
|
#elif defined(WINPR_HAVE_CORKSCREW)
|
2022-04-29 12:56:35 +03:00
|
|
|
return winpr_corkscrew_backtrace(size);
|
2016-02-06 00:28:45 +03:00
|
|
|
#elif (defined(_WIN32) || defined(_WIN64)) && !defined(_UWP)
|
2022-04-29 12:51:18 +03:00
|
|
|
return winpr_win_backtrace(size);
|
2014-08-14 12:36:50 +04:00
|
|
|
#else
|
|
|
|
LOGF(support_msg);
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-05-21 20:25:35 +03:00
|
|
|
char** winpr_backtrace_symbols(void* buffer, size_t* used)
|
2014-08-14 12:36:50 +04:00
|
|
|
{
|
|
|
|
if (used)
|
|
|
|
*used = 0;
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
LOGF(support_msg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-05-04 16:11:04 +03:00
|
|
|
#if defined(USE_UNWIND)
|
2022-04-29 12:28:49 +03:00
|
|
|
return winpr_unwind_backtrace_symbols(buffer, used);
|
2023-01-10 16:30:23 +03:00
|
|
|
#elif defined(WINPR_HAVE_EXECINFO_H)
|
2022-04-29 12:38:46 +03:00
|
|
|
return winpr_execinfo_backtrace_symbols(buffer, used);
|
2023-01-10 16:30:23 +03:00
|
|
|
#elif defined(WINPR_HAVE_CORKSCREW)
|
2022-04-29 12:56:35 +03:00
|
|
|
return winpr_corkscrew_backtrace_symbols(buffer, used);
|
2016-02-06 00:28:45 +03:00
|
|
|
#elif (defined(_WIN32) || defined(_WIN64)) && !defined(_UWP)
|
2022-04-29 12:51:18 +03:00
|
|
|
return winpr_win_backtrace_symbols(buffer, used);
|
2014-08-14 12:36:50 +04:00
|
|
|
#else
|
|
|
|
LOGF(support_msg);
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-05-21 20:25:35 +03:00
|
|
|
void winpr_backtrace_symbols_fd(void* buffer, int fd)
|
2014-08-14 12:36:50 +04:00
|
|
|
{
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
LOGF(support_msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#if defined(WINPR_HAVE_EXECINFO_H) && !defined(USE_UNWIND)
|
2022-04-29 12:38:46 +03:00
|
|
|
winpr_execinfo_backtrace_symbols_fd(buffer, fd);
|
|
|
|
#elif !defined(ANDROID)
|
2015-01-23 00:06:18 +03:00
|
|
|
{
|
2022-04-29 12:38:46 +03:00
|
|
|
size_t i;
|
|
|
|
size_t used = 0;
|
|
|
|
char** lines = winpr_backtrace_symbols(buffer, &used);
|
2015-01-23 00:06:18 +03:00
|
|
|
|
2022-04-29 12:38:46 +03:00
|
|
|
if (!lines)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < used; i++)
|
2022-04-29 14:34:39 +03:00
|
|
|
_write(fd, lines[i], (unsigned)strnlen(lines[i], UINT32_MAX));
|
2015-05-21 20:25:35 +03:00
|
|
|
}
|
2014-08-14 12:36:50 +04:00
|
|
|
#else
|
|
|
|
LOGF(support_msg);
|
|
|
|
#endif
|
|
|
|
}
|
2015-07-03 10:22:02 +03:00
|
|
|
|
|
|
|
void winpr_log_backtrace(const char* tag, DWORD level, DWORD size)
|
2018-12-17 11:09:02 +03:00
|
|
|
{
|
|
|
|
winpr_log_backtrace_ex(WLog_Get(tag), level, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void winpr_log_backtrace_ex(wLog* log, DWORD level, DWORD size)
|
2015-07-03 10:22:02 +03:00
|
|
|
{
|
|
|
|
size_t used, x;
|
2018-12-17 11:09:02 +03:00
|
|
|
char** msg;
|
|
|
|
void* stack = winpr_backtrace(20);
|
2015-07-03 10:22:02 +03:00
|
|
|
|
|
|
|
if (!stack)
|
|
|
|
{
|
2018-12-17 11:09:02 +03:00
|
|
|
WLog_Print(log, WLOG_ERROR, "winpr_backtrace failed!\n");
|
2021-09-17 09:52:47 +03:00
|
|
|
goto fail;
|
2015-07-03 10:22:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
msg = winpr_backtrace_symbols(stack, &used);
|
2018-12-17 11:09:02 +03:00
|
|
|
|
2015-07-03 10:22:02 +03:00
|
|
|
if (msg)
|
|
|
|
{
|
2018-12-17 11:09:02 +03:00
|
|
|
for (x = 0; x < used; x++)
|
2022-12-09 11:54:13 +03:00
|
|
|
WLog_Print(log, level, "%" PRIuz ": %s", x, msg[x]);
|
2015-07-03 10:22:02 +03:00
|
|
|
}
|
2021-02-25 15:52:47 +03:00
|
|
|
free(msg);
|
2018-12-17 11:09:02 +03:00
|
|
|
|
2021-09-17 09:52:47 +03:00
|
|
|
fail:
|
2015-07-03 10:22:02 +03:00
|
|
|
winpr_backtrace_free(stack);
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:28:45 +03:00
|
|
|
char* winpr_strerror(DWORD dw, char* dmsg, size_t size)
|
|
|
|
{
|
2015-12-16 17:15:46 +03:00
|
|
|
#if defined(_WIN32)
|
2022-04-29 12:51:18 +03:00
|
|
|
return winpr_win_strerror(dw, dmsg, size);
|
2019-11-06 17:24:51 +03:00
|
|
|
#else /* defined(_WIN32) */
|
2015-12-09 20:27:37 +03:00
|
|
|
_snprintf(dmsg, size, "%s", strerror(dw));
|
2015-12-16 17:15:46 +03:00
|
|
|
#endif /* defined(_WIN32) */
|
2015-12-09 20:27:37 +03:00
|
|
|
return dmsg;
|
|
|
|
}
|