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