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 /* Identify the cpu we're running on. */
63 static struct btrace_cpu
64 btrace_this_cpu (void)
66 struct btrace_cpu cpu
;
67 unsigned int eax
, ebx
, ecx
, edx
;
70 memset (&cpu
, 0, sizeof (cpu
));
72 ok
= x86_cpuid (0, &eax
, &ebx
, &ecx
, &edx
);
75 if (ebx
== signature_INTEL_ebx
&& ecx
== signature_INTEL_ecx
76 && edx
== signature_INTEL_edx
)
78 unsigned int cpuid
, ignore
;
80 ok
= x86_cpuid (1, &cpuid
, &ignore
, &ignore
, &ignore
);
83 cpu
.vendor
= CV_INTEL
;
85 cpu
.family
= (cpuid
>> 8) & 0xf;
86 cpu
.model
= (cpuid
>> 4) & 0xf;
88 if (cpu
.family
== 0x6)
89 cpu
.model
+= (cpuid
>> 12) & 0xf0;
97 /* Return non-zero if there is new data in PEVENT; zero otherwise. */
100 perf_event_new_data (const struct perf_event_buffer
*pev
)
102 return *pev
->data_head
!= pev
->last_head
;
105 /* Check whether an address is in the kernel. */
108 perf_event_is_kernel_addr (const struct btrace_target_info
*tinfo
,
113 /* If we don't know the size of a pointer, we can't check. Let's assume it's
114 not a kernel address in this case. */
115 if (tinfo
->ptr_bits
== 0)
118 /* A bit mask for the most significant bit in an address. */
119 mask
= (uint64_t) 1 << (tinfo
->ptr_bits
- 1);
121 /* Check whether the most significant bit in the address is set. */
122 return (addr
& mask
) != 0;
125 /* Check whether a perf event record should be skipped. */
128 perf_event_skip_bts_record (const struct btrace_target_info
*tinfo
,
129 const struct perf_event_bts
*bts
)
131 /* The hardware may report branches from kernel into user space. Branches
132 from user into kernel space will be suppressed. We filter the former to
133 provide a consistent branch trace excluding kernel. */
134 return perf_event_is_kernel_addr (tinfo
, bts
->from
);
137 /* Perform a few consistency checks on a perf event sample record. This is
138 meant to catch cases when we get out of sync with the perf event stream. */
141 perf_event_sample_ok (const struct perf_event_sample
*sample
)
143 if (sample
->header
.type
!= PERF_RECORD_SAMPLE
)
146 if (sample
->header
.size
!= sizeof (*sample
))
152 /* Branch trace is collected in a circular buffer [begin; end) as pairs of from
153 and to addresses (plus a header).
155 Start points into that buffer at the next sample position.
156 We read the collected samples backwards from start.
158 While reading the samples, we convert the information into a list of blocks.
159 For two adjacent samples s1 and s2, we form a block b such that b.begin =
160 s1.to and b.end = s2.from.
162 In case the buffer overflows during sampling, one sample may have its lower
163 part at the end and its upper part at the beginning of the buffer. */
165 static VEC (btrace_block_s
) *
166 perf_event_read_bts (struct btrace_target_info
* tinfo
, const uint8_t *begin
,
167 const uint8_t *end
, const uint8_t *start
,
168 unsigned long long size
)
170 VEC (btrace_block_s
) *btrace
= NULL
;
171 struct perf_event_sample sample
;
172 unsigned long long read
= 0;
173 struct btrace_block block
= { 0, 0 };
174 struct regcache
*regcache
;
176 gdb_assert (begin
<= start
);
177 gdb_assert (start
<= end
);
179 /* The first block ends at the current pc. */
180 regcache
= get_thread_regcache_for_ptid (tinfo
->ptid
);
181 block
.end
= regcache_read_pc (regcache
);
183 /* The buffer may contain a partial record as its last entry (i.e. when the
184 buffer size is not a multiple of the sample size). */
185 read
= sizeof (sample
) - 1;
187 for (; read
< size
; read
+= sizeof (sample
))
189 const struct perf_event_sample
*psample
;
191 /* Find the next perf_event sample in a backwards traversal. */
192 start
-= sizeof (sample
);
194 /* If we're still inside the buffer, we're done. */
196 psample
= (const struct perf_event_sample
*) start
;
201 /* We're to the left of the ring buffer, we will wrap around and
202 reappear at the very right of the ring buffer. */
204 missing
= (begin
- start
);
205 start
= (end
- missing
);
207 /* If the entire sample is missing, we're done. */
208 if (missing
== sizeof (sample
))
209 psample
= (const struct perf_event_sample
*) start
;
214 /* The sample wrapped around. The lower part is at the end and
215 the upper part is at the beginning of the buffer. */
216 stack
= (uint8_t *) &sample
;
218 /* Copy the two parts so we have a contiguous sample. */
219 memcpy (stack
, start
, missing
);
220 memcpy (stack
+ missing
, begin
, sizeof (sample
) - missing
);
226 if (!perf_event_sample_ok (psample
))
228 warning (_("Branch trace may be incomplete."));
232 if (perf_event_skip_bts_record (tinfo
, &psample
->bts
))
235 /* We found a valid sample, so we can complete the current block. */
236 block
.begin
= psample
->bts
.to
;
238 VEC_safe_push (btrace_block_s
, btrace
, &block
);
240 /* Start the next block. */
241 block
.end
= psample
->bts
.from
;
244 /* Push the last block (i.e. the first one of inferior execution), as well.
245 We don't know where it ends, but we know where it starts. If we're
246 reading delta trace, we can fill in the start address later on.
247 Otherwise we will prune it. */
249 VEC_safe_push (btrace_block_s
, btrace
, &block
);
254 /* Check whether the kernel supports BTS. */
257 kernel_supports_bts (void)
259 struct perf_event_attr attr
;
268 warning (_("test bts: cannot fork: %s."), strerror (errno
));
272 status
= ptrace (PTRACE_TRACEME
, 0, NULL
, NULL
);
275 warning (_("test bts: cannot PTRACE_TRACEME: %s."),
280 status
= raise (SIGTRAP
);
283 warning (_("test bts: cannot raise SIGTRAP: %s."),
291 pid
= waitpid (child
, &status
, 0);
294 warning (_("test bts: bad pid %ld, error: %s."),
295 (long) pid
, strerror (errno
));
299 if (!WIFSTOPPED (status
))
301 warning (_("test bts: expected stop. status: %d."),
306 memset (&attr
, 0, sizeof (attr
));
308 attr
.type
= PERF_TYPE_HARDWARE
;
309 attr
.config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
;
310 attr
.sample_period
= 1;
311 attr
.sample_type
= PERF_SAMPLE_IP
| PERF_SAMPLE_ADDR
;
312 attr
.exclude_kernel
= 1;
314 attr
.exclude_idle
= 1;
316 file
= syscall (SYS_perf_event_open
, &attr
, child
, -1, -1, 0);
320 kill (child
, SIGKILL
);
321 ptrace (PTRACE_KILL
, child
, NULL
, NULL
);
323 pid
= waitpid (child
, &status
, 0);
326 warning (_("test bts: bad pid %ld, error: %s."),
327 (long) pid
, strerror (errno
));
328 if (!WIFSIGNALED (status
))
329 warning (_("test bts: expected killed. status: %d."),
337 /* Check whether an Intel cpu supports BTS. */
340 intel_supports_bts (const struct btrace_cpu
*cpu
)
347 case 0x1a: /* Nehalem */
351 case 0x25: /* Westmere */
354 case 0x2a: /* Sandy Bridge */
356 case 0x3a: /* Ivy Bridge */
358 /* AAJ122: LBR, BTM, or BTS records may have incorrect branch
359 "from" information afer an EIST transition, T-states, C1E, or
360 Adaptive Thermal Throttling. */
368 /* Check whether the cpu supports BTS. */
371 cpu_supports_bts (void)
373 struct btrace_cpu cpu
;
375 cpu
= btrace_this_cpu ();
379 /* Don't know about others. Let's assume they do. */
383 return intel_supports_bts (&cpu
);
387 /* Check whether the linux target supports BTS. */
390 linux_supports_bts (void)
396 if (!kernel_supports_bts ())
398 else if (!cpu_supports_bts ())
407 /* See linux-btrace.h. */
410 linux_supports_btrace (struct target_ops
*ops
, enum btrace_format format
)
414 case BTRACE_FORMAT_NONE
:
417 case BTRACE_FORMAT_BTS
:
418 return linux_supports_bts ();
421 internal_error (__FILE__
, __LINE__
, _("Unknown branch trace format"));
424 /* Enable branch tracing in BTS format. */
426 static struct btrace_target_info
*
427 linux_enable_bts (ptid_t ptid
, const struct btrace_config_bts
*conf
)
429 struct perf_event_mmap_page
*header
;
430 struct btrace_target_info
*tinfo
;
431 struct btrace_tinfo_bts
*bts
;
432 unsigned long long size
, pages
;
435 tinfo
= xzalloc (sizeof (*tinfo
));
439 tinfo
->conf
.format
= BTRACE_FORMAT_BTS
;
440 bts
= &tinfo
->variant
.bts
;
442 bts
->attr
.size
= sizeof (bts
->attr
);
443 bts
->attr
.type
= PERF_TYPE_HARDWARE
;
444 bts
->attr
.config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
;
445 bts
->attr
.sample_period
= 1;
447 /* We sample from and to address. */
448 bts
->attr
.sample_type
= PERF_SAMPLE_IP
| PERF_SAMPLE_ADDR
;
450 bts
->attr
.exclude_kernel
= 1;
451 bts
->attr
.exclude_hv
= 1;
452 bts
->attr
.exclude_idle
= 1;
454 pid
= ptid_get_lwp (ptid
);
456 pid
= ptid_get_pid (ptid
);
459 bts
->file
= syscall (SYS_perf_event_open
, &bts
->attr
, pid
, -1, -1, 0);
463 /* Convert the requested size in bytes to pages (rounding up). */
464 pages
= (((unsigned long long) conf
->size
) + PAGE_SIZE
- 1) / PAGE_SIZE
;
465 /* We need at least one page. */
469 /* The buffer size can be requested in powers of two pages. Adjust PAGES
470 to the next power of two. */
471 for (pg
= 0; pages
!= (1u << pg
); ++pg
)
472 if ((pages
& (1u << pg
)) != 0)
475 /* We try to allocate the requested size.
476 If that fails, try to get as much as we can. */
477 for (; pages
> 0; pages
>>= 1)
481 size
= pages
* PAGE_SIZE
;
482 length
= size
+ PAGE_SIZE
;
484 /* Check for overflows. */
485 if ((unsigned long long) length
< size
)
488 /* The number of pages we request needs to be a power of two. */
489 header
= mmap (NULL
, length
, PROT_READ
, MAP_SHARED
, bts
->file
, 0);
490 if (header
!= MAP_FAILED
)
494 if (header
== MAP_FAILED
)
497 bts
->header
= header
;
498 bts
->bts
.mem
= ((const uint8_t *) header
) + PAGE_SIZE
;
499 bts
->bts
.size
= size
;
500 bts
->bts
.data_head
= &header
->data_head
;
501 bts
->bts
.last_head
= 0;
503 tinfo
->conf
.bts
.size
= size
;
507 /* We were not able to allocate any buffer. */
515 /* See linux-btrace.h. */
517 struct btrace_target_info
*
518 linux_enable_btrace (ptid_t ptid
, const struct btrace_config
*conf
)
520 struct btrace_target_info
*tinfo
;
523 switch (conf
->format
)
525 case BTRACE_FORMAT_NONE
:
528 case BTRACE_FORMAT_BTS
:
529 tinfo
= linux_enable_bts (ptid
, &conf
->bts
);
536 /* Disable BTS tracing. */
538 static enum btrace_error
539 linux_disable_bts (struct btrace_tinfo_bts
*tinfo
)
541 munmap((void *) tinfo
->header
, tinfo
->bts
.size
+ PAGE_SIZE
);
544 return BTRACE_ERR_NONE
;
547 /* See linux-btrace.h. */
550 linux_disable_btrace (struct btrace_target_info
*tinfo
)
552 enum btrace_error errcode
;
554 errcode
= BTRACE_ERR_NOT_SUPPORTED
;
555 switch (tinfo
->conf
.format
)
557 case BTRACE_FORMAT_NONE
:
560 case BTRACE_FORMAT_BTS
:
561 errcode
= linux_disable_bts (&tinfo
->variant
.bts
);
565 if (errcode
== BTRACE_ERR_NONE
)
571 /* Read branch trace data in BTS format for the thread given by TINFO into
572 BTRACE using the TYPE reading method. */
574 static enum btrace_error
575 linux_read_bts (struct btrace_data_bts
*btrace
,
576 struct btrace_target_info
*tinfo
,
577 enum btrace_read_type type
)
579 struct perf_event_buffer
*pevent
;
580 const uint8_t *begin
, *end
, *start
;
581 unsigned long long data_head
, data_tail
, buffer_size
, size
;
582 unsigned int retries
= 5;
584 pevent
= &tinfo
->variant
.bts
.bts
;
586 /* For delta reads, we return at least the partial last block containing
588 if (type
== BTRACE_READ_NEW
&& !perf_event_new_data (pevent
))
589 return BTRACE_ERR_NONE
;
591 buffer_size
= pevent
->size
;
592 data_tail
= pevent
->last_head
;
594 /* We may need to retry reading the trace. See below. */
597 data_head
= *pevent
->data_head
;
599 /* Delete any leftover trace from the previous iteration. */
600 VEC_free (btrace_block_s
, btrace
->blocks
);
602 if (type
== BTRACE_READ_DELTA
)
604 /* Determine the number of bytes to read and check for buffer
607 /* Check for data head overflows. We might be able to recover from
608 those but they are very unlikely and it's not really worth the
610 if (data_head
< data_tail
)
611 return BTRACE_ERR_OVERFLOW
;
613 /* If the buffer is smaller than the trace delta, we overflowed. */
614 size
= data_head
- data_tail
;
615 if (buffer_size
< size
)
616 return BTRACE_ERR_OVERFLOW
;
620 /* Read the entire buffer. */
623 /* Adjust the size if the buffer has not overflowed, yet. */
624 if (data_head
< size
)
628 /* Data_head keeps growing; the buffer itself is circular. */
630 start
= begin
+ data_head
% buffer_size
;
632 if (data_head
<= buffer_size
)
635 end
= begin
+ pevent
->size
;
637 btrace
->blocks
= perf_event_read_bts (tinfo
, begin
, end
, start
, size
);
639 /* The stopping thread notifies its ptracer before it is scheduled out.
640 On multi-core systems, the debugger might therefore run while the
641 kernel might be writing the last branch trace records.
643 Let's check whether the data head moved while we read the trace. */
644 if (data_head
== *pevent
->data_head
)
648 pevent
->last_head
= data_head
;
650 /* Prune the incomplete last block (i.e. the first one of inferior execution)
651 if we're not doing a delta read. There is no way of filling in its zeroed
653 if (!VEC_empty (btrace_block_s
, btrace
->blocks
)
654 && type
!= BTRACE_READ_DELTA
)
655 VEC_pop (btrace_block_s
, btrace
->blocks
);
657 return BTRACE_ERR_NONE
;
660 /* See linux-btrace.h. */
663 linux_read_btrace (struct btrace_data
*btrace
,
664 struct btrace_target_info
*tinfo
,
665 enum btrace_read_type type
)
667 switch (tinfo
->conf
.format
)
669 case BTRACE_FORMAT_NONE
:
670 return BTRACE_ERR_NOT_SUPPORTED
;
672 case BTRACE_FORMAT_BTS
:
673 /* We read btrace in BTS format. */
674 btrace
->format
= BTRACE_FORMAT_BTS
;
675 btrace
->variant
.bts
.blocks
= NULL
;
677 return linux_read_bts (&btrace
->variant
.bts
, tinfo
, type
);
680 internal_error (__FILE__
, __LINE__
, _("Unkown branch trace format."));
683 /* See linux-btrace.h. */
685 const struct btrace_config
*
686 linux_btrace_conf (const struct btrace_target_info
*tinfo
)
691 #else /* !HAVE_LINUX_PERF_EVENT_H */
693 /* See linux-btrace.h. */
696 linux_supports_btrace (struct target_ops
*ops
, enum btrace_format format
)
701 /* See linux-btrace.h. */
703 struct btrace_target_info
*
704 linux_enable_btrace (ptid_t ptid
, const struct btrace_config
*conf
)
709 /* See linux-btrace.h. */
712 linux_disable_btrace (struct btrace_target_info
*tinfo
)
714 return BTRACE_ERR_NOT_SUPPORTED
;
717 /* See linux-btrace.h. */
720 linux_read_btrace (struct btrace_data
*btrace
,
721 struct btrace_target_info
*tinfo
,
722 enum btrace_read_type type
)
724 return BTRACE_ERR_NOT_SUPPORTED
;
727 /* See linux-btrace.h. */
729 const struct btrace_config
*
730 linux_btrace_conf (const struct btrace_target_info
*tinfo
)
735 #endif /* !HAVE_LINUX_PERF_EVENT_H */