Fix typo in 9b54bd30006c008b4a951331b273613d5bac3abf
[pm.git] / toolkit / xre / nsX11ErrorHandler.cpp
blob7c9591ea2b74819c58a27cab8ca27fb6409934e6
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"
8 #include "prenv.h"
9 #include "nsXULAppAPI.h"
10 #include "nsDebug.h"
12 #include "mozilla/X11Util.h"
13 #include <X11/Xlib.h>
15 #define BUFSIZE 2048 // What Xlib uses with XGetErrorDatabaseText
17 extern "C" {
18 int
19 X11Error(Display *display, XErrorEvent *event) {
20 // Get an indication of how long ago the request that caused the error was
21 // made.
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);
29 } else {
30 // Extension request
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
37 // to open |display|.
38 Display *tmpDisplay = XOpenDisplay(nullptr);
39 if (tmpDisplay) {
40 int nExts;
41 char** extNames = XListExtensions(tmpDisplay, &nExts);
42 int first_error;
43 if (extNames) {
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]);
50 message.Append('.');
51 message.AppendInt(event->minor_code);
52 break;
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
64 // this error.
65 if (message.EqualsLiteral("XInputExtension.4") &&
66 event->error_code == first_error + 0) {
67 return 0;
69 #endif
73 char buffer[BUFSIZE];
74 if (message.IsEmpty()) {
75 buffer[0] = '\0';
76 } else {
77 XGetErrorDatabaseText(display, "XRequest", message.get(), "",
78 buffer, sizeof(buffer));
81 nsAutoCString notes;
82 if (buffer[0]) {
83 notes.Append(buffer);
84 } else {
85 notes.AppendLiteral("Request ");
86 notes.AppendInt(event->request_code);
87 notes.Append('.');
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));
95 notes.Append(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
105 // processed.
106 if (age > 1) {
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");
112 } else {
113 notes.AppendLiteral("; ");
114 notes.AppendInt(uint32_t(age));
115 notes.AppendLiteral(" requests ago");
119 #ifdef DEBUG
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);
124 #ifdef MOZ_X11
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.");
131 #endif
132 #endif
134 #ifdef MOZ_WIDGET_QT
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
142 #endif
144 NS_RUNTIMEABORT(notes.get());
145 return 0; // not reached
149 #if (MOZ_WIDGET_GTK != 3)
150 void
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);
161 #endif