1 /*
2 * Author: William Chia-Wei Cheng (bill.cheng@acm.org)
3 *
4 * Copyright (C) 2001-2009, William Chia-Wei Cheng.
5 *
6 * This file may be distributed under the terms of the Q Public License
7 * as defined by Trolltech AS of Norway and appearing in the file
8 * LICENSE.QPL included in the packaging of this file.
9 *
10 * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING
11 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
13 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
14 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
16 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 * @(#)$Header: /mm2/home/cvs/bc-src/tgif/tgif_dbg.c,v 1.8 2011/05/16 16:22:00 william Exp $
19 */
20 #define _INCLUDE_FROM_TGIF_DBG_C_
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <X11/Xlib.h>
29 #include <X11/Xutil.h>
30 #include <X11/cursorfont.h>
31 #include <X11/keysym.h>
32
33 #ifdef _TGIF_DBG /* debug, do not translate */
34
35 #undef _Xconst
36 #define _Xconst
37
38 static char gszTraceFile[]="TGIF_DBG.LOG";
39
40 static FILE *gpTraceFile=NULL;
41
42 int gnAllocCount=0, gnFreeCount=0;
43 int gnCreateGCCount=0, gnFreeGCCount=0;
44 int gnCreateImageCount=0, gnDestroyImageCount=0;
45 int gnCreateCursorCount=0, gnFreeCursorCount=0;
46 int gnCreatePixmapCount=0, gnFreePixmapCount=0;
47 int gnCreateStringCount=0, gnFreeStringCount=0;
48
tgif_dbg_add_to_log(pszString)49 void tgif_dbg_add_to_log(pszString)
50 char *pszString;
51 {
52 if (gpTraceFile != NULL) {
53 fprintf(gpTraceFile, "%% %s\n", (pszString==NULL) ? "" : pszString);
54 }
55 }
56
tgif_dbg_enable(nEnable)57 void tgif_dbg_enable(nEnable)
58 int nEnable;
59 {
60 static FILE *st_fp=NULL;
61
62 if (nEnable) {
63 gpTraceFile = st_fp;
64 } else {
65 st_fp = gpTraceFile;
66 gpTraceFile = NULL;
67 }
68 }
69
70 /* --------------- utility functions --------------- */
71
got_trigger(nOne)72 void got_trigger(nOne)
73 int nOne;
74 {
75 if (nOne) { }
76 }
77
tgif_track(nBegin)78 void tgif_track(nBegin)
79 int nBegin;
80 {
81 switch (nBegin) {
82 case 2:
83 if (gpTraceFile != NULL && gpTraceFile != stderr) fclose(gpTraceFile);
84 gpTraceFile = stderr;
85 break;
86 case 1:
87 if (gpTraceFile != NULL) fclose(gpTraceFile);
88 gpTraceFile = fopen(gszTraceFile, "w");
89 if (gpTraceFile == NULL) {
90 fprintf(stderr, "Cannot open '%s' for write.\n", gszTraceFile);
91 } else {
92 fprintf(stderr, "Set gnAllocTrigger for alloc().\n");
93 fprintf(stderr, "Set gnFreeTrigger for free().\n");
94 }
95 break;
96 case 0:
97 if (gpTraceFile != NULL) {
98 if (gpTraceFile != stderr) fclose(gpTraceFile);
99 gpTraceFile = NULL;
100 }
101 break;
102 }
103 gnAllocCount = gnFreeCount = 0;
104 gnCreateGCCount = gnFreeGCCount = 0;
105 gnCreateImageCount = gnDestroyImageCount = 0;
106 gnCreateCursorCount = gnFreeCursorCount = 0;
107 gnCreatePixmapCount = gnFreePixmapCount = 0;
108 gnCreateStringCount = gnFreeStringCount = 0;
109 }
110
tgif_dbg_dump_count(msg)111 void tgif_dbg_dump_count(msg)
112 char *msg;
113 {
114 if (gpTraceFile != NULL) {
115 fprintf(gpTraceFile,
116 "%% %s Next gnAllocCount is %1d and next gnFreeCount is %1d\n",
117 msg, gnAllocCount+1, gnFreeCount+1);
118 }
119 }
120
121 /* --------------- malloc, realloc, free --------------- */
122
123 int gnAllocTrigger=0, gnFreeTrigger=0;
124
tgif_malloc(nSize)125 void *tgif_malloc(nSize)
126 size_t nSize;
127 {
128 char *pVoid=malloc(nSize);
129
130 if (gpTraceFile != NULL) {
131 if (++gnAllocCount == gnAllocTrigger) {
132 got_trigger(1);
133 }
134 fprintf(gpTraceFile, "a: %6d 0x%08x %d\n",
135 gnAllocCount, (unsigned int)pVoid, (unsigned int)(nSize));
136 }
137 return (void*)pVoid;
138 }
139
tgif_realloc(pVoid,nSize)140 void *tgif_realloc(pVoid, nSize)
141 void *pVoid;
142 size_t nSize;
143 {
144 char *pszVoid;
145
146 if (gpTraceFile != NULL) {
147 if (++gnFreeCount == gnFreeTrigger) {
148 got_trigger(1);
149 }
150 fprintf(gpTraceFile, "f: %6d 0x%08x\n",
151 gnFreeCount, (unsigned int)pVoid);
152 }
153 pszVoid = realloc(pVoid, nSize);
154 if (gpTraceFile != NULL) {
155 if (++gnAllocCount == gnAllocTrigger) {
156 got_trigger(1);
157 }
158 fprintf(gpTraceFile, "a: %6d 0x%08x %d\n",
159 gnAllocCount, (unsigned int)pszVoid, (unsigned int)(nSize));
160 }
161 return (void*)pszVoid;
162 }
163
tgif_free(pVoid)164 void tgif_free(pVoid)
165 void *pVoid;
166 {
167 if (gpTraceFile != NULL) {
168 if (++gnFreeCount == gnFreeTrigger) {
169 got_trigger(1);
170 }
171 fprintf(gpTraceFile, "f: %6d 0x%08x\n",
172 gnFreeCount, (unsigned int)pVoid);
173 }
174 if (pVoid == NULL) {
175 /* debug, do not translate */
176 fprintf(stderr, "ERROR: free(NULL) called!\n");
177 return;
178 }
179 free(pVoid);
180 }
181
tgif_strlen(psz)182 size_t tgif_strlen(psz)
183 char *psz;
184 {
185 if (psz == NULL) {
186 /* debug, do not translate */
187 fprintf(stderr, "ERROR: strlen(NULL) called!\n");
188 }
189 return strlen(psz);
190 }
191
192 /* --------------- XCreateGC, XFreeGC --------------- */
193
194 int gnCreateGCTrigger=0, gnFreeGCTrigger=0;
195
Tgif_XCreateGC(display,d,valuemask,values)196 GC Tgif_XCreateGC(display, d, valuemask, values)
197 Display *display;
198 Drawable d;
199 unsigned long valuemask;
200 XGCValues *values;
201 {
202 GC gc=XCreateGC(display, d, valuemask, values);
203
204 if (gpTraceFile != NULL) {
205 if (++gnCreateGCCount == gnCreateGCTrigger) {
206 got_trigger(1);
207 }
208 fprintf(gpTraceFile, "g: %6d 0x%08x %ld\n",
209 gnCreateGCCount, (unsigned int)gc, (long)valuemask);
210 }
211 return gc;
212 }
213
Tgif_XFreeGC(display,gc)214 void Tgif_XFreeGC(display, gc)
215 Display *display;
216 GC gc;
217 {
218 if (gpTraceFile != NULL) {
219 if (++gnFreeGCCount == gnFreeGCTrigger) {
220 got_trigger(1);
221 }
222 fprintf(gpTraceFile, "h: %6d 0x%08x\n",
223 gnFreeGCCount, (unsigned int)gc);
224 }
225 XFreeGC(display, gc);
226 }
227
228 /* --------------- XCreateImage, XDestroyImage --------------- */
229
230 int gnCreateImageTrigger=0, gnDestroyImageTrigger=0;
231
Tgif_XCreateImage(display,visual,depth,format,offset,data,width,height,bitmap_pad,bytes_per_line)232 XImage *Tgif_XCreateImage(display, visual, depth, format, offset, data,
233 width, height, bitmap_pad, bytes_per_line)
234 Display *display;
235 Visual *visual;
236 unsigned int depth;
237 int format;
238 int offset;
239 char *data;
240 unsigned int width;
241 unsigned int height;
242 int bitmap_pad;
243 int bytes_per_line;
244 {
245 XImage *ximage=XCreateImage(display, visual, depth, format, offset, data,
246 width, height, bitmap_pad, bytes_per_line);
247
248 if (gpTraceFile != NULL) {
249 if (++gnCreateImageCount == gnCreateImageTrigger) {
250 got_trigger(1);
251 }
252 fprintf(gpTraceFile, "i: %6d 0x%08x %d\n",
253 gnCreateImageCount, (unsigned int)ximage,
254 (unsigned int)(width*height));
255 }
256 return ximage;
257 }
258
Tgif_XGetImage(display,d,x,y,width,height,plane_mask,format)259 XImage *Tgif_XGetImage(display, d, x, y, width, height, plane_mask, format)
260 Display *display;
261 Drawable d;
262 int x;
263 int y;
264 unsigned int width;
265 unsigned int height;
266 unsigned long plane_mask;
267 int format;
268 {
269 XImage *ximage=XGetImage(display, d, x, y, width, height, plane_mask,
270 format);
271
272 if (gpTraceFile != NULL) {
273 if (++gnCreateImageCount == gnCreateImageTrigger) {
274 got_trigger(1);
275 }
276 fprintf(gpTraceFile, "i: %6d 0x%08x %d\n",
277 gnCreateImageCount, (unsigned int)ximage,
278 (unsigned int)(width*height));
279 }
280 return ximage;
281 }
282
Tgif_XDestroyImage(ximage)283 void Tgif_XDestroyImage(ximage)
284 XImage *ximage;
285 {
286 if (gpTraceFile != NULL) {
287 if (++gnDestroyImageCount == gnDestroyImageTrigger) {
288 got_trigger(1);
289 }
290 fprintf(gpTraceFile, "j: %6d 0x%08x\n",
291 gnDestroyImageCount, (unsigned int)ximage);
292 }
293 (*(ximage->f.destroy_image))(ximage);
294 }
295
296 /* --------------- XCreateFontCursor, XFreeGC --------------- */
297
298 int gnCreateCursorTrigger=0, gnFreeCursorTrigger=0;
299
Tgif_XCreateFontCursor(display,shape)300 Cursor Tgif_XCreateFontCursor(display, shape)
301 Display *display;
302 unsigned int shape;
303 {
304 Cursor cursor=XCreateFontCursor(display, shape);
305
306 if (gpTraceFile != NULL) {
307 if (++gnCreateCursorCount == gnCreateCursorTrigger) {
308 got_trigger(1);
309 }
310 fprintf(gpTraceFile, "c: %6d 0x%08x %d\n",
311 gnCreateCursorCount, (unsigned int)cursor, shape);
312 }
313 return cursor;
314 }
315
Tgif_XCreatePixmapCursor(display,source,mask,foreground_color,background_color,x,y)316 Cursor Tgif_XCreatePixmapCursor(display, source, mask, foreground_color,
317 background_color, x, y)
318 Display *display;
319 Pixmap source;
320 Pixmap mask;
321 XColor *foreground_color;
322 XColor *background_color;
323 unsigned int x;
324 unsigned int y;
325 {
326 Cursor cursor=XCreatePixmapCursor(display, source, mask, foreground_color,
327 background_color, x, y);
328
329 if (gpTraceFile != NULL) {
330 if (++gnCreateCursorCount == gnCreateCursorTrigger) {
331 got_trigger(1);
332 }
333 fprintf(gpTraceFile, "c: %6d 0x%08x %d\n",
334 gnCreateCursorCount, (unsigned int)cursor, (unsigned int)source);
335 }
336 return cursor;
337 }
338
Tgif_XFreeCursor(display,cursor)339 void Tgif_XFreeCursor(display, cursor)
340 Display *display;
341 Cursor cursor;
342 {
343 if (gpTraceFile != NULL) {
344 if (++gnFreeCursorCount == gnFreeCursorTrigger) {
345 got_trigger(1);
346 }
347 fprintf(gpTraceFile, "d: %6d 0x%08x\n",
348 gnFreeCursorCount, (unsigned int)cursor);
349 }
350 XFreeCursor(display, cursor);
351 }
352
353 /* --------------- XCreatePixmap, XFreeGC --------------- */
354
355 int gnCreatePixmapTrigger=0, gnFreePixmapTrigger=0;
356
Tgif_XCreatePixmap(display,d,width,height,depth)357 Pixmap Tgif_XCreatePixmap(display, d, width, height, depth)
358 Display *display;
359 Drawable d;
360 unsigned int width;
361 unsigned int height;
362 unsigned int depth;
363 {
364 Pixmap pixmap=XCreatePixmap(display, d, width, height, depth);
365
366 if (gpTraceFile != NULL) {
367 if (++gnCreatePixmapCount == gnCreatePixmapTrigger) {
368 got_trigger(1);
369 }
370 fprintf(gpTraceFile, "p: %6d 0x%08x %d\n",
371 gnCreatePixmapCount, (unsigned int)pixmap, width*height*(depth>>3));
372 }
373 return pixmap;
374 }
375
Tgif_XCreateBitmapFromData(display,d,data,width,height)376 Pixmap Tgif_XCreateBitmapFromData(display, d, data, width, height)
377 Display *display;
378 Drawable d;
379 _Xconst char *data;
380 unsigned int width;
381 unsigned int height;
382 {
383 Pixmap pixmap=XCreateBitmapFromData(display, d, data, width, height);
384
385 if (gpTraceFile != NULL) {
386 if (++gnCreatePixmapCount == gnCreatePixmapTrigger) {
387 got_trigger(1);
388 }
389 fprintf(gpTraceFile, "p: %6d 0x%08x %d\n",
390 gnCreatePixmapCount, (unsigned int)pixmap, width*height);
391 }
392 return pixmap;
393 }
394
Tgif_XReadBitmapFile(display,d,filename,width_return,height_return,bitmap_return,x_hot_return,y_hot_return)395 int Tgif_XReadBitmapFile(display, d, filename, width_return, height_return,
396 bitmap_return, x_hot_return, y_hot_return)
397 Display *display;
398 Drawable d;
399 _Xconst char *filename;
400 unsigned int *width_return;
401 unsigned int *height_return;
402 Pixmap *bitmap_return;
403 int *x_hot_return, *y_hot_return;
404 {
405 int rc=XReadBitmapFile(display, d, filename, width_return, height_return,
406 bitmap_return, x_hot_return, y_hot_return);
407
408 if (gpTraceFile != NULL) {
409 if (++gnCreatePixmapCount == gnCreatePixmapTrigger) {
410 got_trigger(1);
411 }
412 fprintf(gpTraceFile, "p: %6d 0x%08x %d\n",
413 gnCreatePixmapCount,
414 (bitmap_return==NULL || rc != BitmapSuccess) ? 0 :
415 (unsigned int)(*bitmap_return),
416 (width_return==NULL || height_return==NULL) ? 0 :
417 (int)((*width_return)*(*height_return)));
418 }
419 return rc;
420 }
421
Tgif_XFreePixmap(display,pixmap)422 void Tgif_XFreePixmap(display, pixmap)
423 Display *display;
424 Pixmap pixmap;
425 {
426 if (gpTraceFile != NULL) {
427 if (++gnFreePixmapCount == gnFreePixmapTrigger) {
428 got_trigger(1);
429 }
430 fprintf(gpTraceFile, "q: %6d 0x%08x\n",
431 gnFreePixmapCount, (unsigned int)pixmap);
432 }
433 XFreePixmap(display, pixmap);
434 }
435
436 /* ----------- XFetchBytes, XQueryTree, XGetAtomName, XFreeString ----------- */
437
438 int gnCreateStringTrigger=0, gnFreeStringTrigger=0;
439
Tgif_XFetchBytes(display,nbytes_return)440 char *Tgif_XFetchBytes(display, nbytes_return)
441 Display *display;
442 int *nbytes_return;
443 {
444 char *pszString=XFetchBytes(display, nbytes_return);
445
446 if (gpTraceFile != NULL) {
447 if (++gnCreateStringCount == gnCreateStringTrigger) {
448 got_trigger(1);
449 }
450 fprintf(gpTraceFile, "s: %6d 0x%08x %d\n",
451 gnCreateStringCount, (unsigned int)pszString,
452 nbytes_return == NULL ? 0 : (*nbytes_return));
453 }
454 return pszString;
455 }
456
Tgif_XQueryTree(display,w,root_return,parent_return,children_return,nchildren_return)457 Status Tgif_XQueryTree(display, w, root_return, parent_return, children_return,
458 nchildren_return)
459 Display *display;
460 Window w;
461 Window *root_return;
462 Window *parent_return;
463 Window **children_return;
464 unsigned int *nchildren_return;
465 {
466 Status status=XQueryTree(display, w, root_return, parent_return,
467 children_return, nchildren_return);
468
469 if (gpTraceFile != NULL && children_return != NULL &&
470 (*children_return) != NULL) {
471 if (++gnCreateStringCount == gnCreateStringTrigger) {
472 got_trigger(1);
473 }
474 fprintf(gpTraceFile, "s: %6d 0x%08x %d\n",
475 gnCreateStringCount, (unsigned int)(*children_return),
476 (unsigned int)w);
477 }
478 return status;
479 }
480
Tgif_XGetAtomName(display,atom)481 char *Tgif_XGetAtomName(display, atom)
482 Display *display;
483 Atom atom;
484 {
485 char *pszString=XGetAtomName(display, atom);
486
487 if (gpTraceFile != NULL) {
488 if (++gnCreateStringCount == gnCreateStringTrigger) {
489 got_trigger(1);
490 }
491 fprintf(gpTraceFile, "s: %6d 0x%08x %d\n",
492 gnCreateStringCount, (unsigned int)pszString, (unsigned int)atom);
493 }
494 return pszString;
495 }
496
Tgif_XListInstalledColormaps(display,w,num_return)497 Colormap *Tgif_XListInstalledColormaps(display, w, num_return)
498 Display *display;
499 Window w;
500 int *num_return;
501 {
502 Colormap *pColormap=XListInstalledColormaps(display, w, num_return);
503
504 if (gpTraceFile != NULL) {
505 if (++gnCreateStringCount == gnCreateStringTrigger) {
506 got_trigger(1);
507 }
508 fprintf(gpTraceFile, "s: %6d 0x%08x %d\n",
509 gnCreateStringCount, (unsigned int)pColormap,
510 (unsigned int)sizeof(Colormap*));
511 }
512 return pColormap;
513 }
514
Tgif_XFree(pVoid)515 void Tgif_XFree(pVoid)
516 void *pVoid;
517 {
518 if (gpTraceFile != NULL) {
519 if (++gnFreeStringCount == gnFreeStringTrigger) {
520 got_trigger(1);
521 }
522 fprintf(gpTraceFile, "t: %6d 0x%08x\n",
523 gnFreeStringCount, (unsigned int)pVoid);
524 }
525 XFree(pVoid);
526 }
527
528 #endif /* _TGIF_DBG */
529
530