File: trace.cc

package info (click to toggle)
aegis 4.24-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 37,640 kB
  • ctags: 12,410
  • sloc: cpp: 178,288; sh: 79,325; makefile: 34,810; yacc: 4,605; perl: 1,499; ansic: 492; awk: 325
file content (608 lines) | stat: -rw-r--r-- 11,189 bytes parent folder | download | duplicates (3)
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
//
//	aegis - project change supervisor
//	Copyright (C) 1991-1995, 1997, 1999, 2002-2006, 2008 Peter Miller
//
//	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 3 of the License, or
//	(at your option) any later version.
//
//	This program 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 General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program. If not, see
//	<http://www.gnu.org/licenses/>.
//

#include <common/ac/ctype.h>
#include <common/ac/stdio.h>
#include <common/ac/string.h>
#include <common/ac/stddef.h>
#include <common/ac/stdarg.h>
#include <common/ac/time.h>

#include <common/error.h>
#include <common/mem.h>
#include <common/progname.h>
#include <common/str.h>
#include <common/nstring.h>
#include <common/trace.h>


#define INDENT 2

struct known_ty
{
    string_ty       *filename;
    int             flag;
    int             *flag_p;
    known_ty        *next;
};

static string_ty *file_name;
static int      line_number;
static int      page_width;
static known_ty	*known;
static int      depth;


static string_ty *
base_name(const char *file)
{
    const char      *cp1;
    const char      *cp2;

    cp1 = strrchr(file, '/');
    if (cp1)
	++cp1;
    else
	cp1 = file;
    cp2 = strrchr(cp1, '.');
    if (!cp2)
	cp2 = cp1 + strlen(cp1);
    if (cp2 > cp1 + 6)
	return str_n_from_c(cp1, 6);
    return str_n_from_c(cp1, cp2 - cp1);
}


int
trace_pretest(const char *file, int *result)
{
    string_ty	    *s;
    known_ty	    *kp;

    s = base_name(file);
    for (kp = known; kp; kp = kp->next)
    {
	if (str_equal(s, kp->filename))
	{
	    str_free(s);
	    break;
	}
    }
    if (!kp)
    {
	kp = (known_ty *)mem_alloc(sizeof(known_ty));
	kp->filename = s;
	kp->next = known;
	kp->flag = 2; // disabled
	known = kp;
    }
    kp->flag_p = result;
    *result = kp->flag;
    return *result;
}


void
trace_where(const char *file, int line)
{
    string_ty	    *s;

    //
    // take new name fist, because will probably be same as last
    // thus saving a free and a malloc (which are slow)
    //
    s = base_name(file);
    if (file_name)
	str_free(file_name);
    file_name = s;
    line_number = line;
}


static void
trace_putchar(int c)
{
    static char     buffer[200];
    static char     *cp;
    static int      in_col;
    static int      out_col;

    if (!page_width)
    {
	// don't use last column, many terminals are dumb
	page_width = 79;
	// allow for progname, filename and line number (8 each)
	page_width -= 24;
	if (page_width < 16)
	    page_width = 16;
    }
    if (!cp)
    {
	cp = strendcpy(buffer, progname_get(), buffer + sizeof(buffer));
	if (cp > buffer + 6)
	    cp = buffer + 6;
	*cp++ = ':';
	*cp++ = '\t';
	cp = strendcpy(cp, file_name->str_text, buffer + sizeof(buffer));
	*cp++ = ':';
	*cp++ = '\t';
	snprintf(cp, buffer + sizeof(buffer) - cp, "%d:\t", line_number);
	cp += strlen(cp);
	in_col = 0;
	out_col = 0;
    }
    switch (c)
    {
    case '\n':
	*cp++ = '\n';
	*cp = 0;
	fflush(stdout);
	fputs(buffer, stderr);
	fflush(stderr);
	if (ferror(stderr))
	    nfatal("(stderr)");
	cp = 0;
	break;

    case ' ':
	if (out_col)
	    ++in_col;
	break;

    case '\t':
	if (out_col)
	    in_col = (in_col/INDENT + 1) * INDENT;
	break;

    case '}':
    case ')':
    case ']':
	if (depth > 0)
	    --depth;
	// fall through

    default:
	if (!out_col)
	{
	    if (c != '#')
	       	// modulo so never too long
	       	in_col = (INDENT * depth) % page_width;
	    else
	       	in_col = 0;
	}
	if (in_col >= page_width)
	{
	    trace_putchar('\n');
	    trace_putchar(c);
	    return;
	}
	while (((out_col + 8) & -8) <= in_col && out_col + 1 < in_col)
	{
	    *cp++ = '\t';
	    out_col = (out_col + 8) & -8;
	}
	while (out_col < in_col)
	{
	    *cp++ = ' ';
	    ++out_col;
	}
	if (c == '{' || c == '(' || c == '[')
	    ++depth;
	*cp++ = c;
	in_col++;
	out_col++;
	break;
    }
}


void
trace_printf(const char *s, ...)
{
    va_list         ap;
    char	    buffer[3000];

    va_start(ap, s);
    vsnprintf(buffer, sizeof(buffer), s, ap);
    va_end(ap);
    for (s = buffer; *s; ++s)
	trace_putchar(*s);
}


const char*
trace_args()
{
    static nstring args;
    if (args.empty())
    {
	for (known_ty *kp = known; kp; kp = kp->next)
	{
	    if (kp->flag != 3)
		continue;
	    if (args.empty())
		args += " -Trace";
	    args += " ";
	    args += kp->filename->str_text;
	}
    }
    return args.c_str();
}


void
trace_enable(const char *file)
{
    string_ty	    *s;
    known_ty	    *kp;

    s = base_name(file);
    for (kp = known; kp; kp = kp->next)
    {
	if (str_equal(s, kp->filename))
	{
	    str_free(s);
	    break;
	}
    }
    if (!kp)
    {
	kp = (known_ty *)mem_alloc(sizeof(known_ty));
	kp->filename = s;
	kp->flag_p = 0;
	kp->next = known;
	known = kp;
    }
    kp->flag = 3; // enabled
    if (kp->flag_p)
	*kp->flag_p = kp->flag;

    //
    // this silences a warning...
    //
#ifdef DEBUG
    trace_pretest_result = 1;
#endif
}


void
trace_bool_real(const char *name, const bool &value)
{
    if (name && *name)
	trace_printf("%s = ", name);
    trace_printf("%s", (value ? "true" : "false"));
    trace_printf(name && *name ? ";\n" : ",\n");
}


void
trace_char_real(const char *name, const char *vp)
{
    trace_printf("%s = '", name);
    if (!isprint((unsigned char)*vp) || strchr("(){}[]", *vp))
    {
	const char *s = strchr("\bb\nn\tt\rr\ff", *vp);
	if (s)
	{
	    trace_putchar('\\');
	    trace_putchar(s[1]);
	}
	else
	    trace_printf("\\%03o", (unsigned char)*vp);
    }
    else
    {
	    if (strchr("'\\", *vp))
		    trace_putchar('\\');
	    trace_putchar(*vp);
    }
    trace_printf("'; /* 0x%02X, %d */\n", (unsigned char)*vp, *vp);
}


void
trace_char_unsigned_real(const char *name, const unsigned char *vp)
{
    trace_printf("%s = '", name);
    if (!isprint((unsigned char)*vp) || strchr("(){}[]", *vp))
    {
	const char *s = strchr("\bb\nn\tt\rr\ff", *vp);
	if (s)
	{
    	    trace_putchar('\\');
    	    trace_putchar(s[1]);
	}
	else
    	    trace_printf("\\%03o", *vp);
    }
    else
    {
	if (strchr("'\\", *vp))
    	    trace_putchar('\\');
	trace_putchar(*vp);
    }
    trace_printf("'; /* 0x%02X, %d */\n", *vp, *vp);
}


void
trace_int_real(const char *name, const int *vp)
{
    trace_printf("%s = %d;\n", name, *vp);
}


void
trace_int_unsigned_real(const char *name, const unsigned int *vp)
{
    trace_printf("%s = %u;\n", name, *vp);
}


void
trace_long_real(const char *name, const long *vp)
{
    if (name && *name)
	trace_printf("%s = ", name);
    trace_printf("%ld", *vp);
    trace_printf(name && *name ? ";\n" : ",\n");
}


void
trace_long_unsigned_real(const char *name, const unsigned long *vp)
{
    trace_printf("%s = %lu;\n", name, *vp);
}


void
trace_pointer_real(const char *name, const void *vptrptr)
{
    const void *ptr;
    const void *const *ptr_ptr = (const void *const *)vptrptr;

    ptr = *ptr_ptr;
    if (!ptr)
	trace_printf("%s = NULL;\n", name);
    else
	trace_printf("%s = 0x%08lX;\n", name, (long)ptr);
}


void
trace_short_real(const char *name, const short *vp)
{
    trace_printf("%s = %hd;\n", name, *vp);
}


void
trace_short_unsigned_real(const char *name, const unsigned short *vp)
{
    trace_printf("%s = %hu;\n", name, *vp);
}


void
trace_string_real(const char *name, const string_ty *value)
{
    trace_string_real(name, (value ? value->str_text : 0));
}


void
trace_string_real(const char *name, const char *vp)
{
    const char	    *s;
    long	    count;

    if (name && *name)
	trace_printf("%s = ", name);
    if (!vp)
    {
	trace_printf("NULL");
    }
    else
    {
	trace_printf("\"");
	count = 0;
	for (s = vp; *s; ++s)
	{
	    switch (*s)
	    {
	    case '(':
	    case '[':
	    case '{':
		++count;
		break;

	    case ')':
	    case ']':
	    case '}':
		--count;
		break;
	    }
	}
	if (count > 0)
	    count = -count;
	else
	    count = 0;
	for (s = vp; *s; ++s)
	{
	    unsigned char c = *s;
	    if (!isprint(c))
	    {
		const char      *cp;

		cp = strchr("\bb\ff\nn\rr\tt", c);
		if (cp)
		    trace_printf("\\%c", cp[1]);
		else
		{
		    escape:
		    trace_printf("\\%03o", c);
		}
	    }
	    else
	    {
		switch (c)
		{
		case '(':
		case '[':
		case '{':
		    ++count;
		    if (count <= 0)
			goto escape;
		    break;

		case ')':
		case ']':
		case '}':
		    --count;
		    if (count < 0)
			goto escape;
		    break;

		case '\\':
		case '"':
		    trace_printf("\\");
		    break;
		}
		trace_printf("%c", c);
	    }
	}
	trace_printf("\"");
    }
    trace_printf(name && *name ? ";\n" : ",\n");
}


void
trace_indent_reset()
{
    depth = 0;
}


const char *
unctrl(int c)
{
    static char     buffer[30];

    if (c < 0 || c >= 256)
	snprintf(buffer, sizeof(buffer), "%d", c);
    else
    {
	int             cc;

	switch (c)
	{
	case '\a':
	    cc = 'a';
	    goto escaped;

	case '\b':
	    cc = 'b';
	    goto escaped;

	case '\f':
	    cc = 'f';
	    goto escaped;

	case '\n':
	    cc = 'n';
	    goto escaped;

	case '\r':
	    cc = 'r';
	    goto escaped;

	case '\t':
	    cc = 't';
	    goto escaped;

	case '\v':
	    cc = 'v';
	    goto escaped;


	case '\'':
	case '\\':
	    cc = c;
	    escaped:
	    snprintf(buffer, sizeof(buffer), "0x%02X '\\%c'", c, cc);
	    break;

	case ' ': case '!': case '"': case '#':
	case '$': case '%': case '&':
	case '(': case ')': case '*': case '+':
	case ',': case '-': case '.': case '/':
	case '0': case '1': case '2': case '3':
	case '4': case '5': case '6': case '7':
	case '8': case '9': case ':': case ';':
	case '<': case '=': case '>': case '?':
	case '@': case 'A': case 'B': case 'C':
	case 'D': case 'E': case 'F': case 'G':
	case 'H': case 'I': case 'J': case 'K':
	case 'L': case 'M': case 'N': case 'O':
	case 'P': case 'Q': case 'R': case 'S':
	case 'T': case 'U': case 'V': case 'W':
	case 'X': case 'Y': case 'Z': case '[':
		  case ']': case '^': case '_':
	case '`': case 'a': case 'b': case 'c':
	case 'd': case 'e': case 'f': case 'g':
	case 'h': case 'i': case 'j': case 'k':
	case 'l': case 'm': case 'n': case 'o':
	case 'p': case 'q': case 'r': case 's':
	case 't': case 'u': case 'v': case 'w':
	case 'x': case 'y': case 'z': case '{':
	case '|': case '}': case '~':
	    snprintf(buffer, sizeof(buffer), "0x%02X '%c'", c, c);
	    break;

	default:
	    snprintf(buffer, sizeof(buffer), "0x%02X '\\%o'", c, c);
	    break;
	}
    }
    return buffer;
}


void
trace_time_real(const char *name, long fake)
{
    if (name && *name)
	trace_printf("%s = ", name);
    time_t value = (time_t)fake;
    trace_printf("%ld /* %.24s */", fake, ctime(&value));
    trace_printf(name && *name ? ";\n" : ",\n");
}


void
trace_double_real(const char *name, const double &value)
{
    if (name && *name)
	trace_printf("%s = ", name);
    trace_printf("%g", value);
    trace_printf(name && *name ? ";\n" : ",\n");
}