Home | History | Annotate | Line # | Download | only in nat
linux-btrace.c revision 1.3
      1  1.1  christos /* Linux-dependent part of branch trace support for GDB, and GDBserver.
      2  1.1  christos 
      3  1.1  christos    Copyright (C) 2013-2015 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    Contributed by Intel Corp. <markus.t.metzger (at) intel.com>
      6  1.1  christos 
      7  1.1  christos    This file is part of GDB.
      8  1.1  christos 
      9  1.1  christos    This program is free software; you can redistribute it and/or modify
     10  1.1  christos    it under the terms of the GNU General Public License as published by
     11  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     12  1.1  christos    (at your option) any later version.
     13  1.1  christos 
     14  1.1  christos    This program is distributed in the hope that it will be useful,
     15  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     16  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17  1.1  christos    GNU General Public License for more details.
     18  1.1  christos 
     19  1.1  christos    You should have received a copy of the GNU General Public License
     20  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     21  1.1  christos 
     22  1.1  christos #include "common-defs.h"
     23  1.1  christos #include "linux-btrace.h"
     24  1.1  christos #include "common-regcache.h"
     25  1.1  christos #include "gdb_wait.h"
     26  1.1  christos #include "x86-cpuid.h"
     27  1.1  christos 
     28  1.1  christos #ifdef HAVE_SYS_SYSCALL_H
     29  1.1  christos #include <sys/syscall.h>
     30  1.1  christos #endif
     31  1.1  christos 
     32  1.1  christos #if HAVE_LINUX_PERF_EVENT_H && defined(SYS_perf_event_open)
     33  1.1  christos #include <unistd.h>
     34  1.1  christos #include <sys/mman.h>
     35  1.1  christos #include <sys/user.h>
     36  1.1  christos #include <sys/ptrace.h>
     37  1.1  christos #include <sys/types.h>
     38  1.1  christos #include <signal.h>
     39  1.3  christos #include <sys/utsname.h>
     40  1.1  christos 
     41  1.1  christos /* A branch trace record in perf_event.  */
     42  1.1  christos struct perf_event_bts
     43  1.1  christos {
     44  1.1  christos   /* The linear address of the branch source.  */
     45  1.1  christos   uint64_t from;
     46  1.1  christos 
     47  1.1  christos   /* The linear address of the branch destination.  */
     48  1.1  christos   uint64_t to;
     49  1.1  christos };
     50  1.1  christos 
     51  1.1  christos /* A perf_event branch trace sample.  */
     52  1.1  christos struct perf_event_sample
     53  1.1  christos {
     54  1.1  christos   /* The perf_event sample header.  */
     55  1.1  christos   struct perf_event_header header;
     56  1.1  christos 
     57  1.1  christos   /* The perf_event branch tracing payload.  */
     58  1.1  christos   struct perf_event_bts bts;
     59  1.1  christos };
     60  1.1  christos 
     61  1.3  christos /* Identify the cpu we're running on.  */
     62  1.3  christos static struct btrace_cpu
     63  1.3  christos btrace_this_cpu (void)
     64  1.3  christos {
     65  1.3  christos   struct btrace_cpu cpu;
     66  1.3  christos   unsigned int eax, ebx, ecx, edx;
     67  1.3  christos   int ok;
     68  1.3  christos 
     69  1.3  christos   memset (&cpu, 0, sizeof (cpu));
     70  1.3  christos 
     71  1.3  christos   ok = x86_cpuid (0, &eax, &ebx, &ecx, &edx);
     72  1.3  christos   if (ok != 0)
     73  1.3  christos     {
     74  1.3  christos       if (ebx == signature_INTEL_ebx && ecx == signature_INTEL_ecx
     75  1.3  christos 	  && edx == signature_INTEL_edx)
     76  1.3  christos 	{
     77  1.3  christos 	  unsigned int cpuid, ignore;
     78  1.3  christos 
     79  1.3  christos 	  ok = x86_cpuid (1, &cpuid, &ignore, &ignore, &ignore);
     80  1.3  christos 	  if (ok != 0)
     81  1.3  christos 	    {
     82  1.3  christos 	      cpu.vendor = CV_INTEL;
     83  1.3  christos 
     84  1.3  christos 	      cpu.family = (cpuid >> 8) & 0xf;
     85  1.3  christos 	      cpu.model = (cpuid >> 4) & 0xf;
     86  1.3  christos 
     87  1.3  christos 	      if (cpu.family == 0x6)
     88  1.3  christos 		cpu.model += (cpuid >> 12) & 0xf0;
     89  1.3  christos 	    }
     90  1.3  christos 	}
     91  1.3  christos     }
     92  1.3  christos 
     93  1.3  christos   return cpu;
     94  1.3  christos }
     95  1.1  christos 
     96  1.3  christos /* Return non-zero if there is new data in PEVENT; zero otherwise.  */
     97  1.3  christos 
     98  1.3  christos static int
     99  1.3  christos perf_event_new_data (const struct perf_event_buffer *pev)
    100  1.1  christos {
    101  1.3  christos   return *pev->data_head != pev->last_head;
    102  1.1  christos }
    103  1.1  christos 
    104  1.3  christos /* Try to determine the size of a pointer in bits for the OS.
    105  1.3  christos 
    106  1.3  christos    This is the same as the size of a pointer for the inferior process
    107  1.3  christos    except when a 32-bit inferior is running on a 64-bit OS.  */
    108  1.3  christos 
    109  1.3  christos /* Copy the last SIZE bytes from PEV ending at DATA_HEAD and return a pointer
    110  1.3  christos    to the memory holding the copy.
    111  1.3  christos    The caller is responsible for freeing the memory.  */
    112  1.1  christos 
    113  1.3  christos static gdb_byte *
    114  1.3  christos perf_event_read (const struct perf_event_buffer *pev, unsigned long data_head,
    115  1.3  christos 		 unsigned long size)
    116  1.1  christos {
    117  1.3  christos   const gdb_byte *begin, *end, *start, *stop;
    118  1.3  christos   gdb_byte *buffer;
    119  1.3  christos   unsigned long data_tail, buffer_size;
    120  1.3  christos 
    121  1.3  christos   if (size == 0)
    122  1.3  christos     return NULL;
    123  1.3  christos 
    124  1.3  christos   gdb_assert (size <= data_head);
    125  1.3  christos   data_tail = data_head - size;
    126  1.3  christos 
    127  1.3  christos   buffer_size = pev->size;
    128  1.3  christos   begin = pev->mem;
    129  1.3  christos   start = begin + data_tail % buffer_size;
    130  1.3  christos   stop = begin + data_head % buffer_size;
    131  1.3  christos 
    132  1.3  christos   buffer = xmalloc (size);
    133  1.3  christos 
    134  1.3  christos   if (start < stop)
    135  1.3  christos     memcpy (buffer, start, stop - start);
    136  1.3  christos   else
    137  1.3  christos     {
    138  1.3  christos       end = begin + buffer_size;
    139  1.3  christos 
    140  1.3  christos       memcpy (buffer, start, end - start);
    141  1.3  christos       memcpy (buffer + (end - start), begin, stop - begin);
    142  1.3  christos     }
    143  1.3  christos 
    144  1.3  christos   return buffer;
    145  1.1  christos }
    146  1.1  christos 
    147  1.3  christos /* Copy the perf event buffer data from PEV.
    148  1.3  christos    Store a pointer to the copy into DATA and its size in SIZE.  */
    149  1.1  christos 
    150  1.3  christos static void
    151  1.3  christos perf_event_read_all (struct perf_event_buffer *pev, gdb_byte **data,
    152  1.3  christos 		     unsigned long *psize)
    153  1.1  christos {
    154  1.3  christos   unsigned long data_head, size;
    155  1.3  christos 
    156  1.3  christos   data_head = *pev->data_head;
    157  1.3  christos 
    158  1.3  christos   size = pev->size;
    159  1.3  christos   if (data_head < size)
    160  1.3  christos     size = data_head;
    161  1.3  christos 
    162  1.3  christos   *data = perf_event_read (pev, data_head, size);
    163  1.3  christos   *psize = size;
    164  1.3  christos 
    165  1.3  christos   pev->last_head = data_head;
    166  1.1  christos }
    167  1.1  christos 
    168  1.3  christos /* Determine the event type.
    169  1.3  christos    Returns zero on success and fills in TYPE; returns -1 otherwise.  */
    170  1.1  christos 
    171  1.3  christos static int
    172  1.3  christos perf_event_pt_event_type (int *type)
    173  1.1  christos {
    174  1.3  christos   FILE *file;
    175  1.3  christos   int found;
    176  1.3  christos 
    177  1.3  christos   file = fopen ("/sys/bus/event_source/devices/intel_pt/type", "r");
    178  1.3  christos   if (file == NULL)
    179  1.3  christos     return -1;
    180  1.3  christos 
    181  1.3  christos   found = fscanf (file, "%d", type);
    182  1.3  christos 
    183  1.3  christos   fclose (file);
    184  1.3  christos 
    185  1.3  christos   if (found == 1)
    186  1.3  christos     return 0;
    187  1.3  christos   return -1;
    188  1.1  christos }
    189  1.1  christos 
    190  1.3  christos static int
    191  1.3  christos linux_determine_kernel_ptr_bits (void)
    192  1.3  christos {
    193  1.3  christos   struct utsname utsn;
    194  1.3  christos   int errcode;
    195  1.3  christos 
    196  1.3  christos   memset (&utsn, 0, sizeof (utsn));
    197  1.3  christos 
    198  1.3  christos   errcode = uname (&utsn);
    199  1.3  christos   if (errcode < 0)
    200  1.3  christos     return 0;
    201  1.3  christos 
    202  1.3  christos   /* We only need to handle the 64-bit host case, here.  For 32-bit host,
    203  1.3  christos      the pointer size can be filled in later based on the inferior.  */
    204  1.3  christos   if (strcmp (utsn.machine, "x86_64") == 0)
    205  1.3  christos     return 64;
    206  1.1  christos 
    207  1.3  christos   return 0;
    208  1.1  christos }
    209  1.1  christos 
    210  1.1  christos /* Check whether an address is in the kernel.  */
    211  1.1  christos 
    212  1.1  christos static inline int
    213  1.1  christos perf_event_is_kernel_addr (const struct btrace_target_info *tinfo,
    214  1.1  christos 			   uint64_t addr)
    215  1.1  christos {
    216  1.1  christos   uint64_t mask;
    217  1.1  christos 
    218  1.1  christos   /* If we don't know the size of a pointer, we can't check.  Let's assume it's
    219  1.1  christos      not a kernel address in this case.  */
    220  1.1  christos   if (tinfo->ptr_bits == 0)
    221  1.1  christos     return 0;
    222  1.1  christos 
    223  1.1  christos   /* A bit mask for the most significant bit in an address.  */
    224  1.1  christos   mask = (uint64_t) 1 << (tinfo->ptr_bits - 1);
    225  1.1  christos 
    226  1.1  christos   /* Check whether the most significant bit in the address is set.  */
    227  1.1  christos   return (addr & mask) != 0;
    228  1.1  christos }
    229  1.1  christos 
    230  1.1  christos /* Check whether a perf event record should be skipped.  */
    231  1.1  christos 
    232  1.1  christos static inline int
    233  1.3  christos perf_event_skip_bts_record (const struct btrace_target_info *tinfo,
    234  1.3  christos 			    const struct perf_event_bts *bts)
    235  1.1  christos {
    236  1.1  christos   /* The hardware may report branches from kernel into user space.  Branches
    237  1.1  christos      from user into kernel space will be suppressed.  We filter the former to
    238  1.1  christos      provide a consistent branch trace excluding kernel.  */
    239  1.1  christos   return perf_event_is_kernel_addr (tinfo, bts->from);
    240  1.1  christos }
    241  1.1  christos 
    242  1.1  christos /* Perform a few consistency checks on a perf event sample record.  This is
    243  1.1  christos    meant to catch cases when we get out of sync with the perf event stream.  */
    244  1.1  christos 
    245  1.1  christos static inline int
    246  1.1  christos perf_event_sample_ok (const struct perf_event_sample *sample)
    247  1.1  christos {
    248  1.1  christos   if (sample->header.type != PERF_RECORD_SAMPLE)
    249  1.1  christos     return 0;
    250  1.1  christos 
    251  1.1  christos   if (sample->header.size != sizeof (*sample))
    252  1.1  christos     return 0;
    253  1.1  christos 
    254  1.1  christos   return 1;
    255  1.1  christos }
    256  1.1  christos 
    257  1.1  christos /* Branch trace is collected in a circular buffer [begin; end) as pairs of from
    258  1.1  christos    and to addresses (plus a header).
    259  1.1  christos 
    260  1.1  christos    Start points into that buffer at the next sample position.
    261  1.1  christos    We read the collected samples backwards from start.
    262  1.1  christos 
    263  1.1  christos    While reading the samples, we convert the information into a list of blocks.
    264  1.1  christos    For two adjacent samples s1 and s2, we form a block b such that b.begin =
    265  1.1  christos    s1.to and b.end = s2.from.
    266  1.1  christos 
    267  1.1  christos    In case the buffer overflows during sampling, one sample may have its lower
    268  1.1  christos    part at the end and its upper part at the beginning of the buffer.  */
    269  1.1  christos 
    270  1.1  christos static VEC (btrace_block_s) *
    271  1.1  christos perf_event_read_bts (struct btrace_target_info* tinfo, const uint8_t *begin,
    272  1.3  christos 		     const uint8_t *end, const uint8_t *start,
    273  1.3  christos 		     unsigned long long size)
    274  1.1  christos {
    275  1.1  christos   VEC (btrace_block_s) *btrace = NULL;
    276  1.1  christos   struct perf_event_sample sample;
    277  1.3  christos   unsigned long long read = 0;
    278  1.1  christos   struct btrace_block block = { 0, 0 };
    279  1.1  christos   struct regcache *regcache;
    280  1.1  christos 
    281  1.1  christos   gdb_assert (begin <= start);
    282  1.1  christos   gdb_assert (start <= end);
    283  1.1  christos 
    284  1.1  christos   /* The first block ends at the current pc.  */
    285  1.1  christos   regcache = get_thread_regcache_for_ptid (tinfo->ptid);
    286  1.1  christos   block.end = regcache_read_pc (regcache);
    287  1.1  christos 
    288  1.1  christos   /* The buffer may contain a partial record as its last entry (i.e. when the
    289  1.1  christos      buffer size is not a multiple of the sample size).  */
    290  1.1  christos   read = sizeof (sample) - 1;
    291  1.1  christos 
    292  1.1  christos   for (; read < size; read += sizeof (sample))
    293  1.1  christos     {
    294  1.1  christos       const struct perf_event_sample *psample;
    295  1.1  christos 
    296  1.1  christos       /* Find the next perf_event sample in a backwards traversal.  */
    297  1.1  christos       start -= sizeof (sample);
    298  1.1  christos 
    299  1.1  christos       /* If we're still inside the buffer, we're done.  */
    300  1.1  christos       if (begin <= start)
    301  1.1  christos 	psample = (const struct perf_event_sample *) start;
    302  1.1  christos       else
    303  1.1  christos 	{
    304  1.1  christos 	  int missing;
    305  1.1  christos 
    306  1.1  christos 	  /* We're to the left of the ring buffer, we will wrap around and
    307  1.1  christos 	     reappear at the very right of the ring buffer.  */
    308  1.1  christos 
    309  1.1  christos 	  missing = (begin - start);
    310  1.1  christos 	  start = (end - missing);
    311  1.1  christos 
    312  1.1  christos 	  /* If the entire sample is missing, we're done.  */
    313  1.1  christos 	  if (missing == sizeof (sample))
    314  1.1  christos 	    psample = (const struct perf_event_sample *) start;
    315  1.1  christos 	  else
    316  1.1  christos 	    {
    317  1.1  christos 	      uint8_t *stack;
    318  1.1  christos 
    319  1.1  christos 	      /* The sample wrapped around.  The lower part is at the end and
    320  1.1  christos 		 the upper part is at the beginning of the buffer.  */
    321  1.1  christos 	      stack = (uint8_t *) &sample;
    322  1.1  christos 
    323  1.1  christos 	      /* Copy the two parts so we have a contiguous sample.  */
    324  1.1  christos 	      memcpy (stack, start, missing);
    325  1.1  christos 	      memcpy (stack + missing, begin, sizeof (sample) - missing);
    326  1.1  christos 
    327  1.1  christos 	      psample = &sample;
    328  1.1  christos 	    }
    329  1.1  christos 	}
    330  1.1  christos 
    331  1.1  christos       if (!perf_event_sample_ok (psample))
    332  1.1  christos 	{
    333  1.1  christos 	  warning (_("Branch trace may be incomplete."));
    334  1.1  christos 	  break;
    335  1.1  christos 	}
    336  1.1  christos 
    337  1.3  christos       if (perf_event_skip_bts_record (tinfo, &psample->bts))
    338  1.1  christos 	continue;
    339  1.1  christos 
    340  1.1  christos       /* We found a valid sample, so we can complete the current block.  */
    341  1.1  christos       block.begin = psample->bts.to;
    342  1.1  christos 
    343  1.1  christos       VEC_safe_push (btrace_block_s, btrace, &block);
    344  1.1  christos 
    345  1.1  christos       /* Start the next block.  */
    346  1.1  christos       block.end = psample->bts.from;
    347  1.1  christos     }
    348  1.1  christos 
    349  1.1  christos   /* Push the last block (i.e. the first one of inferior execution), as well.
    350  1.1  christos      We don't know where it ends, but we know where it starts.  If we're
    351  1.1  christos      reading delta trace, we can fill in the start address later on.
    352  1.1  christos      Otherwise we will prune it.  */
    353  1.1  christos   block.begin = 0;
    354  1.1  christos   VEC_safe_push (btrace_block_s, btrace, &block);
    355  1.1  christos 
    356  1.1  christos   return btrace;
    357  1.1  christos }
    358  1.1  christos 
    359  1.3  christos /* Check whether the kernel supports BTS.  */
    360  1.1  christos 
    361  1.1  christos static int
    362  1.3  christos kernel_supports_bts (void)
    363  1.1  christos {
    364  1.1  christos   struct perf_event_attr attr;
    365  1.1  christos   pid_t child, pid;
    366  1.1  christos   int status, file;
    367  1.1  christos 
    368  1.1  christos   errno = 0;
    369  1.1  christos   child = fork ();
    370  1.1  christos   switch (child)
    371  1.1  christos     {
    372  1.1  christos     case -1:
    373  1.3  christos       warning (_("test bts: cannot fork: %s."), strerror (errno));
    374  1.1  christos       return 0;
    375  1.1  christos 
    376  1.1  christos     case 0:
    377  1.1  christos       status = ptrace (PTRACE_TRACEME, 0, NULL, NULL);
    378  1.1  christos       if (status != 0)
    379  1.1  christos 	{
    380  1.3  christos 	  warning (_("test bts: cannot PTRACE_TRACEME: %s."),
    381  1.1  christos 		   strerror (errno));
    382  1.1  christos 	  _exit (1);
    383  1.1  christos 	}
    384  1.1  christos 
    385  1.1  christos       status = raise (SIGTRAP);
    386  1.1  christos       if (status != 0)
    387  1.1  christos 	{
    388  1.3  christos 	  warning (_("test bts: cannot raise SIGTRAP: %s."),
    389  1.1  christos 		   strerror (errno));
    390  1.1  christos 	  _exit (1);
    391  1.1  christos 	}
    392  1.1  christos 
    393  1.1  christos       _exit (1);
    394  1.1  christos 
    395  1.1  christos     default:
    396  1.1  christos       pid = waitpid (child, &status, 0);
    397  1.1  christos       if (pid != child)
    398  1.1  christos 	{
    399  1.3  christos 	  warning (_("test bts: bad pid %ld, error: %s."),
    400  1.1  christos 		   (long) pid, strerror (errno));
    401  1.1  christos 	  return 0;
    402  1.1  christos 	}
    403  1.1  christos 
    404  1.1  christos       if (!WIFSTOPPED (status))
    405  1.1  christos 	{
    406  1.3  christos 	  warning (_("test bts: expected stop. status: %d."),
    407  1.1  christos 		   status);
    408  1.1  christos 	  return 0;
    409  1.1  christos 	}
    410  1.1  christos 
    411  1.1  christos       memset (&attr, 0, sizeof (attr));
    412  1.1  christos 
    413  1.1  christos       attr.type = PERF_TYPE_HARDWARE;
    414  1.1  christos       attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
    415  1.1  christos       attr.sample_period = 1;
    416  1.1  christos       attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_ADDR;
    417  1.1  christos       attr.exclude_kernel = 1;
    418  1.1  christos       attr.exclude_hv = 1;
    419  1.1  christos       attr.exclude_idle = 1;
    420  1.1  christos 
    421  1.1  christos       file = syscall (SYS_perf_event_open, &attr, child, -1, -1, 0);
    422  1.1  christos       if (file >= 0)
    423  1.1  christos 	close (file);
    424  1.1  christos 
    425  1.1  christos       kill (child, SIGKILL);
    426  1.1  christos       ptrace (PTRACE_KILL, child, NULL, NULL);
    427  1.1  christos 
    428  1.1  christos       pid = waitpid (child, &status, 0);
    429  1.1  christos       if (pid != child)
    430  1.1  christos 	{
    431  1.3  christos 	  warning (_("test bts: bad pid %ld, error: %s."),
    432  1.1  christos 		   (long) pid, strerror (errno));
    433  1.1  christos 	  if (!WIFSIGNALED (status))
    434  1.3  christos 	    warning (_("test bts: expected killed. status: %d."),
    435  1.1  christos 		     status);
    436  1.1  christos 	}
    437  1.1  christos 
    438  1.1  christos       return (file >= 0);
    439  1.1  christos     }
    440  1.1  christos }
    441  1.1  christos 
    442  1.3  christos /* Check whether the kernel supports Intel(R) Processor Trace.  */
    443  1.1  christos 
    444  1.1  christos static int
    445  1.3  christos kernel_supports_pt (void)
    446  1.1  christos {
    447  1.3  christos   struct perf_event_attr attr;
    448  1.3  christos   pid_t child, pid;
    449  1.3  christos   int status, file, type;
    450  1.3  christos 
    451  1.3  christos   errno = 0;
    452  1.3  christos   child = fork ();
    453  1.3  christos   switch (child)
    454  1.3  christos     {
    455  1.3  christos     case -1:
    456  1.3  christos       warning (_("test pt: cannot fork: %s."), strerror (errno));
    457  1.3  christos       return 0;
    458  1.3  christos 
    459  1.3  christos     case 0:
    460  1.3  christos       status = ptrace (PTRACE_TRACEME, 0, NULL, NULL);
    461  1.3  christos       if (status != 0)
    462  1.3  christos 	{
    463  1.3  christos 	  warning (_("test pt: cannot PTRACE_TRACEME: %s."),
    464  1.3  christos 		   strerror (errno));
    465  1.3  christos 	  _exit (1);
    466  1.3  christos 	}
    467  1.3  christos 
    468  1.3  christos       status = raise (SIGTRAP);
    469  1.3  christos       if (status != 0)
    470  1.3  christos 	{
    471  1.3  christos 	  warning (_("test pt: cannot raise SIGTRAP: %s."),
    472  1.3  christos 		   strerror (errno));
    473  1.3  christos 	  _exit (1);
    474  1.3  christos 	}
    475  1.3  christos 
    476  1.3  christos       _exit (1);
    477  1.3  christos 
    478  1.3  christos     default:
    479  1.3  christos       pid = waitpid (child, &status, 0);
    480  1.3  christos       if (pid != child)
    481  1.3  christos 	{
    482  1.3  christos 	  warning (_("test pt: bad pid %ld, error: %s."),
    483  1.3  christos 		   (long) pid, strerror (errno));
    484  1.3  christos 	  return 0;
    485  1.3  christos 	}
    486  1.1  christos 
    487  1.3  christos       if (!WIFSTOPPED (status))
    488  1.3  christos 	{
    489  1.3  christos 	  warning (_("test pt: expected stop. status: %d."),
    490  1.3  christos 		   status);
    491  1.3  christos 	  return 0;
    492  1.3  christos 	}
    493  1.3  christos 
    494  1.3  christos       status = perf_event_pt_event_type (&type);
    495  1.3  christos       if (status != 0)
    496  1.3  christos 	file = -1;
    497  1.3  christos       else
    498  1.3  christos 	{
    499  1.3  christos 	  memset (&attr, 0, sizeof (attr));
    500  1.3  christos 
    501  1.3  christos 	  attr.size = sizeof (attr);
    502  1.3  christos 	  attr.type = type;
    503  1.3  christos 	  attr.exclude_kernel = 1;
    504  1.3  christos 	  attr.exclude_hv = 1;
    505  1.3  christos 	  attr.exclude_idle = 1;
    506  1.3  christos 
    507  1.3  christos 	  file = syscall (SYS_perf_event_open, &attr, child, -1, -1, 0);
    508  1.3  christos 	  if (file >= 0)
    509  1.3  christos 	    close (file);
    510  1.3  christos 	}
    511  1.3  christos 
    512  1.3  christos       kill (child, SIGKILL);
    513  1.3  christos       ptrace (PTRACE_KILL, child, NULL, NULL);
    514  1.3  christos 
    515  1.3  christos       pid = waitpid (child, &status, 0);
    516  1.3  christos       if (pid != child)
    517  1.3  christos 	{
    518  1.3  christos 	  warning (_("test pt: bad pid %ld, error: %s."),
    519  1.3  christos 		   (long) pid, strerror (errno));
    520  1.3  christos 	  if (!WIFSIGNALED (status))
    521  1.3  christos 	    warning (_("test pt: expected killed. status: %d."),
    522  1.3  christos 		     status);
    523  1.3  christos 	}
    524  1.3  christos 
    525  1.3  christos       return (file >= 0);
    526  1.3  christos     }
    527  1.3  christos }
    528  1.1  christos 
    529  1.3  christos /* Check whether an Intel cpu supports BTS.  */
    530  1.1  christos 
    531  1.3  christos static int
    532  1.3  christos intel_supports_bts (const struct btrace_cpu *cpu)
    533  1.3  christos {
    534  1.3  christos   switch (cpu->family)
    535  1.1  christos     {
    536  1.1  christos     case 0x6:
    537  1.3  christos       switch (cpu->model)
    538  1.1  christos 	{
    539  1.1  christos 	case 0x1a: /* Nehalem */
    540  1.1  christos 	case 0x1f:
    541  1.1  christos 	case 0x1e:
    542  1.1  christos 	case 0x2e:
    543  1.1  christos 	case 0x25: /* Westmere */
    544  1.1  christos 	case 0x2c:
    545  1.1  christos 	case 0x2f:
    546  1.1  christos 	case 0x2a: /* Sandy Bridge */
    547  1.1  christos 	case 0x2d:
    548  1.1  christos 	case 0x3a: /* Ivy Bridge */
    549  1.1  christos 
    550  1.1  christos 	  /* AAJ122: LBR, BTM, or BTS records may have incorrect branch
    551  1.1  christos 	     "from" information afer an EIST transition, T-states, C1E, or
    552  1.1  christos 	     Adaptive Thermal Throttling.  */
    553  1.1  christos 	  return 0;
    554  1.1  christos 	}
    555  1.1  christos     }
    556  1.1  christos 
    557  1.1  christos   return 1;
    558  1.1  christos }
    559  1.1  christos 
    560  1.3  christos /* Check whether the cpu supports BTS.  */
    561  1.1  christos 
    562  1.1  christos static int
    563  1.3  christos cpu_supports_bts (void)
    564  1.1  christos {
    565  1.3  christos   struct btrace_cpu cpu;
    566  1.3  christos 
    567  1.3  christos   cpu = btrace_this_cpu ();
    568  1.3  christos   switch (cpu.vendor)
    569  1.3  christos     {
    570  1.3  christos     default:
    571  1.3  christos       /* Don't know about others.  Let's assume they do.  */
    572  1.3  christos       return 1;
    573  1.1  christos 
    574  1.3  christos     case CV_INTEL:
    575  1.3  christos       return intel_supports_bts (&cpu);
    576  1.3  christos     }
    577  1.3  christos }
    578  1.3  christos 
    579  1.3  christos /* Check whether the linux target supports BTS.  */
    580  1.3  christos 
    581  1.3  christos static int
    582  1.3  christos linux_supports_bts (void)
    583  1.3  christos {
    584  1.3  christos   static int cached;
    585  1.1  christos 
    586  1.3  christos   if (cached == 0)
    587  1.3  christos     {
    588  1.3  christos       if (!kernel_supports_bts ())
    589  1.3  christos 	cached = -1;
    590  1.3  christos       else if (!cpu_supports_bts ())
    591  1.3  christos 	cached = -1;
    592  1.3  christos       else
    593  1.3  christos 	cached = 1;
    594  1.3  christos     }
    595  1.1  christos 
    596  1.3  christos   return cached > 0;
    597  1.1  christos }
    598  1.1  christos 
    599  1.3  christos /* Check whether the linux target supports Intel(R) Processor Trace.  */
    600  1.1  christos 
    601  1.3  christos static int
    602  1.3  christos linux_supports_pt (void)
    603  1.1  christos {
    604  1.1  christos   static int cached;
    605  1.1  christos 
    606  1.1  christos   if (cached == 0)
    607  1.1  christos     {
    608  1.3  christos       if (!kernel_supports_pt ())
    609  1.1  christos 	cached = -1;
    610  1.1  christos       else
    611  1.1  christos 	cached = 1;
    612  1.1  christos     }
    613  1.1  christos 
    614  1.1  christos   return cached > 0;
    615  1.1  christos }
    616  1.1  christos 
    617  1.1  christos /* See linux-btrace.h.  */
    618  1.1  christos 
    619  1.3  christos int
    620  1.3  christos linux_supports_btrace (struct target_ops *ops, enum btrace_format format)
    621  1.3  christos {
    622  1.3  christos   switch (format)
    623  1.3  christos     {
    624  1.3  christos     case BTRACE_FORMAT_NONE:
    625  1.3  christos       return 0;
    626  1.3  christos 
    627  1.3  christos     case BTRACE_FORMAT_BTS:
    628  1.3  christos       return linux_supports_bts ();
    629  1.3  christos 
    630  1.3  christos     case BTRACE_FORMAT_PT:
    631  1.3  christos       return linux_supports_pt ();
    632  1.3  christos     }
    633  1.3  christos 
    634  1.3  christos   internal_error (__FILE__, __LINE__, _("Unknown branch trace format"));
    635  1.3  christos }
    636  1.3  christos 
    637  1.3  christos /* Enable branch tracing in BTS format.  */
    638  1.3  christos 
    639  1.3  christos static struct btrace_target_info *
    640  1.3  christos linux_enable_bts (ptid_t ptid, const struct btrace_config_bts *conf)
    641  1.1  christos {
    642  1.3  christos   struct perf_event_mmap_page *header;
    643  1.1  christos   struct btrace_target_info *tinfo;
    644  1.3  christos   struct btrace_tinfo_bts *bts;
    645  1.3  christos   unsigned long long size, pages, data_offset, data_size;
    646  1.1  christos   int pid, pg;
    647  1.1  christos 
    648  1.1  christos   tinfo = xzalloc (sizeof (*tinfo));
    649  1.1  christos   tinfo->ptid = ptid;
    650  1.3  christos   tinfo->ptr_bits = linux_determine_kernel_ptr_bits ();
    651  1.3  christos 
    652  1.3  christos   tinfo->conf.format = BTRACE_FORMAT_BTS;
    653  1.3  christos   bts = &tinfo->variant.bts;
    654  1.1  christos 
    655  1.3  christos   bts->attr.size = sizeof (bts->attr);
    656  1.3  christos   bts->attr.type = PERF_TYPE_HARDWARE;
    657  1.3  christos   bts->attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
    658  1.3  christos   bts->attr.sample_period = 1;
    659  1.1  christos 
    660  1.1  christos   /* We sample from and to address.  */
    661  1.3  christos   bts->attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_ADDR;
    662  1.1  christos 
    663  1.3  christos   bts->attr.exclude_kernel = 1;
    664  1.3  christos   bts->attr.exclude_hv = 1;
    665  1.3  christos   bts->attr.exclude_idle = 1;
    666  1.1  christos 
    667  1.3  christos   pid = ptid_get_lwp (ptid);
    668  1.3  christos   if (pid == 0)
    669  1.3  christos     pid = ptid_get_pid (ptid);
    670  1.3  christos 
    671  1.3  christos   errno = 0;
    672  1.3  christos   bts->file = syscall (SYS_perf_event_open, &bts->attr, pid, -1, -1, 0);
    673  1.3  christos   if (bts->file < 0)
    674  1.3  christos     goto err_out;
    675  1.3  christos 
    676  1.3  christos   /* Convert the requested size in bytes to pages (rounding up).  */
    677  1.3  christos   pages = (((unsigned long long) conf->size) + PAGE_SIZE - 1) / PAGE_SIZE;
    678  1.3  christos   /* We need at least one page.  */
    679  1.3  christos   if (pages == 0)
    680  1.3  christos     pages = 1;
    681  1.3  christos 
    682  1.3  christos   /* The buffer size can be requested in powers of two pages.  Adjust PAGES
    683  1.3  christos      to the next power of two.  */
    684  1.3  christos   for (pg = 0; pages != (1u << pg); ++pg)
    685  1.3  christos     if ((pages & (1u << pg)) != 0)
    686  1.3  christos       pages += (1u << pg);
    687  1.3  christos 
    688  1.3  christos   /* We try to allocate the requested size.
    689  1.3  christos      If that fails, try to get as much as we can.  */
    690  1.3  christos   for (; pages > 0; pages >>= 1)
    691  1.3  christos     {
    692  1.3  christos       size_t length;
    693  1.3  christos 
    694  1.3  christos       size = pages * PAGE_SIZE;
    695  1.3  christos       length = size + PAGE_SIZE;
    696  1.3  christos 
    697  1.3  christos       /* Check for overflows.  */
    698  1.3  christos       if ((unsigned long long) length < size)
    699  1.3  christos 	continue;
    700  1.3  christos 
    701  1.3  christos       /* The number of pages we request needs to be a power of two.  */
    702  1.3  christos       header = mmap (NULL, length, PROT_READ, MAP_SHARED, bts->file, 0);
    703  1.3  christos       if (header != MAP_FAILED)
    704  1.3  christos 	break;
    705  1.3  christos     }
    706  1.3  christos 
    707  1.3  christos   if (pages == 0)
    708  1.3  christos     goto err_file;
    709  1.3  christos 
    710  1.3  christos   data_offset = PAGE_SIZE;
    711  1.3  christos   data_size = size;
    712  1.3  christos 
    713  1.3  christos #if defined (PERF_ATTR_SIZE_VER5)
    714  1.3  christos   if (offsetof (struct perf_event_mmap_page, data_size) <= header->size)
    715  1.3  christos     {
    716  1.3  christos       data_offset = header->data_offset;
    717  1.3  christos       data_size = header->data_size;
    718  1.3  christos     }
    719  1.3  christos #endif /* defined (PERF_ATTR_SIZE_VER5) */
    720  1.3  christos 
    721  1.3  christos   bts->header = header;
    722  1.3  christos   bts->bts.mem = ((const uint8_t *) header) + data_offset;
    723  1.3  christos   bts->bts.size = data_size;
    724  1.3  christos   bts->bts.data_head = &header->data_head;
    725  1.3  christos   bts->bts.last_head = 0;
    726  1.3  christos 
    727  1.3  christos   tinfo->conf.bts.size = data_size;
    728  1.3  christos   return tinfo;
    729  1.3  christos 
    730  1.3  christos  err_file:
    731  1.3  christos   /* We were not able to allocate any buffer.  */
    732  1.3  christos   close (bts->file);
    733  1.3  christos 
    734  1.3  christos  err_out:
    735  1.3  christos   xfree (tinfo);
    736  1.3  christos   return NULL;
    737  1.3  christos }
    738  1.3  christos 
    739  1.3  christos #if defined (PERF_ATTR_SIZE_VER5)
    740  1.3  christos 
    741  1.3  christos /* Enable branch tracing in Intel(R) Processor Trace format.  */
    742  1.3  christos 
    743  1.3  christos static struct btrace_target_info *
    744  1.3  christos linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf)
    745  1.3  christos {
    746  1.3  christos   struct perf_event_mmap_page *header;
    747  1.3  christos   struct btrace_target_info *tinfo;
    748  1.3  christos   struct btrace_tinfo_pt *pt;
    749  1.3  christos   unsigned long long pages, size;
    750  1.3  christos   int pid, pg, errcode, type;
    751  1.3  christos 
    752  1.3  christos   if (conf->size == 0)
    753  1.3  christos     return NULL;
    754  1.3  christos 
    755  1.3  christos   errcode = perf_event_pt_event_type (&type);
    756  1.3  christos   if (errcode != 0)
    757  1.3  christos     return NULL;
    758  1.1  christos 
    759  1.1  christos   pid = ptid_get_lwp (ptid);
    760  1.1  christos   if (pid == 0)
    761  1.1  christos     pid = ptid_get_pid (ptid);
    762  1.1  christos 
    763  1.3  christos   tinfo = xzalloc (sizeof (*tinfo));
    764  1.3  christos   tinfo->ptid = ptid;
    765  1.3  christos   tinfo->ptr_bits = 0;
    766  1.3  christos 
    767  1.3  christos   tinfo->conf.format = BTRACE_FORMAT_PT;
    768  1.3  christos   pt = &tinfo->variant.pt;
    769  1.3  christos 
    770  1.3  christos   pt->attr.size = sizeof (pt->attr);
    771  1.3  christos   pt->attr.type = type;
    772  1.3  christos 
    773  1.3  christos   pt->attr.exclude_kernel = 1;
    774  1.3  christos   pt->attr.exclude_hv = 1;
    775  1.3  christos   pt->attr.exclude_idle = 1;
    776  1.3  christos 
    777  1.1  christos   errno = 0;
    778  1.3  christos   pt->file = syscall (SYS_perf_event_open, &pt->attr, pid, -1, -1, 0);
    779  1.3  christos   if (pt->file < 0)
    780  1.1  christos     goto err;
    781  1.1  christos 
    782  1.3  christos   /* Allocate the configuration page. */
    783  1.3  christos   header = mmap (NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
    784  1.3  christos 		 pt->file, 0);
    785  1.3  christos   if (header == MAP_FAILED)
    786  1.3  christos     goto err_file;
    787  1.3  christos 
    788  1.3  christos   header->aux_offset = header->data_offset + header->data_size;
    789  1.3  christos 
    790  1.3  christos   /* Convert the requested size in bytes to pages (rounding up).  */
    791  1.3  christos   pages = (((unsigned long long) conf->size) + PAGE_SIZE - 1) / PAGE_SIZE;
    792  1.3  christos   /* We need at least one page.  */
    793  1.3  christos   if (pages == 0)
    794  1.3  christos     pages = 1;
    795  1.3  christos 
    796  1.3  christos   /* The buffer size can be requested in powers of two pages.  Adjust PAGES
    797  1.3  christos      to the next power of two.  */
    798  1.3  christos   for (pg = 0; pages != (1u << pg); ++pg)
    799  1.3  christos     if ((pages & (1u << pg)) != 0)
    800  1.3  christos       pages += (1u << pg);
    801  1.3  christos 
    802  1.3  christos   /* We try to allocate the requested size.
    803  1.3  christos      If that fails, try to get as much as we can.  */
    804  1.3  christos   for (; pages > 0; pages >>= 1)
    805  1.1  christos     {
    806  1.3  christos       size_t length;
    807  1.3  christos 
    808  1.3  christos       size = pages * PAGE_SIZE;
    809  1.3  christos       length = size;
    810  1.3  christos 
    811  1.3  christos       /* Check for overflows.  */
    812  1.3  christos       if ((unsigned long long) length < size)
    813  1.1  christos 	continue;
    814  1.1  christos 
    815  1.3  christos       header->aux_size = size;
    816  1.3  christos 
    817  1.3  christos       pt->pt.mem = mmap (NULL, length, PROT_READ, MAP_SHARED, pt->file,
    818  1.3  christos 			 header->aux_offset);
    819  1.3  christos       if (pt->pt.mem != MAP_FAILED)
    820  1.3  christos 	break;
    821  1.1  christos     }
    822  1.1  christos 
    823  1.3  christos   if (pages == 0)
    824  1.3  christos     goto err_conf;
    825  1.3  christos 
    826  1.3  christos   pt->header = header;
    827  1.3  christos   pt->pt.size = size;
    828  1.3  christos   pt->pt.data_head = &header->aux_head;
    829  1.3  christos 
    830  1.3  christos   tinfo->conf.pt.size = size;
    831  1.3  christos   return tinfo;
    832  1.3  christos 
    833  1.3  christos  err_conf:
    834  1.3  christos   munmap((void *) header, PAGE_SIZE);
    835  1.3  christos 
    836  1.3  christos  err_file:
    837  1.3  christos   close (pt->file);
    838  1.1  christos 
    839  1.1  christos  err:
    840  1.1  christos   xfree (tinfo);
    841  1.1  christos   return NULL;
    842  1.1  christos }
    843  1.1  christos 
    844  1.3  christos #else /* !defined (PERF_ATTR_SIZE_VER5) */
    845  1.3  christos 
    846  1.3  christos static struct btrace_target_info *
    847  1.3  christos linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf)
    848  1.3  christos {
    849  1.3  christos   errno = EOPNOTSUPP;
    850  1.3  christos   return NULL;
    851  1.3  christos }
    852  1.3  christos 
    853  1.3  christos #endif /* !defined (PERF_ATTR_SIZE_VER5) */
    854  1.3  christos 
    855  1.1  christos /* See linux-btrace.h.  */
    856  1.1  christos 
    857  1.3  christos struct btrace_target_info *
    858  1.3  christos linux_enable_btrace (ptid_t ptid, const struct btrace_config *conf)
    859  1.1  christos {
    860  1.3  christos   struct btrace_target_info *tinfo;
    861  1.3  christos 
    862  1.3  christos   tinfo = NULL;
    863  1.3  christos   switch (conf->format)
    864  1.3  christos     {
    865  1.3  christos     case BTRACE_FORMAT_NONE:
    866  1.3  christos       break;
    867  1.3  christos 
    868  1.3  christos     case BTRACE_FORMAT_BTS:
    869  1.3  christos       tinfo = linux_enable_bts (ptid, &conf->bts);
    870  1.3  christos       break;
    871  1.3  christos 
    872  1.3  christos     case BTRACE_FORMAT_PT:
    873  1.3  christos       tinfo = linux_enable_pt (ptid, &conf->pt);
    874  1.3  christos       break;
    875  1.3  christos     }
    876  1.3  christos 
    877  1.3  christos   return tinfo;
    878  1.3  christos }
    879  1.1  christos 
    880  1.3  christos /* Disable BTS tracing.  */
    881  1.1  christos 
    882  1.3  christos static enum btrace_error
    883  1.3  christos linux_disable_bts (struct btrace_tinfo_bts *tinfo)
    884  1.3  christos {
    885  1.3  christos   munmap((void *) tinfo->header, tinfo->bts.size + PAGE_SIZE);
    886  1.1  christos   close (tinfo->file);
    887  1.1  christos 
    888  1.1  christos   return BTRACE_ERR_NONE;
    889  1.1  christos }
    890  1.1  christos 
    891  1.3  christos /* Disable Intel(R) Processor Trace tracing.  */
    892  1.1  christos 
    893  1.3  christos static enum btrace_error
    894  1.3  christos linux_disable_pt (struct btrace_tinfo_pt *tinfo)
    895  1.1  christos {
    896  1.3  christos   munmap((void *) tinfo->pt.mem, tinfo->pt.size);
    897  1.3  christos   munmap((void *) tinfo->header, PAGE_SIZE);
    898  1.3  christos   close (tinfo->file);
    899  1.1  christos 
    900  1.3  christos   return BTRACE_ERR_NONE;
    901  1.1  christos }
    902  1.1  christos 
    903  1.1  christos /* See linux-btrace.h.  */
    904  1.1  christos 
    905  1.1  christos enum btrace_error
    906  1.3  christos linux_disable_btrace (struct btrace_target_info *tinfo)
    907  1.3  christos {
    908  1.3  christos   enum btrace_error errcode;
    909  1.3  christos 
    910  1.3  christos   errcode = BTRACE_ERR_NOT_SUPPORTED;
    911  1.3  christos   switch (tinfo->conf.format)
    912  1.3  christos     {
    913  1.3  christos     case BTRACE_FORMAT_NONE:
    914  1.3  christos       break;
    915  1.3  christos 
    916  1.3  christos     case BTRACE_FORMAT_BTS:
    917  1.3  christos       errcode = linux_disable_bts (&tinfo->variant.bts);
    918  1.3  christos       break;
    919  1.3  christos 
    920  1.3  christos     case BTRACE_FORMAT_PT:
    921  1.3  christos       errcode = linux_disable_pt (&tinfo->variant.pt);
    922  1.3  christos       break;
    923  1.3  christos     }
    924  1.3  christos 
    925  1.3  christos   if (errcode == BTRACE_ERR_NONE)
    926  1.3  christos     xfree (tinfo);
    927  1.3  christos 
    928  1.3  christos   return errcode;
    929  1.3  christos }
    930  1.3  christos 
    931  1.3  christos /* Read branch trace data in BTS format for the thread given by TINFO into
    932  1.3  christos    BTRACE using the TYPE reading method.  */
    933  1.3  christos 
    934  1.3  christos static enum btrace_error
    935  1.3  christos linux_read_bts (struct btrace_data_bts *btrace,
    936  1.3  christos 		struct btrace_target_info *tinfo,
    937  1.3  christos 		enum btrace_read_type type)
    938  1.1  christos {
    939  1.3  christos   struct perf_event_buffer *pevent;
    940  1.1  christos   const uint8_t *begin, *end, *start;
    941  1.3  christos   unsigned long long data_head, data_tail, buffer_size, size;
    942  1.3  christos   unsigned int retries = 5;
    943  1.3  christos 
    944  1.3  christos   pevent = &tinfo->variant.bts.bts;
    945  1.1  christos 
    946  1.1  christos   /* For delta reads, we return at least the partial last block containing
    947  1.1  christos      the current PC.  */
    948  1.3  christos   if (type == BTRACE_READ_NEW && !perf_event_new_data (pevent))
    949  1.1  christos     return BTRACE_ERR_NONE;
    950  1.1  christos 
    951  1.3  christos   buffer_size = pevent->size;
    952  1.3  christos   data_tail = pevent->last_head;
    953  1.1  christos 
    954  1.1  christos   /* We may need to retry reading the trace.  See below.  */
    955  1.1  christos   while (retries--)
    956  1.1  christos     {
    957  1.3  christos       data_head = *pevent->data_head;
    958  1.1  christos 
    959  1.1  christos       /* Delete any leftover trace from the previous iteration.  */
    960  1.3  christos       VEC_free (btrace_block_s, btrace->blocks);
    961  1.1  christos 
    962  1.1  christos       if (type == BTRACE_READ_DELTA)
    963  1.1  christos 	{
    964  1.1  christos 	  /* Determine the number of bytes to read and check for buffer
    965  1.1  christos 	     overflows.  */
    966  1.1  christos 
    967  1.1  christos 	  /* Check for data head overflows.  We might be able to recover from
    968  1.1  christos 	     those but they are very unlikely and it's not really worth the
    969  1.1  christos 	     effort, I think.  */
    970  1.1  christos 	  if (data_head < data_tail)
    971  1.1  christos 	    return BTRACE_ERR_OVERFLOW;
    972  1.1  christos 
    973  1.1  christos 	  /* If the buffer is smaller than the trace delta, we overflowed.  */
    974  1.1  christos 	  size = data_head - data_tail;
    975  1.1  christos 	  if (buffer_size < size)
    976  1.1  christos 	    return BTRACE_ERR_OVERFLOW;
    977  1.1  christos 	}
    978  1.1  christos       else
    979  1.1  christos 	{
    980  1.1  christos 	  /* Read the entire buffer.  */
    981  1.1  christos 	  size = buffer_size;
    982  1.1  christos 
    983  1.1  christos 	  /* Adjust the size if the buffer has not overflowed, yet.  */
    984  1.1  christos 	  if (data_head < size)
    985  1.1  christos 	    size = data_head;
    986  1.1  christos 	}
    987  1.1  christos 
    988  1.1  christos       /* Data_head keeps growing; the buffer itself is circular.  */
    989  1.3  christos       begin = pevent->mem;
    990  1.1  christos       start = begin + data_head % buffer_size;
    991  1.1  christos 
    992  1.1  christos       if (data_head <= buffer_size)
    993  1.1  christos 	end = start;
    994  1.1  christos       else
    995  1.3  christos 	end = begin + pevent->size;
    996  1.1  christos 
    997  1.3  christos       btrace->blocks = perf_event_read_bts (tinfo, begin, end, start, size);
    998  1.1  christos 
    999  1.1  christos       /* The stopping thread notifies its ptracer before it is scheduled out.
   1000  1.1  christos 	 On multi-core systems, the debugger might therefore run while the
   1001  1.1  christos 	 kernel might be writing the last branch trace records.
   1002  1.1  christos 
   1003  1.1  christos 	 Let's check whether the data head moved while we read the trace.  */
   1004  1.3  christos       if (data_head == *pevent->data_head)
   1005  1.1  christos 	break;
   1006  1.1  christos     }
   1007  1.1  christos 
   1008  1.3  christos   pevent->last_head = data_head;
   1009  1.1  christos 
   1010  1.1  christos   /* Prune the incomplete last block (i.e. the first one of inferior execution)
   1011  1.1  christos      if we're not doing a delta read.  There is no way of filling in its zeroed
   1012  1.1  christos      BEGIN element.  */
   1013  1.3  christos   if (!VEC_empty (btrace_block_s, btrace->blocks)
   1014  1.3  christos       && type != BTRACE_READ_DELTA)
   1015  1.3  christos     VEC_pop (btrace_block_s, btrace->blocks);
   1016  1.1  christos 
   1017  1.1  christos   return BTRACE_ERR_NONE;
   1018  1.1  christos }
   1019  1.1  christos 
   1020  1.3  christos /* Fill in the Intel(R) Processor Trace configuration information.  */
   1021  1.3  christos 
   1022  1.3  christos static void
   1023  1.3  christos linux_fill_btrace_pt_config (struct btrace_data_pt_config *conf)
   1024  1.3  christos {
   1025  1.3  christos   conf->cpu = btrace_this_cpu ();
   1026  1.3  christos }
   1027  1.3  christos 
   1028  1.3  christos /* Read branch trace data in Intel(R) Processor Trace format for the thread
   1029  1.3  christos    given by TINFO into BTRACE using the TYPE reading method.  */
   1030  1.3  christos 
   1031  1.3  christos static enum btrace_error
   1032  1.3  christos linux_read_pt (struct btrace_data_pt *btrace,
   1033  1.3  christos 	       struct btrace_target_info *tinfo,
   1034  1.3  christos 	       enum btrace_read_type type)
   1035  1.3  christos {
   1036  1.3  christos   struct perf_event_buffer *pt;
   1037  1.3  christos 
   1038  1.3  christos   pt = &tinfo->variant.pt.pt;
   1039  1.3  christos 
   1040  1.3  christos   linux_fill_btrace_pt_config (&btrace->config);
   1041  1.3  christos 
   1042  1.3  christos   switch (type)
   1043  1.3  christos     {
   1044  1.3  christos     case BTRACE_READ_DELTA:
   1045  1.3  christos       /* We don't support delta reads.  The data head (i.e. aux_head) wraps
   1046  1.3  christos 	 around to stay inside the aux buffer.  */
   1047  1.3  christos       return BTRACE_ERR_NOT_SUPPORTED;
   1048  1.3  christos 
   1049  1.3  christos     case BTRACE_READ_NEW:
   1050  1.3  christos       if (!perf_event_new_data (pt))
   1051  1.3  christos 	return BTRACE_ERR_NONE;
   1052  1.3  christos 
   1053  1.3  christos       /* Fall through.  */
   1054  1.3  christos     case BTRACE_READ_ALL:
   1055  1.3  christos       perf_event_read_all (pt, &btrace->data, &btrace->size);
   1056  1.3  christos       return BTRACE_ERR_NONE;
   1057  1.3  christos     }
   1058  1.3  christos 
   1059  1.3  christos   internal_error (__FILE__, __LINE__, _("Unkown btrace read type."));
   1060  1.3  christos }
   1061  1.3  christos 
   1062  1.3  christos /* See linux-btrace.h.  */
   1063  1.3  christos 
   1064  1.3  christos enum btrace_error
   1065  1.3  christos linux_read_btrace (struct btrace_data *btrace,
   1066  1.3  christos 		   struct btrace_target_info *tinfo,
   1067  1.3  christos 		   enum btrace_read_type type)
   1068  1.3  christos {
   1069  1.3  christos   switch (tinfo->conf.format)
   1070  1.3  christos     {
   1071  1.3  christos     case BTRACE_FORMAT_NONE:
   1072  1.3  christos       return BTRACE_ERR_NOT_SUPPORTED;
   1073  1.3  christos 
   1074  1.3  christos     case BTRACE_FORMAT_BTS:
   1075  1.3  christos       /* We read btrace in BTS format.  */
   1076  1.3  christos       btrace->format = BTRACE_FORMAT_BTS;
   1077  1.3  christos       btrace->variant.bts.blocks = NULL;
   1078  1.3  christos 
   1079  1.3  christos       return linux_read_bts (&btrace->variant.bts, tinfo, type);
   1080  1.3  christos 
   1081  1.3  christos     case BTRACE_FORMAT_PT:
   1082  1.3  christos       /* We read btrace in Intel(R) Processor Trace format.  */
   1083  1.3  christos       btrace->format = BTRACE_FORMAT_PT;
   1084  1.3  christos       btrace->variant.pt.data = NULL;
   1085  1.3  christos       btrace->variant.pt.size = 0;
   1086  1.3  christos 
   1087  1.3  christos       return linux_read_pt (&btrace->variant.pt, tinfo, type);
   1088  1.3  christos     }
   1089  1.3  christos 
   1090  1.3  christos   internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
   1091  1.3  christos }
   1092  1.3  christos 
   1093  1.3  christos /* See linux-btrace.h.  */
   1094  1.3  christos 
   1095  1.3  christos const struct btrace_config *
   1096  1.3  christos linux_btrace_conf (const struct btrace_target_info *tinfo)
   1097  1.3  christos {
   1098  1.3  christos   return &tinfo->conf;
   1099  1.3  christos }
   1100  1.3  christos 
   1101  1.1  christos #else /* !HAVE_LINUX_PERF_EVENT_H */
   1102  1.1  christos 
   1103  1.1  christos /* See linux-btrace.h.  */
   1104  1.1  christos 
   1105  1.1  christos int
   1106  1.3  christos linux_supports_btrace (struct target_ops *ops, enum btrace_format format)
   1107  1.1  christos {
   1108  1.1  christos   return 0;
   1109  1.1  christos }
   1110  1.1  christos 
   1111  1.1  christos /* See linux-btrace.h.  */
   1112  1.1  christos 
   1113  1.1  christos struct btrace_target_info *
   1114  1.3  christos linux_enable_btrace (ptid_t ptid, const struct btrace_config *conf)
   1115  1.1  christos {
   1116  1.1  christos   return NULL;
   1117  1.1  christos }
   1118  1.1  christos 
   1119  1.1  christos /* See linux-btrace.h.  */
   1120  1.1  christos 
   1121  1.1  christos enum btrace_error
   1122  1.1  christos linux_disable_btrace (struct btrace_target_info *tinfo)
   1123  1.1  christos {
   1124  1.1  christos   return BTRACE_ERR_NOT_SUPPORTED;
   1125  1.1  christos }
   1126  1.1  christos 
   1127  1.1  christos /* See linux-btrace.h.  */
   1128  1.1  christos 
   1129  1.1  christos enum btrace_error
   1130  1.3  christos linux_read_btrace (struct btrace_data *btrace,
   1131  1.1  christos 		   struct btrace_target_info *tinfo,
   1132  1.1  christos 		   enum btrace_read_type type)
   1133  1.1  christos {
   1134  1.1  christos   return BTRACE_ERR_NOT_SUPPORTED;
   1135  1.1  christos }
   1136  1.1  christos 
   1137  1.3  christos /* See linux-btrace.h.  */
   1138  1.3  christos 
   1139  1.3  christos const struct btrace_config *
   1140  1.3  christos linux_btrace_conf (const struct btrace_target_info *tinfo)
   1141  1.3  christos {
   1142  1.3  christos   return NULL;
   1143  1.3  christos }
   1144  1.3  christos 
   1145  1.1  christos #endif /* !HAVE_LINUX_PERF_EVENT_H */
   1146