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