Repository navigation
Expand file tree
/
Copy pathannotate-data.c
More file actions
2260 lines (1879 loc) · 57.5 KB
/
Copy pathannotate-data.c
File metadata and controls
2260 lines (1879 loc) · 57.5 KB
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 */
/*
* Convert sample address to data type using DWARF debug info.
*
* Written by Namhyung Kim <namhyung@kernel.org>
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <linux/zalloc.h>
#include "annotate.h"
#include "annotate-data.h"
#include "debuginfo.h"
#include "debug.h"
#include "dso.h"
#include "dwarf-regs.h"
#include "evsel.h"
#include "evlist.h"
#include "map.h"
#include "map_symbol.h"
#include "sort.h"
#include "strbuf.h"
#include "symbol.h"
#include "symbol_conf.h"
#include "thread.h"
/* register number of the stack pointer */
#define X86_REG_SP 7
#define ARM64_REG_SP 31
static void delete_var_types(struct die_var_type *var_types);
#define pr_debug_dtp(fmt, ...) \
do { \
if (debug_type_profile) \
pr_info(fmt, ##__VA_ARGS__); \
else \
pr_debug3(fmt, ##__VA_ARGS__); \
} while (0)
void pr_debug_type_name(Dwarf_Die *die, enum type_state_kind kind)
{
struct strbuf sb;
char *str;
Dwarf_Word size = 0;
if (!debug_type_profile && verbose < 3)
return;
switch (kind) {
case TSR_KIND_INVALID:
pr_info("\n");
return;
case TSR_KIND_PERCPU_BASE:
pr_info(" percpu base\n");
return;
case TSR_KIND_CONST:
pr_info(" constant\n");
return;
case TSR_KIND_PERCPU_POINTER:
pr_info(" percpu pointer");
/* it also prints the type info */
break;
case TSR_KIND_POINTER:
pr_info(" pointer");
/* it also prints the type info */
break;
case TSR_KIND_CANARY:
pr_info(" stack canary\n");
return;
case TSR_KIND_GLOBAL_ADDR:
pr_info(" global address\n");
return;
case TSR_KIND_TYPE:
default:
break;
}
if (dwarf_aggregate_size(die, &size) != 0)
size = 0;
strbuf_init(&sb, 32);
die_get_typename_from_type(die, &sb);
str = strbuf_detach(&sb, NULL);
pr_info(" type='%s' size=%#lx (die:%#lx)\n",
str, (long)size, (long)dwarf_dieoffset(die));
free(str);
}
static void pr_debug_location(Dwarf_Die *die, u64 pc, int reg)
{
ptrdiff_t off = 0;
Dwarf_Attribute attr;
Dwarf_Addr base, start, end;
Dwarf_Op *ops;
size_t nops;
if (!debug_type_profile && verbose < 3)
return;
if (dwarf_attr(die, DW_AT_location, &attr) == NULL)
return;
while ((off = dwarf_getlocations(&attr, off, &base, &start, &end, &ops, &nops)) > 0) {
if (reg != DWARF_REG_PC && end <= pc)
continue;
if (reg != DWARF_REG_PC && start > pc)
break;
pr_info(" variable location: ");
switch (ops->atom) {
case DW_OP_reg0 ...DW_OP_reg31:
pr_info("reg%d\n", ops->atom - DW_OP_reg0);
break;
case DW_OP_breg0 ...DW_OP_breg31:
pr_info("base=reg%d, offset=%#lx\n",
ops->atom - DW_OP_breg0, (long)ops->number);
break;
case DW_OP_regx:
pr_info("reg%ld\n", (long)ops->number);
break;
case DW_OP_bregx:
pr_info("base=reg%ld, offset=%#lx\n",
(long)ops->number, (long)ops->number2);
break;
case DW_OP_fbreg:
pr_info("use frame base, offset=%#lx\n", (long)ops->number);
break;
case DW_OP_addr:
pr_info("address=%#lx\n", (long)ops->number);
break;
default:
pr_info("unknown: code=%#x, number=%#lx\n",
ops->atom, (long)ops->number);
break;
}
break;
}
}
static void pr_debug_scope(Dwarf_Die *scope_die)
{
int tag;
if (!debug_type_profile && verbose < 3)
return;
pr_info("(die:%lx) ", (long)dwarf_dieoffset(scope_die));
tag = dwarf_tag(scope_die);
if (tag == DW_TAG_subprogram)
pr_info("[function] %s\n", die_name(scope_die));
else if (tag == DW_TAG_inlined_subroutine)
pr_info("[inlined] %s\n", die_name(scope_die));
else if (tag == DW_TAG_lexical_block)
pr_info("[block]\n");
else
pr_info("[unknown] tag=%x\n", tag);
}
bool has_reg_type(struct type_state *state, int reg)
{
return (unsigned)reg < ARRAY_SIZE(state->regs);
}
static void init_type_state(struct type_state *state, const struct arch *arch)
{
memset(state, 0, sizeof(*state));
INIT_LIST_HEAD(&state->stack_vars);
if (arch__is_x86(arch)) {
state->regs[0].caller_saved = true;
state->regs[1].caller_saved = true;
state->regs[2].caller_saved = true;
state->regs[4].caller_saved = true;
state->regs[5].caller_saved = true;
state->regs[8].caller_saved = true;
state->regs[9].caller_saved = true;
state->regs[10].caller_saved = true;
state->regs[11].caller_saved = true;
state->ret_reg = 0;
state->stack_reg = X86_REG_SP;
} else if (arch__is_arm64(arch)) {
/*
* Mark x0..x18 as caller-saved. x30 (LR) is intentionally
* omitted from the caller_saved set: it is clobbered by
* calls, but holds only the return address and is not used
* to carry data type information.
*/
for (int i = 0; i < 19; i++)
state->regs[i].caller_saved = true;
state->ret_reg = 0;
state->stack_reg = ARM64_REG_SP;
}
}
static void exit_type_state(struct type_state *state)
{
struct type_state_stack *stack, *tmp;
list_for_each_entry_safe(stack, tmp, &state->stack_vars, list) {
list_del(&stack->list);
free(stack);
}
}
/*
* Compare type name and size to maintain them in a tree.
* I'm not sure if DWARF would have information of a single type in many
* different places (compilation units). If not, it could compare the
* offset of the type entry in the .debug_info section.
*/
static int data_type_cmp(const void *_key, const struct rb_node *node)
{
const struct annotated_data_type *key = _key;
struct annotated_data_type *type;
type = rb_entry(node, struct annotated_data_type, node);
if (key->self.size != type->self.size)
return key->self.size - type->self.size;
return strcmp(key->self.type_name, type->self.type_name);
}
static bool data_type_less(struct rb_node *node_a, const struct rb_node *node_b)
{
struct annotated_data_type *a, *b;
a = rb_entry(node_a, struct annotated_data_type, node);
b = rb_entry(node_b, struct annotated_data_type, node);
if (a->self.size != b->self.size)
return a->self.size < b->self.size;
return strcmp(a->self.type_name, b->self.type_name) < 0;
}
/*
* A broken type can point back at one of its own ancestors: bound the
* nesting so it doesn't recurse until the stack is gone.
*/
#define MAX_MEMBER_DEPTH 32
/* Recursively add new members for struct/union */
static int __add_member_cb(Dwarf_Die *die, void *arg)
{
struct annotated_member *parent = arg;
struct annotated_member *member, *prev;
Dwarf_Die die_mem;
Dwarf_Word size, loc, bit_size = 0;
Dwarf_Attribute attr;
struct strbuf sb;
int tag;
if (dwarf_tag(die) != DW_TAG_member)
return DIE_FIND_CB_SIBLING;
if (die_get_real_type(die, &die_mem) == NULL)
return DIE_FIND_CB_SIBLING;
member = zalloc(sizeof(*member));
if (member == NULL)
return DIE_FIND_CB_END;
if (strbuf_init(&sb, 32) < 0) {
free(member);
return DIE_FIND_CB_END;
}
if (die_get_typename(die, &sb) < 0)
strbuf_add(&sb, "(unknown type)", 14);
if (dwarf_aggregate_size(&die_mem, &size) < 0 || size == 0) {
if (dwarf_tag(&die_mem) == DW_TAG_array_type) { /* flex-array? */
die_get_real_type(&die_mem, &die_mem);
member->is_flex_array = true;
if (dwarf_aggregate_size(&die_mem, &size) < 0)
size = 0;
} else {
size = 0;
}
}
if (dwarf_attr_integrate(die, DW_AT_data_member_location, &attr)) {
if (dwarf_formudata(&attr, &loc) != 0) {
if (die_get_data_member_location(die, &loc) != 0)
loc = 0;
}
} else {
/* bitfield member */
if (dwarf_attr_integrate(die, DW_AT_data_bit_offset, &attr) &&
dwarf_formudata(&attr, &loc) == 0)
loc /= 8;
else
loc = 0;
if (dwarf_attr_integrate(die, DW_AT_bit_size, &attr) &&
dwarf_formudata(&attr, &bit_size) == 0)
size = (bit_size + 7) / 8;
}
member->type_name = strbuf_detach(&sb, NULL);
/* member->var_name can be NULL */
if (dwarf_diename(die)) {
if (bit_size) {
if (asprintf(&member->var_name, "%s:%ld",
dwarf_diename(die), (long)bit_size) < 0)
member->var_name = NULL;
} else {
const char *name = dwarf_diename(die);
member->var_name = name ? strdup(name) : NULL;
}
if (member->var_name == NULL) {
free(member);
return DIE_FIND_CB_END;
}
}
member->size = size;
member->offset = loc + parent->offset;
member->depth = parent->depth + 1;
INIT_LIST_HEAD(&member->children);
list_for_each_entry_reverse(prev, &parent->children, node) {
if (prev->offset <= member->offset)
break;
}
list_add(&member->node, &prev->node);
tag = dwarf_tag(&die_mem);
switch (tag) {
case DW_TAG_union_type:
member->is_union = true;
/* fall through */
case DW_TAG_structure_type:
/* Only aggregates have children to expand, so only they get truncated. */
if (member->depth >= MAX_MEMBER_DEPTH) {
/* Consumed by the JSON exporter added in a later series. */
member->truncated = true;
pr_debug_dtp("member nesting limit reached at %s\n",
member->type_name ?: "(unknown type)");
break;
}
die_find_child(&die_mem, __add_member_cb, member, &die_mem);
break;
default:
break;
}
return DIE_FIND_CB_SIBLING;
}
static void add_member_types(struct annotated_data_type *parent, Dwarf_Die *type)
{
Dwarf_Die die_mem;
die_find_child(type, __add_member_cb, &parent->self, &die_mem);
}
static void delete_members(struct annotated_member *member)
{
struct annotated_member *child, *tmp;
list_for_each_entry_safe(child, tmp, &member->children, node) {
list_del(&child->node);
delete_members(child);
zfree(&child->type_name);
zfree(&child->var_name);
free(child);
}
}
static struct annotated_member *find_flex_array(struct annotated_member *m)
{
struct annotated_member *child;
if (list_empty(&m->children))
return NULL;
if (m->is_union) {
list_for_each_entry(child, &m->children, node) {
if (child->is_flex_array)
return child;
}
list_for_each_entry(child, &m->children, node) {
struct annotated_member *grand_child;
grand_child = find_flex_array(child);
if (grand_child)
return grand_child;
}
return NULL;
}
child = list_last_entry(&m->children, struct annotated_member, node);
if (child->is_flex_array)
return child;
return find_flex_array(child);
}
static struct annotated_member *get_flex_array_member(struct annotated_data_type *adt)
{
return find_flex_array(&adt->self);
}
static int fill_member_name(char *buf, size_t sz, struct annotated_member *m,
int offset, bool first, bool has_flex_array)
{
struct annotated_member *child;
bool found = false;
int len;
if (list_empty(&m->children))
return 0;
list_for_each_entry(child, &m->children, node) {
if (offset < child->offset || offset >= child->offset + child->size)
continue;
found = true;
break;
}
if (!found && has_flex_array) {
/*
* It may have an intermediate struct that has another struct that
* contains a flex array. In that case, the outer struct itself is
* has no array and the size is less than the offset so the above
* logic won't find the outer struct at the offset.
*/
child = find_flex_array(m);
if (child == NULL || offset < child->offset)
return 0;
/* find the immediate child that includes a flex array */
if (m->is_union) {
list_for_each_entry(child, &m->children, node) {
if (child->is_flex_array || find_flex_array(child)) {
found = true;
break;
}
}
} else {
child = list_last_entry(&m->children, struct annotated_member, node);
found = true;
}
}
if (found) {
/* It can have anonymous struct/union members */
if (child->var_name) {
len = scnprintf(buf, sz, "%s%s",
first ? "" : ".", child->var_name);
first = false;
} else {
len = 0;
}
return fill_member_name(buf + len, sz - len, child, offset, first,
has_flex_array) + len;
}
return 0;
}
int annotated_data_type__get_member_name(struct annotated_data_type *adt,
char *buf, size_t sz, int member_offset)
{
return fill_member_name(buf, sz, &adt->self, member_offset, /*first=*/true,
adt->flex_array);
}
static struct annotated_data_type *dso__findnew_data_type(struct dso *dso,
Dwarf_Die *type_die)
{
struct annotated_data_type *result = NULL;
struct annotated_data_type key;
struct rb_node *node;
struct strbuf sb;
char *type_name;
Dwarf_Word size;
strbuf_init(&sb, 32);
if (die_get_typename_from_type(type_die, &sb) < 0)
strbuf_add(&sb, "(unknown type)", 14);
type_name = strbuf_detach(&sb, NULL);
if (dwarf_tag(type_die) == DW_TAG_typedef)
die_get_real_type(type_die, type_die);
if (dwarf_aggregate_size(type_die, &size) != 0)
size = 0;
/* Check existing nodes in dso->data_types tree */
key.self.type_name = type_name;
key.self.size = size;
node = rb_find(&key, dso__data_types(dso), data_type_cmp);
if (node) {
result = rb_entry(node, struct annotated_data_type, node);
free(type_name);
return result;
}
/* If not, add a new one */
result = zalloc(sizeof(*result));
if (result == NULL) {
free(type_name);
return NULL;
}
result->self.type_name = type_name;
result->self.size = size;
INIT_LIST_HEAD(&result->self.children);
result->flex_array = die_has_flex_array(type_die);
if (symbol_conf.annotate_data_member)
add_member_types(result, type_die);
rb_add(&result->node, dso__data_types(dso), data_type_less);
return result;
}
static bool find_cu_die(struct debuginfo *di, u64 pc, Dwarf_Die *cu_die)
{
Dwarf_Off off, next_off;
size_t header_size;
if (dwarf_addrdie(di->dbg, pc, cu_die) != NULL)
return cu_die;
/*
* There are some kernels don't have full aranges and contain only a few
* aranges entries. Fallback to iterate all CU entries in .debug_info
* in case it's missing.
*/
off = 0;
while (dwarf_nextcu(di->dbg, off, &next_off, &header_size,
NULL, NULL, NULL) == 0) {
if (dwarf_offdie(di->dbg, off + header_size, cu_die) &&
dwarf_haspc(cu_die, pc))
return true;
off = next_off;
}
return false;
}
enum type_match_result {
PERF_TMR_UNKNOWN = 0,
PERF_TMR_OK,
PERF_TMR_NO_TYPE,
PERF_TMR_NO_POINTER,
PERF_TMR_NO_SIZE,
PERF_TMR_BAD_OFFSET,
PERF_TMR_BAIL_OUT,
};
static const char *match_result_str(enum type_match_result tmr)
{
switch (tmr) {
case PERF_TMR_OK:
return "Good!";
case PERF_TMR_NO_TYPE:
return "no type information";
case PERF_TMR_NO_POINTER:
return "no/void pointer";
case PERF_TMR_NO_SIZE:
return "type size is unknown";
case PERF_TMR_BAD_OFFSET:
return "offset bigger than size";
case PERF_TMR_UNKNOWN:
case PERF_TMR_BAIL_OUT:
default:
return "invalid state";
}
}
static bool is_compound_type(Dwarf_Die *type_die)
{
int tag = dwarf_tag(type_die);
return tag == DW_TAG_structure_type || tag == DW_TAG_union_type;
}
/* returns if Type B has better information than Type A */
static bool is_better_type(Dwarf_Die *type_a, Dwarf_Die *type_b)
{
Dwarf_Word size_a, size_b;
Dwarf_Die die_a, die_b;
Dwarf_Die ptr_a, ptr_b;
Dwarf_Die *ptr_type_a, *ptr_type_b;
ptr_type_a = die_get_pointer_type(type_a, &ptr_a);
ptr_type_b = die_get_pointer_type(type_b, &ptr_b);
/* pointer type is preferred */
if ((ptr_type_a != NULL) != (ptr_type_b != NULL))
return ptr_type_b != NULL;
if (ptr_type_b) {
/*
* We want to compare the target type, but 'void *' can fail to
* get the target type.
*/
if (die_get_real_type(ptr_type_a, &die_a) == NULL)
return true;
if (die_get_real_type(ptr_type_b, &die_b) == NULL)
return false;
type_a = &die_a;
type_b = &die_b;
}
/* bigger type is preferred */
if (dwarf_aggregate_size(type_a, &size_a) < 0 ||
dwarf_aggregate_size(type_b, &size_b) < 0)
return false;
if (size_a != size_b)
return size_a < size_b;
/* struct or union is preferred */
if (is_compound_type(type_a) != is_compound_type(type_b))
return is_compound_type(type_b);
/* typedef is preferred */
if (dwarf_tag(type_b) == DW_TAG_typedef)
return true;
return false;
}
static enum type_match_result check_type_offset(Dwarf_Die *type_die, int offset)
{
Dwarf_Word size;
/* Get the size of the actual type */
if (dwarf_aggregate_size(type_die, &size) < 0)
return PERF_TMR_NO_SIZE;
/* Minimal sanity check */
if (offset < 0)
return PERF_TMR_BAD_OFFSET;
if ((unsigned)offset >= size && !die_has_flex_array(type_die))
return PERF_TMR_BAD_OFFSET;
return PERF_TMR_OK;
}
/* The type info will be saved in @type_die */
static enum type_match_result check_variable(struct data_loc_info *dloc,
Dwarf_Die *var_die,
Dwarf_Die *type_die, int reg,
int offset, bool is_fbreg)
{
bool needs_pointer = true;
Dwarf_Die sized_type;
if (reg == DWARF_REG_PC)
needs_pointer = false;
else if (reg == dloc->fbreg || is_fbreg)
needs_pointer = false;
else if (arch__is_x86(dloc->arch) && reg == X86_REG_SP)
needs_pointer = false;
/* Get the type of the variable */
if (__die_get_real_type(var_die, type_die) == NULL)
return PERF_TMR_NO_TYPE;
/*
* Usually it expects a pointer type for a memory access.
* Convert to a real type it points to. But global variables
* and local variables are accessed directly without a pointer.
*/
if (needs_pointer) {
if (die_get_pointer_type(type_die, type_die) == NULL ||
__die_get_real_type(type_die, type_die) == NULL)
return PERF_TMR_NO_POINTER;
}
if (dwarf_tag(type_die) == DW_TAG_typedef)
die_get_real_type(type_die, &sized_type);
else
sized_type = *type_die;
return check_type_offset(&sized_type, offset);
}
struct type_state_stack *find_stack_state(struct type_state *state,
int offset)
{
struct type_state_stack *stack;
list_for_each_entry(stack, &state->stack_vars, list) {
if (offset == stack->offset)
return stack;
if (stack->compound && stack->offset < offset &&
offset < stack->offset + stack->size)
return stack;
}
return NULL;
}
void set_stack_state(struct type_state_stack *stack, int offset, u8 kind,
Dwarf_Die *type_die, int ptr_offset, u64 imm_value)
{
int tag;
Dwarf_Word size;
if (kind == TSR_KIND_POINTER) {
/* TODO: arch-dependent pointer size */
size = sizeof(void *);
}
else if (dwarf_aggregate_size(type_die, &size) < 0)
size = 0;
stack->type = *type_die;
stack->size = size;
stack->offset = offset;
stack->ptr_offset = ptr_offset;
stack->kind = kind;
stack->imm_value = imm_value;
if (kind == TSR_KIND_POINTER || kind == TSR_KIND_CONST ||
kind == TSR_KIND_GLOBAL_ADDR) {
stack->compound = false;
return;
}
tag = dwarf_tag(type_die);
switch (tag) {
case DW_TAG_structure_type:
case DW_TAG_union_type:
stack->compound = (kind != TSR_KIND_PERCPU_POINTER);
break;
default:
stack->compound = false;
break;
}
}
struct type_state_stack *findnew_stack_state(struct type_state *state,
int offset, u8 kind,
Dwarf_Die *type_die,
int ptr_offset, u64 imm_value)
{
struct type_state_stack *stack = find_stack_state(state, offset);
if (stack) {
set_stack_state(stack, offset, kind, type_die, ptr_offset, imm_value);
return stack;
}
stack = malloc(sizeof(*stack));
if (stack) {
set_stack_state(stack, offset, kind, type_die, ptr_offset, imm_value);
list_add(&stack->list, &state->stack_vars);
}
return stack;
}
void delete_stack_state(struct type_state *state, int offset)
{
struct type_state_stack *stack = find_stack_state(state, offset);
if (stack) {
list_del(&stack->list);
free(stack);
}
}
void invalidate_reg_state(struct type_state_reg *reg)
{
reg->kind = TSR_KIND_INVALID;
reg->ok = false;
reg->lifetime_active = false;
reg->lifetime_end = 0;
reg->copied_from = -1;
}
/* Maintain a cache for quick global variable lookup */
struct global_var_entry {
struct rb_node node;
char *name;
u64 start;
u64 end;
u64 die_offset;
int die_tag;
bool from_alt; /* die_offset is relative to the alt (dwz) file */
};
static int global_var_cmp(const void *_key, const struct rb_node *node)
{
const u64 addr = (uintptr_t)_key;
struct global_var_entry *gvar;
gvar = rb_entry(node, struct global_var_entry, node);
if (gvar->start <= addr && addr < gvar->end)
return 0;
return gvar->start > addr ? -1 : 1;
}
static bool global_var_less(struct rb_node *node_a, const struct rb_node *node_b)
{
struct global_var_entry *gvar_a, *gvar_b;
gvar_a = rb_entry(node_a, struct global_var_entry, node);
gvar_b = rb_entry(node_b, struct global_var_entry, node);
return gvar_a->start < gvar_b->start;
}
static struct global_var_entry *global_var__find(struct data_loc_info *dloc, u64 addr)
{
struct dso *dso = map__dso(dloc->ms->map);
struct rb_node *node;
node = rb_find((void *)(uintptr_t)addr, dso__global_vars(dso), global_var_cmp);
if (node == NULL)
return NULL;
return rb_entry(node, struct global_var_entry, node);
}
static bool global_var__add(struct data_loc_info *dloc, u64 addr,
const char *name, Dwarf_Die *type_die, bool from_alt)
{
struct dso *dso = map__dso(dloc->ms->map);
struct global_var_entry *gvar;
Dwarf_Word size;
if (dwarf_aggregate_size(type_die, &size) < 0)
return false;
gvar = malloc(sizeof(*gvar));
if (gvar == NULL)
return false;
gvar->name = name ? strdup(name) : NULL;
if (name && gvar->name == NULL) {
free(gvar);
return false;
}
gvar->start = addr;
gvar->end = addr + size;
gvar->die_offset = dwarf_dieoffset(type_die);
gvar->die_tag = dwarf_tag(type_die);
gvar->from_alt = from_alt;
rb_add(&gvar->node, dso__global_vars(dso), global_var_less);
return true;
}
void global_var_type__tree_delete(struct rb_root *root)
{
struct global_var_entry *gvar;
while (!RB_EMPTY_ROOT(root)) {
struct rb_node *node = rb_first(root);
rb_erase(node, root);
gvar = rb_entry(node, struct global_var_entry, node);
zfree(&gvar->name);
free(gvar);
}
}
bool get_global_var_info(struct data_loc_info *dloc, u64 addr,
const char **var_name, int *var_offset)
{
struct addr_location al;
struct symbol *sym;
u64 mem_addr;
/* Kernel symbols might be relocated */
mem_addr = addr + map__reloc(dloc->ms->map);
addr_location__init(&al);
sym = thread__find_symbol_fb(dloc->thread, dloc->cpumode,
mem_addr, &al);
if (sym) {
*var_name = sym->name;
/* Calculate type offset from the start of variable */
*var_offset = mem_addr - map__unmap_ip(al.map, sym->start);
} else {
*var_name = NULL;
}
addr_location__exit(&al);
if (*var_name == NULL)
return false;
return true;
}
static void global_var__collect(struct data_loc_info *dloc)
{
Dwarf *dwarf = dloc->di->dbg;
Dwarf_Off off, next_off;
Dwarf_Die cu_die, type_die;
size_t header_size;
/* Iterate all CU and collect global variables that have no location in a register. */
off = 0;
while (dwarf_nextcu(dwarf, off, &next_off, &header_size,
NULL, NULL, NULL) == 0) {
struct die_var_type *var_types = NULL;
struct die_var_type *pos;
if (dwarf_offdie(dwarf, off + header_size, &cu_die) == NULL) {
off = next_off;
continue;
}
die_collect_global_vars(&cu_die, &var_types);
for (pos = var_types; pos; pos = pos->next) {
const char *var_name = NULL;
int var_offset = 0;
if (pos->reg != -1)
continue;
if (!die_get_type_die(dwarf, pos->die_off, pos->die_tag,
pos->from_alt, &type_die))
continue;
get_global_var_info(dloc, pos->addr, &var_name, &var_offset);
global_var__add(dloc, pos->addr, var_name, &type_die,
pos->from_alt);
}
delete_var_types(var_types);
off = next_off;
}
}
bool get_global_var_type(Dwarf_Die *cu_die, struct data_loc_info *dloc,
u64 ip, u64 var_addr, int *var_offset,
Dwarf_Die *type_die)
{
u64 pc;
int offset;
const char *var_name = NULL;
struct global_var_entry *gvar;
struct dso *dso = map__dso(dloc->ms->map);
Dwarf_Die var_die;
if (RB_EMPTY_ROOT(dso__global_vars(dso)))
global_var__collect(dloc);
gvar = global_var__find(dloc, var_addr);
if (gvar) {
if (!die_get_type_die(dloc->di->dbg, gvar->die_offset,
gvar->die_tag, gvar->from_alt, type_die))
return false;
*var_offset = var_addr - gvar->start;
return true;
}
/* Try to get the variable by address first */
if (die_find_variable_by_addr(cu_die, var_addr, &var_die, type_die,
&offset)) {
var_name = dwarf_diename(&var_die);
*var_offset = offset;
goto ok;
}
if (!get_global_var_info(dloc, var_addr, &var_name, var_offset))
return false;
pc = map__rip_2objdump(dloc->ms->map, ip);
/* Try to get the name of global variable */
if (die_find_variable_at(cu_die, var_name, pc, &var_die) &&
check_variable(dloc, &var_die, type_die, DWARF_REG_PC, *var_offset,
/*is_fbreg=*/false) == PERF_TMR_OK)
goto ok;
return false;
ok:
/* The address should point to the start of the variable */
global_var__add(dloc, var_addr - *var_offset, var_name, type_die,
!die_same_file(cu_die, type_die));
return true;
}
static bool die_is_same(Dwarf_Die *die_a, Dwarf_Die *die_b)
{