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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
|
/*
* file debug.c - debugging routines
*
* $Id: debug.c,v 1.37 2006/02/18 21:40:02 fzago Exp $
*
* Program XBLAST
* (C) by Oliver Vogel (e-mail: m.vogel@ndh.net)
*
* 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; or (at your option)
* any later version
*
* This program is distributed in the hope that it will be entertaining,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILTY 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, write to the Free Software Foundation, Inc.
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "xblast.h"
#ifdef DEBUG_ALLOC
#undef malloc
#undef calloc
#undef free
#endif
/*
* local macros
*/
/* size of alloc tracking table */
#define TABLE_SIZE 20000
/*
* local types
*/
/* data structure for tracking allocs */
typedef struct
{
const void *ptr;
size_t bytes;
const char *file;
int line;
} AllocData;
/*
* local variables
*/
#ifdef DEBUG
/* start value for timer */
static struct timeval timeStart;
#endif
#ifdef DEBUG_ALLOC
#define USETABLE
#undef USETABLE
static int count = 0;
static FILE *fout = NULL;
static const char *outFile = NULL;
#ifdef USETABLE
static AllocData *table = NULL;
#endif
static size_t currentAlloc = 0;
static size_t maxAlloc = 0;
#endif
#if defined(DEBUG) || !defined(__GNUC__)
/*
* global function: Dbg_StartClock
* description: start millisec for profiling
*/
void
Dbg_StartClock (void)
{
#ifdef DEBUG
gettimeofday (&timeStart, NULL);
#endif
} /* Dbg_StartClock */
/*
* global function: Dbg_FinishClock
* description: stop millisec for profiling
*/
time_t
Dbg_FinishClock (void)
{
#ifdef DEBUG
struct timeval timeEnd;
gettimeofday (&timeEnd, NULL);
return (timeEnd.tv_sec - timeStart.tv_sec) * 1000 + (timeEnd.tv_usec -
timeStart.tv_usec) / 1000;
#endif
} /* Dbg_FinishClock */
/*
* global function: Dbg_Out
* description: formatted debug output to stderr
* parameters: fmt - format string as in printf
* ... - variable args as in printf
*/
void
Dbg_Out (const char *fmt, ...)
{
#ifdef DEBUG
va_list argList;
#ifdef W32
static int init = 1;
static FILE *fp;
fp = stderr;
if (init) {
fp = fopen ("error.log", "w+");
init = 0;
}
#endif
va_start (argList, fmt);
#ifdef W32
vfprintf (fp, fmt, argList);
#else
vfprintf (stderr, fmt, argList);
#endif
va_end (argList);
#endif
} /* Dbg_Out */
#endif
/*
* debug outputs for certain sections
* for gcc: use of variadic macro to define away, use -std=c99 with -pedantic
* for other: use empty routines which should be optimized away
*/
#if defined(DEBUG_CONFIG) || !defined(__GNUC__)
void
Dbg_Config (const char *fmt, ...)
{
#ifdef DEBUG_CONFIG
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "CONFIG: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_FILE) || !defined(__GNUC__)
void
Dbg_File (const char *fmt, ...)
{
#ifdef DEBUG_FILE
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "FILE: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_LEVEL) || !defined(__GNUC__)
void
Dbg_Level (const char *fmt, ...)
{
#ifdef DEBUG_LEVEL
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "LEVEL: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_GAME) || !defined(__GNUC__)
void
Dbg_Game (const char *fmt, ...)
{
#ifdef DEBUG_GAME
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "GAME: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_ACTION) || !defined(__GNUC__)
void
Dbg_Action (const char *fmt, ...)
{
#ifdef DEBUG_ACTION
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "ACTION: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_NETWORK) || !defined(__GNUC__)
void
Dbg_Network (const char *fmt, ...)
{
#ifdef DEBUG_NETWORK
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "NETWORK: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_SERVER) || !defined(__GNUC__)
void
Dbg_Server (const char *fmt, ...)
{
#ifdef DEBUG_SERVER
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "SERVER: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_CLIENT) || !defined(__GNUC__)
void
Dbg_Client (const char *fmt, ...)
{
#ifdef DEBUG_CLIENT
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "CLIENT: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_USER) || !defined(__GNUC__)
void
Dbg_User (const char *fmt, ...)
{
#ifdef DEBUG_USER
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "USER: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_LISTEN) || !defined(__GNUC__)
void
Dbg_Listen (const char *fmt, ...)
{
#ifdef DEBUG_LISTEN
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "LISTEN: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_STREAM) || !defined(__GNUC__)
void
Dbg_Stream (const char *fmt, ...)
{
#ifdef DEBUG_STREAM
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "STREAM: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_S2C) || !defined(__GNUC__)
void
Dbg_S2C (const char *fmt, ...)
{
#ifdef DEBUG_S2C
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "S2C: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_C2S) || !defined(__GNUC__)
void
Dbg_C2S (const char *fmt, ...)
{
#ifdef DEBUG_C2S
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "C2S: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_X2C) || !defined(__GNUC__)
void
Dbg_X2C (const char *fmt, ...)
{
#ifdef DEBUG_X2C
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "X2C: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_C2X) || !defined(__GNUC__)
void
Dbg_C2X (const char *fmt, ...)
{
#ifdef DEBUG_C2X
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "C2X: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_C2B) || !defined(__GNUC__)
void
Dbg_C2B (const char *fmt, ...)
{
#ifdef DEBUG_C2B
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "C2B: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_DGRAM) || !defined(__GNUC__)
void
Dbg_Dgram (const char *fmt, ...)
{
#ifdef DEBUG_DGRAM
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "DGRAM: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_D2C) || !defined(__GNUC__)
void
Dbg_D2C (const char *fmt, ...)
{
#ifdef DEBUG_D2C
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "D2C: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_D2S) || !defined(__GNUC__)
void
Dbg_D2S (const char *fmt, ...)
{
#ifdef DEBUG_D2S
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "D2S: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_BROWSE) || !defined(__GNUC__)
void
Dbg_Browse (const char *fmt, ...)
{
#ifdef DEBUG_BROWSE
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "BROWSE: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_NEWGAME) || !defined(__GNUC__)
void
Dbg_Newgame (const char *fmt, ...)
{
#ifdef DEBUG_NEWGAME
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "NEWGAME: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_REPLY) || !defined(__GNUC__)
void
Dbg_Reply (const char *fmt, ...)
{
#ifdef DEBUG_REPLY
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "REPLY: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_QUERY) || !defined(__GNUC__)
void
Dbg_Query (const char *fmt, ...)
{
#ifdef DEBUG_QUERY
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "QUERY: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_CENTRAL) || !defined(__GNUC__)
void
Dbg_Central (const char *fmt, ...)
{
#ifdef DEBUG_CENTRAL
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "CENTRAL: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_COMM) || !defined(__GNUC__)
void
Dbg_Comm (const char *fmt, ...)
{
#ifdef DEBUG_COMM
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "COMM: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_SOCKET) || !defined(__GNUC__)
void
Dbg_Socket (const char *fmt, ...)
{
#ifdef DEBUG_SOCKET
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "SOCKET: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_VERSION) || !defined(__GNUC__)
void
Dbg_Version (const char *fmt, ...)
{
#ifdef DEBUG_VERSION
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "VERSION: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#if defined(DEBUG_CHAT) || !defined(__GNUC__)
void
Dbg_Chat (const char *fmt, ...)
{
#ifdef DEBUG_CHAT
va_list argList;
va_start (argList, fmt);
fprintf (stderr, "CHAT: ");
vfprintf (stderr, fmt, argList);
va_end (argList);
#endif
}
#endif
#ifdef DEBUG_ALLOC
/*
*
*/
void
Dbg_FinishAlloc (void)
{
#ifdef USETABLE
int i, j;
size_t sumBytes = 0;
size_t sumAlloc = 0;
size_t sumTotal = 0;
char *ptr;
for (i = 0; i < count; i++) {
if (table[i].ptr != NULL) {
fprintf (fout, "[%d] unfreed memory at 0x%08lX (%s:%d)", i, (unsigned long)table[i].ptr,
table[i].file, table[i].line);
sumAlloc++;
sumBytes += table[i].bytes;
/* output */
fputc ('\"', fout);
for (ptr = table[i].ptr, j = 0; j < table[i].bytes && j < 16; j++, ptr++) {
if (isprint (*ptr)) {
fputc (*ptr, fout);
}
else {
fprintf (fout, "\\%03o", (unsigned)*ptr);
}
}
fputs ("\"\n", fout);
}
sumTotal += table[i].bytes;
}
fprintf (stderr, "%u/%u unfree memory segments with total %u/%u/%u bytes\n", sumAlloc, count,
sumBytes, maxAlloc, sumTotal);
#else
fprintf (stderr, "count=%u current=%lu bytes max=%lu bytes\n", count, (unsigned long)currentAlloc, (unsigned long)maxAlloc);
#endif
} /* Finish */
/*
* replacement for malloc
*/
void *
Dbg_Malloc (const char *file, int line, size_t nBytes)
{
void *result = malloc (nBytes);
#ifdef USETABLE
assert (count < TABLE_SIZE);
if (NULL == table) {
table = calloc (TABLE_SIZE, sizeof (*table));
assert (NULL != table);
}
table[count].ptr = result;
table[count].bytes = nBytes;
table[count].file = file;
table[count].line = line;
#endif
/* statistics */
currentAlloc += nBytes;
if (currentAlloc > maxAlloc) {
maxAlloc = currentAlloc;
}
/* log it */
if (NULL == fout) {
fout = (outFile == NULL) ? stderr : fopen (outFile, "w");
}
fprintf (fout, "[%d] 0x%08lX = malloc (%lu); %s:%d\n", count, (unsigned long)result,(unsigned long)nBytes,
file, line);
count++;
return result;
} /* Malloc */
/*
* replacement for calloc
*/
void *
Dbg_Calloc (const char *file, int line, size_t nElem, size_t sElem)
{
void *result = calloc (nElem, sElem);
#ifdef USETABLE
assert (count < TABLE_SIZE);
if (NULL == table) {
table = calloc (TABLE_SIZE, sizeof (*table));
assert (NULL != table);
}
table[count].ptr = result;
table[count].bytes = nElem * sElem;
table[count].file = file;
table[count].line = line;
#endif
/* statistics */
currentAlloc += nElem * sElem;
if (currentAlloc > maxAlloc) {
maxAlloc = currentAlloc;
}
/* log it */
if (NULL == fout) {
fout = (outFile == NULL) ? stderr : fopen (outFile, "w");
}
/* hook in clean up function */
fprintf (fout, "[%d] 0x%08lX = calloc (%lu,%lu); %s:%d\n", count, (unsigned long)result,
(unsigned long)nElem, (unsigned long)sElem, file, line);
count++;
return result;
} /* Calloc */
/*
* replacement for free
*/
void
Dbg_Free (const char *file, int line, void *ptr)
{
int index = 0;
#ifdef USETABLE
for (index = 0; index < count; index++) {
if (table[index].ptr == ptr) {
break;
}
}
table[index].ptr = NULL;
/* statistics */
currentAlloc -= table[index].bytes;
/* log it */
if (index == count) {
fprintf (stderr, "[?] free (0x%08lX); %s:%d\n", (unsigned long)ptr, file, line);
return;
}
#endif
if (NULL == fout) {
fout = (outFile == NULL) ? stderr : fopen (outFile, "w");
}
fprintf (fout, "[%d] free (0x%08lX); %s:%d\n", index, (unsigned long)ptr, file, line);
free (ptr);
} /* Free */
/*
* replacement for free
*/
void
Dbg_Vfree (const char *file, int line, void *ptr)
{
int index = 0;
#ifdef USETABLE
for (index = 0; index < count; index++) {
if (table[index].ptr == ptr) {
break;
}
}
table[index].ptr = NULL;
/* statistics */
currentAlloc -= table[index].bytes;
/* log it */
if (index == count) {
fprintf (stderr, "[?] unlock (0x%08lX); %s:%d\n", (unsigned long)ptr, file, line);
return;
}
#endif
if (NULL == fout) {
fout = (outFile == NULL) ? stderr : fopen (outFile, "w");
}
fprintf (fout, "[%d] unlock (0x%08lX); %s:%d\n", index, (unsigned long)ptr, file, line);
} /* Free */
#endif
/*
* end of file alloc.
*/
|