record-btrace: add bts buffer size configuration option
[external/binutils.git] / gdb / nat / linux-btrace.c
1 /* Linux-dependent part of branch trace support for GDB, and GDBserver.
2
3    Copyright (C) 2013-2015 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 "common-defs.h"
23 #include "linux-btrace.h"
24 #include "common-regcache.h"
25 #include "gdb_wait.h"
26 #include "x86-cpuid.h"
27
28 #ifdef HAVE_SYS_SYSCALL_H
29 #include <sys/syscall.h>
30 #endif
31
32 #if HAVE_LINUX_PERF_EVENT_H && defined(SYS_perf_event_open)
33
34 #include <stdint.h>
35 #include <unistd.h>
36 #include <sys/mman.h>
37 #include <sys/user.h>
38 #include <sys/ptrace.h>
39 #include <sys/types.h>
40 #include <signal.h>
41
42 /* A branch trace record in perf_event.  */
43 struct perf_event_bts
44 {
45   /* The linear address of the branch source.  */
46   uint64_t from;
47
48   /* The linear address of the branch destination.  */
49   uint64_t to;
50 };
51
52 /* A perf_event branch trace sample.  */
53 struct perf_event_sample
54 {
55   /* The perf_event sample header.  */
56   struct perf_event_header header;
57
58   /* The perf_event branch tracing payload.  */
59   struct perf_event_bts bts;
60 };
61
62 /* Return non-zero if there is new data in PEVENT; zero otherwise.  */
63
64 static int
65 perf_event_new_data (const struct perf_event_buffer *pev)
66 {
67   return *pev->data_head != pev->last_head;
68 }
69
70 /* Check whether an address is in the kernel.  */
71
72 static inline int
73 perf_event_is_kernel_addr (const struct btrace_target_info *tinfo,
74                            uint64_t addr)
75 {
76   uint64_t mask;
77
78   /* If we don't know the size of a pointer, we can't check.  Let's assume it's
79      not a kernel address in this case.  */
80   if (tinfo->ptr_bits == 0)
81     return 0;
82
83   /* A bit mask for the most significant bit in an address.  */
84   mask = (uint64_t) 1 << (tinfo->ptr_bits - 1);
85
86   /* Check whether the most significant bit in the address is set.  */
87   return (addr & mask) != 0;
88 }
89
90 /* Check whether a perf event record should be skipped.  */
91
92 static inline int
93 perf_event_skip_bts_record (const struct btrace_target_info *tinfo,
94                             const struct perf_event_bts *bts)
95 {
96   /* The hardware may report branches from kernel into user space.  Branches
97      from user into kernel space will be suppressed.  We filter the former to
98      provide a consistent branch trace excluding kernel.  */
99   return perf_event_is_kernel_addr (tinfo, bts->from);
100 }
101
102 /* Perform a few consistency checks on a perf event sample record.  This is
103    meant to catch cases when we get out of sync with the perf event stream.  */
104
105 static inline int
106 perf_event_sample_ok (const struct perf_event_sample *sample)
107 {
108   if (sample->header.type != PERF_RECORD_SAMPLE)
109     return 0;
110
111   if (sample->header.size != sizeof (*sample))
112     return 0;
113
114   return 1;
115 }
116
117 /* Branch trace is collected in a circular buffer [begin; end) as pairs of from
118    and to addresses (plus a header).
119
120    Start points into that buffer at the next sample position.
121    We read the collected samples backwards from start.
122
123    While reading the samples, we convert the information into a list of blocks.
124    For two adjacent samples s1 and s2, we form a block b such that b.begin =
125    s1.to and b.end = s2.from.
126
127    In case the buffer overflows during sampling, one sample may have its lower
128    part at the end and its upper part at the beginning of the buffer.  */
129
130 static VEC (btrace_block_s) *
131 perf_event_read_bts (struct btrace_target_info* tinfo, const uint8_t *begin,
132                      const uint8_t *end, const uint8_t *start,
133                      unsigned long long size)
134 {
135   VEC (btrace_block_s) *btrace = NULL;
136   struct perf_event_sample sample;
137   unsigned long long read = 0;
138   struct btrace_block block = { 0, 0 };
139   struct regcache *regcache;
140
141   gdb_assert (begin <= start);
142   gdb_assert (start <= end);
143
144   /* The first block ends at the current pc.  */
145   regcache = get_thread_regcache_for_ptid (tinfo->ptid);
146   block.end = regcache_read_pc (regcache);
147
148   /* The buffer may contain a partial record as its last entry (i.e. when the
149      buffer size is not a multiple of the sample size).  */
150   read = sizeof (sample) - 1;
151
152   for (; read < size; read += sizeof (sample))
153     {
154       const struct perf_event_sample *psample;
155
156       /* Find the next perf_event sample in a backwards traversal.  */
157       start -= sizeof (sample);
158
159       /* If we're still inside the buffer, we're done.  */
160       if (begin <= start)
161         psample = (const struct perf_event_sample *) start;
162       else
163         {
164           int missing;
165
166           /* We're to the left of the ring buffer, we will wrap around and
167              reappear at the very right of the ring buffer.  */
168
169           missing = (begin - start);
170           start = (end - missing);
171
172           /* If the entire sample is missing, we're done.  */
173           if (missing == sizeof (sample))
174             psample = (const struct perf_event_sample *) start;
175           else
176             {
177               uint8_t *stack;
178
179               /* The sample wrapped around.  The lower part is at the end and
180                  the upper part is at the beginning of the buffer.  */
181               stack = (uint8_t *) &sample;
182
183               /* Copy the two parts so we have a contiguous sample.  */
184               memcpy (stack, start, missing);
185               memcpy (stack + missing, begin, sizeof (sample) - missing);
186
187               psample = &sample;
188             }
189         }
190
191       if (!perf_event_sample_ok (psample))
192         {
193           warning (_("Branch trace may be incomplete."));
194           break;
195         }
196
197       if (perf_event_skip_bts_record (tinfo, &psample->bts))
198         continue;
199
200       /* We found a valid sample, so we can complete the current block.  */
201       block.begin = psample->bts.to;
202
203       VEC_safe_push (btrace_block_s, btrace, &block);
204
205       /* Start the next block.  */
206       block.end = psample->bts.from;
207     }
208
209   /* Push the last block (i.e. the first one of inferior execution), as well.
210      We don't know where it ends, but we know where it starts.  If we're
211      reading delta trace, we can fill in the start address later on.
212      Otherwise we will prune it.  */
213   block.begin = 0;
214   VEC_safe_push (btrace_block_s, btrace, &block);
215
216   return btrace;
217 }
218
219 /* Check whether the kernel supports BTS.  */
220
221 static int
222 kernel_supports_bts (void)
223 {
224   struct perf_event_attr attr;
225   pid_t child, pid;
226   int status, file;
227
228   errno = 0;
229   child = fork ();
230   switch (child)
231     {
232     case -1:
233       warning (_("test bts: cannot fork: %s."), strerror (errno));
234       return 0;
235
236     case 0:
237       status = ptrace (PTRACE_TRACEME, 0, NULL, NULL);
238       if (status != 0)
239         {
240           warning (_("test bts: cannot PTRACE_TRACEME: %s."),
241                    strerror (errno));
242           _exit (1);
243         }
244
245       status = raise (SIGTRAP);
246       if (status != 0)
247         {
248           warning (_("test bts: cannot raise SIGTRAP: %s."),
249                    strerror (errno));
250           _exit (1);
251         }
252
253       _exit (1);
254
255     default:
256       pid = waitpid (child, &status, 0);
257       if (pid != child)
258         {
259           warning (_("test bts: bad pid %ld, error: %s."),
260                    (long) pid, strerror (errno));
261           return 0;
262         }
263
264       if (!WIFSTOPPED (status))
265         {
266           warning (_("test bts: expected stop. status: %d."),
267                    status);
268           return 0;
269         }
270
271       memset (&attr, 0, sizeof (attr));
272
273       attr.type = PERF_TYPE_HARDWARE;
274       attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
275       attr.sample_period = 1;
276       attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_ADDR;
277       attr.exclude_kernel = 1;
278       attr.exclude_hv = 1;
279       attr.exclude_idle = 1;
280
281       file = syscall (SYS_perf_event_open, &attr, child, -1, -1, 0);
282       if (file >= 0)
283         close (file);
284
285       kill (child, SIGKILL);
286       ptrace (PTRACE_KILL, child, NULL, NULL);
287
288       pid = waitpid (child, &status, 0);
289       if (pid != child)
290         {
291           warning (_("test bts: bad pid %ld, error: %s."),
292                    (long) pid, strerror (errno));
293           if (!WIFSIGNALED (status))
294             warning (_("test bts: expected killed. status: %d."),
295                      status);
296         }
297
298       return (file >= 0);
299     }
300 }
301
302 /* Check whether an Intel cpu supports BTS.  */
303
304 static int
305 intel_supports_bts (void)
306 {
307   unsigned int cpuid, model, family;
308
309   if (!x86_cpuid (1, &cpuid, NULL, NULL, NULL))
310     return 0;
311
312   family = (cpuid >> 8) & 0xf;
313   model = (cpuid >> 4) & 0xf;
314
315   switch (family)
316     {
317     case 0x6:
318       model += (cpuid >> 12) & 0xf0;
319
320       switch (model)
321         {
322         case 0x1a: /* Nehalem */
323         case 0x1f:
324         case 0x1e:
325         case 0x2e:
326         case 0x25: /* Westmere */
327         case 0x2c:
328         case 0x2f:
329         case 0x2a: /* Sandy Bridge */
330         case 0x2d:
331         case 0x3a: /* Ivy Bridge */
332
333           /* AAJ122: LBR, BTM, or BTS records may have incorrect branch
334              "from" information afer an EIST transition, T-states, C1E, or
335              Adaptive Thermal Throttling.  */
336           return 0;
337         }
338     }
339
340   return 1;
341 }
342
343 /* Check whether the cpu supports BTS.  */
344
345 static int
346 cpu_supports_bts (void)
347 {
348   unsigned int ebx, ecx, edx;
349
350   if (!x86_cpuid (0, NULL, &ebx, &ecx, &edx))
351     return 0;
352
353   if (ebx == signature_INTEL_ebx && ecx == signature_INTEL_ecx
354       && edx == signature_INTEL_edx)
355     return intel_supports_bts ();
356
357   /* Don't know about others.  Let's assume they do.  */
358   return 1;
359 }
360
361 /* Check whether the linux target supports BTS.  */
362
363 static int
364 linux_supports_bts (void)
365 {
366   static int cached;
367
368   if (cached == 0)
369     {
370       if (!kernel_supports_bts ())
371         cached = -1;
372       else if (!cpu_supports_bts ())
373         cached = -1;
374       else
375         cached = 1;
376     }
377
378   return cached > 0;
379 }
380
381 /* See linux-btrace.h.  */
382
383 int
384 linux_supports_btrace (struct target_ops *ops, enum btrace_format format)
385 {
386   switch (format)
387     {
388     case BTRACE_FORMAT_NONE:
389       return 0;
390
391     case BTRACE_FORMAT_BTS:
392       return linux_supports_bts ();
393     }
394
395   internal_error (__FILE__, __LINE__, _("Unknown branch trace format"));
396 }
397
398 /* Enable branch tracing in BTS format.  */
399
400 static struct btrace_target_info *
401 linux_enable_bts (ptid_t ptid, const struct btrace_config_bts *conf)
402 {
403   struct perf_event_mmap_page *header;
404   struct btrace_target_info *tinfo;
405   struct btrace_tinfo_bts *bts;
406   unsigned long long size, pages;
407   int pid, pg;
408
409   tinfo = xzalloc (sizeof (*tinfo));
410   tinfo->ptid = ptid;
411   tinfo->ptr_bits = 0;
412
413   tinfo->conf.format = BTRACE_FORMAT_BTS;
414   bts = &tinfo->variant.bts;
415
416   bts->attr.size = sizeof (bts->attr);
417   bts->attr.type = PERF_TYPE_HARDWARE;
418   bts->attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
419   bts->attr.sample_period = 1;
420
421   /* We sample from and to address.  */
422   bts->attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_ADDR;
423
424   bts->attr.exclude_kernel = 1;
425   bts->attr.exclude_hv = 1;
426   bts->attr.exclude_idle = 1;
427
428   pid = ptid_get_lwp (ptid);
429   if (pid == 0)
430     pid = ptid_get_pid (ptid);
431
432   errno = 0;
433   bts->file = syscall (SYS_perf_event_open, &bts->attr, pid, -1, -1, 0);
434   if (bts->file < 0)
435     goto err;
436
437   /* Convert the requested size in bytes to pages (rounding up).  */
438   pages = (((unsigned long long) conf->size) + PAGE_SIZE - 1) / PAGE_SIZE;
439   /* We need at least one page.  */
440   if (pages == 0)
441     pages = 1;
442
443   /* The buffer size can be requested in powers of two pages.  Adjust PAGES
444      to the next power of two.  */
445   for (pg = 0; pages != (1u << pg); ++pg)
446     if ((pages & (1u << pg)) != 0)
447       pages += (1u << pg);
448
449   /* We try to allocate the requested size.
450      If that fails, try to get as much as we can.  */
451   for (; pages > 0; pages >>= 1)
452     {
453       size_t length;
454
455       size = pages * PAGE_SIZE;
456       length = size + PAGE_SIZE;
457
458       /* Check for overflows.  */
459       if ((unsigned long long) length < size)
460         continue;
461
462       /* The number of pages we request needs to be a power of two.  */
463       header = mmap (NULL, length, PROT_READ, MAP_SHARED, bts->file, 0);
464       if (header != MAP_FAILED)
465         break;
466     }
467
468   if (header == MAP_FAILED)
469     goto err_file;
470
471   bts->header = header;
472   bts->bts.mem = ((const uint8_t *) header) + PAGE_SIZE;
473   bts->bts.size = size;
474   bts->bts.data_head = &header->data_head;
475   bts->bts.last_head = 0;
476
477   tinfo->conf.bts.size = size;
478   return tinfo;
479
480  err_file:
481   /* We were not able to allocate any buffer.  */
482   close (bts->file);
483
484  err:
485   xfree (tinfo);
486   return NULL;
487 }
488
489 /* See linux-btrace.h.  */
490
491 struct btrace_target_info *
492 linux_enable_btrace (ptid_t ptid, const struct btrace_config *conf)
493 {
494   struct btrace_target_info *tinfo;
495
496   tinfo = NULL;
497   switch (conf->format)
498     {
499     case BTRACE_FORMAT_NONE:
500       break;
501
502     case BTRACE_FORMAT_BTS:
503       tinfo = linux_enable_bts (ptid, &conf->bts);
504       break;
505     }
506
507   return tinfo;
508 }
509
510 /* Disable BTS tracing.  */
511
512 static enum btrace_error
513 linux_disable_bts (struct btrace_tinfo_bts *tinfo)
514 {
515   munmap((void *) tinfo->header, tinfo->bts.size + PAGE_SIZE);
516   close (tinfo->file);
517
518   return BTRACE_ERR_NONE;
519 }
520
521 /* See linux-btrace.h.  */
522
523 enum btrace_error
524 linux_disable_btrace (struct btrace_target_info *tinfo)
525 {
526   enum btrace_error errcode;
527
528   errcode = BTRACE_ERR_NOT_SUPPORTED;
529   switch (tinfo->conf.format)
530     {
531     case BTRACE_FORMAT_NONE:
532       break;
533
534     case BTRACE_FORMAT_BTS:
535       errcode = linux_disable_bts (&tinfo->variant.bts);
536       break;
537     }
538
539   if (errcode == BTRACE_ERR_NONE)
540     xfree (tinfo);
541
542   return errcode;
543 }
544
545 /* Read branch trace data in BTS format for the thread given by TINFO into
546    BTRACE using the TYPE reading method.  */
547
548 static enum btrace_error
549 linux_read_bts (struct btrace_data_bts *btrace,
550                 struct btrace_target_info *tinfo,
551                 enum btrace_read_type type)
552 {
553   struct perf_event_buffer *pevent;
554   const uint8_t *begin, *end, *start;
555   unsigned long long data_head, data_tail, buffer_size, size;
556   unsigned int retries = 5;
557
558   pevent = &tinfo->variant.bts.bts;
559
560   /* For delta reads, we return at least the partial last block containing
561      the current PC.  */
562   if (type == BTRACE_READ_NEW && !perf_event_new_data (pevent))
563     return BTRACE_ERR_NONE;
564
565   buffer_size = pevent->size;
566   data_tail = pevent->last_head;
567
568   /* We may need to retry reading the trace.  See below.  */
569   while (retries--)
570     {
571       data_head = *pevent->data_head;
572
573       /* Delete any leftover trace from the previous iteration.  */
574       VEC_free (btrace_block_s, btrace->blocks);
575
576       if (type == BTRACE_READ_DELTA)
577         {
578           /* Determine the number of bytes to read and check for buffer
579              overflows.  */
580
581           /* Check for data head overflows.  We might be able to recover from
582              those but they are very unlikely and it's not really worth the
583              effort, I think.  */
584           if (data_head < data_tail)
585             return BTRACE_ERR_OVERFLOW;
586
587           /* If the buffer is smaller than the trace delta, we overflowed.  */
588           size = data_head - data_tail;
589           if (buffer_size < size)
590             return BTRACE_ERR_OVERFLOW;
591         }
592       else
593         {
594           /* Read the entire buffer.  */
595           size = buffer_size;
596
597           /* Adjust the size if the buffer has not overflowed, yet.  */
598           if (data_head < size)
599             size = data_head;
600         }
601
602       /* Data_head keeps growing; the buffer itself is circular.  */
603       begin = pevent->mem;
604       start = begin + data_head % buffer_size;
605
606       if (data_head <= buffer_size)
607         end = start;
608       else
609         end = begin + pevent->size;
610
611       btrace->blocks = perf_event_read_bts (tinfo, begin, end, start, size);
612
613       /* The stopping thread notifies its ptracer before it is scheduled out.
614          On multi-core systems, the debugger might therefore run while the
615          kernel might be writing the last branch trace records.
616
617          Let's check whether the data head moved while we read the trace.  */
618       if (data_head == *pevent->data_head)
619         break;
620     }
621
622   pevent->last_head = data_head;
623
624   /* Prune the incomplete last block (i.e. the first one of inferior execution)
625      if we're not doing a delta read.  There is no way of filling in its zeroed
626      BEGIN element.  */
627   if (!VEC_empty (btrace_block_s, btrace->blocks)
628       && type != BTRACE_READ_DELTA)
629     VEC_pop (btrace_block_s, btrace->blocks);
630
631   return BTRACE_ERR_NONE;
632 }
633
634 /* See linux-btrace.h.  */
635
636 enum btrace_error
637 linux_read_btrace (struct btrace_data *btrace,
638                    struct btrace_target_info *tinfo,
639                    enum btrace_read_type type)
640 {
641   switch (tinfo->conf.format)
642     {
643     case BTRACE_FORMAT_NONE:
644       return BTRACE_ERR_NOT_SUPPORTED;
645
646     case BTRACE_FORMAT_BTS:
647       /* We read btrace in BTS format.  */
648       btrace->format = BTRACE_FORMAT_BTS;
649       btrace->variant.bts.blocks = NULL;
650
651       return linux_read_bts (&btrace->variant.bts, tinfo, type);
652     }
653
654   internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
655 }
656
657 /* See linux-btrace.h.  */
658
659 const struct btrace_config *
660 linux_btrace_conf (const struct btrace_target_info *tinfo)
661 {
662   return &tinfo->conf;
663 }
664
665 #else /* !HAVE_LINUX_PERF_EVENT_H */
666
667 /* See linux-btrace.h.  */
668
669 int
670 linux_supports_btrace (struct target_ops *ops, enum btrace_format format)
671 {
672   return 0;
673 }
674
675 /* See linux-btrace.h.  */
676
677 struct btrace_target_info *
678 linux_enable_btrace (ptid_t ptid, const struct btrace_config *conf)
679 {
680   return NULL;
681 }
682
683 /* See linux-btrace.h.  */
684
685 enum btrace_error
686 linux_disable_btrace (struct btrace_target_info *tinfo)
687 {
688   return BTRACE_ERR_NOT_SUPPORTED;
689 }
690
691 /* See linux-btrace.h.  */
692
693 enum btrace_error
694 linux_read_btrace (struct btrace_data *btrace,
695                    struct btrace_target_info *tinfo,
696                    enum btrace_read_type type)
697 {
698   return BTRACE_ERR_NOT_SUPPORTED;
699 }
700
701 /* See linux-btrace.h.  */
702
703 const struct btrace_config *
704 linux_btrace_conf (const struct btrace_target_info *tinfo)
705 {
706   return NULL;
707 }
708
709 #endif /* !HAVE_LINUX_PERF_EVENT_H */