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>
41 #include <sys/utsname.h>
43 /* A branch trace record in perf_event. */
46 /* The linear address of the branch source. */
49 /* The linear address of the branch destination. */
53 /* A perf_event branch trace sample. */
54 struct perf_event_sample
56 /* The perf_event sample header. */
57 struct perf_event_header header;
59 /* The perf_event branch tracing payload. */
60 struct perf_event_bts bts;
63 /* Identify the cpu we're running on. */
64 static struct btrace_cpu
65 btrace_this_cpu (void)
67 struct btrace_cpu cpu;
68 unsigned int eax, ebx, ecx, edx;
71 memset (&cpu, 0, sizeof (cpu));
73 ok = x86_cpuid (0, &eax, &ebx, &ecx, &edx);
76 if (ebx == signature_INTEL_ebx && ecx == signature_INTEL_ecx
77 && edx == signature_INTEL_edx)
79 unsigned int cpuid, ignore;
81 ok = x86_cpuid (1, &cpuid, &ignore, &ignore, &ignore);
84 cpu.vendor = CV_INTEL;
86 cpu.family = (cpuid >> 8) & 0xf;
87 cpu.model = (cpuid >> 4) & 0xf;
89 if (cpu.family == 0x6)
90 cpu.model += (cpuid >> 12) & 0xf0;
98 /* Return non-zero if there is new data in PEVENT; zero otherwise. */
101 perf_event_new_data (const struct perf_event_buffer *pev)
103 return *pev->data_head != pev->last_head;
106 /* Try to determine the size of a pointer in bits for the OS.
108 This is the same as the size of a pointer for the inferior process
109 except when a 32-bit inferior is running on a 64-bit OS. */
112 linux_determine_kernel_ptr_bits (void)
117 memset (&utsn, 0, sizeof (utsn));
119 errcode = uname (&utsn);
123 /* We only need to handle the 64-bit host case, here. For 32-bit host,
124 the pointer size can be filled in later based on the inferior. */
125 if (strcmp (utsn.machine, "x86_64") == 0)
131 /* Check whether an address is in the kernel. */
134 perf_event_is_kernel_addr (const struct btrace_target_info *tinfo,
139 /* If we don't know the size of a pointer, we can't check. Let's assume it's
140 not a kernel address in this case. */
141 if (tinfo->ptr_bits == 0)
144 /* A bit mask for the most significant bit in an address. */
145 mask = (uint64_t) 1 << (tinfo->ptr_bits - 1);
147 /* Check whether the most significant bit in the address is set. */
148 return (addr & mask) != 0;
151 /* Check whether a perf event record should be skipped. */
154 perf_event_skip_bts_record (const struct btrace_target_info *tinfo,
155 const struct perf_event_bts *bts)
157 /* The hardware may report branches from kernel into user space. Branches
158 from user into kernel space will be suppressed. We filter the former to
159 provide a consistent branch trace excluding kernel. */
160 return perf_event_is_kernel_addr (tinfo, bts->from);
163 /* Perform a few consistency checks on a perf event sample record. This is
164 meant to catch cases when we get out of sync with the perf event stream. */
167 perf_event_sample_ok (const struct perf_event_sample *sample)
169 if (sample->header.type != PERF_RECORD_SAMPLE)
172 if (sample->header.size != sizeof (*sample))
178 /* Branch trace is collected in a circular buffer [begin; end) as pairs of from
179 and to addresses (plus a header).
181 Start points into that buffer at the next sample position.
182 We read the collected samples backwards from start.
184 While reading the samples, we convert the information into a list of blocks.
185 For two adjacent samples s1 and s2, we form a block b such that b.begin =
186 s1.to and b.end = s2.from.
188 In case the buffer overflows during sampling, one sample may have its lower
189 part at the end and its upper part at the beginning of the buffer. */
191 static VEC (btrace_block_s) *
192 perf_event_read_bts (struct btrace_target_info* tinfo, const uint8_t *begin,
193 const uint8_t *end, const uint8_t *start,
194 unsigned long long size)
196 VEC (btrace_block_s) *btrace = NULL;
197 struct perf_event_sample sample;
198 unsigned long long read = 0;
199 struct btrace_block block = { 0, 0 };
200 struct regcache *regcache;
202 gdb_assert (begin <= start);
203 gdb_assert (start <= end);
205 /* The first block ends at the current pc. */
206 regcache = get_thread_regcache_for_ptid (tinfo->ptid);
207 block.end = regcache_read_pc (regcache);
209 /* The buffer may contain a partial record as its last entry (i.e. when the
210 buffer size is not a multiple of the sample size). */
211 read = sizeof (sample) - 1;
213 for (; read < size; read += sizeof (sample))
215 const struct perf_event_sample *psample;
217 /* Find the next perf_event sample in a backwards traversal. */
218 start -= sizeof (sample);
220 /* If we're still inside the buffer, we're done. */
222 psample = (const struct perf_event_sample *) start;
227 /* We're to the left of the ring buffer, we will wrap around and
228 reappear at the very right of the ring buffer. */
230 missing = (begin - start);
231 start = (end - missing);
233 /* If the entire sample is missing, we're done. */
234 if (missing == sizeof (sample))
235 psample = (const struct perf_event_sample *) start;
240 /* The sample wrapped around. The lower part is at the end and
241 the upper part is at the beginning of the buffer. */
242 stack = (uint8_t *) &sample;
244 /* Copy the two parts so we have a contiguous sample. */
245 memcpy (stack, start, missing);
246 memcpy (stack + missing, begin, sizeof (sample) - missing);
252 if (!perf_event_sample_ok (psample))
254 warning (_("Branch trace may be incomplete."));
258 if (perf_event_skip_bts_record (tinfo, &psample->bts))
261 /* We found a valid sample, so we can complete the current block. */
262 block.begin = psample->bts.to;
264 VEC_safe_push (btrace_block_s, btrace, &block);
266 /* Start the next block. */
267 block.end = psample->bts.from;
270 /* Push the last block (i.e. the first one of inferior execution), as well.
271 We don't know where it ends, but we know where it starts. If we're
272 reading delta trace, we can fill in the start address later on.
273 Otherwise we will prune it. */
275 VEC_safe_push (btrace_block_s, btrace, &block);
280 /* Check whether the kernel supports BTS. */
283 kernel_supports_bts (void)
285 struct perf_event_attr attr;
294 warning (_("test bts: cannot fork: %s."), strerror (errno));
298 status = ptrace (PTRACE_TRACEME, 0, NULL, NULL);
301 warning (_("test bts: cannot PTRACE_TRACEME: %s."),
306 status = raise (SIGTRAP);
309 warning (_("test bts: cannot raise SIGTRAP: %s."),
317 pid = waitpid (child, &status, 0);
320 warning (_("test bts: bad pid %ld, error: %s."),
321 (long) pid, strerror (errno));
325 if (!WIFSTOPPED (status))
327 warning (_("test bts: expected stop. status: %d."),
332 memset (&attr, 0, sizeof (attr));
334 attr.type = PERF_TYPE_HARDWARE;
335 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
336 attr.sample_period = 1;
337 attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_ADDR;
338 attr.exclude_kernel = 1;
340 attr.exclude_idle = 1;
342 file = syscall (SYS_perf_event_open, &attr, child, -1, -1, 0);
346 kill (child, SIGKILL);
347 ptrace (PTRACE_KILL, child, NULL, NULL);
349 pid = waitpid (child, &status, 0);
352 warning (_("test bts: bad pid %ld, error: %s."),
353 (long) pid, strerror (errno));
354 if (!WIFSIGNALED (status))
355 warning (_("test bts: expected killed. status: %d."),
363 /* Check whether an Intel cpu supports BTS. */
366 intel_supports_bts (const struct btrace_cpu *cpu)
373 case 0x1a: /* Nehalem */
377 case 0x25: /* Westmere */
380 case 0x2a: /* Sandy Bridge */
382 case 0x3a: /* Ivy Bridge */
384 /* AAJ122: LBR, BTM, or BTS records may have incorrect branch
385 "from" information afer an EIST transition, T-states, C1E, or
386 Adaptive Thermal Throttling. */
394 /* Check whether the cpu supports BTS. */
397 cpu_supports_bts (void)
399 struct btrace_cpu cpu;
401 cpu = btrace_this_cpu ();
405 /* Don't know about others. Let's assume they do. */
409 return intel_supports_bts (&cpu);
413 /* Check whether the linux target supports BTS. */
416 linux_supports_bts (void)
422 if (!kernel_supports_bts ())
424 else if (!cpu_supports_bts ())
433 /* See linux-btrace.h. */
436 linux_supports_btrace (struct target_ops *ops, enum btrace_format format)
440 case BTRACE_FORMAT_NONE:
443 case BTRACE_FORMAT_BTS:
444 return linux_supports_bts ();
447 internal_error (__FILE__, __LINE__, _("Unknown branch trace format"));
450 /* Enable branch tracing in BTS format. */
452 static struct btrace_target_info *
453 linux_enable_bts (ptid_t ptid, const struct btrace_config_bts *conf)
455 struct perf_event_mmap_page *header;
456 struct btrace_target_info *tinfo;
457 struct btrace_tinfo_bts *bts;
458 unsigned long long size, pages;
461 tinfo = xzalloc (sizeof (*tinfo));
463 tinfo->ptr_bits = linux_determine_kernel_ptr_bits ();
465 tinfo->conf.format = BTRACE_FORMAT_BTS;
466 bts = &tinfo->variant.bts;
468 bts->attr.size = sizeof (bts->attr);
469 bts->attr.type = PERF_TYPE_HARDWARE;
470 bts->attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
471 bts->attr.sample_period = 1;
473 /* We sample from and to address. */
474 bts->attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_ADDR;
476 bts->attr.exclude_kernel = 1;
477 bts->attr.exclude_hv = 1;
478 bts->attr.exclude_idle = 1;
480 pid = ptid_get_lwp (ptid);
482 pid = ptid_get_pid (ptid);
485 bts->file = syscall (SYS_perf_event_open, &bts->attr, pid, -1, -1, 0);
489 /* Convert the requested size in bytes to pages (rounding up). */
490 pages = (((unsigned long long) conf->size) + PAGE_SIZE - 1) / PAGE_SIZE;
491 /* We need at least one page. */
495 /* The buffer size can be requested in powers of two pages. Adjust PAGES
496 to the next power of two. */
497 for (pg = 0; pages != (1u << pg); ++pg)
498 if ((pages & (1u << pg)) != 0)
501 /* We try to allocate the requested size.
502 If that fails, try to get as much as we can. */
503 for (; pages > 0; pages >>= 1)
507 size = pages * PAGE_SIZE;
508 length = size + PAGE_SIZE;
510 /* Check for overflows. */
511 if ((unsigned long long) length < size)
514 /* The number of pages we request needs to be a power of two. */
515 header = mmap (NULL, length, PROT_READ, MAP_SHARED, bts->file, 0);
516 if (header != MAP_FAILED)
520 if (header == MAP_FAILED)
523 bts->header = header;
524 bts->bts.mem = ((const uint8_t *) header) + PAGE_SIZE;
525 bts->bts.size = size;
526 bts->bts.data_head = &header->data_head;
527 bts->bts.last_head = 0;
529 tinfo->conf.bts.size = size;
533 /* We were not able to allocate any buffer. */
541 /* See linux-btrace.h. */
543 struct btrace_target_info *
544 linux_enable_btrace (ptid_t ptid, const struct btrace_config *conf)
546 struct btrace_target_info *tinfo;
549 switch (conf->format)
551 case BTRACE_FORMAT_NONE:
554 case BTRACE_FORMAT_BTS:
555 tinfo = linux_enable_bts (ptid, &conf->bts);
562 /* Disable BTS tracing. */
564 static enum btrace_error
565 linux_disable_bts (struct btrace_tinfo_bts *tinfo)
567 munmap((void *) tinfo->header, tinfo->bts.size + PAGE_SIZE);
570 return BTRACE_ERR_NONE;
573 /* See linux-btrace.h. */
576 linux_disable_btrace (struct btrace_target_info *tinfo)
578 enum btrace_error errcode;
580 errcode = BTRACE_ERR_NOT_SUPPORTED;
581 switch (tinfo->conf.format)
583 case BTRACE_FORMAT_NONE:
586 case BTRACE_FORMAT_BTS:
587 errcode = linux_disable_bts (&tinfo->variant.bts);
591 if (errcode == BTRACE_ERR_NONE)
597 /* Read branch trace data in BTS format for the thread given by TINFO into
598 BTRACE using the TYPE reading method. */
600 static enum btrace_error
601 linux_read_bts (struct btrace_data_bts *btrace,
602 struct btrace_target_info *tinfo,
603 enum btrace_read_type type)
605 struct perf_event_buffer *pevent;
606 const uint8_t *begin, *end, *start;
607 unsigned long long data_head, data_tail, buffer_size, size;
608 unsigned int retries = 5;
610 pevent = &tinfo->variant.bts.bts;
612 /* For delta reads, we return at least the partial last block containing
614 if (type == BTRACE_READ_NEW && !perf_event_new_data (pevent))
615 return BTRACE_ERR_NONE;
617 buffer_size = pevent->size;
618 data_tail = pevent->last_head;
620 /* We may need to retry reading the trace. See below. */
623 data_head = *pevent->data_head;
625 /* Delete any leftover trace from the previous iteration. */
626 VEC_free (btrace_block_s, btrace->blocks);
628 if (type == BTRACE_READ_DELTA)
630 /* Determine the number of bytes to read and check for buffer
633 /* Check for data head overflows. We might be able to recover from
634 those but they are very unlikely and it's not really worth the
636 if (data_head < data_tail)
637 return BTRACE_ERR_OVERFLOW;
639 /* If the buffer is smaller than the trace delta, we overflowed. */
640 size = data_head - data_tail;
641 if (buffer_size < size)
642 return BTRACE_ERR_OVERFLOW;
646 /* Read the entire buffer. */
649 /* Adjust the size if the buffer has not overflowed, yet. */
650 if (data_head < size)
654 /* Data_head keeps growing; the buffer itself is circular. */
656 start = begin + data_head % buffer_size;
658 if (data_head <= buffer_size)
661 end = begin + pevent->size;
663 btrace->blocks = perf_event_read_bts (tinfo, begin, end, start, size);
665 /* The stopping thread notifies its ptracer before it is scheduled out.
666 On multi-core systems, the debugger might therefore run while the
667 kernel might be writing the last branch trace records.
669 Let's check whether the data head moved while we read the trace. */
670 if (data_head == *pevent->data_head)
674 pevent->last_head = data_head;
676 /* Prune the incomplete last block (i.e. the first one of inferior execution)
677 if we're not doing a delta read. There is no way of filling in its zeroed
679 if (!VEC_empty (btrace_block_s, btrace->blocks)
680 && type != BTRACE_READ_DELTA)
681 VEC_pop (btrace_block_s, btrace->blocks);
683 return BTRACE_ERR_NONE;
686 /* See linux-btrace.h. */
689 linux_read_btrace (struct btrace_data *btrace,
690 struct btrace_target_info *tinfo,
691 enum btrace_read_type type)
693 switch (tinfo->conf.format)
695 case BTRACE_FORMAT_NONE:
696 return BTRACE_ERR_NOT_SUPPORTED;
698 case BTRACE_FORMAT_BTS:
699 /* We read btrace in BTS format. */
700 btrace->format = BTRACE_FORMAT_BTS;
701 btrace->variant.bts.blocks = NULL;
703 return linux_read_bts (&btrace->variant.bts, tinfo, type);
706 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
709 /* See linux-btrace.h. */
711 const struct btrace_config *
712 linux_btrace_conf (const struct btrace_target_info *tinfo)
717 #else /* !HAVE_LINUX_PERF_EVENT_H */
719 /* See linux-btrace.h. */
722 linux_supports_btrace (struct target_ops *ops, enum btrace_format format)
727 /* See linux-btrace.h. */
729 struct btrace_target_info *
730 linux_enable_btrace (ptid_t ptid, const struct btrace_config *conf)
735 /* See linux-btrace.h. */
738 linux_disable_btrace (struct btrace_target_info *tinfo)
740 return BTRACE_ERR_NOT_SUPPORTED;
743 /* See linux-btrace.h. */
746 linux_read_btrace (struct btrace_data *btrace,
747 struct btrace_target_info *tinfo,
748 enum btrace_read_type type)
750 return BTRACE_ERR_NOT_SUPPORTED;
753 /* See linux-btrace.h. */
755 const struct btrace_config *
756 linux_btrace_conf (const struct btrace_target_info *tinfo)
761 #endif /* !HAVE_LINUX_PERF_EVENT_H */