-
Notifications
You must be signed in to change notification settings - Fork 0
/
pahole.c
3327 lines (2785 loc) · 83.5 KB
/
pahole.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
/*
SPDX-License-Identifier: GPL-2.0-only
Copyright (C) 2006 Mandriva Conectiva S.A.
Copyright (C) 2006 Arnaldo Carvalho de Melo <[email protected]>
Copyright (C) 2007- Arnaldo Carvalho de Melo <[email protected]>
*/
#include <argp.h>
#include <assert.h>
#include <stdio.h>
#include <dwarf.h>
#include <elfutils/version.h>
#include <inttypes.h>
#include <limits.h>
#include <pthread.h>
#include <search.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <bpf/btf.h>
#include "bpf/libbpf.h"
#include "dwarves_reorganize.h"
#include "dwarves.h"
#include "dutil.h"
//#include "ctf_encoder.h" FIXME: disabled, probably its better to move to Oracle's libctf
#include "btf_encoder.h"
static struct btf_encoder *btf_encoder;
static char *detached_btf_filename;
static bool btf_encode;
static bool btf_gen_floats;
static bool ctf_encode;
static bool sort_output;
static bool need_resort;
static bool first_obj_only;
static bool skip_encoding_btf_vars;
static bool btf_encode_force;
static const char *base_btf_file;
static const char *prettify_input_filename;
static FILE *prettify_input;
static uint8_t class__include_anonymous;
static uint8_t class__include_nested_anonymous;
static uint8_t word_size, original_word_size;
static char *class__exclude_prefix;
static size_t class__exclude_prefix_len;
static char *class__include_prefix;
static size_t class__include_prefix_len;
static char *cu__exclude_prefix;
static size_t cu__exclude_prefix_len;
static char *decl_exclude_prefix;
static size_t decl_exclude_prefix_len;
static uint16_t nr_holes;
static uint16_t nr_bit_holes;
static uint16_t hole_size_ge;
static uint8_t show_packable;
static bool show_with_flexible_array;
static uint8_t global_verbose;
static uint8_t recursive;
static size_t cacheline_size;
static uint8_t find_containers;
static uint8_t find_pointers_in_structs;
static int reorganize;
static bool show_private_classes;
static bool defined_in;
static bool just_unions;
static bool just_structs;
static bool just_packed_structs;
static int show_reorg_steps;
static const char *class_name;
static LIST_HEAD(class_names);
static char separator = '\t';
static struct conf_fprintf conf = {
.emit_stats = 1,
};
static struct conf_load conf_load = {
.conf_fprintf = &conf,
};
struct structure {
struct list_head node;
struct rb_node rb_node;
struct class *class;
struct cu *cu;
uint32_t id;
uint32_t nr_files;
uint32_t nr_methods;
};
static struct structure *structure__new(struct class *class, struct cu *cu, uint32_t id)
{
struct structure *st = malloc(sizeof(*st));
if (st != NULL) {
st->nr_files = 1;
st->nr_methods = 0;
st->class = class;
st->cu = cu;
st->id = id;
}
return st;
}
static void structure__delete(struct structure *st)
{
if (st == NULL)
return;
free(st);
}
static struct rb_root structures__tree = RB_ROOT;
static LIST_HEAD(structures__list);
static pthread_mutex_t structures_lock = PTHREAD_MUTEX_INITIALIZER;
static int type__compare_members_types(struct type *a, struct cu *cu_a, struct type *b, struct cu *cu_b)
{
int ret = strcmp(type__name(a), type__name(b));
if (ret)
return ret;
// a->nr_members should be equal to b->nr_members at this point
if (a->nr_members == 0)
return 0;
struct class_member *ma, *mb = type__first_member(b);
type__for_each_member(a, ma) {
struct tag *type_ma = cu__type(cu_a, ma->tag.type),
*type_mb = cu__type(cu_b, mb->tag.type);
if (type_ma && !type_mb && class_member__name(mb) == NULL) {
/*
* FIXME This is happening with a vmlinux built with
* clang and thin-LTO, and since this is not
* multithreadeded, we can get the previous behaviour
* by considering just the first occurence, the one with
* all the class member names and proper types, and since
* the name, size, number of members is the same, consider them equal
* and use the complete type (the first one found).
* With this btfdiff works for both non-thin-LTO and thin-LTO vmlinux files
*/
return 0;
}
if (!type_ma || !type_mb) // shuldn't happen
return type_ma ? 1 : -1; // best effort
const char *name_a = class_member__name(ma),
*name_b = class_member__name(mb);
if (name_a && name_b) {
ret = strcmp(name_a, name_b);
if (ret)
return ret;
}
ret = (int)ma->bit_offset - (int)mb->bit_offset;
if (ret)
return ret;
ret = (int)ma->bitfield_size - (int)mb->bitfield_size;
if (ret)
return ret;
char bf_a[1024], bf_b[1024];
ret = strcmp(tag__name(type_ma, cu_a, bf_a, sizeof(bf_a), NULL),
tag__name(type_mb, cu_b, bf_b, sizeof(bf_b), NULL));
if (ret)
return ret;
mb = class_member__next(mb);
}
return 0;
}
static int type__compare_members(struct type *a, struct type *b)
{
int ret;
// a->nr_members should be equal to b->nr_members at this point
if (a->nr_members == 0)
return 0;
struct class_member *ma, *mb = type__first_member(b);
// Don't look at the types, as we may be referring to a CU being loaded
// in another thread and since we're not locking the ptr_table's, we
// may race When printing all the types using --sort we'll do an extra
// check that takes into account the types, since at that time all the
// ptr_tables/cus are quiescent.
type__for_each_member(a, ma) {
const char *name_a = class_member__name(ma),
*name_b = class_member__name(mb);
if (name_a && name_b) {
ret = strcmp(name_a, name_b);
if (ret)
return ret;
}
ret = (int)ma->bit_offset - (int)mb->bit_offset;
if (ret)
return ret;
ret = (int)ma->bitfield_size - (int)mb->bitfield_size;
if (ret)
return ret;
mb = class_member__next(mb);
}
/*
Since we didn't check the types, we may end with at least this btfdiff output:
+++ /tmp/btfdiff.btf.b5DJu4 2021-08-18 12:06:27.773932193 -0300
@@ -31035,7 +31035,7 @@ struct elf_note_info {
struct memelfnote auxv; / * 56 24 * /
/ * --- cacheline 1 boundary (64 bytes) was 16 bytes ago --- * /
struct memelfnote files; / * 80 24 * /
- compat_siginfo_t csigdata; / * 104 128 * /
+ siginfo_t csigdata; / * 104 128 * /
So if we're printing everything, consider the types as different and
at the end with type__compare_members_types() when using --sort,
we'll need as well to resort, to avoid things like:
@@ -47965,8 +47965,8 @@ struct instance_attribute {
/ * XXX last struct has 6 bytes of padding * /
- ssize_t (*show)(struct edac_device_instance *, char *); / * 16 8 * /
- ssize_t (*store)(struct edac_device_instance *, const char *, size_t); / * 24 8 * /
+ ssize_t (*show)(struct edac_pci_ctl_info *, char *); / * 16 8 * /
+ ssize_t (*store)(struct edac_pci_ctl_info *, const char *, size_t); / * 24 8 * /
/ * size: 32, cachelines: 1, members: 3 * /
/ * paddings: 1, sum paddings: 6 * /
@@ -47977,8 +47977,8 @@ struct instance_attribute {
/ * XXX last struct has 6 bytes of padding * /
- ssize_t (*show)(struct edac_pci_ctl_info *, char *); / * 16 8 * /
- ssize_t (*store)(struct edac_pci_ctl_info *, const char *, size_t); / * 24 8 * /
+ ssize_t (*show)(struct edac_device_instance *, char *); / * 16 8 * /
+ ssize_t (*store)(struct edac_device_instance *, const char *, size_t); / * 24 8 * /
/ * size: 32, cachelines: 1, members: 3 * /
/ * paddings: 1, sum paddings: 6 * /
I.e. the difference is in the arguments to those show/store function
pointers, but since we didn't took the types into account when first
sorting, we need to resort.
So the first sort weeds out duplicates when loading from multiple
CUs, i.e. DWARF, the second will make sure both BTF and DWARF are
sorted taking into account types and then btfdiff finally will be
happy and we can continue to depend on it for regression tests for
the BTF and DWARF encoder and loader
*/
if (sort_output) {
need_resort = true;
return 1;
}
return 0;
}
static int type__compare(struct type *a, struct cu *cu_a, struct type *b, struct cu *cu_b)
{
int ret = strcmp(type__name(a), type__name(b));
if (ret)
goto found;
ret = (int)a->size - (int)b->size;
if (ret)
goto found;
ret = (int)a->nr_members - (int)b->nr_members;
if (ret)
goto found;
ret = type__compare_members(a, b);
found:
return ret;
}
static struct structure *__structures__add(struct class *class, struct cu *cu, uint32_t id, bool *existing_entry)
{
struct rb_node **p = &structures__tree.rb_node;
struct rb_node *parent = NULL;
struct structure *str;
while (*p != NULL) {
int rc;
parent = *p;
str = rb_entry(parent, struct structure, rb_node);
rc = type__compare(&str->class->type, str->cu, &class->type, cu);
if (rc > 0)
p = &(*p)->rb_left;
else if (rc < 0)
p = &(*p)->rb_right;
else {
*existing_entry = true;
return str;
}
}
str = structure__new(class, cu, id);
if (str == NULL)
return NULL;
*existing_entry = false;
rb_link_node(&str->rb_node, parent, p);
rb_insert_color(&str->rb_node, &structures__tree);
/* For linear traversals */
list_add_tail(&str->node, &structures__list);
return str;
}
static struct structure *structures__add(struct class *class, struct cu *cu, uint32_t id, bool *existing_entry)
{
struct structure *str;
pthread_mutex_lock(&structures_lock);
str = __structures__add(class, cu, id, existing_entry);
pthread_mutex_unlock(&structures_lock);
return str;
}
static void __structures__delete(void)
{
struct rb_node *next = rb_first(&structures__tree);
while (next) {
struct structure *pos = rb_entry(next, struct structure, rb_node);
next = rb_next(&pos->rb_node);
rb_erase(&pos->rb_node, &structures__tree);
structure__delete(pos);
}
}
void structures__delete(void)
{
pthread_mutex_lock(&structures_lock);
__structures__delete();
pthread_mutex_unlock(&structures_lock);
}
static void nr_definitions_formatter(struct structure *st)
{
printf("%s%c%u\n", class__name(st->class), separator, st->nr_files);
}
static void nr_members_formatter(struct class *class, struct cu *cu __maybe_unused, uint32_t id __maybe_unused)
{
printf("%s%c%u\n", class__name(class), separator, class__nr_members(class));
}
static void nr_methods_formatter(struct structure *st)
{
printf("%s%c%u\n", class__name(st->class), separator, st->nr_methods);
}
static void size_formatter(struct class *class, struct cu *cu __maybe_unused, uint32_t id __maybe_unused)
{
printf("%s%c%d%c%u\n", class__name(class), separator,
class__size(class), separator, tag__is_union(class__tag(class)) ? 0 : class->nr_holes);
}
static void class_name_len_formatter(struct class *class, struct cu *cu __maybe_unused, uint32_t id __maybe_unused)
{
const char *name = class__name(class);
printf("%s%c%zd\n", name, separator, strlen(name));
}
static void class_name_formatter(struct class *class, struct cu *cu __maybe_unused, uint32_t id __maybe_unused)
{
puts(class__name(class));
}
static void class_formatter(struct class *class, struct cu *cu, uint32_t id)
{
struct tag *typedef_alias = NULL;
struct tag *tag = class__tag(class);
const char *name = class__name(class);
if (name == NULL) {
/*
* Find the first typedef for this struct, this is enough
* as if we optimize the struct all the typedefs will be
* affected.
*/
typedef_alias = cu__find_first_typedef_of_type(cu, id);
/*
* If there is no typedefs for this anonymous struct it is
* found just inside another struct, and in this case it'll
* be printed when the type it is in is printed, but if
* the user still wants to see its statistics, just use
* --nested_anon_include.
*/
if (typedef_alias == NULL && !class__include_nested_anonymous)
return;
}
if (typedef_alias != NULL) {
struct type *tdef = tag__type(typedef_alias);
conf.prefix = "typedef";
conf.suffix = type__name(tdef);
} else
conf.prefix = conf.suffix = NULL;
tag__fprintf(tag, cu, &conf, stdout);
putchar('\n');
}
static void print_packable_info(struct class *c, struct cu *cu, uint32_t id)
{
const struct tag *t = class__tag(c);
const size_t orig_size = class__size(c);
const size_t new_size = class__size(c->priv);
const size_t savings = orig_size - new_size;
const char *name = class__name(c);
/* Anonymous struct? Try finding a typedef */
if (name == NULL) {
const struct tag *tdef =
cu__find_first_typedef_of_type(cu, id);
if (tdef != NULL)
name = class__name(tag__class(tdef));
}
if (name != NULL)
printf("%s%c%zd%c%zd%c%zd\n",
name, separator,
orig_size, separator,
new_size, separator,
savings);
else
printf("%s(%d)%c%zd%c%zd%c%zd\n",
tag__decl_file(t, cu),
tag__decl_line(t, cu),
separator,
orig_size, separator,
new_size, separator,
savings);
}
static void (*stats_formatter)(struct structure *st);
static void print_stats(void)
{
struct structure *pos;
list_for_each_entry(pos, &structures__list, node)
stats_formatter(pos);
}
static struct class *class__filter(struct class *class, struct cu *cu,
uint32_t tag_id);
static void (*formatter)(struct class *class,
struct cu *cu, uint32_t id) = class_formatter;
static void print_classes(struct cu *cu)
{
uint32_t id;
struct class *pos;
cu__for_each_struct_or_union(cu, id, pos) {
bool existing_entry;
struct structure *str;
if (pos->type.namespace.name == 0 &&
!(class__include_anonymous ||
class__include_nested_anonymous))
continue;
if (!class__filter(pos, cu, id))
continue;
/*
* FIXME: No sense in adding an anonymous struct to the list of
* structs already printed, as we look for the name... The
* right fix probably will be to call class__fprintf on a
* in-memory FILE, do a hash, and look it by full contents, not
* by name. And this is needed for CTF as well, but its late now
* and I'm sleepy, will leave for later...
*/
if (pos->type.namespace.name != 0) {
str = structures__add(pos, cu, id, &existing_entry);
if (str == NULL) {
fprintf(stderr, "pahole: insufficient memory for "
"processing %s, skipping it...\n", cu->name);
return;
}
/* Already printed... */
if (existing_entry) {
str->nr_files++;
continue;
}
}
if (show_packable && !global_verbose)
print_packable_info(pos, cu, id);
else if (sort_output && formatter == class_formatter)
continue; // we'll print it at the end, in order, out of structures__tree
else if (formatter != NULL)
formatter(pos, cu, id);
}
}
static void __print_ordered_classes(struct rb_root *root)
{
struct rb_node *next = rb_first(root);
while (next) {
struct structure *st = rb_entry(next, struct structure, rb_node);
class_formatter(st->class, st->cu, st->id);
next = rb_next(&st->rb_node);
}
}
static void resort_add(struct rb_root *resorted, struct structure *str)
{
struct rb_node **p = &resorted->rb_node;
struct rb_node *parent = NULL;
struct structure *node;
while (*p != NULL) {
int rc;
parent = *p;
node = rb_entry(parent, struct structure, rb_node);
rc = type__compare_members_types(&node->class->type, node->cu, &str->class->type, str->cu);
if (rc > 0)
p = &(*p)->rb_left;
else if (rc < 0)
p = &(*p)->rb_right;
else
return; // Duplicate, ignore it
}
rb_link_node(&str->rb_node, parent, p);
rb_insert_color(&str->rb_node, resorted);
}
static void resort_classes(struct rb_root *resorted, struct list_head *head)
{
struct structure *str;
list_for_each_entry(str, head, node)
resort_add(resorted, str);
}
static void print_ordered_classes(void)
{
if (!need_resort) {
__print_ordered_classes(&structures__tree);
} else {
struct rb_root resorted = RB_ROOT;
resort_classes(&resorted, &structures__list);
__print_ordered_classes(&resorted);
}
}
static struct cu *cu__filter(struct cu *cu)
{
if (cu__exclude_prefix != NULL &&
(cu->name == NULL ||
strncmp(cu__exclude_prefix, cu->name,
cu__exclude_prefix_len) == 0))
return NULL;
return cu;
}
static int class__packable(struct class *class, struct cu *cu)
{
struct class *clone;
if (class->nr_holes == 0 && class->nr_bit_holes == 0)
return 0;
clone = class__clone(class, NULL);
if (clone == NULL)
return 0;
class__reorganize(clone, cu, 0, stdout);
if (class__size(class) > class__size(clone)) {
class->priv = clone;
return 1;
}
class__delete(clone);
return 0;
}
static bool class__has_flexible_array(struct class *class, struct cu *cu)
{
struct class_member *member = type__last_member(&class->type);
if (member == NULL)
return false;
struct tag *type = cu__type(cu, member->tag.type);
if (type->tag != DW_TAG_array_type)
return false;
struct array_type *array = tag__array_type(type);
if (array->dimensions > 1)
return false;
if (array->nr_entries == NULL || array->nr_entries[0] == 0)
return true;
return false;
}
static struct class *class__filter(struct class *class, struct cu *cu,
uint32_t tag_id)
{
struct tag *tag = class__tag(class);
const char *name;
if (just_unions && !tag__is_union(tag))
return NULL;
if (just_structs && !tag__is_struct(tag))
return NULL;
if (just_packed_structs) {
/* Is it not packed? */
if (!class__infer_packed_attributes(class, cu))
return NULL;
}
if (!tag->top_level) {
class__find_holes(class);
if (!show_private_classes)
return NULL;
}
name = class__name(class);
if (class__is_declaration(class))
return NULL;
if (!class__include_anonymous && name == NULL)
return NULL;
if (class__exclude_prefix != NULL) {
if (name == NULL) {
const struct tag *tdef =
cu__find_first_typedef_of_type(cu, tag_id);
if (tdef != NULL) {
struct class *c = tag__class(tdef);
name = class__name(c);
}
}
if (name != NULL && strncmp(class__exclude_prefix, name,
class__exclude_prefix_len) == 0)
return NULL;
}
if (class__include_prefix != NULL) {
if (name == NULL) {
const struct tag *tdef =
cu__find_first_typedef_of_type(cu, tag_id);
if (tdef != NULL) {
struct class *c = tag__class(tdef);
name = class__name(c);
}
}
if (name != NULL && strncmp(class__include_prefix, name,
class__include_prefix_len) != 0)
return NULL;
}
if (decl_exclude_prefix != NULL &&
(!tag__decl_file(tag, cu) ||
strncmp(decl_exclude_prefix, tag__decl_file(tag, cu),
decl_exclude_prefix_len) == 0))
return NULL;
/*
* if --unions was used and we got here, its a union and we satisfy the other
* filters/options, so don't filter it.
*/
if (just_unions)
return class;
/*
* The following only make sense for structs, i.e. 'struct class',
* and as we can get here with a union, that is represented by a 'struct type',
* bail out if we get here with an union and we are not looking for things
* that need finding holes, like --packable, --nr_holes, etc
*/
if (!tag__is_struct(tag))
return (just_structs || show_packable || nr_holes || nr_bit_holes || hole_size_ge) ? NULL : class;
if (tag->top_level)
class__find_holes(class);
if (class->nr_holes < nr_holes ||
class->nr_bit_holes < nr_bit_holes ||
(hole_size_ge != 0 && !class__has_hole_ge(class, hole_size_ge)))
return NULL;
if (show_packable && !class__packable(class, cu))
return NULL;
if (show_with_flexible_array && !class__has_flexible_array(class, cu))
return NULL;
return class;
}
static void union__find_new_size(struct tag *tag, struct cu *cu);
static void class__resize_LP(struct tag *tag, struct cu *cu)
{
struct tag *tag_pos;
struct class *class = tag__class(tag);
size_t word_size_diff;
size_t orig_size = class->type.size;
if (tag__type(tag)->resized)
return;
tag__type(tag)->resized = 1;
if (original_word_size > word_size)
word_size_diff = original_word_size - word_size;
else
word_size_diff = word_size - original_word_size;
type__for_each_tag(tag__type(tag), tag_pos) {
struct tag *type;
size_t diff = 0;
size_t array_multiplier = 1;
/* we want only data members, i.e. with byte_offset attr */
if (tag_pos->tag != DW_TAG_member &&
tag_pos->tag != DW_TAG_inheritance)
continue;
type = cu__type(cu, tag_pos->type);
tag__assert_search_result(type);
if (type->tag == DW_TAG_array_type) {
int i;
for (i = 0; i < tag__array_type(type)->dimensions; ++i)
array_multiplier *= tag__array_type(type)->nr_entries[i];
type = cu__type(cu, type->type);
tag__assert_search_result(type);
}
if (tag__is_typedef(type)) {
type = tag__follow_typedef(type, cu);
tag__assert_search_result(type);
}
switch (type->tag) {
case DW_TAG_base_type: {
struct base_type *bt = tag__base_type(type);
char bf[64];
const char *name = base_type__name(bt, bf, sizeof(bf));
if (strcmp(name, "long int") != 0 &&
strcmp(name, "long unsigned int") != 0)
break;
}
/* fallthru */
case DW_TAG_pointer_type:
diff = word_size_diff;
break;
case DW_TAG_structure_type:
case DW_TAG_union_type:
if (tag__is_union(type))
union__find_new_size(type, cu);
else
class__resize_LP(type, cu);
diff = tag__type(type)->size_diff;
break;
}
diff *= array_multiplier;
if (diff != 0) {
struct class_member *m = tag__class_member(tag_pos);
if (original_word_size > word_size) {
class->type.size -= diff;
class__subtract_offsets_from(class, m, diff);
} else {
class->type.size += diff;
class__add_offsets_from(class, m, diff);
}
}
}
if (original_word_size > word_size)
tag__type(tag)->size_diff = orig_size - class->type.size;
else
tag__type(tag)->size_diff = class->type.size - orig_size;
class__find_holes(class);
class__fixup_alignment(class, cu);
}
static void union__find_new_size(struct tag *tag, struct cu *cu)
{
struct tag *tag_pos;
struct type *type = tag__type(tag);
size_t max_size = 0;
if (type->resized)
return;
type->resized = 1;
type__for_each_tag(type, tag_pos) {
struct tag *type;
size_t size;
/* we want only data members, i.e. with byte_offset attr */
if (tag_pos->tag != DW_TAG_member &&
tag_pos->tag != DW_TAG_inheritance)
continue;
type = cu__type(cu, tag_pos->type);
tag__assert_search_result(type);
if (tag__is_typedef(type))
type = tag__follow_typedef(type, cu);
if (tag__is_union(type))
union__find_new_size(type, cu);
else if (tag__is_struct(type))
class__resize_LP(type, cu);
size = tag__size(type, cu);
if (size > max_size)
max_size = size;
}
if (max_size > type->size)
type->size_diff = max_size - type->size;
else
type->size_diff = type->size - max_size;
type->size = max_size;
}
static void tag__fixup_word_size(struct tag *tag, struct cu *cu)
{
if (tag__is_struct(tag) || tag__is_union(tag)) {
struct tag *pos;
namespace__for_each_tag(tag__namespace(tag), pos)
tag__fixup_word_size(pos, cu);
}
switch (tag->tag) {
case DW_TAG_base_type: {
struct base_type *bt = tag__base_type(tag);
/*
* This shouldn't happen, but at least on a tcp_ipv6.c
* built with GNU C 4.3.0 20080130 (Red Hat 4.3.0-0.7),
* one was found, so just bail out.
*/
if (!bt->name)
return;
char bf[64];
const char *name = base_type__name(bt, bf, sizeof(bf));
if (strcmp(name, "long int") == 0 ||
strcmp(name, "long unsigned int") == 0)
bt->bit_size = word_size * 8;
}
break;
case DW_TAG_structure_type:
class__resize_LP(tag, cu);
break;
case DW_TAG_union_type:
union__find_new_size(tag, cu);
break;
}
return;
}
static void cu_fixup_word_size_iterator(struct cu *cu)
{
original_word_size = cu->addr_size;
cu->addr_size = word_size;
uint32_t id;
struct tag *pos;
cu__for_each_type(cu, id, pos)
tag__fixup_word_size(pos, cu);
}
static void cu__account_nr_methods(struct cu *cu)
{
struct function *pos_function;
struct structure *str;
uint32_t id;
cu__for_each_function(cu, id, pos_function) {
struct class_member *pos;
function__for_each_parameter(pos_function, cu, pos) {
struct tag *type = cu__type(cu, pos->tag.type);
if (type == NULL || !tag__is_pointer(type))
continue;
type = cu__type(cu, type->type);
if (type == NULL || !tag__is_struct(type))
continue;
struct type *ctype = tag__type(type);
if (ctype->namespace.name == 0)
continue;
struct class *class = tag__class(type);
if (!class__filter(class, cu, 0))
continue;
bool existing_entry;
str = structures__add(class, cu, id, &existing_entry);
if (str == NULL) {
fprintf(stderr, "pahole: insufficient memory "
"for processing %s, skipping it...\n",
cu->name);
return;
}
if (!existing_entry)
class__find_holes(class);
++str->nr_methods;
}
}
}
static char tab[128];
static void print_structs_with_pointer_to(struct cu *cu, uint32_t type)
{
struct class *pos;
struct class_member *pos_member;
uint32_t id;
cu__for_each_struct_or_union(cu, id, pos) {
bool looked = false;
/*
* Set it to NULL just to silence the compiler, as the printf
* at the end of the type__for_each_member() loop is only reached
* after str _is_ set, as looked starts as false, str is used with
* structures_add and if it is NULL, we return.
*/
struct structure *str = NULL;
if (pos->type.namespace.name == 0)