1 /*
2 * Copyright (C) 2010-2013 Kim Woelders
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies of the Software, its documentation and marketing & publicity
13 * materials, and acknowledgment shall be given in the documentation, materials
14 * and software packages that this Software was used.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 /*
24 * Based on hack by raster - ecore_x/xlib/ecore_x.c.
25 */
26 #include <dlfcn.h>
27 #include <execinfo.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <X11/Xlib.h>
32 #include "util.h"
33
34 typedef Status(RF) (Display * dpy, void *rep, int extra,
35 Bool discard);
36
37 /* find the real Xlib and the real X function */
38 static void *
GetFunc(const char * name)39 GetFunc(const char *name)
40 {
41 void *lib_xlib;
42 void *func;
43
44 lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY);
45
46 func = dlsym(lib_xlib, name);
47
48 return func;
49 }
50
51 extern Status _XReply(Display * dpy, void *rep, int extra, Bool discard);
52
53 __EXPORT__ Status
_XReply(Display * dpy,void * rep,int extra,Bool discard)54 _XReply(Display * dpy, void *rep, int extra, Bool discard)
55 {
56 static RF *func = NULL;
57
58 char s[1024];
59 void *bt[128];
60 int i, n, l;
61 char **sym;
62
63 if (!func)
64 func = (RF *) GetFunc("_XReply");
65
66 l = 0;
67 l += snprintf(s + l, sizeof(s) - l, "RT: ");
68 n = backtrace(bt, 128);
69 if (n <= 0)
70 goto done;
71
72 sym = backtrace_symbols(bt, n);
73 if (!sym)
74 goto done;
75
76 for (i = 1; i < n; i++)
77 {
78 #if 1
79 char *p, *name;
80
81 name = strchr(sym[i], '(');
82 if (name)
83 {
84 name++;
85 p = strchr(name, '+');
86 if (!p)
87 p = strchr(name, ')');
88 if (p)
89 *p = '\0';
90 }
91 if (!name || *name == '\0')
92 name = (char *)"?";
93 l += snprintf(s + l, sizeof(s) - l, "%s", name);
94 #else
95 l += snprintf(s + l, sizeof(s) - l, "%s", sym[i]);
96 #endif
97
98 if (i < n - 1)
99 l += snprintf(s + l, sizeof(s) - l, " < ");
100 }
101 free(sym);
102
103 done:
104 printf("%s\n", s);
105
106 return func(dpy, rep, extra, discard);
107 }
108