1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <assert.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <poll.h>
9 #include <unistd.h>
10 #include <linux/perf_event.h>
11 #include <sys/mman.h>
12 #include "trace_helpers.h"
13 
14 #define DEBUGFS "/sys/kernel/debug/tracing/"
15 
16 #define MAX_SYMS 300000
17 static struct ksym syms[MAX_SYMS];
18 static int sym_cnt;
19 
20 static int ksym_cmp(const void *p1, const void *p2)
21 {
22 	return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
23 }
24 
25 int load_kallsyms(void)
26 {
27 	FILE *f = fopen("/proc/kallsyms", "r");
28 	char func[256], buf[256];
29 	char symbol;
30 	void *addr;
31 	int i = 0;
32 
33 	if (!f)
34 		return -ENOENT;
35 
36 	while (fgets(buf, sizeof(buf), f)) {
37 		if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
38 			break;
39 		if (!addr)
40 			continue;
41 		syms[i].addr = (long) addr;
42 		syms[i].name = strdup(func);
43 		i++;
44 	}
45 	fclose(f);
46 	sym_cnt = i;
47 	qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
48 	return 0;
49 }
50 
51 struct ksym *ksym_search(long key)
52 {
53 	int start = 0, end = sym_cnt;
54 	int result;
55 
56 	/* kallsyms not loaded. return NULL */
57 	if (sym_cnt <= 0)
58 		return NULL;
59 
60 	while (start < end) {
61 		size_t mid = start + (end - start) / 2;
62 
63 		result = key - syms[mid].addr;
64 		if (result < 0)
65 			end = mid;
66 		else if (result > 0)
67 			start = mid + 1;
68 		else
69 			return &syms[mid];
70 	}
71 
72 	if (start >= 1 && syms[start - 1].addr < key &&
73 	    key < syms[start].addr)
74 		/* valid ksym */
75 		return &syms[start - 1];
76 
77 	/* out of range. return _stext */
78 	return &syms[0];
79 }
80 
81 long ksym_get_addr(const char *name)
82 {
83 	int i;
84 
85 	for (i = 0; i < sym_cnt; i++) {
86 		if (strcmp(syms[i].name, name) == 0)
87 			return syms[i].addr;
88 	}
89 
90 	return 0;
91 }
92 
93 /* open kallsyms and read symbol addresses on the fly. Without caching all symbols,
94  * this is faster than load + find.
95  */
96 int kallsyms_find(const char *sym, unsigned long long *addr)
97 {
98 	char type, name[500];
99 	unsigned long long value;
100 	int err = 0;
101 	FILE *f;
102 
103 	f = fopen("/proc/kallsyms", "r");
104 	if (!f)
105 		return -EINVAL;
106 
107 	while (fscanf(f, "%llx %c %499s%*[^\n]\n", &value, &type, name) > 0) {
108 		if (strcmp(name, sym) == 0) {
109 			*addr = value;
110 			goto out;
111 		}
112 	}
113 	err = -ENOENT;
114 
115 out:
116 	fclose(f);
117 	return err;
118 }
119 
120 void read_trace_pipe(void)
121 {
122 	int trace_fd;
123 
124 	trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
125 	if (trace_fd < 0)
126 		return;
127 
128 	while (1) {
129 		static char buf[4096];
130 		ssize_t sz;
131 
132 		sz = read(trace_fd, buf, sizeof(buf) - 1);
133 		if (sz > 0) {
134 			buf[sz] = 0;
135 			puts(buf);
136 		}
137 	}
138 }
139 
140 #if defined(__powerpc64__) && defined(_CALL_ELF) && _CALL_ELF == 2
141 
142 #define OP_RT_RA_MASK   0xffff0000UL
143 #define LIS_R2          0x3c400000UL
144 #define ADDIS_R2_R12    0x3c4c0000UL
145 #define ADDI_R2_R2      0x38420000UL
146 
147 ssize_t get_uprobe_offset(const void *addr, ssize_t base)
148 {
149 	u32 *insn = (u32 *)(uintptr_t)addr;
150 
151 	/*
152 	 * A PPC64 ABIv2 function may have a local and a global entry
153 	 * point. We need to use the local entry point when patching
154 	 * functions, so identify and step over the global entry point
155 	 * sequence.
156 	 *
157 	 * The global entry point sequence is always of the form:
158 	 *
159 	 * addis r2,r12,XXXX
160 	 * addi  r2,r2,XXXX
161 	 *
162 	 * A linker optimisation may convert the addis to lis:
163 	 *
164 	 * lis   r2,XXXX
165 	 * addi  r2,r2,XXXX
166 	 */
167 	if ((((*insn & OP_RT_RA_MASK) == ADDIS_R2_R12) ||
168 	     ((*insn & OP_RT_RA_MASK) == LIS_R2)) &&
169 	    ((*(insn + 1) & OP_RT_RA_MASK) == ADDI_R2_R2))
170 		return (ssize_t)(insn + 2) - base;
171 	else
172 		return (uintptr_t)addr - base;
173 }
174 
175 #else
176 
177 ssize_t get_uprobe_offset(const void *addr, ssize_t base)
178 {
179 	return (uintptr_t)addr - base;
180 }
181 
182 #endif
183 
184 ssize_t get_base_addr(void)
185 {
186 	size_t start, offset;
187 	char buf[256];
188 	FILE *f;
189 
190 	f = fopen("/proc/self/maps", "r");
191 	if (!f)
192 		return -errno;
193 
194 	while (fscanf(f, "%zx-%*x %s %zx %*[^\n]\n",
195 		      &start, buf, &offset) == 3) {
196 		if (strcmp(buf, "r-xp") == 0) {
197 			fclose(f);
198 			return start - offset;
199 		}
200 	}
201 
202 	fclose(f);
203 	return -EINVAL;
204 }
205 
206 ssize_t get_rel_offset(uintptr_t addr)
207 {
208 	size_t start, end, offset;
209 	char buf[256];
210 	FILE *f;
211 
212 	f = fopen("/proc/self/maps", "r");
213 	if (!f)
214 		return -errno;
215 
216 	while (fscanf(f, "%zx-%zx %s %zx %*[^\n]\n", &start, &end, buf, &offset) == 4) {
217 		if (addr >= start && addr < end) {
218 			fclose(f);
219 			return (size_t)addr - start + offset;
220 		}
221 	}
222 
223 	fclose(f);
224 	return -EINVAL;
225 }
226