1 /* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsX11ErrorHandler.h"
9 #include "nsXULAppAPI.h"
12 #include "mozilla/X11Util.h"
15 #define BUFSIZE 2048 // What Xlib uses with XGetErrorDatabaseText
19 X11Error(Display
*display
, XErrorEvent
*event
) {
20 // Get an indication of how long ago the request that caused the error was
22 unsigned long age
= NextRequest(display
) - event
->serial
;
24 // Get a string to represent the request that caused the error.
25 nsAutoCString message
;
26 if (event
->request_code
< 128) {
27 // Core protocol request
28 message
.AppendInt(event
->request_code
);
32 // man XSetErrorHandler says "the error handler should not call any
33 // functions (directly or indirectly) on the display that will generate
34 // protocol requests or that will look for input events" so we use another
35 // temporary Display to request extension information. This assumes on
36 // the DISPLAY environment variable has been set and matches what was used
38 Display
*tmpDisplay
= XOpenDisplay(nullptr);
41 char** extNames
= XListExtensions(tmpDisplay
, &nExts
);
44 for (int i
= 0; i
< nExts
; ++i
) {
45 int major_opcode
, first_event
;
46 if (XQueryExtension(tmpDisplay
, extNames
[i
],
47 &major_opcode
, &first_event
, &first_error
)
48 && major_opcode
== event
->request_code
) {
49 message
.Append(extNames
[i
]);
51 message
.AppendInt(event
->minor_code
);
56 XFreeExtensionList(extNames
);
58 XCloseDisplay(tmpDisplay
);
60 #if (MOZ_WIDGET_GTK == 2)
61 // GDK2 calls XCloseDevice the devices that it opened on startup, but
62 // the XI protocol no longer ensures that the devices will still exist.
63 // If they have been removed, then a BadDevice error results. Ignore
65 if (message
.EqualsLiteral("XInputExtension.4") &&
66 event
->error_code
== first_error
+ 0) {
74 if (message
.IsEmpty()) {
77 XGetErrorDatabaseText(display
, "XRequest", message
.get(), "",
78 buffer
, sizeof(buffer
));
85 notes
.AppendLiteral("Request ");
86 notes
.AppendInt(event
->request_code
);
88 notes
.AppendInt(event
->minor_code
);
91 notes
.AppendLiteral(": ");
93 // Get a string to describe the error.
94 XGetErrorText(display
, event
->error_code
, buffer
, sizeof(buffer
));
97 // For requests where Xlib gets the reply synchronously, |age| will be 1
98 // and the stack will include the function making the request. For
99 // asynchronous requests, the current stack will often be unrelated to the
100 // point of making the request, even if |age| is 1, but sometimes this may
101 // help us count back to the point of the request. With XSynchronize on,
102 // the stack will include the function making the request, even though
103 // |age| will be 2 for asynchronous requests because XSynchronize is
104 // implemented by an empty request from an XSync, which has not yet been
107 // XSynchronize returns the previous "after function". If a second
108 // XSynchronize call returns the same function after an enable call then
109 // synchronization must have already been enabled.
110 if (XSynchronize(display
, True
) == XSynchronize(display
, False
)) {
111 notes
.AppendLiteral("; sync");
113 notes
.AppendLiteral("; ");
114 notes
.AppendInt(uint32_t(age
));
115 notes
.AppendLiteral(" requests ago");
120 // The resource id is unlikely to be useful in a crash report without
121 // context of other ids, but add it to the debug console output.
122 notes
.AppendLiteral("; id=0x");
123 notes
.AppendInt(uint32_t(event
->resourceid
), 16);
125 // Actually, for requests where Xlib gets the reply synchronously,
126 // MOZ_X_SYNC=1 will not be necessary, but we don't have a table to tell us
127 // which requests get a synchronous reply.
128 if (!PR_GetEnv("MOZ_X_SYNC")) {
129 notes
.AppendLiteral("\nRe-running with MOZ_X_SYNC=1 in the environment may give a more helpful backtrace.");
135 // We should not abort here if MOZ_X_SYNC is not set
136 // until http://bugreports.qt.nokia.com/browse/QTBUG-4042
137 // not fixed, just print error value
138 if (!PR_GetEnv("MOZ_X_SYNC")) {
139 fprintf(stderr
, "XError: %s\n", notes
.get());
140 return 0; // temporary workaround for bug 161472
144 NS_RUNTIMEABORT(notes
.get());
145 return 0; // not reached
149 #if (MOZ_WIDGET_GTK != 3)
151 InstallX11ErrorHandler()
153 XSetErrorHandler(X11Error
);
155 Display
*display
= mozilla::DefaultXDisplay();
156 NS_ASSERTION(display
, "No X display");
157 if (PR_GetEnv("MOZ_X_SYNC")) {
158 XSynchronize(display
, True
);