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
|
/**
* WinPR: Windows Portable Runtime
* WinPR Debugging helpers
*
* Copyright 2022 Armin Novak <armin.novak@thincast.com>
* Copyright 2022 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.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <assert.h>
#include <stdlib.h>
#include <unwind.h>
#include <winpr/string.h>
#include "debug.h"
#include <dlfcn.h>
typedef struct
{
uintptr_t pc;
void* langSpecificData;
} unwind_info_t;
typedef struct
{
size_t pos;
size_t size;
unwind_info_t* info;
} unwind_context_t;
static _Unwind_Reason_Code unwind_backtrace_callback(struct _Unwind_Context* context, void* arg)
{
unwind_context_t* ctx = arg;
assert(ctx);
if (ctx->pos < ctx->size)
{
unwind_info_t* info = &ctx->info[ctx->pos++];
info->pc = _Unwind_GetIP(context);
info->langSpecificData = (void*)_Unwind_GetLanguageSpecificData(context);
}
return _URC_NO_REASON;
}
void* winpr_unwind_backtrace(DWORD size)
{
_Unwind_Reason_Code rc = _URC_FOREIGN_EXCEPTION_CAUGHT;
unwind_context_t* ctx = calloc(1, sizeof(unwind_context_t));
if (!ctx)
goto fail;
ctx->size = size;
ctx->info = calloc(size, sizeof(unwind_info_t));
if (!ctx->info)
goto fail;
rc = _Unwind_Backtrace(unwind_backtrace_callback, ctx);
if (rc != _URC_END_OF_STACK)
goto fail;
return ctx;
fail:
winpr_unwind_backtrace_free(ctx);
return NULL;
}
void winpr_unwind_backtrace_free(void* buffer)
{
unwind_context_t* ctx = buffer;
if (!ctx)
return;
free(ctx->info);
free(ctx);
}
#define UNWIND_MAX_LINE_SIZE 1024
char** winpr_unwind_backtrace_symbols(void* buffer, size_t* used)
{
union
{
char* cp;
char** cpp;
} cnv;
unwind_context_t* ctx = buffer;
cnv.cpp = NULL;
if (!ctx)
return NULL;
cnv.cpp = calloc(ctx->pos * (sizeof(char*) + UNWIND_MAX_LINE_SIZE), sizeof(char*));
if (!cnv.cpp)
return NULL;
if (used)
*used = ctx->pos;
for (size_t x = 0; x < ctx->pos; x++)
{
char* msg = cnv.cp + ctx->pos * sizeof(char*) + x * UNWIND_MAX_LINE_SIZE;
const unwind_info_t* info = &ctx->info[x];
Dl_info dlinfo = { 0 };
int rc = dladdr((void*)info->pc, &dlinfo);
cnv.cpp[x] = msg;
if (rc == 0)
_snprintf(msg, UNWIND_MAX_LINE_SIZE, "unresolvable, address=%p", (void*)info->pc);
else
_snprintf(msg, UNWIND_MAX_LINE_SIZE, "dli_fname=%s [%p], dli_sname=%s [%p]",
dlinfo.dli_fname, dlinfo.dli_fbase, dlinfo.dli_sname, dlinfo.dli_saddr);
}
return cnv.cpp;
}
|