-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathedbt_unix.c
272 lines (231 loc) · 6.07 KB
/
edbt_unix.c
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <edidentifier.h>
__CIDENT_RCSID(gr_edbt_unix_c,"$Id: edbt_unix.c,v 1.17 2024/07/19 05:05:03 cvsuser Exp $")
/* -*- mode: c; indent-width: 4; -*- */
/* $Id: edbt_unix.c,v 1.17 2024/07/19 05:05:03 cvsuser Exp $
* unix backtrace implementation
*
*
*
* Copyright (c) 1998 - 2024, Adam Young.
* All rights reserved.
*
* This file is part of the GRIEF Editor.
*
* The GRIEF Editor is free software: you can redistribute it
* and/or modify it under the terms of the GRIEF Editor License.
*
* Redistributions of source code must retain the above copyright
* notice, and must be distributed with the license document above.
*
* Redistributions in binary form must reproduce the above copyright
* notice, and must include the license document above in
* the documentation and/or other materials provided with the
* distribution.
*
* The GRIEF Editor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* License for more details.
* ==end==
*/
#if defined(HAVE_CONFIG_H)
#include <config.h>
#endif
#include <edstacktrace.h>
// #define HAVE_BACKTRACE
// #define HAVE_WALKCONTEXT
// #define HAVE_PSTACK
// #define HAVE_PROCSTACK
#if defined(unix) || defined(__unix__)
#if !defined(__CYGWIN__) && !defined(linux)
#if defined(HAVE_BACKTRACE)
#include <execinfo.h>
#else /*!BACKTRACE*/
#if !defined(HAVE_PSTACK) && !defined(HAVE_PROCSTACK)
#if defined(sun) && defined(__SVR4)
#define HAVE_PSTACK
#elif defined(_AIX)
#define HAVE_PROCSTACK
#endif
#endif /*!PSTACK && !PROCSTACK*/
#if defined(HAVE_PSTACK) || defined(HAVE_PROCSTACK)
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#endif
#if defined(HAVE_WALKCONTEXT) /* Solaris 9 & later */
#include <sys/elf.h>
#include <ucontext.h>
#include <signal.h>
#include <dlfcn.h>
#endif
#ifdef _LP64
#define ElfSym Elf64_Sym
#else
#define ElfSym Elf32_Sym
#endif
#endif /*!BACKTRACE*/
void
edbt_init(const char *progname, int options, FILE *out)
{
__CUNUSED(progname)
__CUNUSED(options)
__CUNUSED(out)
}
void
edbt_auto(void)
{
}
#if defined(HAVE_PSTACK) || defined(HAVE_PROCSTACK)
static int
backtrace_pstack(FILE *out)
{
int pipefd[2];
pid_t pid;
if (pipe(pipefd) != 0) {
return -1;
}
if (-1 == (pid = fork1())) {
return -1;
} else if (0 == pid) {
char parent[16];
seteuid(0);
close(STDIN_FILENO);
close(STDOUT_FILENO);
dup2(pipefd[1],STDOUT_FILENO);
closefrom(STDERR_FILENO);
snprintf(parent, sizeof(parent), "%d", getppid());
#if defined(HAVE_PSTACK)
execle("/usr/bin/pstack", "pstack", parent, NULL);
#else
execle("/usr/bin/procstack", "procstack", parent, NULL);
#endif
_exit(1);
} else {
int status, done = 0;
char linebuf[512];
close(pipefd[1]);
while (! done) {
int bytesread = read(pipefd[0], linebuf, sizeof(linebuf)-1);
if (bytesread > 0) {
linebuf[bytesread] = 0;
fprintf(out, "%s", linebuf);
} else if ((bytesread < 0) || ((EINTR != errno) && (EAGAIN != errno))) {
done = 1;
}
}
close(pipefd[0]);
waitpid(pid, &status, 0);
if (status != 0)
return -1;
}
return 0;
}
#endif /*HAVE_PSTACK || HAVE_PROCSTACK*/
#if defined(HAVE_WALKCONTEXT)
/*
* called for each frame on the stack to print it's contents.
*/
struct wcargs {
FILE *out;
int depth;
};
static int
walkcontext_frame(uintptr_t pc, int signo, void *arg)
{
strict wcargs *args = (struct wcargs *}arg;
const int depth = args->depth;
Dl_info dlinfo = {0};
ElfSym *dlsym = NULL;
char header[64];
if (signo) {
char signame[SIG2STR_MAX];
if (sig2str(signo, signame) != 0) {
strcpy(signame, "unknown");
}
fprintf(out, "** Signal %d (%s)\n", signo, signame);
}
snprintf(header, sizeof(header), "%d: 0x%lx", depth, pc);
args->depth += 1;
if (dladdr1((void *) pc, &dlinfo, (void **) &dlsym, RTLD_DL_SYMENT)) {
unsigned long offset = pc - (uintptr_t) dlinfo.dli_saddr;
const char *symname;
if (offset < dlsym->st_size) { /* inside a function */
symname = dlinfo.dli_sname;
} else { /* found which file it was in, but not which function */
symname = "<section start>";
offset = pc - (uintptr_t)dlinfo.dli_fbase;
}
fprintf(out, "%s: %s:%s+0x%lx\n", header, dlinfo.dli_fname, symname, offset);
} else {
fprintf(out, "%s\n", header);
}
return 0;
}
#endif /*HAVE_WALKCONTEXT*/
void
edbt_stackdump(FILE *out, int level)
{
#if defined(HAVE_BACKTRACE)
size_t size, i;
void *frame[32];
char **strings;
fprintf(out, "\nBacktrace:\n");
size = backtrace(frame, 32);
strings = backtrace_symbols(frame, size);
for (i = 0; i < size; ++i) {
fprintf(out, "%d: %s\n", i, strings[i]);
}
free(strings);
#else /*!BACKTRACE*/
fprintf(out, "\nBacktrace:\n");
#if defined(HAVE_PSTACK) || defined(HAVE_PROCSTACK)
if (-1 == backtrace_pstack(out))
#endif
{
#if defined(HAVE_WALKCONTEXT)
struct wcargs args = {out, 1};
ucontext_t u = {0};
if (0 == getcontext(&u)) {
walkcontext(&u, walkcontext_frame, &args);
} else
#endif
{
fprintf(out, "Failed to get backtrace info");
}
}
fprintf(out, "\n");
#endif /*!BACKTRACE*/
}
#endif /*!__CYGWIN__ && !linux */
#else /*unix*/
#if defined(__APPLE__)
void
edbt_init(const char *progname, int options, FILE *out)
{
__CUNUSED(progname)
__CUNUSED(options)
__CUNUSED(out)
}
void
edbt_auto(void)
{
}
void
edbt_stackdump(FILE *out, int level)
{
__CUNUSED(out)
__CUNUSED(level)
}
#endif /*__APPLE__*/
#endif /*!unix*/
/*quiet archivers*/
extern int edbt_unix(void);
int
edbt_unix(void)
{
return 0;
}
/*end*/