1 | /* |
---|
2 | * Copyright (C) 2002 Red Hat, Inc. |
---|
3 | * |
---|
4 | * Permission is hereby granted, free of charge, to any person |
---|
5 | * obtaining a copy of this software and associated documentation |
---|
6 | * files (the "Software"), to deal in the Software without |
---|
7 | * restriction, including without limitation the rights to use, copy, |
---|
8 | * modify, merge, publish, distribute, sublicense, and/or sell copies |
---|
9 | * of the Software, and to permit persons to whom the Software is |
---|
10 | * furnished to do so, subject to the following conditions: |
---|
11 | * |
---|
12 | * The above copyright notice and this permission notice shall be |
---|
13 | * included in all copies or substantial portions of the Software. |
---|
14 | * |
---|
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
---|
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
---|
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
---|
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
---|
19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
---|
20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
---|
21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
---|
22 | * SOFTWARE. |
---|
23 | */ |
---|
24 | |
---|
25 | #include <sys/types.h> |
---|
26 | #include <stdlib.h> |
---|
27 | #include <stdio.h> |
---|
28 | #include <unistd.h> |
---|
29 | #include <string.h> |
---|
30 | #include <errno.h> |
---|
31 | #include <signal.h> |
---|
32 | |
---|
33 | #ifndef TRUE |
---|
34 | #define TRUE 1 |
---|
35 | #endif |
---|
36 | |
---|
37 | #ifndef FALSE |
---|
38 | #define FALSE 0 |
---|
39 | #endif |
---|
40 | |
---|
41 | #ifndef NULL |
---|
42 | #define NULL ((void*) 0) |
---|
43 | #endif |
---|
44 | |
---|
45 | #ifdef HAVE_BACKTRACE |
---|
46 | #include <execinfo.h> |
---|
47 | static void |
---|
48 | print_backtrace (void) |
---|
49 | { |
---|
50 | void *bt[500]; |
---|
51 | int bt_size; |
---|
52 | int i; |
---|
53 | char **syms; |
---|
54 | |
---|
55 | bt_size = backtrace (bt, 500); |
---|
56 | |
---|
57 | syms = backtrace_symbols (bt, bt_size); |
---|
58 | |
---|
59 | i = 0; |
---|
60 | while (i < bt_size) |
---|
61 | { |
---|
62 | fprintf (stderr, " %s\n", syms[i]); |
---|
63 | ++i; |
---|
64 | } |
---|
65 | |
---|
66 | free (syms); |
---|
67 | } |
---|
68 | #else |
---|
69 | static void |
---|
70 | print_backtrace (void) |
---|
71 | { |
---|
72 | fprintf (stderr, "Not compiled with backtrace support\n"); |
---|
73 | } |
---|
74 | #endif |
---|
75 | |
---|
76 | static int error_trap_depth = 0; |
---|
77 | |
---|
78 | static int |
---|
79 | x_error_handler (Display *xdisplay, |
---|
80 | XErrorEvent *error) |
---|
81 | { |
---|
82 | char buf[64]; |
---|
83 | |
---|
84 | XGetErrorText (xdisplay, error->error_code, buf, 63); |
---|
85 | |
---|
86 | if (error_trap_depth == 0) |
---|
87 | { |
---|
88 | print_backtrace (); |
---|
89 | |
---|
90 | fprintf (stderr, "Unexpected X error: %s serial %ld error_code %d request_code %d minor_code %d)\n", |
---|
91 | buf, |
---|
92 | error->serial, |
---|
93 | error->error_code, |
---|
94 | error->request_code, |
---|
95 | error->minor_code); |
---|
96 | |
---|
97 | exit (1); |
---|
98 | } |
---|
99 | |
---|
100 | return 1; /* return value is meaningless */ |
---|
101 | } |
---|
102 | |
---|
103 | static void |
---|
104 | error_trap_push (SnDisplay *display, |
---|
105 | Display *xdisplay) |
---|
106 | { |
---|
107 | ++error_trap_depth; |
---|
108 | } |
---|
109 | |
---|
110 | static void |
---|
111 | error_trap_pop (SnDisplay *display, |
---|
112 | Display *xdisplay) |
---|
113 | { |
---|
114 | if (error_trap_depth == 0) |
---|
115 | { |
---|
116 | fprintf (stderr, "Error trap underflow!\n"); |
---|
117 | exit (1); |
---|
118 | } |
---|
119 | |
---|
120 | XSync (xdisplay, False); /* get all errors out of the queue */ |
---|
121 | --error_trap_depth; |
---|
122 | } |
---|