summaryrefslogtreecommitdiff
path: root/src/x11/meta-x11-errors.c
blob: a9e74b3da7088f4c48e43d068c4946d1d7db68de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/*
 * Copyright (C) 2001 Havoc Pennington, error trapping inspired by GDK
 * code copyrighted by the GTK team.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

/**
 * SECTION:errors
 * @title: Errors
 * @short_description: Mutter X error handling
 */

#include "config.h"

#include "meta/meta-x11-errors.h"

#include <errno.h>
#include <stdlib.h>
#include <X11/Xlibint.h>

#include "x11/meta-x11-display-private.h"

/* compare X sequence numbers handling wraparound */
#define SEQUENCE_COMPARE(a,op,b) (((long) (a) - (long) (b)) op 0)

typedef struct _MetaErrorTrap
{
  /* Next sequence when trap was pushed, i.e. first sequence to
   * ignore
   */
  unsigned long start_sequence;

  /* Next sequence when trap was popped, i.e. first sequence
   * to not ignore. 0 if trap is still active.
   */
  unsigned long end_sequence;

  /* Most recent error code within the sequence */
  int error_code;
} MetaErrorTrap;

/* Previously existing error handler */
typedef int (* MetaXErrorHandler) (Display *, XErrorEvent *);
static MetaXErrorHandler old_error_handler = NULL;
/* number of times we've pushed the error handler */
static int error_handler_push_count = 0;
static MetaX11Display *error_x11_display = NULL;

/* look up the extension name for a given major opcode.  grubs around in
 * xlib to do it since a) it’s already cached there b) XQueryExtension
 * emits protocol so we can’t use it in an error handler.
 */
static const char *
decode_request_code (Display *dpy,
                     int      code)
{
  _XExtension *ext;

  if (code < 128)
    return "core protocol";

  for (ext = dpy->ext_procs; ext; ext = ext->next)
    {
      if (ext->codes.major_opcode == code)
        return ext->name;
    }

  return "unknown";
}

static void
display_error_event (MetaX11Display *x11_display,
                     XErrorEvent    *error)
{
  GList *l;
  gboolean ignore = FALSE;

  for (l = x11_display->error_traps; l; l = l->next)
    {
      MetaErrorTrap *trap;

      trap = l->data;

      if (SEQUENCE_COMPARE (trap->start_sequence, <=, error->serial) &&
          (trap->end_sequence == 0 ||
           SEQUENCE_COMPARE (trap->end_sequence, >, error->serial)))
        {
          ignore = TRUE;
          trap->error_code = error->error_code;
          break; /* only innermost trap gets the error code */
        }
    }

  if (!ignore)
    {
      char buf[64];

      XGetErrorText (x11_display->xdisplay, error->error_code, buf, 63);

      g_error ("Received an X Window System error.\n"
               "This probably reflects a bug in the program.\n"
               "The error was '%s'.\n"
               "  (Details: serial %ld error_code %d request_code %d (%s) minor_code %d)\n"
               "  (Note to programmers: normally, X errors are reported asynchronously;\n"
               "   that is, you will receive the error a while after causing it.\n"
               "   To debug your program, run it with the MUTTER_SYNC environment\n"
               "   variable to change this behavior. You can then get a meaningful\n"
               "   backtrace from your debugger if you break on the meta_x_error() function.)",
               buf,
               error->serial,
               error->error_code,
               error->request_code,
               decode_request_code (x11_display->xdisplay, error->request_code),
               error->minor_code);
    }
}

static int
meta_x_error (Display     *xdisplay,
              XErrorEvent *error)
{
  if (error->error_code)
    display_error_event (error_x11_display, error);

  return 0;
}

static void
error_handler_push (void)
{
  MetaXErrorHandler previous_handler;

  previous_handler = XSetErrorHandler (meta_x_error);

  if (error_handler_push_count > 0)
    {
      if (previous_handler != meta_x_error)
        g_warning ("XSetErrorHandler() called with a Mutter X11 error trap pushed. Don't do that.");
    }
  else
    {
      old_error_handler = previous_handler;
    }

  error_handler_push_count += 1;
}

static void
error_handler_pop (void)
{
  g_return_if_fail (error_handler_push_count > 0);

  error_handler_push_count -= 1;

  if (error_handler_push_count == 0)
    {
      XSetErrorHandler (old_error_handler);
      old_error_handler = NULL;
    }
}

static void
delete_outdated_error_traps (MetaX11Display *x11_display)
{
  GList *l;
  unsigned long processed_sequence;

  processed_sequence = XLastKnownRequestProcessed (x11_display->xdisplay);
  l = x11_display->error_traps;

  while (l != NULL)
    {
      MetaErrorTrap *trap = l->data;

      if (trap->end_sequence != 0 &&
          SEQUENCE_COMPARE (trap->end_sequence, <=, processed_sequence))
        {
          GList *link = l;

          l = l->next;
          x11_display->error_traps =
            g_list_delete_link (x11_display->error_traps, link);
          g_free (trap);
        }
      else
        {
          l = l->next;
        }
    }
}

void
meta_x11_display_init_error_traps (MetaX11Display *x11_display)
{
  error_x11_display = x11_display;
  XSetErrorHandler (meta_x_error);
}

void
meta_x11_error_trap_push (MetaX11Display *x11_display)
{
  MetaErrorTrap *trap;

  delete_outdated_error_traps (x11_display);

  /* set up the Xlib callback to tell us about errors */
  error_handler_push ();

  trap = g_new0 (MetaErrorTrap, 1);
  trap->start_sequence = XNextRequest (x11_display->xdisplay);
  trap->error_code = Success;

  x11_display->error_traps =
    g_list_prepend (x11_display->error_traps, trap);
}

static int
meta_x11_error_trap_pop_internal (MetaX11Display *x11_display,
                                  gboolean        need_code)
{
  MetaErrorTrap *trap = NULL;
  GList *l;
  int result;

  g_return_val_if_fail (x11_display->error_traps != NULL, Success);

  /* Find the first trap that hasn't been popped already */
  for (l = x11_display->error_traps; l; l = l->next)
    {
      trap = l->data;

      if (trap->end_sequence == 0)
        break;
    }

  g_return_val_if_fail (trap != NULL, Success);
  g_assert (trap->end_sequence == 0);

  /* May need to sync to fill in trap->error_code if we care about
   * getting an error code.
   */
  if (need_code)
    {
      unsigned long processed_sequence, next_sequence;

      next_sequence = XNextRequest (x11_display->xdisplay);
      processed_sequence = XLastKnownRequestProcessed (x11_display->xdisplay);

      /* If our last request was already processed, there is no point
       * in syncing. i.e. if last request was a round trip (or even if
       * we got an event with the serial of a non-round-trip)
       */
      if ((next_sequence - 1) != processed_sequence)
        {
          XSync (x11_display->xdisplay, False);
        }

      result = trap->error_code;
    }
  else
    {
      result = Success;
    }

  /* record end of trap, giving us a range of
   * error sequences we'll ignore.
   */
  trap->end_sequence = XNextRequest (x11_display->xdisplay);

  /* remove the Xlib callback */
  error_handler_pop ();

  /* we may already be outdated */
  delete_outdated_error_traps (x11_display);

  return result;
}

void
meta_x11_error_trap_pop (MetaX11Display *x11_display)
{
  meta_x11_error_trap_pop_internal (x11_display, FALSE);
}

int
meta_x11_error_trap_pop_with_return (MetaX11Display *x11_display)
{
  return meta_x11_error_trap_pop_internal (x11_display, TRUE);
}