blob: fe86612afadc2326fdacb619ee77ec8a4f75682d [file] [log] [blame]
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001#include <dirent.h>
2#include <errno.h>
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03003#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <sys/param.h>
9#include <fcntl.h>
10#include <unistd.h>
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -020011#include <inttypes.h>
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -030012#include "build-id.h"
Namhyung Kime334c722012-02-10 10:10:17 +090013#include "util.h"
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030014#include "debug.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030015#include "symbol.h"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030016#include "strlist.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030017
18#include <libelf.h>
19#include <gelf.h>
20#include <elf.h>
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -020021#include <limits.h>
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030022#include <sys/utsname.h>
Peter Zijlstra2cdbc462009-08-05 14:05:16 +020023
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -020024#ifndef KSYM_NAME_LEN
Ricardo Ribalda Delgadoc752d042011-10-20 09:43:26 +020025#define KSYM_NAME_LEN 256
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -020026#endif
27
Arnaldo Carvalho de Meloc12e15e2009-11-21 14:31:25 -020028#ifndef NT_GNU_BUILD_ID
29#define NT_GNU_BUILD_ID 3
30#endif
31
Jiri Olsa4dff6242012-07-22 14:14:39 +020032static void dso_cache__free(struct rb_root *root);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030033static bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
Dave Martin21916c32010-07-30 09:08:08 -030034static int elf_read_build_id(Elf *elf, void *bf, size_t size);
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -020035static void dsos__add(struct list_head *head, struct dso *dso);
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020036static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030037static int dso__load_kernel_sym(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -020038 symbol_filter_t filter);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030039static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080040 symbol_filter_t filter);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -020041static int vmlinux_path__nr_entries;
42static char **vmlinux_path;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030043
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -020044struct symbol_conf symbol_conf = {
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -020045 .exclude_other = true,
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020046 .use_modules = true,
47 .try_vmlinux_path = true,
Stephane Eranian3e6a2a72011-05-17 17:32:07 +020048 .annotate_src = true,
David Ahernec5761e2010-12-09 13:27:07 -070049 .symfs = "",
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020050};
51
Jiri Olsa44f24cb2012-07-22 14:14:32 +020052static enum dso_binary_type binary_type_symtab[] = {
53 DSO_BINARY_TYPE__KALLSYMS,
54 DSO_BINARY_TYPE__GUEST_KALLSYMS,
55 DSO_BINARY_TYPE__JAVA_JIT,
56 DSO_BINARY_TYPE__DEBUGLINK,
57 DSO_BINARY_TYPE__BUILD_ID_CACHE,
58 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
59 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
60 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
61 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
62 DSO_BINARY_TYPE__GUEST_KMODULE,
63 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
64 DSO_BINARY_TYPE__NOT_FOUND,
65};
66
Jiri Olsa028df762012-08-01 14:47:57 +020067#define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
Jiri Olsa44f24cb2012-07-22 14:14:32 +020068
Jiri Olsa949d1602012-07-22 14:14:33 +020069static enum dso_binary_type binary_type_data[] = {
70 DSO_BINARY_TYPE__BUILD_ID_CACHE,
71 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
72 DSO_BINARY_TYPE__NOT_FOUND,
73};
74
Jiri Olsa028df762012-08-01 14:47:57 +020075#define DSO_BINARY_TYPE__DATA_CNT ARRAY_SIZE(binary_type_data)
Jiri Olsa949d1602012-07-22 14:14:33 +020076
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030077int dso__name_len(const struct dso *dso)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030078{
David Miller1e2dd2f2012-03-25 16:28:22 -040079 if (!dso)
80 return strlen("[unknown]");
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030081 if (verbose)
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030082 return dso->long_name_len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030083
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030084 return dso->short_name_len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030085}
86
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030087bool dso__loaded(const struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020088{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030089 return dso->loaded & (1 << type);
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020090}
91
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030092bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020093{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030094 return dso->sorted_by_name & (1 << type);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020095}
96
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030097static void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020098{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030099 dso->sorted_by_name |= (1 << type);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200100}
101
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -0200102bool symbol_type__is_a(char symbol_type, enum map_type map_type)
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -0200103{
Anton Blanchard31877902011-08-24 16:40:16 +1000104 symbol_type = toupper(symbol_type);
105
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -0200106 switch (map_type) {
107 case MAP__FUNCTION:
108 return symbol_type == 'T' || symbol_type == 'W';
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200109 case MAP__VARIABLE:
Anton Blanchard31877902011-08-24 16:40:16 +1000110 return symbol_type == 'D';
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -0200111 default:
112 return false;
113 }
114}
115
Anton Blanchard694bf402011-08-24 16:40:17 +1000116static int prefix_underscores_count(const char *str)
117{
118 const char *tail = str;
119
120 while (*tail == '_')
121 tail++;
122
123 return tail - str;
124}
125
126#define SYMBOL_A 0
127#define SYMBOL_B 1
128
129static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
130{
131 s64 a;
132 s64 b;
133
134 /* Prefer a symbol with non zero length */
135 a = syma->end - syma->start;
136 b = symb->end - symb->start;
137 if ((b == 0) && (a > 0))
138 return SYMBOL_A;
139 else if ((a == 0) && (b > 0))
140 return SYMBOL_B;
141
142 /* Prefer a non weak symbol over a weak one */
143 a = syma->binding == STB_WEAK;
144 b = symb->binding == STB_WEAK;
145 if (b && !a)
146 return SYMBOL_A;
147 if (a && !b)
148 return SYMBOL_B;
149
150 /* Prefer a global symbol over a non global one */
151 a = syma->binding == STB_GLOBAL;
152 b = symb->binding == STB_GLOBAL;
153 if (a && !b)
154 return SYMBOL_A;
155 if (b && !a)
156 return SYMBOL_B;
157
158 /* Prefer a symbol with less underscores */
159 a = prefix_underscores_count(syma->name);
160 b = prefix_underscores_count(symb->name);
161 if (b > a)
162 return SYMBOL_A;
163 else if (a > b)
164 return SYMBOL_B;
165
166 /* If all else fails, choose the symbol with the longest name */
167 if (strlen(syma->name) >= strlen(symb->name))
168 return SYMBOL_A;
169 else
170 return SYMBOL_B;
171}
172
173static void symbols__fixup_duplicate(struct rb_root *symbols)
174{
175 struct rb_node *nd;
176 struct symbol *curr, *next;
177
178 nd = rb_first(symbols);
179
180 while (nd) {
181 curr = rb_entry(nd, struct symbol, rb_node);
182again:
183 nd = rb_next(&curr->rb_node);
184 next = rb_entry(nd, struct symbol, rb_node);
185
186 if (!nd)
187 break;
188
189 if (curr->start != next->start)
190 continue;
191
192 if (choose_best_symbol(curr, next) == SYMBOL_A) {
193 rb_erase(&next->rb_node, symbols);
194 goto again;
195 } else {
196 nd = rb_next(&curr->rb_node);
197 rb_erase(&curr->rb_node, symbols);
198 }
199 }
200}
201
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300202static void symbols__fixup_end(struct rb_root *symbols)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300203{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300204 struct rb_node *nd, *prevnd = rb_first(symbols);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300205 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300206
207 if (prevnd == NULL)
208 return;
209
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300210 curr = rb_entry(prevnd, struct symbol, rb_node);
211
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300212 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300213 prev = curr;
214 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300215
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200216 if (prev->end == prev->start && prev->end != curr->start)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300217 prev->end = curr->start - 1;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300218 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300219
220 /* Last entry */
221 if (curr->end == curr->start)
222 curr->end = roundup(curr->start, 4096);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300223}
224
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300225static void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300226{
227 struct map *prev, *curr;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300228 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300229
230 if (prevnd == NULL)
231 return;
232
233 curr = rb_entry(prevnd, struct map, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300234
235 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
236 prev = curr;
237 curr = rb_entry(nd, struct map, rb_node);
238 prev->end = curr->start - 1;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300239 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -0200240
241 /*
242 * We still haven't the actual symbols, so guess the
243 * last map final address.
244 */
Ian Munsie9d1faba2010-11-25 15:12:53 +1100245 curr->end = ~0ULL;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300246}
247
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300248static void map_groups__fixup_end(struct map_groups *mg)
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200249{
250 int i;
251 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300252 __map_groups__fixup_end(mg, i);
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200253}
254
Arnaldo Carvalho de Meloc408fed2010-08-05 12:59:47 -0300255static struct symbol *symbol__new(u64 start, u64 len, u8 binding,
256 const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300257{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300258 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300259 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
260 sizeof(*sym) + namelen));
261 if (sym == NULL)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200262 return NULL;
263
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200264 if (symbol_conf.priv_size)
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300265 sym = ((void *)sym) + symbol_conf.priv_size;
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200266
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300267 sym->start = start;
268 sym->end = len ? start + len - 1 : start;
269 sym->binding = binding;
270 sym->namelen = namelen - 1;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200271
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300272 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
273 __func__, name, start, sym->end);
274 memcpy(sym->name, name, namelen);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200275
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300276 return sym;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300277}
278
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300279void symbol__delete(struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300280{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300281 free(((void *)sym) - symbol_conf.priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300282}
283
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300284static size_t symbol__fprintf(struct symbol *sym, FILE *fp)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300285{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200286 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300287 sym->start, sym->end,
288 sym->binding == STB_GLOBAL ? 'g' :
289 sym->binding == STB_LOCAL ? 'l' : 'w',
290 sym->name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300291}
292
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900293size_t symbol__fprintf_symname_offs(const struct symbol *sym,
294 const struct addr_location *al, FILE *fp)
295{
296 unsigned long offset;
297 size_t length;
298
299 if (sym && sym->name) {
300 length = fprintf(fp, "%s", sym->name);
301 if (al) {
302 offset = al->addr - sym->start;
303 length += fprintf(fp, "+0x%lx", offset);
304 }
305 return length;
306 } else
307 return fprintf(fp, "[unknown]");
308}
309
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900310size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
311{
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900312 return symbol__fprintf_symname_offs(sym, NULL, fp);
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900313}
314
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300315void dso__set_long_name(struct dso *dso, char *name)
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200316{
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -0200317 if (name == NULL)
318 return;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300319 dso->long_name = name;
320 dso->long_name_len = strlen(name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200321}
322
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300323static void dso__set_short_name(struct dso *dso, const char *name)
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300324{
325 if (name == NULL)
326 return;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300327 dso->short_name = name;
328 dso->short_name_len = strlen(name);
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300329}
330
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300331static void dso__set_basename(struct dso *dso)
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200332{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300333 dso__set_short_name(dso, basename(dso->long_name));
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200334}
335
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200336struct dso *dso__new(const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300337{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300338 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300339
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300340 if (dso != NULL) {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200341 int i;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300342 strcpy(dso->name, name);
343 dso__set_long_name(dso, dso->name);
344 dso__set_short_name(dso, dso->name);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200345 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300346 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
Jiri Olsa4dff6242012-07-22 14:14:39 +0200347 dso->cache = RB_ROOT;
Jiri Olsa44f24cb2012-07-22 14:14:32 +0200348 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
Jiri Olsa949d1602012-07-22 14:14:33 +0200349 dso->data_type = DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300350 dso->loaded = 0;
351 dso->sorted_by_name = 0;
352 dso->has_build_id = 0;
353 dso->kernel = DSO_TYPE_USER;
Jiri Olsa8db48412012-05-30 14:23:42 +0200354 dso->needs_swap = DSO_SWAP__UNSET;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300355 INIT_LIST_HEAD(&dso->node);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300356 }
357
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300358 return dso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300359}
360
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300361static void symbols__delete(struct rb_root *symbols)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300362{
363 struct symbol *pos;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300364 struct rb_node *next = rb_first(symbols);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300365
366 while (next) {
367 pos = rb_entry(next, struct symbol, rb_node);
368 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300369 rb_erase(&pos->rb_node, symbols);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200370 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300371 }
372}
373
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300374void dso__delete(struct dso *dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300375{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200376 int i;
377 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300378 symbols__delete(&dso->symbols[i]);
379 if (dso->sname_alloc)
380 free((char *)dso->short_name);
381 if (dso->lname_alloc)
382 free(dso->long_name);
Jiri Olsa4dff6242012-07-22 14:14:39 +0200383 dso_cache__free(&dso->cache);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300384 free(dso);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300385}
386
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300387void dso__set_build_id(struct dso *dso, void *build_id)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200388{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300389 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
390 dso->has_build_id = 1;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200391}
392
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300393static void symbols__insert(struct rb_root *symbols, struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300394{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300395 struct rb_node **p = &symbols->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300396 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000397 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300398 struct symbol *s;
399
400 while (*p != NULL) {
401 parent = *p;
402 s = rb_entry(parent, struct symbol, rb_node);
403 if (ip < s->start)
404 p = &(*p)->rb_left;
405 else
406 p = &(*p)->rb_right;
407 }
408 rb_link_node(&sym->rb_node, parent, p);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300409 rb_insert_color(&sym->rb_node, symbols);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300410}
411
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300412static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300413{
414 struct rb_node *n;
415
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300416 if (symbols == NULL)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300417 return NULL;
418
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300419 n = symbols->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300420
421 while (n) {
422 struct symbol *s = rb_entry(n, struct symbol, rb_node);
423
424 if (ip < s->start)
425 n = n->rb_left;
426 else if (ip > s->end)
427 n = n->rb_right;
428 else
429 return s;
430 }
431
432 return NULL;
433}
434
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200435struct symbol_name_rb_node {
436 struct rb_node rb_node;
437 struct symbol sym;
438};
439
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300440static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200441{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300442 struct rb_node **p = &symbols->rb_node;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200443 struct rb_node *parent = NULL;
Rabin Vincent02a9d032010-11-23 22:08:18 +0530444 struct symbol_name_rb_node *symn, *s;
445
446 symn = container_of(sym, struct symbol_name_rb_node, sym);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200447
448 while (*p != NULL) {
449 parent = *p;
450 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
451 if (strcmp(sym->name, s->sym.name) < 0)
452 p = &(*p)->rb_left;
453 else
454 p = &(*p)->rb_right;
455 }
456 rb_link_node(&symn->rb_node, parent, p);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300457 rb_insert_color(&symn->rb_node, symbols);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200458}
459
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300460static void symbols__sort_by_name(struct rb_root *symbols,
461 struct rb_root *source)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200462{
463 struct rb_node *nd;
464
465 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
466 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300467 symbols__insert_by_name(symbols, pos);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200468 }
469}
470
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300471static struct symbol *symbols__find_by_name(struct rb_root *symbols,
472 const char *name)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200473{
474 struct rb_node *n;
475
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300476 if (symbols == NULL)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200477 return NULL;
478
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300479 n = symbols->rb_node;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200480
481 while (n) {
482 struct symbol_name_rb_node *s;
483 int cmp;
484
485 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
486 cmp = strcmp(name, s->sym.name);
487
488 if (cmp < 0)
489 n = n->rb_left;
490 else if (cmp > 0)
491 n = n->rb_right;
492 else
493 return &s->sym;
494 }
495
496 return NULL;
497}
498
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300499struct symbol *dso__find_symbol(struct dso *dso,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200500 enum map_type type, u64 addr)
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200501{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300502 return symbols__find(&dso->symbols[type], addr);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200503}
504
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300505struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200506 const char *name)
507{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300508 return symbols__find_by_name(&dso->symbol_names[type], name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200509}
510
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300511void dso__sort_by_name(struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200512{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300513 dso__set_sorted_by_name(dso, type);
514 return symbols__sort_by_name(&dso->symbol_names[type],
515 &dso->symbols[type]);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200516}
517
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300518int build_id__sprintf(const u8 *build_id, int len, char *bf)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200519{
520 char *bid = bf;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300521 const u8 *raw = build_id;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200522 int i;
523
524 for (i = 0; i < len; ++i) {
525 sprintf(bid, "%02x", *raw);
526 ++raw;
527 bid += 2;
528 }
529
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300530 return raw - build_id;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200531}
532
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300533size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300534{
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200535 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200536
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300537 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200538 return fprintf(fp, "%s", sbuild_id);
539}
540
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300541size_t dso__fprintf_symbols_by_name(struct dso *dso,
542 enum map_type type, FILE *fp)
Srikar Dronamraju90f18e62010-08-25 19:13:29 +0530543{
544 size_t ret = 0;
545 struct rb_node *nd;
546 struct symbol_name_rb_node *pos;
547
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300548 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
Srikar Dronamraju90f18e62010-08-25 19:13:29 +0530549 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
550 fprintf(fp, "%s\n", pos->sym.name);
551 }
552
553 return ret;
554}
555
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300556size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200557{
558 struct rb_node *nd;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300559 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200560
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300561 if (dso->short_name != dso->long_name)
562 ret += fprintf(fp, "%s, ", dso->long_name);
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -0300563 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300564 dso->loaded ? "" : "NOT ");
565 ret += dso__fprintf_buildid(dso, fp);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200566 ret += fprintf(fp, ")\n");
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300567 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200568 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
569 ret += symbol__fprintf(pos, fp);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300570 }
571
572 return ret;
573}
574
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200575int kallsyms__parse(const char *filename, void *arg,
576 int (*process_symbol)(void *arg, const char *name,
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200577 char type, u64 start, u64 end))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300578{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300579 char *line = NULL;
580 size_t n;
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200581 int err = -1;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200582 FILE *file = fopen(filename, "r");
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300583
584 if (file == NULL)
585 goto out_failure;
586
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200587 err = 0;
588
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300589 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000590 u64 start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300591 int line_len, len;
592 char symbol_type;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300593 char *symbol_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300594
595 line_len = getline(&line, &n, file);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800596 if (line_len < 0 || !line)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300597 break;
598
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300599 line[--line_len] = '\0'; /* \n */
600
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300601 len = hex2u64(line, &start);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300602
603 len++;
604 if (len + 2 >= line_len)
605 continue;
606
Anton Blanchard31877902011-08-24 16:40:16 +1000607 symbol_type = line[len];
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200608 len += 2;
609 symbol_name = line + len;
610 len = line_len - len;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300611
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200612 if (len >= KSYM_NAME_LEN) {
613 err = -1;
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200614 break;
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200615 }
616
Anton Blanchard3f5a4272011-08-24 16:40:15 +1000617 /*
618 * module symbols are not sorted so we add all
619 * symbols with zero length and rely on
620 * symbols__fixup_end() to fix it up.
621 */
622 err = process_symbol(arg, symbol_name,
623 symbol_type, start, start);
624 if (err)
625 break;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300626 }
627
628 free(line);
629 fclose(file);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200630 return err;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300631
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300632out_failure:
633 return -1;
634}
635
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200636struct process_kallsyms_args {
637 struct map *map;
638 struct dso *dso;
639};
640
Arnaldo Carvalho de Meloc408fed2010-08-05 12:59:47 -0300641static u8 kallsyms2elf_type(char type)
642{
643 if (type == 'W')
644 return STB_WEAK;
645
646 return isupper(type) ? STB_GLOBAL : STB_LOCAL;
647}
648
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200649static int map__process_kallsym_symbol(void *arg, const char *name,
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200650 char type, u64 start, u64 end)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200651{
652 struct symbol *sym;
653 struct process_kallsyms_args *a = arg;
654 struct rb_root *root = &a->dso->symbols[a->map->type];
655
656 if (!symbol_type__is_a(type, a->map->type))
657 return 0;
658
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200659 sym = symbol__new(start, end - start + 1,
660 kallsyms2elf_type(type), name);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200661 if (sym == NULL)
662 return -ENOMEM;
663 /*
664 * We will pass the symbols to the filter later, in
665 * map__split_kallsyms, when we have split the maps per module
666 */
667 symbols__insert(root, sym);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800668
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200669 return 0;
670}
671
672/*
673 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
674 * so that we can in the next step set the symbol ->end address and then
675 * call kernel_maps__split_kallsyms.
676 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300677static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200678 struct map *map)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200679{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300680 struct process_kallsyms_args args = { .map = map, .dso = dso, };
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200681 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200682}
683
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300684/*
685 * Split the symbols into maps, making sure there are no overlaps, i.e. the
686 * kernel range is broken in several maps, named [kernel].N, as we don't have
687 * the original ELF section names vmlinux have.
688 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300689static int dso__split_kallsyms(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200690 symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300691{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200692 struct map_groups *kmaps = map__kmap(map)->kmaps;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300693 struct machine *machine = kmaps->machine;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200694 struct map *curr_map = map;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300695 struct symbol *pos;
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200696 int count = 0, moved = 0;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300697 struct rb_root *root = &dso->symbols[map->type];
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200698 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300699 int kernel_range = 0;
700
701 while (next) {
702 char *module;
703
704 pos = rb_entry(next, struct symbol, rb_node);
705 next = rb_next(&pos->rb_node);
706
707 module = strchr(pos->name, '\t');
708 if (module) {
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200709 if (!symbol_conf.use_modules)
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200710 goto discard_symbol;
711
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300712 *module++ = '\0';
713
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200714 if (strcmp(curr_map->dso->short_name, module)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800715 if (curr_map != map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300716 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300717 machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800718 /*
719 * We assume all symbols of a module are
720 * continuous in * kallsyms, so curr_map
721 * points to a module and all its
722 * symbols are in its kmap. Mark it as
723 * loaded.
724 */
725 dso__set_loaded(curr_map->dso,
726 curr_map->type);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300727 }
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200728
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800729 curr_map = map_groups__find_by_name(kmaps,
730 map->type, module);
731 if (curr_map == NULL) {
Arnaldo Carvalho de Melo2f519032010-05-17 17:57:59 -0300732 pr_debug("%s/proc/{kallsyms,modules} "
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800733 "inconsistency while looking "
734 "for \"%s\" module!\n",
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300735 machine->root_dir, module);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800736 curr_map = map;
737 goto discard_symbol;
738 }
739
740 if (curr_map->dso->loaded &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300741 !machine__is_default_guest(machine))
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200742 goto discard_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300743 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300744 /*
745 * So that we look just like we get from .ko files,
746 * i.e. not prelinked, relative to map->start.
747 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200748 pos->start = curr_map->map_ip(curr_map, pos->start);
749 pos->end = curr_map->map_ip(curr_map, pos->end);
750 } else if (curr_map != map) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300751 char dso_name[PATH_MAX];
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300752 struct dso *ndso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300753
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200754 if (count == 0) {
755 curr_map = map;
756 goto filter_symbol;
757 }
758
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300759 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800760 snprintf(dso_name, sizeof(dso_name),
761 "[guest.kernel].%d",
762 kernel_range++);
763 else
764 snprintf(dso_name, sizeof(dso_name),
765 "[kernel].%d",
766 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300767
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300768 ndso = dso__new(dso_name);
769 if (ndso == NULL)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300770 return -1;
771
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300772 ndso->kernel = dso->kernel;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800773
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300774 curr_map = map__new2(pos->start, ndso, map->type);
Zhang, Yanmin37fe5fc2010-02-25 11:00:51 +0800775 if (curr_map == NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300776 dso__delete(ndso);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300777 return -1;
778 }
779
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200780 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200781 map_groups__insert(kmaps, curr_map);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300782 ++kernel_range;
783 }
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200784filter_symbol:
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200785 if (filter && filter(curr_map, pos)) {
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200786discard_symbol: rb_erase(&pos->rb_node, root);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200787 symbol__delete(pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300788 } else {
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200789 if (curr_map != map) {
790 rb_erase(&pos->rb_node, root);
791 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200792 ++moved;
793 } else
794 ++count;
Mike Galbraith9974f492009-07-02 08:05:58 +0200795 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300796 }
797
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800798 if (curr_map != map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300799 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300800 machine__is_default_guest(kmaps->machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800801 dso__set_loaded(curr_map->dso, curr_map->type);
802 }
803
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200804 return count + moved;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300805}
806
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300807static bool symbol__restricted_filename(const char *filename,
808 const char *restricted_filename)
809{
810 bool restricted = false;
811
812 if (symbol_conf.kptr_restrict) {
813 char *r = realpath(filename, NULL);
814
815 if (r != NULL) {
816 restricted = strcmp(r, restricted_filename) == 0;
817 free(r);
818 return restricted;
819 }
820 }
821
822 return restricted;
823}
824
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300825int dso__load_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200826 struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300827{
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300828 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
829 return -1;
830
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300831 if (dso__load_all_kallsyms(dso, filename, map) < 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300832 return -1;
833
Anton Blanchard694bf402011-08-24 16:40:17 +1000834 symbols__fixup_duplicate(&dso->symbols[map->type]);
Anton Blanchard3f5a4272011-08-24 16:40:15 +1000835 symbols__fixup_end(&dso->symbols[map->type]);
836
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300837 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Jiri Olsa44f24cb2012-07-22 14:14:32 +0200838 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800839 else
Jiri Olsa44f24cb2012-07-22 14:14:32 +0200840 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300841
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300842 return dso__split_kallsyms(dso, map, filter);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300843}
844
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300845static int dso__load_perf_map(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200846 symbol_filter_t filter)
Pekka Enberg80d496b2009-06-08 21:12:48 +0300847{
848 char *line = NULL;
849 size_t n;
850 FILE *file;
851 int nr_syms = 0;
852
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300853 file = fopen(dso->long_name, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +0300854 if (file == NULL)
855 goto out_failure;
856
857 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000858 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300859 struct symbol *sym;
860 int line_len, len;
861
862 line_len = getline(&line, &n, file);
863 if (line_len < 0)
864 break;
865
866 if (!line)
867 goto out_failure;
868
869 line[--line_len] = '\0'; /* \n */
870
871 len = hex2u64(line, &start);
872
873 len++;
874 if (len + 2 >= line_len)
875 continue;
876
877 len += hex2u64(line + len, &size);
878
879 len++;
880 if (len + 2 >= line_len)
881 continue;
882
Arnaldo Carvalho de Meloc408fed2010-08-05 12:59:47 -0300883 sym = symbol__new(start, size, STB_GLOBAL, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300884
885 if (sym == NULL)
886 goto out_delete_line;
887
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300888 if (filter && filter(map, sym))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200889 symbol__delete(sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300890 else {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300891 symbols__insert(&dso->symbols[map->type], sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300892 nr_syms++;
893 }
894 }
895
896 free(line);
897 fclose(file);
898
899 return nr_syms;
900
901out_delete_line:
902 free(line);
903out_failure:
904 return -1;
905}
906
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300907/**
908 * elf_symtab__for_each_symbol - iterate thru all the symbols
909 *
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300910 * @syms: struct elf_symtab instance to iterate
Ingo Molnar83a09442009-08-15 12:26:57 +0200911 * @idx: uint32_t idx
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300912 * @sym: GElf_Sym iterator
913 */
Ingo Molnar83a09442009-08-15 12:26:57 +0200914#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
915 for (idx = 0, gelf_getsym(syms, idx, &sym);\
916 idx < nr_syms; \
917 idx++, gelf_getsym(syms, idx, &sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300918
919static inline uint8_t elf_sym__type(const GElf_Sym *sym)
920{
921 return GELF_ST_TYPE(sym->st_info);
922}
923
924static inline int elf_sym__is_function(const GElf_Sym *sym)
925{
926 return elf_sym__type(sym) == STT_FUNC &&
927 sym->st_name != 0 &&
Arnaldo Carvalho de Melo81833132009-10-05 23:35:03 -0300928 sym->st_shndx != SHN_UNDEF;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300929}
930
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200931static inline bool elf_sym__is_object(const GElf_Sym *sym)
932{
933 return elf_sym__type(sym) == STT_OBJECT &&
934 sym->st_name != 0 &&
935 sym->st_shndx != SHN_UNDEF;
936}
937
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200938static inline int elf_sym__is_label(const GElf_Sym *sym)
939{
940 return elf_sym__type(sym) == STT_NOTYPE &&
941 sym->st_name != 0 &&
942 sym->st_shndx != SHN_UNDEF &&
943 sym->st_shndx != SHN_ABS;
944}
945
946static inline const char *elf_sec__name(const GElf_Shdr *shdr,
947 const Elf_Data *secstrs)
948{
949 return secstrs->d_buf + shdr->sh_name;
950}
951
952static inline int elf_sec__is_text(const GElf_Shdr *shdr,
953 const Elf_Data *secstrs)
954{
955 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
956}
957
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200958static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
959 const Elf_Data *secstrs)
960{
961 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
962}
963
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300964static inline const char *elf_sym__name(const GElf_Sym *sym,
965 const Elf_Data *symstrs)
966{
967 return symstrs->d_buf + sym->st_name;
968}
969
970static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
971 GElf_Shdr *shp, const char *name,
Ingo Molnar83a09442009-08-15 12:26:57 +0200972 size_t *idx)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300973{
974 Elf_Scn *sec = NULL;
975 size_t cnt = 1;
976
977 while ((sec = elf_nextscn(elf, sec)) != NULL) {
978 char *str;
979
980 gelf_getshdr(sec, shp);
981 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
982 if (!strcmp(name, str)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200983 if (idx)
984 *idx = cnt;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300985 break;
986 }
987 ++cnt;
988 }
989
990 return sec;
991}
992
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300993#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
994 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
995 idx < nr_entries; \
996 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
997
998#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
999 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
1000 idx < nr_entries; \
1001 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
1002
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001003/*
1004 * We need to check if we have a .dynsym, so that we can handle the
1005 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
1006 * .dynsym or .symtab).
1007 * And always look at the original dso, not at debuginfo packages, that
1008 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
1009 */
Jiri Olsa33ff5812012-04-18 15:46:58 +02001010static int
1011dso__synthesize_plt_symbols(struct dso *dso, char *name, struct map *map,
1012 symbol_filter_t filter)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001013{
1014 uint32_t nr_rel_entries, idx;
1015 GElf_Sym sym;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001016 u64 plt_offset;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001017 GElf_Shdr shdr_plt;
1018 struct symbol *f;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001019 GElf_Shdr shdr_rel_plt, shdr_dynsym;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001020 Elf_Data *reldata, *syms, *symstrs;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001021 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
1022 size_t dynsym_idx;
1023 GElf_Ehdr ehdr;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001024 char sympltname[1024];
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001025 Elf *elf;
1026 int nr = 0, symidx, fd, err = 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001027
David Ahernec5761e2010-12-09 13:27:07 -07001028 fd = open(name, O_RDONLY);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001029 if (fd < 0)
1030 goto out;
1031
Marti Raudsepp84087122009-10-24 19:10:36 +03001032 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001033 if (elf == NULL)
1034 goto out_close;
1035
1036 if (gelf_getehdr(elf, &ehdr) == NULL)
1037 goto out_elf_end;
1038
1039 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
1040 ".dynsym", &dynsym_idx);
1041 if (scn_dynsym == NULL)
1042 goto out_elf_end;
1043
1044 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001045 ".rela.plt", NULL);
1046 if (scn_plt_rel == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001047 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001048 ".rel.plt", NULL);
1049 if (scn_plt_rel == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001050 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001051 }
1052
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001053 err = -1;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001054
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001055 if (shdr_rel_plt.sh_link != dynsym_idx)
1056 goto out_elf_end;
1057
1058 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
1059 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001060
1061 /*
Ingo Molnar83a09442009-08-15 12:26:57 +02001062 * Fetch the relocation section to find the idxes to the GOT
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001063 * and the symbols in the .dynsym they refer to.
1064 */
1065 reldata = elf_getdata(scn_plt_rel, NULL);
1066 if (reldata == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001067 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001068
1069 syms = elf_getdata(scn_dynsym, NULL);
1070 if (syms == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001071 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001072
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001073 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001074 if (scn_symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001075 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001076
1077 symstrs = elf_getdata(scn_symstrs, NULL);
1078 if (symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001079 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001080
1081 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
1082 plt_offset = shdr_plt.sh_offset;
1083
1084 if (shdr_rel_plt.sh_type == SHT_RELA) {
1085 GElf_Rela pos_mem, *pos;
1086
1087 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
1088 nr_rel_entries) {
1089 symidx = GELF_R_SYM(pos->r_info);
1090 plt_offset += shdr_plt.sh_entsize;
1091 gelf_getsym(syms, symidx, &sym);
1092 snprintf(sympltname, sizeof(sympltname),
1093 "%s@plt", elf_sym__name(&sym, symstrs));
1094
1095 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Meloc408fed2010-08-05 12:59:47 -03001096 STB_GLOBAL, sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001097 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001098 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001099
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -02001100 if (filter && filter(map, f))
1101 symbol__delete(f);
1102 else {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001103 symbols__insert(&dso->symbols[map->type], f);
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -02001104 ++nr;
1105 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001106 }
1107 } else if (shdr_rel_plt.sh_type == SHT_REL) {
1108 GElf_Rel pos_mem, *pos;
1109 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
1110 nr_rel_entries) {
1111 symidx = GELF_R_SYM(pos->r_info);
1112 plt_offset += shdr_plt.sh_entsize;
1113 gelf_getsym(syms, symidx, &sym);
1114 snprintf(sympltname, sizeof(sympltname),
1115 "%s@plt", elf_sym__name(&sym, symstrs));
1116
1117 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Meloc408fed2010-08-05 12:59:47 -03001118 STB_GLOBAL, sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001119 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001120 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001121
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -02001122 if (filter && filter(map, f))
1123 symbol__delete(f);
1124 else {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001125 symbols__insert(&dso->symbols[map->type], f);
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -02001126 ++nr;
1127 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001128 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001129 }
1130
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001131 err = 0;
1132out_elf_end:
1133 elf_end(elf);
1134out_close:
1135 close(fd);
1136
1137 if (err == 0)
1138 return nr;
1139out:
Arnaldo Carvalho de Melofe2197b2010-03-11 20:12:40 -03001140 pr_debug("%s: problems reading %s PLT info.\n",
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001141 __func__, dso->long_name);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001142 return 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001143}
1144
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001145static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -02001146{
1147 switch (type) {
1148 case MAP__FUNCTION:
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001149 return elf_sym__is_function(sym);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001150 case MAP__VARIABLE:
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001151 return elf_sym__is_object(sym);
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -02001152 default:
1153 return false;
1154 }
1155}
1156
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001157static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
1158 enum map_type type)
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -02001159{
1160 switch (type) {
1161 case MAP__FUNCTION:
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001162 return elf_sec__is_text(shdr, secstrs);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001163 case MAP__VARIABLE:
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001164 return elf_sec__is_data(shdr, secstrs);
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -02001165 default:
1166 return false;
1167 }
1168}
1169
Eric B Munson70c38562010-06-14 14:56:33 +01001170static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
1171{
1172 Elf_Scn *sec = NULL;
1173 GElf_Shdr shdr;
1174 size_t cnt = 1;
1175
1176 while ((sec = elf_nextscn(elf, sec)) != NULL) {
1177 gelf_getshdr(sec, &shdr);
1178
1179 if ((addr >= shdr.sh_addr) &&
1180 (addr < (shdr.sh_addr + shdr.sh_size)))
1181 return cnt;
1182
1183 ++cnt;
1184 }
1185
1186 return -1;
1187}
1188
Jiri Olsa8db48412012-05-30 14:23:42 +02001189static int dso__swap_init(struct dso *dso, unsigned char eidata)
1190{
1191 static unsigned int const endian = 1;
1192
1193 dso->needs_swap = DSO_SWAP__NO;
1194
1195 switch (eidata) {
1196 case ELFDATA2LSB:
1197 /* We are big endian, DSO is little endian. */
1198 if (*(unsigned char const *)&endian != 1)
1199 dso->needs_swap = DSO_SWAP__YES;
1200 break;
1201
1202 case ELFDATA2MSB:
1203 /* We are little endian, DSO is big endian. */
1204 if (*(unsigned char const *)&endian != 0)
1205 dso->needs_swap = DSO_SWAP__YES;
1206 break;
1207
1208 default:
1209 pr_err("unrecognized DSO data encoding %d\n", eidata);
1210 return -EINVAL;
1211 }
1212
1213 return 0;
1214}
1215
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001216static int dso__load_sym(struct dso *dso, struct map *map, const char *name,
Dave Martin6da80ce2010-07-30 09:50:09 -03001217 int fd, symbol_filter_t filter, int kmodule,
1218 int want_symtab)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001219{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001220 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001221 struct map *curr_map = map;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001222 struct dso *curr_dso = dso;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001223 Elf_Data *symstrs, *secstrs;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001224 uint32_t nr_syms;
1225 int err = -1;
Ingo Molnar83a09442009-08-15 12:26:57 +02001226 uint32_t idx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001227 GElf_Ehdr ehdr;
Eric B Munson70c38562010-06-14 14:56:33 +01001228 GElf_Shdr shdr, opdshdr;
1229 Elf_Data *syms, *opddata = NULL;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001230 GElf_Sym sym;
Eric B Munson70c38562010-06-14 14:56:33 +01001231 Elf_Scn *sec, *sec_strndx, *opdsec;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001232 Elf *elf;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001233 int nr = 0;
Eric B Munson70c38562010-06-14 14:56:33 +01001234 size_t opdidx = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001235
Marti Raudsepp84087122009-10-24 19:10:36 +03001236 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001237 if (elf == NULL) {
Dave Martin8b1389e2010-07-30 09:36:08 -03001238 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001239 goto out_close;
1240 }
1241
1242 if (gelf_getehdr(elf, &ehdr) == NULL) {
Dave Martin8b1389e2010-07-30 09:36:08 -03001243 pr_debug("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001244 goto out_elf_end;
1245 }
1246
Jiri Olsa8db48412012-05-30 14:23:42 +02001247 if (dso__swap_init(dso, ehdr.e_ident[EI_DATA]))
1248 goto out_elf_end;
1249
Dave Martin6da80ce2010-07-30 09:50:09 -03001250 /* Always reject images with a mismatched build-id: */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001251 if (dso->has_build_id) {
Dave Martin21916c32010-07-30 09:08:08 -03001252 u8 build_id[BUILD_ID_SIZE];
1253
Stephane Eranianbe96ea82010-10-22 17:25:01 +02001254 if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
Dave Martin21916c32010-07-30 09:08:08 -03001255 goto out_elf_end;
1256
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001257 if (!dso__build_id_equal(dso, build_id))
Dave Martin21916c32010-07-30 09:08:08 -03001258 goto out_elf_end;
1259 }
1260
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001261 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
1262 if (sec == NULL) {
Dave Martin6da80ce2010-07-30 09:50:09 -03001263 if (want_symtab)
1264 goto out_elf_end;
1265
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001266 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
1267 if (sec == NULL)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001268 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001269 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001270
Eric B Munson70c38562010-06-14 14:56:33 +01001271 opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
Anton Blanchardadb09182011-08-24 16:40:14 +10001272 if (opdshdr.sh_type != SHT_PROGBITS)
1273 opdsec = NULL;
Eric B Munson70c38562010-06-14 14:56:33 +01001274 if (opdsec)
1275 opddata = elf_rawdata(opdsec, NULL);
1276
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001277 syms = elf_getdata(sec, NULL);
1278 if (syms == NULL)
1279 goto out_elf_end;
1280
1281 sec = elf_getscn(elf, shdr.sh_link);
1282 if (sec == NULL)
1283 goto out_elf_end;
1284
1285 symstrs = elf_getdata(sec, NULL);
1286 if (symstrs == NULL)
1287 goto out_elf_end;
1288
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001289 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
1290 if (sec_strndx == NULL)
1291 goto out_elf_end;
1292
1293 secstrs = elf_getdata(sec_strndx, NULL);
Stoyan Gaydarov9b30a262009-07-30 05:25:29 -05001294 if (secstrs == NULL)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001295 goto out_elf_end;
1296
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001297 nr_syms = shdr.sh_size / shdr.sh_entsize;
1298
Arjan van de Vene9fbc9d2009-06-06 21:22:33 +02001299 memset(&sym, 0, sizeof(sym));
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001300 if (dso->kernel == DSO_TYPE_USER) {
1301 dso->adjust_symbols = (ehdr.e_type == ET_EXEC ||
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -03001302 elf_section_by_name(elf, &ehdr, &shdr,
1303 ".gnu.prelink_undo",
1304 NULL) != NULL);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001305 } else {
1306 dso->adjust_symbols = 0;
1307 }
Ingo Molnar83a09442009-08-15 12:26:57 +02001308 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001309 struct symbol *f;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001310 const char *elf_name = elf_sym__name(&sym, symstrs);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001311 char *demangled = NULL;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001312 int is_label = elf_sym__is_label(&sym);
1313 const char *section_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001314
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001315 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1316 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
1317 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001318
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -02001319 if (!is_label && !elf_sym__is_a(&sym, map->type))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001320 continue;
1321
Dave Martin696b97a2010-08-09 12:21:18 +01001322 /* Reject ARM ELF "mapping symbols": these aren't unique and
1323 * don't identify functions, so will confuse the profile
1324 * output: */
1325 if (ehdr.e_machine == EM_ARM) {
1326 if (!strcmp(elf_name, "$a") ||
1327 !strcmp(elf_name, "$d") ||
1328 !strcmp(elf_name, "$t"))
1329 continue;
1330 }
1331
Eric B Munson70c38562010-06-14 14:56:33 +01001332 if (opdsec && sym.st_shndx == opdidx) {
1333 u32 offset = sym.st_value - opdshdr.sh_addr;
1334 u64 *opd = opddata->d_buf + offset;
Jiri Olsa8db48412012-05-30 14:23:42 +02001335 sym.st_value = DSO__SWAP(dso, u64, *opd);
Eric B Munson70c38562010-06-14 14:56:33 +01001336 sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
1337 }
1338
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001339 sec = elf_getscn(elf, sym.st_shndx);
1340 if (!sec)
1341 goto out_elf_end;
1342
1343 gelf_getshdr(sec, &shdr);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001344
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -02001345 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001346 continue;
1347
1348 section_name = elf_sec__name(&shdr, secstrs);
Ingo Molnar0b73da32009-06-06 15:48:52 +02001349
Dr. David Alan Gilbertb2f8fb22011-01-21 16:40:19 +00001350 /* On ARM, symbols for thumb functions have 1 added to
1351 * the symbol address as a flag - remove it */
1352 if ((ehdr.e_machine == EM_ARM) &&
1353 (map->type == MAP__FUNCTION) &&
1354 (sym.st_value & 1))
1355 --sym.st_value;
1356
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001357 if (dso->kernel != DSO_TYPE_USER || kmodule) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001358 char dso_name[PATH_MAX];
1359
1360 if (strcmp(section_name,
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -03001361 (curr_dso->short_name +
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001362 dso->short_name_len)) == 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001363 goto new_symbol;
1364
1365 if (strcmp(section_name, ".text") == 0) {
1366 curr_map = map;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001367 curr_dso = dso;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001368 goto new_symbol;
1369 }
1370
1371 snprintf(dso_name, sizeof(dso_name),
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001372 "%s%s", dso->short_name, section_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001373
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001374 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001375 if (curr_map == NULL) {
1376 u64 start = sym.st_value;
1377
1378 if (kmodule)
1379 start += map->start + shdr.sh_offset;
1380
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001381 curr_dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001382 if (curr_dso == NULL)
1383 goto out_elf_end;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001384 curr_dso->kernel = dso->kernel;
1385 curr_dso->long_name = dso->long_name;
1386 curr_dso->long_name_len = dso->long_name_len;
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001387 curr_map = map__new2(start, curr_dso,
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001388 map->type);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001389 if (curr_map == NULL) {
1390 dso__delete(curr_dso);
1391 goto out_elf_end;
1392 }
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -02001393 curr_map->map_ip = identity__map_ip;
1394 curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001395 curr_dso->symtab_type = dso->symtab_type;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001396 map_groups__insert(kmap->kmaps, curr_map);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001397 dsos__add(&dso->node, curr_dso);
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001398 dso__set_loaded(curr_dso, map->type);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001399 } else
1400 curr_dso = curr_map->dso;
1401
1402 goto new_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001403 }
1404
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001405 if (curr_dso->adjust_symbols) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001406 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1407 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -02001408 (u64)sym.st_value, (u64)shdr.sh_addr,
1409 (u64)shdr.sh_offset);
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001410 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001411 }
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001412 /*
1413 * We need to figure out if the object was created from C++ sources
1414 * DWARF DW_compile_unit has this, but we don't always have access
1415 * to it...
1416 */
Ingo Molnar83a09442009-08-15 12:26:57 +02001417 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001418 if (demangled != NULL)
Ingo Molnar83a09442009-08-15 12:26:57 +02001419 elf_name = demangled;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001420new_symbol:
Arnaldo Carvalho de Meloc408fed2010-08-05 12:59:47 -03001421 f = symbol__new(sym.st_value, sym.st_size,
1422 GELF_ST_BIND(sym.st_info), elf_name);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001423 free(demangled);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001424 if (!f)
1425 goto out_elf_end;
1426
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001427 if (filter && filter(curr_map, f))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001428 symbol__delete(f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -03001429 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001430 symbols__insert(&curr_dso->symbols[curr_map->type], f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -03001431 nr++;
1432 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001433 }
1434
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001435 /*
1436 * For misannotated, zeroed, ASM function sizes.
1437 */
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001438 if (nr > 0) {
Anton Blanchard694bf402011-08-24 16:40:17 +10001439 symbols__fixup_duplicate(&dso->symbols[map->type]);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001440 symbols__fixup_end(&dso->symbols[map->type]);
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001441 if (kmap) {
1442 /*
1443 * We need to fixup this here too because we create new
1444 * maps here, for things like vsyscall sections.
1445 */
1446 __map_groups__fixup_end(kmap->kmaps, map->type);
1447 }
1448 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001449 err = nr;
1450out_elf_end:
1451 elf_end(elf);
1452out_close:
1453 return err;
1454}
1455
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001456static bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -02001457{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001458 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -02001459}
1460
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001461bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001462{
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001463 bool have_build_id = false;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001464 struct dso *pos;
1465
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001466 list_for_each_entry(pos, head, node) {
1467 if (with_hits && !pos->hit)
1468 continue;
Arnaldo Carvalho de Melof6e14672010-05-19 11:13:02 -03001469 if (pos->has_build_id) {
1470 have_build_id = true;
1471 continue;
1472 }
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001473 if (filename__read_build_id(pos->long_name, pos->build_id,
1474 sizeof(pos->build_id)) > 0) {
1475 have_build_id = true;
1476 pos->has_build_id = true;
1477 }
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001478 }
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001479
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001480 return have_build_id;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001481}
1482
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001483/*
1484 * Align offset to 4 bytes as needed for note name and descriptor data.
1485 */
1486#define NOTE_ALIGN(n) (((n) + 3) & -4U)
1487
Dave Martin21916c32010-07-30 09:08:08 -03001488static int elf_read_build_id(Elf *elf, void *bf, size_t size)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001489{
Dave Martin21916c32010-07-30 09:08:08 -03001490 int err = -1;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001491 GElf_Ehdr ehdr;
1492 GElf_Shdr shdr;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001493 Elf_Data *data;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001494 Elf_Scn *sec;
Pekka Enberge57cfcd2009-11-22 12:29:44 +02001495 Elf_Kind ek;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001496 void *ptr;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001497
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001498 if (size < BUILD_ID_SIZE)
1499 goto out;
1500
Pekka Enberge57cfcd2009-11-22 12:29:44 +02001501 ek = elf_kind(elf);
1502 if (ek != ELF_K_ELF)
Dave Martin21916c32010-07-30 09:08:08 -03001503 goto out;
Pekka Enberge57cfcd2009-11-22 12:29:44 +02001504
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001505 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001506 pr_err("%s: cannot get elf header.\n", __func__);
Dave Martin21916c32010-07-30 09:08:08 -03001507 goto out;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001508 }
1509
Jiri Olsa1388d715d2012-06-19 17:48:05 +02001510 /*
1511 * Check following sections for notes:
1512 * '.note.gnu.build-id'
1513 * '.notes'
1514 * '.note' (VDSO specific)
1515 */
1516 do {
1517 sec = elf_section_by_name(elf, &ehdr, &shdr,
1518 ".note.gnu.build-id", NULL);
1519 if (sec)
1520 break;
1521
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001522 sec = elf_section_by_name(elf, &ehdr, &shdr,
1523 ".notes", NULL);
Jiri Olsa1388d715d2012-06-19 17:48:05 +02001524 if (sec)
1525 break;
1526
1527 sec = elf_section_by_name(elf, &ehdr, &shdr,
1528 ".note", NULL);
1529 if (sec)
1530 break;
1531
1532 return err;
1533
1534 } while (0);
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001535
1536 data = elf_getdata(sec, NULL);
1537 if (data == NULL)
Dave Martin21916c32010-07-30 09:08:08 -03001538 goto out;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001539
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001540 ptr = data->d_buf;
1541 while (ptr < (data->d_buf + data->d_size)) {
1542 GElf_Nhdr *nhdr = ptr;
Stephane Eranianbe96ea82010-10-22 17:25:01 +02001543 size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
1544 descsz = NOTE_ALIGN(nhdr->n_descsz);
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001545 const char *name;
1546
1547 ptr += sizeof(*nhdr);
1548 name = ptr;
1549 ptr += namesz;
1550 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1551 nhdr->n_namesz == sizeof("GNU")) {
1552 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
Stephane Eranianbe96ea82010-10-22 17:25:01 +02001553 size_t sz = min(size, descsz);
1554 memcpy(bf, ptr, sz);
1555 memset(bf + sz, 0, size - sz);
1556 err = descsz;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001557 break;
1558 }
1559 }
1560 ptr += descsz;
1561 }
Dave Martin21916c32010-07-30 09:08:08 -03001562
1563out:
1564 return err;
1565}
1566
1567int filename__read_build_id(const char *filename, void *bf, size_t size)
1568{
1569 int fd, err = -1;
1570 Elf *elf;
1571
1572 if (size < BUILD_ID_SIZE)
1573 goto out;
1574
1575 fd = open(filename, O_RDONLY);
1576 if (fd < 0)
1577 goto out;
1578
1579 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1580 if (elf == NULL) {
1581 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1582 goto out_close;
1583 }
1584
1585 err = elf_read_build_id(elf, bf, size);
1586
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001587 elf_end(elf);
1588out_close:
1589 close(fd);
1590out:
1591 return err;
1592}
1593
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001594int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1595{
1596 int fd, err = -1;
1597
1598 if (size < BUILD_ID_SIZE)
1599 goto out;
1600
1601 fd = open(filename, O_RDONLY);
1602 if (fd < 0)
1603 goto out;
1604
1605 while (1) {
1606 char bf[BUFSIZ];
1607 GElf_Nhdr nhdr;
Stephane Eranianbe96ea82010-10-22 17:25:01 +02001608 size_t namesz, descsz;
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001609
1610 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1611 break;
1612
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001613 namesz = NOTE_ALIGN(nhdr.n_namesz);
1614 descsz = NOTE_ALIGN(nhdr.n_descsz);
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001615 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1616 nhdr.n_namesz == sizeof("GNU")) {
Stephane Eranianbe96ea82010-10-22 17:25:01 +02001617 if (read(fd, bf, namesz) != (ssize_t)namesz)
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001618 break;
1619 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
Stephane Eranianbe96ea82010-10-22 17:25:01 +02001620 size_t sz = min(descsz, size);
1621 if (read(fd, build_id, sz) == (ssize_t)sz) {
1622 memset(build_id + sz, 0, size - sz);
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001623 err = 0;
1624 break;
1625 }
Stephane Eranianbe96ea82010-10-22 17:25:01 +02001626 } else if (read(fd, bf, descsz) != (ssize_t)descsz)
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001627 break;
1628 } else {
1629 int n = namesz + descsz;
1630 if (read(fd, bf, n) != n)
1631 break;
1632 }
1633 }
1634 close(fd);
1635out:
1636 return err;
1637}
1638
Pierre-Loup A. Griffais209bd9e2012-06-22 11:38:13 -07001639static int filename__read_debuglink(const char *filename,
1640 char *debuglink, size_t size)
1641{
1642 int fd, err = -1;
1643 Elf *elf;
1644 GElf_Ehdr ehdr;
1645 GElf_Shdr shdr;
1646 Elf_Data *data;
1647 Elf_Scn *sec;
1648 Elf_Kind ek;
1649
1650 fd = open(filename, O_RDONLY);
1651 if (fd < 0)
1652 goto out;
1653
1654 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1655 if (elf == NULL) {
1656 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1657 goto out_close;
1658 }
1659
1660 ek = elf_kind(elf);
1661 if (ek != ELF_K_ELF)
1662 goto out_close;
1663
1664 if (gelf_getehdr(elf, &ehdr) == NULL) {
1665 pr_err("%s: cannot get elf header.\n", __func__);
1666 goto out_close;
1667 }
1668
1669 sec = elf_section_by_name(elf, &ehdr, &shdr,
1670 ".gnu_debuglink", NULL);
1671 if (sec == NULL)
1672 goto out_close;
1673
1674 data = elf_getdata(sec, NULL);
1675 if (data == NULL)
1676 goto out_close;
1677
1678 /* the start of this section is a zero-terminated string */
1679 strncpy(debuglink, data->d_buf, size);
1680
1681 elf_end(elf);
1682
1683out_close:
1684 close(fd);
1685out:
1686 return err;
1687}
1688
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001689char dso__symtab_origin(const struct dso *dso)
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001690{
1691 static const char origin[] = {
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001692 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
1693 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
1694 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
1695 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
1696 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
1697 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
1698 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
1699 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
1700 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
1701 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
1702 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001703 };
1704
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001705 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001706 return '!';
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001707 return origin[dso->symtab_type];
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001708}
1709
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001710int dso__binary_type_file(struct dso *dso, enum dso_binary_type type,
1711 char *root_dir, char *file, size_t size)
1712{
1713 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1714 int ret = 0;
1715
1716 switch (type) {
1717 case DSO_BINARY_TYPE__DEBUGLINK: {
1718 char *debuglink;
1719
1720 strncpy(file, dso->long_name, size);
1721 debuglink = file + dso->long_name_len;
1722 while (debuglink != file && *debuglink != '/')
1723 debuglink--;
1724 if (*debuglink == '/')
1725 debuglink++;
1726 filename__read_debuglink(dso->long_name, debuglink,
1727 size - (debuglink - file));
1728 }
1729 break;
1730 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1731 /* skip the locally configured cache if a symfs is given */
1732 if (symbol_conf.symfs[0] ||
1733 (dso__build_id_filename(dso, file, size) == NULL))
1734 ret = -1;
1735 break;
1736
1737 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1738 snprintf(file, size, "%s/usr/lib/debug%s.debug",
1739 symbol_conf.symfs, dso->long_name);
1740 break;
1741
1742 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1743 snprintf(file, size, "%s/usr/lib/debug%s",
1744 symbol_conf.symfs, dso->long_name);
1745 break;
1746
1747 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1748 if (!dso->has_build_id) {
1749 ret = -1;
1750 break;
1751 }
1752
1753 build_id__sprintf(dso->build_id,
1754 sizeof(dso->build_id),
1755 build_id_hex);
1756 snprintf(file, size,
1757 "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
1758 symbol_conf.symfs, build_id_hex, build_id_hex + 2);
1759 break;
1760
1761 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1762 snprintf(file, size, "%s%s",
1763 symbol_conf.symfs, dso->long_name);
1764 break;
1765
1766 case DSO_BINARY_TYPE__GUEST_KMODULE:
1767 snprintf(file, size, "%s%s%s", symbol_conf.symfs,
1768 root_dir, dso->long_name);
1769 break;
1770
1771 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1772 snprintf(file, size, "%s%s", symbol_conf.symfs,
1773 dso->long_name);
1774 break;
1775
1776 default:
1777 case DSO_BINARY_TYPE__KALLSYMS:
1778 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1779 case DSO_BINARY_TYPE__JAVA_JIT:
1780 case DSO_BINARY_TYPE__NOT_FOUND:
1781 ret = -1;
1782 break;
1783 }
1784
1785 return ret;
1786}
1787
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001788int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001789{
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001790 char *name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001791 int ret = -1;
1792 int fd;
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001793 u_int i;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001794 struct machine *machine;
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001795 char *root_dir = (char *) "";
Dave Martin6da80ce2010-07-30 09:50:09 -03001796 int want_symtab;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001797
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001798 dso__set_loaded(dso, map->type);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001799
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001800 if (dso->kernel == DSO_TYPE_KERNEL)
1801 return dso__load_kernel_sym(dso, map, filter);
1802 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1803 return dso__load_guest_kernel_sym(dso, map, filter);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001804
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001805 if (map->groups && map->groups->machine)
1806 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001807 else
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001808 machine = NULL;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001809
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001810 name = malloc(PATH_MAX);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001811 if (!name)
1812 return -1;
1813
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001814 dso->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001815
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001816 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
Pekka Enberg981c1252011-08-09 22:54:18 +03001817 struct stat st;
1818
Vasiliy Kulikove9b52ef2011-08-12 00:55:37 +04001819 if (lstat(dso->name, &st) < 0)
Pekka Enberg981c1252011-08-09 22:54:18 +03001820 return -1;
1821
1822 if (st.st_uid && (st.st_uid != geteuid())) {
1823 pr_warning("File %s not owned by current user or root, "
1824 "ignoring it.\n", dso->name);
1825 return -1;
1826 }
1827
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001828 ret = dso__load_perf_map(dso, map, filter);
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001829 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1830 DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001831 return ret;
1832 }
1833
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001834 if (machine)
1835 root_dir = machine->root_dir;
1836
Dave Martin6da80ce2010-07-30 09:50:09 -03001837 /* Iterate over candidate debug images.
1838 * On the first pass, only load images if they have a full symtab.
1839 * Failing that, do a second pass where we accept .dynsym also
1840 */
Arnaldo Carvalho de Melo60e4b102011-03-22 15:42:14 -03001841 want_symtab = 1;
1842restart:
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001843 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
Dave Martin6da80ce2010-07-30 09:50:09 -03001844
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001845 dso->symtab_type = binary_type_symtab[i];
Dave Martin6da80ce2010-07-30 09:50:09 -03001846
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001847 if (dso__binary_type_file(dso, dso->symtab_type,
1848 root_dir, name, PATH_MAX))
1849 continue;
Dave Martin6da80ce2010-07-30 09:50:09 -03001850
1851 /* Name is now the name of the next image to try */
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001852 fd = open(name, O_RDONLY);
Dave Martin6da80ce2010-07-30 09:50:09 -03001853 if (fd < 0)
1854 continue;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001855
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001856 ret = dso__load_sym(dso, map, name, fd, filter, 0,
Dave Martin6da80ce2010-07-30 09:50:09 -03001857 want_symtab);
1858 close(fd);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001859
Dave Martin6da80ce2010-07-30 09:50:09 -03001860 /*
1861 * Some people seem to have debuginfo files _WITHOUT_ debug
1862 * info!?!?
1863 */
1864 if (!ret)
1865 continue;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001866
Dave Martin6da80ce2010-07-30 09:50:09 -03001867 if (ret > 0) {
Jiri Olsa33ff5812012-04-18 15:46:58 +02001868 int nr_plt;
1869
1870 nr_plt = dso__synthesize_plt_symbols(dso, name, map, filter);
Dave Martin6da80ce2010-07-30 09:50:09 -03001871 if (nr_plt > 0)
1872 ret += nr_plt;
1873 break;
1874 }
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001875 }
Dave Martin6da80ce2010-07-30 09:50:09 -03001876
Arnaldo Carvalho de Melo60e4b102011-03-22 15:42:14 -03001877 /*
1878 * If we wanted a full symtab but no image had one,
1879 * relax our requirements and repeat the search.
1880 */
1881 if (ret <= 0 && want_symtab) {
1882 want_symtab = 0;
1883 goto restart;
1884 }
1885
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001886 free(name);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001887 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
Arnaldo Carvalho de Melo1340e6b2009-08-11 17:04:36 -03001888 return 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001889 return ret;
1890}
1891
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001892struct map *map_groups__find_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001893 enum map_type type, const char *name)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001894{
1895 struct rb_node *nd;
1896
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001897 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001898 struct map *map = rb_entry(nd, struct map, rb_node);
1899
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001900 if (map->dso && strcmp(map->dso->short_name, name) == 0)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001901 return map;
1902 }
1903
1904 return NULL;
1905}
1906
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001907static int dso__kernel_module_get_build_id(struct dso *dso,
1908 const char *root_dir)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001909{
1910 char filename[PATH_MAX];
1911 /*
1912 * kernel module short names are of the form "[module]" and
1913 * we need just "module" here.
1914 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001915 const char *name = dso->short_name + 1;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001916
1917 snprintf(filename, sizeof(filename),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001918 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1919 root_dir, (int)strlen(name) - 1, name);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001920
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001921 if (sysfs__read_build_id(filename, dso->build_id,
1922 sizeof(dso->build_id)) == 0)
1923 dso->has_build_id = true;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001924
1925 return 0;
1926}
1927
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001928static int map_groups__set_modules_path_dir(struct map_groups *mg,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001929 const char *dir_name)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001930{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001931 struct dirent *dent;
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001932 DIR *dir = opendir(dir_name);
Gui Jianfeng74534342010-06-24 15:04:02 +08001933 int ret = 0;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001934
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001935 if (!dir) {
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001936 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001937 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001938 }
1939
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001940 while ((dent = readdir(dir)) != NULL) {
1941 char path[PATH_MAX];
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001942 struct stat st;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001943
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001944 /*sshfs might return bad dent->d_type, so we have to stat*/
Namhyung Kim2b600f92011-12-13 00:16:51 +09001945 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001946 if (stat(path, &st))
1947 continue;
1948
1949 if (S_ISDIR(st.st_mode)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001950 if (!strcmp(dent->d_name, ".") ||
1951 !strcmp(dent->d_name, ".."))
1952 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001953
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001954 ret = map_groups__set_modules_path_dir(mg, path);
Gui Jianfeng74534342010-06-24 15:04:02 +08001955 if (ret < 0)
1956 goto out;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001957 } else {
1958 char *dot = strrchr(dent->d_name, '.'),
1959 dso_name[PATH_MAX];
1960 struct map *map;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001961 char *long_name;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001962
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001963 if (dot == NULL || strcmp(dot, ".ko"))
1964 continue;
1965 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1966 (int)(dot - dent->d_name), dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001967
Arnaldo Carvalho de Meloa2a99e82009-10-05 14:26:18 -03001968 strxfrchar(dso_name, '-', '_');
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001969 map = map_groups__find_by_name(mg, MAP__FUNCTION,
1970 dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001971 if (map == NULL)
1972 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001973
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001974 long_name = strdup(path);
Gui Jianfeng74534342010-06-24 15:04:02 +08001975 if (long_name == NULL) {
1976 ret = -1;
1977 goto out;
1978 }
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001979 dso__set_long_name(map->dso, long_name);
Arnaldo Carvalho de Melo6e406252010-07-29 15:11:30 -03001980 map->dso->lname_alloc = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001981 dso__kernel_module_get_build_id(map->dso, "");
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001982 }
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001983 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001984
Gui Jianfeng74534342010-06-24 15:04:02 +08001985out:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001986 closedir(dir);
Gui Jianfeng74534342010-06-24 15:04:02 +08001987 return ret;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001988}
1989
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001990static char *get_kernel_version(const char *root_dir)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001991{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001992 char version[PATH_MAX];
1993 FILE *file;
1994 char *name, *tmp;
1995 const char *prefix = "Linux version ";
1996
1997 sprintf(version, "%s/proc/version", root_dir);
1998 file = fopen(version, "r");
1999 if (!file)
2000 return NULL;
2001
2002 version[0] = '\0';
2003 tmp = fgets(version, sizeof(version), file);
2004 fclose(file);
2005
2006 name = strstr(version, prefix);
2007 if (!name)
2008 return NULL;
2009 name += strlen(prefix);
2010 tmp = strchr(name, ' ');
2011 if (tmp)
2012 *tmp = '\0';
2013
2014 return strdup(name);
2015}
2016
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002017static int machine__set_modules_path(struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002018{
2019 char *version;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002020 char modules_path[PATH_MAX];
2021
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002022 version = get_kernel_version(machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002023 if (!version)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002024 return -1;
2025
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002026 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002027 machine->root_dir, version);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002028 free(version);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002029
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002030 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002031}
2032
2033/*
2034 * Constructor variant for modules (where we know from /proc/modules where
2035 * they are loaded) and for vmlinux, where only after we load all the
2036 * symbols we'll know where it starts and ends.
2037 */
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02002038static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002039{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002040 struct map *map = calloc(1, (sizeof(*map) +
2041 (dso->kernel ? sizeof(struct kmap) : 0)));
2042 if (map != NULL) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002043 /*
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02002044 * ->end will be filled after we load all the symbols
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002045 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002046 map__init(map, type, start, 0, 0, dso);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002047 }
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02002048
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002049 return map;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002050}
2051
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002052struct map *machine__new_module(struct machine *machine, u64 start,
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002053 const char *filename)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02002054{
2055 struct map *map;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002056 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02002057
2058 if (dso == NULL)
2059 return NULL;
2060
2061 map = map__new2(start, dso, MAP__FUNCTION);
2062 if (map == NULL)
2063 return NULL;
2064
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002065 if (machine__is_host(machine))
Jiri Olsa44f24cb2012-07-22 14:14:32 +02002066 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002067 else
Jiri Olsa44f24cb2012-07-22 14:14:32 +02002068 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002069 map_groups__insert(&machine->kmaps, map);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02002070 return map;
2071}
2072
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002073static int machine__create_modules(struct machine *machine)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002074{
2075 char *line = NULL;
2076 size_t n;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002077 FILE *file;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002078 struct map *map;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002079 const char *modules;
2080 char path[PATH_MAX];
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002081
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002082 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002083 modules = symbol_conf.default_guest_modules;
2084 else {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002085 sprintf(path, "%s/proc/modules", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002086 modules = path;
2087 }
2088
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002089 if (symbol__restricted_filename(path, "/proc/modules"))
2090 return -1;
2091
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002092 file = fopen(modules, "r");
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002093 if (file == NULL)
2094 return -1;
2095
2096 while (!feof(file)) {
2097 char name[PATH_MAX];
2098 u64 start;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002099 char *sep;
2100 int line_len;
2101
2102 line_len = getline(&line, &n, file);
2103 if (line_len < 0)
2104 break;
2105
2106 if (!line)
2107 goto out_failure;
2108
2109 line[--line_len] = '\0'; /* \n */
2110
2111 sep = strrchr(line, 'x');
2112 if (sep == NULL)
2113 continue;
2114
2115 hex2u64(sep + 1, &start);
2116
2117 sep = strchr(line, ' ');
2118 if (sep == NULL)
2119 continue;
2120
2121 *sep = '\0';
2122
2123 snprintf(name, sizeof(name), "[%s]", line);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002124 map = machine__new_module(machine, start, name);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02002125 if (map == NULL)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002126 goto out_delete_line;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002127 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002128 }
2129
2130 free(line);
2131 fclose(file);
2132
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002133 return machine__set_modules_path(machine);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002134
2135out_delete_line:
2136 free(line);
2137out_failure:
2138 return -1;
2139}
2140
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002141int dso__load_vmlinux(struct dso *dso, struct map *map,
Franck Bui-Huufd930ff2010-12-10 14:06:03 +01002142 const char *vmlinux, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03002143{
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02002144 int err = -1, fd;
David Ahernec5761e2010-12-09 13:27:07 -07002145 char symfs_vmlinux[PATH_MAX];
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03002146
Arnaldo Carvalho de Meloa639dc62011-02-28 13:54:38 -03002147 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
David Ahernec5761e2010-12-09 13:27:07 -07002148 symbol_conf.symfs, vmlinux);
2149 fd = open(symfs_vmlinux, O_RDONLY);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03002150 if (fd < 0)
2151 return -1;
2152
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002153 dso__set_long_name(dso, (char *)vmlinux);
2154 dso__set_loaded(dso, map->type);
2155 err = dso__load_sym(dso, map, symfs_vmlinux, fd, filter, 0, 0);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03002156 close(fd);
2157
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03002158 if (err > 0)
David Ahernec5761e2010-12-09 13:27:07 -07002159 pr_debug("Using %s for symbols\n", symfs_vmlinux);
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03002160
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03002161 return err;
2162}
2163
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002164int dso__load_vmlinux_path(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02002165 symbol_filter_t filter)
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02002166{
2167 int i, err = 0;
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03002168 char *filename;
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02002169
2170 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03002171 vmlinux_path__nr_entries + 1);
2172
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002173 filename = dso__build_id_filename(dso, NULL, 0);
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03002174 if (filename != NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002175 err = dso__load_vmlinux(dso, map, filename, filter);
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03002176 if (err > 0) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002177 dso__set_long_name(dso, filename);
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03002178 goto out;
2179 }
2180 free(filename);
2181 }
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02002182
2183 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002184 err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02002185 if (err > 0) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002186 dso__set_long_name(dso, strdup(vmlinux_path[i]));
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02002187 break;
2188 }
2189 }
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03002190out:
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02002191 return err;
2192}
2193
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002194static int dso__load_kernel_sym(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02002195 symbol_filter_t filter)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03002196{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002197 int err;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02002198 const char *kallsyms_filename = NULL;
2199 char *kallsyms_allocated_filename = NULL;
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02002200 /*
David Ahernb226a5a72010-12-07 19:39:46 -07002201 * Step 1: if the user specified a kallsyms or vmlinux filename, use
2202 * it and only it, reporting errors to the user if it cannot be used.
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02002203 *
2204 * For instance, try to analyse an ARM perf.data file _without_ a
2205 * build-id, or if the user specifies the wrong path to the right
2206 * vmlinux file, obviously we can't fallback to another vmlinux (a
2207 * x86_86 one, on the machine where analysis is being performed, say),
2208 * or worse, /proc/kallsyms.
2209 *
2210 * If the specified file _has_ a build-id and there is a build-id
2211 * section in the perf.data file, we will still do the expected
2212 * validation in dso__load_vmlinux and will bail out if they don't
2213 * match.
2214 */
David Ahernb226a5a72010-12-07 19:39:46 -07002215 if (symbol_conf.kallsyms_name != NULL) {
2216 kallsyms_filename = symbol_conf.kallsyms_name;
2217 goto do_kallsyms;
2218 }
2219
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02002220 if (symbol_conf.vmlinux_name != NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002221 err = dso__load_vmlinux(dso, map,
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02002222 symbol_conf.vmlinux_name, filter);
Arnaldo Carvalho de Meloe7dadc02010-06-03 18:35:55 -03002223 if (err > 0) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002224 dso__set_long_name(dso,
Arnaldo Carvalho de Meloe7dadc02010-06-03 18:35:55 -03002225 strdup(symbol_conf.vmlinux_name));
2226 goto out_fixup;
2227 }
2228 return err;
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02002229 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002230
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002231 if (vmlinux_path != NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002232 err = dso__load_vmlinux_path(dso, map, filter);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02002233 if (err > 0)
2234 goto out_fixup;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002235 }
2236
David Ahernec5761e2010-12-09 13:27:07 -07002237 /* do not try local files if a symfs was given */
2238 if (symbol_conf.symfs[0] != 0)
2239 return -1;
2240
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02002241 /*
2242 * Say the kernel DSO was created when processing the build-id header table,
2243 * we have a build-id, so check if it is the same as the running kernel,
2244 * using it if it is.
2245 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002246 if (dso->has_build_id) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02002247 u8 kallsyms_build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02002248 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02002249
2250 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
Arnaldo Carvalho de Melo8d0591f2010-01-14 18:30:05 -02002251 sizeof(kallsyms_build_id)) == 0) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002252 if (dso__build_id_equal(dso, kallsyms_build_id)) {
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02002253 kallsyms_filename = "/proc/kallsyms";
Arnaldo Carvalho de Melo8d0591f2010-01-14 18:30:05 -02002254 goto do_kallsyms;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02002255 }
Arnaldo Carvalho de Melo8d0591f2010-01-14 18:30:05 -02002256 }
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02002257 /*
2258 * Now look if we have it on the build-id cache in
2259 * $HOME/.debug/[kernel.kallsyms].
2260 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002261 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02002262 sbuild_id);
2263
2264 if (asprintf(&kallsyms_allocated_filename,
2265 "%s/.debug/[kernel.kallsyms]/%s",
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03002266 getenv("HOME"), sbuild_id) == -1) {
2267 pr_err("Not enough memory for kallsyms file lookup\n");
Arnaldo Carvalho de Melo8d0591f2010-01-14 18:30:05 -02002268 return -1;
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03002269 }
Arnaldo Carvalho de Melo8d0591f2010-01-14 18:30:05 -02002270
Arnaldo Carvalho de Melo19fc2de2010-01-22 14:35:02 -02002271 kallsyms_filename = kallsyms_allocated_filename;
2272
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02002273 if (access(kallsyms_filename, F_OK)) {
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03002274 pr_err("No kallsyms or vmlinux with build-id %s "
2275 "was found\n", sbuild_id);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02002276 free(kallsyms_allocated_filename);
2277 return -1;
2278 }
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02002279 } else {
2280 /*
2281 * Last resort, if we don't have a build-id and couldn't find
2282 * any vmlinux file, try the running kernel kallsyms table.
2283 */
2284 kallsyms_filename = "/proc/kallsyms";
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -02002285 }
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03002286
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02002287do_kallsyms:
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002288 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03002289 if (err > 0)
2290 pr_debug("Using %s for symbols\n", kallsyms_filename);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02002291 free(kallsyms_allocated_filename);
2292
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002293 if (err > 0) {
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002294out_fixup:
Arnaldo Carvalho de Meloe1c7c6a2010-01-22 14:35:01 -02002295 if (kallsyms_filename != NULL)
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002296 dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02002297 map__fixup_start(map);
2298 map__fixup_end(map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002299 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03002300
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03002301 return err;
2302}
2303
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002304static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
2305 symbol_filter_t filter)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002306{
2307 int err;
2308 const char *kallsyms_filename = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002309 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002310 char path[PATH_MAX];
2311
2312 if (!map->groups) {
2313 pr_debug("Guest kernel map hasn't the point to groups\n");
2314 return -1;
2315 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002316 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002317
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002318 if (machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002319 /*
2320 * if the user specified a vmlinux filename, use it and only
2321 * it, reporting errors to the user if it cannot be used.
2322 * Or use file guest_kallsyms inputted by user on commandline
2323 */
2324 if (symbol_conf.default_guest_vmlinux_name != NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002325 err = dso__load_vmlinux(dso, map,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002326 symbol_conf.default_guest_vmlinux_name, filter);
2327 goto out_try_fixup;
2328 }
2329
2330 kallsyms_filename = symbol_conf.default_guest_kallsyms;
2331 if (!kallsyms_filename)
2332 return -1;
2333 } else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002334 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002335 kallsyms_filename = path;
2336 }
2337
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002338 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002339 if (err > 0)
2340 pr_debug("Using %s for symbols\n", kallsyms_filename);
2341
2342out_try_fixup:
2343 if (err > 0) {
2344 if (kallsyms_filename != NULL) {
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -03002345 machine__mmap_name(machine, path, sizeof(path));
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002346 dso__set_long_name(dso, strdup(path));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002347 }
2348 map__fixup_start(map);
2349 map__fixup_end(map);
2350 }
2351
2352 return err;
2353}
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002354
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02002355static void dsos__add(struct list_head *head, struct dso *dso)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002356{
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02002357 list_add_tail(&dso->node, head);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002358}
2359
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02002360static struct dso *dsos__find(struct list_head *head, const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002361{
2362 struct dso *pos;
2363
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02002364 list_for_each_entry(pos, head, node)
Arnaldo Carvalho de Melocf4e5b02010-01-14 23:45:27 -02002365 if (strcmp(pos->long_name, name) == 0)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002366 return pos;
2367 return NULL;
2368}
2369
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -02002370struct dso *__dsos__findnew(struct list_head *head, const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002371{
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -02002372 struct dso *dso = dsos__find(head, name);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002373
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -02002374 if (!dso) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02002375 dso = dso__new(name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02002376 if (dso != NULL) {
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -02002377 dsos__add(head, dso);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02002378 dso__set_basename(dso);
2379 }
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02002380 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002381
2382 return dso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002383}
2384
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03002385size_t __dsos__fprintf(struct list_head *head, FILE *fp)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002386{
2387 struct dso *pos;
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03002388 size_t ret = 0;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002389
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02002390 list_for_each_entry(pos, head, node) {
2391 int i;
2392 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03002393 ret += dso__fprintf(pos, i, fp);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02002394 }
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03002395
2396 return ret;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002397}
2398
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002399size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp)
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02002400{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002401 struct rb_node *nd;
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03002402 size_t ret = 0;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002403
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002404 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002405 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03002406 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
2407 ret += __dsos__fprintf(&pos->user_dsos, fp);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002408 }
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03002409
2410 return ret;
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02002411}
2412
Arnaldo Carvalho de Melo88d3d9b72010-01-14 23:45:30 -02002413static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
2414 bool with_hits)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02002415{
2416 struct dso *pos;
2417 size_t ret = 0;
2418
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02002419 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo88d3d9b72010-01-14 23:45:30 -02002420 if (with_hits && !pos->hit)
2421 continue;
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02002422 ret += dso__fprintf_buildid(pos, fp);
Arnaldo Carvalho de Melo1124ba72009-11-16 21:45:25 -02002423 ret += fprintf(fp, " %s\n", pos->long_name);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02002424 }
2425 return ret;
2426}
2427
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002428size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
2429 bool with_hits)
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03002430{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002431 return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, with_hits) +
2432 __dsos__fprintf_buildid(&machine->user_dsos, fp, with_hits);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03002433}
2434
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002435size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
2436 FILE *fp, bool with_hits)
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02002437{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002438 struct rb_node *nd;
2439 size_t ret = 0;
2440
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002441 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002442 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03002443 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002444 }
2445 return ret;
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02002446}
2447
Jiri Olsaf57b05e2011-06-01 21:43:46 +02002448static struct dso*
2449dso__kernel_findnew(struct machine *machine, const char *name,
2450 const char *short_name, int dso_type)
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02002451{
Jiri Olsaf57b05e2011-06-01 21:43:46 +02002452 /*
2453 * The kernel dso could be created by build_id processing.
2454 */
2455 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02002456
Jiri Olsaf57b05e2011-06-01 21:43:46 +02002457 /*
2458 * We need to run this in all cases, since during the build_id
2459 * processing we had no idea this was the kernel dso.
2460 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002461 if (dso != NULL) {
Jiri Olsaf57b05e2011-06-01 21:43:46 +02002462 dso__set_short_name(dso, short_name);
2463 dso->kernel = dso_type;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002464 }
2465
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002466 return dso;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002467}
2468
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002469void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002470{
2471 char path[PATH_MAX];
2472
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002473 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002474 return;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002475 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002476 if (sysfs__read_build_id(path, dso->build_id,
2477 sizeof(dso->build_id)) == 0)
2478 dso->has_build_id = true;
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02002479}
2480
Jiri Olsaf57b05e2011-06-01 21:43:46 +02002481static struct dso *machine__get_kernel(struct machine *machine)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002482{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002483 const char *vmlinux_name = NULL;
2484 struct dso *kernel;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002485
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002486 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002487 vmlinux_name = symbol_conf.vmlinux_name;
Jiri Olsaf57b05e2011-06-01 21:43:46 +02002488 if (!vmlinux_name)
2489 vmlinux_name = "[kernel.kallsyms]";
2490
2491 kernel = dso__kernel_findnew(machine, vmlinux_name,
2492 "[kernel]",
2493 DSO_TYPE_KERNEL);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002494 } else {
Jiri Olsaf57b05e2011-06-01 21:43:46 +02002495 char bf[PATH_MAX];
2496
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002497 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002498 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
Jiri Olsaf57b05e2011-06-01 21:43:46 +02002499 if (!vmlinux_name)
2500 vmlinux_name = machine__mmap_name(machine, bf,
2501 sizeof(bf));
2502
2503 kernel = dso__kernel_findnew(machine, vmlinux_name,
2504 "[guest.kernel]",
2505 DSO_TYPE_GUEST_KERNEL);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -02002506 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002507
Jiri Olsaf57b05e2011-06-01 21:43:46 +02002508 if (kernel != NULL && (!kernel->has_build_id))
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002509 dso__read_running_kernel_build_id(kernel, machine);
Jiri Olsaf57b05e2011-06-01 21:43:46 +02002510
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002511 return kernel;
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002512}
2513
Ming Leid214afb2010-11-25 19:27:25 +08002514struct process_args {
2515 u64 start;
2516};
2517
2518static int symbol__in_kernel(void *arg, const char *name,
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -02002519 char type __used, u64 start, u64 end __used)
Ming Leid214afb2010-11-25 19:27:25 +08002520{
2521 struct process_args *args = arg;
2522
2523 if (strchr(name, '['))
2524 return 0;
2525
2526 args->start = start;
2527 return 1;
2528}
2529
2530/* Figure out the start address of kernel map from /proc/kallsyms */
2531static u64 machine__get_kernel_start_addr(struct machine *machine)
2532{
2533 const char *filename;
2534 char path[PATH_MAX];
2535 struct process_args args;
2536
2537 if (machine__is_host(machine)) {
2538 filename = "/proc/kallsyms";
2539 } else {
2540 if (machine__is_default_guest(machine))
2541 filename = (char *)symbol_conf.default_guest_kallsyms;
2542 else {
2543 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
2544 filename = path;
2545 }
2546 }
2547
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002548 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
2549 return 0;
2550
Ming Leid214afb2010-11-25 19:27:25 +08002551 if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
2552 return 0;
2553
2554 return args.start;
2555}
2556
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002557int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002558{
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02002559 enum map_type type;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002560 u64 start = machine__get_kernel_start_addr(machine);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002561
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02002562 for (type = 0; type < MAP__NR_TYPES; ++type) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02002563 struct kmap *kmap;
2564
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002565 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
2566 if (machine->vmlinux_maps[type] == NULL)
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02002567 return -1;
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002568
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002569 machine->vmlinux_maps[type]->map_ip =
2570 machine->vmlinux_maps[type]->unmap_ip =
2571 identity__map_ip;
2572 kmap = map__kmap(machine->vmlinux_maps[type]);
2573 kmap->kmaps = &machine->kmaps;
2574 map_groups__insert(&machine->kmaps,
2575 machine->vmlinux_maps[type]);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002576 }
2577
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002578 return 0;
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02002579}
2580
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002581void machine__destroy_kernel_maps(struct machine *machine)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -03002582{
2583 enum map_type type;
2584
2585 for (type = 0; type < MAP__NR_TYPES; ++type) {
2586 struct kmap *kmap;
2587
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002588 if (machine->vmlinux_maps[type] == NULL)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -03002589 continue;
2590
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002591 kmap = map__kmap(machine->vmlinux_maps[type]);
2592 map_groups__remove(&machine->kmaps,
2593 machine->vmlinux_maps[type]);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -03002594 if (kmap->ref_reloc_sym) {
2595 /*
2596 * ref_reloc_sym is shared among all maps, so free just
2597 * on one of them.
2598 */
2599 if (type == MAP__FUNCTION) {
2600 free((char *)kmap->ref_reloc_sym->name);
2601 kmap->ref_reloc_sym->name = NULL;
2602 free(kmap->ref_reloc_sym);
2603 }
2604 kmap->ref_reloc_sym = NULL;
2605 }
2606
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002607 map__delete(machine->vmlinux_maps[type]);
2608 machine->vmlinux_maps[type] = NULL;
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -03002609 }
2610}
2611
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002612int machine__create_kernel_maps(struct machine *machine)
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002613{
Jiri Olsaf57b05e2011-06-01 21:43:46 +02002614 struct dso *kernel = machine__get_kernel(machine);
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002615
2616 if (kernel == NULL ||
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002617 __machine__create_kernel_maps(machine, kernel) < 0)
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002618 return -1;
2619
David Ahernf51304d2012-07-20 17:25:46 -06002620 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
2621 if (machine__is_host(machine))
2622 pr_debug("Problems creating module maps, "
2623 "continuing anyway...\n");
2624 else
2625 pr_debug("Problems creating module maps for guest %d, "
2626 "continuing anyway...\n", machine->pid);
2627 }
2628
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002629 /*
2630 * Now that we have all the maps created, just set the ->end of them:
2631 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002632 map_groups__fixup_end(&machine->kmaps);
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002633 return 0;
2634}
2635
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002636static void vmlinux_path__exit(void)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02002637{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002638 while (--vmlinux_path__nr_entries >= 0) {
2639 free(vmlinux_path[vmlinux_path__nr_entries]);
2640 vmlinux_path[vmlinux_path__nr_entries] = NULL;
2641 }
2642
2643 free(vmlinux_path);
2644 vmlinux_path = NULL;
2645}
2646
2647static int vmlinux_path__init(void)
2648{
2649 struct utsname uts;
2650 char bf[PATH_MAX];
2651
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002652 vmlinux_path = malloc(sizeof(char *) * 5);
2653 if (vmlinux_path == NULL)
2654 return -1;
2655
2656 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
2657 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2658 goto out_fail;
2659 ++vmlinux_path__nr_entries;
2660 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
2661 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2662 goto out_fail;
2663 ++vmlinux_path__nr_entries;
David Ahernec5761e2010-12-09 13:27:07 -07002664
2665 /* only try running kernel version if no symfs was given */
2666 if (symbol_conf.symfs[0] != 0)
2667 return 0;
2668
2669 if (uname(&uts) < 0)
2670 return -1;
2671
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002672 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
2673 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2674 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2675 goto out_fail;
2676 ++vmlinux_path__nr_entries;
2677 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
2678 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2679 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2680 goto out_fail;
2681 ++vmlinux_path__nr_entries;
2682 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
2683 uts.release);
2684 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2685 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2686 goto out_fail;
2687 ++vmlinux_path__nr_entries;
2688
2689 return 0;
2690
2691out_fail:
2692 vmlinux_path__exit();
2693 return -1;
2694}
2695
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002696size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03002697{
2698 int i;
2699 size_t printed = 0;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002700 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03002701
2702 if (kdso->has_build_id) {
2703 char filename[PATH_MAX];
2704 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
2705 printed += fprintf(fp, "[0] %s\n", filename);
2706 }
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03002707
2708 for (i = 0; i < vmlinux_path__nr_entries; ++i)
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03002709 printed += fprintf(fp, "[%d] %s\n",
2710 i + kdso->has_build_id, vmlinux_path[i]);
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03002711
2712 return printed;
2713}
2714
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002715static int setup_list(struct strlist **list, const char *list_str,
2716 const char *list_name)
2717{
2718 if (list_str == NULL)
2719 return 0;
2720
2721 *list = strlist__new(true, list_str);
2722 if (!*list) {
2723 pr_err("problems parsing %s list\n", list_name);
2724 return -1;
2725 }
2726 return 0;
2727}
2728
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002729static bool symbol__read_kptr_restrict(void)
2730{
2731 bool value = false;
2732
2733 if (geteuid() != 0) {
2734 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
2735 if (fp != NULL) {
2736 char line[8];
2737
2738 if (fgets(line, sizeof(line), fp) != NULL)
2739 value = atoi(line) != 0;
2740
2741 fclose(fp);
2742 }
2743 }
2744
2745 return value;
2746}
2747
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02002748int symbol__init(void)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002749{
David Ahernec5761e2010-12-09 13:27:07 -07002750 const char *symfs;
2751
Jovi Zhang85e00b52010-09-09 13:30:59 -03002752 if (symbol_conf.initialized)
2753 return 0;
2754
David S. Miller4d439512011-03-29 14:18:39 -03002755 symbol_conf.priv_size = ALIGN(symbol_conf.priv_size, sizeof(u64));
2756
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02002757 elf_version(EV_CURRENT);
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02002758 if (symbol_conf.sort_by_name)
2759 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2760 sizeof(struct symbol));
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02002761
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02002762 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002763 return -1;
2764
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02002765 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2766 pr_err("'.' is the only non valid --field-separator argument\n");
2767 return -1;
2768 }
2769
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002770 if (setup_list(&symbol_conf.dso_list,
2771 symbol_conf.dso_list_str, "dso") < 0)
2772 return -1;
2773
2774 if (setup_list(&symbol_conf.comm_list,
2775 symbol_conf.comm_list_str, "comm") < 0)
2776 goto out_free_dso_list;
2777
2778 if (setup_list(&symbol_conf.sym_list,
2779 symbol_conf.sym_list_str, "symbol") < 0)
2780 goto out_free_comm_list;
2781
David Ahernec5761e2010-12-09 13:27:07 -07002782 /*
2783 * A path to symbols of "/" is identical to ""
2784 * reset here for simplicity.
2785 */
2786 symfs = realpath(symbol_conf.symfs, NULL);
2787 if (symfs == NULL)
2788 symfs = symbol_conf.symfs;
2789 if (strcmp(symfs, "/") == 0)
2790 symbol_conf.symfs = "";
2791 if (symfs != symbol_conf.symfs)
2792 free((void *)symfs);
2793
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002794 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2795
Jovi Zhang85e00b52010-09-09 13:30:59 -03002796 symbol_conf.initialized = true;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002797 return 0;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002798
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002799out_free_comm_list:
2800 strlist__delete(symbol_conf.comm_list);
Namhyung Kimd74c8962011-12-13 00:16:52 +09002801out_free_dso_list:
2802 strlist__delete(symbol_conf.dso_list);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002803 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002804}
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002805
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002806void symbol__exit(void)
2807{
Jovi Zhang85e00b52010-09-09 13:30:59 -03002808 if (!symbol_conf.initialized)
2809 return;
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002810 strlist__delete(symbol_conf.sym_list);
2811 strlist__delete(symbol_conf.dso_list);
2812 strlist__delete(symbol_conf.comm_list);
2813 vmlinux_path__exit();
2814 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
Jovi Zhang85e00b52010-09-09 13:30:59 -03002815 symbol_conf.initialized = false;
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002816}
2817
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002818int machines__create_kernel_maps(struct rb_root *machines, pid_t pid)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002819{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002820 struct machine *machine = machines__findnew(machines, pid);
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02002821
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002822 if (machine == NULL)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002823 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002824
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002825 return machine__create_kernel_maps(machine);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002826}
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03002827
2828static int hex(char ch)
2829{
2830 if ((ch >= '0') && (ch <= '9'))
2831 return ch - '0';
2832 if ((ch >= 'a') && (ch <= 'f'))
2833 return ch - 'a' + 10;
2834 if ((ch >= 'A') && (ch <= 'F'))
2835 return ch - 'A' + 10;
2836 return -1;
2837}
2838
2839/*
2840 * While we find nice hex chars, build a long_val.
2841 * Return number of chars processed.
2842 */
2843int hex2u64(const char *ptr, u64 *long_val)
2844{
2845 const char *p = ptr;
2846 *long_val = 0;
2847
2848 while (*p) {
2849 const int hex_val = hex(*p);
2850
2851 if (hex_val < 0)
2852 break;
2853
2854 *long_val = (*long_val << 4) | hex_val;
2855 p++;
2856 }
2857
2858 return p - ptr;
2859}
2860
2861char *strxfrchar(char *s, char from, char to)
2862{
2863 char *p = s;
2864
2865 while ((p = strchr(p, from)) != NULL)
2866 *p++ = to;
2867
2868 return s;
2869}
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002870
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002871int machines__create_guest_kernel_maps(struct rb_root *machines)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002872{
2873 int ret = 0;
2874 struct dirent **namelist = NULL;
2875 int i, items = 0;
2876 char path[PATH_MAX];
2877 pid_t pid;
2878
2879 if (symbol_conf.default_guest_vmlinux_name ||
2880 symbol_conf.default_guest_modules ||
2881 symbol_conf.default_guest_kallsyms) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002882 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002883 }
2884
2885 if (symbol_conf.guestmount) {
2886 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2887 if (items <= 0)
2888 return -ENOENT;
2889 for (i = 0; i < items; i++) {
2890 if (!isdigit(namelist[i]->d_name[0])) {
2891 /* Filter out . and .. */
2892 continue;
2893 }
2894 pid = atoi(namelist[i]->d_name);
2895 sprintf(path, "%s/%s/proc/kallsyms",
2896 symbol_conf.guestmount,
2897 namelist[i]->d_name);
2898 ret = access(path, R_OK);
2899 if (ret) {
2900 pr_debug("Can't access file %s\n", path);
2901 goto failure;
2902 }
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002903 machines__create_kernel_maps(machines, pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002904 }
2905failure:
2906 free(namelist);
2907 }
2908
2909 return ret;
2910}
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002911
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002912void machines__destroy_guest_kernel_maps(struct rb_root *machines)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -03002913{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002914 struct rb_node *next = rb_first(machines);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -03002915
2916 while (next) {
2917 struct machine *pos = rb_entry(next, struct machine, rb_node);
2918
2919 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002920 rb_erase(&pos->rb_node, machines);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -03002921 machine__delete(pos);
2922 }
2923}
2924
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002925int machine__load_kallsyms(struct machine *machine, const char *filename,
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002926 enum map_type type, symbol_filter_t filter)
2927{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002928 struct map *map = machine->vmlinux_maps[type];
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002929 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2930
2931 if (ret > 0) {
2932 dso__set_loaded(map->dso, type);
2933 /*
2934 * Since /proc/kallsyms will have multiple sessions for the
2935 * kernel, with modules between them, fixup the end of all
2936 * sections.
2937 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002938 __map_groups__fixup_end(&machine->kmaps, type);
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002939 }
2940
2941 return ret;
2942}
2943
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002944int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002945 symbol_filter_t filter)
2946{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002947 struct map *map = machine->vmlinux_maps[type];
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002948 int ret = dso__load_vmlinux_path(map->dso, map, filter);
2949
2950 if (ret > 0) {
2951 dso__set_loaded(map->dso, type);
2952 map__reloc_vmlinux(map);
2953 }
2954
2955 return ret;
2956}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302957
2958struct map *dso__new_map(const char *name)
2959{
Srikar Dronamraju378474e2012-05-31 17:16:56 +05302960 struct map *map = NULL;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302961 struct dso *dso = dso__new(name);
Srikar Dronamraju378474e2012-05-31 17:16:56 +05302962
2963 if (dso)
2964 map = map__new2(0, dso, MAP__FUNCTION);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302965
2966 return map;
2967}
Jiri Olsa949d1602012-07-22 14:14:33 +02002968
2969static int open_dso(struct dso *dso, struct machine *machine)
2970{
2971 char *root_dir = (char *) "";
2972 char *name;
2973 int fd;
2974
2975 name = malloc(PATH_MAX);
2976 if (!name)
2977 return -ENOMEM;
2978
2979 if (machine)
2980 root_dir = machine->root_dir;
2981
2982 if (dso__binary_type_file(dso, dso->data_type,
2983 root_dir, name, PATH_MAX)) {
2984 free(name);
2985 return -EINVAL;
2986 }
2987
2988 fd = open(name, O_RDONLY);
2989 free(name);
2990 return fd;
2991}
2992
2993int dso__data_fd(struct dso *dso, struct machine *machine)
2994{
2995 int i = 0;
2996
2997 if (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND)
2998 return open_dso(dso, machine);
2999
3000 do {
3001 int fd;
3002
3003 dso->data_type = binary_type_data[i++];
3004
3005 fd = open_dso(dso, machine);
3006 if (fd >= 0)
3007 return fd;
3008
3009 } while (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND);
3010
3011 return -EINVAL;
3012}
3013
Jiri Olsa4dff6242012-07-22 14:14:39 +02003014static void
3015dso_cache__free(struct rb_root *root)
Jiri Olsa949d1602012-07-22 14:14:33 +02003016{
Jiri Olsa4dff6242012-07-22 14:14:39 +02003017 struct rb_node *next = rb_first(root);
3018
3019 while (next) {
3020 struct dso_cache *cache;
3021
3022 cache = rb_entry(next, struct dso_cache, rb_node);
3023 next = rb_next(&cache->rb_node);
3024 rb_erase(&cache->rb_node, root);
3025 free(cache);
3026 }
Jiri Olsa949d1602012-07-22 14:14:33 +02003027}
3028
Jiri Olsa4dff6242012-07-22 14:14:39 +02003029static struct dso_cache*
3030dso_cache__find(struct rb_root *root, u64 offset)
Jiri Olsa949d1602012-07-22 14:14:33 +02003031{
Jiri Olsa4dff6242012-07-22 14:14:39 +02003032 struct rb_node **p = &root->rb_node;
3033 struct rb_node *parent = NULL;
3034 struct dso_cache *cache;
3035
3036 while (*p != NULL) {
3037 u64 end;
3038
3039 parent = *p;
3040 cache = rb_entry(parent, struct dso_cache, rb_node);
3041 end = cache->offset + DSO__DATA_CACHE_SIZE;
3042
3043 if (offset < cache->offset)
3044 p = &(*p)->rb_left;
3045 else if (offset >= end)
3046 p = &(*p)->rb_right;
3047 else
3048 return cache;
3049 }
3050 return NULL;
Jiri Olsa949d1602012-07-22 14:14:33 +02003051}
3052
Jiri Olsa4dff6242012-07-22 14:14:39 +02003053static void
3054dso_cache__insert(struct rb_root *root, struct dso_cache *new)
Jiri Olsa949d1602012-07-22 14:14:33 +02003055{
Jiri Olsa4dff6242012-07-22 14:14:39 +02003056 struct rb_node **p = &root->rb_node;
3057 struct rb_node *parent = NULL;
3058 struct dso_cache *cache;
3059 u64 offset = new->offset;
3060
3061 while (*p != NULL) {
3062 u64 end;
3063
3064 parent = *p;
3065 cache = rb_entry(parent, struct dso_cache, rb_node);
3066 end = cache->offset + DSO__DATA_CACHE_SIZE;
3067
3068 if (offset < cache->offset)
3069 p = &(*p)->rb_left;
3070 else if (offset >= end)
3071 p = &(*p)->rb_right;
3072 }
3073
3074 rb_link_node(&new->rb_node, parent, p);
3075 rb_insert_color(&new->rb_node, root);
3076}
3077
3078static ssize_t
3079dso_cache__memcpy(struct dso_cache *cache, u64 offset,
3080 u8 *data, u64 size)
3081{
3082 u64 cache_offset = offset - cache->offset;
3083 u64 cache_size = min(cache->size - cache_offset, size);
3084
3085 memcpy(data, cache->data + cache_offset, cache_size);
3086 return cache_size;
3087}
3088
3089static ssize_t
3090dso_cache__read(struct dso *dso, struct machine *machine,
3091 u64 offset, u8 *data, ssize_t size)
3092{
3093 struct dso_cache *cache;
3094 ssize_t ret;
Jiri Olsa949d1602012-07-22 14:14:33 +02003095 int fd;
3096
3097 fd = dso__data_fd(dso, machine);
3098 if (fd < 0)
3099 return -1;
3100
3101 do {
Jiri Olsa4dff6242012-07-22 14:14:39 +02003102 u64 cache_offset;
3103
3104 ret = -ENOMEM;
3105
3106 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
3107 if (!cache)
Jiri Olsa949d1602012-07-22 14:14:33 +02003108 break;
3109
Jiri Olsa4dff6242012-07-22 14:14:39 +02003110 cache_offset = offset & DSO__DATA_CACHE_MASK;
3111 ret = -EINVAL;
3112
3113 if (-1 == lseek(fd, cache_offset, SEEK_SET))
Jiri Olsa949d1602012-07-22 14:14:33 +02003114 break;
3115
Jiri Olsa4dff6242012-07-22 14:14:39 +02003116 ret = read(fd, cache->data, DSO__DATA_CACHE_SIZE);
3117 if (ret <= 0)
3118 break;
3119
3120 cache->offset = cache_offset;
3121 cache->size = ret;
3122 dso_cache__insert(&dso->cache, cache);
3123
3124 ret = dso_cache__memcpy(cache, offset, data, size);
Jiri Olsa949d1602012-07-22 14:14:33 +02003125
3126 } while (0);
3127
Jiri Olsa4dff6242012-07-22 14:14:39 +02003128 if (ret <= 0)
3129 free(cache);
3130
Jiri Olsa949d1602012-07-22 14:14:33 +02003131 close(fd);
Jiri Olsa4dff6242012-07-22 14:14:39 +02003132 return ret;
3133}
3134
3135static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
3136 u64 offset, u8 *data, ssize_t size)
3137{
3138 struct dso_cache *cache;
3139
3140 cache = dso_cache__find(&dso->cache, offset);
3141 if (cache)
3142 return dso_cache__memcpy(cache, offset, data, size);
3143 else
3144 return dso_cache__read(dso, machine, offset, data, size);
Jiri Olsa949d1602012-07-22 14:14:33 +02003145}
3146
3147ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
3148 u64 offset, u8 *data, ssize_t size)
3149{
Jiri Olsa4dff6242012-07-22 14:14:39 +02003150 ssize_t r = 0;
3151 u8 *p = data;
3152
3153 do {
3154 ssize_t ret;
3155
3156 ret = dso_cache_read(dso, machine, offset, p, size);
3157 if (ret < 0)
3158 return ret;
3159
3160 /* Reached EOF, return what we have. */
3161 if (!ret)
3162 break;
3163
3164 BUG_ON(ret > size);
3165
3166 r += ret;
3167 p += ret;
3168 offset += ret;
3169 size -= ret;
3170
3171 } while (size);
3172
3173 return r;
Jiri Olsa949d1602012-07-22 14:14:33 +02003174}
3175
3176ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
3177 struct machine *machine, u64 addr,
3178 u8 *data, ssize_t size)
3179{
3180 u64 offset = map->map_ip(map, addr);
3181 return dso__data_read_offset(dso, machine, offset, data, size);
3182}