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_bts
*conf
)
403 struct perf_event_mmap_page
*header
;
404 struct btrace_target_info
*tinfo
;
405 struct btrace_tinfo_bts
*bts
;
406 unsigned long long size
, pages
;
409 tinfo
= xzalloc (sizeof (*tinfo
));
413 tinfo
->conf
.format
= BTRACE_FORMAT_BTS
;
414 bts
= &tinfo
->variant
.bts
;
416 bts
->attr
.size
= sizeof (bts
->attr
);
417 bts
->attr
.type
= PERF_TYPE_HARDWARE
;
418 bts
->attr
.config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
;
419 bts
->attr
.sample_period
= 1;
421 /* We sample from and to address. */
422 bts
->attr
.sample_type
= PERF_SAMPLE_IP
| PERF_SAMPLE_ADDR
;
424 bts
->attr
.exclude_kernel
= 1;
425 bts
->attr
.exclude_hv
= 1;
426 bts
->attr
.exclude_idle
= 1;
428 pid
= ptid_get_lwp (ptid
);
430 pid
= ptid_get_pid (ptid
);
433 bts
->file
= syscall (SYS_perf_event_open
, &bts
->attr
, pid
, -1, -1, 0);
437 /* Convert the requested size in bytes to pages (rounding up). */
438 pages
= (((unsigned long long) conf
->size
) + PAGE_SIZE
- 1) / PAGE_SIZE
;
439 /* We need at least one page. */
443 /* The buffer size can be requested in powers of two pages. Adjust PAGES
444 to the next power of two. */
445 for (pg
= 0; pages
!= (1u << pg
); ++pg
)
446 if ((pages
& (1u << pg
)) != 0)
449 /* We try to allocate the requested size.
450 If that fails, try to get as much as we can. */
451 for (; pages
> 0; pages
>>= 1)
455 size
= pages
* PAGE_SIZE
;
456 length
= size
+ PAGE_SIZE
;
458 /* Check for overflows. */
459 if ((unsigned long long) length
< size
)
462 /* The number of pages we request needs to be a power of two. */
463 header
= mmap (NULL
, length
, PROT_READ
, MAP_SHARED
, bts
->file
, 0);
464 if (header
!= MAP_FAILED
)
468 if (header
== MAP_FAILED
)
471 bts
->header
= header
;
472 bts
->bts
.mem
= ((const uint8_t *) header
) + PAGE_SIZE
;
473 bts
->bts
.size
= size
;
474 bts
->bts
.data_head
= &header
->data_head
;
475 bts
->bts
.last_head
= 0;
477 tinfo
->conf
.bts
.size
= size
;
481 /* We were not able to allocate any buffer. */
489 /* See linux-btrace.h. */
491 struct btrace_target_info
*
492 linux_enable_btrace (ptid_t ptid
, const struct btrace_config
*conf
)
494 struct btrace_target_info
*tinfo
;
497 switch (conf
->format
)
499 case BTRACE_FORMAT_NONE
:
502 case BTRACE_FORMAT_BTS
:
503 tinfo
= linux_enable_bts (ptid
, &conf
->bts
);
510 /* Disable BTS tracing. */
512 static enum btrace_error
513 linux_disable_bts (struct btrace_tinfo_bts
*tinfo
)
515 munmap((void *) tinfo
->header
, tinfo
->bts
.size
+ PAGE_SIZE
);
518 return BTRACE_ERR_NONE
;
521 /* See linux-btrace.h. */
524 linux_disable_btrace (struct btrace_target_info
*tinfo
)
526 enum btrace_error errcode
;
528 errcode
= BTRACE_ERR_NOT_SUPPORTED
;
529 switch (tinfo
->conf
.format
)
531 case BTRACE_FORMAT_NONE
:
534 case BTRACE_FORMAT_BTS
:
535 errcode
= linux_disable_bts (&tinfo
->variant
.bts
);
539 if (errcode
== BTRACE_ERR_NONE
)
545 /* Read branch trace data in BTS format for the thread given by TINFO into
546 BTRACE using the TYPE reading method. */
548 static enum btrace_error
549 linux_read_bts (struct btrace_data_bts
*btrace
,
550 struct btrace_target_info
*tinfo
,
551 enum btrace_read_type type
)
553 struct perf_event_buffer
*pevent
;
554 const uint8_t *begin
, *end
, *start
;
555 unsigned long long data_head
, data_tail
, buffer_size
, size
;
556 unsigned int retries
= 5;
558 pevent
= &tinfo
->variant
.bts
.bts
;
560 /* For delta reads, we return at least the partial last block containing
562 if (type
== BTRACE_READ_NEW
&& !perf_event_new_data (pevent
))
563 return BTRACE_ERR_NONE
;
565 buffer_size
= pevent
->size
;
566 data_tail
= pevent
->last_head
;
568 /* We may need to retry reading the trace. See below. */
571 data_head
= *pevent
->data_head
;
573 /* Delete any leftover trace from the previous iteration. */
574 VEC_free (btrace_block_s
, btrace
->blocks
);
576 if (type
== BTRACE_READ_DELTA
)
578 /* Determine the number of bytes to read and check for buffer
581 /* Check for data head overflows. We might be able to recover from
582 those but they are very unlikely and it's not really worth the
584 if (data_head
< data_tail
)
585 return BTRACE_ERR_OVERFLOW
;
587 /* If the buffer is smaller than the trace delta, we overflowed. */
588 size
= data_head
- data_tail
;
589 if (buffer_size
< size
)
590 return BTRACE_ERR_OVERFLOW
;
594 /* Read the entire buffer. */
597 /* Adjust the size if the buffer has not overflowed, yet. */
598 if (data_head
< size
)
602 /* Data_head keeps growing; the buffer itself is circular. */
604 start
= begin
+ data_head
% buffer_size
;
606 if (data_head
<= buffer_size
)
609 end
= begin
+ pevent
->size
;
611 btrace
->blocks
= perf_event_read_bts (tinfo
, begin
, end
, start
, size
);
613 /* The stopping thread notifies its ptracer before it is scheduled out.
614 On multi-core systems, the debugger might therefore run while the
615 kernel might be writing the last branch trace records.
617 Let's check whether the data head moved while we read the trace. */
618 if (data_head
== *pevent
->data_head
)
622 pevent
->last_head
= data_head
;
624 /* Prune the incomplete last block (i.e. the first one of inferior execution)
625 if we're not doing a delta read. There is no way of filling in its zeroed
627 if (!VEC_empty (btrace_block_s
, btrace
->blocks
)
628 && type
!= BTRACE_READ_DELTA
)
629 VEC_pop (btrace_block_s
, btrace
->blocks
);
631 return BTRACE_ERR_NONE
;
634 /* See linux-btrace.h. */
637 linux_read_btrace (struct btrace_data
*btrace
,
638 struct btrace_target_info
*tinfo
,
639 enum btrace_read_type type
)
641 switch (tinfo
->conf
.format
)
643 case BTRACE_FORMAT_NONE
:
644 return BTRACE_ERR_NOT_SUPPORTED
;
646 case BTRACE_FORMAT_BTS
:
647 /* We read btrace in BTS format. */
648 btrace
->format
= BTRACE_FORMAT_BTS
;
649 btrace
->variant
.bts
.blocks
= NULL
;
651 return linux_read_bts (&btrace
->variant
.bts
, tinfo
, type
);
654 internal_error (__FILE__
, __LINE__
, _("Unkown branch trace format."));
657 /* See linux-btrace.h. */
659 const struct btrace_config
*
660 linux_btrace_conf (const struct btrace_target_info
*tinfo
)
665 #else /* !HAVE_LINUX_PERF_EVENT_H */
667 /* See linux-btrace.h. */
670 linux_supports_btrace (struct target_ops
*ops
, enum btrace_format format
)
675 /* See linux-btrace.h. */
677 struct btrace_target_info
*
678 linux_enable_btrace (ptid_t ptid
, const struct btrace_config
*conf
)
683 /* See linux-btrace.h. */
686 linux_disable_btrace (struct btrace_target_info
*tinfo
)
688 return BTRACE_ERR_NOT_SUPPORTED
;
691 /* See linux-btrace.h. */
694 linux_read_btrace (struct btrace_data
*btrace
,
695 struct btrace_target_info
*tinfo
,
696 enum btrace_read_type type
)
698 return BTRACE_ERR_NOT_SUPPORTED
;
701 /* See linux-btrace.h. */
703 const struct btrace_config
*
704 linux_btrace_conf (const struct btrace_target_info
*tinfo
)
709 #endif /* !HAVE_LINUX_PERF_EVENT_H */