-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_xerror_linux.c
203 lines (192 loc) · 6.89 KB
/
handle_xerror_linux.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
//go:build !no_xorg
/*
Copyright 1985, 1986, 1987, 1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice 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 OPEN GROUP 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.
Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from The Open Group.
*/
#include <X11/Xlibint.h>
#include <stdio.h>
#include <string.h>
#include <dlfcn.h>
typedef int (*xGetErrorText) (
Display *display,
int code,
char *buffer_return,
int length
);
typedef int (*xGetErrorDatabaseText) (
Display *display,
_Xconst char *name,
_Xconst char *message,
_Xconst char *default_string,
char *buffer_return,
int length
);
struct X11Lib {
xGetErrorText XGetErrorText;
xGetErrorDatabaseText XGetErrorDatabaseText;
};
static int _XPrintDefaultError(
struct X11Lib *lib,
Display *dpy,
XErrorEvent *event,
FILE *fp)
{
char buffer[BUFSIZ];
char mesg[BUFSIZ];
char number[32];
const char *mtype = "XlibMessage";
register _XExtension *ext = (_XExtension *)NULL;
_XExtension *bext = (_XExtension *)NULL;
lib->XGetErrorText(dpy, event->error_code, buffer, BUFSIZ);
lib->XGetErrorDatabaseText(dpy, mtype, "XError", "X Error", mesg, BUFSIZ);
(void) fprintf(fp, "%s: %s\n ", mesg, buffer);
lib->XGetErrorDatabaseText(dpy, mtype, "MajorCode", "Request Major code %d",
mesg, BUFSIZ);
(void) fprintf(fp, mesg, event->request_code);
if (event->request_code < 128) {
snprintf(number, sizeof(number), "%d", event->request_code);
lib->XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, BUFSIZ);
} else {
for (ext = dpy->ext_procs;
ext && (ext->codes.major_opcode != event->request_code);
ext = ext->next)
;
if (ext) {
strncpy(buffer, ext->name, BUFSIZ);
buffer[BUFSIZ - 1] = '\0';
} else
buffer[0] = '\0';
}
(void) fprintf(fp, " (%s)\n", buffer);
if (event->request_code >= 128) {
lib->XGetErrorDatabaseText(dpy, mtype, "MinorCode", "Request Minor code %d",
mesg, BUFSIZ);
fputs(" ", fp);
(void) fprintf(fp, mesg, event->minor_code);
if (ext) {
snprintf(mesg, sizeof(mesg), "%s.%d", ext->name, event->minor_code);
lib->XGetErrorDatabaseText(dpy, "XRequest", mesg, "", buffer, BUFSIZ);
(void) fprintf(fp, " (%s)", buffer);
}
fputs("\n", fp);
}
if (event->error_code >= 128) {
/* kludge, try to find the extension that caused it */
buffer[0] = '\0';
for (ext = dpy->ext_procs; ext; ext = ext->next) {
if (ext->error_string)
(*ext->error_string)(dpy, event->error_code, &ext->codes,
buffer, BUFSIZ);
if (buffer[0]) {
bext = ext;
break;
}
if (ext->codes.first_error &&
ext->codes.first_error < (int)event->error_code &&
(!bext || ext->codes.first_error > bext->codes.first_error))
bext = ext;
}
if (bext)
snprintf(buffer, sizeof(buffer), "%s.%d", bext->name,
event->error_code - bext->codes.first_error);
else
strcpy(buffer, "Value");
lib->XGetErrorDatabaseText(dpy, mtype, buffer, "", mesg, BUFSIZ);
if (mesg[0]) {
fputs(" ", fp);
(void) fprintf(fp, mesg, event->resourceid);
fputs("\n", fp);
}
/* let extensions try to print the values */
for (ext = dpy->ext_procs; ext; ext = ext->next) {
if (ext->error_values)
(*ext->error_values)(dpy, event, fp);
}
} else if ((event->error_code == BadWindow) ||
(event->error_code == BadPixmap) ||
(event->error_code == BadCursor) ||
(event->error_code == BadFont) ||
(event->error_code == BadDrawable) ||
(event->error_code == BadColor) ||
(event->error_code == BadGC) ||
(event->error_code == BadIDChoice) ||
(event->error_code == BadValue) ||
(event->error_code == BadAtom)) {
if (event->error_code == BadValue)
lib->XGetErrorDatabaseText(dpy, mtype, "Value", "Value 0x%x",
mesg, BUFSIZ);
else if (event->error_code == BadAtom)
lib->XGetErrorDatabaseText(dpy, mtype, "AtomID", "AtomID 0x%x",
mesg, BUFSIZ);
else
lib->XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%x",
mesg, BUFSIZ);
fputs(" ", fp);
(void) fprintf(fp, mesg, event->resourceid);
fputs("\n", fp);
}
lib->XGetErrorDatabaseText(dpy, mtype, "ErrorSerial", "Error Serial #%d",
mesg, BUFSIZ);
fputs(" ", fp);
(void) fprintf(fp, mesg, event->serial);
lib->XGetErrorDatabaseText(dpy, mtype, "CurrentSerial", "Current Serial #%lld",
mesg, BUFSIZ);
fputs("\n ", fp);
(void) fprintf(fp, mesg, (unsigned long long)(X_DPY_GET_REQUEST(dpy)));
fputs("\n", fp);
if (event->error_code == BadImplementation) return 0;
return 1;
}
/*
handleXError is _XDefaultError from libx11/src/XlibInt.c stripped of its
exit(1) call and with dynamic XGetErrorText and XGetErrorDatabaseText added.
*/
int handleXError(Display *dpy, XErrorEvent *event) {
dlerror();
void *h = dlopen("libX11.so", RTLD_LAZY);
if (h == NULL) {
(void) fprintf(stderr, "failed to open libX11: %s\n", dlerror());
(void) fprintf(stderr, "X error: resourceid=%ld serial=%ld error_code=%d request_code=%d minor_code=%d\n",
event->resourceid, event->serial, event->error_code, event->request_code, event->minor_code);
return 0;
}
struct X11Lib lib = {
.XGetErrorText = dlsym(h, "XGetErrorText"),
.XGetErrorDatabaseText = dlsym(h, "xGetErrorDatabaseText")
};
if (lib.XGetErrorText != NULL && lib.XGetErrorDatabaseText != NULL) {
_XPrintDefaultError(&lib, dpy, event, stderr);
} else {
if (lib.XGetErrorText == NULL) {
(void) fprintf(stderr, "failed to get XGetErrorText: %s\n", dlerror());
}
if (lib.XGetErrorDatabaseText == NULL) {
(void) fprintf(stderr, "failed to get XGetErrorDatabaseText: %s\n", dlerror());
}
(void) fprintf(stderr, "X error: resourceid=%ld serial=%ld error_code=%d request_code=%d minor_code=%d\n",
event->resourceid, event->serial, event->error_code, event->request_code, event->minor_code);
}
dlclose(h);
char *e = dlerror();
if (e != NULL) {
(void) fprintf(stderr, "failed to close libX11: %s\n", e);
}
return 0;
}