1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Convert sample address to data type using DWARF debug info.
5 * Written by Namhyung Kim <namhyung@kernel.org>
13 #include "annotate-data.h"
14 #include "debuginfo.h"
17 #include "dwarf-regs.h"
21 #include "map_symbol.h"
24 #include "symbol_conf.h"
27 * Compare type name and size to maintain them in a tree.
28 * I'm not sure if DWARF would have information of a single type in many
29 * different places (compilation units). If not, it could compare the
30 * offset of the type entry in the .debug_info section.
32 static int data_type_cmp(const void *_key, const struct rb_node *node)
34 const struct annotated_data_type *key = _key;
35 struct annotated_data_type *type;
37 type = rb_entry(node, struct annotated_data_type, node);
39 if (key->self.size != type->self.size)
40 return key->self.size - type->self.size;
41 return strcmp(key->self.type_name, type->self.type_name);
44 static bool data_type_less(struct rb_node *node_a, const struct rb_node *node_b)
46 struct annotated_data_type *a, *b;
48 a = rb_entry(node_a, struct annotated_data_type, node);
49 b = rb_entry(node_b, struct annotated_data_type, node);
51 if (a->self.size != b->self.size)
52 return a->self.size < b->self.size;
53 return strcmp(a->self.type_name, b->self.type_name) < 0;
56 /* Recursively add new members for struct/union */
57 static int __add_member_cb(Dwarf_Die *die, void *arg)
59 struct annotated_member *parent = arg;
60 struct annotated_member *member;
61 Dwarf_Die member_type, die_mem;
67 if (dwarf_tag(die) != DW_TAG_member)
68 return DIE_FIND_CB_SIBLING;
70 member = zalloc(sizeof(*member));
72 return DIE_FIND_CB_END;
75 die_get_typename(die, &sb);
77 die_get_real_type(die, &member_type);
78 if (dwarf_aggregate_size(&member_type, &size) < 0)
81 if (!dwarf_attr_integrate(die, DW_AT_data_member_location, &attr))
84 dwarf_formudata(&attr, &loc);
86 member->type_name = strbuf_detach(&sb, NULL);
87 /* member->var_name can be NULL */
88 if (dwarf_diename(die))
89 member->var_name = strdup(dwarf_diename(die));
91 member->offset = loc + parent->offset;
92 INIT_LIST_HEAD(&member->children);
93 list_add_tail(&member->node, &parent->children);
95 tag = dwarf_tag(&member_type);
97 case DW_TAG_structure_type:
98 case DW_TAG_union_type:
99 die_find_child(&member_type, __add_member_cb, member, &die_mem);
104 return DIE_FIND_CB_SIBLING;
107 static void add_member_types(struct annotated_data_type *parent, Dwarf_Die *type)
111 die_find_child(type, __add_member_cb, &parent->self, &die_mem);
114 static void delete_members(struct annotated_member *member)
116 struct annotated_member *child, *tmp;
118 list_for_each_entry_safe(child, tmp, &member->children, node) {
119 list_del(&child->node);
120 delete_members(child);
121 free(child->type_name);
122 free(child->var_name);
127 static struct annotated_data_type *dso__findnew_data_type(struct dso *dso,
130 struct annotated_data_type *result = NULL;
131 struct annotated_data_type key;
132 struct rb_node *node;
137 strbuf_init(&sb, 32);
138 if (die_get_typename_from_type(type_die, &sb) < 0)
139 strbuf_add(&sb, "(unknown type)", 14);
140 type_name = strbuf_detach(&sb, NULL);
141 dwarf_aggregate_size(type_die, &size);
143 /* Check existing nodes in dso->data_types tree */
144 key.self.type_name = type_name;
145 key.self.size = size;
146 node = rb_find(&key, &dso->data_types, data_type_cmp);
148 result = rb_entry(node, struct annotated_data_type, node);
153 /* If not, add a new one */
154 result = zalloc(sizeof(*result));
155 if (result == NULL) {
160 result->self.type_name = type_name;
161 result->self.size = size;
162 INIT_LIST_HEAD(&result->self.children);
164 if (symbol_conf.annotate_data_member)
165 add_member_types(result, type_die);
167 rb_add(&result->node, &dso->data_types, data_type_less);
171 static bool find_cu_die(struct debuginfo *di, u64 pc, Dwarf_Die *cu_die)
173 Dwarf_Off off, next_off;
176 if (dwarf_addrdie(di->dbg, pc, cu_die) != NULL)
180 * There are some kernels don't have full aranges and contain only a few
181 * aranges entries. Fallback to iterate all CU entries in .debug_info
182 * in case it's missing.
185 while (dwarf_nextcu(di->dbg, off, &next_off, &header_size,
186 NULL, NULL, NULL) == 0) {
187 if (dwarf_offdie(di->dbg, off + header_size, cu_die) &&
188 dwarf_haspc(cu_die, pc))
196 /* The type info will be saved in @type_die */
197 static int check_variable(Dwarf_Die *var_die, Dwarf_Die *type_die, int offset,
202 /* Get the type of the variable */
203 if (die_get_real_type(var_die, type_die) == NULL) {
204 pr_debug("variable has no type\n");
205 ann_data_stat.no_typeinfo++;
210 * Usually it expects a pointer type for a memory access.
211 * Convert to a real type it points to. But global variables
212 * and local variables are accessed directly without a pointer.
215 if ((dwarf_tag(type_die) != DW_TAG_pointer_type &&
216 dwarf_tag(type_die) != DW_TAG_array_type) ||
217 die_get_real_type(type_die, type_die) == NULL) {
218 pr_debug("no pointer or no type\n");
219 ann_data_stat.no_typeinfo++;
224 /* Get the size of the actual type */
225 if (dwarf_aggregate_size(type_die, &size) < 0) {
226 pr_debug("type size is unknown\n");
227 ann_data_stat.invalid_size++;
231 /* Minimal sanity check */
232 if ((unsigned)offset >= size) {
233 pr_debug("offset: %d is bigger than size: %" PRIu64 "\n", offset, size);
234 ann_data_stat.bad_offset++;
241 /* The result will be saved in @type_die */
242 static int find_data_type_die(struct debuginfo *di, u64 pc, u64 addr,
243 const char *var_name, struct annotated_op_loc *loc,
246 Dwarf_Die cu_die, var_die;
247 Dwarf_Die *scopes = NULL;
252 bool is_fbreg = false;
255 /* Get a compile_unit for this address */
256 if (!find_cu_die(di, pc, &cu_die)) {
257 pr_debug("cannot find CU for address %" PRIx64 "\n", pc);
258 ann_data_stat.no_cuinfo++;
263 offset = loc->offset;
265 if (reg == DWARF_REG_PC) {
266 if (die_find_variable_by_addr(&cu_die, pc, addr, &var_die, &offset)) {
267 ret = check_variable(&var_die, type_die, offset,
268 /*is_pointer=*/false);
269 loc->offset = offset;
273 if (var_name && die_find_variable_at(&cu_die, var_name, pc,
275 ret = check_variable(&var_die, type_die, 0,
276 /*is_pointer=*/false);
277 /* loc->offset will be updated by the caller */
282 /* Get a list of nested scopes - i.e. (inlined) functions and blocks. */
283 nr_scopes = die_get_scopes(&cu_die, pc, &scopes);
285 if (reg != DWARF_REG_PC && dwarf_hasattr(&scopes[0], DW_AT_frame_base)) {
286 Dwarf_Attribute attr;
289 /* Check if the 'reg' is assigned as frame base register */
290 if (dwarf_attr(&scopes[0], DW_AT_frame_base, &attr) != NULL &&
291 dwarf_formblock(&attr, &block) == 0 && block.length == 1) {
292 switch (*block.data) {
293 case DW_OP_reg0 ... DW_OP_reg31:
294 fbreg = *block.data - DW_OP_reg0;
296 case DW_OP_call_frame_cfa:
297 if (die_get_cfa(di->dbg, pc, &fbreg,
308 is_fbreg = (reg == fbreg);
310 offset = loc->offset - fb_offset;
312 /* Search from the inner-most scope to the outer */
313 for (i = nr_scopes - 1; i >= 0; i--) {
314 if (reg == DWARF_REG_PC) {
315 if (!die_find_variable_by_addr(&scopes[i], pc, addr,
319 /* Look up variables/parameters in this scope */
320 if (!die_find_variable_by_reg(&scopes[i], pc, reg,
321 &offset, is_fbreg, &var_die))
325 /* Found a variable, see if it's correct */
326 ret = check_variable(&var_die, type_die, offset,
327 reg != DWARF_REG_PC && !is_fbreg);
328 loc->offset = offset;
332 if (loc->multi_regs && reg == loc->reg1 && loc->reg1 != loc->reg2) {
338 ann_data_stat.no_var++;
346 * find_data_type - Return a data type at the location
347 * @ms: map and symbol at the location
348 * @ip: instruction address of the memory access
349 * @loc: instruction operand location
350 * @addr: data address of the memory access
351 * @var_name: global variable name
353 * This functions searches the debug information of the binary to get the data
354 * type it accesses. The exact location is expressed by (@ip, reg, offset)
355 * for pointer variables or (@ip, @addr) for global variables. Note that global
356 * variables might update the @loc->offset after finding the start of the variable.
357 * If it cannot find a global variable by address, it tried to fine a declaration
358 * of the variable using @var_name. In that case, @loc->offset won't be updated.
360 * It return %NULL if not found.
362 struct annotated_data_type *find_data_type(struct map_symbol *ms, u64 ip,
363 struct annotated_op_loc *loc, u64 addr,
364 const char *var_name)
366 struct annotated_data_type *result = NULL;
367 struct dso *dso = map__dso(ms->map);
368 struct debuginfo *di;
372 di = debuginfo__new(dso->long_name);
374 pr_debug("cannot get the debug info\n");
379 * IP is a relative instruction address from the start of the map, as
380 * it can be randomized/relocated, it needs to translate to PC which is
381 * a file address for DWARF processing.
383 pc = map__rip_2objdump(ms->map, ip);
384 if (find_data_type_die(di, pc, addr, var_name, loc, &type_die) < 0)
387 result = dso__findnew_data_type(dso, &type_die);
390 debuginfo__delete(di);
394 static int alloc_data_type_histograms(struct annotated_data_type *adt, int nr_entries)
397 size_t sz = sizeof(struct type_hist);
399 sz += sizeof(struct type_hist_entry) * adt->self.size;
401 /* Allocate a table of pointers for each event */
402 adt->nr_histograms = nr_entries;
403 adt->histograms = calloc(nr_entries, sizeof(*adt->histograms));
404 if (adt->histograms == NULL)
408 * Each histogram is allocated for the whole size of the type.
409 * TODO: Probably we can move the histogram to members.
411 for (i = 0; i < nr_entries; i++) {
412 adt->histograms[i] = zalloc(sz);
413 if (adt->histograms[i] == NULL)
420 free(adt->histograms[i]);
421 free(adt->histograms);
425 static void delete_data_type_histograms(struct annotated_data_type *adt)
427 for (int i = 0; i < adt->nr_histograms; i++)
428 free(adt->histograms[i]);
429 free(adt->histograms);
432 void annotated_data_type__tree_delete(struct rb_root *root)
434 struct annotated_data_type *pos;
436 while (!RB_EMPTY_ROOT(root)) {
437 struct rb_node *node = rb_first(root);
439 rb_erase(node, root);
440 pos = rb_entry(node, struct annotated_data_type, node);
441 delete_members(&pos->self);
442 delete_data_type_histograms(pos);
443 free(pos->self.type_name);
449 * annotated_data_type__update_samples - Update histogram
450 * @adt: Data type to update
451 * @evsel: Event to update
452 * @offset: Offset in the type
453 * @nr_samples: Number of samples at this offset
454 * @period: Event count at this offset
456 * This function updates type histogram at @ofs for @evsel. Samples are
457 * aggregated before calling this function so it can be called with more
458 * than one samples at a certain offset.
460 int annotated_data_type__update_samples(struct annotated_data_type *adt,
461 struct evsel *evsel, int offset,
462 int nr_samples, u64 period)
469 if (adt->histograms == NULL) {
470 int nr = evsel->evlist->core.nr_entries;
472 if (alloc_data_type_histograms(adt, nr) < 0)
476 if (offset < 0 || offset >= adt->self.size)
479 h = adt->histograms[evsel->core.idx];
481 h->nr_samples += nr_samples;
482 h->addr[offset].nr_samples += nr_samples;
484 h->addr[offset].period += period;