-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathobjects.c
1410 lines (1149 loc) · 29.2 KB
/
objects.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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright(C) 2016, Red Hat, Inc., Jerome Marchand
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/>.
*/
/*
* Internal representation and manipulation of symbols
*/
#ifndef _GNU_SOURCE /* We use GNU basename() that doesn't modify the arg */
#error "We need GNU version of basename()!"
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <strings.h>
#include <limits.h>
#include <errno.h>
#include <unistd.h>
#include <getopt.h>
#include <libgen.h>
#include <sys/stat.h>
#include "objects.h"
#include "utils.h"
#include "main.h"
#include "record.h"
/* Indentation offset for c-style and tree debug outputs */
#define C_INDENT_OFFSET 8
#define DBG_INDENT_OFFSET 4
obj_list_t *obj_list_new(obj_t *obj)
{
obj_list_t *list = safe_zmalloc(sizeof(obj_list_t));
list->member = obj;
list->next = NULL;
return list;
}
static void obj_list_init(obj_list_head_t *head, obj_t *obj)
{
obj_list_t *list = obj_list_new(obj);
head->first = head->last = list;
}
obj_list_head_t *obj_list_head_new(obj_t *obj)
{
obj_list_head_t *h = safe_zmalloc(sizeof(obj_list_head_t));
obj_list_init(h, obj);
return h;
}
static bool obj_list_empty(obj_list_head_t *head)
{
return head->first == NULL;
}
void obj_list_add(obj_list_head_t *head, obj_t *obj)
{
obj_list_t *list;
if (obj_list_empty(head)) {
obj_list_init(head, obj);
return;
}
list = obj_list_new(obj);
if (head->last->next)
fprintf(stderr, "head->last is not the last\n");
head->last->next = list;
head->last = list;
}
obj_t *obj_new(obj_types type, char *name)
{
obj_t *new = safe_zmalloc(sizeof(obj_t));
new->type = type;
new->name = global_string_get_move(name);
new->ns = NULL;
return new;
}
static void _obj_free(obj_t *o, obj_t *skip);
static void _obj_list_free(obj_list_head_t *l, obj_t *skip)
{
obj_list_t *list;
obj_list_t *next;
if (l == NULL)
return;
list = l->first;
free(l);
while (list) {
_obj_free(list->member, skip);
next = list->next;
free(list);
list = next;
}
}
static void obj_list_free(obj_list_head_t *l)
{
_obj_list_free(l, NULL);
}
/*
* Free the tree o, but keep the subtree skip.
*/
static void _obj_free(obj_t *o, obj_t *skip)
{
if (!o || (o == skip))
return;
if (o->type == __type_reffile && o->depend_rec_node) {
list_del(o->depend_rec_node);
o->depend_rec_node = NULL;
}
_obj_list_free(o->member_list, skip);
if (o->ptr)
_obj_free(o->ptr, skip);
if (is_weak(o))
free(o->link);
free(o);
}
/*
* Free the all object
*/
void obj_free(obj_t *o)
{
_obj_free(o, NULL);
}
#define _CREATE_NEW_FUNC(type, suffix) \
obj_t *obj_##type##_##suffix(char *name) \
{ \
obj_t *new = obj_new(__type_##type, name); \
return new; \
}
#define CREATE_NEW_FUNC(type) _CREATE_NEW_FUNC(type, new)
#define CREATE_NEW_FUNC_NONAME(type) \
_CREATE_NEW_FUNC(type, new_) \
obj_t *obj_##type##_new() \
{ \
return obj_##type##_new_(NULL); \
}
#define _CREATE_NEW_ADD_FUNC(type, infix) \
obj_t *obj_##type##_##infix##_##add(char *name, obj_t *obj) \
{ \
obj_t *new = obj_new(__type_##type, name); \
new->ptr = obj; \
return new; \
}
#define CREATE_NEW_ADD_FUNC(type) _CREATE_NEW_ADD_FUNC(type, new)
#define CREATE_NEW_ADD_FUNC_NONAME(type) \
_CREATE_NEW_ADD_FUNC(type, new_) \
obj_t *obj_##type##_new_add(obj_t *obj) \
{ \
return obj_##type##_new__add(NULL, obj); \
}
CREATE_NEW_FUNC(struct)
CREATE_NEW_FUNC(union)
CREATE_NEW_FUNC(enum)
CREATE_NEW_FUNC(constant)
CREATE_NEW_FUNC_NONAME(reffile)
CREATE_NEW_ADD_FUNC(func)
CREATE_NEW_ADD_FUNC(typedef)
CREATE_NEW_ADD_FUNC(var)
CREATE_NEW_ADD_FUNC(struct_member)
CREATE_NEW_ADD_FUNC_NONAME(ptr)
CREATE_NEW_ADD_FUNC_NONAME(array)
CREATE_NEW_ADD_FUNC_NONAME(qualifier)
CREATE_NEW_FUNC(assembly)
CREATE_NEW_FUNC(weak)
obj_t *obj_basetype_new(char *base_type)
{
obj_t *new = obj_new(__type_base, NULL);
new->base_type = global_string_get_move(base_type);
return new;
}
const char *obj_type_name[NR_OBJ_TYPES+1] = {
"reference file",
"struct",
"union",
"enum",
"func",
"ptr",
"typedef",
"array",
"var",
"struct member",
"type qualifier",
"base",
"constant",
"assembly",
"weak",
"unknown type"
};
static const char *typetostr(obj_t *o)
{
int t = o->type;
if (t >= NR_OBJ_TYPES)
t = NR_OBJ_TYPES;
return obj_type_name[t];
}
static int c_precedence(obj_t *o)
{
switch (o->type) {
case __type_func:
case __type_array:
return 1;
case __type_ptr:
return 2;
default:
return INT_MAX;
}
}
/*
* Returns whether parentheses are needed
*
* Pointer have a higher precedence than function and array, so we need to put
* parentheses around a pointer to a function of array.
*/
static bool is_paren_needed(obj_t *node)
{
obj_t *child = node->ptr;
while (child) {
if (c_precedence(child) < c_precedence(node))
return true;
child = child->ptr;
}
return false;
}
static char *print_margin_offset(const char *prefix, const char *s, int depth)
{
size_t len = snprintf(NULL, 0, "%-*s", depth * C_INDENT_OFFSET, s) + 1;
char *ret;
if (prefix)
len += strlen(prefix);
if (!len)
return NULL;
ret = safe_zmalloc(len);
snprintf(ret, len, "%s%-*s",
prefix ? prefix : "", depth * C_INDENT_OFFSET, s);
return ret;
}
static char *print_margin(const char *prefix, int depth)
{
return print_margin_offset(prefix, "", depth);
}
/*
* Return type for print_* functions
*
* Because C mixes prefix and postfix operator, the code generation from a node
* may need to add code before, after or in the middle of the code generated by
* subtrees. Thus we sometimes need two return two strings.
*
* Attention to the precedence and associativity sould be taken when
* deciding where a specific string should be inserted
*/
typedef struct {
char *prefix;
char *postfix;
} pp_t;
static void free_pp(pp_t pp)
{
free(pp.prefix);
free(pp.postfix);
}
static pp_t _print_tree(obj_t *o, int depth, bool newline, const char *prefix);
/*
* Add prefix p at the begining of string s (reallocated)
*
* space: add a space between p and s
* freep: free the string p
*/
static char *_prefix_str(char **s, char *p, bool space, bool freep)
{
size_t lenp = strlen(p), lens = 0, newlen;
if (*s)
lens = strlen(*s);
newlen = lens + lenp + 1;
if (space)
newlen++;
*s = safe_realloc(*s, newlen);
if (lens)
memmove(space ? *s+lenp+1 : *s+lenp, *s, lens + 1);
else
(*s)[lenp] = '\0';
memcpy(*s, p, lenp);
if (space)
(*s)[lenp] = ' ';
if (freep)
free(p);
return *s;
}
static char *prefix_str(char **s, char *p)
{
if (!p)
return *s;
return _prefix_str(s, p, false, false);
}
static char *prefix_str_free(char **s, char *p)
{
if (!p)
return *s;
return _prefix_str(s, p, false, true);
}
static char *prefix_str_space(char **s, const char *p)
{
if (!p)
return *s;
/* freep is false so we can pass const char * */
return _prefix_str(s, (char *)p, true, false);
}
/*
* Add suffix p at the end of string s (realocated)
*
* space: add a space between p and s
* freep: free the string p
*/
static char *_postfix_str(char **s, char *p, bool space, bool freep)
{
int lenp = strlen(p), lens = 0, newlen;
if (*s)
lens = strlen(*s);
newlen = lens + lenp + 1;
if (space)
newlen++;
*s = safe_realloc(*s, newlen);
if (lens == 0)
(*s)[0] = '\0';
if (space)
strcat(*s, " ");
strcat(*s, p);
if (freep)
free(p);
return *s;
}
static char *postfix_str(char **s, const char *p)
{
if (!p)
return *s;
/* freep is false so we can pass const char * */
return _postfix_str(s, (char *)p, false, false);
}
static char *postfix_str_free(char **s, char *p)
{
if (!p)
return *s;
return _postfix_str(s, p, false, true);
}
static pp_t print_base(obj_t *o, int depth, const char *prefix)
{
pp_t ret = {NULL, NULL};
safe_asprintf(&ret.prefix, "%s ", o->base_type);
return ret;
}
static pp_t print_constant(obj_t *o, int depth, const char *prefix)
{
pp_t ret = {NULL, NULL};
safe_asprintf(&ret.prefix, "%s = %li", o->name, (long)o->constant);
return ret;
}
static pp_t print_reffile(obj_t *o, int depth, const char *prefix)
{
pp_t ret = {NULL, NULL};
char *s = filenametotype(o->base_type);
s = safe_realloc(s, strlen(s) + 2);
strcat(s, " ");
ret.prefix = s;
return ret;
}
/* Print a struct, enum or an union */
static pp_t print_structlike(obj_t *o, int depth, const char *prefix)
{
pp_t ret = {NULL, NULL}, tmp;
obj_list_t *list = NULL;
char *s, *margin;
if (o->name)
safe_asprintf(&s, "%s %s {\n", typetostr(o), o->name);
else
safe_asprintf(&s, "%s {\n", typetostr(o));
if (o->member_list)
list = o->member_list->first;
while (list) {
tmp = _print_tree(list->member, depth+1, true, prefix);
postfix_str_free(&s, tmp.prefix);
postfix_str_free(&s, tmp.postfix);
postfix_str(&s, o->type == __type_enum ? ",\n" : ";\n");
list = list->next;
}
margin = print_margin(prefix, depth);
postfix_str_free(&s, margin);
postfix_str(&s, "}");
ret.prefix = s;
return ret;
}
static pp_t print_func(obj_t *o, int depth, const char *prefix)
{
pp_t ret = {NULL, NULL}, return_type;
obj_list_t *list = NULL;
obj_t *next = o->ptr;
char *s, *margin;
const char *name;
return_type = _print_tree(next, depth, false, prefix);
ret.prefix = return_type.prefix;
if (o->name)
name = o->name;
else
name = "";
safe_asprintf(&s, "%s(\n", name);
if (o->member_list)
list = o->member_list->first;
while (list) {
pp_t arg = _print_tree(list->member, depth+1, true, prefix);
postfix_str_free(&s, arg.prefix);
postfix_str_free(&s, arg.postfix);
list = list->next;
postfix_str(&s, list ? ",\n" : "\n");
}
margin = print_margin(prefix, depth);
postfix_str_free(&s, margin);
postfix_str(&s, ")");
ret.postfix = s;
return ret;
}
static pp_t print_array(obj_t *o, int depth, const char *prefix)
{
pp_t ret;
char *s;
obj_t *next = o->ptr;
ret = _print_tree(next, depth, false, prefix);
safe_asprintf(&s, "[%lu]", o->constant);
prefix_str_free(&ret.postfix, s);
return ret;
}
static pp_t print_ptr(obj_t *o, int depth, const char *prefix)
{
pp_t ret;
bool need_paren = is_paren_needed(o);
obj_t *next = o->ptr;
ret = _print_tree(next, depth, false, prefix);
if (need_paren) {
postfix_str(&ret.prefix, "(*");
prefix_str(&ret.postfix, ")");
} else
postfix_str(&ret.prefix, "*");
return ret;
}
/* Print a var or a struct_member */
static pp_t print_varlike(obj_t *o, int depth, const char *prefix)
{
pp_t ret;
char *s = NULL;
if (is_bitfield(o))
safe_asprintf(&s, "%s:%i",
o->name, o->last_bit - o->first_bit + 1);
else
s = (char *)o->name;
ret = _print_tree(o->ptr, depth, false, prefix);
if (s)
postfix_str(&ret.prefix, s);
if (is_bitfield(o))
free(s);
return ret;
}
static pp_t print_typedef(obj_t *o, int depth, const char *prefix)
{
pp_t ret;
ret = _print_tree(o->ptr, depth, false, prefix);
prefix_str(&ret.prefix, "typedef ");
postfix_str(&ret.prefix, o->name);
return ret;
}
static pp_t print_qualifier(obj_t *o, int depth, const char *prefix)
{
pp_t ret;
ret = _print_tree(o->ptr, depth, false, prefix);
prefix_str_space(&ret.prefix, o->base_type);
return ret;
}
static pp_t print_assembly(obj_t *o, int depth, const char *prefix)
{
pp_t ret = {NULL, NULL};
prefix_str(&ret.prefix, "assembly ");
postfix_str(&ret.prefix, o->name);
return ret;
}
static pp_t print_weak(obj_t *o, int depth, const char *prefix)
{
pp_t ret = {NULL, NULL};
prefix_str(&ret.prefix, "weak ");
postfix_str(&ret.prefix, o->name);
postfix_str(&ret.prefix, " -> ");
postfix_str(&ret.prefix, o->link);
return ret;
}
#define BASIC_CASE(type) \
case __type_##type: \
ret = print_##type(o, depth, prefix); \
break;
struct dopt display_options;
/*
* Display an object in a c-like format
*
* o: object to be displayed
* depth: current indentation depth
* newline: is this the begining of a new line?
* prefix: prefix to be printed at the begining of each line
*/
static pp_t _print_tree(obj_t *o, int depth, bool newline, const char *prefix)
{
pp_t ret = {NULL, NULL};
char *margin;
/* silence coverity on write-only variable */
(void)ret;
if (!o)
fail("NULL pointer in _print_tree\n");
debug("_print_tree(): %s\n", typetostr(o));
switch (o->type) {
BASIC_CASE(reffile);
BASIC_CASE(constant);
BASIC_CASE(base);
BASIC_CASE(typedef);
BASIC_CASE(qualifier);
BASIC_CASE(func);
BASIC_CASE(array);
BASIC_CASE(ptr);
BASIC_CASE(assembly);
BASIC_CASE(weak);
case __type_var:
case __type_struct_member:
ret = print_varlike(o, depth, prefix);
break;
case __type_struct:
case __type_union:
case __type_enum:
ret = print_structlike(o, depth, prefix);
break;
default:
fail("WIP: doesn't handle %s\n", typetostr(o));
}
if (!newline)
return ret;
if (o->type == __type_struct_member && !display_options.no_offset) {
char *offstr;
if (is_bitfield(o))
safe_asprintf(&offstr, "0x%lx:%2i-%-2i ",
o->offset, o->first_bit, o->last_bit);
else
safe_asprintf(&offstr, "0x%lx ", o->offset);
margin = print_margin_offset(prefix, offstr, depth);
free(offstr);
} else
margin = print_margin(prefix, depth);
prefix_str_free(&ret.prefix, margin);
return ret;
}
void obj_print_tree__prefix(obj_t *root, const char *prefix, FILE *stream)
{
pp_t s = _print_tree(root, 0, true, prefix);
fprintf(stream, "%s%s;\n",
s.prefix ? s.prefix : "",
s.postfix ? s.postfix : "");
free_pp(s);
}
void obj_print_tree(obj_t *root)
{
obj_print_tree__prefix(root, NULL, stdout);
}
static void fill_parent_rec(obj_t *o, obj_t *parent)
{
obj_list_t *list = NULL;
o->parent = parent;
if (o->member_list)
list = o->member_list->first;
while (list) {
fill_parent_rec(list->member, o);
list = list->next;
}
if (o->ptr)
fill_parent_rec(o->ptr, o);
}
/*
* Walk the tree and fill all the parents field
*/
void obj_fill_parent(obj_t *root)
{
fill_parent_rec(root, NULL);
}
static int walk_list(obj_list_t *list, cb_t cb_pre, cb_t cb_in, cb_t cb_post,
void *args, bool ptr_first)
{
int ret = CB_CONT;
while (list) {
ret = obj_walk_tree3(list->member, cb_pre, cb_in, cb_post,
args, ptr_first);
if (ret == CB_FAIL)
return ret;
else
ret = CB_CONT;
list = list->next;
}
return ret;
}
static int walk_ptr(obj_t *o, cb_t cb_pre, cb_t cb_in, cb_t cb_post,
void *args, bool ptr_first)
{
int ret = CB_CONT;
if (o->ptr) {
ret = obj_walk_tree3(o->ptr, cb_pre, cb_in, cb_post,
args, ptr_first);
if (ret == CB_FAIL)
return ret;
else
ret = CB_CONT;
}
return ret;
}
/*
* Tree walk with prefix, infix and postfix callbacks
*
* o: walked tree
* cb_pre: callback function called before walking the subtrees
* cb_in: callback function called between walking the subtrees
* cp_post: callback function called between walking the subtrees
* args: argument passed to the callbacks
* ptr_first: whether we walk member_list of ptr first
*/
int obj_walk_tree3(obj_t *o, cb_t cb_pre, cb_t cb_in, cb_t cb_post,
void *args, bool ptr_first)
{
obj_list_t *list = NULL;
int ret = CB_CONT;
if (cb_pre) {
ret = cb_pre(o, args);
if (ret)
return ret;
}
if (o->member_list)
list = o->member_list->first;
if (ptr_first)
ret = walk_ptr(o, cb_pre, cb_in, cb_post, args, ptr_first);
else
ret = walk_list(list, cb_pre, cb_in, cb_post, args, ptr_first);
if (ret == CB_FAIL)
return ret;
if (cb_in) {
ret = cb_in(o, args);
if (ret)
return ret;
}
if (ptr_first)
ret = walk_list(list, cb_pre, cb_in, cb_post, args, ptr_first);
else
ret = walk_ptr(o, cb_pre, cb_in, cb_post, args, ptr_first);
if (ret == CB_FAIL)
return ret;
if (cb_post) {
ret = cb_post(o, args);
if (ret)
return ret;
}
return ret;
}
/*
* Simple tree walk with a prefix callback
*
* It walks the member_list subtree first.
*/
int obj_walk_tree(obj_t *root, cb_t cb, void *args)
{
return obj_walk_tree3(root, cb, NULL, NULL, args, false);
}
static void _show_node(FILE *f, obj_t *o, int margin)
{
if (o)
fprintf(f,
"\%*s<%s, \"%s\", \"%s\", %p, %p, %p, %lu, %i, %i>\n",
margin, "", typetostr(o), o->name, o->base_type,
o, o->parent, o->ptr,
o->offset, o->first_bit, o->last_bit);
else
fprintf(f, "\%*s<(nil)>\n", margin, "");
}
static void show_node(obj_t *o, int margin)
{
_show_node(stdout, o, margin);
}
static int debug_node(obj_t *node, void *args)
{
int *depth = (int *) args;
show_node(node, *depth * DBG_INDENT_OFFSET);
(*depth)++;
return CB_CONT;
}
static int dec_depth(obj_t *node, void *args)
{
int *depth = (int *) args;
(*depth)--;
return CB_CONT;
}
/*
* Print a raw representation of the internal object tree
*/
int obj_debug_tree(obj_t *root)
{
int depth = 0;
return obj_walk_tree3(root, debug_node, NULL, dec_depth, &depth, false);
}
/*
* Hide some RH_KABI magic
*
* WARNING: this code is ugly and full of add-hoc hacks, but I'm
* afraid it can't be fixed. It has to follow the internals of
* RH_KABI_* macros. Also, it may have to change if RH_KABI_*
* functions change in the future.
*
* RH_KABI_REPLACE the old field by a complex construction of union
* and struct used to check that the new field didn't change the
* alignement. It is of the form:
* union {
* _new;
* struct {
* _orig;
* } __UNIQUE_ID_rh_kabi_hideXX
* union {};
* }
*
* RH_KABI_USE2(_P) replace a single field by two field that fits in
* the same space. It puts the two new field into an unnamed
* struct. We don't hide that as we have no way to know if that struct
* is an artifact from RH_KABI_USE2 or was added deliberately.
*
* RH_KABI_DEPRECATE(_FN) prefix the field name with
* rh_reserved_. This is not the most specific string. It currently
* appears in a few places that deprecates the field by hand, in which
* it's OK to hide it too, but for some reason in
* block_device_operations the reserved fields are of the form "void
* *rh_reserved_ptrsX" instead of the usual "unsigned long
* rh_reservedX". Treat this case as an exception.
*
* Most RH_KABI_* functions, don't add any recognizable code so we
* can't hide them here.
*/
static int hide_kabi_cb(obj_t *o, void *args)
{
obj_t *kabi_struct, *new, *old, *parent = o->parent, *keeper;
obj_list_head_t *lh;
obj_list_t *l;
bool show_new_field = (bool) args;
if (o->name) {
if (!strncmp(o->name, RH_KABI_HIDE, RH_KABI_HIDE_LEN))
fail("Missed a kabi unique ID\n");
/* Hide RH_KABI_DEPRECATE* */
if (!strncmp(o->name, "rh_reserved_", 12) &&
strncmp(o->name, "rh_reserved_ptrs", 16)) {
char *tmp = strdup(o->name+12);
o->name = global_string_get_move(tmp);
}
}
/* Hide RH_KABI_REPLACE */
if ((o->type != __type_union) || o->name ||
!(lh = o->member_list) || obj_list_empty(lh) ||
!(l = lh->first) || !(new = l->member) ||
!(l = l->next) || !(kabi_struct = l->member) ||
(kabi_struct->type != __type_var) ||
!kabi_struct->name ||
strncmp(kabi_struct->name, RH_KABI_HIDE, RH_KABI_HIDE_LEN))
return CB_CONT;
if (!kabi_struct->ptr || kabi_struct->ptr->type != __type_struct ||
!(lh = kabi_struct->ptr->member_list) || obj_list_empty(lh) ||
!(l = lh->first) || !(old = l->member))
fail("Unexpeted rh_kabi_hide struct format\n");
/*
* It is a rh_kabi_hide union
* old is the first member of kabi_struct
*
* Need to replace that:
* <struct member, "(null)", "(null)", 0x1ea9840 16 0 0> (parent)
* <union, "(null)", "(null)", (nil) 0 0 0> (o)
* <var, "new_field", "(null)", 0x1ea9540 0 0 0> (new)
* <base, "(null)", "int", (nil) 0 0 0>
* <var, "__UNIQUE_ID_rh_kabi_hide55", "(null)", 0x1ea9700 0 0 0>
* <struct, "(null)", "(null)", (nil) 0 0 0>
* <struct member, "old_field", "(null)", 0x1ea9640 0 0 0>
* <base, "(null)", "int", (nil) 0 0 0> ^(old)
* <var, "(null)", "(null)", 0x1ea97a0 0 0 0>
* <union, "(null)", "(null)", (nil) 0 0 0>
*
* by that:
* <struct member, "new_field", "(null)", 0x1ea9540 16 0 0>
* <base, "(null)", "int", (nil) 0 0 0>
*
* or that:
* <struct member, "old_field", "(null)", 0x1ea9640 0 0 0>
* <base, "(null)", "int", (nil) 0 0 0>
*
* Parent is always an unary node, struct_member or var
*/
if (!parent ||
!((parent->type == __type_var) ||
(parent->type == __type_struct_member)) ||
(parent->ptr != o) || parent->name) {
_show_node(stderr, parent, 0);
fail("Unexpected parent\n");
}
if (new->type != __type_var) {
_show_node(stderr, new, 0);
fail("Unexpected new field\n");
}
if (old->type != __type_struct_member) {
_show_node(stderr, old, 0);
fail("Unexpected old field\n");
}
keeper = show_new_field ? new : old;
parent->name = keeper->name;
parent->ptr = keeper->ptr;
parent->ptr->parent = parent;
_obj_free(o, keeper);
free(keeper);
return CB_SKIP;
}
int obj_hide_kabi(obj_t *root, bool show_new_field)
{
return obj_walk_tree(root, hide_kabi_cb, (void *)show_new_field);
}
static bool obj_is_declaration(obj_t *obj)
{
if (obj->type != __type_reffile || obj->ref_record == NULL)