Bug 628949 - Update visible region / glass regions after we paint. r=roc a=2.0.
[mozilla-central.git] / toolkit / xre / nsX11ErrorHandler.cpp
blob4bfd81d5c9f2586b6ef807958ae2ac8d8b4c4082
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
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/
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.
15 * The Original Code is mozilla.org code.
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.
22 * Contributor(s):
23 * Karl Tomlinson <karlt+@karlt.net>
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.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsX11ErrorHandler.h"
41 #ifdef MOZ_IPC
42 #include "mozilla/plugins/PluginProcessChild.h"
43 using mozilla::plugins::PluginProcessChild;
44 #endif
46 #include "prenv.h"
47 #include "nsXULAppAPI.h"
48 #include "nsExceptionHandler.h"
49 #include "nsDebug.h"
51 #include "mozilla/X11Util.h"
52 #include <X11/Xlib.h>
54 #define BUFSIZE 2048 // What Xlib uses with XGetErrorDatabaseText
56 extern "C" {
57 static int
58 IgnoreError(Display *display, XErrorEvent *event) {
59 return 0; // This return value is ignored.
62 static int
63 X11Error(Display *display, XErrorEvent *event) {
64 nsCAutoString notes;
65 char buffer[BUFSIZE];
67 // Get an indication of how long ago the request that caused the error was
68 // made. Do this before querying extensions etc below.
69 unsigned long age = NextRequest(display) - event->serial;
71 // Ignore subsequent errors, which may get processed during the extension
72 // queries below for example.
73 XSetErrorHandler(IgnoreError);
75 // Get a string to represent the request that caused the error.
76 nsCAutoString message;
77 if (event->request_code < 128) {
78 // Core protocol request
79 message.AppendInt(event->request_code);
80 } else {
81 // Extension request
82 int nExts;
83 char** extNames = XListExtensions(display, &nExts);
84 if (extNames) {
85 for (int i = 0; i < nExts; ++i) {
86 int major_opcode, first_event, first_error;
87 if (XQueryExtension(display, extNames[i],
88 &major_opcode, &first_event, &first_error)
89 && major_opcode == event->request_code) {
90 message.Append(extNames[i]);
91 message.Append('.');
92 message.AppendInt(event->minor_code);
93 break;
97 XFreeExtensionList(extNames);
101 if (message.IsEmpty()) {
102 buffer[0] = '\0';
103 } else {
104 XGetErrorDatabaseText(display, "XRequest", message.get(), "",
105 buffer, sizeof(buffer));
108 if (buffer[0]) {
109 notes.Append(buffer);
110 } else {
111 notes.Append("Request ");
112 notes.AppendInt(event->request_code);
113 notes.Append('.');
114 notes.AppendInt(event->minor_code);
117 notes.Append(": ");
119 // Get a string to describe the error.
120 XGetErrorText(display, event->error_code, buffer, sizeof(buffer));
121 notes.Append(buffer);
123 // For requests where Xlib gets the reply synchronously, |age| will be 1
124 // and the stack will include the function making the request. For
125 // asynchronous requests, the current stack will often be unrelated to the
126 // point of making the request, even if |age| is 1, but sometimes this may
127 // help us count back to the point of the request. With XSynchronize on,
128 // the stack will include the function making the request, even though
129 // |age| will be 2 for asynchronous requests because XSynchronize is
130 // implemented by an empty request from an XSync, which has not yet been
131 // processed.
132 if (age > 1) {
133 // XSynchronize returns the previous "after function". If a second
134 // XSynchronize call returns the same function after an enable call then
135 // synchronization must have already been enabled.
136 if (XSynchronize(display, True) == XSynchronize(display, False)) {
137 notes.Append("; sync");
138 } else {
139 notes.Append("; ");
140 notes.AppendInt(PRUint32(age));
141 notes.Append(" requests ago");
145 #ifdef MOZ_CRASHREPORTER
146 switch (XRE_GetProcessType()) {
147 case GeckoProcessType_Default:
148 CrashReporter::AppendAppNotesToCrashReport(notes);
149 break;
150 #ifdef MOZ_IPC
151 case GeckoProcessType_Plugin:
152 if (CrashReporter::GetEnabled()) {
153 // This is assuming that X operations are performed on the plugin
154 // thread. If plugins are using X on another thread, then we'll need to
155 // handle that differently.
156 PluginProcessChild::AppendNotesToCrashReport(notes);
158 break;
159 #endif
160 default:
161 ; // crash report notes not supported.
163 #endif
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 notes.Append("; id=0x");
169 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 if (!PR_GetEnv("MOZ_X_SYNC")) {
175 notes.Append("\nRe-running with MOZ_X_SYNC=1 in the environment may give a more helpful backtrace.");
177 #endif
178 #endif
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
188 #endif
190 NS_RUNTIMEABORT(notes.get());
191 return 0; // not reached
195 void
196 InstallX11ErrorHandler()
198 XSetErrorHandler(X11Error);
200 Display *display = mozilla::DefaultXDisplay();
201 NS_ASSERTION(display, "No X display");
202 if (PR_GetEnv("MOZ_X_SYNC")) {
203 XSynchronize(display, True);