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