File Index
Files and directories
Sign in
FILE

annotate-data.c

File preview with syntax highlighting.

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