1 : /* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * the Mozilla Foundation <http://www.mozilla.org/>
19 : * Portions created by the Initial Developer are Copyright (C) 2010
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Karl Tomlinson <karlt+@karlt.net>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsX11ErrorHandler.h"
40 :
41 : #include "prenv.h"
42 : #include "nsXULAppAPI.h"
43 : #include "nsExceptionHandler.h"
44 : #include "nsDebug.h"
45 :
46 : #include "mozilla/X11Util.h"
47 : #include <X11/Xlib.h>
48 :
49 : #define BUFSIZE 2048 // What Xlib uses with XGetErrorDatabaseText
50 :
51 : extern "C" {
52 : static int
53 0 : X11Error(Display *display, XErrorEvent *event) {
54 : // Get an indication of how long ago the request that caused the error was
55 : // made.
56 0 : unsigned long age = NextRequest(display) - event->serial;
57 :
58 : // Get a string to represent the request that caused the error.
59 0 : nsCAutoString message;
60 0 : if (event->request_code < 128) {
61 : // Core protocol request
62 0 : message.AppendInt(event->request_code);
63 : } else {
64 : // Extension request
65 :
66 : // man XSetErrorHandler says "the error handler should not call any
67 : // functions (directly or indirectly) on the display that will generate
68 : // protocol requests or that will look for input events" so we use another
69 : // temporary Display to request extension information. This assumes on
70 : // the DISPLAY environment variable has been set and matches what was used
71 : // to open |display|.
72 0 : Display *tmpDisplay = XOpenDisplay(NULL);
73 0 : if (tmpDisplay) {
74 : int nExts;
75 0 : char** extNames = XListExtensions(tmpDisplay, &nExts);
76 : int first_error;
77 0 : if (extNames) {
78 0 : for (int i = 0; i < nExts; ++i) {
79 : int major_opcode, first_event;
80 0 : if (XQueryExtension(tmpDisplay, extNames[i],
81 0 : &major_opcode, &first_event, &first_error)
82 : && major_opcode == event->request_code) {
83 0 : message.Append(extNames[i]);
84 0 : message.Append('.');
85 0 : message.AppendInt(event->minor_code);
86 0 : break;
87 : }
88 : }
89 :
90 0 : XFreeExtensionList(extNames);
91 : }
92 0 : XCloseDisplay(tmpDisplay);
93 :
94 : #ifdef MOZ_WIDGET_GTK2
95 : // GDK2 calls XCloseDevice the devices that it opened on startup, but
96 : // the XI protocol no longer ensures that the devices will still exist.
97 : // If they have been removed, then a BadDevice error results. Ignore
98 : // this error.
99 0 : if (message.EqualsLiteral("XInputExtension.4") &&
100 : event->error_code == first_error + 0) {
101 0 : return 0;
102 : }
103 : #endif
104 : }
105 : }
106 :
107 : char buffer[BUFSIZE];
108 0 : if (message.IsEmpty()) {
109 0 : buffer[0] = '\0';
110 : } else {
111 : XGetErrorDatabaseText(display, "XRequest", message.get(), "",
112 0 : buffer, sizeof(buffer));
113 : }
114 :
115 0 : nsCAutoString notes;
116 0 : if (buffer[0]) {
117 0 : notes.Append(buffer);
118 : } else {
119 0 : notes.Append("Request ");
120 0 : notes.AppendInt(event->request_code);
121 0 : notes.Append('.');
122 0 : notes.AppendInt(event->minor_code);
123 : }
124 :
125 0 : notes.Append(": ");
126 :
127 : // Get a string to describe the error.
128 0 : XGetErrorText(display, event->error_code, buffer, sizeof(buffer));
129 0 : notes.Append(buffer);
130 :
131 : // For requests where Xlib gets the reply synchronously, |age| will be 1
132 : // and the stack will include the function making the request. For
133 : // asynchronous requests, the current stack will often be unrelated to the
134 : // point of making the request, even if |age| is 1, but sometimes this may
135 : // help us count back to the point of the request. With XSynchronize on,
136 : // the stack will include the function making the request, even though
137 : // |age| will be 2 for asynchronous requests because XSynchronize is
138 : // implemented by an empty request from an XSync, which has not yet been
139 : // processed.
140 0 : if (age > 1) {
141 : // XSynchronize returns the previous "after function". If a second
142 : // XSynchronize call returns the same function after an enable call then
143 : // synchronization must have already been enabled.
144 0 : if (XSynchronize(display, True) == XSynchronize(display, False)) {
145 0 : notes.Append("; sync");
146 : } else {
147 0 : notes.Append("; ");
148 0 : notes.AppendInt(PRUint32(age));
149 0 : notes.Append(" requests ago");
150 : }
151 : }
152 :
153 : #ifdef MOZ_CRASHREPORTER
154 0 : switch (XRE_GetProcessType()) {
155 : case GeckoProcessType_Default:
156 : case GeckoProcessType_Plugin:
157 : case GeckoProcessType_Content:
158 0 : CrashReporter::AppendAppNotesToCrashReport(notes);
159 0 : break;
160 : default:
161 : ; // crash report notes not supported.
162 : }
163 : #endif
164 :
165 : #ifdef DEBUG
166 : // The resource id is unlikely to be useful in a crash report without
167 : // context of other ids, but add it to the debug console output.
168 0 : notes.Append("; id=0x");
169 0 : notes.AppendInt(PRUint32(event->resourceid), 16);
170 : #ifdef MOZ_X11
171 : // Actually, for requests where Xlib gets the reply synchronously,
172 : // MOZ_X_SYNC=1 will not be necessary, but we don't have a table to tell us
173 : // which requests get a synchronous reply.
174 0 : if (!PR_GetEnv("MOZ_X_SYNC")) {
175 0 : notes.Append("\nRe-running with MOZ_X_SYNC=1 in the environment may give a more helpful backtrace.");
176 : }
177 : #endif
178 : #endif
179 :
180 : #ifdef MOZ_WIDGET_QT
181 : // We should not abort here if MOZ_X_SYNC is not set
182 : // until http://bugreports.qt.nokia.com/browse/QTBUG-4042
183 : // not fixed, just print error value
184 : if (!PR_GetEnv("MOZ_X_SYNC")) {
185 : fprintf(stderr, "XError: %s\n", notes.get());
186 : return 0; // temporary workaround for bug 161472
187 : }
188 : #endif
189 :
190 0 : NS_RUNTIMEABORT(notes.get());
191 0 : return 0; // not reached
192 : }
193 : }
194 :
195 : void
196 0 : InstallX11ErrorHandler()
197 : {
198 0 : XSetErrorHandler(X11Error);
199 :
200 0 : Display *display = mozilla::DefaultXDisplay();
201 0 : NS_ASSERTION(display, "No X display");
202 0 : if (PR_GetEnv("MOZ_X_SYNC")) {
203 0 : XSynchronize(display, True);
204 : }
205 0 : }
|