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"
30 #include "gdbthread.h"
32 #include "i386-cpuid.h"
34 #ifdef HAVE_SYS_SYSCALL_H
35 #include <sys/syscall.h>
38 #if HAVE_LINUX_PERF_EVENT_H && defined(SYS_perf_event_open)
46 #include <sys/ptrace.h>
47 #include <sys/types.h>
50 /* A branch trace record in perf_event. */
53 /* The linear address of the branch source. */
56 /* The linear address of the branch destination. */
60 /* A perf_event branch trace sample. */
61 struct perf_event_sample
63 /* The perf_event sample header. */
64 struct perf_event_header header
;
66 /* The perf_event branch tracing payload. */
67 struct perf_event_bts bts
;
70 /* Get the perf_event header. */
72 static inline volatile struct perf_event_mmap_page
*
73 perf_event_header (struct btrace_target_info
* tinfo
)
78 /* Get the size of the perf_event mmap buffer. */
81 perf_event_mmap_size (const struct btrace_target_info
*tinfo
)
83 /* The branch trace buffer is preceded by a configuration page. */
84 return (tinfo
->size
+ 1) * PAGE_SIZE
;
87 /* Get the size of the perf_event buffer. */
90 perf_event_buffer_size (struct btrace_target_info
* tinfo
)
92 return tinfo
->size
* PAGE_SIZE
;
95 /* Get the start address of the perf_event buffer. */
97 static inline const uint8_t *
98 perf_event_buffer_begin (struct btrace_target_info
* tinfo
)
100 return ((const uint8_t *) tinfo
->buffer
) + PAGE_SIZE
;
103 /* Get the end address of the perf_event buffer. */
105 static inline const uint8_t *
106 perf_event_buffer_end (struct btrace_target_info
* tinfo
)
108 return perf_event_buffer_begin (tinfo
) + perf_event_buffer_size (tinfo
);
111 /* Check whether an address is in the kernel. */
114 perf_event_is_kernel_addr (const struct btrace_target_info
*tinfo
,
119 /* If we don't know the size of a pointer, we can't check. Let's assume it's
120 not a kernel address in this case. */
121 if (tinfo
->ptr_bits
== 0)
124 /* A bit mask for the most significant bit in an address. */
125 mask
= (uint64_t) 1 << (tinfo
->ptr_bits
- 1);
127 /* Check whether the most significant bit in the address is set. */
128 return (addr
& mask
) != 0;
131 /* Check whether a perf event record should be skipped. */
134 perf_event_skip_record (const struct btrace_target_info
*tinfo
,
135 const struct perf_event_bts
*bts
)
137 /* The hardware may report branches from kernel into user space. Branches
138 from user into kernel space will be suppressed. We filter the former to
139 provide a consistent branch trace excluding kernel. */
140 return perf_event_is_kernel_addr (tinfo
, bts
->from
);
143 /* Perform a few consistency checks on a perf event sample record. This is
144 meant to catch cases when we get out of sync with the perf event stream. */
147 perf_event_sample_ok (const struct perf_event_sample
*sample
)
149 if (sample
->header
.type
!= PERF_RECORD_SAMPLE
)
152 if (sample
->header
.size
!= sizeof (*sample
))
158 /* Branch trace is collected in a circular buffer [begin; end) as pairs of from
159 and to addresses (plus a header).
161 Start points into that buffer at the next sample position.
162 We read the collected samples backwards from start.
164 While reading the samples, we convert the information into a list of blocks.
165 For two adjacent samples s1 and s2, we form a block b such that b.begin =
166 s1.to and b.end = s2.from.
168 In case the buffer overflows during sampling, one sample may have its lower
169 part at the end and its upper part at the beginning of the buffer. */
171 static VEC (btrace_block_s
) *
172 perf_event_read_bts (struct btrace_target_info
* tinfo
, const uint8_t *begin
,
173 const uint8_t *end
, const uint8_t *start
, size_t size
)
175 VEC (btrace_block_s
) *btrace
= NULL
;
176 struct perf_event_sample sample
;
178 struct btrace_block block
= { 0, 0 };
179 struct regcache
*regcache
;
181 gdb_assert (begin
<= start
);
182 gdb_assert (start
<= end
);
184 /* The first block ends at the current pc. */
186 regcache
= get_thread_regcache (find_thread_ptid (tinfo
->ptid
), 1);
188 regcache
= get_thread_regcache (tinfo
->ptid
);
190 block
.end
= regcache_read_pc (regcache
);
192 /* The buffer may contain a partial record as its last entry (i.e. when the
193 buffer size is not a multiple of the sample size). */
194 read
= sizeof (sample
) - 1;
196 for (; read
< size
; read
+= sizeof (sample
))
198 const struct perf_event_sample
*psample
;
200 /* Find the next perf_event sample in a backwards traversal. */
201 start
-= sizeof (sample
);
203 /* If we're still inside the buffer, we're done. */
205 psample
= (const struct perf_event_sample
*) start
;
210 /* We're to the left of the ring buffer, we will wrap around and
211 reappear at the very right of the ring buffer. */
213 missing
= (begin
- start
);
214 start
= (end
- missing
);
216 /* If the entire sample is missing, we're done. */
217 if (missing
== sizeof (sample
))
218 psample
= (const struct perf_event_sample
*) start
;
223 /* The sample wrapped around. The lower part is at the end and
224 the upper part is at the beginning of the buffer. */
225 stack
= (uint8_t *) &sample
;
227 /* Copy the two parts so we have a contiguous sample. */
228 memcpy (stack
, start
, missing
);
229 memcpy (stack
+ missing
, begin
, sizeof (sample
) - missing
);
235 if (!perf_event_sample_ok (psample
))
237 warning (_("Branch trace may be incomplete."));
241 if (perf_event_skip_record (tinfo
, &psample
->bts
))
244 /* We found a valid sample, so we can complete the current block. */
245 block
.begin
= psample
->bts
.to
;
247 VEC_safe_push (btrace_block_s
, btrace
, &block
);
249 /* Start the next block. */
250 block
.end
= psample
->bts
.from
;
253 /* Push the last block (i.e. the first one of inferior execution), as well.
254 We don't know where it ends, but we know where it starts. If we're
255 reading delta trace, we can fill in the start address later on.
256 Otherwise we will prune it. */
258 VEC_safe_push (btrace_block_s
, btrace
, &block
);
263 /* Check whether the kernel supports branch tracing. */
266 kernel_supports_btrace (void)
268 struct perf_event_attr attr
;
277 warning (_("test branch tracing: cannot fork: %s."), strerror (errno
));
281 status
= ptrace (PTRACE_TRACEME
, 0, NULL
, NULL
);
284 warning (_("test branch tracing: cannot PTRACE_TRACEME: %s."),
289 status
= raise (SIGTRAP
);
292 warning (_("test branch tracing: cannot raise SIGTRAP: %s."),
300 pid
= waitpid (child
, &status
, 0);
303 warning (_("test branch tracing: bad pid %ld, error: %s."),
304 (long) pid
, strerror (errno
));
308 if (!WIFSTOPPED (status
))
310 warning (_("test branch tracing: expected stop. status: %d."),
315 memset (&attr
, 0, sizeof (attr
));
317 attr
.type
= PERF_TYPE_HARDWARE
;
318 attr
.config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
;
319 attr
.sample_period
= 1;
320 attr
.sample_type
= PERF_SAMPLE_IP
| PERF_SAMPLE_ADDR
;
321 attr
.exclude_kernel
= 1;
323 attr
.exclude_idle
= 1;
325 file
= syscall (SYS_perf_event_open
, &attr
, child
, -1, -1, 0);
329 kill (child
, SIGKILL
);
330 ptrace (PTRACE_KILL
, child
, NULL
, NULL
);
332 pid
= waitpid (child
, &status
, 0);
335 warning (_("test branch tracing: bad pid %ld, error: %s."),
336 (long) pid
, strerror (errno
));
337 if (!WIFSIGNALED (status
))
338 warning (_("test branch tracing: expected killed. status: %d."),
346 /* Check whether an Intel cpu supports branch tracing. */
349 intel_supports_btrace (void)
351 unsigned int cpuid
, model
, family
;
353 if (!i386_cpuid (1, &cpuid
, NULL
, NULL
, NULL
))
356 family
= (cpuid
>> 8) & 0xf;
357 model
= (cpuid
>> 4) & 0xf;
362 model
+= (cpuid
>> 12) & 0xf0;
366 case 0x1a: /* Nehalem */
370 case 0x25: /* Westmere */
373 case 0x2a: /* Sandy Bridge */
375 case 0x3a: /* Ivy Bridge */
377 /* AAJ122: LBR, BTM, or BTS records may have incorrect branch
378 "from" information afer an EIST transition, T-states, C1E, or
379 Adaptive Thermal Throttling. */
387 /* Check whether the cpu supports branch tracing. */
390 cpu_supports_btrace (void)
392 unsigned int ebx
, ecx
, edx
;
394 if (!i386_cpuid (0, NULL
, &ebx
, &ecx
, &edx
))
397 if (ebx
== signature_INTEL_ebx
&& ecx
== signature_INTEL_ecx
398 && edx
== signature_INTEL_edx
)
399 return intel_supports_btrace ();
401 /* Don't know about others. Let's assume they do. */
405 /* See linux-btrace.h. */
408 linux_supports_btrace (struct target_ops
*ops
)
414 if (!kernel_supports_btrace ())
416 else if (!cpu_supports_btrace ())
425 /* See linux-btrace.h. */
427 struct btrace_target_info
*
428 linux_enable_btrace (ptid_t ptid
)
430 struct btrace_target_info
*tinfo
;
433 tinfo
= xzalloc (sizeof (*tinfo
));
436 tinfo
->attr
.size
= sizeof (tinfo
->attr
);
437 tinfo
->attr
.type
= PERF_TYPE_HARDWARE
;
438 tinfo
->attr
.config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
;
439 tinfo
->attr
.sample_period
= 1;
441 /* We sample from and to address. */
442 tinfo
->attr
.sample_type
= PERF_SAMPLE_IP
| PERF_SAMPLE_ADDR
;
444 tinfo
->attr
.exclude_kernel
= 1;
445 tinfo
->attr
.exclude_hv
= 1;
446 tinfo
->attr
.exclude_idle
= 1;
450 pid
= ptid_get_lwp (ptid
);
452 pid
= ptid_get_pid (ptid
);
455 tinfo
->file
= syscall (SYS_perf_event_open
, &tinfo
->attr
, pid
, -1, -1, 0);
459 /* We try to allocate as much buffer as we can get.
460 We could allow the user to specify the size of the buffer, but then
461 we'd leave this search for the maximum buffer size to him. */
462 for (pg
= 4; pg
>= 0; --pg
)
464 /* The number of pages we request needs to be a power of two. */
465 tinfo
->size
= 1 << pg
;
466 tinfo
->buffer
= mmap (NULL
, perf_event_mmap_size (tinfo
),
467 PROT_READ
, MAP_SHARED
, tinfo
->file
, 0);
468 if (tinfo
->buffer
== MAP_FAILED
)
474 /* We were not able to allocate any buffer. */
482 /* See linux-btrace.h. */
485 linux_disable_btrace (struct btrace_target_info
*tinfo
)
490 errcode
= munmap (tinfo
->buffer
, perf_event_mmap_size (tinfo
));
492 return BTRACE_ERR_UNKNOWN
;
497 return BTRACE_ERR_NONE
;
500 /* Check whether the branch trace has changed. */
503 linux_btrace_has_changed (struct btrace_target_info
*tinfo
)
505 volatile struct perf_event_mmap_page
*header
= perf_event_header (tinfo
);
507 return header
->data_head
!= tinfo
->data_head
;
510 /* See linux-btrace.h. */
513 linux_read_btrace (VEC (btrace_block_s
) **btrace
,
514 struct btrace_target_info
*tinfo
,
515 enum btrace_read_type type
)
517 volatile struct perf_event_mmap_page
*header
;
518 const uint8_t *begin
, *end
, *start
;
519 unsigned long data_head
, data_tail
, retries
= 5;
520 size_t buffer_size
, size
;
522 /* For delta reads, we return at least the partial last block containing
524 if (type
== BTRACE_READ_NEW
&& !linux_btrace_has_changed (tinfo
))
525 return BTRACE_ERR_NONE
;
527 header
= perf_event_header (tinfo
);
528 buffer_size
= perf_event_buffer_size (tinfo
);
529 data_tail
= tinfo
->data_head
;
531 /* We may need to retry reading the trace. See below. */
534 data_head
= header
->data_head
;
536 /* Delete any leftover trace from the previous iteration. */
537 VEC_free (btrace_block_s
, *btrace
);
539 if (type
== BTRACE_READ_DELTA
)
541 /* Determine the number of bytes to read and check for buffer
544 /* Check for data head overflows. We might be able to recover from
545 those but they are very unlikely and it's not really worth the
547 if (data_head
< data_tail
)
548 return BTRACE_ERR_OVERFLOW
;
550 /* If the buffer is smaller than the trace delta, we overflowed. */
551 size
= data_head
- data_tail
;
552 if (buffer_size
< size
)
553 return BTRACE_ERR_OVERFLOW
;
557 /* Read the entire buffer. */
560 /* Adjust the size if the buffer has not overflowed, yet. */
561 if (data_head
< size
)
565 /* Data_head keeps growing; the buffer itself is circular. */
566 begin
= perf_event_buffer_begin (tinfo
);
567 start
= begin
+ data_head
% buffer_size
;
569 if (data_head
<= buffer_size
)
572 end
= perf_event_buffer_end (tinfo
);
574 *btrace
= perf_event_read_bts (tinfo
, begin
, end
, start
, size
);
576 /* The stopping thread notifies its ptracer before it is scheduled out.
577 On multi-core systems, the debugger might therefore run while the
578 kernel might be writing the last branch trace records.
580 Let's check whether the data head moved while we read the trace. */
581 if (data_head
== header
->data_head
)
585 tinfo
->data_head
= data_head
;
587 /* Prune the incomplete last block (i.e. the first one of inferior execution)
588 if we're not doing a delta read. There is no way of filling in its zeroed
590 if (!VEC_empty (btrace_block_s
, *btrace
) && type
!= BTRACE_READ_DELTA
)
591 VEC_pop (btrace_block_s
, *btrace
);
593 return BTRACE_ERR_NONE
;
596 #else /* !HAVE_LINUX_PERF_EVENT_H */
598 /* See linux-btrace.h. */
601 linux_supports_btrace (struct target_ops
*ops
)
606 /* See linux-btrace.h. */
608 struct btrace_target_info
*
609 linux_enable_btrace (ptid_t ptid
)
614 /* See linux-btrace.h. */
617 linux_disable_btrace (struct btrace_target_info
*tinfo
)
619 return BTRACE_ERR_NOT_SUPPORTED
;
622 /* See linux-btrace.h. */
625 linux_read_btrace (VEC (btrace_block_s
) **btrace
,
626 struct btrace_target_info
*tinfo
,
627 enum btrace_read_type type
)
629 return BTRACE_ERR_NOT_SUPPORTED
;
632 #endif /* !HAVE_LINUX_PERF_EVENT_H */