File: XError.c

package info (click to toggle)
fvwm 1:2.6.8-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 15,804 kB
  • sloc: ansic: 145,770; xml: 17,093; perl: 7,302; sh: 4,921; makefile: 1,094; yacc: 688; lex: 187; sed: 11
file content (307 lines) | stat: -rw-r--r-- 6,414 bytes parent folder | download
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
/* -*-c-*- */
/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see: <http://www.gnu.org/licenses/>
 */

/*
 * This function should be used by all modules and fvwm when a X error
 * occurs and the module exits.
 */

#include "config.h"

#include <stdio.h>
#include <X11/Xlib.h>
#include <fvwmlib.h>
#include "FRenderInit.h"
#include "XError.h"
#undef XSetErrorHandler

#define USE_GET_ERROR_TEXT 1
#ifndef USE_GET_ERROR_TEXT
static char *error_name(unsigned char code);
#endif
static char *request_name(unsigned char code);
static char unknown[32];

void do_coredump(void)
{
	fprintf(stderr, " Leaving a core dump now\n");
	{
		char *nullp;

		nullp = NULL;

		*nullp = 99;
	}
	/* exit if this fails */
	exit(99);
}

#define USE_GET_ERROR_TEXT 1
void PrintXErrorAndCoredump(Display *dpy, XErrorEvent *error, char *MyName)
{
	char msg[256];
	Bool suc = False;

	msg[255] = 0;
#ifdef USE_GET_ERROR_TEXT
	/* can't call this from within an error handler! */
	/* DV (21-Nov-2000): Well, actually we *can* call it in an error
	 * handler since it does not trigger a protocol request. */
	if (error->error_code >= FirstExtensionError)
	{
		suc = FRenderGetErrorText(error->error_code, msg);
	}
	if (!suc)
		XGetErrorText(dpy, error->error_code, msg, sizeof(msg));
	fprintf(stderr,"%s: Cause of next X Error.\n", MyName);
	fprintf(stderr, "   Error: %d (%s)\n", error->error_code, msg);
#else
	fprintf(stderr,"%s: Cause of next X Error.\n", MyName);
	if (error->error_code >= FirstExtensionError)
	{
		suc = FRenderGetErrorText(error->error_code, msg);
	}
	if (suc)
		fprintf(stderr, "   Error: %d (%s)\n",
			error->error_code, msg);
	else
		fprintf(stderr, "   Error: %d (%s)\n",
			error->error_code, error_name(error->error_code));
#endif
	fprintf(stderr, "   Major opcode of failed request:  %d (%s)\n",
		error->request_code, request_name(error->request_code));
	fprintf(stderr, "   Minor opcode of failed request:  %d \n",
		error->minor_code);
	/* error->resourceid may be uninitialised. This is no proble since we
	 * are dumping core anyway. */
	fprintf(stderr, "   Resource id of failed request:  0x%lx \n",
		error->resourceid);

	/* leave a coredump */
	do_coredump();
}

#ifndef USE_GET_ERROR_TEXT
/* this comes out of X.h */
static char *error_names[] = {
	"BadRequest",
	"BadValue",
	"BadWindow",
	"BadPixmap",
	"BadAtom",
	"BadCursor",
	"BadFont",
	"BadMatch",
	"BadDrawable",
	"BadAccess",
	"BadAlloc",
	"BadColor",
	"BadGC",
	"BadIDChoice",
	"BadName",
	"BadLength",
	"BadImplementation",
};

static char *error_name(unsigned char code)
{
	if (code == 0 || code > (sizeof(error_names) / sizeof(char *)))
	{
		sprintf(unknown, "Unknown: %d", (int)code);
		return unknown;
	}
	return error_names[code - 1];
}
#endif

/* this comes out of Xproto.h */
static char *code_names[] = {
	"CreateWindow",
	"ChangeWindowAttributes",
	"GetWindowAttributes",
	"DestroyWindow",
	"DestroySubwindows",
	"ChangeSaveSet",
	"ReparentWindow",
	"MapWindow",
	"MapSubwindows",
	"UnmapWindow",
	"UnmapSubwindows",
	"ConfigureWindow",
	"CirculateWindow",
	"GetGeometry",
	"QueryTree",
	"InternAtom",
	"GetAtomName",
	"ChangeProperty",
	"DeleteProperty",
	"GetProperty",
	"ListProperties",
	"SetSelectionOwner",
	"GetSelectionOwner",
	"ConvertSelection",
	"SendEvent",
	"GrabPointer",
	"UngrabPointer",
	"GrabButton",
	"UngrabButton",
	"ChangeActivePointerGrab",
	"GrabKeyboard",
	"UngrabKeyboard",
	"GrabKey",
	"UngrabKey",
	"AllowEvents",
	"GrabServer",
	"UngrabServer",
	"QueryPointer",
	"GetMotionEvents",
	"TranslateCoords",
	"WarpPointer",
	"SetInputFocus",
	"GetInputFocus",
	"QueryKeymap",
	"OpenFont",
	"CloseFont",
	"QueryFont",
	"QueryTextExtents",
	"ListFonts",
	"ListFontsWithInfo",
	"SetFontPath",
	"GetFontPath",
	"CreatePixmap",
	"FreePixmap",
	"CreateGC",
	"ChangeGC",
	"CopyGC",
	"SetDashes",
	"SetClipRectangles",
	"FreeGC",
	"ClearArea",
	"CopyArea",
	"CopyPlane",
	"PolyPoint",
	"PolyLine",
	"PolySegment",
	"PolyRectangle",
	"PolyArc",
	"FillPoly",
	"PolyFillRectangle",
	"PolyFillArc",
	"PutImage",
	"GetImage",
	"PolyText",
	"PolyText1",
	"ImageText",
	"ImageText1",
	"CreateColormap",
	"FreeColormap",
	"CopyColormapAndFree",
	"InstallColormap",
	"UninstallColormap",
	"ListInstalledColormaps",
	"AllocColor",
	"AllocNamedColor",
	"AllocColorCells",
	"AllocColorPlanes",
	"FreeColors",
	"StoreColors",
	"StoreNamedColor",
	"QueryColors",
	"LookupColor",
	"CreateCursor",
	"CreateGlyphCursor",
	"FreeCursor",
	"RecolorCursor",
	"QueryBestSize",
	"QueryExtension",
	"ListExtensions",
	"ChangeKeyboardMapping",
	"GetKeyboardMapping",
	"ChangeKeyboardControl",
	"GetKeyboardControl",
	"Bell",
	"ChangePointerControl",
	"GetPointerControl",
	"SetScreenSaver",
	"GetScreenSaver",
	"ChangeHosts",
	"ListHosts",
	"SetAccessControl",
	"SetCloseDownMode",
	"KillClient",
	"RotateProperties",
	"ForceScreenSaver",
	"SetPointerMapping",
	"GetPointerMapping",
	"SetModifierMapping",
	"GetModifierMapping",
};

static char *request_name(unsigned char code)
{
	if (code == 0 || code > (sizeof(code_names) / sizeof(char *)))
	{
		if (code == FRenderGetMajorOpCode())
		{
			sprintf(unknown, "XRender");
		}
		else
		{
			sprintf(unknown, "Unknown: %d", (int)code);
		}
		return unknown;
	}
	return code_names[code - 1];
}

/* -------------------------- error handler stack -------------------------- */

static ferror_handler_t old_handler = NULL;

void ferror_set_temp_error_handler(ferror_handler_t new_handler)
{
	if (old_handler != NULL)
	{
		do_coredump();
	}
	old_handler = XSetErrorHandler(old_handler);

	return;
}

void ferror_reset_temp_error_handler(void)
{
	if (old_handler == NULL)
	{
		do_coredump();
	}
	XSetErrorHandler(old_handler);
	old_handler = NULL;

	return;
}

int ferror_call_next_error_handler(Display *dpy, XErrorEvent *error)
{
	int rc;

	if (old_handler == NULL)
	{
		do_coredump();
	}
	rc = old_handler(dpy, error);

	return rc;
}