File: debug.cpp

package info (click to toggle)
crystalspace 0.94-20020412-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 62,276 kB
  • ctags: 52,843
  • sloc: cpp: 274,783; ansic: 6,608; perl: 6,276; objc: 3,952; asm: 2,942; python: 2,354; php: 542; pascal: 530; sh: 430; makefile: 370; awk: 193
file content (728 lines) | stat: -rw-r--r-- 18,367 bytes parent folder | download
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/*
    Debugging tools.
    Copyright (C) 2001 by Jorrit Tyberghein

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "cssysdef.h"
#include "csutil/scf.h"
#include "csutil/util.h"
#include "csutil/debug.h"
#include "csutil/scf.h"
#include "iutil/objreg.h"

//-----------------------------------------------------------------------------

struct csDGEL;

// A link element (parent->child or child->parent)
struct csDGELLinkEl
{
  csDGEL* link;		// Child or parent linked too.
  uint32 timestamp;	// Time of creation of link.
};

struct csDGEL
{
  void* object;		// Pointer to the object.
  uint32 timestamp;	// Timestamp of last allocation.
  uint8 scf;		// If true 'object' is an iBase.
  uint8 used;		// If true the object is currently allocated.
  uint8 marker;		// To see what we already dumped.
  uint8 recurse_marker;	// To see what we're dumping in this recursion.
  uint16 num_parents;
  uint16 num_children;
  csDGELLinkEl* parents;
  csDGELLinkEl* children;
  char* description;
  char* type;
  char* file;
  int linenr;

  csDGEL ()
  {
    object = NULL;
    scf = false;
    used = false;
    timestamp = 0;
    description = NULL;
    type = NULL;
    file = NULL;
    num_parents = 0;
    parents = NULL;
    num_children = 0;
    children = NULL;
  }
  void Clear ()
  {
    delete[] description; description = NULL;
    delete[] type; type = NULL;
    delete[] parents; parents = NULL; num_parents = 0;
    delete[] children; children = NULL; num_children = 0;
    file = NULL;
  }
  ~csDGEL ()
  {
    Clear ();
  }

  void AddChild (csDGEL* child, uint32 timestamp)
  {
    if (!children)
    {
      children = new csDGELLinkEl[1];
    }
    else
    {
      csDGELLinkEl* new_children = new csDGELLinkEl[num_children+1];
      memcpy (new_children, children, sizeof (csDGELLinkEl)*num_children);
      delete[] children; children = new_children;
    }
    children[num_children].link = child;
    children[num_children++].timestamp = timestamp;
  }
  void RemoveChild (csDGEL* child)
  {
    if (!children) return;
    if (num_children == 1)
    {
      if (child == children[0].link)
      {
        delete[] children; children = NULL;
	num_children = 0;
      }
      return;
    }
    int i, j = 0;
    for (i = 0 ; i < num_children ; i++)
    {
      if (child != children[i].link) children[j++] = children[i];
    }
    num_children = j;
  }
  void AddParent (csDGEL* parent, uint32 timestamp)
  {
    if (!parents)
    {
      parents = new csDGELLinkEl[1];
    }
    else
    {
      csDGELLinkEl* new_parents = new csDGELLinkEl[num_parents+1];
      memcpy (new_parents, parents, sizeof (csDGELLinkEl)*num_parents);
      delete[] parents; parents = new_parents;
    }
    parents[num_parents].link = parent;
    parents[num_parents++].timestamp = timestamp;
  }
  void RemoveParent (csDGEL* parent)
  {
    if (!parents) return;
    if (num_parents == 1)
    {
      if (parent == parents[0].link)
      {
        delete[] parents; parents = NULL;
	num_parents = 0;
      }
      return;
    }
    int i, j = 0;
    for (i = 0 ; i < num_parents ; i++)
    {
      if (parent != parents[i].link) parents[j++] = parents[i];
    }
    num_parents = j;
  }
};

class csDebugGraph : public iBase
{
public:
  int num_els;
  int max_els;
  csDGEL** els;
  uint32 last_timestamp;

  csDebugGraph ()
  {
    SCF_CONSTRUCT_IBASE (NULL);
    num_els = 0;
    max_els = 100;
    els = new csDGEL* [max_els];
    last_timestamp = 1;
  }
  virtual ~csDebugGraph ()
  {
    Clear ();
    delete[] els;
  }
  void Clear ()
  {
    int i;
    for (i = 0 ; i < num_els ; i++)
    {
      delete els[i];
    }
    delete[] els;
    num_els = 0;
    max_els = 100;
    els = new csDGEL* [max_els];
    last_timestamp = 1;
  }

  csDGEL* AddEl (void* object)
  {
    if (num_els >= max_els)
    {
      max_els += 100;
      csDGEL** new_els = new csDGEL* [max_els];
      memcpy (new_els, els, sizeof (csDGEL*) * num_els);
      delete[] els;
      els = new_els;
    }

    csDGEL* el = new csDGEL ();
    els[num_els++] = el;
    el->used = false;
    el->object = object;
    return el;
  }

  csDGEL* FindEl (void* object)
  {
    int i;
    for (i = 0 ; i < num_els ; i++)
    {
      if (els[i]->object == object) return els[i];
    }
    return NULL;
  }

  SCF_DECLARE_IBASE;
};

SCF_IMPLEMENT_IBASE (csDebugGraph)
SCF_IMPLEMENT_IBASE_END

static csDebugGraph* SetupDebugGraph (iObjectRegistry* object_reg)
{
  iBase* idg = CS_QUERY_REGISTRY_TAG (object_reg, "__Debug_Graph__");
  if (!idg)
  {
    idg = new csDebugGraph ();
    if (!object_reg->Register (idg, "__Debug_Graph__"))
    {
      // If registering fails this probably means we are in the destruction
      // pass and the object registry doesn't allow new updates anymore.
      idg->DecRef ();
      return NULL;
    }
  }
  idg->DecRef ();
  return (csDebugGraph*)idg;
}

//-----------------------------------------------------------------------------

void csDebuggingGraph::SetupGraph (iObjectRegistry* object_reg)
{
  if (!SetupDebugGraph (object_reg)) return;
#ifdef CS_DEBUG
  iSCF::SCF->object_reg = object_reg;
#endif
}

void csDebuggingGraph::AddObject (iObjectRegistry* object_reg,
	void* object, bool scf, char* file, int linenr,
  	char* description, ...)
{
#ifdef CS_DEBUG
  if (!object_reg) object_reg = iSCF::SCF->object_reg;
#endif
  if (!object_reg) return;
  csDebugGraph* dg = SetupDebugGraph (object_reg);
  if (!dg) return;

  csDGEL* el = dg->FindEl (object);
  if (el)
  {
    // The element is already there. This either means that
    // the object was freed first and now a new object happens
    // to be allocated on the same position (this is a valid
    // situation), or else it means that the object is allocated
    // twice! This is not a valid situation because it means
    // that DG_ADD or DG_ADDI is used with a missing DG_REM
    // in between.
    if (el->used)
    {
      printf ("ERROR! Object is added twice to the debug graph!\n");
      printf ("%p %s", el->object, el->description);
      fflush (stdout);
      CS_ASSERT (false);
      return;
    }

    // Reinitialize the element. We will also clear the list of
    // parents and children here since this is a new element and the
    // previous lists are certainly invalid. Note that it is possible
    // that other elements still point to this element from a previous
    // incarnation. That case can be detected with the timestamp: timestamp
    // of this creation will be bigger than the timestamp of the creation
    // of the link to this item. The Dump will show this anomaly.
    el->Clear ();
  }
  else
  {
    // We have a new element.
    el = dg->AddEl (object);
  }

  el->used = true;
  el->timestamp = dg->last_timestamp++;
  el->scf = scf;

  if (description)
  {
    char buf[1000];
    va_list arg;
    va_start (arg, description);
    vsprintf (buf, description, arg);
    va_end (arg);
    el->description = csStrNew (buf);
  }
  else el->description = NULL;

  el->file = file;
  el->linenr = linenr;
}

void csDebuggingGraph::AttachDescription (iObjectRegistry* object_reg,
  	void* object, char* description, ...)
{
#ifdef CS_DEBUG
  if (!object_reg) object_reg = iSCF::SCF->object_reg;
#endif
  if (!object_reg) return;
  csDebugGraph* dg = SetupDebugGraph (object_reg);
  if (!dg) return;

  csDGEL* el = dg->FindEl (object);
  if (el == NULL)
  {
    printf ("ERROR! Cannot find object %p to add description:\n'", object);
    va_list arg;
    va_start (arg, description);
    vprintf (description, arg);
    va_end (arg);
    printf ("'\n");
    fflush (stdout);
    CS_ASSERT (false);
    return;
  }

  delete[] el->description;
  if (description)
  {
    char buf[1000];
    va_list arg;
    va_start (arg, description);
    vsprintf (buf, description, arg);
    va_end (arg);
    el->description = csStrNew (buf);
  }
  else el->description = NULL;
}

void csDebuggingGraph::AttachType (iObjectRegistry* object_reg,
  	void* object, char* type)
{
#ifdef CS_DEBUG
  if (!object_reg) object_reg = iSCF::SCF->object_reg;
#endif
  if (!object_reg) return;
  csDebugGraph* dg = SetupDebugGraph (object_reg);
  if (!dg) return;

  csDGEL* el = dg->FindEl (object);
  if (el == NULL)
  {
    printf ("ERROR! Cannot find object %p to add type '%s'\n", object, type);
    fflush (stdout);
    CS_ASSERT (false);
    return;
  }

  delete[] el->type;
  if (type)
    el->type = csStrNew (type);
  else el->type = NULL;
}

void csDebuggingGraph::RemoveObject (iObjectRegistry* object_reg,
	void* object, char* file, int linenr)
{
#ifdef CS_DEBUG
  if (!object_reg) object_reg = iSCF::SCF->object_reg;
#endif
  if (!object_reg) return;
  (void)file;
  (void)linenr;
  csDebugGraph* dg = SetupDebugGraph (object_reg);
  if (!dg) return;

  csDGEL* el = dg->FindEl (object);
  if (!el)
  {
    printf ("ERROR! Cannot find element for object %p!\n", object);
    fflush (stdout);
    CS_ASSERT (false);
    return;
  }
  if (!el->used)
  {
    printf ("ERROR! Element for object %p is not allocated!\n", object);
    fflush (stdout);
    CS_ASSERT (false);
    return;
  }

  el->used = false;
}

void csDebuggingGraph::AddParent (iObjectRegistry* object_reg,
	void* child, void* parent)
{
#ifdef CS_DEBUG
  if (!object_reg) object_reg = iSCF::SCF->object_reg;
#endif
  if (!object_reg) return;
  csDebugGraph* dg = SetupDebugGraph (object_reg);
  if (!dg) return;
  csDGEL* p_el = dg->FindEl (parent);
  // If parent could not be found. Create a dummy place holder for later.
  if (!p_el) p_el = dg->AddEl (parent);
  csDGEL* c_el = dg->FindEl (child);
  // If child could not be found. Create a dummy place holder for later.
  if (!c_el) c_el = dg->AddEl (child);

  c_el->AddParent (p_el, dg->last_timestamp++);
}

void csDebuggingGraph::AddChild (iObjectRegistry* object_reg,
	void* parent, void* child)
{
#ifdef CS_DEBUG
  if (!object_reg) object_reg = iSCF::SCF->object_reg;
#endif
  if (!object_reg) return;
  csDebugGraph* dg = SetupDebugGraph (object_reg);
  if (!dg) return;
  csDGEL* p_el = dg->FindEl (parent);
  // If parent could not be found. Create a dummy place holder for later.
  if (!p_el) p_el = dg->AddEl (parent);
  csDGEL* c_el = dg->FindEl (child);
  // If child could not be found. Create a dummy place holder for later.
  if (!c_el) c_el = dg->AddEl (child);

  p_el->AddChild (c_el, dg->last_timestamp++);
}

void csDebuggingGraph::RemoveParent (iObjectRegistry* object_reg,
	void* child, void* parent)
{
#ifdef CS_DEBUG
  if (!object_reg) object_reg = iSCF::SCF->object_reg;
#endif
  if (!object_reg) return;
  csDebugGraph* dg = SetupDebugGraph (object_reg);
  if (!dg) return;
  csDGEL* c_el = dg->FindEl (child);
  if (!c_el) return;	// Nothing to do if child is not there.
  csDGEL* p_el = dg->FindEl (parent);
  if (!p_el) return;	// Nothing to do if parent doesn't exist either.

  c_el->RemoveParent (p_el);
}

void csDebuggingGraph::RemoveChild (iObjectRegistry* object_reg,
	void* parent, void* child)
{
#ifdef CS_DEBUG
  if (!object_reg) object_reg = iSCF::SCF->object_reg;
#endif
  if (!object_reg) return;
  csDebugGraph* dg = SetupDebugGraph (object_reg);
  if (!dg) return;
  csDGEL* p_el = dg->FindEl (parent);
  if (!p_el) return;	// Nothing to do.
  csDGEL* c_el = dg->FindEl (child);
  if (!c_el) return;	// Nothing to do.

  p_el->RemoveChild (c_el);
}

void csDebuggingGraph::Clear (iObjectRegistry* object_reg)
{
#ifdef CS_DEBUG
  if (!object_reg) object_reg = iSCF::SCF->object_reg;
#endif
  if (!object_reg) return;
  csDebugGraph* dg = SetupDebugGraph (object_reg);
  if (!dg) return;
  dg->Clear ();
}

void csDebuggingGraph::Dump (iObjectRegistry* object_reg)
{
#ifdef CS_DEBUG
  if (!object_reg) object_reg = iSCF::SCF->object_reg;
#endif
  if (!object_reg) return;
  csDebugGraph* dg = SetupDebugGraph (object_reg);
  if (!dg) return;

  csDGEL** els = dg->els;
  // First mark all elements as unused and count the number
  // of elements we have.
  int i, cnt = 0;
  for (i = 0 ; i < dg->num_els ; i++)
  {
    if (els[i]->used)
    {
      cnt++;
      els[i]->marker = false;
    }
    else
      els[i]->marker = true;
    els[i]->recurse_marker = false;
  }

  printf ("====================================================\n");
  printf ("Total number of used objects in graph: %d\n", cnt);

  // Find the first unmarked object and dump it.
  i = 0;
  while (i < dg->num_els)
  {
    if (!els[i]->marker)
    {
      Dump (object_reg, els[i]->object, false);
      i = 0;	// Restart scan.
      printf ("----------------------------------------------------\n");
    }
    else i++;
  }
  fflush (stdout);
}

static void DumpSubTree (int indent, const char* type, uint32 link_timestamp,
	csDGEL* el)
{
  // link_timestamp is the timestamp when the link was created.

  char spaces[1000];
  int ind = indent;
  if (ind > 999) ind = 999;
  char* sp = spaces;
  while (ind >= 10)
  {
    strcpy (sp, "          ");
    sp += 10;
    ind -= 10;
  }
  while (ind >= 1)
  {
    *sp++ = ' ';
    ind--;
  }
  *sp = 0;

  if (el->recurse_marker && *type == 'P')
  {
    // We already encountered this object in this recursion. So we just
    // put a short-hand here.
    printf ("%s%s(%lu) %p <-\n", spaces, type, link_timestamp, el->object);
    return;
  }

  // Show the ref count if it is an scf interface. If the object
  // is no longer used then show '?' instead of ref count to avoid
  // calling an invalid pointer.
  printf ("%s%s(%lu) %p(", spaces, type, link_timestamp, el->object);
  if (el->scf)
  {
    if (el->used)
      printf ("r%d,", ((iBase*)(el->object))->GetRefCount ());
    else
      printf ("r?,-");
  }
  else if (!el->used)
    printf ("-");
  if (el->type)
    printf ("t%lu) %s(%s)", el->timestamp, el->type, el->description);
  else
    printf ("t%lu) %s", el->timestamp, el->description);

  // If the object is used but the link to this object was created
  // BEFORE the object (i.e. timestamps) then this is at least very
  // suspicious and is also marked as such.
  if (el->used && link_timestamp > 0 && link_timestamp < el->timestamp)
  {
    printf (" (SUSPICIOUS!)");
  }

  if (el->marker || *type == 'P' || !el->used)
  {
    if (el->used)
    {
      if (el->marker)
        printf (" (REF)\n");
      else
        printf ("\n");
    }
    else
      printf (" (BAD LINK!)\n");

    if (*type != 'P') el->marker = true;
  }
  else
  {
    el->marker = true;
    printf (" (%s,%d) #p=%d #c=%d\n",
    	el->file, el->linenr, el->num_parents, el->num_children);
    if (el->num_parents + el->num_children > 0)
    {
      bool use_brackets = true;
      if (el->num_children == 0 && el->num_parents == 1)
      {
        // If we have only one parent and no children we check
	// if we already visited that parent in this recursion.
	// In that case we don't print the brackets.
	if (el->parents[0].link->recurse_marker) use_brackets = false;
      }

      if (use_brackets) printf ("%s{\n", spaces);
      el->recurse_marker = true;
      int i;
      for (i = 0 ; i < el->num_parents ; i++)
      {
        DumpSubTree (indent+2, "P", el->parents[i].timestamp,
		el->parents[i].link);
      }
      for (i = 0 ; i < el->num_children ; i++)
      {
        DumpSubTree (indent+2, "C", el->children[i].timestamp,
		el->children[i].link);
      }
      el->recurse_marker = false;
      if (use_brackets) printf ("%s}\n", spaces);
    }
  }
  fflush (stdout);
}

static int compare_el (const void* vel1, const void* vel2)
{
  csDGEL* el1 = *(csDGEL**)vel1;
  csDGEL* el2 = *(csDGEL**)vel2;
  if (el1->num_parents < el2->num_parents) return -1;
  else if (el1->num_parents > el2->num_parents) return 1;
  else return 0;
}

void csDebuggingGraph::Dump (iObjectRegistry* object_reg, void* object,
	bool reset_mark)
{
#ifdef CS_DEBUG
  if (!object_reg) object_reg = iSCF::SCF->object_reg;
#endif
  if (!object_reg) return;
  csDebugGraph* dg = SetupDebugGraph (object_reg);
  if (!dg) return;
  int i;
  if (reset_mark)
  {
    // First mark all elements as unused.
    csDGEL** els = dg->els;
    for (i = 0 ; i < dg->num_els ; i++)
    {
      if (els[i]->used) els[i]->marker = false;
      else els[i]->marker = true;
      els[i]->recurse_marker = false;
    }
  }

  csDGEL* el = dg->FindEl (object);
  CS_ASSERT (el != NULL);

  // First copy all elements that belong to this sub-graph
  // to a local array.
  csDGEL** local_els = new csDGEL* [dg->num_els];
  int done = 0, num = 0;
  local_els[num++] = el; el->marker = true;
  while (done < num)
  {
    csDGEL* lel = local_els[done++];
    if (lel->used)
    {
      for (i = 0 ; i < lel->num_parents ; i++)
      {
        if (!lel->parents[i].link->marker)
        {
          local_els[num++] = lel->parents[i].link;
	  lel->parents[i].link->marker = true;
        }
      }
      for (i = 0 ; i < lel->num_children ; i++)
        if (!lel->children[i].link->marker)
        {
          local_els[num++] = lel->children[i].link;
	  lel->children[i].link->marker = true;
        }
    }
  }

  // Now mark all elements as unused again.
  for (i = 0 ; i < num ; i++)
    local_els[i]->marker = false;

  // Sort all elements based on the number of parents.
  // This means that 'root' like elements will come first in the
  // array.
  qsort (local_els, num, sizeof (csDGEL*), compare_el);

  // Now dump all parents here until all are marked.
  for (i = 0 ; i < num ; i++)
  {
    if (!local_els[i]->used)
    {
      local_els[i]->marker = true;
    }
    else if (!local_els[i]->marker)
    {
      DumpSubTree (0, "R", 0, local_els[i]);
    }
  }

  delete[] local_els;
}

//-----------------------------------------------------------------------------