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