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"
10 #include "nsExceptionHandler.h"
13 #include "mozilla/X11Util.h"
16 #define BUFSIZE 2048 // What Xlib uses with XGetErrorDatabaseText
19 int 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
], &major_opcode
,
47 &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
);
63 if (message
.IsEmpty()) {
66 XGetErrorDatabaseText(display
, "XRequest", message
.get(), "", buffer
,
74 notes
.AppendLiteral("Request ");
75 notes
.AppendInt(event
->request_code
);
77 notes
.AppendInt(event
->minor_code
);
80 notes
.AppendLiteral(": ");
82 // Get a string to describe the error.
83 XGetErrorText(display
, event
->error_code
, buffer
, sizeof(buffer
));
86 // For requests where Xlib gets the reply synchronously, |age| will be 1
87 // and the stack will include the function making the request. For
88 // asynchronous requests, the current stack will often be unrelated to the
89 // point of making the request, even if |age| is 1, but sometimes this may
90 // help us count back to the point of the request. With XSynchronize on,
91 // the stack will include the function making the request, even though
92 // |age| will be 2 for asynchronous requests because XSynchronize is
93 // implemented by an empty request from an XSync, which has not yet been
96 // XSynchronize returns the previous "after function". If a second
97 // XSynchronize call returns the same function after an enable call then
98 // synchronization must have already been enabled.
99 if (XSynchronize(display
, X11True
) == XSynchronize(display
, X11False
)) {
100 notes
.AppendLiteral("; sync");
102 notes
.AppendLiteral("; ");
103 notes
.AppendInt(uint32_t(age
));
104 notes
.AppendLiteral(" requests ago");
108 switch (XRE_GetProcessType()) {
109 case GeckoProcessType_Default
:
110 case GeckoProcessType_Content
:
111 CrashReporter::AppendAppNotesToCrashReport(notes
);
113 default:; // crash report notes not supported.
117 // The resource id is unlikely to be useful in a crash report without
118 // context of other ids, but add it to the debug console output.
119 notes
.AppendLiteral("; id=0x");
120 notes
.AppendInt(uint32_t(event
->resourceid
), 16);
122 // Actually, for requests where Xlib gets the reply synchronously,
123 // MOZ_X_SYNC=1 will not be necessary, but we don't have a table to tell us
124 // which requests get a synchronous reply.
125 if (!PR_GetEnv("MOZ_X_SYNC")) {
127 "\nRe-running with MOZ_X_SYNC=1 in the environment may give a more "
128 "helpful backtrace.");
133 MOZ_CRASH_UNSAFE(notes
.get());
137 void InstallX11ErrorHandler() {
138 XSetErrorHandler(X11Error
);
140 Display
* display
= mozilla::DefaultXDisplay();
141 NS_ASSERTION(display
, "No X display");
142 if (PR_GetEnv("MOZ_X_SYNC")) {
143 XSynchronize(display
, X11True
);