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