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