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"
15 #include "mozilla/X11Util.h"
18 #define BUFSIZE 2048 // What Xlib uses with XGetErrorDatabaseText
24 XExtension(const char* aName
, int aCode
) : name(aName
), major_opcode(aCode
) {}
27 MOZ_RUNINIT
static nsTArray
<XExtension
> sXExtensions
;
29 // man XSetErrorHandler says "the error handler should not call any
30 // functions (directly or indirectly) on the display that will generate
31 // protocol requests or that will look for input events" so we query the
32 // extension list early to avoid problems.
33 static void QueryXExtensions(Display
* aDisplay
) {
34 if (!sXExtensions
.IsEmpty() || !aDisplay
) {
38 char** extNames
= XListExtensions(aDisplay
, &nExts
);
42 for (int i
= 0; i
< nExts
; ++i
) {
43 int major_opcode
, first_event
, first_error
;
44 if (XQueryExtension(aDisplay
, extNames
[i
], &major_opcode
, &first_event
,
46 sXExtensions
.EmplaceBack(extNames
[i
], major_opcode
);
49 XFreeExtensionList(extNames
);
53 int X11Error(Display
* display
, XErrorEvent
* event
) {
55 // Get an indication of how long ago the request that caused the error was
57 unsigned long age
= NextRequest(display
) - event
->serial
;
60 // Get a string to represent the request that caused the error.
61 nsAutoCString message
;
62 if (event
->request_code
< 128) {
63 // Core protocol request
64 message
.AppendInt(event
->request_code
);
67 for (XExtension
& ext
: sXExtensions
) {
68 if (ext
.major_opcode
== event
->request_code
) {
69 message
.Append(ext
.name
);
71 message
.AppendInt(event
->minor_code
);
78 if (message
.IsEmpty()) {
81 XGetErrorDatabaseText(display
, "XRequest", message
.get(), "", buffer
,
89 notes
.AppendLiteral("Request ");
90 notes
.AppendInt(event
->request_code
);
92 notes
.AppendInt(event
->minor_code
);
95 notes
.AppendLiteral(": ");
97 // Get a string to describe the error.
98 XGetErrorText(display
, event
->error_code
, buffer
, sizeof(buffer
));
102 // For requests where Xlib gets the reply synchronously, |age| will be 1
103 // and the stack will include the function making the request. For
104 // asynchronous requests, the current stack will often be unrelated to the
105 // point of making the request, even if |age| is 1, but sometimes this may
106 // help us count back to the point of the request. With XSynchronize on,
107 // the stack will include the function making the request, even though
108 // |age| will be 2 for asynchronous requests because XSynchronize is
109 // implemented by an empty request from an XSync, which has not yet been
112 // XSynchronize returns the previous "after function". If a second
113 // XSynchronize call returns the same function after an enable call then
114 // synchronization must have already been enabled.
115 if (XSynchronize(display
, X11True
) == XSynchronize(display
, X11False
)) {
116 notes
.AppendLiteral("; sync");
118 notes
.AppendLiteral("; ");
119 notes
.AppendInt(uint32_t(age
));
120 notes
.AppendLiteral(" requests ago");
124 // The resource id is unlikely to be useful in a crash report without
125 // context of other ids, but add it to the debug console output.
126 notes
.AppendLiteral("; id=0x");
127 notes
.AppendInt(uint32_t(event
->resourceid
), 16);
129 // Actually, for requests where Xlib gets the reply synchronously,
130 // MOZ_X_SYNC=1 will not be necessary, but we don't have a table to tell us
131 // which requests get a synchronous reply.
132 if (!PR_GetEnv("MOZ_X_SYNC")) {
134 "\nRe-running with MOZ_X_SYNC=1 in the environment may give a more "
135 "helpful backtrace.");
140 NS_WARNING(notes
.get());
145 void InstallX11ErrorHandler() {
146 XSetErrorHandler(X11Error
);
148 if (Display
* display
= mozilla::DefaultXDisplay()) {
149 QueryXExtensions(display
);
150 if (PR_GetEnv("MOZ_X_SYNC")) {
151 XSynchronize(display
, X11True
);
156 void CleanupX11ErrorHandler() { sXExtensions
.Clear(); }