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