3
LD_PRELOAD library that gives statistic on number of roundtrips in an application.
5
$XREPLY_BACKTRACE defines whether and how backtraces will be printed for every
6
roundtrip. If not set, only total number of roundtrips is printed after the process
7
exits. If set to a number, backtrace for every roundtrip will be printed, and the
8
backtraces will be as deep as the given number. If set to C<number> (e.g. C10),
9
the backtraces will be "compressed" - every backtrace will be printed only once
10
after the process exits, together with number of times it occurred.
22
#include <X11/Xlibint.h>
24
/* Since these symbols are weak, the apps can provide their own, and therefore
25
e.g. temporarily suspend counting of roundtrips. At least theoretically,
26
I haven't really tried it.
28
__attribute((weak)) long ___xreply_reply_count = 0;
29
__attribute((weak)) int ___xreply_reply_enabled = 1;
31
#define MAX_BACKTRACES 1024
33
extern long ___xreply_reply_count;
34
extern int ___xreply_reply_enabled;
36
typedef Status(*xreply_ptr_t)(Display*, xReply*, int, Bool);
38
static xreply_ptr_t xreply_ptr = NULL;
39
static int xreply_backtrace_set = 0;
40
static int xreply_backtrace_type = 0;
42
struct xreply_struct {
47
static struct xreply_struct backtraces[ MAX_BACKTRACES ];
48
static int backtraces_size = 0;
50
static int xreply_compare(const void* left, const void* right)
52
int left_count = ((struct xreply_struct*)left)->count;
53
int right_count = ((struct xreply_struct*)right)->count;
54
return right_count - left_count;
57
static void xreply_print(void)
61
fd = open("/proc/self/cmdline", O_RDONLY);
67
fprintf(stderr, "XREPLY (%d : %s): %ld\n", getpid(), tmp, ___xreply_reply_count);
68
if(xreply_backtrace_type < 0) {
70
qsort(backtraces, backtraces_size, sizeof(struct xreply_struct), xreply_compare);
74
fprintf(stderr, "%d:%s\n\n", backtraces[ i ].count, backtraces[ i ].text);
78
static void xreply_backtrace()
81
int n = backtrace(trace, 256);
82
char** strings = backtrace_symbols(trace, n);
84
if(xreply_backtrace_type > 0) {
85
fprintf(stderr, "%ld [\n", ___xreply_reply_count);
86
if(n > xreply_backtrace_type)
87
n = xreply_backtrace_type;
92
fprintf(stderr, "%d: %s\n", i, strings[ i ]);
93
fprintf(stderr, "]\n");
95
char stack[ 256 * 20 ];
99
if(n > -xreply_backtrace_type)
100
n = -xreply_backtrace_type;
104
const char* start = strrchr(strings[ i ], '[');
106
assert(!"No [ in address.");
108
if(sscanf(start + 1, "0x%lx", &addr) != 1)
109
assert(!"Failed to parse address.");
110
if(sizeof(void*) == 4) {
111
sprintf(stack + pos, "0x%8lx", addr);
113
} else if(sizeof(void*) == 8) {
114
sprintf(stack + pos, "0x%16lx", addr);
117
assert(!"Unknown sizeof( void* ).");
122
if(strcmp(backtraces[ i ].key, stack) == 0) {
123
++backtraces[ i ].count;
126
if(i == backtraces_size) {
127
int stack_text_size = 10;
129
char* stack_text_pos;
133
stack_text_size += strlen(strings[ i ]) + 5;
134
stack_text = stack_text_pos = malloc(stack_text_size);
138
stack_text_pos = stpcpy(stack_text_pos, "\n");
139
stack_text_pos = stpcpy(stack_text_pos, strings[ i ]);
141
backtraces[ backtraces_size ].key = strdup(stack);
142
backtraces[ backtraces_size ].text = stack_text;
143
backtraces[ backtraces_size ].count = 1;
145
if(backtraces_size >= MAX_BACKTRACES)
146
assert(!"MAX_BACKTRACES reached.");
153
_XReply(dpy, rep, extra, discard)
154
register Display *dpy;
155
register xReply *rep;
156
int extra; /* number of 32-bit words expected after the reply */
157
Bool discard; /* should I discard data following "extra" words? */
159
if(___xreply_reply_enabled)
160
++___xreply_reply_count;
161
if(xreply_backtrace_set == 0) {
162
if(getenv("XREPLY_BACKTRACE") != NULL) {
163
// C<number> - compress backtraces, saved as negative value in xreply_backtrace_type
164
if(getenv("XREPLY_BACKTRACE")[ 0 ] == 'C')
165
xreply_backtrace_type = -atoi(getenv("XREPLY_BACKTRACE") + 1);
166
else // <number> - print the backtrace every time
167
xreply_backtrace_type = atoi(getenv("XREPLY_BACKTRACE"));
169
xreply_backtrace_type = 0;
171
if(xreply_backtrace_type != 0)
173
if(xreply_ptr == NULL) {
174
xreply_ptr = (xreply_ptr_t)dlsym(RTLD_NEXT, "_XReply");
175
if(xreply_ptr == NULL)
176
assert(!"dlsym() failed.");
177
atexit(xreply_print);
179
return xreply_ptr(dpy, rep, extra, discard);