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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2014-09-17 19:59:06 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
2015-07-03 14:26:15 +03:00
|
|
|
#include <winpr/string.h>
|
2014-09-17 19:59:06 +04:00
|
|
|
|
2014-08-14 12:36:50 +04:00
|
|
|
#if defined(HAVE_EXECINFO_H)
|
|
|
|
#include <execinfo.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(ANDROID)
|
|
|
|
#include <corkscrew/backtrace.h>
|
|
|
|
#endif
|
|
|
|
|
2014-09-17 19:24:12 +04:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
2015-01-23 00:06:18 +03:00
|
|
|
#include <io.h>
|
2014-09-17 19:59:06 +04:00
|
|
|
#include <Windows.h>
|
2014-09-17 19:24:12 +04:00
|
|
|
#include <Dbghelp.h>
|
2015-01-23 00:06:18 +03:00
|
|
|
#define write _write
|
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>
|
|
|
|
|
|
|
|
#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)
|
|
|
|
|
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
|
|
|
|
|
|
|
#if defined(HAVE_EXECINFO_H)
|
|
|
|
typedef struct
|
|
|
|
{
|
2018-12-17 11:09:02 +03:00
|
|
|
void** buffer;
|
2014-08-14 12:36:50 +04:00
|
|
|
size_t max;
|
|
|
|
size_t used;
|
|
|
|
} t_execinfo;
|
|
|
|
#endif
|
|
|
|
|
2014-09-17 19:24:12 +04:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
typedef struct
|
|
|
|
{
|
2018-12-17 11:09:02 +03:00
|
|
|
PVOID* stack;
|
|
|
|
ULONG used;
|
|
|
|
ULONG max;
|
2014-09-17 19:24:12 +04:00
|
|
|
} t_win_stack;
|
|
|
|
#endif
|
|
|
|
|
2014-08-14 12:36:50 +04:00
|
|
|
#if defined(ANDROID)
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2018-12-17 11:09:02 +03:00
|
|
|
backtrace_frame_t* buffer;
|
2014-08-14 12:36:50 +04:00
|
|
|
size_t max;
|
|
|
|
size_t used;
|
|
|
|
} t_corkscrew_data;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2018-12-17 11:09:02 +03:00
|
|
|
void* hdl;
|
|
|
|
ssize_t (*unwind_backtrace)(backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth);
|
|
|
|
ssize_t (*unwind_backtrace_thread)(pid_t tid, backtrace_frame_t* backtrace,
|
|
|
|
size_t ignore_depth, size_t max_depth);
|
|
|
|
ssize_t (*unwind_backtrace_ptrace)(pid_t tid, const ptrace_context_t* context,
|
|
|
|
backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth);
|
|
|
|
void (*get_backtrace_symbols)(const backtrace_frame_t* backtrace, size_t frames,
|
|
|
|
backtrace_symbol_t* backtrace_symbols);
|
|
|
|
void (*get_backtrace_symbols_ptrace)(const ptrace_context_t* context,
|
|
|
|
const backtrace_frame_t* backtrace, size_t frames,
|
|
|
|
backtrace_symbol_t* backtrace_symbols);
|
|
|
|
void (*free_backtrace_symbols)(backtrace_symbol_t* backtrace_symbols, size_t frames);
|
|
|
|
void (*format_backtrace_line)(unsigned frameNumber, const backtrace_frame_t* frame,
|
|
|
|
const backtrace_symbol_t* symbol, char* buffer, size_t bufferSize);
|
2014-08-14 12:36:50 +04:00
|
|
|
} t_corkscrew;
|
|
|
|
|
|
|
|
static pthread_once_t initialized = PTHREAD_ONCE_INIT;
|
2018-12-17 11:09:02 +03:00
|
|
|
static t_corkscrew* fkt = NULL;
|
2014-08-14 12:36:50 +04:00
|
|
|
|
|
|
|
void load_library(void)
|
|
|
|
{
|
|
|
|
static t_corkscrew lib;
|
|
|
|
{
|
|
|
|
lib.hdl = dlopen("libcorkscrew.so", RTLD_LAZY);
|
|
|
|
|
|
|
|
if (!lib.hdl)
|
|
|
|
{
|
|
|
|
LOGF("dlopen error %s", dlerror());
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
lib.unwind_backtrace = dlsym(lib.hdl, "unwind_backtrace");
|
|
|
|
|
|
|
|
if (!lib.unwind_backtrace)
|
|
|
|
{
|
|
|
|
LOGF("dlsym error %s", dlerror());
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
lib.unwind_backtrace_thread = dlsym(lib.hdl, "unwind_backtrace_thread");
|
|
|
|
|
|
|
|
if (!lib.unwind_backtrace_thread)
|
|
|
|
{
|
|
|
|
LOGF("dlsym error %s", dlerror());
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
lib.unwind_backtrace_ptrace = dlsym(lib.hdl, "unwind_backtrace_ptrace");
|
|
|
|
|
|
|
|
if (!lib.unwind_backtrace_ptrace)
|
|
|
|
{
|
|
|
|
LOGF("dlsym error %s", dlerror());
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
lib.get_backtrace_symbols = dlsym(lib.hdl, "get_backtrace_symbols");
|
|
|
|
|
|
|
|
if (!lib.get_backtrace_symbols)
|
|
|
|
{
|
|
|
|
LOGF("dlsym error %s", dlerror());
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
lib.get_backtrace_symbols_ptrace = dlsym(lib.hdl, "get_backtrace_symbols_ptrace");
|
|
|
|
|
|
|
|
if (!lib.get_backtrace_symbols_ptrace)
|
|
|
|
{
|
|
|
|
LOGF("dlsym error %s", dlerror());
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
lib.free_backtrace_symbols = dlsym(lib.hdl, "free_backtrace_symbols");
|
|
|
|
|
|
|
|
if (!lib.free_backtrace_symbols)
|
|
|
|
{
|
|
|
|
LOGF("dlsym error %s", dlerror());
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
lib.format_backtrace_line = dlsym(lib.hdl, "format_backtrace_line");
|
|
|
|
|
|
|
|
if (!lib.format_backtrace_line)
|
|
|
|
{
|
|
|
|
LOGF("dlsym error %s", dlerror());
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fkt = &lib;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fail:
|
|
|
|
{
|
|
|
|
if (lib.hdl)
|
|
|
|
dlclose(lib.hdl);
|
|
|
|
|
|
|
|
fkt = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-11-25 21:46:10 +03:00
|
|
|
#if defined(_WIN32) && (NTDDI_VERSION <= NTDDI_WINXP)
|
|
|
|
|
2018-12-17 11:09:02 +03:00
|
|
|
typedef USHORT(WINAPI* PRTL_CAPTURE_STACK_BACK_TRACE_FN)(ULONG FramesToSkip, ULONG FramesToCapture,
|
|
|
|
PVOID* BackTrace, PULONG BackTraceHash);
|
2015-11-25 21:46:10 +03:00
|
|
|
|
|
|
|
static HMODULE g_NTDLL_Library = NULL;
|
|
|
|
static BOOL g_RtlCaptureStackBackTrace_Detected = FALSE;
|
|
|
|
static BOOL g_RtlCaptureStackBackTrace_Available = FALSE;
|
|
|
|
static PRTL_CAPTURE_STACK_BACK_TRACE_FN g_pRtlCaptureStackBackTrace = NULL;
|
|
|
|
|
2018-12-17 11:09:02 +03:00
|
|
|
USHORT RtlCaptureStackBackTrace(ULONG FramesToSkip, ULONG FramesToCapture, PVOID* BackTrace,
|
|
|
|
PULONG BackTraceHash)
|
2015-11-25 21:46:10 +03:00
|
|
|
{
|
|
|
|
if (!g_RtlCaptureStackBackTrace_Detected)
|
|
|
|
{
|
|
|
|
g_NTDLL_Library = LoadLibraryA("kernel32.dll");
|
|
|
|
|
|
|
|
if (g_NTDLL_Library)
|
|
|
|
{
|
2018-12-17 11:09:02 +03:00
|
|
|
g_pRtlCaptureStackBackTrace = (PRTL_CAPTURE_STACK_BACK_TRACE_FN) GetProcAddress(g_NTDLL_Library,
|
|
|
|
"RtlCaptureStackBackTrace");
|
2015-11-25 21:46:10 +03:00
|
|
|
g_RtlCaptureStackBackTrace_Available = (g_pRtlCaptureStackBackTrace) ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_RtlCaptureStackBackTrace_Available = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_RtlCaptureStackBackTrace_Detected = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_RtlCaptureStackBackTrace_Available)
|
|
|
|
{
|
|
|
|
return (*g_pRtlCaptureStackBackTrace)(FramesToSkip, FramesToCapture, BackTrace, BackTraceHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-05-21 20:25:35 +03:00
|
|
|
void winpr_backtrace_free(void* buffer)
|
2014-08-14 12:36:50 +04:00
|
|
|
{
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
LOGF(support_msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(HAVE_EXECINFO_H)
|
2015-05-21 20:25:35 +03:00
|
|
|
t_execinfo* data = (t_execinfo*) buffer;
|
2015-05-11 10:07:39 +03:00
|
|
|
free(data->buffer);
|
2014-08-14 12:36:50 +04:00
|
|
|
free(data);
|
|
|
|
#elif defined(ANDROID)
|
2018-12-17 11:09:02 +03:00
|
|
|
t_corkscrew_data* data = (t_corkscrew_data*)buffer;
|
2015-05-11 10:07:39 +03:00
|
|
|
free(data->buffer);
|
2014-08-14 12:36:50 +04:00
|
|
|
free(data);
|
2014-09-17 19:24:12 +04:00
|
|
|
#elif defined(_WIN32) || defined(_WIN64)
|
2014-09-17 20:08:57 +04:00
|
|
|
{
|
2018-12-17 11:09:02 +03:00
|
|
|
t_win_stack* data = (t_win_stack*)buffer;
|
2015-05-11 10:07:39 +03:00
|
|
|
free(data->stack);
|
2014-09-17 20:08:57 +04:00
|
|
|
free(data);
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
#if defined(HAVE_EXECINFO_H)
|
2015-05-21 20:25:35 +03:00
|
|
|
t_execinfo* data = calloc(1, sizeof(t_execinfo));
|
2014-08-14 12:36:50 +04:00
|
|
|
|
|
|
|
if (!data)
|
|
|
|
return NULL;
|
|
|
|
|
2015-05-21 20:25:35 +03:00
|
|
|
data->buffer = calloc(size, sizeof(void*));
|
2014-08-14 12:36:50 +04:00
|
|
|
|
|
|
|
if (!data->buffer)
|
2014-11-16 17:44:02 +03:00
|
|
|
{
|
|
|
|
free(data);
|
2014-08-14 12:36:50 +04:00
|
|
|
return NULL;
|
2014-11-16 17:44:02 +03:00
|
|
|
}
|
2014-08-14 12:36:50 +04:00
|
|
|
|
|
|
|
data->max = size;
|
|
|
|
data->used = backtrace(data->buffer, size);
|
|
|
|
return data;
|
|
|
|
#elif defined(ANDROID)
|
2015-05-21 20:25:35 +03:00
|
|
|
t_corkscrew_data* data = calloc(1, sizeof(t_corkscrew_data));
|
2014-08-14 12:36:50 +04:00
|
|
|
|
|
|
|
if (!data)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
data->buffer = calloc(size, sizeof(backtrace_frame_t));
|
|
|
|
|
|
|
|
if (!data->buffer)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_once(&initialized, load_library);
|
|
|
|
data->max = size;
|
|
|
|
data->used = fkt->unwind_backtrace(data->buffer, 0, size);
|
|
|
|
return data;
|
2016-02-06 00:28:45 +03:00
|
|
|
#elif (defined(_WIN32) || defined(_WIN64)) && !defined(_UWP)
|
2014-09-17 20:08:57 +04:00
|
|
|
HANDLE process = GetCurrentProcess();
|
2015-05-21 20:25:35 +03:00
|
|
|
t_win_stack* data = calloc(1, sizeof(t_win_stack));
|
2014-09-17 19:24:12 +04:00
|
|
|
|
2014-09-17 20:08:57 +04:00
|
|
|
if (!data)
|
|
|
|
return NULL;
|
2014-09-17 19:24:12 +04:00
|
|
|
|
2014-09-17 20:08:57 +04:00
|
|
|
data->max = size;
|
|
|
|
data->stack = calloc(data->max, sizeof(PVOID));
|
2015-05-21 20:25:35 +03:00
|
|
|
|
2014-09-17 20:08:57 +04:00
|
|
|
if (!data->stack)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-09-17 19:24:12 +04:00
|
|
|
|
2015-05-21 20:25:35 +03:00
|
|
|
SymInitialize(process, NULL, TRUE);
|
2015-11-25 21:46:10 +03:00
|
|
|
data->used = RtlCaptureStackBackTrace(2, size, data->stack, NULL);
|
2014-08-14 12:36:50 +04:00
|
|
|
return data;
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(HAVE_EXECINFO_H)
|
2015-05-21 20:25:35 +03:00
|
|
|
t_execinfo* data = (t_execinfo*) buffer;
|
|
|
|
|
|
|
|
if (!data)
|
|
|
|
return NULL;
|
2014-08-14 12:36:50 +04:00
|
|
|
|
|
|
|
if (used)
|
|
|
|
*used = data->used;
|
|
|
|
|
|
|
|
return backtrace_symbols(data->buffer, data->used);
|
|
|
|
#elif defined(ANDROID)
|
2015-05-21 20:25:35 +03:00
|
|
|
t_corkscrew_data* data = (t_corkscrew_data*) buffer;
|
|
|
|
|
|
|
|
if (!data)
|
|
|
|
return NULL;
|
|
|
|
|
2014-08-14 12:36:50 +04:00
|
|
|
pthread_once(&initialized, load_library);
|
|
|
|
|
|
|
|
if (!fkt)
|
|
|
|
{
|
|
|
|
LOGF(support_msg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t line_len = (data->max > 1024) ? data->max : 1024;
|
|
|
|
size_t i;
|
2016-05-27 16:53:49 +03:00
|
|
|
size_t array_size = data->used * sizeof(char*);
|
|
|
|
size_t lines_size = data->used * line_len;
|
2018-12-17 11:09:02 +03:00
|
|
|
char** vlines = calloc(1, array_size + lines_size);
|
2015-05-21 20:25:35 +03:00
|
|
|
backtrace_symbol_t* symbols = calloc(data->used, sizeof(backtrace_symbol_t));
|
2014-08-14 12:36:50 +04:00
|
|
|
|
2016-05-27 16:53:49 +03:00
|
|
|
if (!vlines || !symbols)
|
2014-08-14 12:36:50 +04:00
|
|
|
{
|
2016-05-27 16:53:49 +03:00
|
|
|
free(vlines);
|
|
|
|
free(symbols);
|
2014-08-14 12:36:50 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-27 16:53:49 +03:00
|
|
|
/* Set the pointers in the allocated buffer's initial array section */
|
2015-05-21 20:25:35 +03:00
|
|
|
for (i = 0; i < data->used; i++)
|
2016-05-27 16:53:49 +03:00
|
|
|
vlines[i] = (char*)vlines + array_size + i * line_len;
|
2014-08-14 12:36:50 +04:00
|
|
|
|
|
|
|
fkt->get_backtrace_symbols(data->buffer, data->used, symbols);
|
|
|
|
|
2018-12-17 11:09:02 +03:00
|
|
|
for (i = 0; i < data->used; i++)
|
2014-08-14 12:36:50 +04:00
|
|
|
fkt->format_backtrace_line(i, &data->buffer[i], &symbols[i], vlines[i], line_len);
|
|
|
|
|
|
|
|
fkt->free_backtrace_symbols(symbols, data->used);
|
2014-09-17 19:24:12 +04:00
|
|
|
free(symbols);
|
2014-08-14 12:36:50 +04:00
|
|
|
|
|
|
|
if (used)
|
|
|
|
*used = data->used;
|
|
|
|
|
2016-05-27 16:53:49 +03:00
|
|
|
return vlines;
|
2014-08-14 12:36:50 +04:00
|
|
|
}
|
2018-12-17 11:09:02 +03:00
|
|
|
|
2016-02-06 00:28:45 +03:00
|
|
|
#elif (defined(_WIN32) || defined(_WIN64)) && !defined(_UWP)
|
2014-09-17 20:08:57 +04:00
|
|
|
{
|
|
|
|
size_t i;
|
2015-05-21 20:25:35 +03:00
|
|
|
size_t line_len = 1024;
|
|
|
|
HANDLE process = GetCurrentProcess();
|
|
|
|
t_win_stack* data = (t_win_stack*) buffer;
|
2016-05-27 16:53:49 +03:00
|
|
|
size_t array_size = data->used * sizeof(char*);
|
|
|
|
size_t lines_size = data->used * line_len;
|
2018-12-17 11:09:02 +03:00
|
|
|
char** vlines = calloc(1, array_size + lines_size);
|
2017-05-30 11:46:43 +03:00
|
|
|
SYMBOL_INFO* symbol = calloc(1, sizeof(SYMBOL_INFO) + line_len * sizeof(char));
|
2015-05-21 20:25:35 +03:00
|
|
|
IMAGEHLP_LINE64* line = (IMAGEHLP_LINE64*) calloc(1, sizeof(IMAGEHLP_LINE64));
|
2014-09-17 20:08:57 +04:00
|
|
|
|
2016-05-27 16:53:49 +03:00
|
|
|
if (!vlines || !symbol || !line)
|
2014-09-17 20:08:57 +04:00
|
|
|
{
|
2018-12-17 11:09:02 +03:00
|
|
|
free(vlines);
|
|
|
|
free(symbol);
|
|
|
|
free(line);
|
|
|
|
return NULL;
|
2014-09-17 20:08:57 +04:00
|
|
|
}
|
2015-05-21 20:25:35 +03:00
|
|
|
|
2014-09-17 20:08:57 +04:00
|
|
|
line->SizeOfStruct = sizeof(IMAGEHLP_LINE64);
|
|
|
|
symbol->MaxNameLen = line_len;
|
|
|
|
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
|
|
|
|
|
2016-05-27 16:53:49 +03:00
|
|
|
/* Set the pointers in the allocated buffer's initial array section */
|
2015-05-21 20:25:35 +03:00
|
|
|
for (i = 0; i < data->used; i++)
|
2016-05-27 16:53:49 +03:00
|
|
|
vlines[i] = (char*)vlines + array_size + i * line_len;
|
2014-09-17 20:08:57 +04:00
|
|
|
|
2015-05-21 20:25:35 +03:00
|
|
|
for (i = 0; i < data->used; i++)
|
2014-09-17 20:08:57 +04:00
|
|
|
{
|
2015-05-21 20:25:35 +03:00
|
|
|
DWORD64 address = (DWORD64)(data->stack[i]);
|
|
|
|
DWORD displacement;
|
|
|
|
SymFromAddr(process, address, 0, symbol);
|
2014-09-17 19:24:12 +04:00
|
|
|
|
2015-05-21 20:25:35 +03:00
|
|
|
if (SymGetLineFromAddr64(process, address, &displacement, line))
|
|
|
|
{
|
2018-12-17 11:09:02 +03:00
|
|
|
sprintf_s(vlines[i], line_len, "%016"PRIx64": %s in %s:%"PRIu32, symbol->Address, symbol->Name,
|
|
|
|
line->FileName, line->LineNumber);
|
2015-05-21 20:25:35 +03:00
|
|
|
}
|
|
|
|
else
|
2018-03-06 18:32:36 +03:00
|
|
|
sprintf_s(vlines[i], line_len, "%016"PRIx64": %s", symbol->Address, symbol->Name);
|
2018-12-17 11:09:02 +03:00
|
|
|
}
|
2014-09-17 19:24:12 +04:00
|
|
|
|
2018-12-17 11:09:02 +03:00
|
|
|
if (used)
|
|
|
|
*used = data->used;
|
2014-09-17 19:24:12 +04:00
|
|
|
|
2018-12-17 11:09:02 +03:00
|
|
|
free(symbol);
|
|
|
|
free(line);
|
|
|
|
return vlines;
|
2014-09-17 20:08:57 +04:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(HAVE_EXECINFO_H)
|
2015-05-21 20:25:35 +03:00
|
|
|
t_execinfo* data = (t_execinfo*) buffer;
|
|
|
|
|
|
|
|
if (!data)
|
|
|
|
return;
|
|
|
|
|
2014-08-14 12:36:50 +04:00
|
|
|
backtrace_symbols_fd(data->buffer, data->used, fd);
|
2014-09-17 19:24:12 +04:00
|
|
|
#elif defined(_WIN32) || defined(_WIN64) || defined(ANDROID)
|
2015-01-23 00:06:18 +03:00
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
size_t used;
|
|
|
|
char** lines;
|
|
|
|
lines = winpr_backtrace_symbols(buffer, &used);
|
|
|
|
|
|
|
|
if (lines)
|
|
|
|
{
|
|
|
|
for (i = 0; i < used; i++)
|
|
|
|
write(fd, lines[i], strlen(lines[i]));
|
|
|
|
}
|
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");
|
2015-07-03 10:22:02 +03:00
|
|
|
winpr_backtrace_free(stack);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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++)
|
|
|
|
WLog_Print(log, level, "%"PRIuz": %s\n", x, msg[x]);
|
2015-07-03 10:22:02 +03:00
|
|
|
}
|
2018-12-17 11:09:02 +03:00
|
|
|
|
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)
|
2015-12-09 20:27:37 +03:00
|
|
|
DWORD rc;
|
2016-02-06 00:28:45 +03:00
|
|
|
DWORD nSize = 0;
|
|
|
|
DWORD dwFlags = 0;
|
|
|
|
LPTSTR msg = NULL;
|
|
|
|
BOOL alloc = FALSE;
|
|
|
|
dwFlags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
|
|
|
|
#ifdef FORMAT_MESSAGE_ALLOCATE_BUFFER
|
|
|
|
alloc = TRUE;
|
|
|
|
dwFlags |= FORMAT_MESSAGE_ALLOCATE_BUFFER;
|
|
|
|
#else
|
2018-12-17 11:09:02 +03:00
|
|
|
nSize = (DWORD)(size * 2);
|
2016-02-06 00:28:45 +03:00
|
|
|
msg = (LPTSTR) calloc(nSize, sizeof(TCHAR));
|
|
|
|
#endif
|
|
|
|
rc = FormatMessage(dwFlags, NULL, dw, 0, alloc ? (LPTSTR) &msg : msg, nSize, NULL);
|
2015-12-09 20:27:37 +03:00
|
|
|
|
2018-12-17 11:09:02 +03:00
|
|
|
if (rc)
|
|
|
|
{
|
2015-12-09 20:27:37 +03:00
|
|
|
#if defined(UNICODE)
|
|
|
|
WideCharToMultiByte(CP_ACP, 0, msg, rc, dmsg, size - 1, NULL, NULL);
|
2015-12-16 17:15:46 +03:00
|
|
|
#else /* defined(UNICODE) */
|
2015-12-09 20:27:37 +03:00
|
|
|
memcpy(dmsg, msg, min(rc, size - 1));
|
2015-12-16 17:15:46 +03:00
|
|
|
#endif /* defined(UNICODE) */
|
2015-12-09 20:27:37 +03:00
|
|
|
dmsg[min(rc, size - 1)] = 0;
|
2016-02-06 00:28:45 +03:00
|
|
|
#ifdef FORMAT_MESSAGE_ALLOCATE_BUFFER
|
2015-12-09 20:27:37 +03:00
|
|
|
LocalFree(msg);
|
2016-02-06 00:28:45 +03:00
|
|
|
#else
|
|
|
|
free(msg);
|
|
|
|
#endif
|
2018-12-17 11:09:02 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
_snprintf(dmsg, size, "FAILURE: 0x%08"PRIX32"", GetLastError());
|
2015-12-09 20:27:37 +03:00
|
|
|
}
|
2018-12-17 11:09:02 +03:00
|
|
|
|
2015-12-16 17:15:46 +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;
|
|
|
|
}
|