summaryrefslogtreecommitdiffstats
path: root/tools/perf/util/annotate-data.h
blob: 8ac0fd94a0ba4e4b84fe6c6debd0f08cd41a9496 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _PERF_ANNOTATE_DATA_H
#define _PERF_ANNOTATE_DATA_H

#include <errno.h>
#include <linux/compiler.h>
#include <linux/rbtree.h>
#include <linux/types.h>
#include "dwarf-regs.h"
#include "annotate.h"

#ifdef HAVE_DWARF_SUPPORT
#include "debuginfo.h"
#endif

struct annotated_op_loc;
struct debuginfo;
struct evsel;
struct hist_browser_timer;
struct hist_entry;
struct map_symbol;
struct thread;

#define pr_debug_dtp(fmt, ...)					\
do {								\
	if (debug_type_profile)					\
		pr_info(fmt, ##__VA_ARGS__);			\
	else							\
		pr_debug3(fmt, ##__VA_ARGS__);			\
} while (0)

enum type_state_kind {
	TSR_KIND_INVALID = 0,
	TSR_KIND_TYPE,
	TSR_KIND_PERCPU_BASE,
	TSR_KIND_CONST,
	TSR_KIND_POINTER,
	TSR_KIND_CANARY,
};

/**
 * struct annotated_member - Type of member field
 * @node: List entry in the parent list
 * @children: List head for child nodes
 * @type_name: Name of the member type
 * @var_name: Name of the member variable
 * @offset: Offset from the outer data type
 * @size: Size of the member field
 *
 * This represents a member type in a data type.
 */
struct annotated_member {
	struct list_head node;
	struct list_head children;
	char *type_name;
	char *var_name;
	int offset;
	int size;
};

/**
 * struct type_hist_entry - Histogram entry per offset
 * @nr_samples: Number of samples
 * @period: Count of event
 */
struct type_hist_entry {
	int nr_samples;
	u64 period;
};

/**
 * struct type_hist - Type histogram for each event
 * @nr_samples: Total number of samples in this data type
 * @period: Total count of the event in this data type
 * @offset: Array of histogram entry
 */
struct type_hist {
	u64			nr_samples;
	u64			period;
	struct type_hist_entry	addr[];
};

/**
 * struct annotated_data_type - Data type to profile
 * @node: RB-tree node for dso->type_tree
 * @self: Actual type information
 * @nr_histogram: Number of histogram entries
 * @histograms: An array of pointers to histograms
 *
 * This represents a data type accessed by samples in the profile data.
 */
struct annotated_data_type {
	struct rb_node node;
	struct annotated_member self;
	int nr_histograms;
	struct type_hist **histograms;
};

extern struct annotated_data_type unknown_type;
extern struct annotated_data_type stackop_type;
extern struct annotated_data_type canary_type;

/**
 * struct data_loc_info - Data location information
 * @arch: CPU architecture info
 * @thread: Thread info
 * @ms: Map and Symbol info
 * @ip: Instruction address
 * @var_addr: Data address (for global variables)
 * @cpumode: CPU execution mode
 * @op: Instruction operand location (regs and offset)
 * @di: Debug info
 * @fbreg: Frame base register
 * @fb_cfa: Whether the frame needs to check CFA
 * @type_offset: Final offset in the type
 */
struct data_loc_info {
	/* These are input field, should be filled by caller */
	struct arch *arch;
	struct thread *thread;
	struct map_symbol *ms;
	u64 ip;
	u64 var_addr;
	u8 cpumode;
	struct annotated_op_loc *op;
	struct debuginfo *di;

	/* These are used internally */
	int fbreg;
	bool fb_cfa;

	/* This is for the result */
	int type_offset;
};

/**
 * struct annotated_data_stat - Debug statistics
 * @total: Total number of entry
 * @no_sym: No symbol or map found
 * @no_insn: Failed to get disasm line
 * @no_insn_ops: The instruction has no operands
 * @no_mem_ops: The instruction has no memory operands
 * @no_reg: Failed to extract a register from the operand
 * @no_dbginfo: The binary has no debug information
 * @no_cuinfo: Failed to find a compile_unit
 * @no_var: Failed to find a matching variable
 * @no_typeinfo: Failed to get a type info for the variable
 * @invalid_size: Failed to get a size info of the type
 * @bad_offset: The access offset is out of the type
 */
struct annotated_data_stat {
	int total;
	int no_sym;
	int no_insn;
	int no_insn_ops;
	int no_mem_ops;
	int no_reg;
	int no_dbginfo;
	int no_cuinfo;
	int no_var;
	int no_typeinfo;
	int invalid_size;
	int bad_offset;
	int insn_track;
};
extern struct annotated_data_stat ann_data_stat;

#ifdef HAVE_DWARF_SUPPORT
/*
 * Type information in a register, valid when @ok is true.
 * The @caller_saved registers are invalidated after a function call.
 */
struct type_state_reg {
	Dwarf_Die type;
	u32 imm_value;
	bool ok;
	bool caller_saved;
	u8 kind;
	u8 copied_from;
};

/* Type information in a stack location, dynamically allocated */
struct type_state_stack {
	struct list_head list;
	Dwarf_Die type;
	int offset;
	int size;
	bool compound;
	u8 kind;
};

/* FIXME: This should be arch-dependent */
#ifdef __powerpc__
#define TYPE_STATE_MAX_REGS  32
#else
#define TYPE_STATE_MAX_REGS  16
#endif

/*
 * State table to maintain type info in each register and stack location.
 * It'll be updated when new variable is allocated or type info is moved
 * to a new location (register or stack).  As it'd be used with the
 * shortest path of basic blocks, it only maintains a single table.
 */
struct type_state {
	/* state of general purpose registers */
	struct type_state_reg regs[TYPE_STATE_MAX_REGS];
	/* state of stack location */
	struct list_head stack_vars;
	/* return value register */
	int ret_reg;
	/* stack pointer register */
	int stack_reg;
};

/* Returns data type at the location (ip, reg, offset) */
struct annotated_data_type *find_data_type(struct data_loc_info *dloc);

/* Update type access histogram at the given offset */
int annotated_data_type__update_samples(struct annotated_data_type *adt,
					struct evsel *evsel, int offset,
					int nr_samples, u64 period);

/* Release all data type information in the tree */
void annotated_data_type__tree_delete(struct rb_root *root);

/* Release all global variable information in the tree */
void global_var_type__tree_delete(struct rb_root *root);

int hist_entry__annotate_data_tty(struct hist_entry *he, struct evsel *evsel);

bool has_reg_type(struct type_state *state, int reg);
struct type_state_stack *findnew_stack_state(struct type_state *state,
						int offset, u8 kind,
						Dwarf_Die *type_die);
void set_stack_state(struct type_state_stack *stack, int offset, u8 kind,
				Dwarf_Die *type_die);
struct type_state_stack *find_stack_state(struct type_state *state,
						int offset);
bool get_global_var_type(Dwarf_Die *cu_die, struct data_loc_info *dloc,
				u64 ip, u64 var_addr, int *var_offset,
				Dwarf_Die *type_die);
bool get_global_var_info(struct data_loc_info *dloc, u64 addr,
				const char **var_name, int *var_offset);
void pr_debug_type_name(Dwarf_Die *die, enum type_state_kind kind);

#else /* HAVE_DWARF_SUPPORT */

static inline struct annotated_data_type *
find_data_type(struct data_loc_info *dloc __maybe_unused)
{
	return NULL;
}

static inline int
annotated_data_type__update_samples(struct annotated_data_type *adt __maybe_unused,
				    struct evsel *evsel __maybe_unused,
				    int offset __maybe_unused,
				    int nr_samples __maybe_unused,
				    u64 period __maybe_unused)
{
	return -1;
}

static inline void annotated_data_type__tree_delete(struct rb_root *root __maybe_unused)
{
}

static inline void global_var_type__tree_delete(struct rb_root *root __maybe_unused)
{
}

static inline int hist_entry__annotate_data_tty(struct hist_entry *he __maybe_unused,
						struct evsel *evsel __maybe_unused)
{
	return -1;
}

#endif /* HAVE_DWARF_SUPPORT */

#ifdef HAVE_SLANG_SUPPORT
int hist_entry__annotate_data_tui(struct hist_entry *he, struct evsel *evsel,
				  struct hist_browser_timer *hbt);
#else
static inline int hist_entry__annotate_data_tui(struct hist_entry *he __maybe_unused,
						struct evsel *evsel __maybe_unused,
						struct hist_browser_timer *hbt __maybe_unused)
{
	return -1;
}
#endif /* HAVE_SLANG_SUPPORT */

#endif /* _PERF_ANNOTATE_DATA_H */