~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/util/map.c

Version: ~ [ linux-5.3-rc5 ] ~ [ linux-5.2.9 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.67 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.139 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.189 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.189 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.72 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 #include "symbol.h"
  2 #include <errno.h>
  3 #include <inttypes.h>
  4 #include <limits.h>
  5 #include <stdlib.h>
  6 #include <string.h>
  7 #include <stdio.h>
  8 #include <unistd.h>
  9 #include "map.h"
 10 
 11 const char *map_type__name[MAP__NR_TYPES] = {
 12         [MAP__FUNCTION] = "Functions",
 13         [MAP__VARIABLE] = "Variables",
 14 };
 15 
 16 static inline int is_anon_memory(const char *filename)
 17 {
 18         return !strcmp(filename, "//anon") ||
 19                !strcmp(filename, "/dev/zero (deleted)") ||
 20                !strcmp(filename, "/anon_hugepage (deleted)");
 21 }
 22 
 23 static inline int is_no_dso_memory(const char *filename)
 24 {
 25         return !strcmp(filename, "[stack]") ||
 26                !strcmp(filename, "[vdso]")  ||
 27                !strcmp(filename, "[heap]");
 28 }
 29 
 30 void map__init(struct map *self, enum map_type type,
 31                u64 start, u64 end, u64 pgoff, struct dso *dso)
 32 {
 33         self->type     = type;
 34         self->start    = start;
 35         self->end      = end;
 36         self->pgoff    = pgoff;
 37         self->dso      = dso;
 38         self->map_ip   = map__map_ip;
 39         self->unmap_ip = map__unmap_ip;
 40         RB_CLEAR_NODE(&self->rb_node);
 41         self->groups   = NULL;
 42         self->referenced = false;
 43 }
 44 
 45 struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
 46                      u64 pgoff, u32 pid, char *filename,
 47                      enum map_type type)
 48 {
 49         struct map *self = malloc(sizeof(*self));
 50 
 51         if (self != NULL) {
 52                 char newfilename[PATH_MAX];
 53                 struct dso *dso;
 54                 int anon, no_dso;
 55 
 56                 anon = is_anon_memory(filename);
 57                 no_dso = is_no_dso_memory(filename);
 58 
 59                 if (anon) {
 60                         snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
 61                         filename = newfilename;
 62                 }
 63 
 64                 dso = __dsos__findnew(dsos__list, filename);
 65                 if (dso == NULL)
 66                         goto out_delete;
 67 
 68                 map__init(self, type, start, start + len, pgoff, dso);
 69 
 70                 if (anon || no_dso) {
 71                         self->map_ip = self->unmap_ip = identity__map_ip;
 72 
 73                         /*
 74                          * Set memory without DSO as loaded. All map__find_*
 75                          * functions still return NULL, and we avoid the
 76                          * unnecessary map__load warning.
 77                          */
 78                         if (no_dso)
 79                                 dso__set_loaded(dso, self->type);
 80                 }
 81         }
 82         return self;
 83 out_delete:
 84         free(self);
 85         return NULL;
 86 }
 87 
 88 void map__delete(struct map *self)
 89 {
 90         free(self);
 91 }
 92 
 93 void map__fixup_start(struct map *self)
 94 {
 95         struct rb_root *symbols = &self->dso->symbols[self->type];
 96         struct rb_node *nd = rb_first(symbols);
 97         if (nd != NULL) {
 98                 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
 99                 self->start = sym->start;
100         }
101 }
102 
103 void map__fixup_end(struct map *self)
104 {
105         struct rb_root *symbols = &self->dso->symbols[self->type];
106         struct rb_node *nd = rb_last(symbols);
107         if (nd != NULL) {
108                 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
109                 self->end = sym->end;
110         }
111 }
112 
113 #define DSO__DELETED "(deleted)"
114 
115 int map__load(struct map *self, symbol_filter_t filter)
116 {
117         const char *name = self->dso->long_name;
118         int nr;
119 
120         if (dso__loaded(self->dso, self->type))
121                 return 0;
122 
123         nr = dso__load(self->dso, self, filter);
124         if (nr < 0) {
125                 if (self->dso->has_build_id) {
126                         char sbuild_id[BUILD_ID_SIZE * 2 + 1];
127 
128                         build_id__sprintf(self->dso->build_id,
129                                           sizeof(self->dso->build_id),
130                                           sbuild_id);
131                         pr_warning("%s with build id %s not found",
132                                    name, sbuild_id);
133                 } else
134                         pr_warning("Failed to open %s", name);
135 
136                 pr_warning(", continuing without symbols\n");
137                 return -1;
138         } else if (nr == 0) {
139                 const size_t len = strlen(name);
140                 const size_t real_len = len - sizeof(DSO__DELETED);
141 
142                 if (len > sizeof(DSO__DELETED) &&
143                     strcmp(name + real_len + 1, DSO__DELETED) == 0) {
144                         pr_warning("%.*s was updated (is prelink enabled?). "
145                                 "Restart the long running apps that use it!\n",
146                                    (int)real_len, name);
147                 } else {
148                         pr_warning("no symbols found in %s, maybe install "
149                                    "a debug package?\n", name);
150                 }
151 
152                 return -1;
153         }
154         /*
155          * Only applies to the kernel, as its symtabs aren't relative like the
156          * module ones.
157          */
158         if (self->dso->kernel)
159                 map__reloc_vmlinux(self);
160 
161         return 0;
162 }
163 
164 struct symbol *map__find_symbol(struct map *self, u64 addr,
165                                 symbol_filter_t filter)
166 {
167         if (map__load(self, filter) < 0)
168                 return NULL;
169 
170         return dso__find_symbol(self->dso, self->type, addr);
171 }
172 
173 struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
174                                         symbol_filter_t filter)
175 {
176         if (map__load(self, filter) < 0)
177                 return NULL;
178 
179         if (!dso__sorted_by_name(self->dso, self->type))
180                 dso__sort_by_name(self->dso, self->type);
181 
182         return dso__find_symbol_by_name(self->dso, self->type, name);
183 }
184 
185 struct map *map__clone(struct map *self)
186 {
187         struct map *map = malloc(sizeof(*self));
188 
189         if (!map)
190                 return NULL;
191 
192         memcpy(map, self, sizeof(*self));
193 
194         return map;
195 }
196 
197 int map__overlap(struct map *l, struct map *r)
198 {
199         if (l->start > r->start) {
200                 struct map *t = l;
201                 l = r;
202                 r = t;
203         }
204 
205         if (l->end > r->start)
206                 return 1;
207 
208         return 0;
209 }
210 
211 size_t map__fprintf(struct map *self, FILE *fp)
212 {
213         return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
214                        self->start, self->end, self->pgoff, self->dso->name);
215 }
216 
217 /*
218  * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
219  * map->dso->adjust_symbols==1 for ET_EXEC-like cases.
220  */
221 u64 map__rip_2objdump(struct map *map, u64 rip)
222 {
223         u64 addr = map->dso->adjust_symbols ?
224                         map->unmap_ip(map, rip) :       /* RIP -> IP */
225                         rip;
226         return addr;
227 }
228 
229 u64 map__objdump_2ip(struct map *map, u64 addr)
230 {
231         u64 ip = map->dso->adjust_symbols ?
232                         addr :
233                         map->unmap_ip(map, addr);       /* RIP -> IP */
234         return ip;
235 }
236 
237 void map_groups__init(struct map_groups *mg)
238 {
239         int i;
240         for (i = 0; i < MAP__NR_TYPES; ++i) {
241                 mg->maps[i] = RB_ROOT;
242                 INIT_LIST_HEAD(&mg->removed_maps[i]);
243         }
244         mg->machine = NULL;
245 }
246 
247 static void maps__delete(struct rb_root *maps)
248 {
249         struct rb_node *next = rb_first(maps);
250 
251         while (next) {
252                 struct map *pos = rb_entry(next, struct map, rb_node);
253 
254                 next = rb_next(&pos->rb_node);
255                 rb_erase(&pos->rb_node, maps);
256                 map__delete(pos);
257         }
258 }
259 
260 static void maps__delete_removed(struct list_head *maps)
261 {
262         struct map *pos, *n;
263 
264         list_for_each_entry_safe(pos, n, maps, node) {
265                 list_del(&pos->node);
266                 map__delete(pos);
267         }
268 }
269 
270 void map_groups__exit(struct map_groups *mg)
271 {
272         int i;
273 
274         for (i = 0; i < MAP__NR_TYPES; ++i) {
275                 maps__delete(&mg->maps[i]);
276                 maps__delete_removed(&mg->removed_maps[i]);
277         }
278 }
279 
280 void map_groups__flush(struct map_groups *mg)
281 {
282         int type;
283 
284         for (type = 0; type < MAP__NR_TYPES; type++) {
285                 struct rb_root *root = &mg->maps[type];
286                 struct rb_node *next = rb_first(root);
287 
288                 while (next) {
289                         struct map *pos = rb_entry(next, struct map, rb_node);
290                         next = rb_next(&pos->rb_node);
291                         rb_erase(&pos->rb_node, root);
292                         /*
293                          * We may have references to this map, for
294                          * instance in some hist_entry instances, so
295                          * just move them to a separate list.
296                          */
297                         list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
298                 }
299         }
300 }
301 
302 struct symbol *map_groups__find_symbol(struct map_groups *mg,
303                                        enum map_type type, u64 addr,
304                                        struct map **mapp,
305                                        symbol_filter_t filter)
306 {
307         struct map *map = map_groups__find(mg, type, addr);
308 
309         if (map != NULL) {
310                 if (mapp != NULL)
311                         *mapp = map;
312                 return map__find_symbol(map, map->map_ip(map, addr), filter);
313         }
314 
315         return NULL;
316 }
317 
318 struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
319                                                enum map_type type,
320                                                const char *name,
321                                                struct map **mapp,
322                                                symbol_filter_t filter)
323 {
324         struct rb_node *nd;
325 
326         for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
327                 struct map *pos = rb_entry(nd, struct map, rb_node);
328                 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
329 
330                 if (sym == NULL)
331                         continue;
332                 if (mapp != NULL)
333                         *mapp = pos;
334                 return sym;
335         }
336 
337         return NULL;
338 }
339 
340 size_t __map_groups__fprintf_maps(struct map_groups *mg,
341                                   enum map_type type, int verbose, FILE *fp)
342 {
343         size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
344         struct rb_node *nd;
345 
346         for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
347                 struct map *pos = rb_entry(nd, struct map, rb_node);
348                 printed += fprintf(fp, "Map:");
349                 printed += map__fprintf(pos, fp);
350                 if (verbose > 2) {
351                         printed += dso__fprintf(pos->dso, type, fp);
352                         printed += fprintf(fp, "--\n");
353                 }
354         }
355 
356         return printed;
357 }
358 
359 size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
360 {
361         size_t printed = 0, i;
362         for (i = 0; i < MAP__NR_TYPES; ++i)
363                 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
364         return printed;
365 }
366 
367 static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
368                                                  enum map_type type,
369                                                  int verbose, FILE *fp)
370 {
371         struct map *pos;
372         size_t printed = 0;
373 
374         list_for_each_entry(pos, &mg->removed_maps[type], node) {
375                 printed += fprintf(fp, "Map:");
376                 printed += map__fprintf(pos, fp);
377                 if (verbose > 1) {
378                         printed += dso__fprintf(pos->dso, type, fp);
379                         printed += fprintf(fp, "--\n");
380                 }
381         }
382         return printed;
383 }
384 
385 static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
386                                                int verbose, FILE *fp)
387 {
388         size_t printed = 0, i;
389         for (i = 0; i < MAP__NR_TYPES; ++i)
390                 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
391         return printed;
392 }
393 
394 size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
395 {
396         size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
397         printed += fprintf(fp, "Removed maps:\n");
398         return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
399 }
400 
401 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
402                                    int verbose, FILE *fp)
403 {
404         struct rb_root *root = &mg->maps[map->type];
405         struct rb_node *next = rb_first(root);
406         int err = 0;
407 
408         while (next) {
409                 struct map *pos = rb_entry(next, struct map, rb_node);
410                 next = rb_next(&pos->rb_node);
411 
412                 if (!map__overlap(pos, map))
413                         continue;
414 
415                 if (verbose >= 2) {
416                         fputs("overlapping maps:\n", fp);
417                         map__fprintf(map, fp);
418                         map__fprintf(pos, fp);
419                 }
420 
421                 rb_erase(&pos->rb_node, root);
422                 /*
423                  * Now check if we need to create new maps for areas not
424                  * overlapped by the new map:
425                  */
426                 if (map->start > pos->start) {
427                         struct map *before = map__clone(pos);
428 
429                         if (before == NULL) {
430                                 err = -ENOMEM;
431                                 goto move_map;
432                         }
433 
434                         before->end = map->start - 1;
435                         map_groups__insert(mg, before);
436                         if (verbose >= 2)
437                                 map__fprintf(before, fp);
438                 }
439 
440                 if (map->end < pos->end) {
441                         struct map *after = map__clone(pos);
442 
443                         if (after == NULL) {
444                                 err = -ENOMEM;
445                                 goto move_map;
446                         }
447 
448                         after->start = map->end + 1;
449                         map_groups__insert(mg, after);
450                         if (verbose >= 2)
451                                 map__fprintf(after, fp);
452                 }
453 move_map:
454                 /*
455                  * If we have references, just move them to a separate list.
456                  */
457                 if (pos->referenced)
458                         list_add_tail(&pos->node, &mg->removed_maps[map->type]);
459                 else
460                         map__delete(pos);
461 
462                 if (err)
463                         return err;
464         }
465 
466         return 0;
467 }
468 
469 /*
470  * XXX This should not really _copy_ te maps, but refcount them.
471  */
472 int map_groups__clone(struct map_groups *mg,
473                       struct map_groups *parent, enum map_type type)
474 {
475         struct rb_node *nd;
476         for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
477                 struct map *map = rb_entry(nd, struct map, rb_node);
478                 struct map *new = map__clone(map);
479                 if (new == NULL)
480                         return -ENOMEM;
481                 map_groups__insert(mg, new);
482         }
483         return 0;
484 }
485 
486 static u64 map__reloc_map_ip(struct map *map, u64 ip)
487 {
488         return ip + (s64)map->pgoff;
489 }
490 
491 static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
492 {
493         return ip - (s64)map->pgoff;
494 }
495 
496 void map__reloc_vmlinux(struct map *self)
497 {
498         struct kmap *kmap = map__kmap(self);
499         s64 reloc;
500 
501         if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr)
502                 return;
503 
504         reloc = (kmap->ref_reloc_sym->unrelocated_addr -
505                  kmap->ref_reloc_sym->addr);
506 
507         if (!reloc)
508                 return;
509 
510         self->map_ip   = map__reloc_map_ip;
511         self->unmap_ip = map__reloc_unmap_ip;
512         self->pgoff    = reloc;
513 }
514 
515 void maps__insert(struct rb_root *maps, struct map *map)
516 {
517         struct rb_node **p = &maps->rb_node;
518         struct rb_node *parent = NULL;
519         const u64 ip = map->start;
520         struct map *m;
521 
522         while (*p != NULL) {
523                 parent = *p;
524                 m = rb_entry(parent, struct map, rb_node);
525                 if (ip < m->start)
526                         p = &(*p)->rb_left;
527                 else
528                         p = &(*p)->rb_right;
529         }
530 
531         rb_link_node(&map->rb_node, parent, p);
532         rb_insert_color(&map->rb_node, maps);
533 }
534 
535 void maps__remove(struct rb_root *self, struct map *map)
536 {
537         rb_erase(&map->rb_node, self);
538 }
539 
540 struct map *maps__find(struct rb_root *maps, u64 ip)
541 {
542         struct rb_node **p = &maps->rb_node;
543         struct rb_node *parent = NULL;
544         struct map *m;
545 
546         while (*p != NULL) {
547                 parent = *p;
548                 m = rb_entry(parent, struct map, rb_node);
549                 if (ip < m->start)
550                         p = &(*p)->rb_left;
551                 else if (ip > m->end)
552                         p = &(*p)->rb_right;
553                 else
554                         return m;
555         }
556 
557         return NULL;
558 }
559 
560 int machine__init(struct machine *self, const char *root_dir, pid_t pid)
561 {
562         map_groups__init(&self->kmaps);
563         RB_CLEAR_NODE(&self->rb_node);
564         INIT_LIST_HEAD(&self->user_dsos);
565         INIT_LIST_HEAD(&self->kernel_dsos);
566 
567         self->kmaps.machine = self;
568         self->pid           = pid;
569         self->root_dir      = strdup(root_dir);
570         return self->root_dir == NULL ? -ENOMEM : 0;
571 }
572 
573 static void dsos__delete(struct list_head *self)
574 {
575         struct dso *pos, *n;
576 
577         list_for_each_entry_safe(pos, n, self, node) {
578                 list_del(&pos->node);
579                 dso__delete(pos);
580         }
581 }
582 
583 void machine__exit(struct machine *self)
584 {
585         map_groups__exit(&self->kmaps);
586         dsos__delete(&self->user_dsos);
587         dsos__delete(&self->kernel_dsos);
588         free(self->root_dir);
589         self->root_dir = NULL;
590 }
591 
592 void machine__delete(struct machine *self)
593 {
594         machine__exit(self);
595         free(self);
596 }
597 
598 struct machine *machines__add(struct rb_root *self, pid_t pid,
599                               const char *root_dir)
600 {
601         struct rb_node **p = &self->rb_node;
602         struct rb_node *parent = NULL;
603         struct machine *pos, *machine = malloc(sizeof(*machine));
604 
605         if (!machine)
606                 return NULL;
607 
608         if (machine__init(machine, root_dir, pid) != 0) {
609                 free(machine);
610                 return NULL;
611         }
612 
613         while (*p != NULL) {
614                 parent = *p;
615                 pos = rb_entry(parent, struct machine, rb_node);
616                 if (pid < pos->pid)
617                         p = &(*p)->rb_left;
618                 else
619                         p = &(*p)->rb_right;
620         }
621 
622         rb_link_node(&machine->rb_node, parent, p);
623         rb_insert_color(&machine->rb_node, self);
624 
625         return machine;
626 }
627 
628 struct machine *machines__find(struct rb_root *self, pid_t pid)
629 {
630         struct rb_node **p = &self->rb_node;
631         struct rb_node *parent = NULL;
632         struct machine *machine;
633         struct machine *default_machine = NULL;
634 
635         while (*p != NULL) {
636                 parent = *p;
637                 machine = rb_entry(parent, struct machine, rb_node);
638                 if (pid < machine->pid)
639                         p = &(*p)->rb_left;
640                 else if (pid > machine->pid)
641                         p = &(*p)->rb_right;
642                 else
643                         return machine;
644                 if (!machine->pid)
645                         default_machine = machine;
646         }
647 
648         return default_machine;
649 }
650 
651 struct machine *machines__findnew(struct rb_root *self, pid_t pid)
652 {
653         char path[PATH_MAX];
654         const char *root_dir;
655         struct machine *machine = machines__find(self, pid);
656 
657         if (!machine || machine->pid != pid) {
658                 if (pid == HOST_KERNEL_ID || pid == DEFAULT_GUEST_KERNEL_ID)
659                         root_dir = "";
660                 else {
661                         if (!symbol_conf.guestmount)
662                                 goto out;
663                         sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
664                         if (access(path, R_OK)) {
665                                 pr_err("Can't access file %s\n", path);
666                                 goto out;
667                         }
668                         root_dir = path;
669                 }
670                 machine = machines__add(self, pid, root_dir);
671         }
672 
673 out:
674         return machine;
675 }
676 
677 void machines__process(struct rb_root *self, machine__process_t process, void *data)
678 {
679         struct rb_node *nd;
680 
681         for (nd = rb_first(self); nd; nd = rb_next(nd)) {
682                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
683                 process(pos, data);
684         }
685 }
686 
687 char *machine__mmap_name(struct machine *self, char *bf, size_t size)
688 {
689         if (machine__is_host(self))
690                 snprintf(bf, size, "[%s]", "kernel.kallsyms");
691         else if (machine__is_default_guest(self))
692                 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
693         else
694                 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms", self->pid);
695 
696         return bf;
697 }
698 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp