linux-btrace.c revision 1.9 1 1.1 christos /* Linux-dependent part of branch trace support for GDB, and GDBserver.
2 1.1 christos
3 1.9 christos Copyright (C) 2013-2024 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos Contributed by Intel Corp. <markus.t.metzger (at) intel.com>
6 1.1 christos
7 1.1 christos This file is part of GDB.
8 1.1 christos
9 1.1 christos This program is free software; you can redistribute it and/or modify
10 1.1 christos it under the terms of the GNU General Public License as published by
11 1.1 christos the Free Software Foundation; either version 3 of the License, or
12 1.1 christos (at your option) any later version.
13 1.1 christos
14 1.1 christos This program is distributed in the hope that it will be useful,
15 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
16 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 1.1 christos GNU General Public License for more details.
18 1.1 christos
19 1.1 christos You should have received a copy of the GNU General Public License
20 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 1.1 christos
22 1.1 christos #include "linux-btrace.h"
23 1.7 christos #include "gdbsupport/common-regcache.h"
24 1.7 christos #include "gdbsupport/gdb_wait.h"
25 1.1 christos #include "x86-cpuid.h"
26 1.7 christos #include "gdbsupport/filestuff.h"
27 1.7 christos #include "gdbsupport/scoped_fd.h"
28 1.7 christos #include "gdbsupport/scoped_mmap.h"
29 1.4 christos
30 1.4 christos #include <inttypes.h>
31 1.1 christos
32 1.1 christos #include <sys/syscall.h>
33 1.1 christos
34 1.1 christos #if HAVE_LINUX_PERF_EVENT_H && defined(SYS_perf_event_open)
35 1.1 christos #include <unistd.h>
36 1.1 christos #include <sys/mman.h>
37 1.1 christos #include <sys/user.h>
38 1.4 christos #include "nat/gdb_ptrace.h"
39 1.1 christos #include <sys/types.h>
40 1.1 christos #include <signal.h>
41 1.1 christos
42 1.1 christos /* A branch trace record in perf_event. */
43 1.1 christos struct perf_event_bts
44 1.1 christos {
45 1.1 christos /* The linear address of the branch source. */
46 1.1 christos uint64_t from;
47 1.1 christos
48 1.1 christos /* The linear address of the branch destination. */
49 1.1 christos uint64_t to;
50 1.1 christos };
51 1.1 christos
52 1.1 christos /* A perf_event branch trace sample. */
53 1.1 christos struct perf_event_sample
54 1.1 christos {
55 1.1 christos /* The perf_event sample header. */
56 1.1 christos struct perf_event_header header;
57 1.1 christos
58 1.1 christos /* The perf_event branch tracing payload. */
59 1.1 christos struct perf_event_bts bts;
60 1.1 christos };
61 1.1 christos
62 1.3 christos /* Identify the cpu we're running on. */
63 1.3 christos static struct btrace_cpu
64 1.3 christos btrace_this_cpu (void)
65 1.3 christos {
66 1.3 christos struct btrace_cpu cpu;
67 1.3 christos unsigned int eax, ebx, ecx, edx;
68 1.3 christos int ok;
69 1.3 christos
70 1.3 christos memset (&cpu, 0, sizeof (cpu));
71 1.3 christos
72 1.3 christos ok = x86_cpuid (0, &eax, &ebx, &ecx, &edx);
73 1.3 christos if (ok != 0)
74 1.3 christos {
75 1.3 christos if (ebx == signature_INTEL_ebx && ecx == signature_INTEL_ecx
76 1.3 christos && edx == signature_INTEL_edx)
77 1.3 christos {
78 1.3 christos unsigned int cpuid, ignore;
79 1.3 christos
80 1.3 christos ok = x86_cpuid (1, &cpuid, &ignore, &ignore, &ignore);
81 1.3 christos if (ok != 0)
82 1.3 christos {
83 1.3 christos cpu.vendor = CV_INTEL;
84 1.3 christos
85 1.3 christos cpu.family = (cpuid >> 8) & 0xf;
86 1.8 christos if (cpu.family == 0xf)
87 1.8 christos cpu.family += (cpuid >> 20) & 0xff;
88 1.8 christos
89 1.3 christos cpu.model = (cpuid >> 4) & 0xf;
90 1.8 christos if ((cpu.family == 0x6) || ((cpu.family & 0xf) == 0xf))
91 1.3 christos cpu.model += (cpuid >> 12) & 0xf0;
92 1.3 christos }
93 1.3 christos }
94 1.7 christos else if (ebx == signature_AMD_ebx && ecx == signature_AMD_ecx
95 1.7 christos && edx == signature_AMD_edx)
96 1.7 christos cpu.vendor = CV_AMD;
97 1.3 christos }
98 1.3 christos
99 1.3 christos return cpu;
100 1.3 christos }
101 1.1 christos
102 1.3 christos /* Return non-zero if there is new data in PEVENT; zero otherwise. */
103 1.3 christos
104 1.3 christos static int
105 1.3 christos perf_event_new_data (const struct perf_event_buffer *pev)
106 1.1 christos {
107 1.3 christos return *pev->data_head != pev->last_head;
108 1.1 christos }
109 1.1 christos
110 1.3 christos /* Copy the last SIZE bytes from PEV ending at DATA_HEAD and return a pointer
111 1.3 christos to the memory holding the copy.
112 1.3 christos The caller is responsible for freeing the memory. */
113 1.1 christos
114 1.3 christos static gdb_byte *
115 1.4 christos perf_event_read (const struct perf_event_buffer *pev, __u64 data_head,
116 1.4 christos size_t size)
117 1.1 christos {
118 1.3 christos const gdb_byte *begin, *end, *start, *stop;
119 1.3 christos gdb_byte *buffer;
120 1.4 christos size_t buffer_size;
121 1.4 christos __u64 data_tail;
122 1.3 christos
123 1.3 christos if (size == 0)
124 1.3 christos return NULL;
125 1.3 christos
126 1.5 christos /* We should never ask for more data than the buffer can hold. */
127 1.5 christos buffer_size = pev->size;
128 1.5 christos gdb_assert (size <= buffer_size);
129 1.5 christos
130 1.5 christos /* If we ask for more data than we seem to have, we wrap around and read
131 1.5 christos data from the end of the buffer. This is already handled by the %
132 1.5 christos BUFFER_SIZE operation, below. Here, we just need to make sure that we
133 1.5 christos don't underflow.
134 1.5 christos
135 1.5 christos Note that this is perfectly OK for perf event buffers where data_head
136 1.5 christos doesn'grow indefinitely and instead wraps around to remain within the
137 1.5 christos buffer's boundaries. */
138 1.5 christos if (data_head < size)
139 1.5 christos data_head += buffer_size;
140 1.5 christos
141 1.3 christos gdb_assert (size <= data_head);
142 1.3 christos data_tail = data_head - size;
143 1.3 christos
144 1.3 christos begin = pev->mem;
145 1.3 christos start = begin + data_tail % buffer_size;
146 1.3 christos stop = begin + data_head % buffer_size;
147 1.3 christos
148 1.4 christos buffer = (gdb_byte *) xmalloc (size);
149 1.3 christos
150 1.3 christos if (start < stop)
151 1.3 christos memcpy (buffer, start, stop - start);
152 1.3 christos else
153 1.3 christos {
154 1.3 christos end = begin + buffer_size;
155 1.3 christos
156 1.3 christos memcpy (buffer, start, end - start);
157 1.3 christos memcpy (buffer + (end - start), begin, stop - begin);
158 1.3 christos }
159 1.3 christos
160 1.3 christos return buffer;
161 1.1 christos }
162 1.1 christos
163 1.3 christos /* Copy the perf event buffer data from PEV.
164 1.3 christos Store a pointer to the copy into DATA and its size in SIZE. */
165 1.1 christos
166 1.3 christos static void
167 1.3 christos perf_event_read_all (struct perf_event_buffer *pev, gdb_byte **data,
168 1.4 christos size_t *psize)
169 1.1 christos {
170 1.4 christos size_t size;
171 1.4 christos __u64 data_head;
172 1.3 christos
173 1.3 christos data_head = *pev->data_head;
174 1.3 christos size = pev->size;
175 1.3 christos
176 1.3 christos *data = perf_event_read (pev, data_head, size);
177 1.3 christos *psize = size;
178 1.3 christos
179 1.3 christos pev->last_head = data_head;
180 1.1 christos }
181 1.1 christos
182 1.4 christos /* Try to determine the start address of the Linux kernel. */
183 1.4 christos
184 1.4 christos static uint64_t
185 1.4 christos linux_determine_kernel_start (void)
186 1.3 christos {
187 1.4 christos static uint64_t kernel_start;
188 1.4 christos static int cached;
189 1.4 christos
190 1.4 christos if (cached != 0)
191 1.4 christos return kernel_start;
192 1.4 christos
193 1.4 christos cached = 1;
194 1.4 christos
195 1.6 christos gdb_file_up file = gdb_fopen_cloexec ("/proc/kallsyms", "r");
196 1.4 christos if (file == NULL)
197 1.4 christos return kernel_start;
198 1.4 christos
199 1.6 christos while (!feof (file.get ()))
200 1.4 christos {
201 1.4 christos char buffer[1024], symbol[8], *line;
202 1.4 christos uint64_t addr;
203 1.4 christos int match;
204 1.3 christos
205 1.6 christos line = fgets (buffer, sizeof (buffer), file.get ());
206 1.4 christos if (line == NULL)
207 1.4 christos break;
208 1.3 christos
209 1.4 christos match = sscanf (line, "%" SCNx64 " %*[tT] %7s", &addr, symbol);
210 1.4 christos if (match != 2)
211 1.4 christos continue;
212 1.4 christos
213 1.4 christos if (strcmp (symbol, "_text") == 0)
214 1.4 christos {
215 1.4 christos kernel_start = addr;
216 1.4 christos break;
217 1.4 christos }
218 1.4 christos }
219 1.3 christos
220 1.4 christos return kernel_start;
221 1.1 christos }
222 1.1 christos
223 1.1 christos /* Check whether an address is in the kernel. */
224 1.1 christos
225 1.1 christos static inline int
226 1.4 christos perf_event_is_kernel_addr (uint64_t addr)
227 1.1 christos {
228 1.4 christos uint64_t kernel_start;
229 1.1 christos
230 1.4 christos kernel_start = linux_determine_kernel_start ();
231 1.4 christos if (kernel_start != 0ull)
232 1.4 christos return (addr >= kernel_start);
233 1.1 christos
234 1.4 christos /* If we don't know the kernel's start address, let's check the most
235 1.4 christos significant bit. This will work at least for 64-bit kernels. */
236 1.4 christos return ((addr & (1ull << 63)) != 0);
237 1.1 christos }
238 1.1 christos
239 1.1 christos /* Check whether a perf event record should be skipped. */
240 1.1 christos
241 1.1 christos static inline int
242 1.4 christos perf_event_skip_bts_record (const struct perf_event_bts *bts)
243 1.1 christos {
244 1.1 christos /* The hardware may report branches from kernel into user space. Branches
245 1.1 christos from user into kernel space will be suppressed. We filter the former to
246 1.1 christos provide a consistent branch trace excluding kernel. */
247 1.4 christos return perf_event_is_kernel_addr (bts->from);
248 1.1 christos }
249 1.1 christos
250 1.1 christos /* Perform a few consistency checks on a perf event sample record. This is
251 1.1 christos meant to catch cases when we get out of sync with the perf event stream. */
252 1.1 christos
253 1.1 christos static inline int
254 1.1 christos perf_event_sample_ok (const struct perf_event_sample *sample)
255 1.1 christos {
256 1.1 christos if (sample->header.type != PERF_RECORD_SAMPLE)
257 1.1 christos return 0;
258 1.1 christos
259 1.1 christos if (sample->header.size != sizeof (*sample))
260 1.1 christos return 0;
261 1.1 christos
262 1.1 christos return 1;
263 1.1 christos }
264 1.1 christos
265 1.1 christos /* Branch trace is collected in a circular buffer [begin; end) as pairs of from
266 1.1 christos and to addresses (plus a header).
267 1.1 christos
268 1.1 christos Start points into that buffer at the next sample position.
269 1.1 christos We read the collected samples backwards from start.
270 1.1 christos
271 1.1 christos While reading the samples, we convert the information into a list of blocks.
272 1.1 christos For two adjacent samples s1 and s2, we form a block b such that b.begin =
273 1.1 christos s1.to and b.end = s2.from.
274 1.1 christos
275 1.1 christos In case the buffer overflows during sampling, one sample may have its lower
276 1.1 christos part at the end and its upper part at the beginning of the buffer. */
277 1.1 christos
278 1.7 christos static std::vector<btrace_block> *
279 1.9 christos perf_event_read_bts (btrace_target_info *tinfo, const uint8_t *begin,
280 1.4 christos const uint8_t *end, const uint8_t *start, size_t size)
281 1.1 christos {
282 1.7 christos std::vector<btrace_block> *btrace = new std::vector<btrace_block>;
283 1.1 christos struct perf_event_sample sample;
284 1.4 christos size_t read = 0;
285 1.1 christos struct btrace_block block = { 0, 0 };
286 1.1 christos
287 1.1 christos gdb_assert (begin <= start);
288 1.1 christos gdb_assert (start <= end);
289 1.1 christos
290 1.1 christos /* The first block ends at the current pc. */
291 1.9 christos reg_buffer_common *regcache = get_thread_regcache_for_ptid (tinfo->ptid);
292 1.1 christos block.end = regcache_read_pc (regcache);
293 1.1 christos
294 1.1 christos /* The buffer may contain a partial record as its last entry (i.e. when the
295 1.1 christos buffer size is not a multiple of the sample size). */
296 1.1 christos read = sizeof (sample) - 1;
297 1.1 christos
298 1.1 christos for (; read < size; read += sizeof (sample))
299 1.1 christos {
300 1.1 christos const struct perf_event_sample *psample;
301 1.1 christos
302 1.1 christos /* Find the next perf_event sample in a backwards traversal. */
303 1.1 christos start -= sizeof (sample);
304 1.1 christos
305 1.1 christos /* If we're still inside the buffer, we're done. */
306 1.1 christos if (begin <= start)
307 1.1 christos psample = (const struct perf_event_sample *) start;
308 1.1 christos else
309 1.1 christos {
310 1.1 christos int missing;
311 1.1 christos
312 1.1 christos /* We're to the left of the ring buffer, we will wrap around and
313 1.1 christos reappear at the very right of the ring buffer. */
314 1.1 christos
315 1.1 christos missing = (begin - start);
316 1.1 christos start = (end - missing);
317 1.1 christos
318 1.1 christos /* If the entire sample is missing, we're done. */
319 1.1 christos if (missing == sizeof (sample))
320 1.1 christos psample = (const struct perf_event_sample *) start;
321 1.1 christos else
322 1.1 christos {
323 1.1 christos uint8_t *stack;
324 1.1 christos
325 1.1 christos /* The sample wrapped around. The lower part is at the end and
326 1.1 christos the upper part is at the beginning of the buffer. */
327 1.1 christos stack = (uint8_t *) &sample;
328 1.1 christos
329 1.1 christos /* Copy the two parts so we have a contiguous sample. */
330 1.1 christos memcpy (stack, start, missing);
331 1.1 christos memcpy (stack + missing, begin, sizeof (sample) - missing);
332 1.1 christos
333 1.1 christos psample = &sample;
334 1.1 christos }
335 1.1 christos }
336 1.1 christos
337 1.1 christos if (!perf_event_sample_ok (psample))
338 1.1 christos {
339 1.1 christos warning (_("Branch trace may be incomplete."));
340 1.1 christos break;
341 1.1 christos }
342 1.1 christos
343 1.4 christos if (perf_event_skip_bts_record (&psample->bts))
344 1.1 christos continue;
345 1.1 christos
346 1.1 christos /* We found a valid sample, so we can complete the current block. */
347 1.1 christos block.begin = psample->bts.to;
348 1.1 christos
349 1.7 christos btrace->push_back (block);
350 1.1 christos
351 1.1 christos /* Start the next block. */
352 1.1 christos block.end = psample->bts.from;
353 1.1 christos }
354 1.1 christos
355 1.1 christos /* Push the last block (i.e. the first one of inferior execution), as well.
356 1.1 christos We don't know where it ends, but we know where it starts. If we're
357 1.1 christos reading delta trace, we can fill in the start address later on.
358 1.1 christos Otherwise we will prune it. */
359 1.1 christos block.begin = 0;
360 1.7 christos btrace->push_back (block);
361 1.1 christos
362 1.1 christos return btrace;
363 1.1 christos }
364 1.1 christos
365 1.3 christos /* Check whether an Intel cpu supports BTS. */
366 1.1 christos
367 1.3 christos static int
368 1.3 christos intel_supports_bts (const struct btrace_cpu *cpu)
369 1.3 christos {
370 1.3 christos switch (cpu->family)
371 1.1 christos {
372 1.1 christos case 0x6:
373 1.3 christos switch (cpu->model)
374 1.1 christos {
375 1.1 christos case 0x1a: /* Nehalem */
376 1.1 christos case 0x1f:
377 1.1 christos case 0x1e:
378 1.1 christos case 0x2e:
379 1.1 christos case 0x25: /* Westmere */
380 1.1 christos case 0x2c:
381 1.1 christos case 0x2f:
382 1.1 christos case 0x2a: /* Sandy Bridge */
383 1.1 christos case 0x2d:
384 1.1 christos case 0x3a: /* Ivy Bridge */
385 1.1 christos
386 1.1 christos /* AAJ122: LBR, BTM, or BTS records may have incorrect branch
387 1.1 christos "from" information afer an EIST transition, T-states, C1E, or
388 1.1 christos Adaptive Thermal Throttling. */
389 1.1 christos return 0;
390 1.1 christos }
391 1.1 christos }
392 1.1 christos
393 1.1 christos return 1;
394 1.1 christos }
395 1.1 christos
396 1.3 christos /* Check whether the cpu supports BTS. */
397 1.1 christos
398 1.1 christos static int
399 1.3 christos cpu_supports_bts (void)
400 1.1 christos {
401 1.3 christos struct btrace_cpu cpu;
402 1.3 christos
403 1.3 christos cpu = btrace_this_cpu ();
404 1.3 christos switch (cpu.vendor)
405 1.3 christos {
406 1.3 christos default:
407 1.3 christos /* Don't know about others. Let's assume they do. */
408 1.3 christos return 1;
409 1.1 christos
410 1.3 christos case CV_INTEL:
411 1.3 christos return intel_supports_bts (&cpu);
412 1.7 christos
413 1.7 christos case CV_AMD:
414 1.7 christos return 0;
415 1.3 christos }
416 1.3 christos }
417 1.3 christos
418 1.6 christos /* The perf_event_open syscall failed. Try to print a helpful error
419 1.6 christos message. */
420 1.3 christos
421 1.6 christos static void
422 1.6 christos diagnose_perf_event_open_fail ()
423 1.3 christos {
424 1.9 christos int orig_errno = errno;
425 1.9 christos switch (orig_errno)
426 1.3 christos {
427 1.6 christos case EPERM:
428 1.6 christos case EACCES:
429 1.6 christos {
430 1.6 christos static const char filename[] = "/proc/sys/kernel/perf_event_paranoid";
431 1.8 christos errno = 0;
432 1.6 christos gdb_file_up file = gdb_fopen_cloexec (filename, "r");
433 1.6 christos if (file.get () == nullptr)
434 1.8 christos error (_("Failed to open %s (%s). Your system does not support "
435 1.8 christos "process recording."), filename, safe_strerror (errno));
436 1.1 christos
437 1.6 christos int level, found = fscanf (file.get (), "%d", &level);
438 1.6 christos if (found == 1 && level > 2)
439 1.6 christos error (_("You do not have permission to record the process. "
440 1.6 christos "Try setting %s to 2 or less."), filename);
441 1.6 christos }
442 1.1 christos
443 1.6 christos break;
444 1.1 christos }
445 1.1 christos
446 1.9 christos error (_("Failed to start recording: %s"), safe_strerror (orig_errno));
447 1.9 christos }
448 1.9 christos
449 1.9 christos /* Get the linux version of a btrace_target_info. */
450 1.9 christos
451 1.9 christos static linux_btrace_target_info *
452 1.9 christos get_linux_btrace_target_info (btrace_target_info *gtinfo)
453 1.9 christos {
454 1.9 christos return gdb::checked_static_cast<linux_btrace_target_info *> (gtinfo);
455 1.3 christos }
456 1.3 christos
457 1.3 christos /* Enable branch tracing in BTS format. */
458 1.3 christos
459 1.3 christos static struct btrace_target_info *
460 1.3 christos linux_enable_bts (ptid_t ptid, const struct btrace_config_bts *conf)
461 1.1 christos {
462 1.4 christos size_t size, pages;
463 1.4 christos __u64 data_offset;
464 1.1 christos int pid, pg;
465 1.1 christos
466 1.6 christos if (!cpu_supports_bts ())
467 1.6 christos error (_("BTS support has been disabled for the target cpu."));
468 1.6 christos
469 1.9 christos std::unique_ptr<linux_btrace_target_info> tinfo
470 1.9 christos { std::make_unique<linux_btrace_target_info> (ptid) };
471 1.3 christos
472 1.3 christos tinfo->conf.format = BTRACE_FORMAT_BTS;
473 1.1 christos
474 1.9 christos tinfo->attr.size = sizeof (tinfo->attr);
475 1.9 christos tinfo->attr.type = PERF_TYPE_HARDWARE;
476 1.9 christos tinfo->attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
477 1.9 christos tinfo->attr.sample_period = 1;
478 1.1 christos
479 1.1 christos /* We sample from and to address. */
480 1.9 christos tinfo->attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_ADDR;
481 1.1 christos
482 1.9 christos tinfo->attr.exclude_kernel = 1;
483 1.9 christos tinfo->attr.exclude_hv = 1;
484 1.9 christos tinfo->attr.exclude_idle = 1;
485 1.1 christos
486 1.6 christos pid = ptid.lwp ();
487 1.3 christos if (pid == 0)
488 1.6 christos pid = ptid.pid ();
489 1.3 christos
490 1.3 christos errno = 0;
491 1.9 christos scoped_fd fd (syscall (SYS_perf_event_open, &tinfo->attr, pid, -1, -1, 0));
492 1.6 christos if (fd.get () < 0)
493 1.6 christos diagnose_perf_event_open_fail ();
494 1.3 christos
495 1.3 christos /* Convert the requested size in bytes to pages (rounding up). */
496 1.4 christos pages = ((size_t) conf->size / PAGE_SIZE
497 1.4 christos + ((conf->size % PAGE_SIZE) == 0 ? 0 : 1));
498 1.3 christos /* We need at least one page. */
499 1.3 christos if (pages == 0)
500 1.3 christos pages = 1;
501 1.3 christos
502 1.3 christos /* The buffer size can be requested in powers of two pages. Adjust PAGES
503 1.3 christos to the next power of two. */
504 1.4 christos for (pg = 0; pages != ((size_t) 1 << pg); ++pg)
505 1.4 christos if ((pages & ((size_t) 1 << pg)) != 0)
506 1.4 christos pages += ((size_t) 1 << pg);
507 1.3 christos
508 1.3 christos /* We try to allocate the requested size.
509 1.3 christos If that fails, try to get as much as we can. */
510 1.6 christos scoped_mmap data;
511 1.3 christos for (; pages > 0; pages >>= 1)
512 1.3 christos {
513 1.3 christos size_t length;
514 1.4 christos __u64 data_size;
515 1.4 christos
516 1.4 christos data_size = (__u64) pages * PAGE_SIZE;
517 1.4 christos
518 1.4 christos /* Don't ask for more than we can represent in the configuration. */
519 1.4 christos if ((__u64) UINT_MAX < data_size)
520 1.4 christos continue;
521 1.3 christos
522 1.4 christos size = (size_t) data_size;
523 1.3 christos length = size + PAGE_SIZE;
524 1.3 christos
525 1.3 christos /* Check for overflows. */
526 1.4 christos if ((__u64) length != data_size + PAGE_SIZE)
527 1.3 christos continue;
528 1.3 christos
529 1.6 christos errno = 0;
530 1.3 christos /* The number of pages we request needs to be a power of two. */
531 1.6 christos data.reset (nullptr, length, PROT_READ, MAP_SHARED, fd.get (), 0);
532 1.6 christos if (data.get () != MAP_FAILED)
533 1.3 christos break;
534 1.3 christos }
535 1.3 christos
536 1.3 christos if (pages == 0)
537 1.6 christos error (_("Failed to map trace buffer: %s."), safe_strerror (errno));
538 1.3 christos
539 1.6 christos struct perf_event_mmap_page *header = (struct perf_event_mmap_page *)
540 1.6 christos data.get ();
541 1.3 christos data_offset = PAGE_SIZE;
542 1.3 christos
543 1.3 christos #if defined (PERF_ATTR_SIZE_VER5)
544 1.3 christos if (offsetof (struct perf_event_mmap_page, data_size) <= header->size)
545 1.3 christos {
546 1.4 christos __u64 data_size;
547 1.4 christos
548 1.3 christos data_offset = header->data_offset;
549 1.3 christos data_size = header->data_size;
550 1.4 christos
551 1.4 christos size = (unsigned int) data_size;
552 1.4 christos
553 1.4 christos /* Check for overflows. */
554 1.4 christos if ((__u64) size != data_size)
555 1.6 christos error (_("Failed to determine trace buffer size."));
556 1.3 christos }
557 1.3 christos #endif /* defined (PERF_ATTR_SIZE_VER5) */
558 1.3 christos
559 1.9 christos tinfo->pev.size = size;
560 1.9 christos tinfo->pev.data_head = &header->data_head;
561 1.9 christos tinfo->pev.mem = (const uint8_t *) data.release () + data_offset;
562 1.9 christos tinfo->pev.last_head = 0ull;
563 1.9 christos tinfo->header = header;
564 1.9 christos tinfo->file = fd.release ();
565 1.6 christos
566 1.4 christos tinfo->conf.bts.size = (unsigned int) size;
567 1.6 christos return tinfo.release ();
568 1.6 christos }
569 1.6 christos
570 1.6 christos #if defined (PERF_ATTR_SIZE_VER5)
571 1.6 christos
572 1.6 christos /* Determine the event type. */
573 1.6 christos
574 1.6 christos static int
575 1.6 christos perf_event_pt_event_type ()
576 1.6 christos {
577 1.6 christos static const char filename[] = "/sys/bus/event_source/devices/intel_pt/type";
578 1.3 christos
579 1.6 christos errno = 0;
580 1.6 christos gdb_file_up file = gdb_fopen_cloexec (filename, "r");
581 1.6 christos if (file.get () == nullptr)
582 1.8 christos switch (errno)
583 1.8 christos {
584 1.8 christos case EACCES:
585 1.8 christos case EFAULT:
586 1.8 christos case EPERM:
587 1.8 christos error (_("Failed to open %s (%s). You do not have permission "
588 1.8 christos "to use Intel PT."), filename, safe_strerror (errno));
589 1.8 christos
590 1.8 christos case ENOTDIR:
591 1.8 christos case ENOENT:
592 1.8 christos error (_("Failed to open %s (%s). Your system does not support "
593 1.8 christos "Intel PT."), filename, safe_strerror (errno));
594 1.8 christos
595 1.8 christos default:
596 1.8 christos error (_("Failed to open %s: %s."), filename, safe_strerror (errno));
597 1.8 christos }
598 1.6 christos
599 1.6 christos int type, found = fscanf (file.get (), "%d", &type);
600 1.6 christos if (found != 1)
601 1.6 christos error (_("Failed to read the PT event type from %s."), filename);
602 1.3 christos
603 1.6 christos return type;
604 1.3 christos }
605 1.3 christos
606 1.4 christos /* Enable branch tracing in Intel Processor Trace format. */
607 1.3 christos
608 1.3 christos static struct btrace_target_info *
609 1.3 christos linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf)
610 1.3 christos {
611 1.6 christos size_t pages;
612 1.6 christos int pid, pg;
613 1.3 christos
614 1.6 christos pid = ptid.lwp ();
615 1.1 christos if (pid == 0)
616 1.6 christos pid = ptid.pid ();
617 1.1 christos
618 1.9 christos std::unique_ptr<linux_btrace_target_info> tinfo
619 1.9 christos { std::make_unique<linux_btrace_target_info> (ptid) };
620 1.3 christos
621 1.3 christos tinfo->conf.format = BTRACE_FORMAT_PT;
622 1.3 christos
623 1.9 christos tinfo->attr.size = sizeof (tinfo->attr);
624 1.9 christos tinfo->attr.type = perf_event_pt_event_type ();
625 1.3 christos
626 1.9 christos tinfo->attr.exclude_kernel = 1;
627 1.9 christos tinfo->attr.exclude_hv = 1;
628 1.9 christos tinfo->attr.exclude_idle = 1;
629 1.3 christos
630 1.1 christos errno = 0;
631 1.9 christos scoped_fd fd (syscall (SYS_perf_event_open, &tinfo->attr, pid, -1, -1, 0));
632 1.6 christos if (fd.get () < 0)
633 1.6 christos diagnose_perf_event_open_fail ();
634 1.1 christos
635 1.3 christos /* Allocate the configuration page. */
636 1.6 christos scoped_mmap data (nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
637 1.6 christos fd.get (), 0);
638 1.6 christos if (data.get () == MAP_FAILED)
639 1.6 christos error (_("Failed to map trace user page: %s."), safe_strerror (errno));
640 1.6 christos
641 1.6 christos struct perf_event_mmap_page *header = (struct perf_event_mmap_page *)
642 1.6 christos data.get ();
643 1.3 christos
644 1.3 christos header->aux_offset = header->data_offset + header->data_size;
645 1.3 christos
646 1.3 christos /* Convert the requested size in bytes to pages (rounding up). */
647 1.4 christos pages = ((size_t) conf->size / PAGE_SIZE
648 1.4 christos + ((conf->size % PAGE_SIZE) == 0 ? 0 : 1));
649 1.3 christos /* We need at least one page. */
650 1.3 christos if (pages == 0)
651 1.3 christos pages = 1;
652 1.3 christos
653 1.3 christos /* The buffer size can be requested in powers of two pages. Adjust PAGES
654 1.3 christos to the next power of two. */
655 1.4 christos for (pg = 0; pages != ((size_t) 1 << pg); ++pg)
656 1.4 christos if ((pages & ((size_t) 1 << pg)) != 0)
657 1.4 christos pages += ((size_t) 1 << pg);
658 1.3 christos
659 1.3 christos /* We try to allocate the requested size.
660 1.3 christos If that fails, try to get as much as we can. */
661 1.6 christos scoped_mmap aux;
662 1.3 christos for (; pages > 0; pages >>= 1)
663 1.1 christos {
664 1.3 christos size_t length;
665 1.4 christos __u64 data_size;
666 1.4 christos
667 1.4 christos data_size = (__u64) pages * PAGE_SIZE;
668 1.4 christos
669 1.4 christos /* Don't ask for more than we can represent in the configuration. */
670 1.4 christos if ((__u64) UINT_MAX < data_size)
671 1.4 christos continue;
672 1.3 christos
673 1.6 christos length = (size_t) data_size;
674 1.3 christos
675 1.3 christos /* Check for overflows. */
676 1.6 christos if ((__u64) length != data_size)
677 1.1 christos continue;
678 1.1 christos
679 1.4 christos header->aux_size = data_size;
680 1.3 christos
681 1.6 christos errno = 0;
682 1.6 christos aux.reset (nullptr, length, PROT_READ, MAP_SHARED, fd.get (),
683 1.6 christos header->aux_offset);
684 1.6 christos if (aux.get () != MAP_FAILED)
685 1.3 christos break;
686 1.1 christos }
687 1.1 christos
688 1.3 christos if (pages == 0)
689 1.6 christos error (_("Failed to map trace buffer: %s."), safe_strerror (errno));
690 1.3 christos
691 1.9 christos tinfo->pev.size = aux.size ();
692 1.9 christos tinfo->pev.mem = (const uint8_t *) aux.release ();
693 1.9 christos tinfo->pev.data_head = &header->aux_head;
694 1.9 christos tinfo->header = (struct perf_event_mmap_page *) data.release ();
695 1.9 christos gdb_assert (tinfo->header == header);
696 1.9 christos tinfo->file = fd.release ();
697 1.3 christos
698 1.9 christos tinfo->conf.pt.size = (unsigned int) tinfo->pev.size;
699 1.6 christos return tinfo.release ();
700 1.1 christos }
701 1.1 christos
702 1.3 christos #else /* !defined (PERF_ATTR_SIZE_VER5) */
703 1.3 christos
704 1.3 christos static struct btrace_target_info *
705 1.3 christos linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf)
706 1.3 christos {
707 1.6 christos error (_("Intel Processor Trace support was disabled at compile time."));
708 1.3 christos }
709 1.3 christos
710 1.3 christos #endif /* !defined (PERF_ATTR_SIZE_VER5) */
711 1.3 christos
712 1.1 christos /* See linux-btrace.h. */
713 1.1 christos
714 1.3 christos struct btrace_target_info *
715 1.3 christos linux_enable_btrace (ptid_t ptid, const struct btrace_config *conf)
716 1.1 christos {
717 1.3 christos switch (conf->format)
718 1.3 christos {
719 1.3 christos case BTRACE_FORMAT_NONE:
720 1.6 christos error (_("Bad branch trace format."));
721 1.6 christos
722 1.6 christos default:
723 1.6 christos error (_("Unknown branch trace format."));
724 1.3 christos
725 1.3 christos case BTRACE_FORMAT_BTS:
726 1.6 christos return linux_enable_bts (ptid, &conf->bts);
727 1.3 christos
728 1.3 christos case BTRACE_FORMAT_PT:
729 1.6 christos return linux_enable_pt (ptid, &conf->pt);
730 1.3 christos }
731 1.3 christos }
732 1.1 christos
733 1.3 christos /* Disable BTS tracing. */
734 1.1 christos
735 1.9 christos static void
736 1.9 christos linux_disable_bts (struct linux_btrace_target_info *tinfo)
737 1.3 christos {
738 1.9 christos munmap ((void *) tinfo->header, tinfo->pev.size + PAGE_SIZE);
739 1.1 christos close (tinfo->file);
740 1.1 christos }
741 1.1 christos
742 1.4 christos /* Disable Intel Processor Trace tracing. */
743 1.1 christos
744 1.9 christos static void
745 1.9 christos linux_disable_pt (struct linux_btrace_target_info *tinfo)
746 1.1 christos {
747 1.9 christos munmap ((void *) tinfo->pev.mem, tinfo->pev.size);
748 1.9 christos munmap ((void *) tinfo->header, PAGE_SIZE);
749 1.3 christos close (tinfo->file);
750 1.1 christos }
751 1.1 christos
752 1.1 christos /* See linux-btrace.h. */
753 1.1 christos
754 1.1 christos enum btrace_error
755 1.9 christos linux_disable_btrace (struct btrace_target_info *gtinfo)
756 1.3 christos {
757 1.9 christos linux_btrace_target_info *tinfo
758 1.9 christos = get_linux_btrace_target_info (gtinfo);
759 1.3 christos
760 1.3 christos switch (tinfo->conf.format)
761 1.3 christos {
762 1.3 christos case BTRACE_FORMAT_NONE:
763 1.9 christos return BTRACE_ERR_NOT_SUPPORTED;
764 1.3 christos
765 1.3 christos case BTRACE_FORMAT_BTS:
766 1.9 christos linux_disable_bts (tinfo);
767 1.9 christos delete tinfo;
768 1.9 christos return BTRACE_ERR_NONE;
769 1.3 christos
770 1.3 christos case BTRACE_FORMAT_PT:
771 1.9 christos linux_disable_pt (tinfo);
772 1.9 christos delete tinfo;
773 1.9 christos return BTRACE_ERR_NONE;
774 1.3 christos }
775 1.3 christos
776 1.9 christos return BTRACE_ERR_NOT_SUPPORTED;
777 1.3 christos }
778 1.3 christos
779 1.3 christos /* Read branch trace data in BTS format for the thread given by TINFO into
780 1.3 christos BTRACE using the TYPE reading method. */
781 1.3 christos
782 1.3 christos static enum btrace_error
783 1.9 christos linux_read_bts (btrace_data_bts *btrace, linux_btrace_target_info *tinfo,
784 1.3 christos enum btrace_read_type type)
785 1.1 christos {
786 1.1 christos const uint8_t *begin, *end, *start;
787 1.4 christos size_t buffer_size, size;
788 1.8 christos __u64 data_head = 0, data_tail;
789 1.3 christos unsigned int retries = 5;
790 1.3 christos
791 1.1 christos /* For delta reads, we return at least the partial last block containing
792 1.1 christos the current PC. */
793 1.9 christos if (type == BTRACE_READ_NEW && !perf_event_new_data (&tinfo->pev))
794 1.1 christos return BTRACE_ERR_NONE;
795 1.1 christos
796 1.9 christos buffer_size = tinfo->pev.size;
797 1.9 christos data_tail = tinfo->pev.last_head;
798 1.1 christos
799 1.1 christos /* We may need to retry reading the trace. See below. */
800 1.1 christos while (retries--)
801 1.1 christos {
802 1.9 christos data_head = *tinfo->pev.data_head;
803 1.1 christos
804 1.1 christos /* Delete any leftover trace from the previous iteration. */
805 1.7 christos delete btrace->blocks;
806 1.7 christos btrace->blocks = nullptr;
807 1.1 christos
808 1.1 christos if (type == BTRACE_READ_DELTA)
809 1.1 christos {
810 1.4 christos __u64 data_size;
811 1.4 christos
812 1.1 christos /* Determine the number of bytes to read and check for buffer
813 1.1 christos overflows. */
814 1.1 christos
815 1.1 christos /* Check for data head overflows. We might be able to recover from
816 1.1 christos those but they are very unlikely and it's not really worth the
817 1.1 christos effort, I think. */
818 1.1 christos if (data_head < data_tail)
819 1.1 christos return BTRACE_ERR_OVERFLOW;
820 1.1 christos
821 1.1 christos /* If the buffer is smaller than the trace delta, we overflowed. */
822 1.4 christos data_size = data_head - data_tail;
823 1.4 christos if (buffer_size < data_size)
824 1.1 christos return BTRACE_ERR_OVERFLOW;
825 1.4 christos
826 1.4 christos /* DATA_SIZE <= BUFFER_SIZE and therefore fits into a size_t. */
827 1.4 christos size = (size_t) data_size;
828 1.1 christos }
829 1.1 christos else
830 1.1 christos {
831 1.1 christos /* Read the entire buffer. */
832 1.1 christos size = buffer_size;
833 1.1 christos
834 1.1 christos /* Adjust the size if the buffer has not overflowed, yet. */
835 1.1 christos if (data_head < size)
836 1.4 christos size = (size_t) data_head;
837 1.1 christos }
838 1.1 christos
839 1.1 christos /* Data_head keeps growing; the buffer itself is circular. */
840 1.9 christos begin = tinfo->pev.mem;
841 1.1 christos start = begin + data_head % buffer_size;
842 1.1 christos
843 1.1 christos if (data_head <= buffer_size)
844 1.1 christos end = start;
845 1.1 christos else
846 1.9 christos end = begin + tinfo->pev.size;
847 1.1 christos
848 1.3 christos btrace->blocks = perf_event_read_bts (tinfo, begin, end, start, size);
849 1.1 christos
850 1.1 christos /* The stopping thread notifies its ptracer before it is scheduled out.
851 1.1 christos On multi-core systems, the debugger might therefore run while the
852 1.1 christos kernel might be writing the last branch trace records.
853 1.1 christos
854 1.1 christos Let's check whether the data head moved while we read the trace. */
855 1.9 christos if (data_head == *tinfo->pev.data_head)
856 1.1 christos break;
857 1.1 christos }
858 1.1 christos
859 1.9 christos tinfo->pev.last_head = data_head;
860 1.1 christos
861 1.1 christos /* Prune the incomplete last block (i.e. the first one of inferior execution)
862 1.1 christos if we're not doing a delta read. There is no way of filling in its zeroed
863 1.1 christos BEGIN element. */
864 1.7 christos if (!btrace->blocks->empty () && type != BTRACE_READ_DELTA)
865 1.7 christos btrace->blocks->pop_back ();
866 1.1 christos
867 1.1 christos return BTRACE_ERR_NONE;
868 1.1 christos }
869 1.1 christos
870 1.4 christos /* Fill in the Intel Processor Trace configuration information. */
871 1.3 christos
872 1.3 christos static void
873 1.3 christos linux_fill_btrace_pt_config (struct btrace_data_pt_config *conf)
874 1.3 christos {
875 1.3 christos conf->cpu = btrace_this_cpu ();
876 1.3 christos }
877 1.3 christos
878 1.4 christos /* Read branch trace data in Intel Processor Trace format for the thread
879 1.3 christos given by TINFO into BTRACE using the TYPE reading method. */
880 1.3 christos
881 1.3 christos static enum btrace_error
882 1.9 christos linux_read_pt (btrace_data_pt *btrace, linux_btrace_target_info *tinfo,
883 1.3 christos enum btrace_read_type type)
884 1.3 christos {
885 1.3 christos linux_fill_btrace_pt_config (&btrace->config);
886 1.3 christos
887 1.3 christos switch (type)
888 1.3 christos {
889 1.3 christos case BTRACE_READ_DELTA:
890 1.3 christos /* We don't support delta reads. The data head (i.e. aux_head) wraps
891 1.3 christos around to stay inside the aux buffer. */
892 1.3 christos return BTRACE_ERR_NOT_SUPPORTED;
893 1.3 christos
894 1.3 christos case BTRACE_READ_NEW:
895 1.9 christos if (!perf_event_new_data (&tinfo->pev))
896 1.3 christos return BTRACE_ERR_NONE;
897 1.9 christos [[fallthrough]];
898 1.3 christos case BTRACE_READ_ALL:
899 1.9 christos perf_event_read_all (&tinfo->pev, &btrace->data, &btrace->size);
900 1.3 christos return BTRACE_ERR_NONE;
901 1.3 christos }
902 1.3 christos
903 1.8 christos internal_error (_("Unknown btrace read type."));
904 1.3 christos }
905 1.3 christos
906 1.3 christos /* See linux-btrace.h. */
907 1.3 christos
908 1.3 christos enum btrace_error
909 1.3 christos linux_read_btrace (struct btrace_data *btrace,
910 1.9 christos struct btrace_target_info *gtinfo,
911 1.3 christos enum btrace_read_type type)
912 1.3 christos {
913 1.9 christos linux_btrace_target_info *tinfo
914 1.9 christos = get_linux_btrace_target_info (gtinfo);
915 1.9 christos
916 1.3 christos switch (tinfo->conf.format)
917 1.3 christos {
918 1.3 christos case BTRACE_FORMAT_NONE:
919 1.3 christos return BTRACE_ERR_NOT_SUPPORTED;
920 1.3 christos
921 1.3 christos case BTRACE_FORMAT_BTS:
922 1.3 christos /* We read btrace in BTS format. */
923 1.3 christos btrace->format = BTRACE_FORMAT_BTS;
924 1.3 christos btrace->variant.bts.blocks = NULL;
925 1.3 christos
926 1.3 christos return linux_read_bts (&btrace->variant.bts, tinfo, type);
927 1.3 christos
928 1.3 christos case BTRACE_FORMAT_PT:
929 1.4 christos /* We read btrace in Intel Processor Trace format. */
930 1.3 christos btrace->format = BTRACE_FORMAT_PT;
931 1.3 christos btrace->variant.pt.data = NULL;
932 1.3 christos btrace->variant.pt.size = 0;
933 1.3 christos
934 1.3 christos return linux_read_pt (&btrace->variant.pt, tinfo, type);
935 1.3 christos }
936 1.3 christos
937 1.8 christos internal_error (_("Unkown branch trace format."));
938 1.3 christos }
939 1.3 christos
940 1.3 christos /* See linux-btrace.h. */
941 1.3 christos
942 1.3 christos const struct btrace_config *
943 1.3 christos linux_btrace_conf (const struct btrace_target_info *tinfo)
944 1.3 christos {
945 1.3 christos return &tinfo->conf;
946 1.3 christos }
947 1.3 christos
948 1.1 christos #else /* !HAVE_LINUX_PERF_EVENT_H */
949 1.1 christos
950 1.1 christos /* See linux-btrace.h. */
951 1.1 christos
952 1.1 christos struct btrace_target_info *
953 1.3 christos linux_enable_btrace (ptid_t ptid, const struct btrace_config *conf)
954 1.1 christos {
955 1.1 christos return NULL;
956 1.1 christos }
957 1.1 christos
958 1.1 christos /* See linux-btrace.h. */
959 1.1 christos
960 1.1 christos enum btrace_error
961 1.1 christos linux_disable_btrace (struct btrace_target_info *tinfo)
962 1.1 christos {
963 1.1 christos return BTRACE_ERR_NOT_SUPPORTED;
964 1.1 christos }
965 1.1 christos
966 1.1 christos /* See linux-btrace.h. */
967 1.1 christos
968 1.1 christos enum btrace_error
969 1.3 christos linux_read_btrace (struct btrace_data *btrace,
970 1.1 christos struct btrace_target_info *tinfo,
971 1.1 christos enum btrace_read_type type)
972 1.1 christos {
973 1.1 christos return BTRACE_ERR_NOT_SUPPORTED;
974 1.1 christos }
975 1.1 christos
976 1.3 christos /* See linux-btrace.h. */
977 1.3 christos
978 1.3 christos const struct btrace_config *
979 1.3 christos linux_btrace_conf (const struct btrace_target_info *tinfo)
980 1.3 christos {
981 1.3 christos return NULL;
982 1.3 christos }
983 1.3 christos
984 1.1 christos #endif /* !HAVE_LINUX_PERF_EVENT_H */
985