1 /* Linux-dependent part of branch trace support for GDB, and GDBserver.
3 Copyright (C) 2013-2014 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/>. */
28 #include "linux-btrace.h"
29 #include "gdb_assert.h"
31 #include "gdbthread.h"
33 #include "i386-cpuid.h"
35 #ifdef HAVE_SYS_SYSCALL_H
36 #include <sys/syscall.h>
39 #if HAVE_LINUX_PERF_EVENT_H && defined(SYS_perf_event_open)
47 #include <sys/ptrace.h>
48 #include <sys/types.h>
51 /* A branch trace record in perf_event. */
54 /* The linear address of the branch source. */
57 /* The linear address of the branch destination. */
61 /* A perf_event branch trace sample. */
62 struct perf_event_sample
64 /* The perf_event sample header. */
65 struct perf_event_header header
;
67 /* The perf_event branch tracing payload. */
68 struct perf_event_bts bts
;
71 /* Get the perf_event header. */
73 static inline volatile struct perf_event_mmap_page
*
74 perf_event_header (struct btrace_target_info
* tinfo
)
79 /* Get the size of the perf_event mmap buffer. */
82 perf_event_mmap_size (const struct btrace_target_info
*tinfo
)
84 /* The branch trace buffer is preceded by a configuration page. */
85 return (tinfo
->size
+ 1) * PAGE_SIZE
;
88 /* Get the size of the perf_event buffer. */
91 perf_event_buffer_size (struct btrace_target_info
* tinfo
)
93 return tinfo
->size
* PAGE_SIZE
;
96 /* Get the start address of the perf_event buffer. */
98 static inline const uint8_t *
99 perf_event_buffer_begin (struct btrace_target_info
* tinfo
)
101 return ((const uint8_t *) tinfo
->buffer
) + PAGE_SIZE
;
104 /* Get the end address of the perf_event buffer. */
106 static inline const uint8_t *
107 perf_event_buffer_end (struct btrace_target_info
* tinfo
)
109 return perf_event_buffer_begin (tinfo
) + perf_event_buffer_size (tinfo
);
112 /* Check whether an address is in the kernel. */
115 perf_event_is_kernel_addr (const struct btrace_target_info
*tinfo
,
120 /* If we don't know the size of a pointer, we can't check. Let's assume it's
121 not a kernel address in this case. */
122 if (tinfo
->ptr_bits
== 0)
125 /* A bit mask for the most significant bit in an address. */
126 mask
= (uint64_t) 1 << (tinfo
->ptr_bits
- 1);
128 /* Check whether the most significant bit in the address is set. */
129 return (addr
& mask
) != 0;
132 /* Check whether a perf event record should be skipped. */
135 perf_event_skip_record (const struct btrace_target_info
*tinfo
,
136 const struct perf_event_bts
*bts
)
138 /* The hardware may report branches from kernel into user space. Branches
139 from user into kernel space will be suppressed. We filter the former to
140 provide a consistent branch trace excluding kernel. */
141 return perf_event_is_kernel_addr (tinfo
, bts
->from
);
144 /* Perform a few consistency checks on a perf event sample record. This is
145 meant to catch cases when we get out of sync with the perf event stream. */
148 perf_event_sample_ok (const struct perf_event_sample
*sample
)
150 if (sample
->header
.type
!= PERF_RECORD_SAMPLE
)
153 if (sample
->header
.size
!= sizeof (*sample
))
159 /* Branch trace is collected in a circular buffer [begin; end) as pairs of from
160 and to addresses (plus a header).
162 Start points into that buffer at the next sample position.
163 We read the collected samples backwards from start.
165 While reading the samples, we convert the information into a list of blocks.
166 For two adjacent samples s1 and s2, we form a block b such that b.begin =
167 s1.to and b.end = s2.from.
169 In case the buffer overflows during sampling, one sample may have its lower
170 part at the end and its upper part at the beginning of the buffer. */
172 static VEC (btrace_block_s
) *
173 perf_event_read_bts (struct btrace_target_info
* tinfo
, const uint8_t *begin
,
174 const uint8_t *end
, const uint8_t *start
, size_t size
)
176 VEC (btrace_block_s
) *btrace
= NULL
;
177 struct perf_event_sample sample
;
179 struct btrace_block block
= { 0, 0 };
180 struct regcache
*regcache
;
182 gdb_assert (begin
<= start
);
183 gdb_assert (start
<= end
);
185 /* The first block ends at the current pc. */
187 regcache
= get_thread_regcache (find_thread_ptid (tinfo
->ptid
), 1);
189 regcache
= get_thread_regcache (tinfo
->ptid
);
191 block
.end
= regcache_read_pc (regcache
);
193 /* The buffer may contain a partial record as its last entry (i.e. when the
194 buffer size is not a multiple of the sample size). */
195 read
= sizeof (sample
) - 1;
197 for (; read
< size
; read
+= sizeof (sample
))
199 const struct perf_event_sample
*psample
;
201 /* Find the next perf_event sample in a backwards traversal. */
202 start
-= sizeof (sample
);
204 /* If we're still inside the buffer, we're done. */
206 psample
= (const struct perf_event_sample
*) start
;
211 /* We're to the left of the ring buffer, we will wrap around and
212 reappear at the very right of the ring buffer. */
214 missing
= (begin
- start
);
215 start
= (end
- missing
);
217 /* If the entire sample is missing, we're done. */
218 if (missing
== sizeof (sample
))
219 psample
= (const struct perf_event_sample
*) start
;
224 /* The sample wrapped around. The lower part is at the end and
225 the upper part is at the beginning of the buffer. */
226 stack
= (uint8_t *) &sample
;
228 /* Copy the two parts so we have a contiguous sample. */
229 memcpy (stack
, start
, missing
);
230 memcpy (stack
+ missing
, begin
, sizeof (sample
) - missing
);
236 if (!perf_event_sample_ok (psample
))
238 warning (_("Branch trace may be incomplete."));
242 if (perf_event_skip_record (tinfo
, &psample
->bts
))
245 /* We found a valid sample, so we can complete the current block. */
246 block
.begin
= psample
->bts
.to
;
248 VEC_safe_push (btrace_block_s
, btrace
, &block
);
250 /* Start the next block. */
251 block
.end
= psample
->bts
.from
;
254 /* Push the last block (i.e. the first one of inferior execution), as well.
255 We don't know where it ends, but we know where it starts. If we're
256 reading delta trace, we can fill in the start address later on.
257 Otherwise we will prune it. */
259 VEC_safe_push (btrace_block_s
, btrace
, &block
);
264 /* Check whether the kernel supports branch tracing. */
267 kernel_supports_btrace (void)
269 struct perf_event_attr attr
;
278 warning (_("test branch tracing: cannot fork: %s."), strerror (errno
));
282 status
= ptrace (PTRACE_TRACEME
, 0, NULL
, NULL
);
285 warning (_("test branch tracing: cannot PTRACE_TRACEME: %s."),
290 status
= raise (SIGTRAP
);
293 warning (_("test branch tracing: cannot raise SIGTRAP: %s."),
301 pid
= waitpid (child
, &status
, 0);
304 warning (_("test branch tracing: bad pid %ld, error: %s."),
305 (long) pid
, strerror (errno
));
309 if (!WIFSTOPPED (status
))
311 warning (_("test branch tracing: expected stop. status: %d."),
316 memset (&attr
, 0, sizeof (attr
));
318 attr
.type
= PERF_TYPE_HARDWARE
;
319 attr
.config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
;
320 attr
.sample_period
= 1;
321 attr
.sample_type
= PERF_SAMPLE_IP
| PERF_SAMPLE_ADDR
;
322 attr
.exclude_kernel
= 1;
324 attr
.exclude_idle
= 1;
326 file
= syscall (SYS_perf_event_open
, &attr
, child
, -1, -1, 0);
330 kill (child
, SIGKILL
);
331 ptrace (PTRACE_KILL
, child
, NULL
, NULL
);
333 pid
= waitpid (child
, &status
, 0);
336 warning (_("test branch tracing: bad pid %ld, error: %s."),
337 (long) pid
, strerror (errno
));
338 if (!WIFSIGNALED (status
))
339 warning (_("test branch tracing: expected killed. status: %d."),
347 /* Check whether an Intel cpu supports branch tracing. */
350 intel_supports_btrace (void)
352 unsigned int cpuid
, model
, family
;
354 if (!i386_cpuid (1, &cpuid
, NULL
, NULL
, NULL
))
357 family
= (cpuid
>> 8) & 0xf;
358 model
= (cpuid
>> 4) & 0xf;
363 model
+= (cpuid
>> 12) & 0xf0;
367 case 0x1a: /* Nehalem */
371 case 0x25: /* Westmere */
374 case 0x2a: /* Sandy Bridge */
376 case 0x3a: /* Ivy Bridge */
378 /* AAJ122: LBR, BTM, or BTS records may have incorrect branch
379 "from" information afer an EIST transition, T-states, C1E, or
380 Adaptive Thermal Throttling. */
388 /* Check whether the cpu supports branch tracing. */
391 cpu_supports_btrace (void)
393 unsigned int ebx
, ecx
, edx
;
395 if (!i386_cpuid (0, NULL
, &ebx
, &ecx
, &edx
))
398 if (ebx
== signature_INTEL_ebx
&& ecx
== signature_INTEL_ecx
399 && edx
== signature_INTEL_edx
)
400 return intel_supports_btrace ();
402 /* Don't know about others. Let's assume they do. */
406 /* See linux-btrace.h. */
409 linux_supports_btrace (struct target_ops
*ops
)
415 if (!kernel_supports_btrace ())
417 else if (!cpu_supports_btrace ())
426 /* See linux-btrace.h. */
428 struct btrace_target_info
*
429 linux_enable_btrace (ptid_t ptid
)
431 struct btrace_target_info
*tinfo
;
434 tinfo
= xzalloc (sizeof (*tinfo
));
437 tinfo
->attr
.size
= sizeof (tinfo
->attr
);
438 tinfo
->attr
.type
= PERF_TYPE_HARDWARE
;
439 tinfo
->attr
.config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
;
440 tinfo
->attr
.sample_period
= 1;
442 /* We sample from and to address. */
443 tinfo
->attr
.sample_type
= PERF_SAMPLE_IP
| PERF_SAMPLE_ADDR
;
445 tinfo
->attr
.exclude_kernel
= 1;
446 tinfo
->attr
.exclude_hv
= 1;
447 tinfo
->attr
.exclude_idle
= 1;
451 pid
= ptid_get_lwp (ptid
);
453 pid
= ptid_get_pid (ptid
);
456 tinfo
->file
= syscall (SYS_perf_event_open
, &tinfo
->attr
, pid
, -1, -1, 0);
460 /* We try to allocate as much buffer as we can get.
461 We could allow the user to specify the size of the buffer, but then
462 we'd leave this search for the maximum buffer size to him. */
463 for (pg
= 4; pg
>= 0; --pg
)
465 /* The number of pages we request needs to be a power of two. */
466 tinfo
->size
= 1 << pg
;
467 tinfo
->buffer
= mmap (NULL
, perf_event_mmap_size (tinfo
),
468 PROT_READ
, MAP_SHARED
, tinfo
->file
, 0);
469 if (tinfo
->buffer
== MAP_FAILED
)
475 /* We were not able to allocate any buffer. */
483 /* See linux-btrace.h. */
486 linux_disable_btrace (struct btrace_target_info
*tinfo
)
491 errcode
= munmap (tinfo
->buffer
, perf_event_mmap_size (tinfo
));
493 return BTRACE_ERR_UNKNOWN
;
498 return BTRACE_ERR_NONE
;
501 /* Check whether the branch trace has changed. */
504 linux_btrace_has_changed (struct btrace_target_info
*tinfo
)
506 volatile struct perf_event_mmap_page
*header
= perf_event_header (tinfo
);
508 return header
->data_head
!= tinfo
->data_head
;
511 /* See linux-btrace.h. */
514 linux_read_btrace (VEC (btrace_block_s
) **btrace
,
515 struct btrace_target_info
*tinfo
,
516 enum btrace_read_type type
)
518 volatile struct perf_event_mmap_page
*header
;
519 const uint8_t *begin
, *end
, *start
;
520 unsigned long data_head
, data_tail
, retries
= 5;
521 size_t buffer_size
, size
;
523 /* For delta reads, we return at least the partial last block containing
525 if (type
== BTRACE_READ_NEW
&& !linux_btrace_has_changed (tinfo
))
526 return BTRACE_ERR_NONE
;
528 header
= perf_event_header (tinfo
);
529 buffer_size
= perf_event_buffer_size (tinfo
);
530 data_tail
= tinfo
->data_head
;
532 /* We may need to retry reading the trace. See below. */
535 data_head
= header
->data_head
;
537 /* Delete any leftover trace from the previous iteration. */
538 VEC_free (btrace_block_s
, *btrace
);
540 if (type
== BTRACE_READ_DELTA
)
542 /* Determine the number of bytes to read and check for buffer
545 /* Check for data head overflows. We might be able to recover from
546 those but they are very unlikely and it's not really worth the
548 if (data_head
< data_tail
)
549 return BTRACE_ERR_OVERFLOW
;
551 /* If the buffer is smaller than the trace delta, we overflowed. */
552 size
= data_head
- data_tail
;
553 if (buffer_size
< size
)
554 return BTRACE_ERR_OVERFLOW
;
558 /* Read the entire buffer. */
561 /* Adjust the size if the buffer has not overflowed, yet. */
562 if (data_head
< size
)
566 /* Data_head keeps growing; the buffer itself is circular. */
567 begin
= perf_event_buffer_begin (tinfo
);
568 start
= begin
+ data_head
% buffer_size
;
570 if (data_head
<= buffer_size
)
573 end
= perf_event_buffer_end (tinfo
);
575 *btrace
= perf_event_read_bts (tinfo
, begin
, end
, start
, size
);
577 /* The stopping thread notifies its ptracer before it is scheduled out.
578 On multi-core systems, the debugger might therefore run while the
579 kernel might be writing the last branch trace records.
581 Let's check whether the data head moved while we read the trace. */
582 if (data_head
== header
->data_head
)
586 tinfo
->data_head
= data_head
;
588 /* Prune the incomplete last block (i.e. the first one of inferior execution)
589 if we're not doing a delta read. There is no way of filling in its zeroed
591 if (!VEC_empty (btrace_block_s
, *btrace
) && type
!= BTRACE_READ_DELTA
)
592 VEC_pop (btrace_block_s
, *btrace
);
594 return BTRACE_ERR_NONE
;
597 #else /* !HAVE_LINUX_PERF_EVENT_H */
599 /* See linux-btrace.h. */
602 linux_supports_btrace (struct target_ops
*ops
)
607 /* See linux-btrace.h. */
609 struct btrace_target_info
*
610 linux_enable_btrace (ptid_t ptid
)
615 /* See linux-btrace.h. */
618 linux_disable_btrace (struct btrace_target_info
*tinfo
)
620 return BTRACE_ERR_NOT_SUPPORTED
;
623 /* See linux-btrace.h. */
626 linux_read_btrace (VEC (btrace_block_s
) **btrace
,
627 struct btrace_target_info
*tinfo
,
628 enum btrace_read_type type
)
630 return BTRACE_ERR_NOT_SUPPORTED
;
633 #endif /* !HAVE_LINUX_PERF_EVENT_H */