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>
12 #include "annotate-data.h"
13 #include "debuginfo.h"
19 #include "map_symbol.h"
22 #include "symbol_conf.h"
25 * Compare type name and size to maintain them in a tree.
26 * I'm not sure if DWARF would have information of a single type in many
27 * different places (compilation units). If not, it could compare the
28 * offset of the type entry in the .debug_info section.
30 static int data_type_cmp(const void *_key, const struct rb_node *node)
32 const struct annotated_data_type *key = _key;
33 struct annotated_data_type *type;
35 type = rb_entry(node, struct annotated_data_type, node);
37 if (key->self.size != type->self.size)
38 return key->self.size - type->self.size;
39 return strcmp(key->self.type_name, type->self.type_name);
42 static bool data_type_less(struct rb_node *node_a, const struct rb_node *node_b)
44 struct annotated_data_type *a, *b;
46 a = rb_entry(node_a, struct annotated_data_type, node);
47 b = rb_entry(node_b, struct annotated_data_type, node);
49 if (a->self.size != b->self.size)
50 return a->self.size < b->self.size;
51 return strcmp(a->self.type_name, b->self.type_name) < 0;
54 /* Recursively add new members for struct/union */
55 static int __add_member_cb(Dwarf_Die *die, void *arg)
57 struct annotated_member *parent = arg;
58 struct annotated_member *member;
59 Dwarf_Die member_type, die_mem;
65 if (dwarf_tag(die) != DW_TAG_member)
66 return DIE_FIND_CB_SIBLING;
68 member = zalloc(sizeof(*member));
70 return DIE_FIND_CB_END;
73 die_get_typename(die, &sb);
75 die_get_real_type(die, &member_type);
76 if (dwarf_aggregate_size(&member_type, &size) < 0)
79 if (!dwarf_attr_integrate(die, DW_AT_data_member_location, &attr))
82 dwarf_formudata(&attr, &loc);
84 member->type_name = strbuf_detach(&sb, NULL);
85 /* member->var_name can be NULL */
86 if (dwarf_diename(die))
87 member->var_name = strdup(dwarf_diename(die));
89 member->offset = loc + parent->offset;
90 INIT_LIST_HEAD(&member->children);
91 list_add_tail(&member->node, &parent->children);
93 tag = dwarf_tag(&member_type);
95 case DW_TAG_structure_type:
96 case DW_TAG_union_type:
97 die_find_child(&member_type, __add_member_cb, member, &die_mem);
102 return DIE_FIND_CB_SIBLING;
105 static void add_member_types(struct annotated_data_type *parent, Dwarf_Die *type)
109 die_find_child(type, __add_member_cb, &parent->self, &die_mem);
112 static void delete_members(struct annotated_member *member)
114 struct annotated_member *child, *tmp;
116 list_for_each_entry_safe(child, tmp, &member->children, node) {
117 list_del(&child->node);
118 delete_members(child);
119 free(child->type_name);
120 free(child->var_name);
125 static struct annotated_data_type *dso__findnew_data_type(struct dso *dso,
128 struct annotated_data_type *result = NULL;
129 struct annotated_data_type key;
130 struct rb_node *node;
135 strbuf_init(&sb, 32);
136 if (die_get_typename_from_type(type_die, &sb) < 0)
137 strbuf_add(&sb, "(unknown type)", 14);
138 type_name = strbuf_detach(&sb, NULL);
139 dwarf_aggregate_size(type_die, &size);
141 /* Check existing nodes in dso->data_types tree */
142 key.self.type_name = type_name;
143 key.self.size = size;
144 node = rb_find(&key, &dso->data_types, data_type_cmp);
146 result = rb_entry(node, struct annotated_data_type, node);
151 /* If not, add a new one */
152 result = zalloc(sizeof(*result));
153 if (result == NULL) {
158 result->self.type_name = type_name;
159 result->self.size = size;
160 INIT_LIST_HEAD(&result->self.children);
162 if (symbol_conf.annotate_data_member)
163 add_member_types(result, type_die);
165 rb_add(&result->node, &dso->data_types, data_type_less);
169 static bool find_cu_die(struct debuginfo *di, u64 pc, Dwarf_Die *cu_die)
171 Dwarf_Off off, next_off;
174 if (dwarf_addrdie(di->dbg, pc, cu_die) != NULL)
178 * There are some kernels don't have full aranges and contain only a few
179 * aranges entries. Fallback to iterate all CU entries in .debug_info
180 * in case it's missing.
183 while (dwarf_nextcu(di->dbg, off, &next_off, &header_size,
184 NULL, NULL, NULL) == 0) {
185 if (dwarf_offdie(di->dbg, off + header_size, cu_die) &&
186 dwarf_haspc(cu_die, pc))
194 /* The type info will be saved in @type_die */
195 static int check_variable(Dwarf_Die *var_die, Dwarf_Die *type_die, int offset)
199 /* Get the type of the variable */
200 if (die_get_real_type(var_die, type_die) == NULL) {
201 pr_debug("variable has no type\n");
202 ann_data_stat.no_typeinfo++;
207 * It expects a pointer type for a memory access.
208 * Convert to a real type it points to.
210 if (dwarf_tag(type_die) != DW_TAG_pointer_type ||
211 die_get_real_type(type_die, type_die) == NULL) {
212 pr_debug("no pointer or no type\n");
213 ann_data_stat.no_typeinfo++;
217 /* Get the size of the actual type */
218 if (dwarf_aggregate_size(type_die, &size) < 0) {
219 pr_debug("type size is unknown\n");
220 ann_data_stat.invalid_size++;
224 /* Minimal sanity check */
225 if ((unsigned)offset >= size) {
226 pr_debug("offset: %d is bigger than size: %" PRIu64 "\n", offset, size);
227 ann_data_stat.bad_offset++;
234 /* The result will be saved in @type_die */
235 static int find_data_type_die(struct debuginfo *di, u64 pc,
236 int reg, int offset, Dwarf_Die *type_die)
238 Dwarf_Die cu_die, var_die;
239 Dwarf_Die *scopes = NULL;
243 /* Get a compile_unit for this address */
244 if (!find_cu_die(di, pc, &cu_die)) {
245 pr_debug("cannot find CU for address %" PRIx64 "\n", pc);
246 ann_data_stat.no_cuinfo++;
250 /* Get a list of nested scopes - i.e. (inlined) functions and blocks. */
251 nr_scopes = die_get_scopes(&cu_die, pc, &scopes);
253 /* Search from the inner-most scope to the outer */
254 for (i = nr_scopes - 1; i >= 0; i--) {
255 /* Look up variables/parameters in this scope */
256 if (!die_find_variable_by_reg(&scopes[i], pc, reg, &var_die))
259 /* Found a variable, see if it's correct */
260 ret = check_variable(&var_die, type_die, offset);
264 ann_data_stat.no_var++;
272 * find_data_type - Return a data type at the location
273 * @ms: map and symbol at the location
274 * @ip: instruction address of the memory access
275 * @reg: register that holds the base address
276 * @offset: offset from the base address
278 * This functions searches the debug information of the binary to get the data
279 * type it accesses. The exact location is expressed by (ip, reg, offset).
280 * It return %NULL if not found.
282 struct annotated_data_type *find_data_type(struct map_symbol *ms, u64 ip,
285 struct annotated_data_type *result = NULL;
286 struct dso *dso = map__dso(ms->map);
287 struct debuginfo *di;
291 di = debuginfo__new(dso->long_name);
293 pr_debug("cannot get the debug info\n");
298 * IP is a relative instruction address from the start of the map, as
299 * it can be randomized/relocated, it needs to translate to PC which is
300 * a file address for DWARF processing.
302 pc = map__rip_2objdump(ms->map, ip);
303 if (find_data_type_die(di, pc, reg, offset, &type_die) < 0)
306 result = dso__findnew_data_type(dso, &type_die);
309 debuginfo__delete(di);
313 static int alloc_data_type_histograms(struct annotated_data_type *adt, int nr_entries)
316 size_t sz = sizeof(struct type_hist);
318 sz += sizeof(struct type_hist_entry) * adt->self.size;
320 /* Allocate a table of pointers for each event */
321 adt->nr_histograms = nr_entries;
322 adt->histograms = calloc(nr_entries, sizeof(*adt->histograms));
323 if (adt->histograms == NULL)
327 * Each histogram is allocated for the whole size of the type.
328 * TODO: Probably we can move the histogram to members.
330 for (i = 0; i < nr_entries; i++) {
331 adt->histograms[i] = zalloc(sz);
332 if (adt->histograms[i] == NULL)
339 free(adt->histograms[i]);
340 free(adt->histograms);
344 static void delete_data_type_histograms(struct annotated_data_type *adt)
346 for (int i = 0; i < adt->nr_histograms; i++)
347 free(adt->histograms[i]);
348 free(adt->histograms);
351 void annotated_data_type__tree_delete(struct rb_root *root)
353 struct annotated_data_type *pos;
355 while (!RB_EMPTY_ROOT(root)) {
356 struct rb_node *node = rb_first(root);
358 rb_erase(node, root);
359 pos = rb_entry(node, struct annotated_data_type, node);
360 delete_members(&pos->self);
361 delete_data_type_histograms(pos);
362 free(pos->self.type_name);
368 * annotated_data_type__update_samples - Update histogram
369 * @adt: Data type to update
370 * @evsel: Event to update
371 * @offset: Offset in the type
372 * @nr_samples: Number of samples at this offset
373 * @period: Event count at this offset
375 * This function updates type histogram at @ofs for @evsel. Samples are
376 * aggregated before calling this function so it can be called with more
377 * than one samples at a certain offset.
379 int annotated_data_type__update_samples(struct annotated_data_type *adt,
380 struct evsel *evsel, int offset,
381 int nr_samples, u64 period)
388 if (adt->histograms == NULL) {
389 int nr = evsel->evlist->core.nr_entries;
391 if (alloc_data_type_histograms(adt, nr) < 0)
395 if (offset < 0 || offset >= adt->self.size)
398 h = adt->histograms[evsel->core.idx];
400 h->nr_samples += nr_samples;
401 h->addr[offset].nr_samples += nr_samples;
403 h->addr[offset].period += period;