-
Notifications
You must be signed in to change notification settings - Fork 0
/
lens_default.c
714 lines (623 loc) · 16.1 KB
/
lens_default.c
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
/*
* This file is part of ltrace.
* Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
* Copyright (C) 1998,2004,2007,2008,2009 Juan Cespedes
* Copyright (C) 2006 Ian Wienand
* Copyright (C) 2006 Steve Fink
*
* 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 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <ctype.h>
#include <stdlib.h>
#include <assert.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "proc.h"
#include "lens_default.h"
#include "value.h"
#include "expr.h"
#include "type.h"
#include "common.h"
#include "zero.h"
#define READER(NAME, TYPE) \
static int \
NAME(struct value *value, TYPE *ret, struct value_dict *arguments) \
{ \
union { \
TYPE val; \
unsigned char buf[0]; \
} u; \
if (value_extract_buf(value, u.buf, arguments) < 0) \
return -1; \
*ret = u.val; \
return 0; \
}
READER(read_float, float)
READER(read_double, double)
#undef READER
#define HANDLE_WIDTH(BITS) \
do { \
long l; \
if (value_extract_word(value, &l, arguments) < 0) \
return -1; \
int##BITS##_t i = l; \
uint64_t v = (uint64_t)(uint##BITS##_t)i; \
switch (format) { \
case INT_FMT_unknown: \
if (l < -10000 || l > 10000) \
case INT_FMT_x: \
return fprintf(stream, "%#"PRIx64, v); \
case INT_FMT_i: \
case INT_FMT_default: \
return fprintf(stream, "%"PRIi##BITS, i); \
case INT_FMT_u: \
return fprintf(stream, "%"PRIu64, v); \
case INT_FMT_o: \
return fprintf(stream, "0%"PRIo64, v); \
} \
} while (0)
enum int_fmt_t
{
INT_FMT_i,
INT_FMT_u,
INT_FMT_o,
INT_FMT_x,
INT_FMT_unknown,
INT_FMT_default,
};
static int
format_integer(FILE *stream, struct value *value, enum int_fmt_t format,
struct value_dict *arguments)
{
switch (type_sizeof(value->inferior, value->type)) {
case 1: HANDLE_WIDTH(8);
case 2: HANDLE_WIDTH(16);
case 4: HANDLE_WIDTH(32);
case 8: HANDLE_WIDTH(64);
default:
assert(!"unsupported integer width");
abort();
case -1:
return -1;
}
}
#undef HANDLE_WIDTH
static int
acc_fprintf(int *countp, FILE *stream, const char *format, ...)
{
va_list pa;
va_start(pa, format);
int i = account_output(countp, vfprintf(stream, format, pa));
va_end(pa);
return i;
}
static int
format_char(FILE *stream, struct value *value, struct value_dict *arguments)
{
long lc;
if (value_extract_word(value, &lc, arguments) < 0)
return -1;
int c = (int)lc;
const char *fmt;
switch (c) {
case -1:
fmt = "EOF";
break;
case 0:
fmt = "\\0";
break;
case '\a':
fmt = "\\a";
break;
case '\b':
fmt = "\\b";
break;
case '\t':
fmt = "\\t";
break;
case '\n':
fmt = "\\n";
break;
case '\v':
fmt = "\\v";
break;
case '\f':
fmt = "\\f";
break;
case '\r':
fmt = "\\r";
break;
case '\\':
fmt = "\\\\";
break;
default:
if (isprint(c) || c == ' ')
fmt = "%c";
else
fmt = "\\%03o";
}
return fprintf(stream, fmt, c);
}
static int
format_naked_char(FILE *stream, struct value *value,
struct value_dict *arguments)
{
int written = 0;
if (acc_fprintf(&written, stream, "'") < 0
|| account_output(&written,
format_char(stream, value, arguments)) < 0
|| acc_fprintf(&written, stream, "'") < 0)
return -1;
return written;
}
static int
format_double(FILE *stream, double value, enum int_fmt_t format)
{
if (format == INT_FMT_x)
return fprintf(stream, "%a", value);
else
return fprintf(stream, "%f", value);
}
static int
format_floating(FILE *stream, struct value *value, struct value_dict *arguments,
enum int_fmt_t format)
{
switch (value->type->type) {
float f;
double d;
case ARGTYPE_FLOAT:
if (read_float(value, &f, arguments) < 0)
return -1;
return format_double(stream, f, format);
case ARGTYPE_DOUBLE:
if (read_double(value, &d, arguments) < 0)
return -1;
return format_double(stream, d, format);
default:
abort();
}
}
struct format_argument_data
{
struct value *value;
struct value_dict *arguments;
};
static int
format_argument_cb(FILE *stream, void *ptr)
{
struct format_argument_data *data = ptr;
return format_argument(stream, data->value, data->arguments);
}
static int
format_struct(FILE *stream, struct value *value, struct value_dict *arguments)
{
int written = 0;
if (acc_fprintf(&written, stream, "{ ") < 0)
return -1;
int need_delim = 0;
size_t i;
for (i = 0; i < type_struct_size(value->type); ++i) {
struct value element;
if (value_init_element(&element, value, i) < 0)
return -1;
struct format_argument_data data = { &element, arguments };
int o = delim_output(stream, &need_delim,
format_argument_cb, &data);
value_destroy(&element);
if (o < 0)
return -1;
written += o;
}
if (acc_fprintf(&written, stream, " }") < 0)
return -1;
return written;
}
int
format_pointer(FILE *stream, struct value *value, struct value_dict *arguments)
{
if (value_is_zero(value, arguments))
return fprintf(stream, "nil");
/* The following is for detecting recursion. We keep track of
* the values that were already displayed. Each time a
* pointer should be dereferenced, we compare its value to the
* value of each of the pointers dereferenced so far. If one
* of them matches, instead of recursing, we just printf which
* superstructure this pointer recurses to. */
static struct vect pointers = {};
if (pointers.elt_size == 0)
VECT_INIT(&pointers, struct value *);
/* Trim number of expanded structures of the same type. Even
* for non-recursive structure, we don't want to expand all of
* it if it's huge. */
size_t i;
size_t len = vect_size(&pointers);
assert(value->type->type == ARGTYPE_POINTER);
struct arg_type_info *pointee = value->type->u.ptr_info.info;
if (pointee->type == ARGTYPE_STRUCT) {
size_t depth = 0;
for (i = 0; i < len; ++i) {
struct value *old
= *VECT_ELEMENT(&pointers, struct value *, i);
assert(old->type->type == ARGTYPE_POINTER);
struct arg_type_info *old_pointee
= old->type->u.ptr_info.info;
if (old_pointee == pointee)
depth++;
}
if (depth >= options.arraylen)
return fprintf(stream, "...");
}
for (i = len; i-- > 0 ;) {
struct value **old = VECT_ELEMENT(&pointers, struct value *, i);
int rc = value_equal(value, *old, arguments);
if (rc < 0)
return -1;
if (rc > 0) {
size_t reclevel = len - i - 1;
char buf[reclevel + 1];
memset(buf, '^', sizeof buf);
buf[reclevel] = 0;
return fprintf(stream, "recurse%s", buf);
}
}
/* OK, not a recursion. Remember this value for tracking. */
if (VECT_PUSHBACK(&pointers, &value) < 0)
return -1;
struct value element;
int o;
if (value_init_deref(&element, value) < 0) {
o = -1;
goto done;
}
o = format_argument(stream, &element, arguments);
value_destroy(&element);
done:
vect_popback(&pointers);
return o;
}
/*
* LENGTH is an expression whose evaluation will yield the actual
* length of the array.
*
* MAXLEN is the actual maximum length that we care about
*
* BEFORE if LENGTH>MAXLEN, we display ellipsis. We display it before
* the closing parenthesis if BEFORE, otherwise after it.
*
* OPEN, CLOSE, DELIM are opening and closing parenthesis and element
* delimiter.
*/
int
format_array(FILE *stream, struct value *value, struct value_dict *arguments,
struct expr_node *length, size_t maxlen, int before,
const char *open, const char *close, const char *delim)
{
/* We need "long" to be long enough to cover the whole address
* space. */
typedef char assert__long_enough_long[-(sizeof(long) < sizeof(void *))];
long l;
if (expr_eval_word(length, value, arguments, &l) < 0)
return -1;
size_t len = (size_t)l;
int written = 0;
if (acc_fprintf(&written, stream, "%s", open) < 0)
return -1;
size_t i;
for (i = 0; i < len && i <= maxlen; ++i) {
if (i == maxlen) {
if (before && acc_fprintf(&written, stream, "...") < 0)
return -1;
break;
}
if (i > 0 && acc_fprintf(&written, stream, "%s", delim) < 0)
return -1;
struct value element;
if (value_init_element(&element, value, i) < 0)
return -1;
int o = format_argument(stream, &element, arguments);
value_destroy(&element);
if (o < 0)
return -1;
written += o;
}
if (acc_fprintf(&written, stream, "%s", close) < 0)
return -1;
if (i == maxlen && !before && acc_fprintf(&written, stream, "...") < 0)
return -1;
return written;
}
static int
toplevel_format_lens(struct lens *lens, FILE *stream,
struct value *value, struct value_dict *arguments,
enum int_fmt_t int_fmt)
{
switch (value->type->type) {
case ARGTYPE_VOID:
return fprintf(stream, "<void>");
case ARGTYPE_SHORT:
case ARGTYPE_INT:
case ARGTYPE_LONG:
return format_integer(stream, value, int_fmt, arguments);
case ARGTYPE_USHORT:
case ARGTYPE_UINT:
case ARGTYPE_ULONG:
if (int_fmt == INT_FMT_i || int_fmt == INT_FMT_default)
int_fmt = INT_FMT_u;
return format_integer(stream, value, int_fmt, arguments);
case ARGTYPE_CHAR:
if (int_fmt == INT_FMT_default)
return format_naked_char(stream, value, arguments);
return format_integer(stream, value, int_fmt, arguments);
case ARGTYPE_FLOAT:
case ARGTYPE_DOUBLE:
return format_floating(stream, value, arguments, int_fmt);
case ARGTYPE_STRUCT:
return format_struct(stream, value, arguments);
case ARGTYPE_POINTER:
if (value->type->u.array_info.elt_type->type != ARGTYPE_VOID)
return format_pointer(stream, value, arguments);
return format_integer(stream, value, INT_FMT_x, arguments);
case ARGTYPE_ARRAY:
return format_array(stream, value, arguments,
value->type->u.array_info.length,
options.arraylen, 1, "[ ", " ]", ", ");
}
abort();
}
static int
default_lens_format_cb(struct lens *lens, FILE *stream,
struct value *value, struct value_dict *arguments)
{
return toplevel_format_lens(lens, stream, value, arguments,
INT_FMT_default);
}
struct lens default_lens = {
.format_cb = default_lens_format_cb,
};
static int
blind_lens_format_cb(struct lens *lens, FILE *stream,
struct value *value, struct value_dict *arguments)
{
return 0;
}
struct lens blind_lens = {
.format_cb = blind_lens_format_cb,
};
static int
octal_lens_format_cb(struct lens *lens, FILE *stream,
struct value *value, struct value_dict *arguments)
{
return toplevel_format_lens(lens, stream, value, arguments, INT_FMT_o);
}
struct lens octal_lens = {
.format_cb = octal_lens_format_cb,
};
static int
hex_lens_format_cb(struct lens *lens, FILE *stream,
struct value *value, struct value_dict *arguments)
{
return toplevel_format_lens(lens, stream, value, arguments, INT_FMT_x);
}
struct lens hex_lens = {
.format_cb = hex_lens_format_cb,
};
static int
dec_lens_format_cb(struct lens *lens, FILE *stream,
struct value *value, struct value_dict *arguments)
{
return toplevel_format_lens(lens, stream, value, arguments, INT_FMT_u);
}
struct lens dec_lens = {
.format_cb = dec_lens_format_cb,
};
static int
guess_lens_format_cb(struct lens *lens, FILE *stream,
struct value *value, struct value_dict *arguments)
{
return toplevel_format_lens(lens, stream, value, arguments,
INT_FMT_unknown);
}
struct lens guess_lens = {
.format_cb = guess_lens_format_cb,
};
static int
bool_lens_format_cb(struct lens *lens, FILE *stream,
struct value *value, struct value_dict *arguments)
{
switch (value->type->type) {
case ARGTYPE_VOID:
case ARGTYPE_FLOAT:
case ARGTYPE_DOUBLE:
case ARGTYPE_STRUCT:
case ARGTYPE_POINTER:
case ARGTYPE_ARRAY:
return toplevel_format_lens(lens, stream, value,
arguments, INT_FMT_default);
int zero;
case ARGTYPE_SHORT:
case ARGTYPE_INT:
case ARGTYPE_LONG:
case ARGTYPE_USHORT:
case ARGTYPE_UINT:
case ARGTYPE_ULONG:
case ARGTYPE_CHAR:
if ((zero = value_is_zero(value, arguments)) < 0)
return -1;
if (zero)
return fprintf(stream, "false");
else
return fprintf(stream, "true");
}
abort();
}
struct lens bool_lens = {
.format_cb = bool_lens_format_cb,
};
static int
string_lens_format_cb(struct lens *lens, FILE *stream,
struct value *value, struct value_dict *arguments)
{
switch (value->type->type) {
case ARGTYPE_POINTER:
/* This should really be written as either "string",
* or, if lens, then string(array(char, zero)*). But
* I suspect people are so used to the char * C idiom,
* that string(char *) might actually turn up. So
* let's just support it. */
if (value->type->u.ptr_info.info->type == ARGTYPE_CHAR) {
struct arg_type_info info[2];
type_init_array(&info[1],
value->type->u.ptr_info.info, 0,
expr_node_zero(), 0);
type_init_pointer(&info[0], &info[1], 0);
info->lens = lens;
info->own_lens = 0;
struct value tmp;
if (value_clone(&tmp, value) < 0)
return -1;
value_set_type(&tmp, info, 0);
int ret = string_lens_format_cb(lens, stream, &tmp,
arguments);
type_destroy(&info[0]);
type_destroy(&info[1]);
value_destroy(&tmp);
return ret;
}
/* fall-through */
case ARGTYPE_VOID:
case ARGTYPE_FLOAT:
case ARGTYPE_DOUBLE:
case ARGTYPE_STRUCT:
case ARGTYPE_SHORT:
case ARGTYPE_INT:
case ARGTYPE_LONG:
case ARGTYPE_USHORT:
case ARGTYPE_UINT:
case ARGTYPE_ULONG:
return toplevel_format_lens(lens, stream, value,
arguments, INT_FMT_default);
case ARGTYPE_CHAR:
return format_char(stream, value, arguments);
case ARGTYPE_ARRAY:
return format_array(stream, value, arguments,
value->type->u.array_info.length,
options.strlen, 0, "\"", "\"", "");
}
abort();
}
struct lens string_lens = {
.format_cb = string_lens_format_cb,
};
static int
out_bits(FILE *stream, size_t low, size_t high)
{
if (low == high)
return fprintf(stream, "%zd", low);
else
return fprintf(stream, "%zd-%zd", low, high);
}
static unsigned
bitcount(unsigned u)
{
int c = 0;
for (; u > 0; u &= u - 1)
c++;
return c;
}
static int
bitvect_lens_format_cb(struct lens *lens, FILE *stream,
struct value *value, struct value_dict *arguments)
{
unsigned char *data = value_get_data(value, arguments);
if (data == NULL)
return -1;
size_t sz = type_sizeof(value->inferior, value->type);
if (sz == (size_t)-1)
return -1;
size_t i;
unsigned char buf[sz];
switch ((int)value->type->type) {
union bitvect_integral_64
{
uint8_t u8;
uint16_t u16;
uint32_t u32;
uint64_t u64;
unsigned char buf[0];
} bv;
case ARGTYPE_POINTER:
return format_pointer(stream, value, arguments);
case ARGTYPE_STRUCT:
case ARGTYPE_ARRAY:
break;
default:
assert(sz <= sizeof(bv));
memmove(bv.buf, data, sz);
if (sz == 1)
bv.u64 = bv.u8;
else if (sz == 2)
bv.u64 = bv.u16;
else if (sz == 4)
bv.u64 = bv.u32;
for (i = 0; i < sz; ++i) {
buf[i] = bv.u64 & 0xff;
bv.u64 >>= 8;
}
data = buf;
}
size_t bits = 0;
for (i = 0; i < sz; ++i)
bits += bitcount(data[i]);
/* If there's more 1's than 0's, show inverse. */
unsigned neg = bits > sz * 4 ? 0xff : 0x00;
int o = 0;
if (acc_fprintf(&o, stream, "%s<", "~" + (neg == 0x00)) < 0)
return -1;
size_t bitno = 0;
ssize_t low = -1;
for (i = 0; i < sz; ++i) {
unsigned char m;
unsigned char d = data[i] ^ neg;
for (m = 0x01; m != 0; m <<= 1) {
int bit = !!(m & d);
if (low < 0) {
if (bit) {
if (low == -2
&& acc_fprintf(&o, stream, ",") < 0)
return -1;
low = bitno;
}
} else if (!bit) {
if (account_output(&o, out_bits(stream, low,
bitno-1)) < 0)
return -1;
low = -2;
}
bitno++;
}
}
if (low >= 0 && account_output(&o, out_bits(stream, low, bitno-1)) < 0)
return -1;
if (fputc('>', stream) < 0)
return -1;
o += 1;
return o;
}
struct lens bitvect_lens = {
.format_cb = bitvect_lens_format_cb,
};