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 /* Get the perf_event header. */
64 static inline volatile struct perf_event_mmap_page
*
65 perf_event_header (struct btrace_target_info
* tinfo
)
70 /* Get the size of the perf_event mmap buffer. */
73 perf_event_mmap_size (const struct btrace_target_info
*tinfo
)
75 /* The branch trace buffer is preceded by a configuration page. */
76 return (tinfo
->size
+ 1) * PAGE_SIZE
;
79 /* Get the size of the perf_event buffer. */
82 perf_event_buffer_size (struct btrace_target_info
* tinfo
)
84 return tinfo
->size
* PAGE_SIZE
;
87 /* Get the start address of the perf_event buffer. */
89 static inline const uint8_t *
90 perf_event_buffer_begin (struct btrace_target_info
* tinfo
)
92 return ((const uint8_t *) tinfo
->buffer
) + PAGE_SIZE
;
95 /* Get the end address of the perf_event buffer. */
97 static inline const uint8_t *
98 perf_event_buffer_end (struct btrace_target_info
* tinfo
)
100 return perf_event_buffer_begin (tinfo
) + perf_event_buffer_size (tinfo
);
103 /* Check whether an address is in the kernel. */
106 perf_event_is_kernel_addr (const struct btrace_target_info
*tinfo
,
111 /* If we don't know the size of a pointer, we can't check. Let's assume it's
112 not a kernel address in this case. */
113 if (tinfo
->ptr_bits
== 0)
116 /* A bit mask for the most significant bit in an address. */
117 mask
= (uint64_t) 1 << (tinfo
->ptr_bits
- 1);
119 /* Check whether the most significant bit in the address is set. */
120 return (addr
& mask
) != 0;
123 /* Check whether a perf event record should be skipped. */
126 perf_event_skip_record (const struct btrace_target_info
*tinfo
,
127 const struct perf_event_bts
*bts
)
129 /* The hardware may report branches from kernel into user space. Branches
130 from user into kernel space will be suppressed. We filter the former to
131 provide a consistent branch trace excluding kernel. */
132 return perf_event_is_kernel_addr (tinfo
, bts
->from
);
135 /* Perform a few consistency checks on a perf event sample record. This is
136 meant to catch cases when we get out of sync with the perf event stream. */
139 perf_event_sample_ok (const struct perf_event_sample
*sample
)
141 if (sample
->header
.type
!= PERF_RECORD_SAMPLE
)
144 if (sample
->header
.size
!= sizeof (*sample
))
150 /* Branch trace is collected in a circular buffer [begin; end) as pairs of from
151 and to addresses (plus a header).
153 Start points into that buffer at the next sample position.
154 We read the collected samples backwards from start.
156 While reading the samples, we convert the information into a list of blocks.
157 For two adjacent samples s1 and s2, we form a block b such that b.begin =
158 s1.to and b.end = s2.from.
160 In case the buffer overflows during sampling, one sample may have its lower
161 part at the end and its upper part at the beginning of the buffer. */
163 static VEC (btrace_block_s
) *
164 perf_event_read_bts (struct btrace_target_info
* tinfo
, const uint8_t *begin
,
165 const uint8_t *end
, const uint8_t *start
, size_t size
)
167 VEC (btrace_block_s
) *btrace
= NULL
;
168 struct perf_event_sample sample
;
170 struct btrace_block block
= { 0, 0 };
171 struct regcache
*regcache
;
173 gdb_assert (begin
<= start
);
174 gdb_assert (start
<= end
);
176 /* The first block ends at the current pc. */
177 regcache
= get_thread_regcache_for_ptid (tinfo
->ptid
);
178 block
.end
= regcache_read_pc (regcache
);
180 /* The buffer may contain a partial record as its last entry (i.e. when the
181 buffer size is not a multiple of the sample size). */
182 read
= sizeof (sample
) - 1;
184 for (; read
< size
; read
+= sizeof (sample
))
186 const struct perf_event_sample
*psample
;
188 /* Find the next perf_event sample in a backwards traversal. */
189 start
-= sizeof (sample
);
191 /* If we're still inside the buffer, we're done. */
193 psample
= (const struct perf_event_sample
*) start
;
198 /* We're to the left of the ring buffer, we will wrap around and
199 reappear at the very right of the ring buffer. */
201 missing
= (begin
- start
);
202 start
= (end
- missing
);
204 /* If the entire sample is missing, we're done. */
205 if (missing
== sizeof (sample
))
206 psample
= (const struct perf_event_sample
*) start
;
211 /* The sample wrapped around. The lower part is at the end and
212 the upper part is at the beginning of the buffer. */
213 stack
= (uint8_t *) &sample
;
215 /* Copy the two parts so we have a contiguous sample. */
216 memcpy (stack
, start
, missing
);
217 memcpy (stack
+ missing
, begin
, sizeof (sample
) - missing
);
223 if (!perf_event_sample_ok (psample
))
225 warning (_("Branch trace may be incomplete."));
229 if (perf_event_skip_record (tinfo
, &psample
->bts
))
232 /* We found a valid sample, so we can complete the current block. */
233 block
.begin
= psample
->bts
.to
;
235 VEC_safe_push (btrace_block_s
, btrace
, &block
);
237 /* Start the next block. */
238 block
.end
= psample
->bts
.from
;
241 /* Push the last block (i.e. the first one of inferior execution), as well.
242 We don't know where it ends, but we know where it starts. If we're
243 reading delta trace, we can fill in the start address later on.
244 Otherwise we will prune it. */
246 VEC_safe_push (btrace_block_s
, btrace
, &block
);
251 /* Check whether the kernel supports BTS. */
254 kernel_supports_bts (void)
256 struct perf_event_attr attr
;
265 warning (_("test bts: cannot fork: %s."), strerror (errno
));
269 status
= ptrace (PTRACE_TRACEME
, 0, NULL
, NULL
);
272 warning (_("test bts: cannot PTRACE_TRACEME: %s."),
277 status
= raise (SIGTRAP
);
280 warning (_("test bts: cannot raise SIGTRAP: %s."),
288 pid
= waitpid (child
, &status
, 0);
291 warning (_("test bts: bad pid %ld, error: %s."),
292 (long) pid
, strerror (errno
));
296 if (!WIFSTOPPED (status
))
298 warning (_("test bts: expected stop. status: %d."),
303 memset (&attr
, 0, sizeof (attr
));
305 attr
.type
= PERF_TYPE_HARDWARE
;
306 attr
.config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
;
307 attr
.sample_period
= 1;
308 attr
.sample_type
= PERF_SAMPLE_IP
| PERF_SAMPLE_ADDR
;
309 attr
.exclude_kernel
= 1;
311 attr
.exclude_idle
= 1;
313 file
= syscall (SYS_perf_event_open
, &attr
, child
, -1, -1, 0);
317 kill (child
, SIGKILL
);
318 ptrace (PTRACE_KILL
, child
, NULL
, NULL
);
320 pid
= waitpid (child
, &status
, 0);
323 warning (_("test bts: bad pid %ld, error: %s."),
324 (long) pid
, strerror (errno
));
325 if (!WIFSIGNALED (status
))
326 warning (_("test bts: expected killed. status: %d."),
334 /* Check whether an Intel cpu supports BTS. */
337 intel_supports_bts (void)
339 unsigned int cpuid
, model
, family
;
341 if (!x86_cpuid (1, &cpuid
, NULL
, NULL
, NULL
))
344 family
= (cpuid
>> 8) & 0xf;
345 model
= (cpuid
>> 4) & 0xf;
350 model
+= (cpuid
>> 12) & 0xf0;
354 case 0x1a: /* Nehalem */
358 case 0x25: /* Westmere */
361 case 0x2a: /* Sandy Bridge */
363 case 0x3a: /* Ivy Bridge */
365 /* AAJ122: LBR, BTM, or BTS records may have incorrect branch
366 "from" information afer an EIST transition, T-states, C1E, or
367 Adaptive Thermal Throttling. */
375 /* Check whether the cpu supports BTS. */
378 cpu_supports_bts (void)
380 unsigned int ebx
, ecx
, edx
;
382 if (!x86_cpuid (0, NULL
, &ebx
, &ecx
, &edx
))
385 if (ebx
== signature_INTEL_ebx
&& ecx
== signature_INTEL_ecx
386 && edx
== signature_INTEL_edx
)
387 return intel_supports_bts ();
389 /* Don't know about others. Let's assume they do. */
393 /* Check whether the linux target supports BTS. */
396 linux_supports_bts (void)
402 if (!kernel_supports_bts ())
404 else if (!cpu_supports_bts ())
413 /* See linux-btrace.h. */
416 linux_supports_btrace (struct target_ops
*ops
, enum btrace_format format
)
420 case BTRACE_FORMAT_NONE
:
423 case BTRACE_FORMAT_BTS
:
424 return linux_supports_bts ();
427 internal_error (__FILE__
, __LINE__
, _("Unknown branch trace format"));
430 /* See linux-btrace.h. */
432 struct btrace_target_info
*
433 linux_enable_btrace (ptid_t ptid
)
435 struct btrace_target_info
*tinfo
;
438 tinfo
= xzalloc (sizeof (*tinfo
));
441 tinfo
->attr
.size
= sizeof (tinfo
->attr
);
442 tinfo
->attr
.type
= PERF_TYPE_HARDWARE
;
443 tinfo
->attr
.config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
;
444 tinfo
->attr
.sample_period
= 1;
446 /* We sample from and to address. */
447 tinfo
->attr
.sample_type
= PERF_SAMPLE_IP
| PERF_SAMPLE_ADDR
;
449 tinfo
->attr
.exclude_kernel
= 1;
450 tinfo
->attr
.exclude_hv
= 1;
451 tinfo
->attr
.exclude_idle
= 1;
455 pid
= ptid_get_lwp (ptid
);
457 pid
= ptid_get_pid (ptid
);
460 tinfo
->file
= syscall (SYS_perf_event_open
, &tinfo
->attr
, pid
, -1, -1, 0);
464 /* We try to allocate as much buffer as we can get.
465 We could allow the user to specify the size of the buffer, but then
466 we'd leave this search for the maximum buffer size to him. */
467 for (pg
= 4; pg
>= 0; --pg
)
469 /* The number of pages we request needs to be a power of two. */
470 tinfo
->size
= 1 << pg
;
471 tinfo
->buffer
= mmap (NULL
, perf_event_mmap_size (tinfo
),
472 PROT_READ
, MAP_SHARED
, tinfo
->file
, 0);
473 if (tinfo
->buffer
== MAP_FAILED
)
479 /* We were not able to allocate any buffer. */
487 /* See linux-btrace.h. */
490 linux_disable_btrace (struct btrace_target_info
*tinfo
)
495 errcode
= munmap (tinfo
->buffer
, perf_event_mmap_size (tinfo
));
497 return BTRACE_ERR_UNKNOWN
;
502 return BTRACE_ERR_NONE
;
505 /* Check whether the branch trace has changed. */
508 linux_btrace_has_changed (struct btrace_target_info
*tinfo
)
510 volatile struct perf_event_mmap_page
*header
= perf_event_header (tinfo
);
512 return header
->data_head
!= tinfo
->data_head
;
515 /* Read branch trace data in BTS format for the thread given by TINFO into
516 BTRACE using the TYPE reading method. */
518 static enum btrace_error
519 linux_read_bts (struct btrace_data_bts
*btrace
,
520 struct btrace_target_info
*tinfo
,
521 enum btrace_read_type type
)
523 volatile struct perf_event_mmap_page
*header
;
524 const uint8_t *begin
, *end
, *start
;
525 unsigned long data_head
, data_tail
, retries
= 5;
526 size_t buffer_size
, size
;
528 /* For delta reads, we return at least the partial last block containing
530 if (type
== BTRACE_READ_NEW
&& !linux_btrace_has_changed (tinfo
))
531 return BTRACE_ERR_NONE
;
533 header
= perf_event_header (tinfo
);
534 buffer_size
= perf_event_buffer_size (tinfo
);
535 data_tail
= tinfo
->data_head
;
537 /* We may need to retry reading the trace. See below. */
540 data_head
= header
->data_head
;
542 /* Delete any leftover trace from the previous iteration. */
543 VEC_free (btrace_block_s
, btrace
->blocks
);
545 if (type
== BTRACE_READ_DELTA
)
547 /* Determine the number of bytes to read and check for buffer
550 /* Check for data head overflows. We might be able to recover from
551 those but they are very unlikely and it's not really worth the
553 if (data_head
< data_tail
)
554 return BTRACE_ERR_OVERFLOW
;
556 /* If the buffer is smaller than the trace delta, we overflowed. */
557 size
= data_head
- data_tail
;
558 if (buffer_size
< size
)
559 return BTRACE_ERR_OVERFLOW
;
563 /* Read the entire buffer. */
566 /* Adjust the size if the buffer has not overflowed, yet. */
567 if (data_head
< size
)
571 /* Data_head keeps growing; the buffer itself is circular. */
572 begin
= perf_event_buffer_begin (tinfo
);
573 start
= begin
+ data_head
% buffer_size
;
575 if (data_head
<= buffer_size
)
578 end
= perf_event_buffer_end (tinfo
);
580 btrace
->blocks
= perf_event_read_bts (tinfo
, begin
, end
, start
, size
);
582 /* The stopping thread notifies its ptracer before it is scheduled out.
583 On multi-core systems, the debugger might therefore run while the
584 kernel might be writing the last branch trace records.
586 Let's check whether the data head moved while we read the trace. */
587 if (data_head
== header
->data_head
)
591 tinfo
->data_head
= data_head
;
593 /* Prune the incomplete last block (i.e. the first one of inferior execution)
594 if we're not doing a delta read. There is no way of filling in its zeroed
596 if (!VEC_empty (btrace_block_s
, btrace
->blocks
)
597 && type
!= BTRACE_READ_DELTA
)
598 VEC_pop (btrace_block_s
, btrace
->blocks
);
600 return BTRACE_ERR_NONE
;
603 /* See linux-btrace.h. */
606 linux_read_btrace (struct btrace_data
*btrace
,
607 struct btrace_target_info
*tinfo
,
608 enum btrace_read_type type
)
610 /* We read btrace in BTS format. */
611 btrace
->format
= BTRACE_FORMAT_BTS
;
612 btrace
->variant
.bts
.blocks
= NULL
;
614 return linux_read_bts (&btrace
->variant
.bts
, tinfo
, type
);
617 #else /* !HAVE_LINUX_PERF_EVENT_H */
619 /* See linux-btrace.h. */
622 linux_supports_btrace (struct target_ops
*ops
, enum btrace_format format
)
627 /* See linux-btrace.h. */
629 struct btrace_target_info
*
630 linux_enable_btrace (ptid_t ptid
)
635 /* See linux-btrace.h. */
638 linux_disable_btrace (struct btrace_target_info
*tinfo
)
640 return BTRACE_ERR_NOT_SUPPORTED
;
643 /* See linux-btrace.h. */
646 linux_read_btrace (struct btrace_data
*btrace
,
647 struct btrace_target_info
*tinfo
,
648 enum btrace_read_type type
)
650 return BTRACE_ERR_NOT_SUPPORTED
;
653 #endif /* !HAVE_LINUX_PERF_EVENT_H */