xref: /linux/tools/perf/util/annotate-data.c (revision 782959ac248ac3cbac80f7476d4e0410662ff400)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Convert sample address to data type using DWARF debug info.
4  *
5  * Written by Namhyung Kim <namhyung@kernel.org>
6  */
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <inttypes.h>
11 #include <linux/zalloc.h>
12 
13 #include "annotate.h"
14 #include "annotate-data.h"
15 #include "debuginfo.h"
16 #include "debug.h"
17 #include "dso.h"
18 #include "dwarf-regs.h"
19 #include "evsel.h"
20 #include "evlist.h"
21 #include "map.h"
22 #include "map_symbol.h"
23 #include "sort.h"
24 #include "strbuf.h"
25 #include "symbol.h"
26 #include "symbol_conf.h"
27 #include "thread.h"
28 
29 /* register number of the stack pointer */
30 #define X86_REG_SP 7
31 
32 static void delete_var_types(struct die_var_type *var_types);
33 
34 #define pr_debug_dtp(fmt, ...)					\
35 do {								\
36 	if (debug_type_profile)					\
37 		pr_info(fmt, ##__VA_ARGS__);			\
38 	else							\
39 		pr_debug3(fmt, ##__VA_ARGS__);			\
40 } while (0)
41 
42 void pr_debug_type_name(Dwarf_Die *die, enum type_state_kind kind)
43 {
44 	struct strbuf sb;
45 	char *str;
46 	Dwarf_Word size = 0;
47 
48 	if (!debug_type_profile && verbose < 3)
49 		return;
50 
51 	switch (kind) {
52 	case TSR_KIND_INVALID:
53 		pr_info("\n");
54 		return;
55 	case TSR_KIND_PERCPU_BASE:
56 		pr_info(" percpu base\n");
57 		return;
58 	case TSR_KIND_CONST:
59 		pr_info(" constant\n");
60 		return;
61 	case TSR_KIND_POINTER:
62 		pr_info(" pointer");
63 		/* it also prints the type info */
64 		break;
65 	case TSR_KIND_CANARY:
66 		pr_info(" stack canary\n");
67 		return;
68 	case TSR_KIND_TYPE:
69 	default:
70 		break;
71 	}
72 
73 	dwarf_aggregate_size(die, &size);
74 
75 	strbuf_init(&sb, 32);
76 	die_get_typename_from_type(die, &sb);
77 	str = strbuf_detach(&sb, NULL);
78 	pr_info(" type='%s' size=%#lx (die:%#lx)\n",
79 		str, (long)size, (long)dwarf_dieoffset(die));
80 	free(str);
81 }
82 
83 static void pr_debug_location(Dwarf_Die *die, u64 pc, int reg)
84 {
85 	ptrdiff_t off = 0;
86 	Dwarf_Attribute attr;
87 	Dwarf_Addr base, start, end;
88 	Dwarf_Op *ops;
89 	size_t nops;
90 
91 	if (!debug_type_profile && verbose < 3)
92 		return;
93 
94 	if (dwarf_attr(die, DW_AT_location, &attr) == NULL)
95 		return;
96 
97 	while ((off = dwarf_getlocations(&attr, off, &base, &start, &end, &ops, &nops)) > 0) {
98 		if (reg != DWARF_REG_PC && end < pc)
99 			continue;
100 		if (reg != DWARF_REG_PC && start > pc)
101 			break;
102 
103 		pr_info(" variable location: ");
104 		switch (ops->atom) {
105 		case DW_OP_reg0 ...DW_OP_reg31:
106 			pr_info("reg%d\n", ops->atom - DW_OP_reg0);
107 			break;
108 		case DW_OP_breg0 ...DW_OP_breg31:
109 			pr_info("base=reg%d, offset=%#lx\n",
110 				ops->atom - DW_OP_breg0, (long)ops->number);
111 			break;
112 		case DW_OP_regx:
113 			pr_info("reg%ld\n", (long)ops->number);
114 			break;
115 		case DW_OP_bregx:
116 			pr_info("base=reg%ld, offset=%#lx\n",
117 				(long)ops->number, (long)ops->number2);
118 			break;
119 		case DW_OP_fbreg:
120 			pr_info("use frame base, offset=%#lx\n", (long)ops->number);
121 			break;
122 		case DW_OP_addr:
123 			pr_info("address=%#lx\n", (long)ops->number);
124 			break;
125 		default:
126 			pr_info("unknown: code=%#x, number=%#lx\n",
127 				ops->atom, (long)ops->number);
128 			break;
129 		}
130 		break;
131 	}
132 }
133 
134 bool has_reg_type(struct type_state *state, int reg)
135 {
136 	return (unsigned)reg < ARRAY_SIZE(state->regs);
137 }
138 
139 static void init_type_state(struct type_state *state, struct arch *arch)
140 {
141 	memset(state, 0, sizeof(*state));
142 	INIT_LIST_HEAD(&state->stack_vars);
143 
144 	if (arch__is(arch, "x86")) {
145 		state->regs[0].caller_saved = true;
146 		state->regs[1].caller_saved = true;
147 		state->regs[2].caller_saved = true;
148 		state->regs[4].caller_saved = true;
149 		state->regs[5].caller_saved = true;
150 		state->regs[8].caller_saved = true;
151 		state->regs[9].caller_saved = true;
152 		state->regs[10].caller_saved = true;
153 		state->regs[11].caller_saved = true;
154 		state->ret_reg = 0;
155 		state->stack_reg = X86_REG_SP;
156 	}
157 }
158 
159 static void exit_type_state(struct type_state *state)
160 {
161 	struct type_state_stack *stack, *tmp;
162 
163 	list_for_each_entry_safe(stack, tmp, &state->stack_vars, list) {
164 		list_del(&stack->list);
165 		free(stack);
166 	}
167 }
168 
169 /*
170  * Compare type name and size to maintain them in a tree.
171  * I'm not sure if DWARF would have information of a single type in many
172  * different places (compilation units).  If not, it could compare the
173  * offset of the type entry in the .debug_info section.
174  */
175 static int data_type_cmp(const void *_key, const struct rb_node *node)
176 {
177 	const struct annotated_data_type *key = _key;
178 	struct annotated_data_type *type;
179 
180 	type = rb_entry(node, struct annotated_data_type, node);
181 
182 	if (key->self.size != type->self.size)
183 		return key->self.size - type->self.size;
184 	return strcmp(key->self.type_name, type->self.type_name);
185 }
186 
187 static bool data_type_less(struct rb_node *node_a, const struct rb_node *node_b)
188 {
189 	struct annotated_data_type *a, *b;
190 
191 	a = rb_entry(node_a, struct annotated_data_type, node);
192 	b = rb_entry(node_b, struct annotated_data_type, node);
193 
194 	if (a->self.size != b->self.size)
195 		return a->self.size < b->self.size;
196 	return strcmp(a->self.type_name, b->self.type_name) < 0;
197 }
198 
199 /* Recursively add new members for struct/union */
200 static int __add_member_cb(Dwarf_Die *die, void *arg)
201 {
202 	struct annotated_member *parent = arg;
203 	struct annotated_member *member;
204 	Dwarf_Die member_type, die_mem;
205 	Dwarf_Word size, loc;
206 	Dwarf_Attribute attr;
207 	struct strbuf sb;
208 	int tag;
209 
210 	if (dwarf_tag(die) != DW_TAG_member)
211 		return DIE_FIND_CB_SIBLING;
212 
213 	member = zalloc(sizeof(*member));
214 	if (member == NULL)
215 		return DIE_FIND_CB_END;
216 
217 	strbuf_init(&sb, 32);
218 	die_get_typename(die, &sb);
219 
220 	die_get_real_type(die, &member_type);
221 	if (dwarf_aggregate_size(&member_type, &size) < 0)
222 		size = 0;
223 
224 	if (!dwarf_attr_integrate(die, DW_AT_data_member_location, &attr))
225 		loc = 0;
226 	else
227 		dwarf_formudata(&attr, &loc);
228 
229 	member->type_name = strbuf_detach(&sb, NULL);
230 	/* member->var_name can be NULL */
231 	if (dwarf_diename(die))
232 		member->var_name = strdup(dwarf_diename(die));
233 	member->size = size;
234 	member->offset = loc + parent->offset;
235 	INIT_LIST_HEAD(&member->children);
236 	list_add_tail(&member->node, &parent->children);
237 
238 	tag = dwarf_tag(&member_type);
239 	switch (tag) {
240 	case DW_TAG_structure_type:
241 	case DW_TAG_union_type:
242 		die_find_child(&member_type, __add_member_cb, member, &die_mem);
243 		break;
244 	default:
245 		break;
246 	}
247 	return DIE_FIND_CB_SIBLING;
248 }
249 
250 static void add_member_types(struct annotated_data_type *parent, Dwarf_Die *type)
251 {
252 	Dwarf_Die die_mem;
253 
254 	die_find_child(type, __add_member_cb, &parent->self, &die_mem);
255 }
256 
257 static void delete_members(struct annotated_member *member)
258 {
259 	struct annotated_member *child, *tmp;
260 
261 	list_for_each_entry_safe(child, tmp, &member->children, node) {
262 		list_del(&child->node);
263 		delete_members(child);
264 		zfree(&child->type_name);
265 		zfree(&child->var_name);
266 		free(child);
267 	}
268 }
269 
270 static struct annotated_data_type *dso__findnew_data_type(struct dso *dso,
271 							  Dwarf_Die *type_die)
272 {
273 	struct annotated_data_type *result = NULL;
274 	struct annotated_data_type key;
275 	struct rb_node *node;
276 	struct strbuf sb;
277 	char *type_name;
278 	Dwarf_Word size;
279 
280 	strbuf_init(&sb, 32);
281 	if (die_get_typename_from_type(type_die, &sb) < 0)
282 		strbuf_add(&sb, "(unknown type)", 14);
283 	type_name = strbuf_detach(&sb, NULL);
284 	dwarf_aggregate_size(type_die, &size);
285 
286 	/* Check existing nodes in dso->data_types tree */
287 	key.self.type_name = type_name;
288 	key.self.size = size;
289 	node = rb_find(&key, dso__data_types(dso), data_type_cmp);
290 	if (node) {
291 		result = rb_entry(node, struct annotated_data_type, node);
292 		free(type_name);
293 		return result;
294 	}
295 
296 	/* If not, add a new one */
297 	result = zalloc(sizeof(*result));
298 	if (result == NULL) {
299 		free(type_name);
300 		return NULL;
301 	}
302 
303 	result->self.type_name = type_name;
304 	result->self.size = size;
305 	INIT_LIST_HEAD(&result->self.children);
306 
307 	if (symbol_conf.annotate_data_member)
308 		add_member_types(result, type_die);
309 
310 	rb_add(&result->node, dso__data_types(dso), data_type_less);
311 	return result;
312 }
313 
314 static bool find_cu_die(struct debuginfo *di, u64 pc, Dwarf_Die *cu_die)
315 {
316 	Dwarf_Off off, next_off;
317 	size_t header_size;
318 
319 	if (dwarf_addrdie(di->dbg, pc, cu_die) != NULL)
320 		return cu_die;
321 
322 	/*
323 	 * There are some kernels don't have full aranges and contain only a few
324 	 * aranges entries.  Fallback to iterate all CU entries in .debug_info
325 	 * in case it's missing.
326 	 */
327 	off = 0;
328 	while (dwarf_nextcu(di->dbg, off, &next_off, &header_size,
329 			    NULL, NULL, NULL) == 0) {
330 		if (dwarf_offdie(di->dbg, off + header_size, cu_die) &&
331 		    dwarf_haspc(cu_die, pc))
332 			return true;
333 
334 		off = next_off;
335 	}
336 	return false;
337 }
338 
339 /* The type info will be saved in @type_die */
340 static int check_variable(struct data_loc_info *dloc, Dwarf_Die *var_die,
341 			  Dwarf_Die *type_die, int reg, int offset, bool is_fbreg)
342 {
343 	Dwarf_Word size;
344 	bool is_pointer = true;
345 
346 	if (reg == DWARF_REG_PC)
347 		is_pointer = false;
348 	else if (reg == dloc->fbreg || is_fbreg)
349 		is_pointer = false;
350 	else if (arch__is(dloc->arch, "x86") && reg == X86_REG_SP)
351 		is_pointer = false;
352 
353 	/* Get the type of the variable */
354 	if (die_get_real_type(var_die, type_die) == NULL) {
355 		pr_debug_dtp("variable has no type\n");
356 		ann_data_stat.no_typeinfo++;
357 		return -1;
358 	}
359 
360 	/*
361 	 * Usually it expects a pointer type for a memory access.
362 	 * Convert to a real type it points to.  But global variables
363 	 * and local variables are accessed directly without a pointer.
364 	 */
365 	if (is_pointer) {
366 		if ((dwarf_tag(type_die) != DW_TAG_pointer_type &&
367 		     dwarf_tag(type_die) != DW_TAG_array_type) ||
368 		    die_get_real_type(type_die, type_die) == NULL) {
369 			pr_debug_dtp("no pointer or no type\n");
370 			ann_data_stat.no_typeinfo++;
371 			return -1;
372 		}
373 	}
374 
375 	/* Get the size of the actual type */
376 	if (dwarf_aggregate_size(type_die, &size) < 0) {
377 		pr_debug_dtp("type size is unknown\n");
378 		ann_data_stat.invalid_size++;
379 		return -1;
380 	}
381 
382 	/* Minimal sanity check */
383 	if ((unsigned)offset >= size) {
384 		pr_debug_dtp("offset: %d is bigger than size: %"PRIu64"\n",
385 			     offset, size);
386 		ann_data_stat.bad_offset++;
387 		return -1;
388 	}
389 
390 	return 0;
391 }
392 
393 struct type_state_stack *find_stack_state(struct type_state *state,
394 						 int offset)
395 {
396 	struct type_state_stack *stack;
397 
398 	list_for_each_entry(stack, &state->stack_vars, list) {
399 		if (offset == stack->offset)
400 			return stack;
401 
402 		if (stack->compound && stack->offset < offset &&
403 		    offset < stack->offset + stack->size)
404 			return stack;
405 	}
406 	return NULL;
407 }
408 
409 void set_stack_state(struct type_state_stack *stack, int offset, u8 kind,
410 			    Dwarf_Die *type_die)
411 {
412 	int tag;
413 	Dwarf_Word size;
414 
415 	if (dwarf_aggregate_size(type_die, &size) < 0)
416 		size = 0;
417 
418 	tag = dwarf_tag(type_die);
419 
420 	stack->type = *type_die;
421 	stack->size = size;
422 	stack->offset = offset;
423 	stack->kind = kind;
424 
425 	switch (tag) {
426 	case DW_TAG_structure_type:
427 	case DW_TAG_union_type:
428 		stack->compound = (kind != TSR_KIND_POINTER);
429 		break;
430 	default:
431 		stack->compound = false;
432 		break;
433 	}
434 }
435 
436 struct type_state_stack *findnew_stack_state(struct type_state *state,
437 						    int offset, u8 kind,
438 						    Dwarf_Die *type_die)
439 {
440 	struct type_state_stack *stack = find_stack_state(state, offset);
441 
442 	if (stack) {
443 		set_stack_state(stack, offset, kind, type_die);
444 		return stack;
445 	}
446 
447 	stack = malloc(sizeof(*stack));
448 	if (stack) {
449 		set_stack_state(stack, offset, kind, type_die);
450 		list_add(&stack->list, &state->stack_vars);
451 	}
452 	return stack;
453 }
454 
455 /* Maintain a cache for quick global variable lookup */
456 struct global_var_entry {
457 	struct rb_node node;
458 	char *name;
459 	u64 start;
460 	u64 end;
461 	u64 die_offset;
462 };
463 
464 static int global_var_cmp(const void *_key, const struct rb_node *node)
465 {
466 	const u64 addr = (uintptr_t)_key;
467 	struct global_var_entry *gvar;
468 
469 	gvar = rb_entry(node, struct global_var_entry, node);
470 
471 	if (gvar->start <= addr && addr < gvar->end)
472 		return 0;
473 	return gvar->start > addr ? -1 : 1;
474 }
475 
476 static bool global_var_less(struct rb_node *node_a, const struct rb_node *node_b)
477 {
478 	struct global_var_entry *gvar_a, *gvar_b;
479 
480 	gvar_a = rb_entry(node_a, struct global_var_entry, node);
481 	gvar_b = rb_entry(node_b, struct global_var_entry, node);
482 
483 	return gvar_a->start < gvar_b->start;
484 }
485 
486 static struct global_var_entry *global_var__find(struct data_loc_info *dloc, u64 addr)
487 {
488 	struct dso *dso = map__dso(dloc->ms->map);
489 	struct rb_node *node;
490 
491 	node = rb_find((void *)(uintptr_t)addr, dso__global_vars(dso), global_var_cmp);
492 	if (node == NULL)
493 		return NULL;
494 
495 	return rb_entry(node, struct global_var_entry, node);
496 }
497 
498 static bool global_var__add(struct data_loc_info *dloc, u64 addr,
499 			    const char *name, Dwarf_Die *type_die)
500 {
501 	struct dso *dso = map__dso(dloc->ms->map);
502 	struct global_var_entry *gvar;
503 	Dwarf_Word size;
504 
505 	if (dwarf_aggregate_size(type_die, &size) < 0)
506 		return false;
507 
508 	gvar = malloc(sizeof(*gvar));
509 	if (gvar == NULL)
510 		return false;
511 
512 	gvar->name = name ? strdup(name) : NULL;
513 	if (name && gvar->name == NULL) {
514 		free(gvar);
515 		return false;
516 	}
517 
518 	gvar->start = addr;
519 	gvar->end = addr + size;
520 	gvar->die_offset = dwarf_dieoffset(type_die);
521 
522 	rb_add(&gvar->node, dso__global_vars(dso), global_var_less);
523 	return true;
524 }
525 
526 void global_var_type__tree_delete(struct rb_root *root)
527 {
528 	struct global_var_entry *gvar;
529 
530 	while (!RB_EMPTY_ROOT(root)) {
531 		struct rb_node *node = rb_first(root);
532 
533 		rb_erase(node, root);
534 		gvar = rb_entry(node, struct global_var_entry, node);
535 		zfree(&gvar->name);
536 		free(gvar);
537 	}
538 }
539 
540 bool get_global_var_info(struct data_loc_info *dloc, u64 addr,
541 				const char **var_name, int *var_offset)
542 {
543 	struct addr_location al;
544 	struct symbol *sym;
545 	u64 mem_addr;
546 
547 	/* Kernel symbols might be relocated */
548 	mem_addr = addr + map__reloc(dloc->ms->map);
549 
550 	addr_location__init(&al);
551 	sym = thread__find_symbol_fb(dloc->thread, dloc->cpumode,
552 				     mem_addr, &al);
553 	if (sym) {
554 		*var_name = sym->name;
555 		/* Calculate type offset from the start of variable */
556 		*var_offset = mem_addr - map__unmap_ip(al.map, sym->start);
557 	} else {
558 		*var_name = NULL;
559 	}
560 	addr_location__exit(&al);
561 	if (*var_name == NULL)
562 		return false;
563 
564 	return true;
565 }
566 
567 static void global_var__collect(struct data_loc_info *dloc)
568 {
569 	Dwarf *dwarf = dloc->di->dbg;
570 	Dwarf_Off off, next_off;
571 	Dwarf_Die cu_die, type_die;
572 	size_t header_size;
573 
574 	/* Iterate all CU and collect global variables that have no location in a register. */
575 	off = 0;
576 	while (dwarf_nextcu(dwarf, off, &next_off, &header_size,
577 			    NULL, NULL, NULL) == 0) {
578 		struct die_var_type *var_types = NULL;
579 		struct die_var_type *pos;
580 
581 		if (dwarf_offdie(dwarf, off + header_size, &cu_die) == NULL) {
582 			off = next_off;
583 			continue;
584 		}
585 
586 		die_collect_global_vars(&cu_die, &var_types);
587 
588 		for (pos = var_types; pos; pos = pos->next) {
589 			const char *var_name = NULL;
590 			int var_offset = 0;
591 
592 			if (pos->reg != -1)
593 				continue;
594 
595 			if (!dwarf_offdie(dwarf, pos->die_off, &type_die))
596 				continue;
597 
598 			if (!get_global_var_info(dloc, pos->addr, &var_name,
599 						 &var_offset))
600 				continue;
601 
602 			if (var_offset != 0)
603 				continue;
604 
605 			global_var__add(dloc, pos->addr, var_name, &type_die);
606 		}
607 
608 		delete_var_types(var_types);
609 
610 		off = next_off;
611 	}
612 }
613 
614 bool get_global_var_type(Dwarf_Die *cu_die, struct data_loc_info *dloc,
615 				u64 ip, u64 var_addr, int *var_offset,
616 				Dwarf_Die *type_die)
617 {
618 	u64 pc;
619 	int offset;
620 	const char *var_name = NULL;
621 	struct global_var_entry *gvar;
622 	struct dso *dso = map__dso(dloc->ms->map);
623 	Dwarf_Die var_die;
624 
625 	if (RB_EMPTY_ROOT(dso__global_vars(dso)))
626 		global_var__collect(dloc);
627 
628 	gvar = global_var__find(dloc, var_addr);
629 	if (gvar) {
630 		if (!dwarf_offdie(dloc->di->dbg, gvar->die_offset, type_die))
631 			return false;
632 
633 		*var_offset = var_addr - gvar->start;
634 		return true;
635 	}
636 
637 	/* Try to get the variable by address first */
638 	if (die_find_variable_by_addr(cu_die, var_addr, &var_die, &offset) &&
639 	    check_variable(dloc, &var_die, type_die, DWARF_REG_PC, offset,
640 			   /*is_fbreg=*/false) == 0) {
641 		var_name = dwarf_diename(&var_die);
642 		*var_offset = offset;
643 		goto ok;
644 	}
645 
646 	if (!get_global_var_info(dloc, var_addr, &var_name, var_offset))
647 		return false;
648 
649 	pc = map__rip_2objdump(dloc->ms->map, ip);
650 
651 	/* Try to get the name of global variable */
652 	if (die_find_variable_at(cu_die, var_name, pc, &var_die) &&
653 	    check_variable(dloc, &var_die, type_die, DWARF_REG_PC, *var_offset,
654 			   /*is_fbreg=*/false) == 0)
655 		goto ok;
656 
657 	return false;
658 
659 ok:
660 	/* The address should point to the start of the variable */
661 	global_var__add(dloc, var_addr - *var_offset, var_name, type_die);
662 	return true;
663 }
664 
665 /**
666  * update_var_state - Update type state using given variables
667  * @state: type state table
668  * @dloc: data location info
669  * @addr: instruction address to match with variable
670  * @insn_offset: instruction offset (for debug)
671  * @var_types: list of variables with type info
672  *
673  * This function fills the @state table using @var_types info.  Each variable
674  * is used only at the given location and updates an entry in the table.
675  */
676 static void update_var_state(struct type_state *state, struct data_loc_info *dloc,
677 			     u64 addr, u64 insn_offset, struct die_var_type *var_types)
678 {
679 	Dwarf_Die mem_die;
680 	struct die_var_type *var;
681 	int fbreg = dloc->fbreg;
682 	int fb_offset = 0;
683 
684 	if (dloc->fb_cfa) {
685 		if (die_get_cfa(dloc->di->dbg, addr, &fbreg, &fb_offset) < 0)
686 			fbreg = -1;
687 	}
688 
689 	for (var = var_types; var != NULL; var = var->next) {
690 		if (var->addr != addr)
691 			continue;
692 		/* Get the type DIE using the offset */
693 		if (!dwarf_offdie(dloc->di->dbg, var->die_off, &mem_die))
694 			continue;
695 
696 		if (var->reg == DWARF_REG_FB) {
697 			findnew_stack_state(state, var->offset, TSR_KIND_TYPE,
698 					    &mem_die);
699 
700 			pr_debug_dtp("var [%"PRIx64"] -%#x(stack)",
701 				     insn_offset, -var->offset);
702 			pr_debug_type_name(&mem_die, TSR_KIND_TYPE);
703 		} else if (var->reg == fbreg) {
704 			findnew_stack_state(state, var->offset - fb_offset,
705 					    TSR_KIND_TYPE, &mem_die);
706 
707 			pr_debug_dtp("var [%"PRIx64"] -%#x(stack)",
708 				     insn_offset, -var->offset + fb_offset);
709 			pr_debug_type_name(&mem_die, TSR_KIND_TYPE);
710 		} else if (has_reg_type(state, var->reg) && var->offset == 0) {
711 			struct type_state_reg *reg;
712 
713 			reg = &state->regs[var->reg];
714 			reg->type = mem_die;
715 			reg->kind = TSR_KIND_TYPE;
716 			reg->ok = true;
717 
718 			pr_debug_dtp("var [%"PRIx64"] reg%d",
719 				     insn_offset, var->reg);
720 			pr_debug_type_name(&mem_die, TSR_KIND_TYPE);
721 		}
722 	}
723 }
724 
725 /**
726  * update_insn_state - Update type state for an instruction
727  * @state: type state table
728  * @dloc: data location info
729  * @cu_die: compile unit debug entry
730  * @dl: disasm line for the instruction
731  *
732  * This function updates the @state table for the target operand of the
733  * instruction at @dl if it transfers the type like MOV on x86.  Since it
734  * tracks the type, it won't care about the values like in arithmetic
735  * instructions like ADD/SUB/MUL/DIV and INC/DEC.
736  *
737  * Note that ops->reg2 is only available when both mem_ref and multi_regs
738  * are true.
739  */
740 static void update_insn_state(struct type_state *state, struct data_loc_info *dloc,
741 			      Dwarf_Die *cu_die, struct disasm_line *dl)
742 {
743 	if (dloc->arch->update_insn_state)
744 		dloc->arch->update_insn_state(state, dloc, cu_die, dl);
745 }
746 
747 /*
748  * Prepend this_blocks (from the outer scope) to full_blocks, removing
749  * duplicate disasm line.
750  */
751 static void prepend_basic_blocks(struct list_head *this_blocks,
752 				 struct list_head *full_blocks)
753 {
754 	struct annotated_basic_block *first_bb, *last_bb;
755 
756 	last_bb = list_last_entry(this_blocks, typeof(*last_bb), list);
757 	first_bb = list_first_entry(full_blocks, typeof(*first_bb), list);
758 
759 	if (list_empty(full_blocks))
760 		goto out;
761 
762 	/* Last insn in this_blocks should be same as first insn in full_blocks */
763 	if (last_bb->end != first_bb->begin) {
764 		pr_debug("prepend basic blocks: mismatched disasm line %"PRIx64" -> %"PRIx64"\n",
765 			 last_bb->end->al.offset, first_bb->begin->al.offset);
766 		goto out;
767 	}
768 
769 	/* Is the basic block have only one disasm_line? */
770 	if (last_bb->begin == last_bb->end) {
771 		list_del(&last_bb->list);
772 		free(last_bb);
773 		goto out;
774 	}
775 
776 	/* Point to the insn before the last when adding this block to full_blocks */
777 	last_bb->end = list_prev_entry(last_bb->end, al.node);
778 
779 out:
780 	list_splice(this_blocks, full_blocks);
781 }
782 
783 static void delete_basic_blocks(struct list_head *basic_blocks)
784 {
785 	struct annotated_basic_block *bb, *tmp;
786 
787 	list_for_each_entry_safe(bb, tmp, basic_blocks, list) {
788 		list_del(&bb->list);
789 		free(bb);
790 	}
791 }
792 
793 /* Make sure all variables have a valid start address */
794 static void fixup_var_address(struct die_var_type *var_types, u64 addr)
795 {
796 	while (var_types) {
797 		/*
798 		 * Some variables have no address range meaning it's always
799 		 * available in the whole scope.  Let's adjust the start
800 		 * address to the start of the scope.
801 		 */
802 		if (var_types->addr == 0)
803 			var_types->addr = addr;
804 
805 		var_types = var_types->next;
806 	}
807 }
808 
809 static void delete_var_types(struct die_var_type *var_types)
810 {
811 	while (var_types) {
812 		struct die_var_type *next = var_types->next;
813 
814 		free(var_types);
815 		var_types = next;
816 	}
817 }
818 
819 /* should match to is_stack_canary() in util/annotate.c */
820 static void setup_stack_canary(struct data_loc_info *dloc)
821 {
822 	if (arch__is(dloc->arch, "x86")) {
823 		dloc->op->segment = INSN_SEG_X86_GS;
824 		dloc->op->imm = true;
825 		dloc->op->offset = 40;
826 	}
827 }
828 
829 /*
830  * It's at the target address, check if it has a matching type.
831  * It returns 1 if found, 0 if not or -1 if not found but no need to
832  * repeat the search.  The last case is for per-cpu variables which
833  * are similar to global variables and no additional info is needed.
834  */
835 static int check_matching_type(struct type_state *state,
836 			       struct data_loc_info *dloc,
837 			       Dwarf_Die *cu_die, Dwarf_Die *type_die)
838 {
839 	Dwarf_Word size;
840 	u32 insn_offset = dloc->ip - dloc->ms->sym->start;
841 	int reg = dloc->op->reg1;
842 
843 	pr_debug_dtp("chk [%x] reg%d offset=%#x ok=%d kind=%d",
844 		     insn_offset, reg, dloc->op->offset,
845 		     state->regs[reg].ok, state->regs[reg].kind);
846 
847 	if (state->regs[reg].ok && state->regs[reg].kind == TSR_KIND_TYPE) {
848 		int tag = dwarf_tag(&state->regs[reg].type);
849 
850 		/*
851 		 * Normal registers should hold a pointer (or array) to
852 		 * dereference a memory location.
853 		 */
854 		if (tag != DW_TAG_pointer_type && tag != DW_TAG_array_type) {
855 			if (dloc->op->offset < 0 && reg != state->stack_reg)
856 				goto check_kernel;
857 
858 			pr_debug_dtp("\n");
859 			return -1;
860 		}
861 
862 		pr_debug_dtp("\n");
863 
864 		/* Remove the pointer and get the target type */
865 		if (die_get_real_type(&state->regs[reg].type, type_die) == NULL)
866 			return -1;
867 
868 		dloc->type_offset = dloc->op->offset;
869 
870 		/* Get the size of the actual type */
871 		if (dwarf_aggregate_size(type_die, &size) < 0 ||
872 		    (unsigned)dloc->type_offset >= size)
873 			return -1;
874 
875 		return 1;
876 	}
877 
878 	if (reg == dloc->fbreg) {
879 		struct type_state_stack *stack;
880 
881 		pr_debug_dtp(" fbreg\n");
882 
883 		stack = find_stack_state(state, dloc->type_offset);
884 		if (stack == NULL)
885 			return 0;
886 
887 		if (stack->kind == TSR_KIND_CANARY) {
888 			setup_stack_canary(dloc);
889 			return -1;
890 		}
891 
892 		if (stack->kind != TSR_KIND_TYPE)
893 			return 0;
894 
895 		*type_die = stack->type;
896 		/* Update the type offset from the start of slot */
897 		dloc->type_offset -= stack->offset;
898 
899 		return 1;
900 	}
901 
902 	if (dloc->fb_cfa) {
903 		struct type_state_stack *stack;
904 		u64 pc = map__rip_2objdump(dloc->ms->map, dloc->ip);
905 		int fbreg, fboff;
906 
907 		pr_debug_dtp(" cfa\n");
908 
909 		if (die_get_cfa(dloc->di->dbg, pc, &fbreg, &fboff) < 0)
910 			fbreg = -1;
911 
912 		if (reg != fbreg)
913 			return 0;
914 
915 		stack = find_stack_state(state, dloc->type_offset - fboff);
916 		if (stack == NULL)
917 			return 0;
918 
919 		if (stack->kind == TSR_KIND_CANARY) {
920 			setup_stack_canary(dloc);
921 			return -1;
922 		}
923 
924 		if (stack->kind != TSR_KIND_TYPE)
925 			return 0;
926 
927 		*type_die = stack->type;
928 		/* Update the type offset from the start of slot */
929 		dloc->type_offset -= fboff + stack->offset;
930 
931 		return 1;
932 	}
933 
934 	if (state->regs[reg].kind == TSR_KIND_PERCPU_BASE) {
935 		u64 var_addr = dloc->op->offset;
936 		int var_offset;
937 
938 		pr_debug_dtp(" percpu var\n");
939 
940 		if (dloc->op->multi_regs) {
941 			int reg2 = dloc->op->reg2;
942 
943 			if (dloc->op->reg2 == reg)
944 				reg2 = dloc->op->reg1;
945 
946 			if (has_reg_type(state, reg2) && state->regs[reg2].ok &&
947 			    state->regs[reg2].kind == TSR_KIND_CONST)
948 				var_addr += state->regs[reg2].imm_value;
949 		}
950 
951 		if (get_global_var_type(cu_die, dloc, dloc->ip, var_addr,
952 					&var_offset, type_die)) {
953 			dloc->type_offset = var_offset;
954 			return 1;
955 		}
956 		/* No need to retry per-cpu (global) variables */
957 		return -1;
958 	}
959 
960 	if (state->regs[reg].ok && state->regs[reg].kind == TSR_KIND_POINTER) {
961 		pr_debug_dtp(" percpu ptr\n");
962 
963 		/*
964 		 * It's actaully pointer but the address was calculated using
965 		 * some arithmetic.  So it points to the actual type already.
966 		 */
967 		*type_die = state->regs[reg].type;
968 
969 		dloc->type_offset = dloc->op->offset;
970 
971 		/* Get the size of the actual type */
972 		if (dwarf_aggregate_size(type_die, &size) < 0 ||
973 		    (unsigned)dloc->type_offset >= size)
974 			return -1;
975 
976 		return 1;
977 	}
978 
979 	if (state->regs[reg].ok && state->regs[reg].kind == TSR_KIND_CANARY) {
980 		pr_debug_dtp(" stack canary\n");
981 
982 		/*
983 		 * This is a saved value of the stack canary which will be handled
984 		 * in the outer logic when it returns failure here.  Pretend it's
985 		 * from the stack canary directly.
986 		 */
987 		setup_stack_canary(dloc);
988 
989 		return -1;
990 	}
991 
992 check_kernel:
993 	if (dso__kernel(map__dso(dloc->ms->map))) {
994 		u64 addr;
995 		int offset;
996 
997 		/* Direct this-cpu access like "%gs:0x34740" */
998 		if (dloc->op->segment == INSN_SEG_X86_GS && dloc->op->imm &&
999 		    arch__is(dloc->arch, "x86")) {
1000 			pr_debug_dtp(" this-cpu var\n");
1001 
1002 			addr = dloc->op->offset;
1003 
1004 			if (get_global_var_type(cu_die, dloc, dloc->ip, addr,
1005 						&offset, type_die)) {
1006 				dloc->type_offset = offset;
1007 				return 1;
1008 			}
1009 			return -1;
1010 		}
1011 
1012 		/* Access to global variable like "-0x7dcf0500(,%rdx,8)" */
1013 		if (dloc->op->offset < 0 && reg != state->stack_reg) {
1014 			addr = (s64) dloc->op->offset;
1015 
1016 			if (get_global_var_type(cu_die, dloc, dloc->ip, addr,
1017 						&offset, type_die)) {
1018 				pr_debug_dtp(" global var\n");
1019 
1020 				dloc->type_offset = offset;
1021 				return 1;
1022 			}
1023 			pr_debug_dtp(" negative offset\n");
1024 			return -1;
1025 		}
1026 	}
1027 
1028 	pr_debug_dtp("\n");
1029 	return 0;
1030 }
1031 
1032 /* Iterate instructions in basic blocks and update type table */
1033 static int find_data_type_insn(struct data_loc_info *dloc,
1034 			       struct list_head *basic_blocks,
1035 			       struct die_var_type *var_types,
1036 			       Dwarf_Die *cu_die, Dwarf_Die *type_die)
1037 {
1038 	struct type_state state;
1039 	struct symbol *sym = dloc->ms->sym;
1040 	struct annotation *notes = symbol__annotation(sym);
1041 	struct annotated_basic_block *bb;
1042 	int ret = 0;
1043 
1044 	init_type_state(&state, dloc->arch);
1045 
1046 	list_for_each_entry(bb, basic_blocks, list) {
1047 		struct disasm_line *dl = bb->begin;
1048 
1049 		BUG_ON(bb->begin->al.offset == -1 || bb->end->al.offset == -1);
1050 
1051 		pr_debug_dtp("bb: [%"PRIx64" - %"PRIx64"]\n",
1052 			     bb->begin->al.offset, bb->end->al.offset);
1053 
1054 		list_for_each_entry_from(dl, &notes->src->source, al.node) {
1055 			u64 this_ip = sym->start + dl->al.offset;
1056 			u64 addr = map__rip_2objdump(dloc->ms->map, this_ip);
1057 
1058 			/* Skip comment or debug info lines */
1059 			if (dl->al.offset == -1)
1060 				continue;
1061 
1062 			/* Update variable type at this address */
1063 			update_var_state(&state, dloc, addr, dl->al.offset, var_types);
1064 
1065 			if (this_ip == dloc->ip) {
1066 				ret = check_matching_type(&state, dloc,
1067 							  cu_die, type_die);
1068 				goto out;
1069 			}
1070 
1071 			/* Update type table after processing the instruction */
1072 			update_insn_state(&state, dloc, cu_die, dl);
1073 			if (dl == bb->end)
1074 				break;
1075 		}
1076 	}
1077 
1078 out:
1079 	exit_type_state(&state);
1080 	return ret;
1081 }
1082 
1083 /*
1084  * Construct a list of basic blocks for each scope with variables and try to find
1085  * the data type by updating a type state table through instructions.
1086  */
1087 static int find_data_type_block(struct data_loc_info *dloc,
1088 				Dwarf_Die *cu_die, Dwarf_Die *scopes,
1089 				int nr_scopes, Dwarf_Die *type_die)
1090 {
1091 	LIST_HEAD(basic_blocks);
1092 	struct die_var_type *var_types = NULL;
1093 	u64 src_ip, dst_ip, prev_dst_ip;
1094 	int ret = -1;
1095 
1096 	/* TODO: other architecture support */
1097 	if (!arch__is(dloc->arch, "x86"))
1098 		return -1;
1099 
1100 	prev_dst_ip = dst_ip = dloc->ip;
1101 	for (int i = nr_scopes - 1; i >= 0; i--) {
1102 		Dwarf_Addr base, start, end;
1103 		LIST_HEAD(this_blocks);
1104 		int found;
1105 
1106 		if (dwarf_ranges(&scopes[i], 0, &base, &start, &end) < 0)
1107 			break;
1108 
1109 		pr_debug_dtp("scope: [%d/%d] (die:%lx)\n",
1110 			     i + 1, nr_scopes, (long)dwarf_dieoffset(&scopes[i]));
1111 		src_ip = map__objdump_2rip(dloc->ms->map, start);
1112 
1113 again:
1114 		/* Get basic blocks for this scope */
1115 		if (annotate_get_basic_blocks(dloc->ms->sym, src_ip, dst_ip,
1116 					      &this_blocks) < 0) {
1117 			/* Try previous block if they are not connected */
1118 			if (prev_dst_ip != dst_ip) {
1119 				dst_ip = prev_dst_ip;
1120 				goto again;
1121 			}
1122 
1123 			pr_debug_dtp("cannot find a basic block from %"PRIx64" to %"PRIx64"\n",
1124 				     src_ip - dloc->ms->sym->start,
1125 				     dst_ip - dloc->ms->sym->start);
1126 			continue;
1127 		}
1128 		prepend_basic_blocks(&this_blocks, &basic_blocks);
1129 
1130 		/* Get variable info for this scope and add to var_types list */
1131 		die_collect_vars(&scopes[i], &var_types);
1132 		fixup_var_address(var_types, start);
1133 
1134 		/* Find from start of this scope to the target instruction */
1135 		found = find_data_type_insn(dloc, &basic_blocks, var_types,
1136 					    cu_die, type_die);
1137 		if (found > 0) {
1138 			char buf[64];
1139 
1140 			if (dloc->op->multi_regs)
1141 				snprintf(buf, sizeof(buf), "reg%d, reg%d",
1142 					 dloc->op->reg1, dloc->op->reg2);
1143 			else
1144 				snprintf(buf, sizeof(buf), "reg%d", dloc->op->reg1);
1145 
1146 			pr_debug_dtp("found by insn track: %#x(%s) type-offset=%#x\n",
1147 				     dloc->op->offset, buf, dloc->type_offset);
1148 			pr_debug_type_name(type_die, TSR_KIND_TYPE);
1149 			ret = 0;
1150 			break;
1151 		}
1152 
1153 		if (found < 0)
1154 			break;
1155 
1156 		/* Go up to the next scope and find blocks to the start */
1157 		prev_dst_ip = dst_ip;
1158 		dst_ip = src_ip;
1159 	}
1160 
1161 	delete_basic_blocks(&basic_blocks);
1162 	delete_var_types(var_types);
1163 	return ret;
1164 }
1165 
1166 /* The result will be saved in @type_die */
1167 static int find_data_type_die(struct data_loc_info *dloc, Dwarf_Die *type_die)
1168 {
1169 	struct annotated_op_loc *loc = dloc->op;
1170 	Dwarf_Die cu_die, var_die;
1171 	Dwarf_Die *scopes = NULL;
1172 	int reg, offset;
1173 	int ret = -1;
1174 	int i, nr_scopes;
1175 	int fbreg = -1;
1176 	int fb_offset = 0;
1177 	bool is_fbreg = false;
1178 	u64 pc;
1179 	char buf[64];
1180 
1181 	if (dloc->op->multi_regs)
1182 		snprintf(buf, sizeof(buf), "reg%d, reg%d", dloc->op->reg1, dloc->op->reg2);
1183 	else if (dloc->op->reg1 == DWARF_REG_PC)
1184 		snprintf(buf, sizeof(buf), "PC");
1185 	else
1186 		snprintf(buf, sizeof(buf), "reg%d", dloc->op->reg1);
1187 
1188 	pr_debug_dtp("-----------------------------------------------------------\n");
1189 	pr_debug_dtp("find data type for %#x(%s) at %s+%#"PRIx64"\n",
1190 		     dloc->op->offset, buf, dloc->ms->sym->name,
1191 		     dloc->ip - dloc->ms->sym->start);
1192 
1193 	/*
1194 	 * IP is a relative instruction address from the start of the map, as
1195 	 * it can be randomized/relocated, it needs to translate to PC which is
1196 	 * a file address for DWARF processing.
1197 	 */
1198 	pc = map__rip_2objdump(dloc->ms->map, dloc->ip);
1199 
1200 	/* Get a compile_unit for this address */
1201 	if (!find_cu_die(dloc->di, pc, &cu_die)) {
1202 		pr_debug_dtp("cannot find CU for address %"PRIx64"\n", pc);
1203 		ann_data_stat.no_cuinfo++;
1204 		return -1;
1205 	}
1206 
1207 	reg = loc->reg1;
1208 	offset = loc->offset;
1209 
1210 	pr_debug_dtp("CU for %s (die:%#lx)\n",
1211 		     dwarf_diename(&cu_die), (long)dwarf_dieoffset(&cu_die));
1212 
1213 	if (reg == DWARF_REG_PC) {
1214 		if (get_global_var_type(&cu_die, dloc, dloc->ip, dloc->var_addr,
1215 					&offset, type_die)) {
1216 			dloc->type_offset = offset;
1217 
1218 			pr_debug_dtp("found by addr=%#"PRIx64" type_offset=%#x\n",
1219 				     dloc->var_addr, offset);
1220 			pr_debug_type_name(type_die, TSR_KIND_TYPE);
1221 			ret = 0;
1222 			goto out;
1223 		}
1224 	}
1225 
1226 	/* Get a list of nested scopes - i.e. (inlined) functions and blocks. */
1227 	nr_scopes = die_get_scopes(&cu_die, pc, &scopes);
1228 
1229 	if (reg != DWARF_REG_PC && dwarf_hasattr(&scopes[0], DW_AT_frame_base)) {
1230 		Dwarf_Attribute attr;
1231 		Dwarf_Block block;
1232 
1233 		/* Check if the 'reg' is assigned as frame base register */
1234 		if (dwarf_attr(&scopes[0], DW_AT_frame_base, &attr) != NULL &&
1235 		    dwarf_formblock(&attr, &block) == 0 && block.length == 1) {
1236 			switch (*block.data) {
1237 			case DW_OP_reg0 ... DW_OP_reg31:
1238 				fbreg = dloc->fbreg = *block.data - DW_OP_reg0;
1239 				break;
1240 			case DW_OP_call_frame_cfa:
1241 				dloc->fb_cfa = true;
1242 				if (die_get_cfa(dloc->di->dbg, pc, &fbreg,
1243 						&fb_offset) < 0)
1244 					fbreg = -1;
1245 				break;
1246 			default:
1247 				break;
1248 			}
1249 
1250 			pr_debug_dtp("frame base: cfa=%d fbreg=%d\n",
1251 				     dloc->fb_cfa, fbreg);
1252 		}
1253 	}
1254 
1255 retry:
1256 	is_fbreg = (reg == fbreg);
1257 	if (is_fbreg)
1258 		offset = loc->offset - fb_offset;
1259 
1260 	/* Search from the inner-most scope to the outer */
1261 	for (i = nr_scopes - 1; i >= 0; i--) {
1262 		if (reg == DWARF_REG_PC) {
1263 			if (!die_find_variable_by_addr(&scopes[i], dloc->var_addr,
1264 						       &var_die, &offset))
1265 				continue;
1266 		} else {
1267 			/* Look up variables/parameters in this scope */
1268 			if (!die_find_variable_by_reg(&scopes[i], pc, reg,
1269 						      &offset, is_fbreg, &var_die))
1270 				continue;
1271 		}
1272 
1273 		/* Found a variable, see if it's correct */
1274 		ret = check_variable(dloc, &var_die, type_die, reg, offset, is_fbreg);
1275 		if (ret == 0) {
1276 			pr_debug_dtp("found \"%s\" in scope=%d/%d (die: %#lx) ",
1277 				     dwarf_diename(&var_die), i+1, nr_scopes,
1278 				     (long)dwarf_dieoffset(&scopes[i]));
1279 			if (reg == DWARF_REG_PC) {
1280 				pr_debug_dtp("addr=%#"PRIx64" type_offset=%#x\n",
1281 					     dloc->var_addr, offset);
1282 			} else if (reg == DWARF_REG_FB || is_fbreg) {
1283 				pr_debug_dtp("stack_offset=%#x type_offset=%#x\n",
1284 					     fb_offset, offset);
1285 			} else {
1286 				pr_debug_dtp("type_offset=%#x\n", offset);
1287 			}
1288 			pr_debug_location(&var_die, pc, reg);
1289 			pr_debug_type_name(type_die, TSR_KIND_TYPE);
1290 		} else {
1291 			pr_debug_dtp("check variable \"%s\" failed (die: %#lx)\n",
1292 				     dwarf_diename(&var_die),
1293 				     (long)dwarf_dieoffset(&var_die));
1294 			pr_debug_location(&var_die, pc, reg);
1295 			pr_debug_type_name(type_die, TSR_KIND_TYPE);
1296 		}
1297 		dloc->type_offset = offset;
1298 		goto out;
1299 	}
1300 
1301 	if (loc->multi_regs && reg == loc->reg1 && loc->reg1 != loc->reg2) {
1302 		reg = loc->reg2;
1303 		goto retry;
1304 	}
1305 
1306 	if (reg != DWARF_REG_PC) {
1307 		ret = find_data_type_block(dloc, &cu_die, scopes,
1308 					   nr_scopes, type_die);
1309 		if (ret == 0) {
1310 			ann_data_stat.insn_track++;
1311 			goto out;
1312 		}
1313 	}
1314 
1315 	if (ret < 0) {
1316 		pr_debug_dtp("no variable found\n");
1317 		ann_data_stat.no_var++;
1318 	}
1319 
1320 out:
1321 	free(scopes);
1322 	return ret;
1323 }
1324 
1325 /**
1326  * find_data_type - Return a data type at the location
1327  * @dloc: data location
1328  *
1329  * This functions searches the debug information of the binary to get the data
1330  * type it accesses.  The exact location is expressed by (ip, reg, offset)
1331  * for pointer variables or (ip, addr) for global variables.  Note that global
1332  * variables might update the @dloc->type_offset after finding the start of the
1333  * variable.  If it cannot find a global variable by address, it tried to find
1334  * a declaration of the variable using var_name.  In that case, @dloc->offset
1335  * won't be updated.
1336  *
1337  * It return %NULL if not found.
1338  */
1339 struct annotated_data_type *find_data_type(struct data_loc_info *dloc)
1340 {
1341 	struct annotated_data_type *result = NULL;
1342 	struct dso *dso = map__dso(dloc->ms->map);
1343 	Dwarf_Die type_die;
1344 
1345 	dloc->di = debuginfo__new(dso__long_name(dso));
1346 	if (dloc->di == NULL) {
1347 		pr_debug_dtp("cannot get the debug info\n");
1348 		return NULL;
1349 	}
1350 
1351 	/*
1352 	 * The type offset is the same as instruction offset by default.
1353 	 * But when finding a global variable, the offset won't be valid.
1354 	 */
1355 	dloc->type_offset = dloc->op->offset;
1356 
1357 	dloc->fbreg = -1;
1358 
1359 	if (find_data_type_die(dloc, &type_die) < 0)
1360 		goto out;
1361 
1362 	result = dso__findnew_data_type(dso, &type_die);
1363 
1364 out:
1365 	debuginfo__delete(dloc->di);
1366 	return result;
1367 }
1368 
1369 static int alloc_data_type_histograms(struct annotated_data_type *adt, int nr_entries)
1370 {
1371 	int i;
1372 	size_t sz = sizeof(struct type_hist);
1373 
1374 	sz += sizeof(struct type_hist_entry) * adt->self.size;
1375 
1376 	/* Allocate a table of pointers for each event */
1377 	adt->histograms = calloc(nr_entries, sizeof(*adt->histograms));
1378 	if (adt->histograms == NULL)
1379 		return -ENOMEM;
1380 
1381 	/*
1382 	 * Each histogram is allocated for the whole size of the type.
1383 	 * TODO: Probably we can move the histogram to members.
1384 	 */
1385 	for (i = 0; i < nr_entries; i++) {
1386 		adt->histograms[i] = zalloc(sz);
1387 		if (adt->histograms[i] == NULL)
1388 			goto err;
1389 	}
1390 
1391 	adt->nr_histograms = nr_entries;
1392 	return 0;
1393 
1394 err:
1395 	while (--i >= 0)
1396 		zfree(&(adt->histograms[i]));
1397 	zfree(&adt->histograms);
1398 	return -ENOMEM;
1399 }
1400 
1401 static void delete_data_type_histograms(struct annotated_data_type *adt)
1402 {
1403 	for (int i = 0; i < adt->nr_histograms; i++)
1404 		zfree(&(adt->histograms[i]));
1405 
1406 	zfree(&adt->histograms);
1407 	adt->nr_histograms = 0;
1408 }
1409 
1410 void annotated_data_type__tree_delete(struct rb_root *root)
1411 {
1412 	struct annotated_data_type *pos;
1413 
1414 	while (!RB_EMPTY_ROOT(root)) {
1415 		struct rb_node *node = rb_first(root);
1416 
1417 		rb_erase(node, root);
1418 		pos = rb_entry(node, struct annotated_data_type, node);
1419 		delete_members(&pos->self);
1420 		delete_data_type_histograms(pos);
1421 		zfree(&pos->self.type_name);
1422 		free(pos);
1423 	}
1424 }
1425 
1426 /**
1427  * annotated_data_type__update_samples - Update histogram
1428  * @adt: Data type to update
1429  * @evsel: Event to update
1430  * @offset: Offset in the type
1431  * @nr_samples: Number of samples at this offset
1432  * @period: Event count at this offset
1433  *
1434  * This function updates type histogram at @ofs for @evsel.  Samples are
1435  * aggregated before calling this function so it can be called with more
1436  * than one samples at a certain offset.
1437  */
1438 int annotated_data_type__update_samples(struct annotated_data_type *adt,
1439 					struct evsel *evsel, int offset,
1440 					int nr_samples, u64 period)
1441 {
1442 	struct type_hist *h;
1443 
1444 	if (adt == NULL)
1445 		return 0;
1446 
1447 	if (adt->histograms == NULL) {
1448 		int nr = evsel->evlist->core.nr_entries;
1449 
1450 		if (alloc_data_type_histograms(adt, nr) < 0)
1451 			return -1;
1452 	}
1453 
1454 	if (offset < 0 || offset >= adt->self.size)
1455 		return -1;
1456 
1457 	h = adt->histograms[evsel->core.idx];
1458 
1459 	h->nr_samples += nr_samples;
1460 	h->addr[offset].nr_samples += nr_samples;
1461 	h->period += period;
1462 	h->addr[offset].period += period;
1463 	return 0;
1464 }
1465 
1466 static void print_annotated_data_header(struct hist_entry *he, struct evsel *evsel)
1467 {
1468 	struct dso *dso = map__dso(he->ms.map);
1469 	int nr_members = 1;
1470 	int nr_samples = he->stat.nr_events;
1471 	int width = 7;
1472 	const char *val_hdr = "Percent";
1473 
1474 	if (evsel__is_group_event(evsel)) {
1475 		struct hist_entry *pair;
1476 
1477 		list_for_each_entry(pair, &he->pairs.head, pairs.node)
1478 			nr_samples += pair->stat.nr_events;
1479 	}
1480 
1481 	printf("Annotate type: '%s' in %s (%d samples):\n",
1482 	       he->mem_type->self.type_name, dso__name(dso), nr_samples);
1483 
1484 	if (evsel__is_group_event(evsel)) {
1485 		struct evsel *pos;
1486 		int i = 0;
1487 
1488 		for_each_group_evsel(pos, evsel)
1489 			printf(" event[%d] = %s\n", i++, pos->name);
1490 
1491 		nr_members = evsel->core.nr_members;
1492 	}
1493 
1494 	if (symbol_conf.show_total_period) {
1495 		width = 11;
1496 		val_hdr = "Period";
1497 	} else if (symbol_conf.show_nr_samples) {
1498 		width = 7;
1499 		val_hdr = "Samples";
1500 	}
1501 
1502 	printf("============================================================================\n");
1503 	printf("%*s %10s %10s  %s\n", (width + 1) * nr_members, val_hdr,
1504 	       "offset", "size", "field");
1505 }
1506 
1507 static void print_annotated_data_value(struct type_hist *h, u64 period, int nr_samples)
1508 {
1509 	double percent = h->period ? (100.0 * period / h->period) : 0;
1510 	const char *color = get_percent_color(percent);
1511 
1512 	if (symbol_conf.show_total_period)
1513 		color_fprintf(stdout, color, " %11" PRIu64, period);
1514 	else if (symbol_conf.show_nr_samples)
1515 		color_fprintf(stdout, color, " %7d", nr_samples);
1516 	else
1517 		color_fprintf(stdout, color, " %7.2f", percent);
1518 }
1519 
1520 static void print_annotated_data_type(struct annotated_data_type *mem_type,
1521 				      struct annotated_member *member,
1522 				      struct evsel *evsel, int indent)
1523 {
1524 	struct annotated_member *child;
1525 	struct type_hist *h = mem_type->histograms[evsel->core.idx];
1526 	int i, nr_events = 1, samples = 0;
1527 	u64 period = 0;
1528 	int width = symbol_conf.show_total_period ? 11 : 7;
1529 
1530 	for (i = 0; i < member->size; i++) {
1531 		samples += h->addr[member->offset + i].nr_samples;
1532 		period += h->addr[member->offset + i].period;
1533 	}
1534 	print_annotated_data_value(h, period, samples);
1535 
1536 	if (evsel__is_group_event(evsel)) {
1537 		struct evsel *pos;
1538 
1539 		for_each_group_member(pos, evsel) {
1540 			h = mem_type->histograms[pos->core.idx];
1541 
1542 			samples = 0;
1543 			period = 0;
1544 			for (i = 0; i < member->size; i++) {
1545 				samples += h->addr[member->offset + i].nr_samples;
1546 				period += h->addr[member->offset + i].period;
1547 			}
1548 			print_annotated_data_value(h, period, samples);
1549 		}
1550 		nr_events = evsel->core.nr_members;
1551 	}
1552 
1553 	printf(" %10d %10d  %*s%s\t%s",
1554 	       member->offset, member->size, indent, "", member->type_name,
1555 	       member->var_name ?: "");
1556 
1557 	if (!list_empty(&member->children))
1558 		printf(" {\n");
1559 
1560 	list_for_each_entry(child, &member->children, node)
1561 		print_annotated_data_type(mem_type, child, evsel, indent + 4);
1562 
1563 	if (!list_empty(&member->children))
1564 		printf("%*s}", (width + 1) * nr_events + 24 + indent, "");
1565 	printf(";\n");
1566 }
1567 
1568 int hist_entry__annotate_data_tty(struct hist_entry *he, struct evsel *evsel)
1569 {
1570 	print_annotated_data_header(he, evsel);
1571 	print_annotated_data_type(he->mem_type, &he->mem_type->self, evsel, 0);
1572 	printf("\n");
1573 
1574 	/* move to the next entry */
1575 	return '>';
1576 }
1577