blob: 10a44e946f7734b911ed00f74184f754b09d56ba [file] [log] [blame]
Naveen N. Raod2332092015-04-28 17:35:35 +05301/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * Copyright (C) 2015 Naveen N. Rao, IBM Corporation
7 */
8
9#include "debug.h"
10#include "symbol.h"
Naveen N. Rao031b84c2015-04-28 17:35:37 +053011#include "map.h"
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +053012#include "probe-event.h"
Naveen N. Rao44ca9342017-03-08 13:56:10 +053013#include "probe-file.h"
Naveen N. Raod2332092015-04-28 17:35:35 +053014
15#ifdef HAVE_LIBELF_SUPPORT
16bool elf__needs_adjust_symbols(GElf_Ehdr ehdr)
17{
18 return ehdr.e_type == ET_EXEC ||
19 ehdr.e_type == ET_REL ||
20 ehdr.e_type == ET_DYN;
21}
Ananth N Mavinakayanahallic50fc0a2015-04-28 17:35:38 +053022
Naveen N. Raod2332092015-04-28 17:35:35 +053023#endif
Naveen N. Raofb6d5942015-04-28 17:35:36 +053024
Naveen N. Raofb6d5942015-04-28 17:35:36 +053025int arch__choose_best_symbol(struct symbol *syma,
26 struct symbol *symb __maybe_unused)
27{
28 char *sym = syma->name;
29
Sandipan Dasfa694162018-08-28 14:38:48 +053030#if !defined(_CALL_ELF) || _CALL_ELF != 2
Naveen N. Raofb6d5942015-04-28 17:35:36 +053031 /* Skip over any initial dot */
32 if (*sym == '.')
33 sym++;
Sandipan Dasfa694162018-08-28 14:38:48 +053034#endif
Naveen N. Raofb6d5942015-04-28 17:35:36 +053035
36 /* Avoid "SyS" kernel syscall aliases */
37 if (strlen(sym) >= 3 && !strncmp(sym, "SyS", 3))
38 return SYMBOL_B;
39 if (strlen(sym) >= 10 && !strncmp(sym, "compat_SyS", 10))
40 return SYMBOL_B;
41
42 return SYMBOL_A;
43}
Naveen N. Rao031b84c2015-04-28 17:35:37 +053044
Sandipan Dasfa694162018-08-28 14:38:48 +053045#if !defined(_CALL_ELF) || _CALL_ELF != 2
Naveen N. Rao031b84c2015-04-28 17:35:37 +053046/* Allow matching against dot variants */
47int arch__compare_symbol_names(const char *namea, const char *nameb)
48{
49 /* Skip over initial dot */
50 if (*namea == '.')
51 namea++;
52 if (*nameb == '.')
53 nameb++;
54
55 return strcmp(namea, nameb);
56}
Paul Clarked80406452017-04-25 13:15:49 -050057
58int arch__compare_symbol_names_n(const char *namea, const char *nameb,
59 unsigned int n)
60{
61 /* Skip over initial dot */
62 if (*namea == '.')
63 namea++;
64 if (*nameb == '.')
65 nameb++;
66
67 return strncmp(namea, nameb, n);
68}
Masami Hiramatsu4b3a2712017-12-09 01:28:12 +090069
70const char *arch__normalize_symbol_name(const char *name)
71{
72 /* Skip over initial dot */
73 if (name && *name == '.')
74 name++;
75 return name;
76}
Naveen N. Raofb6d5942015-04-28 17:35:36 +053077#endif
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +053078
79#if defined(_CALL_ELF) && _CALL_ELF == 2
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +053080
Naveen N. Rao0b3c2262016-04-12 14:40:50 +053081#ifdef HAVE_LIBELF_SUPPORT
82void arch__sym_update(struct symbol *s, GElf_Sym *sym)
83{
84 s->arch_sym = sym->st_other;
85}
86#endif
87
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +053088#define PPC64LE_LEP_OFFSET 8
89
90void arch__fix_tev_from_maps(struct perf_probe_event *pev,
Naveen N. Rao0b3c2262016-04-12 14:40:50 +053091 struct probe_trace_event *tev, struct map *map,
92 struct symbol *sym)
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +053093{
Naveen N. Rao0b3c2262016-04-12 14:40:50 +053094 int lep_offset;
95
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +053096 /*
Naveen N. Rao239aeba2016-04-12 14:40:49 +053097 * When probing at a function entry point, we normally always want the
98 * LEP since that catches calls to the function through both the GEP and
99 * the LEP. Hence, we would like to probe at an offset of 8 bytes if
100 * the user only specified the function entry.
101 *
102 * However, if the user specifies an offset, we fall back to using the
103 * GEP since all userspace applications (objdump/readelf) show function
104 * disassembly with offsets from the GEP.
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +0530105 */
Naveen N. Rao44ca9342017-03-08 13:56:10 +0530106 if (pev->point.offset || !map || !sym)
Naveen N. Rao239aeba2016-04-12 14:40:49 +0530107 return;
108
Naveen N. Rao44ca9342017-03-08 13:56:10 +0530109 /* For kretprobes, add an offset only if the kernel supports it */
110 if (!pev->uprobes && pev->point.retprobe) {
111#ifdef HAVE_LIBELF_SUPPORT
112 if (!kretprobe_offset_is_supported())
113#endif
114 return;
115 }
116
Naveen N. Rao0b3c2262016-04-12 14:40:50 +0530117 lep_offset = PPC64_LOCAL_ENTRY_OFFSET(sym->arch_sym);
118
Naveen N. Rao239aeba2016-04-12 14:40:49 +0530119 if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS)
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +0530120 tev->point.offset += PPC64LE_LEP_OFFSET;
Naveen N. Rao0b3c2262016-04-12 14:40:50 +0530121 else if (lep_offset) {
122 if (pev->uprobes)
123 tev->point.address += lep_offset;
124 else
125 tev->point.offset += lep_offset;
126 }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +0530127}
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500128
Ravi Bangoriaf046f3d2016-08-11 14:43:59 -0300129#ifdef HAVE_LIBELF_SUPPORT
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500130void arch__post_process_probe_trace_events(struct perf_probe_event *pev,
131 int ntevs)
132{
133 struct probe_trace_event *tev;
134 struct map *map;
135 struct symbol *sym = NULL;
136 struct rb_node *tmp;
137 int i = 0;
138
Krister Johansen544abd42017-07-05 18:48:10 -0700139 map = get_target_map(pev->target, pev->nsi, pev->uprobes);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300140 if (!map || map__load(map) < 0)
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500141 return;
142
143 for (i = 0; i < ntevs; i++) {
144 tev = &pev->tevs[i];
145 map__for_each_symbol(map, sym, tmp) {
Sandipan Das354b0642018-08-09 21:49:29 +0530146 if (map->unmap_ip(map, sym->start) == tev->point.address) {
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500147 arch__fix_tev_from_maps(pev, tev, map, sym);
Sandipan Das354b0642018-08-09 21:49:29 +0530148 break;
149 }
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500150 }
151 }
152}
Ravi Bangoriaf046f3d2016-08-11 14:43:59 -0300153#endif /* HAVE_LIBELF_SUPPORT */
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500154
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +0530155#endif