btrace, linux: add perf event buffer abstraction
[deliverable/binutils-gdb.git] / gdb / nat / linux-btrace.c
CommitLineData
7c97f91e
MM
1/* Linux-dependent part of branch trace support for GDB, and GDBserver.
2
32d0add0 3 Copyright (C) 2013-2015 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
53f81362 22#include "common-defs.h"
7c97f91e 23#include "linux-btrace.h"
361c8ade 24#include "common-regcache.h"
be8b1ea6 25#include "gdb_wait.h"
df7e5265 26#include "x86-cpuid.h"
7c97f91e 27
5b4e221c
MF
28#ifdef HAVE_SYS_SYSCALL_H
29#include <sys/syscall.h>
30#endif
31
32#if HAVE_LINUX_PERF_EVENT_H && defined(SYS_perf_event_open)
7c97f91e 33
7c97f91e
MM
34#include <stdint.h>
35#include <unistd.h>
7c97f91e
MM
36#include <sys/mman.h>
37#include <sys/user.h>
a950d57c
MM
38#include <sys/ptrace.h>
39#include <sys/types.h>
a950d57c 40#include <signal.h>
7c97f91e
MM
41
42/* A branch trace record in perf_event. */
43struct perf_event_bts
44{
45 /* The linear address of the branch source. */
46 uint64_t from;
47
48 /* The linear address of the branch destination. */
49 uint64_t to;
50};
51
52/* A perf_event branch trace sample. */
53struct perf_event_sample
54{
55 /* The perf_event sample header. */
56 struct perf_event_header header;
57
58 /* The perf_event branch tracing payload. */
59 struct perf_event_bts bts;
60};
61
aadf7753 62/* Return non-zero if there is new data in PEVENT; zero otherwise. */
7c97f91e 63
aadf7753
MM
64static int
65perf_event_new_data (const struct perf_event_buffer *pev)
7c97f91e 66{
aadf7753 67 return *pev->data_head != pev->last_head;
7c97f91e
MM
68}
69
70/* Check whether an address is in the kernel. */
71
72static inline int
73perf_event_is_kernel_addr (const struct btrace_target_info *tinfo,
74 uint64_t addr)
75{
76 uint64_t mask;
77
78 /* If we don't know the size of a pointer, we can't check. Let's assume it's
79 not a kernel address in this case. */
80 if (tinfo->ptr_bits == 0)
81 return 0;
82
83 /* A bit mask for the most significant bit in an address. */
84 mask = (uint64_t) 1 << (tinfo->ptr_bits - 1);
85
86 /* Check whether the most significant bit in the address is set. */
87 return (addr & mask) != 0;
88}
89
90/* Check whether a perf event record should be skipped. */
91
92static inline int
93perf_event_skip_record (const struct btrace_target_info *tinfo,
94 const struct perf_event_bts *bts)
95{
96 /* The hardware may report branches from kernel into user space. Branches
97 from user into kernel space will be suppressed. We filter the former to
98 provide a consistent branch trace excluding kernel. */
99 return perf_event_is_kernel_addr (tinfo, bts->from);
100}
101
102/* Perform a few consistency checks on a perf event sample record. This is
103 meant to catch cases when we get out of sync with the perf event stream. */
104
105static inline int
106perf_event_sample_ok (const struct perf_event_sample *sample)
107{
108 if (sample->header.type != PERF_RECORD_SAMPLE)
109 return 0;
110
111 if (sample->header.size != sizeof (*sample))
112 return 0;
113
114 return 1;
115}
116
117/* Branch trace is collected in a circular buffer [begin; end) as pairs of from
118 and to addresses (plus a header).
119
120 Start points into that buffer at the next sample position.
121 We read the collected samples backwards from start.
122
123 While reading the samples, we convert the information into a list of blocks.
124 For two adjacent samples s1 and s2, we form a block b such that b.begin =
125 s1.to and b.end = s2.from.
126
127 In case the buffer overflows during sampling, one sample may have its lower
128 part at the end and its upper part at the beginning of the buffer. */
129
130static VEC (btrace_block_s) *
131perf_event_read_bts (struct btrace_target_info* tinfo, const uint8_t *begin,
aadf7753
MM
132 const uint8_t *end, const uint8_t *start,
133 unsigned long long size)
7c97f91e
MM
134{
135 VEC (btrace_block_s) *btrace = NULL;
136 struct perf_event_sample sample;
aadf7753 137 unsigned long long read = 0;
7c97f91e
MM
138 struct btrace_block block = { 0, 0 };
139 struct regcache *regcache;
140
141 gdb_assert (begin <= start);
142 gdb_assert (start <= end);
143
144 /* The first block ends at the current pc. */
361c8ade 145 regcache = get_thread_regcache_for_ptid (tinfo->ptid);
7c97f91e
MM
146 block.end = regcache_read_pc (regcache);
147
148 /* The buffer may contain a partial record as its last entry (i.e. when the
149 buffer size is not a multiple of the sample size). */
150 read = sizeof (sample) - 1;
151
152 for (; read < size; read += sizeof (sample))
153 {
154 const struct perf_event_sample *psample;
155
156 /* Find the next perf_event sample in a backwards traversal. */
157 start -= sizeof (sample);
158
159 /* If we're still inside the buffer, we're done. */
160 if (begin <= start)
161 psample = (const struct perf_event_sample *) start;
162 else
163 {
164 int missing;
165
166 /* We're to the left of the ring buffer, we will wrap around and
167 reappear at the very right of the ring buffer. */
168
169 missing = (begin - start);
170 start = (end - missing);
171
172 /* If the entire sample is missing, we're done. */
173 if (missing == sizeof (sample))
174 psample = (const struct perf_event_sample *) start;
175 else
176 {
177 uint8_t *stack;
178
179 /* The sample wrapped around. The lower part is at the end and
180 the upper part is at the beginning of the buffer. */
181 stack = (uint8_t *) &sample;
182
183 /* Copy the two parts so we have a contiguous sample. */
184 memcpy (stack, start, missing);
185 memcpy (stack + missing, begin, sizeof (sample) - missing);
186
187 psample = &sample;
188 }
189 }
190
191 if (!perf_event_sample_ok (psample))
192 {
193 warning (_("Branch trace may be incomplete."));
194 break;
195 }
196
197 if (perf_event_skip_record (tinfo, &psample->bts))
198 continue;
199
200 /* We found a valid sample, so we can complete the current block. */
201 block.begin = psample->bts.to;
202
203 VEC_safe_push (btrace_block_s, btrace, &block);
204
205 /* Start the next block. */
206 block.end = psample->bts.from;
207 }
208
969c39fb
MM
209 /* Push the last block (i.e. the first one of inferior execution), as well.
210 We don't know where it ends, but we know where it starts. If we're
211 reading delta trace, we can fill in the start address later on.
212 Otherwise we will prune it. */
213 block.begin = 0;
214 VEC_safe_push (btrace_block_s, btrace, &block);
215
7c97f91e
MM
216 return btrace;
217}
218
043c3577 219/* Check whether the kernel supports BTS. */
a950d57c
MM
220
221static int
043c3577 222kernel_supports_bts (void)
a950d57c
MM
223{
224 struct perf_event_attr attr;
225 pid_t child, pid;
226 int status, file;
227
228 errno = 0;
229 child = fork ();
230 switch (child)
231 {
232 case -1:
043c3577 233 warning (_("test bts: cannot fork: %s."), strerror (errno));
a950d57c
MM
234 return 0;
235
236 case 0:
237 status = ptrace (PTRACE_TRACEME, 0, NULL, NULL);
238 if (status != 0)
239 {
043c3577 240 warning (_("test bts: cannot PTRACE_TRACEME: %s."),
a950d57c
MM
241 strerror (errno));
242 _exit (1);
243 }
244
245 status = raise (SIGTRAP);
246 if (status != 0)
247 {
043c3577 248 warning (_("test bts: cannot raise SIGTRAP: %s."),
a950d57c
MM
249 strerror (errno));
250 _exit (1);
251 }
252
253 _exit (1);
254
255 default:
256 pid = waitpid (child, &status, 0);
257 if (pid != child)
258 {
043c3577 259 warning (_("test bts: bad pid %ld, error: %s."),
a950d57c
MM
260 (long) pid, strerror (errno));
261 return 0;
262 }
263
264 if (!WIFSTOPPED (status))
265 {
043c3577 266 warning (_("test bts: expected stop. status: %d."),
a950d57c
MM
267 status);
268 return 0;
269 }
270
271 memset (&attr, 0, sizeof (attr));
272
273 attr.type = PERF_TYPE_HARDWARE;
274 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
275 attr.sample_period = 1;
276 attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_ADDR;
277 attr.exclude_kernel = 1;
278 attr.exclude_hv = 1;
279 attr.exclude_idle = 1;
280
281 file = syscall (SYS_perf_event_open, &attr, child, -1, -1, 0);
282 if (file >= 0)
283 close (file);
284
285 kill (child, SIGKILL);
286 ptrace (PTRACE_KILL, child, NULL, NULL);
287
288 pid = waitpid (child, &status, 0);
289 if (pid != child)
290 {
043c3577 291 warning (_("test bts: bad pid %ld, error: %s."),
a950d57c
MM
292 (long) pid, strerror (errno));
293 if (!WIFSIGNALED (status))
043c3577 294 warning (_("test bts: expected killed. status: %d."),
a950d57c
MM
295 status);
296 }
297
298 return (file >= 0);
299 }
300}
301
043c3577 302/* Check whether an Intel cpu supports BTS. */
a950d57c
MM
303
304static int
043c3577 305intel_supports_bts (void)
a950d57c 306{
5f8e0b8f
MF
307 unsigned int cpuid, model, family;
308
df7e5265 309 if (!x86_cpuid (1, &cpuid, NULL, NULL, NULL))
4d157a3d 310 return 0;
5f8e0b8f
MF
311
312 family = (cpuid >> 8) & 0xf;
313 model = (cpuid >> 4) & 0xf;
314
315 switch (family)
316 {
317 case 0x6:
318 model += (cpuid >> 12) & 0xf0;
319
320 switch (model)
321 {
322 case 0x1a: /* Nehalem */
323 case 0x1f:
324 case 0x1e:
325 case 0x2e:
326 case 0x25: /* Westmere */
327 case 0x2c:
328 case 0x2f:
329 case 0x2a: /* Sandy Bridge */
330 case 0x2d:
331 case 0x3a: /* Ivy Bridge */
332
333 /* AAJ122: LBR, BTM, or BTS records may have incorrect branch
334 "from" information afer an EIST transition, T-states, C1E, or
335 Adaptive Thermal Throttling. */
336 return 0;
337 }
338 }
a950d57c
MM
339
340 return 1;
a950d57c
MM
341}
342
043c3577 343/* Check whether the cpu supports BTS. */
a950d57c
MM
344
345static int
043c3577 346cpu_supports_bts (void)
a950d57c 347{
4d157a3d 348 unsigned int ebx, ecx, edx;
a950d57c 349
df7e5265 350 if (!x86_cpuid (0, NULL, &ebx, &ecx, &edx))
4d157a3d
MF
351 return 0;
352
4353c9e6
JK
353 if (ebx == signature_INTEL_ebx && ecx == signature_INTEL_ecx
354 && edx == signature_INTEL_edx)
043c3577 355 return intel_supports_bts ();
a950d57c
MM
356
357 /* Don't know about others. Let's assume they do. */
358 return 1;
a950d57c
MM
359}
360
043c3577 361/* Check whether the linux target supports BTS. */
7c97f91e 362
043c3577
MM
363static int
364linux_supports_bts (void)
7c97f91e 365{
a950d57c
MM
366 static int cached;
367
368 if (cached == 0)
369 {
043c3577 370 if (!kernel_supports_bts ())
a950d57c 371 cached = -1;
043c3577 372 else if (!cpu_supports_bts ())
a950d57c
MM
373 cached = -1;
374 else
375 cached = 1;
376 }
377
378 return cached > 0;
7c97f91e
MM
379}
380
381/* See linux-btrace.h. */
382
043c3577
MM
383int
384linux_supports_btrace (struct target_ops *ops, enum btrace_format format)
385{
386 switch (format)
387 {
388 case BTRACE_FORMAT_NONE:
389 return 0;
390
391 case BTRACE_FORMAT_BTS:
392 return linux_supports_bts ();
393 }
394
395 internal_error (__FILE__, __LINE__, _("Unknown branch trace format"));
396}
397
398/* See linux-btrace.h. */
399
7c97f91e
MM
400struct btrace_target_info *
401linux_enable_btrace (ptid_t ptid)
402{
aadf7753 403 struct perf_event_mmap_page *header;
7c97f91e 404 struct btrace_target_info *tinfo;
d0fa7535 405 int pid, pg;
7c97f91e
MM
406
407 tinfo = xzalloc (sizeof (*tinfo));
408 tinfo->ptid = ptid;
409
410 tinfo->attr.size = sizeof (tinfo->attr);
411 tinfo->attr.type = PERF_TYPE_HARDWARE;
412 tinfo->attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
413 tinfo->attr.sample_period = 1;
414
415 /* We sample from and to address. */
416 tinfo->attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_ADDR;
417
418 tinfo->attr.exclude_kernel = 1;
419 tinfo->attr.exclude_hv = 1;
420 tinfo->attr.exclude_idle = 1;
421
422 tinfo->ptr_bits = 0;
423
424 pid = ptid_get_lwp (ptid);
425 if (pid == 0)
426 pid = ptid_get_pid (ptid);
427
428 errno = 0;
429 tinfo->file = syscall (SYS_perf_event_open, &tinfo->attr, pid, -1, -1, 0);
430 if (tinfo->file < 0)
431 goto err;
432
d0fa7535
MM
433 /* We try to allocate as much buffer as we can get.
434 We could allow the user to specify the size of the buffer, but then
435 we'd leave this search for the maximum buffer size to him. */
436 for (pg = 4; pg >= 0; --pg)
437 {
438 /* The number of pages we request needs to be a power of two. */
aadf7753
MM
439 header = mmap (NULL, ((1 << pg) + 1) * PAGE_SIZE, PROT_READ, MAP_SHARED,
440 tinfo->file, 0);
441 if (header != MAP_FAILED)
442 break;
d0fa7535 443 }
7c97f91e 444
aadf7753
MM
445 if (header == MAP_FAILED)
446 goto err_file;
447
448 tinfo->header = header;
449 tinfo->bts.mem = ((const uint8_t *) header) + PAGE_SIZE;
450 tinfo->bts.size = (1 << pg) * PAGE_SIZE;
451 tinfo->bts.data_head = &header->data_head;
452 tinfo->bts.last_head = 0;
453
454 return tinfo;
455
456 err_file:
d0fa7535 457 /* We were not able to allocate any buffer. */
7c97f91e
MM
458 close (tinfo->file);
459
460 err:
461 xfree (tinfo);
462 return NULL;
463}
464
465/* See linux-btrace.h. */
466
969c39fb 467enum btrace_error
7c97f91e
MM
468linux_disable_btrace (struct btrace_target_info *tinfo)
469{
aadf7753 470 munmap((void *) tinfo->header, tinfo->bts.size + PAGE_SIZE);
7c97f91e
MM
471 close (tinfo->file);
472 xfree (tinfo);
473
969c39fb 474 return BTRACE_ERR_NONE;
7c97f91e
MM
475}
476
734b0e4b
MM
477/* Read branch trace data in BTS format for the thread given by TINFO into
478 BTRACE using the TYPE reading method. */
7c97f91e 479
734b0e4b
MM
480static enum btrace_error
481linux_read_bts (struct btrace_data_bts *btrace,
482 struct btrace_target_info *tinfo,
483 enum btrace_read_type type)
7c97f91e 484{
aadf7753 485 struct perf_event_buffer *pevent;
7c97f91e 486 const uint8_t *begin, *end, *start;
aadf7753
MM
487 unsigned long long data_head, data_tail, buffer_size, size;
488 unsigned int retries = 5;
489
490 pevent = &tinfo->bts;
7c97f91e 491
969c39fb
MM
492 /* For delta reads, we return at least the partial last block containing
493 the current PC. */
aadf7753 494 if (type == BTRACE_READ_NEW && !perf_event_new_data (pevent))
969c39fb 495 return BTRACE_ERR_NONE;
7c97f91e 496
aadf7753
MM
497 buffer_size = pevent->size;
498 data_tail = pevent->last_head;
7c97f91e
MM
499
500 /* We may need to retry reading the trace. See below. */
501 while (retries--)
502 {
aadf7753 503 data_head = *pevent->data_head;
7c97f91e 504
ed9edfb5 505 /* Delete any leftover trace from the previous iteration. */
734b0e4b 506 VEC_free (btrace_block_s, btrace->blocks);
ed9edfb5 507
969c39fb 508 if (type == BTRACE_READ_DELTA)
7c97f91e 509 {
969c39fb
MM
510 /* Determine the number of bytes to read and check for buffer
511 overflows. */
512
513 /* Check for data head overflows. We might be able to recover from
514 those but they are very unlikely and it's not really worth the
515 effort, I think. */
516 if (data_head < data_tail)
517 return BTRACE_ERR_OVERFLOW;
518
519 /* If the buffer is smaller than the trace delta, we overflowed. */
520 size = data_head - data_tail;
521 if (buffer_size < size)
522 return BTRACE_ERR_OVERFLOW;
523 }
524 else
525 {
526 /* Read the entire buffer. */
527 size = buffer_size;
7c97f91e 528
969c39fb
MM
529 /* Adjust the size if the buffer has not overflowed, yet. */
530 if (data_head < size)
531 size = data_head;
7c97f91e
MM
532 }
533
969c39fb 534 /* Data_head keeps growing; the buffer itself is circular. */
aadf7753 535 begin = pevent->mem;
969c39fb
MM
536 start = begin + data_head % buffer_size;
537
538 if (data_head <= buffer_size)
539 end = start;
540 else
aadf7753 541 end = begin + pevent->size;
969c39fb 542
734b0e4b 543 btrace->blocks = perf_event_read_bts (tinfo, begin, end, start, size);
969c39fb 544
7c97f91e
MM
545 /* The stopping thread notifies its ptracer before it is scheduled out.
546 On multi-core systems, the debugger might therefore run while the
547 kernel might be writing the last branch trace records.
548
549 Let's check whether the data head moved while we read the trace. */
aadf7753 550 if (data_head == *pevent->data_head)
7c97f91e
MM
551 break;
552 }
553
aadf7753 554 pevent->last_head = data_head;
7c97f91e 555
969c39fb
MM
556 /* Prune the incomplete last block (i.e. the first one of inferior execution)
557 if we're not doing a delta read. There is no way of filling in its zeroed
558 BEGIN element. */
734b0e4b
MM
559 if (!VEC_empty (btrace_block_s, btrace->blocks)
560 && type != BTRACE_READ_DELTA)
561 VEC_pop (btrace_block_s, btrace->blocks);
969c39fb
MM
562
563 return BTRACE_ERR_NONE;
7c97f91e
MM
564}
565
734b0e4b
MM
566/* See linux-btrace.h. */
567
568enum btrace_error
569linux_read_btrace (struct btrace_data *btrace,
570 struct btrace_target_info *tinfo,
571 enum btrace_read_type type)
572{
573 /* We read btrace in BTS format. */
574 btrace->format = BTRACE_FORMAT_BTS;
575 btrace->variant.bts.blocks = NULL;
576
577 return linux_read_bts (&btrace->variant.bts, tinfo, type);
578}
579
7c97f91e
MM
580#else /* !HAVE_LINUX_PERF_EVENT_H */
581
582/* See linux-btrace.h. */
583
584int
043c3577 585linux_supports_btrace (struct target_ops *ops, enum btrace_format format)
7c97f91e
MM
586{
587 return 0;
588}
589
590/* See linux-btrace.h. */
591
592struct btrace_target_info *
593linux_enable_btrace (ptid_t ptid)
594{
595 return NULL;
596}
597
598/* See linux-btrace.h. */
599
969c39fb 600enum btrace_error
7c97f91e
MM
601linux_disable_btrace (struct btrace_target_info *tinfo)
602{
969c39fb 603 return BTRACE_ERR_NOT_SUPPORTED;
7c97f91e
MM
604}
605
606/* See linux-btrace.h. */
607
969c39fb 608enum btrace_error
734b0e4b 609linux_read_btrace (struct btrace_data *btrace,
969c39fb 610 struct btrace_target_info *tinfo,
7c97f91e
MM
611 enum btrace_read_type type)
612{
969c39fb 613 return BTRACE_ERR_NOT_SUPPORTED;
7c97f91e
MM
614}
615
616#endif /* !HAVE_LINUX_PERF_EVENT_H */
This page took 0.629672 seconds and 4 git commands to generate.