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