Bug 1888033 - [Menu Redesign] Add a secret setting and feature flag for the menu...
[gecko.git] / toolkit / xre / nsX11ErrorHandler.cpp
blob77f71db8f9d838cee3fbd47b2088e1964f49524f
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 "nsExceptionHandler.h"
11 #include "nsDebug.h"
12 #include "nsString.h"
13 #include "nsTArray.h"
15 #include "mozilla/X11Util.h"
16 #include <X11/Xlib.h>
18 #define BUFSIZE 2048 // What Xlib uses with XGetErrorDatabaseText
20 struct XExtension {
21 nsCString name;
22 int major_opcode;
24 XExtension(const char* aName, int aCode) : name(aName), major_opcode(aCode) {}
27 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) {
35 return;
37 int nExts = 0;
38 char** extNames = XListExtensions(aDisplay, &nExts);
39 if (!extNames) {
40 return;
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,
45 &first_error)) {
46 sXExtensions.EmplaceBack(extNames[i], major_opcode);
49 XFreeExtensionList(extNames);
52 extern "C" {
53 int X11Error(Display* display, XErrorEvent* event) {
54 #ifdef DEBUG
55 // Get an indication of how long ago the request that caused the error was
56 // made.
57 unsigned long age = NextRequest(display) - event->serial;
58 #endif
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);
65 } else {
66 // Extension request
67 for (XExtension& ext : sXExtensions) {
68 if (ext.major_opcode == event->request_code) {
69 message.Append(ext.name);
70 message.Append('.');
71 message.AppendInt(event->minor_code);
72 break;
77 char buffer[BUFSIZE];
78 if (message.IsEmpty()) {
79 buffer[0] = '\0';
80 } else {
81 XGetErrorDatabaseText(display, "XRequest", message.get(), "", buffer,
82 sizeof(buffer));
85 nsAutoCString notes;
86 if (buffer[0]) {
87 notes.Append(buffer);
88 } else {
89 notes.AppendLiteral("Request ");
90 notes.AppendInt(event->request_code);
91 notes.Append('.');
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));
99 notes.Append(buffer);
101 #ifdef DEBUG
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
110 // processed.
111 if (age > 1) {
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");
117 } else {
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);
128 # ifdef MOZ_X11
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")) {
133 notes.AppendLiteral(
134 "\nRe-running with MOZ_X_SYNC=1 in the environment may give a more "
135 "helpful backtrace.");
137 # endif
138 #endif
140 NS_WARNING(notes.get());
141 return 0;
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(); }