1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Convert sample address to data type using DWARF debug info.
11 #include <linux/zalloc.h>
14 #include "annotate-data.h"
15 #include "debuginfo.h"
18 #include "dwarf-regs.h"
22 #include "map_symbol.h"
26 #include "symbol_conf.h"
29 /* register number of the stack pointer */
32 static void delete_var_types(struct die_var_type *var_types);
34 enum type_state_kind {
43 #define pr_debug_dtp(fmt, ...) \
45 if (debug_type_profile) \
46 pr_info(fmt, ##__VA_ARGS__); \
48 pr_debug3(fmt, ##__VA_ARGS__); \
51 static void pr_debug_type_name(Dwarf_Die *die, enum type_state_kind kind)
57 if (!debug_type_profile && verbose < 3)
61 case TSR_KIND_INVALID:
64 case TSR_KIND_PERCPU_BASE:
65 pr_info(" percpu base\n");
68 pr_info(" constant\n");
70 case TSR_KIND_POINTER:
72 /* it also prints the type info */
75 pr_info(" stack canary\n");
82 dwarf_aggregate_size(die, &size);
85 die_get_typename_from_type(die, &sb);
86 str = strbuf_detach(&sb, NULL);
87 pr_info(" type='%s' size=%#lx (die:%#lx)\n",
88 str, (long)size, (long)dwarf_dieoffset(die));
92 static void pr_debug_location(Dwarf_Die *die, u64 pc, int reg)
96 Dwarf_Addr base, start, end;
100 if (!debug_type_profile && verbose < 3)
103 if (dwarf_attr(die, DW_AT_location, &attr) == NULL)
106 while ((off = dwarf_getlocations(&attr, off, &base, &start, &end, &ops, &nops)) > 0) {
107 if (reg != DWARF_REG_PC && end < pc)
109 if (reg != DWARF_REG_PC && start > pc)
112 pr_info(" variable location: ");
114 case DW_OP_reg0 ...DW_OP_reg31:
115 pr_info("reg%d\n", ops->atom - DW_OP_reg0);
117 case DW_OP_breg0 ...DW_OP_breg31:
118 pr_info("base=reg%d, offset=%#lx\n",
119 ops->atom - DW_OP_breg0, (long)ops->number);
122 pr_info("reg%ld\n", (long)ops->number);
125 pr_info("base=reg%ld, offset=%#lx\n",
126 (long)ops->number, (long)ops->number2);
129 pr_info("use frame base, offset=%#lx\n", (long)ops->number);
132 pr_info("address=%#lx\n", (long)ops->number);
135 pr_info("unknown: code=%#x, number=%#lx\n",
136 ops->atom, (long)ops->number);
144 * Type information in a register, valid when @ok is true.
145 * The @caller_saved registers are invalidated after a function call.
147 struct type_state_reg {
155 /* Type information in a stack location, dynamically allocated */
156 struct type_state_stack {
157 struct list_head list;
165 /* FIXME: This should be arch-dependent */
166 #define TYPE_STATE_MAX_REGS 16
169 * State table to maintain type info in each register and stack location.
170 * It'll be updated when new variable is allocated or type info is moved
171 * to a new location (register or stack). As it'd be used with the
172 * shortest path of basic blocks, it only maintains a single table.
175 /* state of general purpose registers */
176 struct type_state_reg regs[TYPE_STATE_MAX_REGS];
177 /* state of stack location */
178 struct list_head stack_vars;
179 /* return value register */
181 /* stack pointer register */
185 static bool has_reg_type(struct type_state *state, int reg)
187 return (unsigned)reg < ARRAY_SIZE(state->regs);
190 static void init_type_state(struct type_state *state, struct arch *arch)
192 memset(state, 0, sizeof(*state));
193 INIT_LIST_HEAD(&state->stack_vars);
195 if (arch__is(arch, "x86")) {
196 state->regs[0].caller_saved = true;
197 state->regs[1].caller_saved = true;
198 state->regs[2].caller_saved = true;
199 state->regs[4].caller_saved = true;
200 state->regs[5].caller_saved = true;
201 state->regs[8].caller_saved = true;
202 state->regs[9].caller_saved = true;
203 state->regs[10].caller_saved = true;
204 state->regs[11].caller_saved = true;
206 state->stack_reg = X86_REG_SP;
210 static void exit_type_state(struct type_state *state)
212 struct type_state_stack *stack, *tmp;
214 list_for_each_entry_safe(stack, tmp, &state->stack_vars, list) {
215 list_del(&stack->list);
221 * Compare type name and size to maintain them in a tree.
222 * I'm not sure if DWARF would have information of a single type in many
223 * different places (compilation units). If not, it could compare the
224 * offset of the type entry in the .debug_info section.
226 static int data_type_cmp(const void *_key, const struct rb_node *node)
228 const struct annotated_data_type *key = _key;
229 struct annotated_data_type *type;
231 type = rb_entry(node, struct annotated_data_type, node);
233 if (key->self.size != type->self.size)
234 return key->self.size - type->self.size;
235 return strcmp(key->self.type_name, type->self.type_name);
238 static bool data_type_less(struct rb_node *node_a, const struct rb_node *node_b)
240 struct annotated_data_type *a, *b;
242 a = rb_entry(node_a, struct annotated_data_type, node);
243 b = rb_entry(node_b, struct annotated_data_type, node);
245 if (a->self.size != b->self.size)
246 return a->self.size < b->self.size;
247 return strcmp(a->self.type_name, b->self.type_name) < 0;
250 /* Recursively add new members for struct/union */
251 static int __add_member_cb(Dwarf_Die *die, void *arg)
253 struct annotated_member *parent = arg;
254 struct annotated_member *member;
255 Dwarf_Die member_type, die_mem;
256 Dwarf_Word size, loc;
257 Dwarf_Attribute attr;
261 if (dwarf_tag(die) != DW_TAG_member)
262 return DIE_FIND_CB_SIBLING;
264 member = zalloc(sizeof(*member));
266 return DIE_FIND_CB_END;
268 strbuf_init(&sb, 32);
269 die_get_typename(die, &sb);
271 die_get_real_type(die, &member_type);
272 if (dwarf_aggregate_size(&member_type, &size) < 0)
275 if (!dwarf_attr_integrate(die, DW_AT_data_member_location, &attr))
278 dwarf_formudata(&attr, &loc);
280 member->type_name = strbuf_detach(&sb, NULL);
281 /* member->var_name can be NULL */
282 if (dwarf_diename(die))
283 member->var_name = strdup(dwarf_diename(die));
285 member->offset = loc + parent->offset;
286 INIT_LIST_HEAD(&member->children);
287 list_add_tail(&member->node, &parent->children);
289 tag = dwarf_tag(&member_type);
291 case DW_TAG_structure_type:
292 case DW_TAG_union_type:
293 die_find_child(&member_type, __add_member_cb, member, &die_mem);
298 return DIE_FIND_CB_SIBLING;
301 static void add_member_types(struct annotated_data_type *parent, Dwarf_Die *type)
305 die_find_child(type, __add_member_cb, &parent->self, &die_mem);
308 static void delete_members(struct annotated_member *member)
310 struct annotated_member *child, *tmp;
312 list_for_each_entry_safe(child, tmp, &member->children, node) {
313 list_del(&child->node);
314 delete_members(child);
315 zfree(&child->type_name);
316 zfree(&child->var_name);
321 static struct annotated_data_type *dso__findnew_data_type(struct dso *dso,
324 struct annotated_data_type *result = NULL;
325 struct annotated_data_type key;
326 struct rb_node *node;
331 strbuf_init(&sb, 32);
332 if (die_get_typename_from_type(type_die, &sb) < 0)
333 strbuf_add(&sb, "(unknown type)", 14);
334 type_name = strbuf_detach(&sb, NULL);
335 dwarf_aggregate_size(type_die, &size);
337 /* Check existing nodes in dso->data_types tree */
338 key.self.type_name = type_name;
339 key.self.size = size;
340 node = rb_find(&key, dso__data_types(dso), data_type_cmp);
342 result = rb_entry(node, struct annotated_data_type, node);
347 /* If not, add a new one */
348 result = zalloc(sizeof(*result));
349 if (result == NULL) {
354 result->self.type_name = type_name;
355 result->self.size = size;
356 INIT_LIST_HEAD(&result->self.children);
358 if (symbol_conf.annotate_data_member)
359 add_member_types(result, type_die);
361 rb_add(&result->node, dso__data_types(dso), data_type_less);
365 static bool find_cu_die(struct debuginfo *di, u64 pc, Dwarf_Die *cu_die)
367 Dwarf_Off off, next_off;
370 if (dwarf_addrdie(di->dbg, pc, cu_die) != NULL)
374 * There are some kernels don't have full aranges and contain only a few
375 * aranges entries. Fallback to iterate all CU entries in .debug_info
376 * in case it's missing.
379 while (dwarf_nextcu(di->dbg, off, &next_off, &header_size,
380 NULL, NULL, NULL) == 0) {
381 if (dwarf_offdie(di->dbg, off + header_size, cu_die) &&
382 dwarf_haspc(cu_die, pc))
390 /* The type info will be saved in @type_die */
391 static int check_variable(struct data_loc_info *dloc, Dwarf_Die *var_die,
392 Dwarf_Die *type_die, int reg, int offset, bool is_fbreg)
395 bool is_pointer = true;
397 if (reg == DWARF_REG_PC)
399 else if (reg == dloc->fbreg || is_fbreg)
401 else if (arch__is(dloc->arch, "x86") && reg == X86_REG_SP)
404 /* Get the type of the variable */
405 if (die_get_real_type(var_die, type_die) == NULL) {
406 pr_debug_dtp("variable has no type\n");
407 ann_data_stat.no_typeinfo++;
412 * Usually it expects a pointer type for a memory access.
413 * Convert to a real type it points to. But global variables
414 * and local variables are accessed directly without a pointer.
417 if ((dwarf_tag(type_die) != DW_TAG_pointer_type &&
418 dwarf_tag(type_die) != DW_TAG_array_type) ||
419 die_get_real_type(type_die, type_die) == NULL) {
420 pr_debug_dtp("no pointer or no type\n");
421 ann_data_stat.no_typeinfo++;
426 /* Get the size of the actual type */
427 if (dwarf_aggregate_size(type_die, &size) < 0) {
428 pr_debug_dtp("type size is unknown\n");
429 ann_data_stat.invalid_size++;
433 /* Minimal sanity check */
434 if ((unsigned)offset >= size) {
435 pr_debug_dtp("offset: %d is bigger than size: %"PRIu64"\n",
437 ann_data_stat.bad_offset++;
444 static struct type_state_stack *find_stack_state(struct type_state *state,
447 struct type_state_stack *stack;
449 list_for_each_entry(stack, &state->stack_vars, list) {
450 if (offset == stack->offset)
453 if (stack->compound && stack->offset < offset &&
454 offset < stack->offset + stack->size)
460 static void set_stack_state(struct type_state_stack *stack, int offset, u8 kind,
466 if (dwarf_aggregate_size(type_die, &size) < 0)
469 tag = dwarf_tag(type_die);
471 stack->type = *type_die;
473 stack->offset = offset;
477 case DW_TAG_structure_type:
478 case DW_TAG_union_type:
479 stack->compound = (kind != TSR_KIND_POINTER);
482 stack->compound = false;
487 static struct type_state_stack *findnew_stack_state(struct type_state *state,
491 struct type_state_stack *stack = find_stack_state(state, offset);
494 set_stack_state(stack, offset, kind, type_die);
498 stack = malloc(sizeof(*stack));
500 set_stack_state(stack, offset, kind, type_die);
501 list_add(&stack->list, &state->stack_vars);
506 /* Maintain a cache for quick global variable lookup */
507 struct global_var_entry {
515 static int global_var_cmp(const void *_key, const struct rb_node *node)
517 const u64 addr = (uintptr_t)_key;
518 struct global_var_entry *gvar;
520 gvar = rb_entry(node, struct global_var_entry, node);
522 if (gvar->start <= addr && addr < gvar->end)
524 return gvar->start > addr ? -1 : 1;
527 static bool global_var_less(struct rb_node *node_a, const struct rb_node *node_b)
529 struct global_var_entry *gvar_a, *gvar_b;
531 gvar_a = rb_entry(node_a, struct global_var_entry, node);
532 gvar_b = rb_entry(node_b, struct global_var_entry, node);
534 return gvar_a->start < gvar_b->start;
537 static struct global_var_entry *global_var__find(struct data_loc_info *dloc, u64 addr)
539 struct dso *dso = map__dso(dloc->ms->map);
540 struct rb_node *node;
542 node = rb_find((void *)(uintptr_t)addr, dso__global_vars(dso), global_var_cmp);
546 return rb_entry(node, struct global_var_entry, node);
549 static bool global_var__add(struct data_loc_info *dloc, u64 addr,
550 const char *name, Dwarf_Die *type_die)
552 struct dso *dso = map__dso(dloc->ms->map);
553 struct global_var_entry *gvar;
556 if (dwarf_aggregate_size(type_die, &size) < 0)
559 gvar = malloc(sizeof(*gvar));
563 gvar->name = name ? strdup(name) : NULL;
564 if (name && gvar->name == NULL) {
570 gvar->end = addr + size;
571 gvar->die_offset = dwarf_dieoffset(type_die);
573 rb_add(&gvar->node, dso__global_vars(dso), global_var_less);
577 void global_var_type__tree_delete(struct rb_root *root)
579 struct global_var_entry *gvar;
581 while (!RB_EMPTY_ROOT(root)) {
582 struct rb_node *node = rb_first(root);
584 rb_erase(node, root);
585 gvar = rb_entry(node, struct global_var_entry, node);
591 static bool get_global_var_info(struct data_loc_info *dloc, u64 addr,
592 const char **var_name, int *var_offset)
594 struct addr_location al;
598 /* Kernel symbols might be relocated */
599 mem_addr = addr + map__reloc(dloc->ms->map);
601 addr_location__init(&al);
602 sym = thread__find_symbol_fb(dloc->thread, dloc->cpumode,
605 *var_name = sym->name;
606 /* Calculate type offset from the start of variable */
607 *var_offset = mem_addr - map__unmap_ip(al.map, sym->start);
611 addr_location__exit(&al);
612 if (*var_name == NULL)
618 static void global_var__collect(struct data_loc_info *dloc)
620 Dwarf *dwarf = dloc->di->dbg;
621 Dwarf_Off off, next_off;
622 Dwarf_Die cu_die, type_die;
625 /* Iterate all CU and collect global variables that have no location in a register. */
627 while (dwarf_nextcu(dwarf, off, &next_off, &header_size,
628 NULL, NULL, NULL) == 0) {
629 struct die_var_type *var_types = NULL;
630 struct die_var_type *pos;
632 if (dwarf_offdie(dwarf, off + header_size, &cu_die) == NULL) {
637 die_collect_global_vars(&cu_die, &var_types);
639 for (pos = var_types; pos; pos = pos->next) {
640 const char *var_name = NULL;
646 if (!dwarf_offdie(dwarf, pos->die_off, &type_die))
649 if (!get_global_var_info(dloc, pos->addr, &var_name,
656 global_var__add(dloc, pos->addr, var_name, &type_die);
659 delete_var_types(var_types);
665 static bool get_global_var_type(Dwarf_Die *cu_die, struct data_loc_info *dloc,
666 u64 ip, u64 var_addr, int *var_offset,
671 const char *var_name = NULL;
672 struct global_var_entry *gvar;
673 struct dso *dso = map__dso(dloc->ms->map);
676 if (RB_EMPTY_ROOT(dso__global_vars(dso)))
677 global_var__collect(dloc);
679 gvar = global_var__find(dloc, var_addr);
681 if (!dwarf_offdie(dloc->di->dbg, gvar->die_offset, type_die))
684 *var_offset = var_addr - gvar->start;
688 /* Try to get the variable by address first */
689 if (die_find_variable_by_addr(cu_die, var_addr, &var_die, &offset) &&
690 check_variable(dloc, &var_die, type_die, DWARF_REG_PC, offset,
691 /*is_fbreg=*/false) == 0) {
692 var_name = dwarf_diename(&var_die);
693 *var_offset = offset;
697 if (!get_global_var_info(dloc, var_addr, &var_name, var_offset))
700 pc = map__rip_2objdump(dloc->ms->map, ip);
702 /* Try to get the name of global variable */
703 if (die_find_variable_at(cu_die, var_name, pc, &var_die) &&
704 check_variable(dloc, &var_die, type_die, DWARF_REG_PC, *var_offset,
705 /*is_fbreg=*/false) == 0)
711 /* The address should point to the start of the variable */
712 global_var__add(dloc, var_addr - *var_offset, var_name, type_die);
717 * update_var_state - Update type state using given variables
718 * @state: type state table
719 * @dloc: data location info
720 * @addr: instruction address to match with variable
721 * @insn_offset: instruction offset (for debug)
722 * @var_types: list of variables with type info
724 * This function fills the @state table using @var_types info. Each variable
725 * is used only at the given location and updates an entry in the table.
727 static void update_var_state(struct type_state *state, struct data_loc_info *dloc,
728 u64 addr, u64 insn_offset, struct die_var_type *var_types)
731 struct die_var_type *var;
732 int fbreg = dloc->fbreg;
736 if (die_get_cfa(dloc->di->dbg, addr, &fbreg, &fb_offset) < 0)
740 for (var = var_types; var != NULL; var = var->next) {
741 if (var->addr != addr)
743 /* Get the type DIE using the offset */
744 if (!dwarf_offdie(dloc->di->dbg, var->die_off, &mem_die))
747 if (var->reg == DWARF_REG_FB) {
748 findnew_stack_state(state, var->offset, TSR_KIND_TYPE,
751 pr_debug_dtp("var [%"PRIx64"] -%#x(stack)",
752 insn_offset, -var->offset);
753 pr_debug_type_name(&mem_die, TSR_KIND_TYPE);
754 } else if (var->reg == fbreg) {
755 findnew_stack_state(state, var->offset - fb_offset,
756 TSR_KIND_TYPE, &mem_die);
758 pr_debug_dtp("var [%"PRIx64"] -%#x(stack)",
759 insn_offset, -var->offset + fb_offset);
760 pr_debug_type_name(&mem_die, TSR_KIND_TYPE);
761 } else if (has_reg_type(state, var->reg) && var->offset == 0) {
762 struct type_state_reg *reg;
764 reg = &state->regs[var->reg];
766 reg->kind = TSR_KIND_TYPE;
769 pr_debug_dtp("var [%"PRIx64"] reg%d",
770 insn_offset, var->reg);
771 pr_debug_type_name(&mem_die, TSR_KIND_TYPE);
776 static void update_insn_state_x86(struct type_state *state,
777 struct data_loc_info *dloc, Dwarf_Die *cu_die,
778 struct disasm_line *dl)
780 struct annotated_insn_loc loc;
781 struct annotated_op_loc *src = &loc.ops[INSN_OP_SOURCE];
782 struct annotated_op_loc *dst = &loc.ops[INSN_OP_TARGET];
783 struct type_state_reg *tsr;
785 u32 insn_offset = dl->al.offset;
786 int fbreg = dloc->fbreg;
789 if (annotate_get_insn_location(dloc->arch, dl, &loc) < 0)
792 if (ins__is_call(&dl->ins)) {
793 struct symbol *func = dl->ops.target.sym;
798 /* __fentry__ will preserve all registers */
799 if (!strcmp(func->name, "__fentry__"))
802 pr_debug_dtp("call [%x] %s\n", insn_offset, func->name);
804 /* Otherwise invalidate caller-saved registers after call */
805 for (unsigned i = 0; i < ARRAY_SIZE(state->regs); i++) {
806 if (state->regs[i].caller_saved)
807 state->regs[i].ok = false;
810 /* Update register with the return type (if any) */
811 if (die_find_func_rettype(cu_die, func->name, &type_die)) {
812 tsr = &state->regs[state->ret_reg];
813 tsr->type = type_die;
814 tsr->kind = TSR_KIND_TYPE;
817 pr_debug_dtp("call [%x] return -> reg%d",
818 insn_offset, state->ret_reg);
819 pr_debug_type_name(&type_die, tsr->kind);
824 if (!strncmp(dl->ins.name, "add", 3)) {
825 u64 imm_value = -1ULL;
827 const char *var_name = NULL;
828 struct map_symbol *ms = dloc->ms;
829 u64 ip = ms->sym->start + dl->al.offset;
831 if (!has_reg_type(state, dst->reg1))
834 tsr = &state->regs[dst->reg1];
837 imm_value = src->offset;
838 else if (has_reg_type(state, src->reg1) &&
839 state->regs[src->reg1].kind == TSR_KIND_CONST)
840 imm_value = state->regs[src->reg1].imm_value;
841 else if (src->reg1 == DWARF_REG_PC) {
842 u64 var_addr = annotate_calc_pcrel(dloc->ms, ip,
845 if (get_global_var_info(dloc, var_addr,
846 &var_name, &offset) &&
847 !strcmp(var_name, "this_cpu_off") &&
848 tsr->kind == TSR_KIND_CONST) {
849 tsr->kind = TSR_KIND_PERCPU_BASE;
850 imm_value = tsr->imm_value;
856 if (tsr->kind != TSR_KIND_PERCPU_BASE)
859 if (get_global_var_type(cu_die, dloc, ip, imm_value, &offset,
860 &type_die) && offset == 0) {
862 * This is not a pointer type, but it should be treated
865 tsr->type = type_die;
866 tsr->kind = TSR_KIND_POINTER;
869 pr_debug_dtp("add [%x] percpu %#"PRIx64" -> reg%d",
870 insn_offset, imm_value, dst->reg1);
871 pr_debug_type_name(&tsr->type, tsr->kind);
876 if (strncmp(dl->ins.name, "mov", 3))
880 u64 ip = dloc->ms->sym->start + dl->al.offset;
881 u64 pc = map__rip_2objdump(dloc->ms->map, ip);
883 if (die_get_cfa(dloc->di->dbg, pc, &fbreg, &fboff) < 0)
887 /* Case 1. register to register or segment:offset to register transfers */
888 if (!src->mem_ref && !dst->mem_ref) {
889 if (!has_reg_type(state, dst->reg1))
892 tsr = &state->regs[dst->reg1];
893 if (dso__kernel(map__dso(dloc->ms->map)) &&
894 src->segment == INSN_SEG_X86_GS && src->imm) {
895 u64 ip = dloc->ms->sym->start + dl->al.offset;
900 * In kernel, %gs points to a per-cpu region for the
901 * current CPU. Access with a constant offset should
902 * be treated as a global variable access.
904 var_addr = src->offset;
906 if (var_addr == 40) {
907 tsr->kind = TSR_KIND_CANARY;
910 pr_debug_dtp("mov [%x] stack canary -> reg%d\n",
911 insn_offset, dst->reg1);
915 if (!get_global_var_type(cu_die, dloc, ip, var_addr,
916 &offset, &type_die) ||
917 !die_get_member_type(&type_die, offset, &type_die)) {
922 tsr->type = type_die;
923 tsr->kind = TSR_KIND_TYPE;
926 pr_debug_dtp("mov [%x] this-cpu addr=%#"PRIx64" -> reg%d",
927 insn_offset, var_addr, dst->reg1);
928 pr_debug_type_name(&tsr->type, tsr->kind);
933 tsr->kind = TSR_KIND_CONST;
934 tsr->imm_value = src->offset;
937 pr_debug_dtp("mov [%x] imm=%#x -> reg%d\n",
938 insn_offset, tsr->imm_value, dst->reg1);
942 if (!has_reg_type(state, src->reg1) ||
943 !state->regs[src->reg1].ok) {
948 tsr->type = state->regs[src->reg1].type;
949 tsr->kind = state->regs[src->reg1].kind;
952 pr_debug_dtp("mov [%x] reg%d -> reg%d",
953 insn_offset, src->reg1, dst->reg1);
954 pr_debug_type_name(&tsr->type, tsr->kind);
956 /* Case 2. memory to register transers */
957 if (src->mem_ref && !dst->mem_ref) {
958 int sreg = src->reg1;
960 if (!has_reg_type(state, dst->reg1))
963 tsr = &state->regs[dst->reg1];
966 /* Check stack variables with offset */
968 struct type_state_stack *stack;
969 int offset = src->offset - fboff;
971 stack = find_stack_state(state, offset);
975 } else if (!stack->compound) {
976 tsr->type = stack->type;
977 tsr->kind = stack->kind;
979 } else if (die_get_member_type(&stack->type,
980 offset - stack->offset,
982 tsr->type = type_die;
983 tsr->kind = TSR_KIND_TYPE;
990 pr_debug_dtp("mov [%x] -%#x(stack) -> reg%d",
991 insn_offset, -offset, dst->reg1);
992 pr_debug_type_name(&tsr->type, tsr->kind);
994 /* And then dereference the pointer if it has one */
995 else if (has_reg_type(state, sreg) && state->regs[sreg].ok &&
996 state->regs[sreg].kind == TSR_KIND_TYPE &&
997 die_deref_ptr_type(&state->regs[sreg].type,
998 src->offset, &type_die)) {
999 tsr->type = type_die;
1000 tsr->kind = TSR_KIND_TYPE;
1003 pr_debug_dtp("mov [%x] %#x(reg%d) -> reg%d",
1004 insn_offset, src->offset, sreg, dst->reg1);
1005 pr_debug_type_name(&tsr->type, tsr->kind);
1007 /* Or check if it's a global variable */
1008 else if (sreg == DWARF_REG_PC) {
1009 struct map_symbol *ms = dloc->ms;
1010 u64 ip = ms->sym->start + dl->al.offset;
1014 addr = annotate_calc_pcrel(ms, ip, src->offset, dl);
1016 if (!get_global_var_type(cu_die, dloc, ip, addr, &offset,
1018 !die_get_member_type(&type_die, offset, &type_die)) {
1023 tsr->type = type_die;
1024 tsr->kind = TSR_KIND_TYPE;
1027 pr_debug_dtp("mov [%x] global addr=%"PRIx64" -> reg%d",
1028 insn_offset, addr, dst->reg1);
1029 pr_debug_type_name(&type_die, tsr->kind);
1031 /* And check percpu access with base register */
1032 else if (has_reg_type(state, sreg) &&
1033 state->regs[sreg].kind == TSR_KIND_PERCPU_BASE) {
1034 u64 ip = dloc->ms->sym->start + dl->al.offset;
1035 u64 var_addr = src->offset;
1038 if (src->multi_regs) {
1039 int reg2 = (sreg == src->reg1) ? src->reg2 : src->reg1;
1041 if (has_reg_type(state, reg2) && state->regs[reg2].ok &&
1042 state->regs[reg2].kind == TSR_KIND_CONST)
1043 var_addr += state->regs[reg2].imm_value;
1047 * In kernel, %gs points to a per-cpu region for the
1048 * current CPU. Access with a constant offset should
1049 * be treated as a global variable access.
1051 if (get_global_var_type(cu_die, dloc, ip, var_addr,
1052 &offset, &type_die) &&
1053 die_get_member_type(&type_die, offset, &type_die)) {
1054 tsr->type = type_die;
1055 tsr->kind = TSR_KIND_TYPE;
1058 if (src->multi_regs) {
1059 pr_debug_dtp("mov [%x] percpu %#x(reg%d,reg%d) -> reg%d",
1060 insn_offset, src->offset, src->reg1,
1061 src->reg2, dst->reg1);
1063 pr_debug_dtp("mov [%x] percpu %#x(reg%d) -> reg%d",
1064 insn_offset, src->offset, sreg, dst->reg1);
1066 pr_debug_type_name(&tsr->type, tsr->kind);
1071 /* And then dereference the calculated pointer if it has one */
1072 else if (has_reg_type(state, sreg) && state->regs[sreg].ok &&
1073 state->regs[sreg].kind == TSR_KIND_POINTER &&
1074 die_get_member_type(&state->regs[sreg].type,
1075 src->offset, &type_die)) {
1076 tsr->type = type_die;
1077 tsr->kind = TSR_KIND_TYPE;
1080 pr_debug_dtp("mov [%x] pointer %#x(reg%d) -> reg%d",
1081 insn_offset, src->offset, sreg, dst->reg1);
1082 pr_debug_type_name(&tsr->type, tsr->kind);
1084 /* Or try another register if any */
1085 else if (src->multi_regs && sreg == src->reg1 &&
1086 src->reg1 != src->reg2) {
1092 const char *var_name = NULL;
1094 /* it might be per-cpu variable (in kernel) access */
1095 if (src->offset < 0) {
1096 if (get_global_var_info(dloc, (s64)src->offset,
1097 &var_name, &offset) &&
1098 !strcmp(var_name, "__per_cpu_offset")) {
1099 tsr->kind = TSR_KIND_PERCPU_BASE;
1101 pr_debug_dtp("mov [%x] percpu base reg%d\n",
1102 insn_offset, dst->reg1);
1109 /* Case 3. register to memory transfers */
1110 if (!src->mem_ref && dst->mem_ref) {
1111 if (!has_reg_type(state, src->reg1) ||
1112 !state->regs[src->reg1].ok)
1115 /* Check stack variables with offset */
1116 if (dst->reg1 == fbreg) {
1117 struct type_state_stack *stack;
1118 int offset = dst->offset - fboff;
1120 tsr = &state->regs[src->reg1];
1122 stack = find_stack_state(state, offset);
1125 * The source register is likely to hold a type
1126 * of member if it's a compound type. Do not
1127 * update the stack variable type since we can
1128 * get the member type later by using the
1129 * die_get_member_type().
1131 if (!stack->compound)
1132 set_stack_state(stack, offset, tsr->kind,
1135 findnew_stack_state(state, offset, tsr->kind,
1139 pr_debug_dtp("mov [%x] reg%d -> -%#x(stack)",
1140 insn_offset, src->reg1, -offset);
1141 pr_debug_type_name(&tsr->type, tsr->kind);
1144 * Ignore other transfers since it'd set a value in a struct
1145 * and won't change the type.
1148 /* Case 4. memory to memory transfers (not handled for now) */
1152 * update_insn_state - Update type state for an instruction
1153 * @state: type state table
1154 * @dloc: data location info
1155 * @cu_die: compile unit debug entry
1156 * @dl: disasm line for the instruction
1158 * This function updates the @state table for the target operand of the
1159 * instruction at @dl if it transfers the type like MOV on x86. Since it
1160 * tracks the type, it won't care about the values like in arithmetic
1161 * instructions like ADD/SUB/MUL/DIV and INC/DEC.
1163 * Note that ops->reg2 is only available when both mem_ref and multi_regs
1166 static void update_insn_state(struct type_state *state, struct data_loc_info *dloc,
1167 Dwarf_Die *cu_die, struct disasm_line *dl)
1169 if (arch__is(dloc->arch, "x86"))
1170 update_insn_state_x86(state, dloc, cu_die, dl);
1174 * Prepend this_blocks (from the outer scope) to full_blocks, removing
1175 * duplicate disasm line.
1177 static void prepend_basic_blocks(struct list_head *this_blocks,
1178 struct list_head *full_blocks)
1180 struct annotated_basic_block *first_bb, *last_bb;
1182 last_bb = list_last_entry(this_blocks, typeof(*last_bb), list);
1183 first_bb = list_first_entry(full_blocks, typeof(*first_bb), list);
1185 if (list_empty(full_blocks))
1188 /* Last insn in this_blocks should be same as first insn in full_blocks */
1189 if (last_bb->end != first_bb->begin) {
1190 pr_debug("prepend basic blocks: mismatched disasm line %"PRIx64" -> %"PRIx64"\n",
1191 last_bb->end->al.offset, first_bb->begin->al.offset);
1195 /* Is the basic block have only one disasm_line? */
1196 if (last_bb->begin == last_bb->end) {
1197 list_del(&last_bb->list);
1202 /* Point to the insn before the last when adding this block to full_blocks */
1203 last_bb->end = list_prev_entry(last_bb->end, al.node);
1206 list_splice(this_blocks, full_blocks);
1209 static void delete_basic_blocks(struct list_head *basic_blocks)
1211 struct annotated_basic_block *bb, *tmp;
1213 list_for_each_entry_safe(bb, tmp, basic_blocks, list) {
1214 list_del(&bb->list);
1219 /* Make sure all variables have a valid start address */
1220 static void fixup_var_address(struct die_var_type *var_types, u64 addr)
1224 * Some variables have no address range meaning it's always
1225 * available in the whole scope. Let's adjust the start
1226 * address to the start of the scope.
1228 if (var_types->addr == 0)
1229 var_types->addr = addr;
1231 var_types = var_types->next;
1235 static void delete_var_types(struct die_var_type *var_types)
1238 struct die_var_type *next = var_types->next;
1245 /* should match to is_stack_canary() in util/annotate.c */
1246 static void setup_stack_canary(struct data_loc_info *dloc)
1248 if (arch__is(dloc->arch, "x86")) {
1249 dloc->op->segment = INSN_SEG_X86_GS;
1250 dloc->op->imm = true;
1251 dloc->op->offset = 40;
1256 * It's at the target address, check if it has a matching type.
1257 * It returns 1 if found, 0 if not or -1 if not found but no need to
1258 * repeat the search. The last case is for per-cpu variables which
1259 * are similar to global variables and no additional info is needed.
1261 static int check_matching_type(struct type_state *state,
1262 struct data_loc_info *dloc,
1263 Dwarf_Die *cu_die, Dwarf_Die *type_die)
1266 u32 insn_offset = dloc->ip - dloc->ms->sym->start;
1267 int reg = dloc->op->reg1;
1269 pr_debug_dtp("chk [%x] reg%d offset=%#x ok=%d kind=%d",
1270 insn_offset, reg, dloc->op->offset,
1271 state->regs[reg].ok, state->regs[reg].kind);
1273 if (state->regs[reg].ok && state->regs[reg].kind == TSR_KIND_TYPE) {
1274 int tag = dwarf_tag(&state->regs[reg].type);
1277 * Normal registers should hold a pointer (or array) to
1278 * dereference a memory location.
1280 if (tag != DW_TAG_pointer_type && tag != DW_TAG_array_type) {
1281 if (dloc->op->offset < 0 && reg != state->stack_reg)
1290 /* Remove the pointer and get the target type */
1291 if (die_get_real_type(&state->regs[reg].type, type_die) == NULL)
1294 dloc->type_offset = dloc->op->offset;
1296 /* Get the size of the actual type */
1297 if (dwarf_aggregate_size(type_die, &size) < 0 ||
1298 (unsigned)dloc->type_offset >= size)
1304 if (reg == dloc->fbreg) {
1305 struct type_state_stack *stack;
1307 pr_debug_dtp(" fbreg\n");
1309 stack = find_stack_state(state, dloc->type_offset);
1313 if (stack->kind == TSR_KIND_CANARY) {
1314 setup_stack_canary(dloc);
1318 if (stack->kind != TSR_KIND_TYPE)
1321 *type_die = stack->type;
1322 /* Update the type offset from the start of slot */
1323 dloc->type_offset -= stack->offset;
1329 struct type_state_stack *stack;
1330 u64 pc = map__rip_2objdump(dloc->ms->map, dloc->ip);
1333 pr_debug_dtp(" cfa\n");
1335 if (die_get_cfa(dloc->di->dbg, pc, &fbreg, &fboff) < 0)
1341 stack = find_stack_state(state, dloc->type_offset - fboff);
1345 if (stack->kind == TSR_KIND_CANARY) {
1346 setup_stack_canary(dloc);
1350 if (stack->kind != TSR_KIND_TYPE)
1353 *type_die = stack->type;
1354 /* Update the type offset from the start of slot */
1355 dloc->type_offset -= fboff + stack->offset;
1360 if (state->regs[reg].kind == TSR_KIND_PERCPU_BASE) {
1361 u64 var_addr = dloc->op->offset;
1364 pr_debug_dtp(" percpu var\n");
1366 if (dloc->op->multi_regs) {
1367 int reg2 = dloc->op->reg2;
1369 if (dloc->op->reg2 == reg)
1370 reg2 = dloc->op->reg1;
1372 if (has_reg_type(state, reg2) && state->regs[reg2].ok &&
1373 state->regs[reg2].kind == TSR_KIND_CONST)
1374 var_addr += state->regs[reg2].imm_value;
1377 if (get_global_var_type(cu_die, dloc, dloc->ip, var_addr,
1378 &var_offset, type_die)) {
1379 dloc->type_offset = var_offset;
1382 /* No need to retry per-cpu (global) variables */
1386 if (state->regs[reg].ok && state->regs[reg].kind == TSR_KIND_POINTER) {
1387 pr_debug_dtp(" percpu ptr\n");
1390 * It's actaully pointer but the address was calculated using
1391 * some arithmetic. So it points to the actual type already.
1393 *type_die = state->regs[reg].type;
1395 dloc->type_offset = dloc->op->offset;
1397 /* Get the size of the actual type */
1398 if (dwarf_aggregate_size(type_die, &size) < 0 ||
1399 (unsigned)dloc->type_offset >= size)
1405 if (state->regs[reg].ok && state->regs[reg].kind == TSR_KIND_CANARY) {
1406 pr_debug_dtp(" stack canary\n");
1409 * This is a saved value of the stack canary which will be handled
1410 * in the outer logic when it returns failure here. Pretend it's
1411 * from the stack canary directly.
1413 setup_stack_canary(dloc);
1419 if (dso__kernel(map__dso(dloc->ms->map))) {
1423 /* Direct this-cpu access like "%gs:0x34740" */
1424 if (dloc->op->segment == INSN_SEG_X86_GS && dloc->op->imm &&
1425 arch__is(dloc->arch, "x86")) {
1426 pr_debug_dtp(" this-cpu var\n");
1428 addr = dloc->op->offset;
1430 if (get_global_var_type(cu_die, dloc, dloc->ip, addr,
1431 &offset, type_die)) {
1432 dloc->type_offset = offset;
1438 /* Access to global variable like "-0x7dcf0500(,%rdx,8)" */
1439 if (dloc->op->offset < 0 && reg != state->stack_reg) {
1440 addr = (s64) dloc->op->offset;
1442 if (get_global_var_type(cu_die, dloc, dloc->ip, addr,
1443 &offset, type_die)) {
1444 pr_debug_dtp(" global var\n");
1446 dloc->type_offset = offset;
1449 pr_debug_dtp(" negative offset\n");
1458 /* Iterate instructions in basic blocks and update type table */
1459 static int find_data_type_insn(struct data_loc_info *dloc,
1460 struct list_head *basic_blocks,
1461 struct die_var_type *var_types,
1462 Dwarf_Die *cu_die, Dwarf_Die *type_die)
1464 struct type_state state;
1465 struct symbol *sym = dloc->ms->sym;
1466 struct annotation *notes = symbol__annotation(sym);
1467 struct annotated_basic_block *bb;
1470 init_type_state(&state, dloc->arch);
1472 list_for_each_entry(bb, basic_blocks, list) {
1473 struct disasm_line *dl = bb->begin;
1475 BUG_ON(bb->begin->al.offset == -1 || bb->end->al.offset == -1);
1477 pr_debug_dtp("bb: [%"PRIx64" - %"PRIx64"]\n",
1478 bb->begin->al.offset, bb->end->al.offset);
1480 list_for_each_entry_from(dl, ¬es->src->source, al.node) {
1481 u64 this_ip = sym->start + dl->al.offset;
1482 u64 addr = map__rip_2objdump(dloc->ms->map, this_ip);
1484 /* Skip comment or debug info lines */
1485 if (dl->al.offset == -1)
1488 /* Update variable type at this address */
1489 update_var_state(&state, dloc, addr, dl->al.offset, var_types);
1491 if (this_ip == dloc->ip) {
1492 ret = check_matching_type(&state, dloc,
1497 /* Update type table after processing the instruction */
1498 update_insn_state(&state, dloc, cu_die, dl);
1505 exit_type_state(&state);
1510 * Construct a list of basic blocks for each scope with variables and try to find
1511 * the data type by updating a type state table through instructions.
1513 static int find_data_type_block(struct data_loc_info *dloc,
1514 Dwarf_Die *cu_die, Dwarf_Die *scopes,
1515 int nr_scopes, Dwarf_Die *type_die)
1517 LIST_HEAD(basic_blocks);
1518 struct die_var_type *var_types = NULL;
1519 u64 src_ip, dst_ip, prev_dst_ip;
1522 /* TODO: other architecture support */
1523 if (!arch__is(dloc->arch, "x86"))
1526 prev_dst_ip = dst_ip = dloc->ip;
1527 for (int i = nr_scopes - 1; i >= 0; i--) {
1528 Dwarf_Addr base, start, end;
1529 LIST_HEAD(this_blocks);
1532 if (dwarf_ranges(&scopes[i], 0, &base, &start, &end) < 0)
1535 pr_debug_dtp("scope: [%d/%d] (die:%lx)\n",
1536 i + 1, nr_scopes, (long)dwarf_dieoffset(&scopes[i]));
1537 src_ip = map__objdump_2rip(dloc->ms->map, start);
1540 /* Get basic blocks for this scope */
1541 if (annotate_get_basic_blocks(dloc->ms->sym, src_ip, dst_ip,
1542 &this_blocks) < 0) {
1543 /* Try previous block if they are not connected */
1544 if (prev_dst_ip != dst_ip) {
1545 dst_ip = prev_dst_ip;
1549 pr_debug_dtp("cannot find a basic block from %"PRIx64" to %"PRIx64"\n",
1550 src_ip - dloc->ms->sym->start,
1551 dst_ip - dloc->ms->sym->start);
1554 prepend_basic_blocks(&this_blocks, &basic_blocks);
1556 /* Get variable info for this scope and add to var_types list */
1557 die_collect_vars(&scopes[i], &var_types);
1558 fixup_var_address(var_types, start);
1560 /* Find from start of this scope to the target instruction */
1561 found = find_data_type_insn(dloc, &basic_blocks, var_types,
1566 if (dloc->op->multi_regs)
1567 snprintf(buf, sizeof(buf), "reg%d, reg%d",
1568 dloc->op->reg1, dloc->op->reg2);
1570 snprintf(buf, sizeof(buf), "reg%d", dloc->op->reg1);
1572 pr_debug_dtp("found by insn track: %#x(%s) type-offset=%#x\n",
1573 dloc->op->offset, buf, dloc->type_offset);
1574 pr_debug_type_name(type_die, TSR_KIND_TYPE);
1582 /* Go up to the next scope and find blocks to the start */
1583 prev_dst_ip = dst_ip;
1587 delete_basic_blocks(&basic_blocks);
1588 delete_var_types(var_types);
1592 /* The result will be saved in @type_die */
1593 static int find_data_type_die(struct data_loc_info *dloc, Dwarf_Die *type_die)
1595 struct annotated_op_loc *loc = dloc->op;
1596 Dwarf_Die cu_die, var_die;
1597 Dwarf_Die *scopes = NULL;
1603 bool is_fbreg = false;
1607 if (dloc->op->multi_regs)
1608 snprintf(buf, sizeof(buf), "reg%d, reg%d", dloc->op->reg1, dloc->op->reg2);
1609 else if (dloc->op->reg1 == DWARF_REG_PC)
1610 snprintf(buf, sizeof(buf), "PC");
1612 snprintf(buf, sizeof(buf), "reg%d", dloc->op->reg1);
1614 pr_debug_dtp("-----------------------------------------------------------\n");
1615 pr_debug_dtp("find data type for %#x(%s) at %s+%#"PRIx64"\n",
1616 dloc->op->offset, buf, dloc->ms->sym->name,
1617 dloc->ip - dloc->ms->sym->start);
1620 * IP is a relative instruction address from the start of the map, as
1621 * it can be randomized/relocated, it needs to translate to PC which is
1622 * a file address for DWARF processing.
1624 pc = map__rip_2objdump(dloc->ms->map, dloc->ip);
1626 /* Get a compile_unit for this address */
1627 if (!find_cu_die(dloc->di, pc, &cu_die)) {
1628 pr_debug_dtp("cannot find CU for address %"PRIx64"\n", pc);
1629 ann_data_stat.no_cuinfo++;
1634 offset = loc->offset;
1636 pr_debug_dtp("CU for %s (die:%#lx)\n",
1637 dwarf_diename(&cu_die), (long)dwarf_dieoffset(&cu_die));
1639 if (reg == DWARF_REG_PC) {
1640 if (get_global_var_type(&cu_die, dloc, dloc->ip, dloc->var_addr,
1641 &offset, type_die)) {
1642 dloc->type_offset = offset;
1644 pr_debug_dtp("found by addr=%#"PRIx64" type_offset=%#x\n",
1645 dloc->var_addr, offset);
1646 pr_debug_type_name(type_die, TSR_KIND_TYPE);
1652 /* Get a list of nested scopes - i.e. (inlined) functions and blocks. */
1653 nr_scopes = die_get_scopes(&cu_die, pc, &scopes);
1655 if (reg != DWARF_REG_PC && dwarf_hasattr(&scopes[0], DW_AT_frame_base)) {
1656 Dwarf_Attribute attr;
1659 /* Check if the 'reg' is assigned as frame base register */
1660 if (dwarf_attr(&scopes[0], DW_AT_frame_base, &attr) != NULL &&
1661 dwarf_formblock(&attr, &block) == 0 && block.length == 1) {
1662 switch (*block.data) {
1663 case DW_OP_reg0 ... DW_OP_reg31:
1664 fbreg = dloc->fbreg = *block.data - DW_OP_reg0;
1666 case DW_OP_call_frame_cfa:
1667 dloc->fb_cfa = true;
1668 if (die_get_cfa(dloc->di->dbg, pc, &fbreg,
1676 pr_debug_dtp("frame base: cfa=%d fbreg=%d\n",
1677 dloc->fb_cfa, fbreg);
1682 is_fbreg = (reg == fbreg);
1684 offset = loc->offset - fb_offset;
1686 /* Search from the inner-most scope to the outer */
1687 for (i = nr_scopes - 1; i >= 0; i--) {
1688 if (reg == DWARF_REG_PC) {
1689 if (!die_find_variable_by_addr(&scopes[i], dloc->var_addr,
1693 /* Look up variables/parameters in this scope */
1694 if (!die_find_variable_by_reg(&scopes[i], pc, reg,
1695 &offset, is_fbreg, &var_die))
1699 /* Found a variable, see if it's correct */
1700 ret = check_variable(dloc, &var_die, type_die, reg, offset, is_fbreg);
1702 pr_debug_dtp("found \"%s\" in scope=%d/%d (die: %#lx) ",
1703 dwarf_diename(&var_die), i+1, nr_scopes,
1704 (long)dwarf_dieoffset(&scopes[i]));
1705 if (reg == DWARF_REG_PC) {
1706 pr_debug_dtp("addr=%#"PRIx64" type_offset=%#x\n",
1707 dloc->var_addr, offset);
1708 } else if (reg == DWARF_REG_FB || is_fbreg) {
1709 pr_debug_dtp("stack_offset=%#x type_offset=%#x\n",
1712 pr_debug_dtp("type_offset=%#x\n", offset);
1714 pr_debug_location(&var_die, pc, reg);
1715 pr_debug_type_name(type_die, TSR_KIND_TYPE);
1717 pr_debug_dtp("check variable \"%s\" failed (die: %#lx)\n",
1718 dwarf_diename(&var_die),
1719 (long)dwarf_dieoffset(&var_die));
1720 pr_debug_location(&var_die, pc, reg);
1721 pr_debug_type_name(type_die, TSR_KIND_TYPE);
1723 dloc->type_offset = offset;
1727 if (loc->multi_regs && reg == loc->reg1 && loc->reg1 != loc->reg2) {
1732 if (reg != DWARF_REG_PC) {
1733 ret = find_data_type_block(dloc, &cu_die, scopes,
1734 nr_scopes, type_die);
1736 ann_data_stat.insn_track++;
1742 pr_debug_dtp("no variable found\n");
1743 ann_data_stat.no_var++;
1752 * find_data_type - Return a data type at the location
1753 * @dloc: data location
1755 * This functions searches the debug information of the binary to get the data
1756 * type it accesses. The exact location is expressed by (ip, reg, offset)
1757 * for pointer variables or (ip, addr) for global variables. Note that global
1758 * variables might update the @dloc->type_offset after finding the start of the
1759 * variable. If it cannot find a global variable by address, it tried to find
1760 * a declaration of the variable using var_name. In that case, @dloc->offset
1763 * It return %NULL if not found.
1765 struct annotated_data_type *find_data_type(struct data_loc_info *dloc)
1767 struct annotated_data_type *result = NULL;
1768 struct dso *dso = map__dso(dloc->ms->map);
1771 dloc->di = debuginfo__new(dso__long_name(dso));
1772 if (dloc->di == NULL) {
1773 pr_debug_dtp("cannot get the debug info\n");
1778 * The type offset is the same as instruction offset by default.
1779 * But when finding a global variable, the offset won't be valid.
1781 dloc->type_offset = dloc->op->offset;
1785 if (find_data_type_die(dloc, &type_die) < 0)
1788 result = dso__findnew_data_type(dso, &type_die);
1791 debuginfo__delete(dloc->di);
1795 static int alloc_data_type_histograms(struct annotated_data_type *adt, int nr_entries)
1798 size_t sz = sizeof(struct type_hist);
1800 sz += sizeof(struct type_hist_entry) * adt->self.size;
1802 /* Allocate a table of pointers for each event */
1803 adt->histograms = calloc(nr_entries, sizeof(*adt->histograms));
1804 if (adt->histograms == NULL)
1808 * Each histogram is allocated for the whole size of the type.
1809 * TODO: Probably we can move the histogram to members.
1811 for (i = 0; i < nr_entries; i++) {
1812 adt->histograms[i] = zalloc(sz);
1813 if (adt->histograms[i] == NULL)
1817 adt->nr_histograms = nr_entries;
1822 zfree(&(adt->histograms[i]));
1823 zfree(&adt->histograms);
1827 static void delete_data_type_histograms(struct annotated_data_type *adt)
1829 for (int i = 0; i < adt->nr_histograms; i++)
1830 zfree(&(adt->histograms[i]));
1832 zfree(&adt->histograms);
1833 adt->nr_histograms = 0;
1836 void annotated_data_type__tree_delete(struct rb_root *root)
1838 struct annotated_data_type *pos;
1840 while (!RB_EMPTY_ROOT(root)) {
1841 struct rb_node *node = rb_first(root);
1843 rb_erase(node, root);
1844 pos = rb_entry(node, struct annotated_data_type, node);
1845 delete_members(&pos->self);
1846 delete_data_type_histograms(pos);
1847 zfree(&pos->self.type_name);
1853 * annotated_data_type__update_samples - Update histogram
1854 * @adt: Data type to update
1855 * @evsel: Event to update
1856 * @offset: Offset in the type
1857 * @nr_samples: Number of samples at this offset
1858 * @period: Event count at this offset
1860 * This function updates type histogram at @ofs for @evsel. Samples are
1861 * aggregated before calling this function so it can be called with more
1862 * than one samples at a certain offset.
1864 int annotated_data_type__update_samples(struct annotated_data_type *adt,
1865 struct evsel *evsel, int offset,
1866 int nr_samples, u64 period)
1868 struct type_hist *h;
1873 if (adt->histograms == NULL) {
1874 int nr = evsel->evlist->core.nr_entries;
1876 if (alloc_data_type_histograms(adt, nr) < 0)
1880 if (offset < 0 || offset >= adt->self.size)
1883 h = adt->histograms[evsel->core.idx];
1885 h->nr_samples += nr_samples;
1886 h->addr[offset].nr_samples += nr_samples;
1887 h->period += period;
1888 h->addr[offset].period += period;
1892 static void print_annotated_data_header(struct hist_entry *he, struct evsel *evsel)
1894 struct dso *dso = map__dso(he->ms.map);
1896 int nr_samples = he->stat.nr_events;
1898 const char *val_hdr = "Percent";
1900 if (evsel__is_group_event(evsel)) {
1901 struct hist_entry *pair;
1903 list_for_each_entry(pair, &he->pairs.head, pairs.node)
1904 nr_samples += pair->stat.nr_events;
1907 printf("Annotate type: '%s' in %s (%d samples):\n",
1908 he->mem_type->self.type_name, dso__name(dso), nr_samples);
1910 if (evsel__is_group_event(evsel)) {
1914 for_each_group_evsel(pos, evsel)
1915 printf(" event[%d] = %s\n", i++, pos->name);
1917 nr_members = evsel->core.nr_members;
1920 if (symbol_conf.show_total_period) {
1923 } else if (symbol_conf.show_nr_samples) {
1925 val_hdr = "Samples";
1928 printf("============================================================================\n");
1929 printf("%*s %10s %10s %s\n", (width + 1) * nr_members, val_hdr,
1930 "offset", "size", "field");
1933 static void print_annotated_data_value(struct type_hist *h, u64 period, int nr_samples)
1935 double percent = h->period ? (100.0 * period / h->period) : 0;
1936 const char *color = get_percent_color(percent);
1938 if (symbol_conf.show_total_period)
1939 color_fprintf(stdout, color, " %11" PRIu64, period);
1940 else if (symbol_conf.show_nr_samples)
1941 color_fprintf(stdout, color, " %7d", nr_samples);
1943 color_fprintf(stdout, color, " %7.2f", percent);
1946 static void print_annotated_data_type(struct annotated_data_type *mem_type,
1947 struct annotated_member *member,
1948 struct evsel *evsel, int indent)
1950 struct annotated_member *child;
1951 struct type_hist *h = mem_type->histograms[evsel->core.idx];
1952 int i, nr_events = 1, samples = 0;
1954 int width = symbol_conf.show_total_period ? 11 : 7;
1956 for (i = 0; i < member->size; i++) {
1957 samples += h->addr[member->offset + i].nr_samples;
1958 period += h->addr[member->offset + i].period;
1960 print_annotated_data_value(h, period, samples);
1962 if (evsel__is_group_event(evsel)) {
1965 for_each_group_member(pos, evsel) {
1966 h = mem_type->histograms[pos->core.idx];
1970 for (i = 0; i < member->size; i++) {
1971 samples += h->addr[member->offset + i].nr_samples;
1972 period += h->addr[member->offset + i].period;
1974 print_annotated_data_value(h, period, samples);
1976 nr_events = evsel->core.nr_members;
1979 printf(" %10d %10d %*s%s\t%s",
1980 member->offset, member->size, indent, "", member->type_name,
1981 member->var_name ?: "");
1983 if (!list_empty(&member->children))
1986 list_for_each_entry(child, &member->children, node)
1987 print_annotated_data_type(mem_type, child, evsel, indent + 4);
1989 if (!list_empty(&member->children))
1990 printf("%*s}", (width + 1) * nr_events + 24 + indent, "");
1994 int hist_entry__annotate_data_tty(struct hist_entry *he, struct evsel *evsel)
1996 print_annotated_data_header(he, evsel);
1997 print_annotated_data_type(he->mem_type, &he->mem_type->self, evsel, 0);
2000 /* move to the next entry */