Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: ui/base/x/x11_util.cc

Issue 7889040: Change X11 error handler override to allow easy X11 error checking. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix build issues Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This file defines utility functions for X11 (Linux only). This code has been 5 // This file defines utility functions for X11 (Linux only). This code has been
6 // ported from XCB since we can't use XCB on Ubuntu while its 32-bit support 6 // ported from XCB since we can't use XCB on Ubuntu while its 32-bit support
7 // remains woefully incomplete. 7 // remains woefully incomplete.
8 8
9 #include "ui/base/x/x11_util.h" 9 #include "ui/base/x/x11_util.h"
10 10
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 FROM_HERE, NewRunnableFunction(LogErrorEventDescription, d, *e)); 61 FROM_HERE, NewRunnableFunction(LogErrorEventDescription, d, *e));
62 return 0; 62 return 0;
63 } 63 }
64 64
65 int DefaultX11IOErrorHandler(Display* d) { 65 int DefaultX11IOErrorHandler(Display* d) {
66 // If there's an IO error it likely means the X server has gone away 66 // If there's an IO error it likely means the X server has gone away
67 LOG(ERROR) << "X IO Error detected"; 67 LOG(ERROR) << "X IO Error detected";
68 _exit(1); 68 _exit(1);
69 } 69 }
70 70
71 XErrorHandler current_error_handler = DefaultX11ErrorHandler;
72 XErrorEvent last_error_event = {0, NULL, 0, 0, 0, 0, 0 };
73
74 int BaseX11ErrorHandler(Display* d, XErrorEvent* e) {
75 last_error_event = *e;
76 return current_error_handler(d, e);
77 }
78
71 // Note: The caller should free the resulting value data. 79 // Note: The caller should free the resulting value data.
72 bool GetProperty(XID window, const std::string& property_name, long max_length, 80 bool GetProperty(XID window, const std::string& property_name, long max_length,
73 Atom* type, int* format, unsigned long* num_items, 81 Atom* type, int* format, unsigned long* num_items,
74 unsigned char** property) { 82 unsigned char** property) {
75 Atom property_atom = gdk_x11_get_xatom_by_name_for_display( 83 Atom property_atom = gdk_x11_get_xatom_by_name_for_display(
76 gdk_display_get_default(), property_name.c_str()); 84 gdk_display_get_default(), property_name.c_str());
77 85
78 unsigned long remaining_bytes = 0; 86 unsigned long remaining_bytes = 0;
79 return XGetWindowProperty(GetXDisplay(), 87 return XGetWindowProperty(GetXDisplay(),
80 window, 88 window,
81 property_atom, 89 property_atom,
82 0, // offset into property data to read 90 0, // offset into property data to read
83 max_length, // max length to get 91 max_length, // max length to get
84 False, // deleted 92 False, // deleted
85 AnyPropertyType, 93 AnyPropertyType,
86 type, 94 type,
87 format, 95 format,
88 num_items, 96 num_items,
89 &remaining_bytes, 97 &remaining_bytes,
90 property); 98 property);
91 } 99 }
92 100
101 std::string BuildX11ErrorString(const XErrorEvent& error_event) {
102 char error_str[256];
103 char request_str[256];
104
105 XGetErrorText(error_event.display, error_event.error_code, error_str,
106 sizeof(error_str));
107
108 strncpy(request_str, "Unknown", sizeof(request_str));
109 if (error_event.request_code < 128) {
110 std::string num = base::UintToString(error_event.request_code);
111 XGetErrorDatabaseText(error_event.display, "XRequest", num.c_str(),
112 "Unknown", request_str, sizeof(request_str));
113 } else {
114 int num_ext;
115 char** ext_list = XListExtensions(error_event.display, &num_ext);
116
117 for (int i = 0; i < num_ext; i++) {
118 int ext_code, first_event, first_error;
119 XQueryExtension(error_event.display, ext_list[i], &ext_code, &first_event,
120 &first_error);
121 if (error_event.request_code == ext_code) {
122 std::string msg = StringPrintf(
123 "%s.%d", ext_list[i], error_event.minor_code);
124 XGetErrorDatabaseText(error_event.display, "XRequest", msg.c_str(),
125 "Unknown", request_str, sizeof(request_str));
126 break;
127 }
128 }
129 XFreeExtensionList(ext_list);
130 }
131
132 std::ostringstream error_ss;
133 error_ss << "X Error detected: "
134 << "serial " << error_event.serial << ", "
135 << "error_code " << static_cast<int>(error_event.error_code)
136 << " (" << error_str << "), "
137 << "request_code " << static_cast<int>(error_event.request_code)
138 << ", "
139 << "minor_code " << static_cast<int>(error_event.minor_code)
140 << " (" << request_str << ")";
141 return error_ss.str();
142 }
143
93 } // namespace 144 } // namespace
94 145
95 bool XDisplayExists() { 146 bool XDisplayExists() {
96 return (gdk_display_get_default() != NULL); 147 return (gdk_display_get_default() != NULL);
97 } 148 }
98 149
99 Display* GetXDisplay() { 150 Display* GetXDisplay() {
100 return base::MessagePumpForUI::GetDefaultXDisplay(); 151 return base::MessagePumpForUI::GetDefaultXDisplay();
101 } 152 }
102 153
(...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after
672 event.xclient.format = 32; 723 event.xclient.format = 32;
673 event.xclient.data.l[0] = desktop; 724 event.xclient.data.l[0] = desktop;
674 event.xclient.data.l[1] = 1; // source indication 725 event.xclient.data.l[1] = 1; // source indication
675 726
676 int result = XSendEvent(GetXDisplay(), GetX11RootWindow(), False, 727 int result = XSendEvent(GetXDisplay(), GetX11RootWindow(), False,
677 SubstructureNotifyMask, &event); 728 SubstructureNotifyMask, &event);
678 return result == Success; 729 return result == Success;
679 } 730 }
680 731
681 void SetDefaultX11ErrorHandlers() { 732 void SetDefaultX11ErrorHandlers() {
733 XSetErrorHandler(BaseX11ErrorHandler);
682 SetX11ErrorHandlers(NULL, NULL); 734 SetX11ErrorHandlers(NULL, NULL);
683 } 735 }
684 736
685 bool IsX11WindowFullScreen(XID window) { 737 bool IsX11WindowFullScreen(XID window) {
686 // First check if _NET_WM_STATE property contains _NET_WM_STATE_FULLSCREEN. 738 // First check if _NET_WM_STATE property contains _NET_WM_STATE_FULLSCREEN.
687 static Atom atom = gdk_x11_get_xatom_by_name_for_display( 739 static Atom atom = gdk_x11_get_xatom_by_name_for_display(
688 gdk_display_get_default(), "_NET_WM_STATE_FULLSCREEN"); 740 gdk_display_get_default(), "_NET_WM_STATE_FULLSCREEN");
689 741
690 std::vector<Atom> atom_properties; 742 std::vector<Atom> atom_properties;
691 if (GetAtomArrayProperty(window, 743 if (GetAtomArrayProperty(window,
(...skipping 11 matching lines...) Expand all
703 gfx::Rect window_rect; 755 gfx::Rect window_rect;
704 if (!ui::GetWindowRect(window, &window_rect)) 756 if (!ui::GetWindowRect(window, &window_rect))
705 return false; 757 return false;
706 758
707 return monitor_rect.x == window_rect.x() && 759 return monitor_rect.x == window_rect.x() &&
708 monitor_rect.y == window_rect.y() && 760 monitor_rect.y == window_rect.y() &&
709 monitor_rect.width == window_rect.width() && 761 monitor_rect.width == window_rect.width() &&
710 monitor_rect.height == window_rect.height(); 762 monitor_rect.height == window_rect.height();
711 } 763 }
712 764
765 void CheckFailOnReportedX11Error() {
766 if (last_error_event.display) {
Ami GONE FROM CHROMIUM 2011/09/21 22:21:42 Reversing the test lets you early-return and de-in
dominich 2011/09/22 17:00:15 Done.
767 XSync(last_error_event.display, False);
768 LOG(FATAL) << BuildX11ErrorString(last_error_event);
769 last_error_event.display = NULL;
770 }
sadrul 2011/09/21 21:50:34 Can this return a bool depending on whether there
dominich 2011/09/21 22:05:16 It could, but there's a good chance this will have
771 }
772
713 // ---------------------------------------------------------------------------- 773 // ----------------------------------------------------------------------------
714 // These functions are declared in x11_util_internal.h because they require 774 // These functions are declared in x11_util_internal.h because they require
715 // XLib.h to be included, and it conflicts with many other headers. 775 // XLib.h to be included, and it conflicts with many other headers.
716 XRenderPictFormat* GetRenderARGB32Format(Display* dpy) { 776 XRenderPictFormat* GetRenderARGB32Format(Display* dpy) {
717 static XRenderPictFormat* pictformat = NULL; 777 static XRenderPictFormat* pictformat = NULL;
718 if (pictformat) 778 if (pictformat)
719 return pictformat; 779 return pictformat;
720 780
721 // First look for a 32-bit format which ignores the alpha value 781 // First look for a 32-bit format which ignores the alpha value
722 XRenderPictFormat templ; 782 XRenderPictFormat templ;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 // always blowing away the cache. If we are, then we should figure out why 841 // always blowing away the cache. If we are, then we should figure out why
782 // and make it bigger. 842 // and make it bigger.
783 NOTREACHED(); 843 NOTREACHED();
784 } 844 }
785 845
786 return pictformat; 846 return pictformat;
787 } 847 }
788 848
789 void SetX11ErrorHandlers(XErrorHandler error_handler, 849 void SetX11ErrorHandlers(XErrorHandler error_handler,
790 XIOErrorHandler io_error_handler) { 850 XIOErrorHandler io_error_handler) {
791 XSetErrorHandler(error_handler ? error_handler : DefaultX11ErrorHandler); 851 current_error_handler = error_handler ?
792 XSetIOErrorHandler( 852 error_handler : DefaultX11ErrorHandler;
793 io_error_handler ? io_error_handler : DefaultX11IOErrorHandler); 853 XSetIOErrorHandler(io_error_handler ?
854 io_error_handler : DefaultX11IOErrorHandler);
794 } 855 }
795 856
796 void LogErrorEventDescription(Display* dpy, 857 void LogErrorEventDescription(Display* dpy,
797 const XErrorEvent& error_event) { 858 const XErrorEvent& error_event) {
798 char error_str[256]; 859 DCHECK(dpy == error_event.display)
Ami GONE FROM CHROMIUM 2011/09/21 22:21:42 DCHECK_EQ work?
799 char request_str[256]; 860 << "Attempt to log error for mismatching X11 display.";
800 861 LOG(ERROR) << BuildX11ErrorString(error_event);
801 XGetErrorText(dpy, error_event.error_code, error_str, sizeof(error_str));
802
803 strncpy(request_str, "Unknown", sizeof(request_str));
804 if (error_event.request_code < 128) {
805 std::string num = base::UintToString(error_event.request_code);
806 XGetErrorDatabaseText(
807 dpy, "XRequest", num.c_str(), "Unknown", request_str,
808 sizeof(request_str));
809 } else {
810 int num_ext;
811 char** ext_list = XListExtensions(dpy, &num_ext);
812
813 for (int i = 0; i < num_ext; i++) {
814 int ext_code, first_event, first_error;
815 XQueryExtension(dpy, ext_list[i], &ext_code, &first_event, &first_error);
816 if (error_event.request_code == ext_code) {
817 std::string msg = StringPrintf(
818 "%s.%d", ext_list[i], error_event.minor_code);
819 XGetErrorDatabaseText(
820 dpy, "XRequest", msg.c_str(), "Unknown", request_str,
821 sizeof(request_str));
822 break;
823 }
824 }
825 XFreeExtensionList(ext_list);
826 }
827
828 LOG(ERROR)
829 << "X Error detected: "
830 << "serial " << error_event.serial << ", "
831 << "error_code " << static_cast<int>(error_event.error_code)
832 << " (" << error_str << "), "
833 << "request_code " << static_cast<int>(error_event.request_code) << ", "
834 << "minor_code " << static_cast<int>(error_event.minor_code)
835 << " (" << request_str << ")";
836 } 862 }
837 863
838 // ---------------------------------------------------------------------------- 864 // ----------------------------------------------------------------------------
839 // End of x11_util_internal.h 865 // End of x11_util_internal.h
840 866
841 867
842 } // namespace ui 868 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698