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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
/*
* Copyright 2008 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif
#include "os.h"
#include "misc.h"
#include <errno.h>
#include <string.h>
#ifdef HAVE_LIBUNWIND
#define UNW_LOCAL_ONLY
#include <libunwind.h>
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <dlfcn.h>
void
xorg_backtrace(void)
{
unw_cursor_t cursor;
unw_context_t context;
unw_word_t off;
unw_proc_info_t pip;
int ret, i = 0;
char procname[256];
const char *filename;
Dl_info dlinfo;
pip.unwind_info = NULL;
ret = unw_getcontext(&context);
if (ret) {
ErrorFSigSafe("unw_getcontext failed: %s [%d]\n", unw_strerror(ret),
ret);
return;
}
ret = unw_init_local(&cursor, &context);
if (ret) {
ErrorFSigSafe("unw_init_local failed: %s [%d]\n", unw_strerror(ret),
ret);
return;
}
ErrorFSigSafe("\n");
ErrorFSigSafe("Backtrace:\n");
ret = unw_step(&cursor);
while (ret > 0) {
ret = unw_get_proc_info(&cursor, &pip);
if (ret) {
ErrorFSigSafe("unw_get_proc_info failed: %s [%d]\n",
unw_strerror(ret), ret);
break;
}
off = 0;
ret = unw_get_proc_name(&cursor, procname, 256, &off);
if (ret && ret != -UNW_ENOMEM) {
if (ret != -UNW_EUNSPEC)
ErrorFSigSafe("unw_get_proc_name failed: %s [%d]\n",
unw_strerror(ret), ret);
procname[0] = '?';
procname[1] = 0;
}
if (dladdr((void *)(uintptr_t)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
*dlinfo.dli_fname)
filename = dlinfo.dli_fname;
else
filename = "?";
ErrorFSigSafe("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
ret == -UNW_ENOMEM ? "..." : "", (int)off,
(void *)(uintptr_t)(pip.start_ip + off));
ret = unw_step(&cursor);
if (ret < 0)
ErrorFSigSafe("unw_step failed: %s [%d]\n", unw_strerror(ret), ret);
}
ErrorFSigSafe("\n");
}
#else /* HAVE_LIBUNWIND */
#ifdef HAVE_BACKTRACE
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <dlfcn.h>
#include <execinfo.h>
void
xorg_backtrace(void)
{
const int BT_SIZE = 64;
void *array[BT_SIZE];
const char *mod;
int size, i;
Dl_info info;
ErrorFSigSafe("\n");
ErrorFSigSafe("Backtrace:\n");
size = backtrace(array, BT_SIZE);
for (i = 0; i < size; i++) {
int rc = dladdr(array[i], &info);
if (rc == 0) {
ErrorFSigSafe("%u: ?? [%p]\n", i, array[i]);
continue;
}
mod = (info.dli_fname && *info.dli_fname) ? info.dli_fname : "(vdso)";
if (info.dli_saddr)
ErrorFSigSafe(
"%u: %s (%s+0x%x) [%p]\n",
i,
mod,
info.dli_sname,
(unsigned int)((char *) array[i] -
(char *) info.dli_saddr),
array[i]);
else
ErrorFSigSafe(
"%u: %s (%p+0x%x) [%p]\n",
i,
mod,
info.dli_fbase,
(unsigned int)((char *) array[i] -
(char *) info.dli_fbase),
array[i]);
}
ErrorFSigSafe("\n");
}
#else /* not glibc or glibc < 2.1 */
#if defined(__sun) && defined(__SVR4)
#define HAVE_PSTACK
#endif
#if defined(HAVE_WALKCONTEXT) /* Solaris 9 & later */
#include <ucontext.h>
#include <signal.h>
#include <dlfcn.h>
#include <sys/elf.h>
#ifdef _LP64
#define ElfSym Elf64_Sym
#else
#define ElfSym Elf32_Sym
#endif
/* Called for each frame on the stack to print it's contents */
static int
xorg_backtrace_frame(uintptr_t pc, int signo, void *arg)
{
Dl_info dlinfo;
ElfSym *dlsym;
char header[32];
int depth = *((int *) arg);
if (signo) {
char signame[SIG2STR_MAX];
if (sig2str(signo, signame) != 0) {
strcpy(signame, "unknown");
}
ErrorFSigSafe("** Signal %u (%s)\n", signo, signame);
}
snprintf(header, sizeof(header), "%d: 0x%lx", depth, pc);
*((int *) arg) = depth + 1;
/* Ask system dynamic loader for info on the address */
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;
}
ErrorFSigSafe("%s: %s:%s+0x%x\n", header, dlinfo.dli_fname, symname,
offset);
}
else {
/* Couldn't find symbol info from system dynamic loader, should
* probably poke elfloader here, but haven't written that code yet,
* so we just print the pc.
*/
ErrorFSigSafe("%s\n", header);
}
return 0;
}
#endif /* HAVE_WALKCONTEXT */
#include <sys/types.h>
#if !defined(__WIN32__) || defined(__CYGWIN__)
#include <sys/wait.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/*
fork/exec a program to create a backtrace
Returns 0 if successful.
*/
static int
xorg_backtrace_exec_wrapper(const char *path)
{
#if defined(WIN32) && !defined(__CYGWIN__)
ErrorFSigSafe("Backtrace not implemented on Windows");
return -1;
#else
pid_t kidpid;
int pipefd[2];
if (pipe(pipefd) != 0) {
return -1;
}
kidpid = fork();
if (kidpid == -1) {
/* ERROR */
return -1;
}
else if (kidpid == 0) {
/* CHILD */
char parent[16];
seteuid(0);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
dup2(pipefd[1], STDOUT_FILENO);
dup2(pipefd[1], STDERR_FILENO);
close(pipefd[1]);
snprintf(parent, sizeof(parent), "%d", getppid());
execl(path, path, parent, NULL);
exit(1);
}
else {
/* PARENT */
char btline[256];
int kidstat;
int bytesread;
int done = 0;
close(pipefd[1]);
while (!done) {
bytesread = 0;
do
{
int n = read(pipefd[0], &btline[bytesread], 1);
if (n <= 0)
break;
bytesread = bytesread + n;
if (btline[bytesread-1] == '\n')
break;
}
while (bytesread < sizeof(btline));
if (bytesread > 0) {
btline[bytesread] = 0;
ErrorFSigSafe("%s", btline);
}
else if ((bytesread == 0) ||
((errno != EINTR) && (errno != EAGAIN)))
done = 1;
}
close(pipefd[0]);
waitpid(kidpid, &kidstat, 0);
if (!(WIFEXITED(kidstat) && WEXITSTATUS(kidstat) == 0)) {
ErrorFSigSafe("%s failed with returncode %d\n", path,
WEXITSTATUS(kidstat));
return -1;
}
}
return 0;
#endif
}
#ifdef HAVE_PSTACK
static int
xorg_backtrace_pstack(void)
{
return xorg_backtrace_exec_wrapper("/usr/bin/pstack");
}
#endif
static int
xorg_backtrace_script(void)
{
return xorg_backtrace_exec_wrapper(BINDIR "/xorg-backtrace");
}
#if defined(HAVE_PSTACK) || defined(HAVE_WALKCONTEXT)
void
xorg_backtrace(void)
{
ErrorFSigSafe("\n");
ErrorFSigSafe("Backtrace:\n");
#ifdef HAVE_PSTACK
/* First try fork/exec of pstack - otherwise fall back to walkcontext
pstack is preferred since it can print names of non-exported functions */
if (xorg_backtrace_pstack() < 0)
#endif
{
#ifdef HAVE_WALKCONTEXT
ucontext_t u;
int depth = 1;
if (getcontext(&u) == 0)
walkcontext(&u, xorg_backtrace_frame, &depth);
else
#endif
ErrorFSigSafe("Failed to get backtrace info: %s\n", strerror(errno));
}
ErrorFSigSafe("\n");
}
#else
/* Default fallback if we can't find any way to get a backtrace */
void
xorg_backtrace(void)
{
if (xorg_backtrace_script() == 0)
return;
}
/* Cygwin-specific crash-reporter glue */
#include <X11/Xwindows.h>
#include <sys/cygwin.h>
typedef int (*PFNCYGWINCRASHREPORTERINIT)(const char *, const char *);
typedef void (*PFNCYGWINCRASHREPORTERREPORT)(EXCEPTION_POINTERS *ep);
PFNCYGWINCRASHREPORTERREPORT crashreporter_report = NULL;
#define CRASHREPORT_URL "http://www.dronecode.org.uk/cgi-bin/addreport.php"
void
xorg_crashreport_init(const char *logfile)
{
/* Initialize crashreporter, if available */
HMODULE h = LoadLibrary("cygwin-crashreporter-hooks.dll");
if (h)
{
int result = 0;
PFNCYGWINCRASHREPORTERINIT crashreporter_init = (PFNCYGWINCRASHREPORTERINIT) GetProcAddress (h, "CygwinCrashReporterInit");
crashreporter_report = (PFNCYGWINCRASHREPORTERREPORT) GetProcAddress (h, "CygwinCrashReporterReport");
if (crashreporter_init && crashreporter_report)
{
char *windows_logfile = cygwin_create_path(CCP_POSIX_TO_WIN_A, logfile);
result = (*crashreporter_init) (CRASHREPORT_URL, windows_logfile);
if (!result)
ErrorF("Failed to initialize crashreporting\n");
else
DebugF("crashreporting initialized, status %d\n", result);
free(windows_logfile);
}
else
{
ErrorF("Could not locate crashreporting functions\n");
}
if (!result)
FreeLibrary (h);
}
else
{
DebugF("Could not load crashreporter dll\n");
}
}
#ifdef CW_EXCEPTION_RECORD_FROM_SIGINFO_T
#include <ucontext.h>
#endif
void
xorg_crashreport(int signo, siginfo_t *sip, void *sigcontext)
{
if (crashreporter_report)
{
#ifdef CW_EXCEPTION_RECORD_FROM_SIGINFO_T
ucontext_t *ucontext = (ucontext_t *)sigcontext;
EXCEPTION_RECORD er;
int res = !cygwin_internal(CW_EXCEPTION_RECORD_FROM_SIGINFO_T, sip, &er);
if (ucontext && res)
{
EXCEPTION_POINTERS ep;
ep.ExceptionRecord = &er;
ep.ContextRecord = (CONTEXT *)(&ucontext->uc_mcontext);
crashreporter_report(&ep);
}
else
#endif
crashreporter_report(NULL);
}
}
#endif
#endif
#endif
|