blob: f8a30686520118d0dfcf4910dfac47b7a5aa83c8 [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
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030018#include <elf.h>
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -020019#include <limits.h>
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030020#include <sys/utsname.h>
Peter Zijlstra2cdbc462009-08-05 14:05:16 +020021
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -020022#ifndef KSYM_NAME_LEN
Ricardo Ribalda Delgadoc752d042011-10-20 09:43:26 +020023#define KSYM_NAME_LEN 256
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -020024#endif
25
Jiri Olsa4dff6242012-07-22 14:14:39 +020026static void dso_cache__free(struct rb_root *root);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030027static int dso__load_kernel_sym(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -020028 symbol_filter_t filter);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030029static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080030 symbol_filter_t filter);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -020031static int vmlinux_path__nr_entries;
32static char **vmlinux_path;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030033
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -020034struct symbol_conf symbol_conf = {
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -020035 .exclude_other = true,
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020036 .use_modules = true,
37 .try_vmlinux_path = true,
Stephane Eranian3e6a2a72011-05-17 17:32:07 +020038 .annotate_src = true,
David Ahernec5761e2010-12-09 13:27:07 -070039 .symfs = "",
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020040};
41
Jiri Olsa44f24cb2012-07-22 14:14:32 +020042static enum dso_binary_type binary_type_symtab[] = {
43 DSO_BINARY_TYPE__KALLSYMS,
44 DSO_BINARY_TYPE__GUEST_KALLSYMS,
45 DSO_BINARY_TYPE__JAVA_JIT,
46 DSO_BINARY_TYPE__DEBUGLINK,
47 DSO_BINARY_TYPE__BUILD_ID_CACHE,
48 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
49 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
50 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
51 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
52 DSO_BINARY_TYPE__GUEST_KMODULE,
53 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
54 DSO_BINARY_TYPE__NOT_FOUND,
55};
56
Jiri Olsa028df762012-08-01 14:47:57 +020057#define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
Jiri Olsa44f24cb2012-07-22 14:14:32 +020058
Jiri Olsa949d1602012-07-22 14:14:33 +020059static enum dso_binary_type binary_type_data[] = {
60 DSO_BINARY_TYPE__BUILD_ID_CACHE,
61 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
62 DSO_BINARY_TYPE__NOT_FOUND,
63};
64
Jiri Olsa028df762012-08-01 14:47:57 +020065#define DSO_BINARY_TYPE__DATA_CNT ARRAY_SIZE(binary_type_data)
Jiri Olsa949d1602012-07-22 14:14:33 +020066
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030067int dso__name_len(const struct dso *dso)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030068{
David Miller1e2dd2f2012-03-25 16:28:22 -040069 if (!dso)
70 return strlen("[unknown]");
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030071 if (verbose)
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030072 return dso->long_name_len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030073
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030074 return dso->short_name_len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030075}
76
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030077bool dso__loaded(const struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020078{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030079 return dso->loaded & (1 << type);
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020080}
81
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030082bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020083{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030084 return dso->sorted_by_name & (1 << type);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020085}
86
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030087static void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020088{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030089 dso->sorted_by_name |= (1 << type);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020090}
91
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -020092bool symbol_type__is_a(char symbol_type, enum map_type map_type)
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020093{
Anton Blanchard31877902011-08-24 16:40:16 +100094 symbol_type = toupper(symbol_type);
95
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020096 switch (map_type) {
97 case MAP__FUNCTION:
98 return symbol_type == 'T' || symbol_type == 'W';
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -020099 case MAP__VARIABLE:
Anton Blanchard31877902011-08-24 16:40:16 +1000100 return symbol_type == 'D';
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -0200101 default:
102 return false;
103 }
104}
105
Anton Blanchard694bf402011-08-24 16:40:17 +1000106static int prefix_underscores_count(const char *str)
107{
108 const char *tail = str;
109
110 while (*tail == '_')
111 tail++;
112
113 return tail - str;
114}
115
116#define SYMBOL_A 0
117#define SYMBOL_B 1
118
119static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
120{
121 s64 a;
122 s64 b;
123
124 /* Prefer a symbol with non zero length */
125 a = syma->end - syma->start;
126 b = symb->end - symb->start;
127 if ((b == 0) && (a > 0))
128 return SYMBOL_A;
129 else if ((a == 0) && (b > 0))
130 return SYMBOL_B;
131
132 /* Prefer a non weak symbol over a weak one */
133 a = syma->binding == STB_WEAK;
134 b = symb->binding == STB_WEAK;
135 if (b && !a)
136 return SYMBOL_A;
137 if (a && !b)
138 return SYMBOL_B;
139
140 /* Prefer a global symbol over a non global one */
141 a = syma->binding == STB_GLOBAL;
142 b = symb->binding == STB_GLOBAL;
143 if (a && !b)
144 return SYMBOL_A;
145 if (b && !a)
146 return SYMBOL_B;
147
148 /* Prefer a symbol with less underscores */
149 a = prefix_underscores_count(syma->name);
150 b = prefix_underscores_count(symb->name);
151 if (b > a)
152 return SYMBOL_A;
153 else if (a > b)
154 return SYMBOL_B;
155
156 /* If all else fails, choose the symbol with the longest name */
157 if (strlen(syma->name) >= strlen(symb->name))
158 return SYMBOL_A;
159 else
160 return SYMBOL_B;
161}
162
Namhyung Kime5a18452012-08-06 13:41:20 +0900163void symbols__fixup_duplicate(struct rb_root *symbols)
Anton Blanchard694bf402011-08-24 16:40:17 +1000164{
165 struct rb_node *nd;
166 struct symbol *curr, *next;
167
168 nd = rb_first(symbols);
169
170 while (nd) {
171 curr = rb_entry(nd, struct symbol, rb_node);
172again:
173 nd = rb_next(&curr->rb_node);
174 next = rb_entry(nd, struct symbol, rb_node);
175
176 if (!nd)
177 break;
178
179 if (curr->start != next->start)
180 continue;
181
182 if (choose_best_symbol(curr, next) == SYMBOL_A) {
183 rb_erase(&next->rb_node, symbols);
184 goto again;
185 } else {
186 nd = rb_next(&curr->rb_node);
187 rb_erase(&curr->rb_node, symbols);
188 }
189 }
190}
191
Namhyung Kime5a18452012-08-06 13:41:20 +0900192void symbols__fixup_end(struct rb_root *symbols)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300193{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300194 struct rb_node *nd, *prevnd = rb_first(symbols);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300195 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300196
197 if (prevnd == NULL)
198 return;
199
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300200 curr = rb_entry(prevnd, struct symbol, rb_node);
201
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300202 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300203 prev = curr;
204 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300205
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200206 if (prev->end == prev->start && prev->end != curr->start)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300207 prev->end = curr->start - 1;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300208 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300209
210 /* Last entry */
211 if (curr->end == curr->start)
212 curr->end = roundup(curr->start, 4096);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300213}
214
Namhyung Kime5a18452012-08-06 13:41:20 +0900215void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300216{
217 struct map *prev, *curr;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300218 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300219
220 if (prevnd == NULL)
221 return;
222
223 curr = rb_entry(prevnd, struct map, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300224
225 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
226 prev = curr;
227 curr = rb_entry(nd, struct map, rb_node);
228 prev->end = curr->start - 1;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300229 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -0200230
231 /*
232 * We still haven't the actual symbols, so guess the
233 * last map final address.
234 */
Ian Munsie9d1faba2010-11-25 15:12:53 +1100235 curr->end = ~0ULL;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300236}
237
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300238static void map_groups__fixup_end(struct map_groups *mg)
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200239{
240 int i;
241 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300242 __map_groups__fixup_end(mg, i);
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200243}
244
Namhyung Kime5a18452012-08-06 13:41:20 +0900245struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300246{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300247 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300248 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
249 sizeof(*sym) + namelen));
250 if (sym == NULL)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200251 return NULL;
252
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200253 if (symbol_conf.priv_size)
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300254 sym = ((void *)sym) + symbol_conf.priv_size;
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200255
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300256 sym->start = start;
257 sym->end = len ? start + len - 1 : start;
258 sym->binding = binding;
259 sym->namelen = namelen - 1;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200260
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300261 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
262 __func__, name, start, sym->end);
263 memcpy(sym->name, name, namelen);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200264
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300265 return sym;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300266}
267
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300268void symbol__delete(struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300269{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300270 free(((void *)sym) - symbol_conf.priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300271}
272
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300273static size_t symbol__fprintf(struct symbol *sym, FILE *fp)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300274{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200275 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300276 sym->start, sym->end,
277 sym->binding == STB_GLOBAL ? 'g' :
278 sym->binding == STB_LOCAL ? 'l' : 'w',
279 sym->name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300280}
281
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900282size_t symbol__fprintf_symname_offs(const struct symbol *sym,
283 const struct addr_location *al, FILE *fp)
284{
285 unsigned long offset;
286 size_t length;
287
288 if (sym && sym->name) {
289 length = fprintf(fp, "%s", sym->name);
290 if (al) {
291 offset = al->addr - sym->start;
292 length += fprintf(fp, "+0x%lx", offset);
293 }
294 return length;
295 } else
296 return fprintf(fp, "[unknown]");
297}
298
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900299size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
300{
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900301 return symbol__fprintf_symname_offs(sym, NULL, fp);
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900302}
303
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300304void dso__set_long_name(struct dso *dso, char *name)
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200305{
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -0200306 if (name == NULL)
307 return;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300308 dso->long_name = name;
309 dso->long_name_len = strlen(name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200310}
311
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300312static void dso__set_short_name(struct dso *dso, const char *name)
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300313{
314 if (name == NULL)
315 return;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300316 dso->short_name = name;
317 dso->short_name_len = strlen(name);
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300318}
319
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300320static void dso__set_basename(struct dso *dso)
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200321{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300322 dso__set_short_name(dso, basename(dso->long_name));
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200323}
324
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200325struct dso *dso__new(const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300326{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300327 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300328
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300329 if (dso != NULL) {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200330 int i;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300331 strcpy(dso->name, name);
332 dso__set_long_name(dso, dso->name);
333 dso__set_short_name(dso, dso->name);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200334 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300335 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
Jiri Olsa4dff6242012-07-22 14:14:39 +0200336 dso->cache = RB_ROOT;
Jiri Olsa44f24cb2012-07-22 14:14:32 +0200337 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
Jiri Olsa949d1602012-07-22 14:14:33 +0200338 dso->data_type = DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300339 dso->loaded = 0;
340 dso->sorted_by_name = 0;
341 dso->has_build_id = 0;
342 dso->kernel = DSO_TYPE_USER;
Jiri Olsa8db48412012-05-30 14:23:42 +0200343 dso->needs_swap = DSO_SWAP__UNSET;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300344 INIT_LIST_HEAD(&dso->node);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300345 }
346
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300347 return dso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300348}
349
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300350static void symbols__delete(struct rb_root *symbols)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300351{
352 struct symbol *pos;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300353 struct rb_node *next = rb_first(symbols);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300354
355 while (next) {
356 pos = rb_entry(next, struct symbol, rb_node);
357 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300358 rb_erase(&pos->rb_node, symbols);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200359 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300360 }
361}
362
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300363void dso__delete(struct dso *dso)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300364{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200365 int i;
366 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300367 symbols__delete(&dso->symbols[i]);
368 if (dso->sname_alloc)
369 free((char *)dso->short_name);
370 if (dso->lname_alloc)
371 free(dso->long_name);
Jiri Olsa4dff6242012-07-22 14:14:39 +0200372 dso_cache__free(&dso->cache);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300373 free(dso);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300374}
375
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300376void dso__set_build_id(struct dso *dso, void *build_id)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200377{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300378 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
379 dso->has_build_id = 1;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200380}
381
Namhyung Kime5a18452012-08-06 13:41:20 +0900382void symbols__insert(struct rb_root *symbols, struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300383{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300384 struct rb_node **p = &symbols->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300385 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000386 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300387 struct symbol *s;
388
389 while (*p != NULL) {
390 parent = *p;
391 s = rb_entry(parent, struct symbol, rb_node);
392 if (ip < s->start)
393 p = &(*p)->rb_left;
394 else
395 p = &(*p)->rb_right;
396 }
397 rb_link_node(&sym->rb_node, parent, p);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300398 rb_insert_color(&sym->rb_node, symbols);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300399}
400
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300401static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300402{
403 struct rb_node *n;
404
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300405 if (symbols == NULL)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300406 return NULL;
407
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300408 n = symbols->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300409
410 while (n) {
411 struct symbol *s = rb_entry(n, struct symbol, rb_node);
412
413 if (ip < s->start)
414 n = n->rb_left;
415 else if (ip > s->end)
416 n = n->rb_right;
417 else
418 return s;
419 }
420
421 return NULL;
422}
423
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200424struct symbol_name_rb_node {
425 struct rb_node rb_node;
426 struct symbol sym;
427};
428
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300429static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200430{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300431 struct rb_node **p = &symbols->rb_node;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200432 struct rb_node *parent = NULL;
Rabin Vincent02a9d032010-11-23 22:08:18 +0530433 struct symbol_name_rb_node *symn, *s;
434
435 symn = container_of(sym, struct symbol_name_rb_node, sym);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200436
437 while (*p != NULL) {
438 parent = *p;
439 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
440 if (strcmp(sym->name, s->sym.name) < 0)
441 p = &(*p)->rb_left;
442 else
443 p = &(*p)->rb_right;
444 }
445 rb_link_node(&symn->rb_node, parent, p);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300446 rb_insert_color(&symn->rb_node, symbols);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200447}
448
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300449static void symbols__sort_by_name(struct rb_root *symbols,
450 struct rb_root *source)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200451{
452 struct rb_node *nd;
453
454 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
455 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300456 symbols__insert_by_name(symbols, pos);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200457 }
458}
459
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300460static struct symbol *symbols__find_by_name(struct rb_root *symbols,
461 const char *name)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200462{
463 struct rb_node *n;
464
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300465 if (symbols == NULL)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200466 return NULL;
467
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300468 n = symbols->rb_node;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200469
470 while (n) {
471 struct symbol_name_rb_node *s;
472 int cmp;
473
474 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
475 cmp = strcmp(name, s->sym.name);
476
477 if (cmp < 0)
478 n = n->rb_left;
479 else if (cmp > 0)
480 n = n->rb_right;
481 else
482 return &s->sym;
483 }
484
485 return NULL;
486}
487
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300488struct symbol *dso__find_symbol(struct dso *dso,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200489 enum map_type type, u64 addr)
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200490{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300491 return symbols__find(&dso->symbols[type], addr);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200492}
493
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300494struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200495 const char *name)
496{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300497 return symbols__find_by_name(&dso->symbol_names[type], name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200498}
499
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300500void dso__sort_by_name(struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200501{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300502 dso__set_sorted_by_name(dso, type);
503 return symbols__sort_by_name(&dso->symbol_names[type],
504 &dso->symbols[type]);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200505}
506
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300507int build_id__sprintf(const u8 *build_id, int len, char *bf)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200508{
509 char *bid = bf;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300510 const u8 *raw = build_id;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200511 int i;
512
513 for (i = 0; i < len; ++i) {
514 sprintf(bid, "%02x", *raw);
515 ++raw;
516 bid += 2;
517 }
518
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300519 return raw - build_id;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200520}
521
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300522size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300523{
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200524 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200525
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300526 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200527 return fprintf(fp, "%s", sbuild_id);
528}
529
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300530size_t dso__fprintf_symbols_by_name(struct dso *dso,
531 enum map_type type, FILE *fp)
Srikar Dronamraju90f18e62010-08-25 19:13:29 +0530532{
533 size_t ret = 0;
534 struct rb_node *nd;
535 struct symbol_name_rb_node *pos;
536
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300537 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
Srikar Dronamraju90f18e62010-08-25 19:13:29 +0530538 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
539 fprintf(fp, "%s\n", pos->sym.name);
540 }
541
542 return ret;
543}
544
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300545size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200546{
547 struct rb_node *nd;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300548 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200549
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300550 if (dso->short_name != dso->long_name)
551 ret += fprintf(fp, "%s, ", dso->long_name);
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -0300552 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300553 dso->loaded ? "" : "NOT ");
554 ret += dso__fprintf_buildid(dso, fp);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200555 ret += fprintf(fp, ")\n");
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300556 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200557 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
558 ret += symbol__fprintf(pos, fp);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300559 }
560
561 return ret;
562}
563
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200564int kallsyms__parse(const char *filename, void *arg,
565 int (*process_symbol)(void *arg, const char *name,
Cody P Schafer82151522012-08-10 15:22:48 -0700566 char type, u64 start))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300567{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300568 char *line = NULL;
569 size_t n;
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200570 int err = -1;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200571 FILE *file = fopen(filename, "r");
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300572
573 if (file == NULL)
574 goto out_failure;
575
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200576 err = 0;
577
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300578 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000579 u64 start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300580 int line_len, len;
581 char symbol_type;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300582 char *symbol_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300583
584 line_len = getline(&line, &n, file);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800585 if (line_len < 0 || !line)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300586 break;
587
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300588 line[--line_len] = '\0'; /* \n */
589
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300590 len = hex2u64(line, &start);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300591
592 len++;
593 if (len + 2 >= line_len)
594 continue;
595
Anton Blanchard31877902011-08-24 16:40:16 +1000596 symbol_type = line[len];
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200597 len += 2;
598 symbol_name = line + len;
599 len = line_len - len;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300600
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200601 if (len >= KSYM_NAME_LEN) {
602 err = -1;
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200603 break;
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200604 }
605
Anton Blanchard3f5a4272011-08-24 16:40:15 +1000606 err = process_symbol(arg, symbol_name,
Cody P Schafer82151522012-08-10 15:22:48 -0700607 symbol_type, start);
Anton Blanchard3f5a4272011-08-24 16:40:15 +1000608 if (err)
609 break;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300610 }
611
612 free(line);
613 fclose(file);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200614 return err;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300615
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300616out_failure:
617 return -1;
618}
619
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200620struct process_kallsyms_args {
621 struct map *map;
622 struct dso *dso;
623};
624
Arnaldo Carvalho de Meloc408fed2010-08-05 12:59:47 -0300625static u8 kallsyms2elf_type(char type)
626{
627 if (type == 'W')
628 return STB_WEAK;
629
630 return isupper(type) ? STB_GLOBAL : STB_LOCAL;
631}
632
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200633static int map__process_kallsym_symbol(void *arg, const char *name,
Cody P Schafer82151522012-08-10 15:22:48 -0700634 char type, u64 start)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200635{
636 struct symbol *sym;
637 struct process_kallsyms_args *a = arg;
638 struct rb_root *root = &a->dso->symbols[a->map->type];
639
640 if (!symbol_type__is_a(type, a->map->type))
641 return 0;
642
Cody P Schafer82151522012-08-10 15:22:48 -0700643 /*
644 * module symbols are not sorted so we add all
645 * symbols, setting length to 0, and rely on
646 * symbols__fixup_end() to fix it up.
647 */
648 sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200649 if (sym == NULL)
650 return -ENOMEM;
651 /*
652 * We will pass the symbols to the filter later, in
653 * map__split_kallsyms, when we have split the maps per module
654 */
655 symbols__insert(root, sym);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800656
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200657 return 0;
658}
659
660/*
661 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
662 * so that we can in the next step set the symbol ->end address and then
663 * call kernel_maps__split_kallsyms.
664 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300665static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200666 struct map *map)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200667{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300668 struct process_kallsyms_args args = { .map = map, .dso = dso, };
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200669 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200670}
671
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300672/*
673 * Split the symbols into maps, making sure there are no overlaps, i.e. the
674 * kernel range is broken in several maps, named [kernel].N, as we don't have
675 * the original ELF section names vmlinux have.
676 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300677static int dso__split_kallsyms(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200678 symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300679{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200680 struct map_groups *kmaps = map__kmap(map)->kmaps;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300681 struct machine *machine = kmaps->machine;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200682 struct map *curr_map = map;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300683 struct symbol *pos;
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200684 int count = 0, moved = 0;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300685 struct rb_root *root = &dso->symbols[map->type];
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200686 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300687 int kernel_range = 0;
688
689 while (next) {
690 char *module;
691
692 pos = rb_entry(next, struct symbol, rb_node);
693 next = rb_next(&pos->rb_node);
694
695 module = strchr(pos->name, '\t');
696 if (module) {
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200697 if (!symbol_conf.use_modules)
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200698 goto discard_symbol;
699
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300700 *module++ = '\0';
701
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200702 if (strcmp(curr_map->dso->short_name, module)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800703 if (curr_map != map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300704 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300705 machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800706 /*
707 * We assume all symbols of a module are
708 * continuous in * kallsyms, so curr_map
709 * points to a module and all its
710 * symbols are in its kmap. Mark it as
711 * loaded.
712 */
713 dso__set_loaded(curr_map->dso,
714 curr_map->type);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300715 }
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200716
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800717 curr_map = map_groups__find_by_name(kmaps,
718 map->type, module);
719 if (curr_map == NULL) {
Arnaldo Carvalho de Melo2f519032010-05-17 17:57:59 -0300720 pr_debug("%s/proc/{kallsyms,modules} "
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800721 "inconsistency while looking "
722 "for \"%s\" module!\n",
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300723 machine->root_dir, module);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800724 curr_map = map;
725 goto discard_symbol;
726 }
727
728 if (curr_map->dso->loaded &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300729 !machine__is_default_guest(machine))
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200730 goto discard_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300731 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300732 /*
733 * So that we look just like we get from .ko files,
734 * i.e. not prelinked, relative to map->start.
735 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200736 pos->start = curr_map->map_ip(curr_map, pos->start);
737 pos->end = curr_map->map_ip(curr_map, pos->end);
738 } else if (curr_map != map) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300739 char dso_name[PATH_MAX];
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300740 struct dso *ndso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300741
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200742 if (count == 0) {
743 curr_map = map;
744 goto filter_symbol;
745 }
746
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300747 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800748 snprintf(dso_name, sizeof(dso_name),
749 "[guest.kernel].%d",
750 kernel_range++);
751 else
752 snprintf(dso_name, sizeof(dso_name),
753 "[kernel].%d",
754 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300755
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300756 ndso = dso__new(dso_name);
757 if (ndso == NULL)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300758 return -1;
759
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300760 ndso->kernel = dso->kernel;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800761
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300762 curr_map = map__new2(pos->start, ndso, map->type);
Zhang, Yanmin37fe5fc2010-02-25 11:00:51 +0800763 if (curr_map == NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300764 dso__delete(ndso);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300765 return -1;
766 }
767
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200768 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200769 map_groups__insert(kmaps, curr_map);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300770 ++kernel_range;
771 }
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200772filter_symbol:
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200773 if (filter && filter(curr_map, pos)) {
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200774discard_symbol: rb_erase(&pos->rb_node, root);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200775 symbol__delete(pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300776 } else {
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200777 if (curr_map != map) {
778 rb_erase(&pos->rb_node, root);
779 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200780 ++moved;
781 } else
782 ++count;
Mike Galbraith9974f492009-07-02 08:05:58 +0200783 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300784 }
785
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800786 if (curr_map != map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300787 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300788 machine__is_default_guest(kmaps->machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800789 dso__set_loaded(curr_map->dso, curr_map->type);
790 }
791
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200792 return count + moved;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300793}
794
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300795static bool symbol__restricted_filename(const char *filename,
796 const char *restricted_filename)
797{
798 bool restricted = false;
799
800 if (symbol_conf.kptr_restrict) {
801 char *r = realpath(filename, NULL);
802
803 if (r != NULL) {
804 restricted = strcmp(r, restricted_filename) == 0;
805 free(r);
806 return restricted;
807 }
808 }
809
810 return restricted;
811}
812
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300813int dso__load_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200814 struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300815{
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300816 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
817 return -1;
818
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300819 if (dso__load_all_kallsyms(dso, filename, map) < 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300820 return -1;
821
Anton Blanchard694bf402011-08-24 16:40:17 +1000822 symbols__fixup_duplicate(&dso->symbols[map->type]);
Anton Blanchard3f5a4272011-08-24 16:40:15 +1000823 symbols__fixup_end(&dso->symbols[map->type]);
824
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300825 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Jiri Olsa44f24cb2012-07-22 14:14:32 +0200826 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800827 else
Jiri Olsa44f24cb2012-07-22 14:14:32 +0200828 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300829
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300830 return dso__split_kallsyms(dso, map, filter);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300831}
832
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300833static int dso__load_perf_map(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200834 symbol_filter_t filter)
Pekka Enberg80d496b2009-06-08 21:12:48 +0300835{
836 char *line = NULL;
837 size_t n;
838 FILE *file;
839 int nr_syms = 0;
840
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300841 file = fopen(dso->long_name, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +0300842 if (file == NULL)
843 goto out_failure;
844
845 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000846 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300847 struct symbol *sym;
848 int line_len, len;
849
850 line_len = getline(&line, &n, file);
851 if (line_len < 0)
852 break;
853
854 if (!line)
855 goto out_failure;
856
857 line[--line_len] = '\0'; /* \n */
858
859 len = hex2u64(line, &start);
860
861 len++;
862 if (len + 2 >= line_len)
863 continue;
864
865 len += hex2u64(line + len, &size);
866
867 len++;
868 if (len + 2 >= line_len)
869 continue;
870
Arnaldo Carvalho de Meloc408fed2010-08-05 12:59:47 -0300871 sym = symbol__new(start, size, STB_GLOBAL, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300872
873 if (sym == NULL)
874 goto out_delete_line;
875
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300876 if (filter && filter(map, sym))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200877 symbol__delete(sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300878 else {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300879 symbols__insert(&dso->symbols[map->type], sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300880 nr_syms++;
881 }
882 }
883
884 free(line);
885 fclose(file);
886
887 return nr_syms;
888
889out_delete_line:
890 free(line);
891out_failure:
892 return -1;
893}
894
Namhyung Kime5a18452012-08-06 13:41:20 +0900895bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -0200896{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300897 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -0200898}
899
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800900bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100901{
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200902 bool have_build_id = false;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100903 struct dso *pos;
904
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200905 list_for_each_entry(pos, head, node) {
906 if (with_hits && !pos->hit)
907 continue;
Arnaldo Carvalho de Melof6e14672010-05-19 11:13:02 -0300908 if (pos->has_build_id) {
909 have_build_id = true;
910 continue;
911 }
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200912 if (filename__read_build_id(pos->long_name, pos->build_id,
913 sizeof(pos->build_id)) > 0) {
914 have_build_id = true;
915 pos->has_build_id = true;
916 }
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200917 }
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100918
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200919 return have_build_id;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100920}
921
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300922char dso__symtab_origin(const struct dso *dso)
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300923{
924 static const char origin[] = {
Jiri Olsa44f24cb2012-07-22 14:14:32 +0200925 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
Cody P Schafer21ea4532012-08-10 15:22:56 -0700926 [DSO_BINARY_TYPE__VMLINUX] = 'v',
Jiri Olsa44f24cb2012-07-22 14:14:32 +0200927 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
928 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
929 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
930 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
931 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
932 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
933 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
934 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
935 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
936 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
Cody P Schafer21ea4532012-08-10 15:22:56 -0700937 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300938 };
939
Jiri Olsa44f24cb2012-07-22 14:14:32 +0200940 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300941 return '!';
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300942 return origin[dso->symtab_type];
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300943}
944
Jiri Olsa44f24cb2012-07-22 14:14:32 +0200945int dso__binary_type_file(struct dso *dso, enum dso_binary_type type,
946 char *root_dir, char *file, size_t size)
947{
948 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
949 int ret = 0;
950
951 switch (type) {
952 case DSO_BINARY_TYPE__DEBUGLINK: {
953 char *debuglink;
954
955 strncpy(file, dso->long_name, size);
956 debuglink = file + dso->long_name_len;
957 while (debuglink != file && *debuglink != '/')
958 debuglink--;
959 if (*debuglink == '/')
960 debuglink++;
961 filename__read_debuglink(dso->long_name, debuglink,
962 size - (debuglink - file));
963 }
964 break;
965 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
966 /* skip the locally configured cache if a symfs is given */
967 if (symbol_conf.symfs[0] ||
968 (dso__build_id_filename(dso, file, size) == NULL))
969 ret = -1;
970 break;
971
972 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
973 snprintf(file, size, "%s/usr/lib/debug%s.debug",
974 symbol_conf.symfs, dso->long_name);
975 break;
976
977 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
978 snprintf(file, size, "%s/usr/lib/debug%s",
979 symbol_conf.symfs, dso->long_name);
980 break;
981
982 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
983 if (!dso->has_build_id) {
984 ret = -1;
985 break;
986 }
987
988 build_id__sprintf(dso->build_id,
989 sizeof(dso->build_id),
990 build_id_hex);
991 snprintf(file, size,
992 "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
993 symbol_conf.symfs, build_id_hex, build_id_hex + 2);
994 break;
995
996 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
997 snprintf(file, size, "%s%s",
998 symbol_conf.symfs, dso->long_name);
999 break;
1000
1001 case DSO_BINARY_TYPE__GUEST_KMODULE:
1002 snprintf(file, size, "%s%s%s", symbol_conf.symfs,
1003 root_dir, dso->long_name);
1004 break;
1005
1006 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1007 snprintf(file, size, "%s%s", symbol_conf.symfs,
1008 dso->long_name);
1009 break;
1010
1011 default:
1012 case DSO_BINARY_TYPE__KALLSYMS:
Cody P Schafer21ea4532012-08-10 15:22:56 -07001013 case DSO_BINARY_TYPE__VMLINUX:
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001014 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
Cody P Schafer21ea4532012-08-10 15:22:56 -07001015 case DSO_BINARY_TYPE__GUEST_VMLINUX:
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001016 case DSO_BINARY_TYPE__JAVA_JIT:
1017 case DSO_BINARY_TYPE__NOT_FOUND:
1018 ret = -1;
1019 break;
1020 }
1021
1022 return ret;
1023}
1024
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001025int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001026{
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001027 char *name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001028 int ret = -1;
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001029 struct symsrc ss;
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001030 u_int i;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001031 struct machine *machine;
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001032 char *root_dir = (char *) "";
Dave Martin6da80ce2010-07-30 09:50:09 -03001033 int want_symtab;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001034
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001035 dso__set_loaded(dso, map->type);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001036
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001037 if (dso->kernel == DSO_TYPE_KERNEL)
1038 return dso__load_kernel_sym(dso, map, filter);
1039 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1040 return dso__load_guest_kernel_sym(dso, map, filter);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001041
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001042 if (map->groups && map->groups->machine)
1043 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001044 else
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001045 machine = NULL;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001046
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001047 name = malloc(PATH_MAX);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001048 if (!name)
1049 return -1;
1050
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001051 dso->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001052
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001053 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
Pekka Enberg981c1252011-08-09 22:54:18 +03001054 struct stat st;
1055
Vasiliy Kulikove9b52ef2011-08-12 00:55:37 +04001056 if (lstat(dso->name, &st) < 0)
Pekka Enberg981c1252011-08-09 22:54:18 +03001057 return -1;
1058
1059 if (st.st_uid && (st.st_uid != geteuid())) {
1060 pr_warning("File %s not owned by current user or root, "
1061 "ignoring it.\n", dso->name);
1062 return -1;
1063 }
1064
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001065 ret = dso__load_perf_map(dso, map, filter);
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001066 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1067 DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001068 return ret;
1069 }
1070
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001071 if (machine)
1072 root_dir = machine->root_dir;
1073
Dave Martin6da80ce2010-07-30 09:50:09 -03001074 /* Iterate over candidate debug images.
1075 * On the first pass, only load images if they have a full symtab.
1076 * Failing that, do a second pass where we accept .dynsym also
1077 */
Arnaldo Carvalho de Melo60e4b102011-03-22 15:42:14 -03001078 want_symtab = 1;
1079restart:
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001080 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
Dave Martin6da80ce2010-07-30 09:50:09 -03001081
Cody P Schafer005f9292012-08-10 15:22:58 -07001082 enum dso_binary_type symtab_type = binary_type_symtab[i];
Dave Martin6da80ce2010-07-30 09:50:09 -03001083
Cody P Schafer005f9292012-08-10 15:22:58 -07001084 if (dso__binary_type_file(dso, symtab_type,
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001085 root_dir, name, PATH_MAX))
1086 continue;
Dave Martin6da80ce2010-07-30 09:50:09 -03001087
1088 /* Name is now the name of the next image to try */
Cody P Schafer005f9292012-08-10 15:22:58 -07001089 if (symsrc__init(&ss, dso, name, symtab_type) < 0)
Dave Martin6da80ce2010-07-30 09:50:09 -03001090 continue;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001091
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001092 ret = dso__load_sym(dso, map, &ss, filter, 0,
Dave Martin6da80ce2010-07-30 09:50:09 -03001093 want_symtab);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001094
Dave Martin6da80ce2010-07-30 09:50:09 -03001095 /*
1096 * Some people seem to have debuginfo files _WITHOUT_ debug
1097 * info!?!?
1098 */
Cody P Schafera44f6052012-08-10 15:22:59 -07001099 if (!ret) {
1100 symsrc__destroy(&ss);
Dave Martin6da80ce2010-07-30 09:50:09 -03001101 continue;
Cody P Schafera44f6052012-08-10 15:22:59 -07001102 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001103
Dave Martin6da80ce2010-07-30 09:50:09 -03001104 if (ret > 0) {
Jiri Olsa33ff5812012-04-18 15:46:58 +02001105 int nr_plt;
1106
Cody P Schafera44f6052012-08-10 15:22:59 -07001107 nr_plt = dso__synthesize_plt_symbols(dso, &ss, map, filter);
Dave Martin6da80ce2010-07-30 09:50:09 -03001108 if (nr_plt > 0)
1109 ret += nr_plt;
Cody P Schafera44f6052012-08-10 15:22:59 -07001110 symsrc__destroy(&ss);
Dave Martin6da80ce2010-07-30 09:50:09 -03001111 break;
1112 }
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001113 }
Dave Martin6da80ce2010-07-30 09:50:09 -03001114
Arnaldo Carvalho de Melo60e4b102011-03-22 15:42:14 -03001115 /*
1116 * If we wanted a full symtab but no image had one,
1117 * relax our requirements and repeat the search.
1118 */
1119 if (ret <= 0 && want_symtab) {
1120 want_symtab = 0;
1121 goto restart;
1122 }
1123
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001124 free(name);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001125 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
Arnaldo Carvalho de Melo1340e6b2009-08-11 17:04:36 -03001126 return 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001127 return ret;
1128}
1129
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001130struct map *map_groups__find_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001131 enum map_type type, const char *name)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001132{
1133 struct rb_node *nd;
1134
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001135 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001136 struct map *map = rb_entry(nd, struct map, rb_node);
1137
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001138 if (map->dso && strcmp(map->dso->short_name, name) == 0)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001139 return map;
1140 }
1141
1142 return NULL;
1143}
1144
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001145static int dso__kernel_module_get_build_id(struct dso *dso,
1146 const char *root_dir)
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001147{
1148 char filename[PATH_MAX];
1149 /*
1150 * kernel module short names are of the form "[module]" and
1151 * we need just "module" here.
1152 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001153 const char *name = dso->short_name + 1;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001154
1155 snprintf(filename, sizeof(filename),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001156 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1157 root_dir, (int)strlen(name) - 1, name);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001158
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001159 if (sysfs__read_build_id(filename, dso->build_id,
1160 sizeof(dso->build_id)) == 0)
1161 dso->has_build_id = true;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001162
1163 return 0;
1164}
1165
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001166static int map_groups__set_modules_path_dir(struct map_groups *mg,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001167 const char *dir_name)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001168{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001169 struct dirent *dent;
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001170 DIR *dir = opendir(dir_name);
Gui Jianfeng74534342010-06-24 15:04:02 +08001171 int ret = 0;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001172
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001173 if (!dir) {
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001174 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001175 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001176 }
1177
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001178 while ((dent = readdir(dir)) != NULL) {
1179 char path[PATH_MAX];
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001180 struct stat st;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001181
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001182 /*sshfs might return bad dent->d_type, so we have to stat*/
Namhyung Kim2b600f92011-12-13 00:16:51 +09001183 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001184 if (stat(path, &st))
1185 continue;
1186
1187 if (S_ISDIR(st.st_mode)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001188 if (!strcmp(dent->d_name, ".") ||
1189 !strcmp(dent->d_name, ".."))
1190 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001191
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001192 ret = map_groups__set_modules_path_dir(mg, path);
Gui Jianfeng74534342010-06-24 15:04:02 +08001193 if (ret < 0)
1194 goto out;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001195 } else {
1196 char *dot = strrchr(dent->d_name, '.'),
1197 dso_name[PATH_MAX];
1198 struct map *map;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001199 char *long_name;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001200
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001201 if (dot == NULL || strcmp(dot, ".ko"))
1202 continue;
1203 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1204 (int)(dot - dent->d_name), dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001205
Arnaldo Carvalho de Meloa2a99e82009-10-05 14:26:18 -03001206 strxfrchar(dso_name, '-', '_');
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001207 map = map_groups__find_by_name(mg, MAP__FUNCTION,
1208 dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001209 if (map == NULL)
1210 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001211
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001212 long_name = strdup(path);
Gui Jianfeng74534342010-06-24 15:04:02 +08001213 if (long_name == NULL) {
1214 ret = -1;
1215 goto out;
1216 }
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001217 dso__set_long_name(map->dso, long_name);
Arnaldo Carvalho de Melo6e406252010-07-29 15:11:30 -03001218 map->dso->lname_alloc = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001219 dso__kernel_module_get_build_id(map->dso, "");
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001220 }
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001221 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001222
Gui Jianfeng74534342010-06-24 15:04:02 +08001223out:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001224 closedir(dir);
Gui Jianfeng74534342010-06-24 15:04:02 +08001225 return ret;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001226}
1227
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001228static char *get_kernel_version(const char *root_dir)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001229{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001230 char version[PATH_MAX];
1231 FILE *file;
1232 char *name, *tmp;
1233 const char *prefix = "Linux version ";
1234
1235 sprintf(version, "%s/proc/version", root_dir);
1236 file = fopen(version, "r");
1237 if (!file)
1238 return NULL;
1239
1240 version[0] = '\0';
1241 tmp = fgets(version, sizeof(version), file);
1242 fclose(file);
1243
1244 name = strstr(version, prefix);
1245 if (!name)
1246 return NULL;
1247 name += strlen(prefix);
1248 tmp = strchr(name, ' ');
1249 if (tmp)
1250 *tmp = '\0';
1251
1252 return strdup(name);
1253}
1254
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001255static int machine__set_modules_path(struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001256{
1257 char *version;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001258 char modules_path[PATH_MAX];
1259
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001260 version = get_kernel_version(machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001261 if (!version)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001262 return -1;
1263
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001264 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001265 machine->root_dir, version);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001266 free(version);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001267
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001268 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001269}
1270
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001271struct map *machine__new_module(struct machine *machine, u64 start,
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001272 const char *filename)
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001273{
1274 struct map *map;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001275 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001276
1277 if (dso == NULL)
1278 return NULL;
1279
1280 map = map__new2(start, dso, MAP__FUNCTION);
1281 if (map == NULL)
1282 return NULL;
1283
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001284 if (machine__is_host(machine))
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001285 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001286 else
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001287 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001288 map_groups__insert(&machine->kmaps, map);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001289 return map;
1290}
1291
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001292static int machine__create_modules(struct machine *machine)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001293{
1294 char *line = NULL;
1295 size_t n;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001296 FILE *file;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001297 struct map *map;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001298 const char *modules;
1299 char path[PATH_MAX];
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001300
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001301 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001302 modules = symbol_conf.default_guest_modules;
1303 else {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001304 sprintf(path, "%s/proc/modules", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001305 modules = path;
1306 }
1307
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001308 if (symbol__restricted_filename(path, "/proc/modules"))
1309 return -1;
1310
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001311 file = fopen(modules, "r");
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001312 if (file == NULL)
1313 return -1;
1314
1315 while (!feof(file)) {
1316 char name[PATH_MAX];
1317 u64 start;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001318 char *sep;
1319 int line_len;
1320
1321 line_len = getline(&line, &n, file);
1322 if (line_len < 0)
1323 break;
1324
1325 if (!line)
1326 goto out_failure;
1327
1328 line[--line_len] = '\0'; /* \n */
1329
1330 sep = strrchr(line, 'x');
1331 if (sep == NULL)
1332 continue;
1333
1334 hex2u64(sep + 1, &start);
1335
1336 sep = strchr(line, ' ');
1337 if (sep == NULL)
1338 continue;
1339
1340 *sep = '\0';
1341
1342 snprintf(name, sizeof(name), "[%s]", line);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001343 map = machine__new_module(machine, start, name);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001344 if (map == NULL)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001345 goto out_delete_line;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001346 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001347 }
1348
1349 free(line);
1350 fclose(file);
1351
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001352 return machine__set_modules_path(machine);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001353
1354out_delete_line:
1355 free(line);
1356out_failure:
1357 return -1;
1358}
1359
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001360int dso__load_vmlinux(struct dso *dso, struct map *map,
Franck Bui-Huufd930ff2010-12-10 14:06:03 +01001361 const char *vmlinux, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001362{
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001363 int err = -1;
1364 struct symsrc ss;
David Ahernec5761e2010-12-09 13:27:07 -07001365 char symfs_vmlinux[PATH_MAX];
Cody P Schafer005f9292012-08-10 15:22:58 -07001366 enum dso_binary_type symtab_type;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001367
Arnaldo Carvalho de Meloa639dc62011-02-28 13:54:38 -03001368 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
David Ahernec5761e2010-12-09 13:27:07 -07001369 symbol_conf.symfs, vmlinux);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001370
Cody P Schafer21ea4532012-08-10 15:22:56 -07001371 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Cody P Schafer005f9292012-08-10 15:22:58 -07001372 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
Cody P Schafer21ea4532012-08-10 15:22:56 -07001373 else
Cody P Schafer005f9292012-08-10 15:22:58 -07001374 symtab_type = DSO_BINARY_TYPE__VMLINUX;
Cody P Schafer21ea4532012-08-10 15:22:56 -07001375
Cody P Schafer005f9292012-08-10 15:22:58 -07001376 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001377 return -1;
1378
1379 err = dso__load_sym(dso, map, &ss, filter, 0, 0);
1380 symsrc__destroy(&ss);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001381
Cody P Schafer515850e2012-08-10 15:22:54 -07001382 if (err > 0) {
1383 dso__set_long_name(dso, (char *)vmlinux);
1384 dso__set_loaded(dso, map->type);
David Ahernec5761e2010-12-09 13:27:07 -07001385 pr_debug("Using %s for symbols\n", symfs_vmlinux);
Cody P Schafer515850e2012-08-10 15:22:54 -07001386 }
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001387
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001388 return err;
1389}
1390
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001391int dso__load_vmlinux_path(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001392 symbol_filter_t filter)
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001393{
1394 int i, err = 0;
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001395 char *filename;
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001396
1397 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001398 vmlinux_path__nr_entries + 1);
1399
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001400 filename = dso__build_id_filename(dso, NULL, 0);
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001401 if (filename != NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001402 err = dso__load_vmlinux(dso, map, filename, filter);
Cody P Schafer261ee822012-08-10 15:22:52 -07001403 if (err > 0)
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001404 goto out;
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001405 free(filename);
1406 }
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001407
1408 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001409 err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001410 if (err > 0) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001411 dso__set_long_name(dso, strdup(vmlinux_path[i]));
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001412 break;
1413 }
1414 }
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001415out:
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001416 return err;
1417}
1418
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001419static int dso__load_kernel_sym(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001420 symbol_filter_t filter)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001421{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001422 int err;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001423 const char *kallsyms_filename = NULL;
1424 char *kallsyms_allocated_filename = NULL;
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001425 /*
David Ahernb226a5a72010-12-07 19:39:46 -07001426 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1427 * it and only it, reporting errors to the user if it cannot be used.
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001428 *
1429 * For instance, try to analyse an ARM perf.data file _without_ a
1430 * build-id, or if the user specifies the wrong path to the right
1431 * vmlinux file, obviously we can't fallback to another vmlinux (a
1432 * x86_86 one, on the machine where analysis is being performed, say),
1433 * or worse, /proc/kallsyms.
1434 *
1435 * If the specified file _has_ a build-id and there is a build-id
1436 * section in the perf.data file, we will still do the expected
1437 * validation in dso__load_vmlinux and will bail out if they don't
1438 * match.
1439 */
David Ahernb226a5a72010-12-07 19:39:46 -07001440 if (symbol_conf.kallsyms_name != NULL) {
1441 kallsyms_filename = symbol_conf.kallsyms_name;
1442 goto do_kallsyms;
1443 }
1444
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001445 if (symbol_conf.vmlinux_name != NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001446 err = dso__load_vmlinux(dso, map,
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001447 symbol_conf.vmlinux_name, filter);
Arnaldo Carvalho de Meloe7dadc02010-06-03 18:35:55 -03001448 if (err > 0) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001449 dso__set_long_name(dso,
Arnaldo Carvalho de Meloe7dadc02010-06-03 18:35:55 -03001450 strdup(symbol_conf.vmlinux_name));
1451 goto out_fixup;
1452 }
1453 return err;
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001454 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001455
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001456 if (vmlinux_path != NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001457 err = dso__load_vmlinux_path(dso, map, filter);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001458 if (err > 0)
1459 goto out_fixup;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001460 }
1461
David Ahernec5761e2010-12-09 13:27:07 -07001462 /* do not try local files if a symfs was given */
1463 if (symbol_conf.symfs[0] != 0)
1464 return -1;
1465
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001466 /*
1467 * Say the kernel DSO was created when processing the build-id header table,
1468 * we have a build-id, so check if it is the same as the running kernel,
1469 * using it if it is.
1470 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001471 if (dso->has_build_id) {
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001472 u8 kallsyms_build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001473 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001474
1475 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001476 sizeof(kallsyms_build_id)) == 0) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001477 if (dso__build_id_equal(dso, kallsyms_build_id)) {
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001478 kallsyms_filename = "/proc/kallsyms";
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001479 goto do_kallsyms;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001480 }
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001481 }
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001482 /*
1483 * Now look if we have it on the build-id cache in
1484 * $HOME/.debug/[kernel.kallsyms].
1485 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001486 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001487 sbuild_id);
1488
1489 if (asprintf(&kallsyms_allocated_filename,
1490 "%s/.debug/[kernel.kallsyms]/%s",
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001491 getenv("HOME"), sbuild_id) == -1) {
1492 pr_err("Not enough memory for kallsyms file lookup\n");
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001493 return -1;
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001494 }
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001495
Arnaldo Carvalho de Melo19fc2de2010-01-22 14:35:02 -02001496 kallsyms_filename = kallsyms_allocated_filename;
1497
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001498 if (access(kallsyms_filename, F_OK)) {
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001499 pr_err("No kallsyms or vmlinux with build-id %s "
1500 "was found\n", sbuild_id);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001501 free(kallsyms_allocated_filename);
1502 return -1;
1503 }
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001504 } else {
1505 /*
1506 * Last resort, if we don't have a build-id and couldn't find
1507 * any vmlinux file, try the running kernel kallsyms table.
1508 */
1509 kallsyms_filename = "/proc/kallsyms";
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -02001510 }
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001511
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001512do_kallsyms:
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001513 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001514 if (err > 0)
1515 pr_debug("Using %s for symbols\n", kallsyms_filename);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001516 free(kallsyms_allocated_filename);
1517
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001518 if (err > 0) {
Cody P Schafer0a0317b2012-08-10 15:22:53 -07001519 dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001520out_fixup:
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001521 map__fixup_start(map);
1522 map__fixup_end(map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001523 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001524
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001525 return err;
1526}
1527
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001528static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
1529 symbol_filter_t filter)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001530{
1531 int err;
1532 const char *kallsyms_filename = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001533 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001534 char path[PATH_MAX];
1535
1536 if (!map->groups) {
1537 pr_debug("Guest kernel map hasn't the point to groups\n");
1538 return -1;
1539 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001540 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001541
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001542 if (machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001543 /*
1544 * if the user specified a vmlinux filename, use it and only
1545 * it, reporting errors to the user if it cannot be used.
1546 * Or use file guest_kallsyms inputted by user on commandline
1547 */
1548 if (symbol_conf.default_guest_vmlinux_name != NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001549 err = dso__load_vmlinux(dso, map,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001550 symbol_conf.default_guest_vmlinux_name, filter);
1551 goto out_try_fixup;
1552 }
1553
1554 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1555 if (!kallsyms_filename)
1556 return -1;
1557 } else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001558 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001559 kallsyms_filename = path;
1560 }
1561
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001562 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001563 if (err > 0)
1564 pr_debug("Using %s for symbols\n", kallsyms_filename);
1565
1566out_try_fixup:
1567 if (err > 0) {
1568 if (kallsyms_filename != NULL) {
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -03001569 machine__mmap_name(machine, path, sizeof(path));
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001570 dso__set_long_name(dso, strdup(path));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001571 }
1572 map__fixup_start(map);
1573 map__fixup_end(map);
1574 }
1575
1576 return err;
1577}
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001578
Namhyung Kime5a18452012-08-06 13:41:20 +09001579void dsos__add(struct list_head *head, struct dso *dso)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001580{
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001581 list_add_tail(&dso->node, head);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001582}
1583
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001584static struct dso *dsos__find(struct list_head *head, const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001585{
1586 struct dso *pos;
1587
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001588 list_for_each_entry(pos, head, node)
Arnaldo Carvalho de Melocf4e5b02010-01-14 23:45:27 -02001589 if (strcmp(pos->long_name, name) == 0)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001590 return pos;
1591 return NULL;
1592}
1593
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -02001594struct dso *__dsos__findnew(struct list_head *head, const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001595{
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -02001596 struct dso *dso = dsos__find(head, name);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001597
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -02001598 if (!dso) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001599 dso = dso__new(name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001600 if (dso != NULL) {
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -02001601 dsos__add(head, dso);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001602 dso__set_basename(dso);
1603 }
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001604 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001605
1606 return dso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001607}
1608
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001609size_t __dsos__fprintf(struct list_head *head, FILE *fp)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001610{
1611 struct dso *pos;
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001612 size_t ret = 0;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001613
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001614 list_for_each_entry(pos, head, node) {
1615 int i;
1616 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001617 ret += dso__fprintf(pos, i, fp);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001618 }
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001619
1620 return ret;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001621}
1622
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001623size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp)
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001624{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001625 struct rb_node *nd;
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001626 size_t ret = 0;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001627
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001628 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001629 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001630 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
1631 ret += __dsos__fprintf(&pos->user_dsos, fp);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001632 }
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001633
1634 return ret;
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001635}
1636
Arnaldo Carvalho de Melo88d3d9b72010-01-14 23:45:30 -02001637static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1638 bool with_hits)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001639{
1640 struct dso *pos;
1641 size_t ret = 0;
1642
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001643 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo88d3d9b72010-01-14 23:45:30 -02001644 if (with_hits && !pos->hit)
1645 continue;
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001646 ret += dso__fprintf_buildid(pos, fp);
Arnaldo Carvalho de Melo1124ba72009-11-16 21:45:25 -02001647 ret += fprintf(fp, " %s\n", pos->long_name);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001648 }
1649 return ret;
1650}
1651
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001652size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
1653 bool with_hits)
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001654{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001655 return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, with_hits) +
1656 __dsos__fprintf_buildid(&machine->user_dsos, fp, with_hits);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001657}
1658
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001659size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
1660 FILE *fp, bool with_hits)
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001661{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001662 struct rb_node *nd;
1663 size_t ret = 0;
1664
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001665 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001666 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001667 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001668 }
1669 return ret;
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001670}
1671
Jiri Olsaf57b05e2011-06-01 21:43:46 +02001672static struct dso*
1673dso__kernel_findnew(struct machine *machine, const char *name,
1674 const char *short_name, int dso_type)
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02001675{
Jiri Olsaf57b05e2011-06-01 21:43:46 +02001676 /*
1677 * The kernel dso could be created by build_id processing.
1678 */
1679 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02001680
Jiri Olsaf57b05e2011-06-01 21:43:46 +02001681 /*
1682 * We need to run this in all cases, since during the build_id
1683 * processing we had no idea this was the kernel dso.
1684 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001685 if (dso != NULL) {
Jiri Olsaf57b05e2011-06-01 21:43:46 +02001686 dso__set_short_name(dso, short_name);
1687 dso->kernel = dso_type;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001688 }
1689
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001690 return dso;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001691}
1692
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001693void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001694{
1695 char path[PATH_MAX];
1696
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001697 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001698 return;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001699 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001700 if (sysfs__read_build_id(path, dso->build_id,
1701 sizeof(dso->build_id)) == 0)
1702 dso->has_build_id = true;
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02001703}
1704
Jiri Olsaf57b05e2011-06-01 21:43:46 +02001705static struct dso *machine__get_kernel(struct machine *machine)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001706{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001707 const char *vmlinux_name = NULL;
1708 struct dso *kernel;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001709
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001710 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001711 vmlinux_name = symbol_conf.vmlinux_name;
Jiri Olsaf57b05e2011-06-01 21:43:46 +02001712 if (!vmlinux_name)
1713 vmlinux_name = "[kernel.kallsyms]";
1714
1715 kernel = dso__kernel_findnew(machine, vmlinux_name,
1716 "[kernel]",
1717 DSO_TYPE_KERNEL);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001718 } else {
Jiri Olsaf57b05e2011-06-01 21:43:46 +02001719 char bf[PATH_MAX];
1720
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001721 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001722 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
Jiri Olsaf57b05e2011-06-01 21:43:46 +02001723 if (!vmlinux_name)
1724 vmlinux_name = machine__mmap_name(machine, bf,
1725 sizeof(bf));
1726
1727 kernel = dso__kernel_findnew(machine, vmlinux_name,
1728 "[guest.kernel]",
1729 DSO_TYPE_GUEST_KERNEL);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -02001730 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001731
Jiri Olsaf57b05e2011-06-01 21:43:46 +02001732 if (kernel != NULL && (!kernel->has_build_id))
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001733 dso__read_running_kernel_build_id(kernel, machine);
Jiri Olsaf57b05e2011-06-01 21:43:46 +02001734
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001735 return kernel;
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001736}
1737
Ming Leid214afb2010-11-25 19:27:25 +08001738struct process_args {
1739 u64 start;
1740};
1741
1742static int symbol__in_kernel(void *arg, const char *name,
Cody P Schafer82151522012-08-10 15:22:48 -07001743 char type __used, u64 start)
Ming Leid214afb2010-11-25 19:27:25 +08001744{
1745 struct process_args *args = arg;
1746
1747 if (strchr(name, '['))
1748 return 0;
1749
1750 args->start = start;
1751 return 1;
1752}
1753
1754/* Figure out the start address of kernel map from /proc/kallsyms */
1755static u64 machine__get_kernel_start_addr(struct machine *machine)
1756{
1757 const char *filename;
1758 char path[PATH_MAX];
1759 struct process_args args;
1760
1761 if (machine__is_host(machine)) {
1762 filename = "/proc/kallsyms";
1763 } else {
1764 if (machine__is_default_guest(machine))
1765 filename = (char *)symbol_conf.default_guest_kallsyms;
1766 else {
1767 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1768 filename = path;
1769 }
1770 }
1771
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001772 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1773 return 0;
1774
Ming Leid214afb2010-11-25 19:27:25 +08001775 if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
1776 return 0;
1777
1778 return args.start;
1779}
1780
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001781int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001782{
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02001783 enum map_type type;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001784 u64 start = machine__get_kernel_start_addr(machine);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001785
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02001786 for (type = 0; type < MAP__NR_TYPES; ++type) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001787 struct kmap *kmap;
1788
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001789 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
1790 if (machine->vmlinux_maps[type] == NULL)
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02001791 return -1;
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001792
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001793 machine->vmlinux_maps[type]->map_ip =
1794 machine->vmlinux_maps[type]->unmap_ip =
1795 identity__map_ip;
1796 kmap = map__kmap(machine->vmlinux_maps[type]);
1797 kmap->kmaps = &machine->kmaps;
1798 map_groups__insert(&machine->kmaps,
1799 machine->vmlinux_maps[type]);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001800 }
1801
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001802 return 0;
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001803}
1804
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001805void machine__destroy_kernel_maps(struct machine *machine)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -03001806{
1807 enum map_type type;
1808
1809 for (type = 0; type < MAP__NR_TYPES; ++type) {
1810 struct kmap *kmap;
1811
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001812 if (machine->vmlinux_maps[type] == NULL)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -03001813 continue;
1814
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001815 kmap = map__kmap(machine->vmlinux_maps[type]);
1816 map_groups__remove(&machine->kmaps,
1817 machine->vmlinux_maps[type]);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -03001818 if (kmap->ref_reloc_sym) {
1819 /*
1820 * ref_reloc_sym is shared among all maps, so free just
1821 * on one of them.
1822 */
1823 if (type == MAP__FUNCTION) {
1824 free((char *)kmap->ref_reloc_sym->name);
1825 kmap->ref_reloc_sym->name = NULL;
1826 free(kmap->ref_reloc_sym);
1827 }
1828 kmap->ref_reloc_sym = NULL;
1829 }
1830
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001831 map__delete(machine->vmlinux_maps[type]);
1832 machine->vmlinux_maps[type] = NULL;
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -03001833 }
1834}
1835
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001836int machine__create_kernel_maps(struct machine *machine)
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03001837{
Jiri Olsaf57b05e2011-06-01 21:43:46 +02001838 struct dso *kernel = machine__get_kernel(machine);
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03001839
1840 if (kernel == NULL ||
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001841 __machine__create_kernel_maps(machine, kernel) < 0)
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03001842 return -1;
1843
David Ahernf51304d2012-07-20 17:25:46 -06001844 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1845 if (machine__is_host(machine))
1846 pr_debug("Problems creating module maps, "
1847 "continuing anyway...\n");
1848 else
1849 pr_debug("Problems creating module maps for guest %d, "
1850 "continuing anyway...\n", machine->pid);
1851 }
1852
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03001853 /*
1854 * Now that we have all the maps created, just set the ->end of them:
1855 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001856 map_groups__fixup_end(&machine->kmaps);
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03001857 return 0;
1858}
1859
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001860static void vmlinux_path__exit(void)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001861{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001862 while (--vmlinux_path__nr_entries >= 0) {
1863 free(vmlinux_path[vmlinux_path__nr_entries]);
1864 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1865 }
1866
1867 free(vmlinux_path);
1868 vmlinux_path = NULL;
1869}
1870
1871static int vmlinux_path__init(void)
1872{
1873 struct utsname uts;
1874 char bf[PATH_MAX];
1875
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001876 vmlinux_path = malloc(sizeof(char *) * 5);
1877 if (vmlinux_path == NULL)
1878 return -1;
1879
1880 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1881 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1882 goto out_fail;
1883 ++vmlinux_path__nr_entries;
1884 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1885 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1886 goto out_fail;
1887 ++vmlinux_path__nr_entries;
David Ahernec5761e2010-12-09 13:27:07 -07001888
1889 /* only try running kernel version if no symfs was given */
1890 if (symbol_conf.symfs[0] != 0)
1891 return 0;
1892
1893 if (uname(&uts) < 0)
1894 return -1;
1895
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001896 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1897 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1898 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1899 goto out_fail;
1900 ++vmlinux_path__nr_entries;
1901 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1902 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1903 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1904 goto out_fail;
1905 ++vmlinux_path__nr_entries;
1906 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1907 uts.release);
1908 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1909 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1910 goto out_fail;
1911 ++vmlinux_path__nr_entries;
1912
1913 return 0;
1914
1915out_fail:
1916 vmlinux_path__exit();
1917 return -1;
1918}
1919
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001920size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03001921{
1922 int i;
1923 size_t printed = 0;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001924 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001925
1926 if (kdso->has_build_id) {
1927 char filename[PATH_MAX];
1928 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
1929 printed += fprintf(fp, "[0] %s\n", filename);
1930 }
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03001931
1932 for (i = 0; i < vmlinux_path__nr_entries; ++i)
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001933 printed += fprintf(fp, "[%d] %s\n",
1934 i + kdso->has_build_id, vmlinux_path[i]);
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03001935
1936 return printed;
1937}
1938
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001939static int setup_list(struct strlist **list, const char *list_str,
1940 const char *list_name)
1941{
1942 if (list_str == NULL)
1943 return 0;
1944
1945 *list = strlist__new(true, list_str);
1946 if (!*list) {
1947 pr_err("problems parsing %s list\n", list_name);
1948 return -1;
1949 }
1950 return 0;
1951}
1952
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001953static bool symbol__read_kptr_restrict(void)
1954{
1955 bool value = false;
1956
1957 if (geteuid() != 0) {
1958 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1959 if (fp != NULL) {
1960 char line[8];
1961
1962 if (fgets(line, sizeof(line), fp) != NULL)
1963 value = atoi(line) != 0;
1964
1965 fclose(fp);
1966 }
1967 }
1968
1969 return value;
1970}
1971
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001972int symbol__init(void)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001973{
David Ahernec5761e2010-12-09 13:27:07 -07001974 const char *symfs;
1975
Jovi Zhang85e00b52010-09-09 13:30:59 -03001976 if (symbol_conf.initialized)
1977 return 0;
1978
David S. Miller4d439512011-03-29 14:18:39 -03001979 symbol_conf.priv_size = ALIGN(symbol_conf.priv_size, sizeof(u64));
1980
Namhyung Kim166ccc92012-08-06 13:41:19 +09001981 symbol__elf_init();
1982
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001983 if (symbol_conf.sort_by_name)
1984 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1985 sizeof(struct symbol));
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001986
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001987 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001988 return -1;
1989
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001990 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1991 pr_err("'.' is the only non valid --field-separator argument\n");
1992 return -1;
1993 }
1994
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001995 if (setup_list(&symbol_conf.dso_list,
1996 symbol_conf.dso_list_str, "dso") < 0)
1997 return -1;
1998
1999 if (setup_list(&symbol_conf.comm_list,
2000 symbol_conf.comm_list_str, "comm") < 0)
2001 goto out_free_dso_list;
2002
2003 if (setup_list(&symbol_conf.sym_list,
2004 symbol_conf.sym_list_str, "symbol") < 0)
2005 goto out_free_comm_list;
2006
David Ahernec5761e2010-12-09 13:27:07 -07002007 /*
2008 * A path to symbols of "/" is identical to ""
2009 * reset here for simplicity.
2010 */
2011 symfs = realpath(symbol_conf.symfs, NULL);
2012 if (symfs == NULL)
2013 symfs = symbol_conf.symfs;
2014 if (strcmp(symfs, "/") == 0)
2015 symbol_conf.symfs = "";
2016 if (symfs != symbol_conf.symfs)
2017 free((void *)symfs);
2018
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002019 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2020
Jovi Zhang85e00b52010-09-09 13:30:59 -03002021 symbol_conf.initialized = true;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002022 return 0;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002023
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002024out_free_comm_list:
2025 strlist__delete(symbol_conf.comm_list);
Namhyung Kimd74c8962011-12-13 00:16:52 +09002026out_free_dso_list:
2027 strlist__delete(symbol_conf.dso_list);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002028 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002029}
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002030
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002031void symbol__exit(void)
2032{
Jovi Zhang85e00b52010-09-09 13:30:59 -03002033 if (!symbol_conf.initialized)
2034 return;
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002035 strlist__delete(symbol_conf.sym_list);
2036 strlist__delete(symbol_conf.dso_list);
2037 strlist__delete(symbol_conf.comm_list);
2038 vmlinux_path__exit();
2039 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
Jovi Zhang85e00b52010-09-09 13:30:59 -03002040 symbol_conf.initialized = false;
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002041}
2042
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002043int machines__create_kernel_maps(struct rb_root *machines, pid_t pid)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002044{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002045 struct machine *machine = machines__findnew(machines, pid);
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02002046
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002047 if (machine == NULL)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002048 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002049
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002050 return machine__create_kernel_maps(machine);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002051}
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03002052
2053static int hex(char ch)
2054{
2055 if ((ch >= '0') && (ch <= '9'))
2056 return ch - '0';
2057 if ((ch >= 'a') && (ch <= 'f'))
2058 return ch - 'a' + 10;
2059 if ((ch >= 'A') && (ch <= 'F'))
2060 return ch - 'A' + 10;
2061 return -1;
2062}
2063
2064/*
2065 * While we find nice hex chars, build a long_val.
2066 * Return number of chars processed.
2067 */
2068int hex2u64(const char *ptr, u64 *long_val)
2069{
2070 const char *p = ptr;
2071 *long_val = 0;
2072
2073 while (*p) {
2074 const int hex_val = hex(*p);
2075
2076 if (hex_val < 0)
2077 break;
2078
2079 *long_val = (*long_val << 4) | hex_val;
2080 p++;
2081 }
2082
2083 return p - ptr;
2084}
2085
2086char *strxfrchar(char *s, char from, char to)
2087{
2088 char *p = s;
2089
2090 while ((p = strchr(p, from)) != NULL)
2091 *p++ = to;
2092
2093 return s;
2094}
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002095
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002096int machines__create_guest_kernel_maps(struct rb_root *machines)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002097{
2098 int ret = 0;
2099 struct dirent **namelist = NULL;
2100 int i, items = 0;
2101 char path[PATH_MAX];
2102 pid_t pid;
David Ahern347ed992012-07-29 20:54:35 -06002103 char *endp;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002104
2105 if (symbol_conf.default_guest_vmlinux_name ||
2106 symbol_conf.default_guest_modules ||
2107 symbol_conf.default_guest_kallsyms) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002108 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002109 }
2110
2111 if (symbol_conf.guestmount) {
2112 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2113 if (items <= 0)
2114 return -ENOENT;
2115 for (i = 0; i < items; i++) {
2116 if (!isdigit(namelist[i]->d_name[0])) {
2117 /* Filter out . and .. */
2118 continue;
2119 }
David Ahern347ed992012-07-29 20:54:35 -06002120 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
2121 if ((*endp != '\0') ||
2122 (endp == namelist[i]->d_name) ||
2123 (errno == ERANGE)) {
2124 pr_debug("invalid directory (%s). Skipping.\n",
2125 namelist[i]->d_name);
2126 continue;
2127 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002128 sprintf(path, "%s/%s/proc/kallsyms",
2129 symbol_conf.guestmount,
2130 namelist[i]->d_name);
2131 ret = access(path, R_OK);
2132 if (ret) {
2133 pr_debug("Can't access file %s\n", path);
2134 goto failure;
2135 }
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002136 machines__create_kernel_maps(machines, pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002137 }
2138failure:
2139 free(namelist);
2140 }
2141
2142 return ret;
2143}
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002144
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002145void machines__destroy_guest_kernel_maps(struct rb_root *machines)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -03002146{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002147 struct rb_node *next = rb_first(machines);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -03002148
2149 while (next) {
2150 struct machine *pos = rb_entry(next, struct machine, rb_node);
2151
2152 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002153 rb_erase(&pos->rb_node, machines);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -03002154 machine__delete(pos);
2155 }
2156}
2157
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002158int machine__load_kallsyms(struct machine *machine, const char *filename,
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002159 enum map_type type, symbol_filter_t filter)
2160{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002161 struct map *map = machine->vmlinux_maps[type];
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002162 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2163
2164 if (ret > 0) {
2165 dso__set_loaded(map->dso, type);
2166 /*
2167 * Since /proc/kallsyms will have multiple sessions for the
2168 * kernel, with modules between them, fixup the end of all
2169 * sections.
2170 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002171 __map_groups__fixup_end(&machine->kmaps, type);
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002172 }
2173
2174 return ret;
2175}
2176
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002177int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002178 symbol_filter_t filter)
2179{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002180 struct map *map = machine->vmlinux_maps[type];
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002181 int ret = dso__load_vmlinux_path(map->dso, map, filter);
2182
2183 if (ret > 0) {
2184 dso__set_loaded(map->dso, type);
2185 map__reloc_vmlinux(map);
2186 }
2187
2188 return ret;
2189}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302190
2191struct map *dso__new_map(const char *name)
2192{
Srikar Dronamraju378474e2012-05-31 17:16:56 +05302193 struct map *map = NULL;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302194 struct dso *dso = dso__new(name);
Srikar Dronamraju378474e2012-05-31 17:16:56 +05302195
2196 if (dso)
2197 map = map__new2(0, dso, MAP__FUNCTION);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302198
2199 return map;
2200}
Jiri Olsa949d1602012-07-22 14:14:33 +02002201
2202static int open_dso(struct dso *dso, struct machine *machine)
2203{
2204 char *root_dir = (char *) "";
2205 char *name;
2206 int fd;
2207
2208 name = malloc(PATH_MAX);
2209 if (!name)
2210 return -ENOMEM;
2211
2212 if (machine)
2213 root_dir = machine->root_dir;
2214
2215 if (dso__binary_type_file(dso, dso->data_type,
2216 root_dir, name, PATH_MAX)) {
2217 free(name);
2218 return -EINVAL;
2219 }
2220
2221 fd = open(name, O_RDONLY);
2222 free(name);
2223 return fd;
2224}
2225
2226int dso__data_fd(struct dso *dso, struct machine *machine)
2227{
2228 int i = 0;
2229
2230 if (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND)
2231 return open_dso(dso, machine);
2232
2233 do {
2234 int fd;
2235
2236 dso->data_type = binary_type_data[i++];
2237
2238 fd = open_dso(dso, machine);
2239 if (fd >= 0)
2240 return fd;
2241
2242 } while (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND);
2243
2244 return -EINVAL;
2245}
2246
Jiri Olsa4dff6242012-07-22 14:14:39 +02002247static void
2248dso_cache__free(struct rb_root *root)
Jiri Olsa949d1602012-07-22 14:14:33 +02002249{
Jiri Olsa4dff6242012-07-22 14:14:39 +02002250 struct rb_node *next = rb_first(root);
2251
2252 while (next) {
2253 struct dso_cache *cache;
2254
2255 cache = rb_entry(next, struct dso_cache, rb_node);
2256 next = rb_next(&cache->rb_node);
2257 rb_erase(&cache->rb_node, root);
2258 free(cache);
2259 }
Jiri Olsa949d1602012-07-22 14:14:33 +02002260}
2261
Jiri Olsa4dff6242012-07-22 14:14:39 +02002262static struct dso_cache*
2263dso_cache__find(struct rb_root *root, u64 offset)
Jiri Olsa949d1602012-07-22 14:14:33 +02002264{
Jiri Olsa4dff6242012-07-22 14:14:39 +02002265 struct rb_node **p = &root->rb_node;
2266 struct rb_node *parent = NULL;
2267 struct dso_cache *cache;
2268
2269 while (*p != NULL) {
2270 u64 end;
2271
2272 parent = *p;
2273 cache = rb_entry(parent, struct dso_cache, rb_node);
2274 end = cache->offset + DSO__DATA_CACHE_SIZE;
2275
2276 if (offset < cache->offset)
2277 p = &(*p)->rb_left;
2278 else if (offset >= end)
2279 p = &(*p)->rb_right;
2280 else
2281 return cache;
2282 }
2283 return NULL;
Jiri Olsa949d1602012-07-22 14:14:33 +02002284}
2285
Jiri Olsa4dff6242012-07-22 14:14:39 +02002286static void
2287dso_cache__insert(struct rb_root *root, struct dso_cache *new)
Jiri Olsa949d1602012-07-22 14:14:33 +02002288{
Jiri Olsa4dff6242012-07-22 14:14:39 +02002289 struct rb_node **p = &root->rb_node;
2290 struct rb_node *parent = NULL;
2291 struct dso_cache *cache;
2292 u64 offset = new->offset;
2293
2294 while (*p != NULL) {
2295 u64 end;
2296
2297 parent = *p;
2298 cache = rb_entry(parent, struct dso_cache, rb_node);
2299 end = cache->offset + DSO__DATA_CACHE_SIZE;
2300
2301 if (offset < cache->offset)
2302 p = &(*p)->rb_left;
2303 else if (offset >= end)
2304 p = &(*p)->rb_right;
2305 }
2306
2307 rb_link_node(&new->rb_node, parent, p);
2308 rb_insert_color(&new->rb_node, root);
2309}
2310
2311static ssize_t
2312dso_cache__memcpy(struct dso_cache *cache, u64 offset,
2313 u8 *data, u64 size)
2314{
2315 u64 cache_offset = offset - cache->offset;
2316 u64 cache_size = min(cache->size - cache_offset, size);
2317
2318 memcpy(data, cache->data + cache_offset, cache_size);
2319 return cache_size;
2320}
2321
2322static ssize_t
2323dso_cache__read(struct dso *dso, struct machine *machine,
2324 u64 offset, u8 *data, ssize_t size)
2325{
2326 struct dso_cache *cache;
2327 ssize_t ret;
Jiri Olsa949d1602012-07-22 14:14:33 +02002328 int fd;
2329
2330 fd = dso__data_fd(dso, machine);
2331 if (fd < 0)
2332 return -1;
2333
2334 do {
Jiri Olsa4dff6242012-07-22 14:14:39 +02002335 u64 cache_offset;
2336
2337 ret = -ENOMEM;
2338
2339 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
2340 if (!cache)
Jiri Olsa949d1602012-07-22 14:14:33 +02002341 break;
2342
Jiri Olsa4dff6242012-07-22 14:14:39 +02002343 cache_offset = offset & DSO__DATA_CACHE_MASK;
2344 ret = -EINVAL;
2345
2346 if (-1 == lseek(fd, cache_offset, SEEK_SET))
Jiri Olsa949d1602012-07-22 14:14:33 +02002347 break;
2348
Jiri Olsa4dff6242012-07-22 14:14:39 +02002349 ret = read(fd, cache->data, DSO__DATA_CACHE_SIZE);
2350 if (ret <= 0)
2351 break;
2352
2353 cache->offset = cache_offset;
2354 cache->size = ret;
2355 dso_cache__insert(&dso->cache, cache);
2356
2357 ret = dso_cache__memcpy(cache, offset, data, size);
Jiri Olsa949d1602012-07-22 14:14:33 +02002358
2359 } while (0);
2360
Jiri Olsa4dff6242012-07-22 14:14:39 +02002361 if (ret <= 0)
2362 free(cache);
2363
Jiri Olsa949d1602012-07-22 14:14:33 +02002364 close(fd);
Jiri Olsa4dff6242012-07-22 14:14:39 +02002365 return ret;
2366}
2367
2368static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
2369 u64 offset, u8 *data, ssize_t size)
2370{
2371 struct dso_cache *cache;
2372
2373 cache = dso_cache__find(&dso->cache, offset);
2374 if (cache)
2375 return dso_cache__memcpy(cache, offset, data, size);
2376 else
2377 return dso_cache__read(dso, machine, offset, data, size);
Jiri Olsa949d1602012-07-22 14:14:33 +02002378}
2379
2380ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
2381 u64 offset, u8 *data, ssize_t size)
2382{
Jiri Olsa4dff6242012-07-22 14:14:39 +02002383 ssize_t r = 0;
2384 u8 *p = data;
2385
2386 do {
2387 ssize_t ret;
2388
2389 ret = dso_cache_read(dso, machine, offset, p, size);
2390 if (ret < 0)
2391 return ret;
2392
2393 /* Reached EOF, return what we have. */
2394 if (!ret)
2395 break;
2396
2397 BUG_ON(ret > size);
2398
2399 r += ret;
2400 p += ret;
2401 offset += ret;
2402 size -= ret;
2403
2404 } while (size);
2405
2406 return r;
Jiri Olsa949d1602012-07-22 14:14:33 +02002407}
2408
2409ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
2410 struct machine *machine, u64 addr,
2411 u8 *data, ssize_t size)
2412{
2413 u64 offset = map->map_ip(map, addr);
2414 return dso__data_read_offset(dso, machine, offset, data, size);
2415}