1 /* Linux-dependent part of branch trace support for GDB, and GDBserver.
3 Copyright (C) 2013-2015 Free Software Foundation, Inc.
5 Contributed by Intel Corp. <markus.t.metzger@intel.com>
7 This file is part of GDB.
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.
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.
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/>. */
22 #include "common-defs.h"
23 #include "linux-btrace.h"
24 #include "common-regcache.h"
26 #include "x86-cpuid.h"
28 #ifdef HAVE_SYS_SYSCALL_H
29 #include <sys/syscall.h>
32 #if HAVE_LINUX_PERF_EVENT_H && defined(SYS_perf_event_open)
38 #include <sys/ptrace.h>
39 #include <sys/types.h>
42 /* A branch trace record in perf_event. */
45 /* The linear address of the branch source. */
48 /* The linear address of the branch destination. */
52 /* A perf_event branch trace sample. */
53 struct perf_event_sample
55 /* The perf_event sample header. */
56 struct perf_event_header header;
58 /* The perf_event branch tracing payload. */
59 struct perf_event_bts bts;
62 /* Return non-zero if there is new data in PEVENT; zero otherwise. */
65 perf_event_new_data (const struct perf_event_buffer *pev)
67 return *pev->data_head != pev->last_head;
70 /* Check whether an address is in the kernel. */
73 perf_event_is_kernel_addr (const struct btrace_target_info *tinfo,
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)
83 /* A bit mask for the most significant bit in an address. */
84 mask = (uint64_t) 1 << (tinfo->ptr_bits - 1);
86 /* Check whether the most significant bit in the address is set. */
87 return (addr & mask) != 0;
90 /* Check whether a perf event record should be skipped. */
93 perf_event_skip_bts_record (const struct btrace_target_info *tinfo,
94 const struct perf_event_bts *bts)
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);
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. */
106 perf_event_sample_ok (const struct perf_event_sample *sample)
108 if (sample->header.type != PERF_RECORD_SAMPLE)
111 if (sample->header.size != sizeof (*sample))
117 /* Branch trace is collected in a circular buffer [begin; end) as pairs of from
118 and to addresses (plus a header).
120 Start points into that buffer at the next sample position.
121 We read the collected samples backwards from start.
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.
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. */
130 static VEC (btrace_block_s) *
131 perf_event_read_bts (struct btrace_target_info* tinfo, const uint8_t *begin,
132 const uint8_t *end, const uint8_t *start,
133 unsigned long long size)
135 VEC (btrace_block_s) *btrace = NULL;
136 struct perf_event_sample sample;
137 unsigned long long read = 0;
138 struct btrace_block block = { 0, 0 };
139 struct regcache *regcache;
141 gdb_assert (begin <= start);
142 gdb_assert (start <= end);
144 /* The first block ends at the current pc. */
145 regcache = get_thread_regcache_for_ptid (tinfo->ptid);
146 block.end = regcache_read_pc (regcache);
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;
152 for (; read < size; read += sizeof (sample))
154 const struct perf_event_sample *psample;
156 /* Find the next perf_event sample in a backwards traversal. */
157 start -= sizeof (sample);
159 /* If we're still inside the buffer, we're done. */
161 psample = (const struct perf_event_sample *) start;
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. */
169 missing = (begin - start);
170 start = (end - missing);
172 /* If the entire sample is missing, we're done. */
173 if (missing == sizeof (sample))
174 psample = (const struct perf_event_sample *) start;
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;
183 /* Copy the two parts so we have a contiguous sample. */
184 memcpy (stack, start, missing);
185 memcpy (stack + missing, begin, sizeof (sample) - missing);
191 if (!perf_event_sample_ok (psample))
193 warning (_("Branch trace may be incomplete."));
197 if (perf_event_skip_bts_record (tinfo, &psample->bts))
200 /* We found a valid sample, so we can complete the current block. */
201 block.begin = psample->bts.to;
203 VEC_safe_push (btrace_block_s, btrace, &block);
205 /* Start the next block. */
206 block.end = psample->bts.from;
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. */
214 VEC_safe_push (btrace_block_s, btrace, &block);
219 /* Check whether the kernel supports BTS. */
222 kernel_supports_bts (void)
224 struct perf_event_attr attr;
233 warning (_("test bts: cannot fork: %s."), strerror (errno));
237 status = ptrace (PTRACE_TRACEME, 0, NULL, NULL);
240 warning (_("test bts: cannot PTRACE_TRACEME: %s."),
245 status = raise (SIGTRAP);
248 warning (_("test bts: cannot raise SIGTRAP: %s."),
256 pid = waitpid (child, &status, 0);
259 warning (_("test bts: bad pid %ld, error: %s."),
260 (long) pid, strerror (errno));
264 if (!WIFSTOPPED (status))
266 warning (_("test bts: expected stop. status: %d."),
271 memset (&attr, 0, sizeof (attr));
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;
279 attr.exclude_idle = 1;
281 file = syscall (SYS_perf_event_open, &attr, child, -1, -1, 0);
285 kill (child, SIGKILL);
286 ptrace (PTRACE_KILL, child, NULL, NULL);
288 pid = waitpid (child, &status, 0);
291 warning (_("test bts: bad pid %ld, error: %s."),
292 (long) pid, strerror (errno));
293 if (!WIFSIGNALED (status))
294 warning (_("test bts: expected killed. status: %d."),
302 /* Check whether an Intel cpu supports BTS. */
305 intel_supports_bts (void)
307 unsigned int cpuid, model, family;
309 if (!x86_cpuid (1, &cpuid, NULL, NULL, NULL))
312 family = (cpuid >> 8) & 0xf;
313 model = (cpuid >> 4) & 0xf;
318 model += (cpuid >> 12) & 0xf0;
322 case 0x1a: /* Nehalem */
326 case 0x25: /* Westmere */
329 case 0x2a: /* Sandy Bridge */
331 case 0x3a: /* Ivy Bridge */
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. */
343 /* Check whether the cpu supports BTS. */
346 cpu_supports_bts (void)
348 unsigned int ebx, ecx, edx;
350 if (!x86_cpuid (0, NULL, &ebx, &ecx, &edx))
353 if (ebx == signature_INTEL_ebx && ecx == signature_INTEL_ecx
354 && edx == signature_INTEL_edx)
355 return intel_supports_bts ();
357 /* Don't know about others. Let's assume they do. */
361 /* Check whether the linux target supports BTS. */
364 linux_supports_bts (void)
370 if (!kernel_supports_bts ())
372 else if (!cpu_supports_bts ())
381 /* See linux-btrace.h. */
384 linux_supports_btrace (struct target_ops *ops, enum btrace_format format)
388 case BTRACE_FORMAT_NONE:
391 case BTRACE_FORMAT_BTS:
392 return linux_supports_bts ();
395 internal_error (__FILE__, __LINE__, _("Unknown branch trace format"));
398 /* Enable branch tracing in BTS format. */
400 static struct btrace_target_info *
401 linux_enable_bts (ptid_t ptid, const struct btrace_config *conf)
403 struct perf_event_mmap_page *header;
404 struct btrace_target_info *tinfo;
405 struct btrace_tinfo_bts *bts;
408 tinfo = xzalloc (sizeof (*tinfo));
412 tinfo->conf.format = BTRACE_FORMAT_BTS;
413 bts = &tinfo->variant.bts;
415 bts->attr.size = sizeof (bts->attr);
416 bts->attr.type = PERF_TYPE_HARDWARE;
417 bts->attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
418 bts->attr.sample_period = 1;
420 /* We sample from and to address. */
421 bts->attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_ADDR;
423 bts->attr.exclude_kernel = 1;
424 bts->attr.exclude_hv = 1;
425 bts->attr.exclude_idle = 1;
427 pid = ptid_get_lwp (ptid);
429 pid = ptid_get_pid (ptid);
432 bts->file = syscall (SYS_perf_event_open, &bts->attr, pid, -1, -1, 0);
436 /* We try to allocate as much buffer as we can get.
437 We could allow the user to specify the size of the buffer, but then
438 we'd leave this search for the maximum buffer size to him. */
439 for (pg = 4; pg >= 0; --pg)
441 /* The number of pages we request needs to be a power of two. */
442 header = mmap (NULL, ((1 << pg) + 1) * PAGE_SIZE, PROT_READ, MAP_SHARED,
444 if (header != MAP_FAILED)
448 if (header == MAP_FAILED)
451 bts->header = header;
452 bts->bts.mem = ((const uint8_t *) header) + PAGE_SIZE;
453 bts->bts.size = (1 << pg) * PAGE_SIZE;
454 bts->bts.data_head = &header->data_head;
455 bts->bts.last_head = 0;
460 /* We were not able to allocate any buffer. */
468 /* See linux-btrace.h. */
470 struct btrace_target_info *
471 linux_enable_btrace (ptid_t ptid, const struct btrace_config *conf)
473 struct btrace_target_info *tinfo;
476 switch (conf->format)
478 case BTRACE_FORMAT_NONE:
481 case BTRACE_FORMAT_BTS:
482 tinfo = linux_enable_bts (ptid, conf);
489 /* Disable BTS tracing. */
491 static enum btrace_error
492 linux_disable_bts (struct btrace_tinfo_bts *tinfo)
494 munmap((void *) tinfo->header, tinfo->bts.size + PAGE_SIZE);
497 return BTRACE_ERR_NONE;
500 /* See linux-btrace.h. */
503 linux_disable_btrace (struct btrace_target_info *tinfo)
505 enum btrace_error errcode;
507 errcode = BTRACE_ERR_NOT_SUPPORTED;
508 switch (tinfo->conf.format)
510 case BTRACE_FORMAT_NONE:
513 case BTRACE_FORMAT_BTS:
514 errcode = linux_disable_bts (&tinfo->variant.bts);
518 if (errcode == BTRACE_ERR_NONE)
524 /* Read branch trace data in BTS format for the thread given by TINFO into
525 BTRACE using the TYPE reading method. */
527 static enum btrace_error
528 linux_read_bts (struct btrace_data_bts *btrace,
529 struct btrace_target_info *tinfo,
530 enum btrace_read_type type)
532 struct perf_event_buffer *pevent;
533 const uint8_t *begin, *end, *start;
534 unsigned long long data_head, data_tail, buffer_size, size;
535 unsigned int retries = 5;
537 pevent = &tinfo->variant.bts.bts;
539 /* For delta reads, we return at least the partial last block containing
541 if (type == BTRACE_READ_NEW && !perf_event_new_data (pevent))
542 return BTRACE_ERR_NONE;
544 buffer_size = pevent->size;
545 data_tail = pevent->last_head;
547 /* We may need to retry reading the trace. See below. */
550 data_head = *pevent->data_head;
552 /* Delete any leftover trace from the previous iteration. */
553 VEC_free (btrace_block_s, btrace->blocks);
555 if (type == BTRACE_READ_DELTA)
557 /* Determine the number of bytes to read and check for buffer
560 /* Check for data head overflows. We might be able to recover from
561 those but they are very unlikely and it's not really worth the
563 if (data_head < data_tail)
564 return BTRACE_ERR_OVERFLOW;
566 /* If the buffer is smaller than the trace delta, we overflowed. */
567 size = data_head - data_tail;
568 if (buffer_size < size)
569 return BTRACE_ERR_OVERFLOW;
573 /* Read the entire buffer. */
576 /* Adjust the size if the buffer has not overflowed, yet. */
577 if (data_head < size)
581 /* Data_head keeps growing; the buffer itself is circular. */
583 start = begin + data_head % buffer_size;
585 if (data_head <= buffer_size)
588 end = begin + pevent->size;
590 btrace->blocks = perf_event_read_bts (tinfo, begin, end, start, size);
592 /* The stopping thread notifies its ptracer before it is scheduled out.
593 On multi-core systems, the debugger might therefore run while the
594 kernel might be writing the last branch trace records.
596 Let's check whether the data head moved while we read the trace. */
597 if (data_head == *pevent->data_head)
601 pevent->last_head = data_head;
603 /* Prune the incomplete last block (i.e. the first one of inferior execution)
604 if we're not doing a delta read. There is no way of filling in its zeroed
606 if (!VEC_empty (btrace_block_s, btrace->blocks)
607 && type != BTRACE_READ_DELTA)
608 VEC_pop (btrace_block_s, btrace->blocks);
610 return BTRACE_ERR_NONE;
613 /* See linux-btrace.h. */
616 linux_read_btrace (struct btrace_data *btrace,
617 struct btrace_target_info *tinfo,
618 enum btrace_read_type type)
620 switch (tinfo->conf.format)
622 case BTRACE_FORMAT_NONE:
623 return BTRACE_ERR_NOT_SUPPORTED;
625 case BTRACE_FORMAT_BTS:
626 /* We read btrace in BTS format. */
627 btrace->format = BTRACE_FORMAT_BTS;
628 btrace->variant.bts.blocks = NULL;
630 return linux_read_bts (&btrace->variant.bts, tinfo, type);
633 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
636 /* See linux-btrace.h. */
638 const struct btrace_config *
639 linux_btrace_conf (const struct btrace_target_info *tinfo)
644 #else /* !HAVE_LINUX_PERF_EVENT_H */
646 /* See linux-btrace.h. */
649 linux_supports_btrace (struct target_ops *ops, enum btrace_format format)
654 /* See linux-btrace.h. */
656 struct btrace_target_info *
657 linux_enable_btrace (ptid_t ptid, const struct btrace_config *conf)
662 /* See linux-btrace.h. */
665 linux_disable_btrace (struct btrace_target_info *tinfo)
667 return BTRACE_ERR_NOT_SUPPORTED;
670 /* See linux-btrace.h. */
673 linux_read_btrace (struct btrace_data *btrace,
674 struct btrace_target_info *tinfo,
675 enum btrace_read_type type)
677 return BTRACE_ERR_NOT_SUPPORTED;
680 /* See linux-btrace.h. */
682 const struct btrace_config *
683 linux_btrace_conf (const struct btrace_target_info *tinfo)
688 #endif /* !HAVE_LINUX_PERF_EVENT_H */