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