xref: /linux/tools/perf/util/annotate-data.c (revision d261f9ebcf424535fe04e720a1cfa023be409f52)
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 static int arch_supports_insn_tracking(struct data_loc_info *dloc)
1084 {
1085 	if ((arch__is(dloc->arch, "x86")) || (arch__is(dloc->arch, "powerpc")))
1086 		return 1;
1087 	return 0;
1088 }
1089 
1090 /*
1091  * Construct a list of basic blocks for each scope with variables and try to find
1092  * the data type by updating a type state table through instructions.
1093  */
1094 static int find_data_type_block(struct data_loc_info *dloc,
1095 				Dwarf_Die *cu_die, Dwarf_Die *scopes,
1096 				int nr_scopes, Dwarf_Die *type_die)
1097 {
1098 	LIST_HEAD(basic_blocks);
1099 	struct die_var_type *var_types = NULL;
1100 	u64 src_ip, dst_ip, prev_dst_ip;
1101 	int ret = -1;
1102 
1103 	/* TODO: other architecture support */
1104 	if (!arch_supports_insn_tracking(dloc))
1105 		return -1;
1106 
1107 	prev_dst_ip = dst_ip = dloc->ip;
1108 	for (int i = nr_scopes - 1; i >= 0; i--) {
1109 		Dwarf_Addr base, start, end;
1110 		LIST_HEAD(this_blocks);
1111 		int found;
1112 
1113 		if (dwarf_ranges(&scopes[i], 0, &base, &start, &end) < 0)
1114 			break;
1115 
1116 		pr_debug_dtp("scope: [%d/%d] (die:%lx)\n",
1117 			     i + 1, nr_scopes, (long)dwarf_dieoffset(&scopes[i]));
1118 		src_ip = map__objdump_2rip(dloc->ms->map, start);
1119 
1120 again:
1121 		/* Get basic blocks for this scope */
1122 		if (annotate_get_basic_blocks(dloc->ms->sym, src_ip, dst_ip,
1123 					      &this_blocks) < 0) {
1124 			/* Try previous block if they are not connected */
1125 			if (prev_dst_ip != dst_ip) {
1126 				dst_ip = prev_dst_ip;
1127 				goto again;
1128 			}
1129 
1130 			pr_debug_dtp("cannot find a basic block from %"PRIx64" to %"PRIx64"\n",
1131 				     src_ip - dloc->ms->sym->start,
1132 				     dst_ip - dloc->ms->sym->start);
1133 			continue;
1134 		}
1135 		prepend_basic_blocks(&this_blocks, &basic_blocks);
1136 
1137 		/* Get variable info for this scope and add to var_types list */
1138 		die_collect_vars(&scopes[i], &var_types);
1139 		fixup_var_address(var_types, start);
1140 
1141 		/* Find from start of this scope to the target instruction */
1142 		found = find_data_type_insn(dloc, &basic_blocks, var_types,
1143 					    cu_die, type_die);
1144 		if (found > 0) {
1145 			char buf[64];
1146 
1147 			if (dloc->op->multi_regs)
1148 				snprintf(buf, sizeof(buf), "reg%d, reg%d",
1149 					 dloc->op->reg1, dloc->op->reg2);
1150 			else
1151 				snprintf(buf, sizeof(buf), "reg%d", dloc->op->reg1);
1152 
1153 			pr_debug_dtp("found by insn track: %#x(%s) type-offset=%#x\n",
1154 				     dloc->op->offset, buf, dloc->type_offset);
1155 			pr_debug_type_name(type_die, TSR_KIND_TYPE);
1156 			ret = 0;
1157 			break;
1158 		}
1159 
1160 		if (found < 0)
1161 			break;
1162 
1163 		/* Go up to the next scope and find blocks to the start */
1164 		prev_dst_ip = dst_ip;
1165 		dst_ip = src_ip;
1166 	}
1167 
1168 	delete_basic_blocks(&basic_blocks);
1169 	delete_var_types(var_types);
1170 	return ret;
1171 }
1172 
1173 /* The result will be saved in @type_die */
1174 static int find_data_type_die(struct data_loc_info *dloc, Dwarf_Die *type_die)
1175 {
1176 	struct annotated_op_loc *loc = dloc->op;
1177 	Dwarf_Die cu_die, var_die;
1178 	Dwarf_Die *scopes = NULL;
1179 	int reg, offset;
1180 	int ret = -1;
1181 	int i, nr_scopes;
1182 	int fbreg = -1;
1183 	int fb_offset = 0;
1184 	bool is_fbreg = false;
1185 	u64 pc;
1186 	char buf[64];
1187 
1188 	if (dloc->op->multi_regs)
1189 		snprintf(buf, sizeof(buf), "reg%d, reg%d", dloc->op->reg1, dloc->op->reg2);
1190 	else if (dloc->op->reg1 == DWARF_REG_PC)
1191 		snprintf(buf, sizeof(buf), "PC");
1192 	else
1193 		snprintf(buf, sizeof(buf), "reg%d", dloc->op->reg1);
1194 
1195 	pr_debug_dtp("-----------------------------------------------------------\n");
1196 	pr_debug_dtp("find data type for %#x(%s) at %s+%#"PRIx64"\n",
1197 		     dloc->op->offset, buf, dloc->ms->sym->name,
1198 		     dloc->ip - dloc->ms->sym->start);
1199 
1200 	/*
1201 	 * IP is a relative instruction address from the start of the map, as
1202 	 * it can be randomized/relocated, it needs to translate to PC which is
1203 	 * a file address for DWARF processing.
1204 	 */
1205 	pc = map__rip_2objdump(dloc->ms->map, dloc->ip);
1206 
1207 	/* Get a compile_unit for this address */
1208 	if (!find_cu_die(dloc->di, pc, &cu_die)) {
1209 		pr_debug_dtp("cannot find CU for address %"PRIx64"\n", pc);
1210 		ann_data_stat.no_cuinfo++;
1211 		return -1;
1212 	}
1213 
1214 	reg = loc->reg1;
1215 	offset = loc->offset;
1216 
1217 	pr_debug_dtp("CU for %s (die:%#lx)\n",
1218 		     dwarf_diename(&cu_die), (long)dwarf_dieoffset(&cu_die));
1219 
1220 	if (reg == DWARF_REG_PC) {
1221 		if (get_global_var_type(&cu_die, dloc, dloc->ip, dloc->var_addr,
1222 					&offset, type_die)) {
1223 			dloc->type_offset = offset;
1224 
1225 			pr_debug_dtp("found by addr=%#"PRIx64" type_offset=%#x\n",
1226 				     dloc->var_addr, offset);
1227 			pr_debug_type_name(type_die, TSR_KIND_TYPE);
1228 			ret = 0;
1229 			goto out;
1230 		}
1231 	}
1232 
1233 	/* Get a list of nested scopes - i.e. (inlined) functions and blocks. */
1234 	nr_scopes = die_get_scopes(&cu_die, pc, &scopes);
1235 
1236 	if (reg != DWARF_REG_PC && dwarf_hasattr(&scopes[0], DW_AT_frame_base)) {
1237 		Dwarf_Attribute attr;
1238 		Dwarf_Block block;
1239 
1240 		/* Check if the 'reg' is assigned as frame base register */
1241 		if (dwarf_attr(&scopes[0], DW_AT_frame_base, &attr) != NULL &&
1242 		    dwarf_formblock(&attr, &block) == 0 && block.length == 1) {
1243 			switch (*block.data) {
1244 			case DW_OP_reg0 ... DW_OP_reg31:
1245 				fbreg = dloc->fbreg = *block.data - DW_OP_reg0;
1246 				break;
1247 			case DW_OP_call_frame_cfa:
1248 				dloc->fb_cfa = true;
1249 				if (die_get_cfa(dloc->di->dbg, pc, &fbreg,
1250 						&fb_offset) < 0)
1251 					fbreg = -1;
1252 				break;
1253 			default:
1254 				break;
1255 			}
1256 
1257 			pr_debug_dtp("frame base: cfa=%d fbreg=%d\n",
1258 				     dloc->fb_cfa, fbreg);
1259 		}
1260 	}
1261 
1262 retry:
1263 	is_fbreg = (reg == fbreg);
1264 	if (is_fbreg)
1265 		offset = loc->offset - fb_offset;
1266 
1267 	/* Search from the inner-most scope to the outer */
1268 	for (i = nr_scopes - 1; i >= 0; i--) {
1269 		if (reg == DWARF_REG_PC) {
1270 			if (!die_find_variable_by_addr(&scopes[i], dloc->var_addr,
1271 						       &var_die, &offset))
1272 				continue;
1273 		} else {
1274 			/* Look up variables/parameters in this scope */
1275 			if (!die_find_variable_by_reg(&scopes[i], pc, reg,
1276 						      &offset, is_fbreg, &var_die))
1277 				continue;
1278 		}
1279 
1280 		/* Found a variable, see if it's correct */
1281 		ret = check_variable(dloc, &var_die, type_die, reg, offset, is_fbreg);
1282 		if (ret == 0) {
1283 			pr_debug_dtp("found \"%s\" in scope=%d/%d (die: %#lx) ",
1284 				     dwarf_diename(&var_die), i+1, nr_scopes,
1285 				     (long)dwarf_dieoffset(&scopes[i]));
1286 			if (reg == DWARF_REG_PC) {
1287 				pr_debug_dtp("addr=%#"PRIx64" type_offset=%#x\n",
1288 					     dloc->var_addr, offset);
1289 			} else if (reg == DWARF_REG_FB || is_fbreg) {
1290 				pr_debug_dtp("stack_offset=%#x type_offset=%#x\n",
1291 					     fb_offset, offset);
1292 			} else {
1293 				pr_debug_dtp("type_offset=%#x\n", offset);
1294 			}
1295 			pr_debug_location(&var_die, pc, reg);
1296 			pr_debug_type_name(type_die, TSR_KIND_TYPE);
1297 		} else {
1298 			pr_debug_dtp("check variable \"%s\" failed (die: %#lx)\n",
1299 				     dwarf_diename(&var_die),
1300 				     (long)dwarf_dieoffset(&var_die));
1301 			pr_debug_location(&var_die, pc, reg);
1302 			pr_debug_type_name(type_die, TSR_KIND_TYPE);
1303 		}
1304 		dloc->type_offset = offset;
1305 		goto out;
1306 	}
1307 
1308 	if (loc->multi_regs && reg == loc->reg1 && loc->reg1 != loc->reg2) {
1309 		reg = loc->reg2;
1310 		goto retry;
1311 	}
1312 
1313 	if (reg != DWARF_REG_PC) {
1314 		ret = find_data_type_block(dloc, &cu_die, scopes,
1315 					   nr_scopes, type_die);
1316 		if (ret == 0) {
1317 			ann_data_stat.insn_track++;
1318 			goto out;
1319 		}
1320 	}
1321 
1322 	if (ret < 0) {
1323 		pr_debug_dtp("no variable found\n");
1324 		ann_data_stat.no_var++;
1325 	}
1326 
1327 out:
1328 	free(scopes);
1329 	return ret;
1330 }
1331 
1332 /**
1333  * find_data_type - Return a data type at the location
1334  * @dloc: data location
1335  *
1336  * This functions searches the debug information of the binary to get the data
1337  * type it accesses.  The exact location is expressed by (ip, reg, offset)
1338  * for pointer variables or (ip, addr) for global variables.  Note that global
1339  * variables might update the @dloc->type_offset after finding the start of the
1340  * variable.  If it cannot find a global variable by address, it tried to find
1341  * a declaration of the variable using var_name.  In that case, @dloc->offset
1342  * won't be updated.
1343  *
1344  * It return %NULL if not found.
1345  */
1346 struct annotated_data_type *find_data_type(struct data_loc_info *dloc)
1347 {
1348 	struct annotated_data_type *result = NULL;
1349 	struct dso *dso = map__dso(dloc->ms->map);
1350 	Dwarf_Die type_die;
1351 
1352 	dloc->di = debuginfo__new(dso__long_name(dso));
1353 	if (dloc->di == NULL) {
1354 		pr_debug_dtp("cannot get the debug info\n");
1355 		return NULL;
1356 	}
1357 
1358 	/*
1359 	 * The type offset is the same as instruction offset by default.
1360 	 * But when finding a global variable, the offset won't be valid.
1361 	 */
1362 	dloc->type_offset = dloc->op->offset;
1363 
1364 	dloc->fbreg = -1;
1365 
1366 	if (find_data_type_die(dloc, &type_die) < 0)
1367 		goto out;
1368 
1369 	result = dso__findnew_data_type(dso, &type_die);
1370 
1371 out:
1372 	debuginfo__delete(dloc->di);
1373 	return result;
1374 }
1375 
1376 static int alloc_data_type_histograms(struct annotated_data_type *adt, int nr_entries)
1377 {
1378 	int i;
1379 	size_t sz = sizeof(struct type_hist);
1380 
1381 	sz += sizeof(struct type_hist_entry) * adt->self.size;
1382 
1383 	/* Allocate a table of pointers for each event */
1384 	adt->histograms = calloc(nr_entries, sizeof(*adt->histograms));
1385 	if (adt->histograms == NULL)
1386 		return -ENOMEM;
1387 
1388 	/*
1389 	 * Each histogram is allocated for the whole size of the type.
1390 	 * TODO: Probably we can move the histogram to members.
1391 	 */
1392 	for (i = 0; i < nr_entries; i++) {
1393 		adt->histograms[i] = zalloc(sz);
1394 		if (adt->histograms[i] == NULL)
1395 			goto err;
1396 	}
1397 
1398 	adt->nr_histograms = nr_entries;
1399 	return 0;
1400 
1401 err:
1402 	while (--i >= 0)
1403 		zfree(&(adt->histograms[i]));
1404 	zfree(&adt->histograms);
1405 	return -ENOMEM;
1406 }
1407 
1408 static void delete_data_type_histograms(struct annotated_data_type *adt)
1409 {
1410 	for (int i = 0; i < adt->nr_histograms; i++)
1411 		zfree(&(adt->histograms[i]));
1412 
1413 	zfree(&adt->histograms);
1414 	adt->nr_histograms = 0;
1415 }
1416 
1417 void annotated_data_type__tree_delete(struct rb_root *root)
1418 {
1419 	struct annotated_data_type *pos;
1420 
1421 	while (!RB_EMPTY_ROOT(root)) {
1422 		struct rb_node *node = rb_first(root);
1423 
1424 		rb_erase(node, root);
1425 		pos = rb_entry(node, struct annotated_data_type, node);
1426 		delete_members(&pos->self);
1427 		delete_data_type_histograms(pos);
1428 		zfree(&pos->self.type_name);
1429 		free(pos);
1430 	}
1431 }
1432 
1433 /**
1434  * annotated_data_type__update_samples - Update histogram
1435  * @adt: Data type to update
1436  * @evsel: Event to update
1437  * @offset: Offset in the type
1438  * @nr_samples: Number of samples at this offset
1439  * @period: Event count at this offset
1440  *
1441  * This function updates type histogram at @ofs for @evsel.  Samples are
1442  * aggregated before calling this function so it can be called with more
1443  * than one samples at a certain offset.
1444  */
1445 int annotated_data_type__update_samples(struct annotated_data_type *adt,
1446 					struct evsel *evsel, int offset,
1447 					int nr_samples, u64 period)
1448 {
1449 	struct type_hist *h;
1450 
1451 	if (adt == NULL)
1452 		return 0;
1453 
1454 	if (adt->histograms == NULL) {
1455 		int nr = evsel->evlist->core.nr_entries;
1456 
1457 		if (alloc_data_type_histograms(adt, nr) < 0)
1458 			return -1;
1459 	}
1460 
1461 	if (offset < 0 || offset >= adt->self.size)
1462 		return -1;
1463 
1464 	h = adt->histograms[evsel->core.idx];
1465 
1466 	h->nr_samples += nr_samples;
1467 	h->addr[offset].nr_samples += nr_samples;
1468 	h->period += period;
1469 	h->addr[offset].period += period;
1470 	return 0;
1471 }
1472 
1473 static void print_annotated_data_header(struct hist_entry *he, struct evsel *evsel)
1474 {
1475 	struct dso *dso = map__dso(he->ms.map);
1476 	int nr_members = 1;
1477 	int nr_samples = he->stat.nr_events;
1478 	int width = 7;
1479 	const char *val_hdr = "Percent";
1480 
1481 	if (evsel__is_group_event(evsel)) {
1482 		struct hist_entry *pair;
1483 
1484 		list_for_each_entry(pair, &he->pairs.head, pairs.node)
1485 			nr_samples += pair->stat.nr_events;
1486 	}
1487 
1488 	printf("Annotate type: '%s' in %s (%d samples):\n",
1489 	       he->mem_type->self.type_name, dso__name(dso), nr_samples);
1490 
1491 	if (evsel__is_group_event(evsel)) {
1492 		struct evsel *pos;
1493 		int i = 0;
1494 
1495 		for_each_group_evsel(pos, evsel)
1496 			printf(" event[%d] = %s\n", i++, pos->name);
1497 
1498 		nr_members = evsel->core.nr_members;
1499 	}
1500 
1501 	if (symbol_conf.show_total_period) {
1502 		width = 11;
1503 		val_hdr = "Period";
1504 	} else if (symbol_conf.show_nr_samples) {
1505 		width = 7;
1506 		val_hdr = "Samples";
1507 	}
1508 
1509 	printf("============================================================================\n");
1510 	printf("%*s %10s %10s  %s\n", (width + 1) * nr_members, val_hdr,
1511 	       "offset", "size", "field");
1512 }
1513 
1514 static void print_annotated_data_value(struct type_hist *h, u64 period, int nr_samples)
1515 {
1516 	double percent = h->period ? (100.0 * period / h->period) : 0;
1517 	const char *color = get_percent_color(percent);
1518 
1519 	if (symbol_conf.show_total_period)
1520 		color_fprintf(stdout, color, " %11" PRIu64, period);
1521 	else if (symbol_conf.show_nr_samples)
1522 		color_fprintf(stdout, color, " %7d", nr_samples);
1523 	else
1524 		color_fprintf(stdout, color, " %7.2f", percent);
1525 }
1526 
1527 static void print_annotated_data_type(struct annotated_data_type *mem_type,
1528 				      struct annotated_member *member,
1529 				      struct evsel *evsel, int indent)
1530 {
1531 	struct annotated_member *child;
1532 	struct type_hist *h = mem_type->histograms[evsel->core.idx];
1533 	int i, nr_events = 1, samples = 0;
1534 	u64 period = 0;
1535 	int width = symbol_conf.show_total_period ? 11 : 7;
1536 
1537 	for (i = 0; i < member->size; i++) {
1538 		samples += h->addr[member->offset + i].nr_samples;
1539 		period += h->addr[member->offset + i].period;
1540 	}
1541 	print_annotated_data_value(h, period, samples);
1542 
1543 	if (evsel__is_group_event(evsel)) {
1544 		struct evsel *pos;
1545 
1546 		for_each_group_member(pos, evsel) {
1547 			h = mem_type->histograms[pos->core.idx];
1548 
1549 			samples = 0;
1550 			period = 0;
1551 			for (i = 0; i < member->size; i++) {
1552 				samples += h->addr[member->offset + i].nr_samples;
1553 				period += h->addr[member->offset + i].period;
1554 			}
1555 			print_annotated_data_value(h, period, samples);
1556 		}
1557 		nr_events = evsel->core.nr_members;
1558 	}
1559 
1560 	printf(" %10d %10d  %*s%s\t%s",
1561 	       member->offset, member->size, indent, "", member->type_name,
1562 	       member->var_name ?: "");
1563 
1564 	if (!list_empty(&member->children))
1565 		printf(" {\n");
1566 
1567 	list_for_each_entry(child, &member->children, node)
1568 		print_annotated_data_type(mem_type, child, evsel, indent + 4);
1569 
1570 	if (!list_empty(&member->children))
1571 		printf("%*s}", (width + 1) * nr_events + 24 + indent, "");
1572 	printf(";\n");
1573 }
1574 
1575 int hist_entry__annotate_data_tty(struct hist_entry *he, struct evsel *evsel)
1576 {
1577 	print_annotated_data_header(he, evsel);
1578 	print_annotated_data_type(he->mem_type, &he->mem_type->self, evsel, 0);
1579 	printf("\n");
1580 
1581 	/* move to the next entry */
1582 	return '>';
1583 }
1584