linux-btrace.c revision 1.7 1 1.1 christos /* Linux-dependent part of branch trace support for GDB, and GDBserver.
2 1.1 christos
3 1.7 christos Copyright (C) 2013-2020 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.3 christos cpu.model = (cpuid >> 4) & 0xf;
88 1.3 christos
89 1.3 christos if (cpu.family == 0x6)
90 1.3 christos cpu.model += (cpuid >> 12) & 0xf0;
91 1.3 christos }
92 1.3 christos }
93 1.7 christos else if (ebx == signature_AMD_ebx && ecx == signature_AMD_ecx
94 1.7 christos && edx == signature_AMD_edx)
95 1.7 christos cpu.vendor = CV_AMD;
96 1.3 christos }
97 1.3 christos
98 1.3 christos return cpu;
99 1.3 christos }
100 1.1 christos
101 1.3 christos /* Return non-zero if there is new data in PEVENT; zero otherwise. */
102 1.3 christos
103 1.3 christos static int
104 1.3 christos perf_event_new_data (const struct perf_event_buffer *pev)
105 1.1 christos {
106 1.3 christos return *pev->data_head != pev->last_head;
107 1.1 christos }
108 1.1 christos
109 1.3 christos /* Copy the last SIZE bytes from PEV ending at DATA_HEAD and return a pointer
110 1.3 christos to the memory holding the copy.
111 1.3 christos The caller is responsible for freeing the memory. */
112 1.1 christos
113 1.3 christos static gdb_byte *
114 1.4 christos perf_event_read (const struct perf_event_buffer *pev, __u64 data_head,
115 1.4 christos size_t size)
116 1.1 christos {
117 1.3 christos const gdb_byte *begin, *end, *start, *stop;
118 1.3 christos gdb_byte *buffer;
119 1.4 christos size_t buffer_size;
120 1.4 christos __u64 data_tail;
121 1.3 christos
122 1.3 christos if (size == 0)
123 1.3 christos return NULL;
124 1.3 christos
125 1.5 christos /* We should never ask for more data than the buffer can hold. */
126 1.5 christos buffer_size = pev->size;
127 1.5 christos gdb_assert (size <= buffer_size);
128 1.5 christos
129 1.5 christos /* If we ask for more data than we seem to have, we wrap around and read
130 1.5 christos data from the end of the buffer. This is already handled by the %
131 1.5 christos BUFFER_SIZE operation, below. Here, we just need to make sure that we
132 1.5 christos don't underflow.
133 1.5 christos
134 1.5 christos Note that this is perfectly OK for perf event buffers where data_head
135 1.5 christos doesn'grow indefinitely and instead wraps around to remain within the
136 1.5 christos buffer's boundaries. */
137 1.5 christos if (data_head < size)
138 1.5 christos data_head += buffer_size;
139 1.5 christos
140 1.3 christos gdb_assert (size <= data_head);
141 1.3 christos data_tail = data_head - size;
142 1.3 christos
143 1.3 christos begin = pev->mem;
144 1.3 christos start = begin + data_tail % buffer_size;
145 1.3 christos stop = begin + data_head % buffer_size;
146 1.3 christos
147 1.4 christos buffer = (gdb_byte *) xmalloc (size);
148 1.3 christos
149 1.3 christos if (start < stop)
150 1.3 christos memcpy (buffer, start, stop - start);
151 1.3 christos else
152 1.3 christos {
153 1.3 christos end = begin + buffer_size;
154 1.3 christos
155 1.3 christos memcpy (buffer, start, end - start);
156 1.3 christos memcpy (buffer + (end - start), begin, stop - begin);
157 1.3 christos }
158 1.3 christos
159 1.3 christos return buffer;
160 1.1 christos }
161 1.1 christos
162 1.3 christos /* Copy the perf event buffer data from PEV.
163 1.3 christos Store a pointer to the copy into DATA and its size in SIZE. */
164 1.1 christos
165 1.3 christos static void
166 1.3 christos perf_event_read_all (struct perf_event_buffer *pev, gdb_byte **data,
167 1.4 christos size_t *psize)
168 1.1 christos {
169 1.4 christos size_t size;
170 1.4 christos __u64 data_head;
171 1.3 christos
172 1.3 christos data_head = *pev->data_head;
173 1.3 christos size = pev->size;
174 1.3 christos
175 1.3 christos *data = perf_event_read (pev, data_head, size);
176 1.3 christos *psize = size;
177 1.3 christos
178 1.3 christos pev->last_head = data_head;
179 1.1 christos }
180 1.1 christos
181 1.4 christos /* Try to determine the start address of the Linux kernel. */
182 1.4 christos
183 1.4 christos static uint64_t
184 1.4 christos linux_determine_kernel_start (void)
185 1.3 christos {
186 1.4 christos static uint64_t kernel_start;
187 1.4 christos static int cached;
188 1.4 christos
189 1.4 christos if (cached != 0)
190 1.4 christos return kernel_start;
191 1.4 christos
192 1.4 christos cached = 1;
193 1.4 christos
194 1.6 christos gdb_file_up file = gdb_fopen_cloexec ("/proc/kallsyms", "r");
195 1.4 christos if (file == NULL)
196 1.4 christos return kernel_start;
197 1.4 christos
198 1.6 christos while (!feof (file.get ()))
199 1.4 christos {
200 1.4 christos char buffer[1024], symbol[8], *line;
201 1.4 christos uint64_t addr;
202 1.4 christos int match;
203 1.3 christos
204 1.6 christos line = fgets (buffer, sizeof (buffer), file.get ());
205 1.4 christos if (line == NULL)
206 1.4 christos break;
207 1.3 christos
208 1.4 christos match = sscanf (line, "%" SCNx64 " %*[tT] %7s", &addr, symbol);
209 1.4 christos if (match != 2)
210 1.4 christos continue;
211 1.4 christos
212 1.4 christos if (strcmp (symbol, "_text") == 0)
213 1.4 christos {
214 1.4 christos kernel_start = addr;
215 1.4 christos break;
216 1.4 christos }
217 1.4 christos }
218 1.3 christos
219 1.4 christos return kernel_start;
220 1.1 christos }
221 1.1 christos
222 1.1 christos /* Check whether an address is in the kernel. */
223 1.1 christos
224 1.1 christos static inline int
225 1.4 christos perf_event_is_kernel_addr (uint64_t addr)
226 1.1 christos {
227 1.4 christos uint64_t kernel_start;
228 1.1 christos
229 1.4 christos kernel_start = linux_determine_kernel_start ();
230 1.4 christos if (kernel_start != 0ull)
231 1.4 christos return (addr >= kernel_start);
232 1.1 christos
233 1.4 christos /* If we don't know the kernel's start address, let's check the most
234 1.4 christos significant bit. This will work at least for 64-bit kernels. */
235 1.4 christos return ((addr & (1ull << 63)) != 0);
236 1.1 christos }
237 1.1 christos
238 1.1 christos /* Check whether a perf event record should be skipped. */
239 1.1 christos
240 1.1 christos static inline int
241 1.4 christos perf_event_skip_bts_record (const struct perf_event_bts *bts)
242 1.1 christos {
243 1.1 christos /* The hardware may report branches from kernel into user space. Branches
244 1.1 christos from user into kernel space will be suppressed. We filter the former to
245 1.1 christos provide a consistent branch trace excluding kernel. */
246 1.4 christos return perf_event_is_kernel_addr (bts->from);
247 1.1 christos }
248 1.1 christos
249 1.1 christos /* Perform a few consistency checks on a perf event sample record. This is
250 1.1 christos meant to catch cases when we get out of sync with the perf event stream. */
251 1.1 christos
252 1.1 christos static inline int
253 1.1 christos perf_event_sample_ok (const struct perf_event_sample *sample)
254 1.1 christos {
255 1.1 christos if (sample->header.type != PERF_RECORD_SAMPLE)
256 1.1 christos return 0;
257 1.1 christos
258 1.1 christos if (sample->header.size != sizeof (*sample))
259 1.1 christos return 0;
260 1.1 christos
261 1.1 christos return 1;
262 1.1 christos }
263 1.1 christos
264 1.1 christos /* Branch trace is collected in a circular buffer [begin; end) as pairs of from
265 1.1 christos and to addresses (plus a header).
266 1.1 christos
267 1.1 christos Start points into that buffer at the next sample position.
268 1.1 christos We read the collected samples backwards from start.
269 1.1 christos
270 1.1 christos While reading the samples, we convert the information into a list of blocks.
271 1.1 christos For two adjacent samples s1 and s2, we form a block b such that b.begin =
272 1.1 christos s1.to and b.end = s2.from.
273 1.1 christos
274 1.1 christos In case the buffer overflows during sampling, one sample may have its lower
275 1.1 christos part at the end and its upper part at the beginning of the buffer. */
276 1.1 christos
277 1.7 christos static std::vector<btrace_block> *
278 1.1 christos perf_event_read_bts (struct btrace_target_info* tinfo, const uint8_t *begin,
279 1.4 christos const uint8_t *end, const uint8_t *start, size_t size)
280 1.1 christos {
281 1.7 christos std::vector<btrace_block> *btrace = new std::vector<btrace_block>;
282 1.1 christos struct perf_event_sample sample;
283 1.4 christos size_t read = 0;
284 1.1 christos struct btrace_block block = { 0, 0 };
285 1.1 christos struct regcache *regcache;
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.1 christos 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.6 christos switch (errno)
425 1.3 christos {
426 1.6 christos case EPERM:
427 1.6 christos case EACCES:
428 1.6 christos {
429 1.6 christos static const char filename[] = "/proc/sys/kernel/perf_event_paranoid";
430 1.6 christos gdb_file_up file = gdb_fopen_cloexec (filename, "r");
431 1.6 christos if (file.get () == nullptr)
432 1.6 christos break;
433 1.1 christos
434 1.6 christos int level, found = fscanf (file.get (), "%d", &level);
435 1.6 christos if (found == 1 && level > 2)
436 1.6 christos error (_("You do not have permission to record the process. "
437 1.6 christos "Try setting %s to 2 or less."), filename);
438 1.6 christos }
439 1.1 christos
440 1.6 christos break;
441 1.1 christos }
442 1.1 christos
443 1.6 christos error (_("Failed to start recording: %s"), safe_strerror (errno));
444 1.3 christos }
445 1.3 christos
446 1.3 christos /* Enable branch tracing in BTS format. */
447 1.3 christos
448 1.3 christos static struct btrace_target_info *
449 1.3 christos linux_enable_bts (ptid_t ptid, const struct btrace_config_bts *conf)
450 1.1 christos {
451 1.3 christos struct btrace_tinfo_bts *bts;
452 1.4 christos size_t size, pages;
453 1.4 christos __u64 data_offset;
454 1.1 christos int pid, pg;
455 1.1 christos
456 1.6 christos if (!cpu_supports_bts ())
457 1.6 christos error (_("BTS support has been disabled for the target cpu."));
458 1.6 christos
459 1.6 christos gdb::unique_xmalloc_ptr<btrace_target_info> tinfo
460 1.6 christos (XCNEW (btrace_target_info));
461 1.1 christos tinfo->ptid = ptid;
462 1.3 christos
463 1.3 christos tinfo->conf.format = BTRACE_FORMAT_BTS;
464 1.3 christos bts = &tinfo->variant.bts;
465 1.1 christos
466 1.3 christos bts->attr.size = sizeof (bts->attr);
467 1.3 christos bts->attr.type = PERF_TYPE_HARDWARE;
468 1.3 christos bts->attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
469 1.3 christos bts->attr.sample_period = 1;
470 1.1 christos
471 1.1 christos /* We sample from and to address. */
472 1.3 christos bts->attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_ADDR;
473 1.1 christos
474 1.3 christos bts->attr.exclude_kernel = 1;
475 1.3 christos bts->attr.exclude_hv = 1;
476 1.3 christos bts->attr.exclude_idle = 1;
477 1.1 christos
478 1.6 christos pid = ptid.lwp ();
479 1.3 christos if (pid == 0)
480 1.6 christos pid = ptid.pid ();
481 1.3 christos
482 1.3 christos errno = 0;
483 1.6 christos scoped_fd fd (syscall (SYS_perf_event_open, &bts->attr, pid, -1, -1, 0));
484 1.6 christos if (fd.get () < 0)
485 1.6 christos diagnose_perf_event_open_fail ();
486 1.3 christos
487 1.3 christos /* Convert the requested size in bytes to pages (rounding up). */
488 1.4 christos pages = ((size_t) conf->size / PAGE_SIZE
489 1.4 christos + ((conf->size % PAGE_SIZE) == 0 ? 0 : 1));
490 1.3 christos /* We need at least one page. */
491 1.3 christos if (pages == 0)
492 1.3 christos pages = 1;
493 1.3 christos
494 1.3 christos /* The buffer size can be requested in powers of two pages. Adjust PAGES
495 1.3 christos to the next power of two. */
496 1.4 christos for (pg = 0; pages != ((size_t) 1 << pg); ++pg)
497 1.4 christos if ((pages & ((size_t) 1 << pg)) != 0)
498 1.4 christos pages += ((size_t) 1 << pg);
499 1.3 christos
500 1.3 christos /* We try to allocate the requested size.
501 1.3 christos If that fails, try to get as much as we can. */
502 1.6 christos scoped_mmap data;
503 1.3 christos for (; pages > 0; pages >>= 1)
504 1.3 christos {
505 1.3 christos size_t length;
506 1.4 christos __u64 data_size;
507 1.4 christos
508 1.4 christos data_size = (__u64) pages * PAGE_SIZE;
509 1.4 christos
510 1.4 christos /* Don't ask for more than we can represent in the configuration. */
511 1.4 christos if ((__u64) UINT_MAX < data_size)
512 1.4 christos continue;
513 1.3 christos
514 1.4 christos size = (size_t) data_size;
515 1.3 christos length = size + PAGE_SIZE;
516 1.3 christos
517 1.3 christos /* Check for overflows. */
518 1.4 christos if ((__u64) length != data_size + PAGE_SIZE)
519 1.3 christos continue;
520 1.3 christos
521 1.6 christos errno = 0;
522 1.3 christos /* The number of pages we request needs to be a power of two. */
523 1.6 christos data.reset (nullptr, length, PROT_READ, MAP_SHARED, fd.get (), 0);
524 1.6 christos if (data.get () != MAP_FAILED)
525 1.3 christos break;
526 1.3 christos }
527 1.3 christos
528 1.3 christos if (pages == 0)
529 1.6 christos error (_("Failed to map trace buffer: %s."), safe_strerror (errno));
530 1.3 christos
531 1.6 christos struct perf_event_mmap_page *header = (struct perf_event_mmap_page *)
532 1.6 christos data.get ();
533 1.3 christos data_offset = PAGE_SIZE;
534 1.3 christos
535 1.3 christos #if defined (PERF_ATTR_SIZE_VER5)
536 1.3 christos if (offsetof (struct perf_event_mmap_page, data_size) <= header->size)
537 1.3 christos {
538 1.4 christos __u64 data_size;
539 1.4 christos
540 1.3 christos data_offset = header->data_offset;
541 1.3 christos data_size = header->data_size;
542 1.4 christos
543 1.4 christos size = (unsigned int) data_size;
544 1.4 christos
545 1.4 christos /* Check for overflows. */
546 1.4 christos if ((__u64) size != data_size)
547 1.6 christos error (_("Failed to determine trace buffer size."));
548 1.3 christos }
549 1.3 christos #endif /* defined (PERF_ATTR_SIZE_VER5) */
550 1.3 christos
551 1.4 christos bts->bts.size = size;
552 1.3 christos bts->bts.data_head = &header->data_head;
553 1.7 christos bts->bts.mem = (const uint8_t *) data.release () + data_offset;
554 1.4 christos bts->bts.last_head = 0ull;
555 1.6 christos bts->header = header;
556 1.6 christos bts->file = fd.release ();
557 1.6 christos
558 1.4 christos tinfo->conf.bts.size = (unsigned int) size;
559 1.6 christos return tinfo.release ();
560 1.6 christos }
561 1.6 christos
562 1.6 christos #if defined (PERF_ATTR_SIZE_VER5)
563 1.6 christos
564 1.6 christos /* Determine the event type. */
565 1.6 christos
566 1.6 christos static int
567 1.6 christos perf_event_pt_event_type ()
568 1.6 christos {
569 1.6 christos static const char filename[] = "/sys/bus/event_source/devices/intel_pt/type";
570 1.3 christos
571 1.6 christos errno = 0;
572 1.6 christos gdb_file_up file = gdb_fopen_cloexec (filename, "r");
573 1.6 christos if (file.get () == nullptr)
574 1.6 christos error (_("Failed to open %s: %s."), filename, safe_strerror (errno));
575 1.6 christos
576 1.6 christos int type, found = fscanf (file.get (), "%d", &type);
577 1.6 christos if (found != 1)
578 1.6 christos error (_("Failed to read the PT event type from %s."), filename);
579 1.3 christos
580 1.6 christos return type;
581 1.3 christos }
582 1.3 christos
583 1.4 christos /* Enable branch tracing in Intel Processor Trace format. */
584 1.3 christos
585 1.3 christos static struct btrace_target_info *
586 1.3 christos linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf)
587 1.3 christos {
588 1.3 christos struct btrace_tinfo_pt *pt;
589 1.6 christos size_t pages;
590 1.6 christos int pid, pg;
591 1.3 christos
592 1.6 christos pid = ptid.lwp ();
593 1.1 christos if (pid == 0)
594 1.6 christos pid = ptid.pid ();
595 1.1 christos
596 1.6 christos gdb::unique_xmalloc_ptr<btrace_target_info> tinfo
597 1.6 christos (XCNEW (btrace_target_info));
598 1.3 christos tinfo->ptid = ptid;
599 1.3 christos
600 1.3 christos tinfo->conf.format = BTRACE_FORMAT_PT;
601 1.3 christos pt = &tinfo->variant.pt;
602 1.3 christos
603 1.3 christos pt->attr.size = sizeof (pt->attr);
604 1.6 christos pt->attr.type = perf_event_pt_event_type ();
605 1.3 christos
606 1.3 christos pt->attr.exclude_kernel = 1;
607 1.3 christos pt->attr.exclude_hv = 1;
608 1.3 christos pt->attr.exclude_idle = 1;
609 1.3 christos
610 1.1 christos errno = 0;
611 1.6 christos scoped_fd fd (syscall (SYS_perf_event_open, &pt->attr, pid, -1, -1, 0));
612 1.6 christos if (fd.get () < 0)
613 1.6 christos diagnose_perf_event_open_fail ();
614 1.1 christos
615 1.3 christos /* Allocate the configuration page. */
616 1.6 christos scoped_mmap data (nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
617 1.6 christos fd.get (), 0);
618 1.6 christos if (data.get () == MAP_FAILED)
619 1.6 christos error (_("Failed to map trace user page: %s."), safe_strerror (errno));
620 1.6 christos
621 1.6 christos struct perf_event_mmap_page *header = (struct perf_event_mmap_page *)
622 1.6 christos data.get ();
623 1.3 christos
624 1.3 christos header->aux_offset = header->data_offset + header->data_size;
625 1.3 christos
626 1.3 christos /* Convert the requested size in bytes to pages (rounding up). */
627 1.4 christos pages = ((size_t) conf->size / PAGE_SIZE
628 1.4 christos + ((conf->size % PAGE_SIZE) == 0 ? 0 : 1));
629 1.3 christos /* We need at least one page. */
630 1.3 christos if (pages == 0)
631 1.3 christos pages = 1;
632 1.3 christos
633 1.3 christos /* The buffer size can be requested in powers of two pages. Adjust PAGES
634 1.3 christos to the next power of two. */
635 1.4 christos for (pg = 0; pages != ((size_t) 1 << pg); ++pg)
636 1.4 christos if ((pages & ((size_t) 1 << pg)) != 0)
637 1.4 christos pages += ((size_t) 1 << pg);
638 1.3 christos
639 1.3 christos /* We try to allocate the requested size.
640 1.3 christos If that fails, try to get as much as we can. */
641 1.6 christos scoped_mmap aux;
642 1.3 christos for (; pages > 0; pages >>= 1)
643 1.1 christos {
644 1.3 christos size_t length;
645 1.4 christos __u64 data_size;
646 1.4 christos
647 1.4 christos data_size = (__u64) pages * PAGE_SIZE;
648 1.4 christos
649 1.4 christos /* Don't ask for more than we can represent in the configuration. */
650 1.4 christos if ((__u64) UINT_MAX < data_size)
651 1.4 christos continue;
652 1.3 christos
653 1.6 christos length = (size_t) data_size;
654 1.3 christos
655 1.3 christos /* Check for overflows. */
656 1.6 christos if ((__u64) length != data_size)
657 1.1 christos continue;
658 1.1 christos
659 1.4 christos header->aux_size = data_size;
660 1.3 christos
661 1.6 christos errno = 0;
662 1.6 christos aux.reset (nullptr, length, PROT_READ, MAP_SHARED, fd.get (),
663 1.6 christos header->aux_offset);
664 1.6 christos if (aux.get () != MAP_FAILED)
665 1.3 christos break;
666 1.1 christos }
667 1.1 christos
668 1.3 christos if (pages == 0)
669 1.6 christos error (_("Failed to map trace buffer: %s."), safe_strerror (errno));
670 1.3 christos
671 1.6 christos pt->pt.size = aux.size ();
672 1.6 christos pt->pt.mem = (const uint8_t *) aux.release ();
673 1.6 christos pt->pt.data_head = &header->aux_head;
674 1.7 christos pt->header = (struct perf_event_mmap_page *) data.release ();
675 1.7 christos gdb_assert (pt->header == header);
676 1.6 christos pt->file = fd.release ();
677 1.3 christos
678 1.6 christos tinfo->conf.pt.size = (unsigned int) pt->pt.size;
679 1.6 christos return tinfo.release ();
680 1.1 christos }
681 1.1 christos
682 1.3 christos #else /* !defined (PERF_ATTR_SIZE_VER5) */
683 1.3 christos
684 1.3 christos static struct btrace_target_info *
685 1.3 christos linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf)
686 1.3 christos {
687 1.6 christos error (_("Intel Processor Trace support was disabled at compile time."));
688 1.3 christos }
689 1.3 christos
690 1.3 christos #endif /* !defined (PERF_ATTR_SIZE_VER5) */
691 1.3 christos
692 1.1 christos /* See linux-btrace.h. */
693 1.1 christos
694 1.3 christos struct btrace_target_info *
695 1.3 christos linux_enable_btrace (ptid_t ptid, const struct btrace_config *conf)
696 1.1 christos {
697 1.3 christos switch (conf->format)
698 1.3 christos {
699 1.3 christos case BTRACE_FORMAT_NONE:
700 1.6 christos error (_("Bad branch trace format."));
701 1.6 christos
702 1.6 christos default:
703 1.6 christos error (_("Unknown branch trace format."));
704 1.3 christos
705 1.3 christos case BTRACE_FORMAT_BTS:
706 1.6 christos return linux_enable_bts (ptid, &conf->bts);
707 1.3 christos
708 1.3 christos case BTRACE_FORMAT_PT:
709 1.6 christos return linux_enable_pt (ptid, &conf->pt);
710 1.3 christos }
711 1.3 christos }
712 1.1 christos
713 1.3 christos /* Disable BTS tracing. */
714 1.1 christos
715 1.3 christos static enum btrace_error
716 1.3 christos linux_disable_bts (struct btrace_tinfo_bts *tinfo)
717 1.3 christos {
718 1.3 christos munmap((void *) tinfo->header, tinfo->bts.size + PAGE_SIZE);
719 1.1 christos close (tinfo->file);
720 1.1 christos
721 1.1 christos return BTRACE_ERR_NONE;
722 1.1 christos }
723 1.1 christos
724 1.4 christos /* Disable Intel Processor Trace tracing. */
725 1.1 christos
726 1.3 christos static enum btrace_error
727 1.3 christos linux_disable_pt (struct btrace_tinfo_pt *tinfo)
728 1.1 christos {
729 1.3 christos munmap((void *) tinfo->pt.mem, tinfo->pt.size);
730 1.3 christos munmap((void *) tinfo->header, PAGE_SIZE);
731 1.3 christos close (tinfo->file);
732 1.1 christos
733 1.3 christos return BTRACE_ERR_NONE;
734 1.1 christos }
735 1.1 christos
736 1.1 christos /* See linux-btrace.h. */
737 1.1 christos
738 1.1 christos enum btrace_error
739 1.3 christos linux_disable_btrace (struct btrace_target_info *tinfo)
740 1.3 christos {
741 1.3 christos enum btrace_error errcode;
742 1.3 christos
743 1.3 christos errcode = BTRACE_ERR_NOT_SUPPORTED;
744 1.3 christos switch (tinfo->conf.format)
745 1.3 christos {
746 1.3 christos case BTRACE_FORMAT_NONE:
747 1.3 christos break;
748 1.3 christos
749 1.3 christos case BTRACE_FORMAT_BTS:
750 1.3 christos errcode = linux_disable_bts (&tinfo->variant.bts);
751 1.3 christos break;
752 1.3 christos
753 1.3 christos case BTRACE_FORMAT_PT:
754 1.3 christos errcode = linux_disable_pt (&tinfo->variant.pt);
755 1.3 christos break;
756 1.3 christos }
757 1.3 christos
758 1.3 christos if (errcode == BTRACE_ERR_NONE)
759 1.3 christos xfree (tinfo);
760 1.3 christos
761 1.3 christos return errcode;
762 1.3 christos }
763 1.3 christos
764 1.3 christos /* Read branch trace data in BTS format for the thread given by TINFO into
765 1.3 christos BTRACE using the TYPE reading method. */
766 1.3 christos
767 1.3 christos static enum btrace_error
768 1.3 christos linux_read_bts (struct btrace_data_bts *btrace,
769 1.3 christos struct btrace_target_info *tinfo,
770 1.3 christos enum btrace_read_type type)
771 1.1 christos {
772 1.3 christos struct perf_event_buffer *pevent;
773 1.1 christos const uint8_t *begin, *end, *start;
774 1.4 christos size_t buffer_size, size;
775 1.4 christos __u64 data_head, data_tail;
776 1.3 christos unsigned int retries = 5;
777 1.3 christos
778 1.3 christos pevent = &tinfo->variant.bts.bts;
779 1.1 christos
780 1.1 christos /* For delta reads, we return at least the partial last block containing
781 1.1 christos the current PC. */
782 1.3 christos if (type == BTRACE_READ_NEW && !perf_event_new_data (pevent))
783 1.1 christos return BTRACE_ERR_NONE;
784 1.1 christos
785 1.3 christos buffer_size = pevent->size;
786 1.3 christos data_tail = pevent->last_head;
787 1.1 christos
788 1.1 christos /* We may need to retry reading the trace. See below. */
789 1.1 christos while (retries--)
790 1.1 christos {
791 1.3 christos data_head = *pevent->data_head;
792 1.1 christos
793 1.1 christos /* Delete any leftover trace from the previous iteration. */
794 1.7 christos delete btrace->blocks;
795 1.7 christos btrace->blocks = nullptr;
796 1.1 christos
797 1.1 christos if (type == BTRACE_READ_DELTA)
798 1.1 christos {
799 1.4 christos __u64 data_size;
800 1.4 christos
801 1.1 christos /* Determine the number of bytes to read and check for buffer
802 1.1 christos overflows. */
803 1.1 christos
804 1.1 christos /* Check for data head overflows. We might be able to recover from
805 1.1 christos those but they are very unlikely and it's not really worth the
806 1.1 christos effort, I think. */
807 1.1 christos if (data_head < data_tail)
808 1.1 christos return BTRACE_ERR_OVERFLOW;
809 1.1 christos
810 1.1 christos /* If the buffer is smaller than the trace delta, we overflowed. */
811 1.4 christos data_size = data_head - data_tail;
812 1.4 christos if (buffer_size < data_size)
813 1.1 christos return BTRACE_ERR_OVERFLOW;
814 1.4 christos
815 1.4 christos /* DATA_SIZE <= BUFFER_SIZE and therefore fits into a size_t. */
816 1.4 christos size = (size_t) data_size;
817 1.1 christos }
818 1.1 christos else
819 1.1 christos {
820 1.1 christos /* Read the entire buffer. */
821 1.1 christos size = buffer_size;
822 1.1 christos
823 1.1 christos /* Adjust the size if the buffer has not overflowed, yet. */
824 1.1 christos if (data_head < size)
825 1.4 christos size = (size_t) data_head;
826 1.1 christos }
827 1.1 christos
828 1.1 christos /* Data_head keeps growing; the buffer itself is circular. */
829 1.3 christos begin = pevent->mem;
830 1.1 christos start = begin + data_head % buffer_size;
831 1.1 christos
832 1.1 christos if (data_head <= buffer_size)
833 1.1 christos end = start;
834 1.1 christos else
835 1.3 christos end = begin + pevent->size;
836 1.1 christos
837 1.3 christos btrace->blocks = perf_event_read_bts (tinfo, begin, end, start, size);
838 1.1 christos
839 1.1 christos /* The stopping thread notifies its ptracer before it is scheduled out.
840 1.1 christos On multi-core systems, the debugger might therefore run while the
841 1.1 christos kernel might be writing the last branch trace records.
842 1.1 christos
843 1.1 christos Let's check whether the data head moved while we read the trace. */
844 1.3 christos if (data_head == *pevent->data_head)
845 1.1 christos break;
846 1.1 christos }
847 1.1 christos
848 1.3 christos pevent->last_head = data_head;
849 1.1 christos
850 1.1 christos /* Prune the incomplete last block (i.e. the first one of inferior execution)
851 1.1 christos if we're not doing a delta read. There is no way of filling in its zeroed
852 1.1 christos BEGIN element. */
853 1.7 christos if (!btrace->blocks->empty () && type != BTRACE_READ_DELTA)
854 1.7 christos btrace->blocks->pop_back ();
855 1.1 christos
856 1.1 christos return BTRACE_ERR_NONE;
857 1.1 christos }
858 1.1 christos
859 1.4 christos /* Fill in the Intel Processor Trace configuration information. */
860 1.3 christos
861 1.3 christos static void
862 1.3 christos linux_fill_btrace_pt_config (struct btrace_data_pt_config *conf)
863 1.3 christos {
864 1.3 christos conf->cpu = btrace_this_cpu ();
865 1.3 christos }
866 1.3 christos
867 1.4 christos /* Read branch trace data in Intel Processor Trace format for the thread
868 1.3 christos given by TINFO into BTRACE using the TYPE reading method. */
869 1.3 christos
870 1.3 christos static enum btrace_error
871 1.3 christos linux_read_pt (struct btrace_data_pt *btrace,
872 1.3 christos struct btrace_target_info *tinfo,
873 1.3 christos enum btrace_read_type type)
874 1.3 christos {
875 1.3 christos struct perf_event_buffer *pt;
876 1.3 christos
877 1.3 christos pt = &tinfo->variant.pt.pt;
878 1.3 christos
879 1.3 christos linux_fill_btrace_pt_config (&btrace->config);
880 1.3 christos
881 1.3 christos switch (type)
882 1.3 christos {
883 1.3 christos case BTRACE_READ_DELTA:
884 1.3 christos /* We don't support delta reads. The data head (i.e. aux_head) wraps
885 1.3 christos around to stay inside the aux buffer. */
886 1.3 christos return BTRACE_ERR_NOT_SUPPORTED;
887 1.3 christos
888 1.3 christos case BTRACE_READ_NEW:
889 1.3 christos if (!perf_event_new_data (pt))
890 1.3 christos return BTRACE_ERR_NONE;
891 1.3 christos
892 1.3 christos /* Fall through. */
893 1.3 christos case BTRACE_READ_ALL:
894 1.3 christos perf_event_read_all (pt, &btrace->data, &btrace->size);
895 1.3 christos return BTRACE_ERR_NONE;
896 1.3 christos }
897 1.3 christos
898 1.7 christos internal_error (__FILE__, __LINE__, _("Unknown btrace read type."));
899 1.3 christos }
900 1.3 christos
901 1.3 christos /* See linux-btrace.h. */
902 1.3 christos
903 1.3 christos enum btrace_error
904 1.3 christos linux_read_btrace (struct btrace_data *btrace,
905 1.3 christos struct btrace_target_info *tinfo,
906 1.3 christos enum btrace_read_type type)
907 1.3 christos {
908 1.3 christos switch (tinfo->conf.format)
909 1.3 christos {
910 1.3 christos case BTRACE_FORMAT_NONE:
911 1.3 christos return BTRACE_ERR_NOT_SUPPORTED;
912 1.3 christos
913 1.3 christos case BTRACE_FORMAT_BTS:
914 1.3 christos /* We read btrace in BTS format. */
915 1.3 christos btrace->format = BTRACE_FORMAT_BTS;
916 1.3 christos btrace->variant.bts.blocks = NULL;
917 1.3 christos
918 1.3 christos return linux_read_bts (&btrace->variant.bts, tinfo, type);
919 1.3 christos
920 1.3 christos case BTRACE_FORMAT_PT:
921 1.4 christos /* We read btrace in Intel Processor Trace format. */
922 1.3 christos btrace->format = BTRACE_FORMAT_PT;
923 1.3 christos btrace->variant.pt.data = NULL;
924 1.3 christos btrace->variant.pt.size = 0;
925 1.3 christos
926 1.3 christos return linux_read_pt (&btrace->variant.pt, tinfo, type);
927 1.3 christos }
928 1.3 christos
929 1.3 christos internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
930 1.3 christos }
931 1.3 christos
932 1.3 christos /* See linux-btrace.h. */
933 1.3 christos
934 1.3 christos const struct btrace_config *
935 1.3 christos linux_btrace_conf (const struct btrace_target_info *tinfo)
936 1.3 christos {
937 1.3 christos return &tinfo->conf;
938 1.3 christos }
939 1.3 christos
940 1.1 christos #else /* !HAVE_LINUX_PERF_EVENT_H */
941 1.1 christos
942 1.1 christos /* See linux-btrace.h. */
943 1.1 christos
944 1.1 christos struct btrace_target_info *
945 1.3 christos linux_enable_btrace (ptid_t ptid, const struct btrace_config *conf)
946 1.1 christos {
947 1.1 christos return NULL;
948 1.1 christos }
949 1.1 christos
950 1.1 christos /* See linux-btrace.h. */
951 1.1 christos
952 1.1 christos enum btrace_error
953 1.1 christos linux_disable_btrace (struct btrace_target_info *tinfo)
954 1.1 christos {
955 1.1 christos return BTRACE_ERR_NOT_SUPPORTED;
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.3 christos linux_read_btrace (struct btrace_data *btrace,
962 1.1 christos struct btrace_target_info *tinfo,
963 1.1 christos enum btrace_read_type type)
964 1.1 christos {
965 1.1 christos return BTRACE_ERR_NOT_SUPPORTED;
966 1.1 christos }
967 1.1 christos
968 1.3 christos /* See linux-btrace.h. */
969 1.3 christos
970 1.3 christos const struct btrace_config *
971 1.3 christos linux_btrace_conf (const struct btrace_target_info *tinfo)
972 1.3 christos {
973 1.3 christos return NULL;
974 1.3 christos }
975 1.3 christos
976 1.1 christos #endif /* !HAVE_LINUX_PERF_EVENT_H */
977