nsX11ErrorHandler.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/. */
  5. #include "nsX11ErrorHandler.h"
  6. #include "prenv.h"
  7. #include "nsXULAppAPI.h"
  8. #include "nsDebug.h"
  9. #include "mozilla/X11Util.h"
  10. #include <X11/Xlib.h>
  11. #define BUFSIZE 2048 // What Xlib uses with XGetErrorDatabaseText
  12. extern "C" {
  13. int
  14. X11Error(Display *display, XErrorEvent *event) {
  15. // Get an indication of how long ago the request that caused the error was
  16. // made.
  17. unsigned long age = NextRequest(display) - event->serial;
  18. // Get a string to represent the request that caused the error.
  19. nsAutoCString message;
  20. if (event->request_code < 128) {
  21. // Core protocol request
  22. message.AppendInt(event->request_code);
  23. } else {
  24. // Extension request
  25. // man XSetErrorHandler says "the error handler should not call any
  26. // functions (directly or indirectly) on the display that will generate
  27. // protocol requests or that will look for input events" so we use another
  28. // temporary Display to request extension information. This assumes on
  29. // the DISPLAY environment variable has been set and matches what was used
  30. // to open |display|.
  31. Display *tmpDisplay = XOpenDisplay(nullptr);
  32. if (tmpDisplay) {
  33. int nExts;
  34. char** extNames = XListExtensions(tmpDisplay, &nExts);
  35. int first_error;
  36. if (extNames) {
  37. for (int i = 0; i < nExts; ++i) {
  38. int major_opcode, first_event;
  39. if (XQueryExtension(tmpDisplay, extNames[i],
  40. &major_opcode, &first_event, &first_error)
  41. && major_opcode == event->request_code) {
  42. message.Append(extNames[i]);
  43. message.Append('.');
  44. message.AppendInt(event->minor_code);
  45. break;
  46. }
  47. }
  48. XFreeExtensionList(extNames);
  49. }
  50. XCloseDisplay(tmpDisplay);
  51. #if (MOZ_WIDGET_GTK == 2)
  52. // GDK2 calls XCloseDevice the devices that it opened on startup, but
  53. // the XI protocol no longer ensures that the devices will still exist.
  54. // If they have been removed, then a BadDevice error results. Ignore
  55. // this error.
  56. if (message.EqualsLiteral("XInputExtension.4") &&
  57. event->error_code == first_error + 0) {
  58. return 0;
  59. }
  60. #endif
  61. }
  62. }
  63. char buffer[BUFSIZE];
  64. if (message.IsEmpty()) {
  65. buffer[0] = '\0';
  66. } else {
  67. XGetErrorDatabaseText(display, "XRequest", message.get(), "",
  68. buffer, sizeof(buffer));
  69. }
  70. nsAutoCString notes;
  71. if (buffer[0]) {
  72. notes.Append(buffer);
  73. } else {
  74. notes.AppendLiteral("Request ");
  75. notes.AppendInt(event->request_code);
  76. notes.Append('.');
  77. notes.AppendInt(event->minor_code);
  78. }
  79. notes.AppendLiteral(": ");
  80. // Get a string to describe the error.
  81. XGetErrorText(display, event->error_code, buffer, sizeof(buffer));
  82. notes.Append(buffer);
  83. // For requests where Xlib gets the reply synchronously, |age| will be 1
  84. // and the stack will include the function making the request. For
  85. // asynchronous requests, the current stack will often be unrelated to the
  86. // point of making the request, even if |age| is 1, but sometimes this may
  87. // help us count back to the point of the request. With XSynchronize on,
  88. // the stack will include the function making the request, even though
  89. // |age| will be 2 for asynchronous requests because XSynchronize is
  90. // implemented by an empty request from an XSync, which has not yet been
  91. // processed.
  92. if (age > 1) {
  93. // XSynchronize returns the previous "after function". If a second
  94. // XSynchronize call returns the same function after an enable call then
  95. // synchronization must have already been enabled.
  96. if (XSynchronize(display, True) == XSynchronize(display, False)) {
  97. notes.AppendLiteral("; sync");
  98. } else {
  99. notes.AppendLiteral("; ");
  100. notes.AppendInt(uint32_t(age));
  101. notes.AppendLiteral(" requests ago");
  102. }
  103. }
  104. #ifdef DEBUG
  105. // The resource id is unlikely to be useful in a crash report without
  106. // context of other ids, but add it to the debug console output.
  107. notes.AppendLiteral("; id=0x");
  108. notes.AppendInt(uint32_t(event->resourceid), 16);
  109. #ifdef MOZ_X11
  110. // Actually, for requests where Xlib gets the reply synchronously,
  111. // MOZ_X_SYNC=1 will not be necessary, but we don't have a table to tell us
  112. // which requests get a synchronous reply.
  113. if (!PR_GetEnv("MOZ_X_SYNC")) {
  114. notes.AppendLiteral("\nRe-running with MOZ_X_SYNC=1 in the environment may give a more helpful backtrace.");
  115. }
  116. #endif
  117. #endif
  118. NS_RUNTIMEABORT(notes.get());
  119. return 0; // not reached
  120. }
  121. }
  122. void
  123. InstallX11ErrorHandler()
  124. {
  125. XSetErrorHandler(X11Error);
  126. Display *display = mozilla::DefaultXDisplay();
  127. NS_ASSERTION(display, "No X display");
  128. if (PR_GetEnv("MOZ_X_SYNC")) {
  129. XSynchronize(display, True);
  130. }
  131. }