btrace: allow recording to be started (and stopped) for running threads
[deliverable/binutils-gdb.git] / gdb / btrace.c
1 /* Branch trace support for GDB, the GNU debugger.
2
3 Copyright (C) 2013-2017 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 "defs.h"
23 #include "btrace.h"
24 #include "gdbthread.h"
25 #include "inferior.h"
26 #include "target.h"
27 #include "record.h"
28 #include "symtab.h"
29 #include "disasm.h"
30 #include "source.h"
31 #include "filenames.h"
32 #include "xml-support.h"
33 #include "regcache.h"
34 #include "rsp-low.h"
35 #include "gdbcmd.h"
36 #include "cli/cli-utils.h"
37
38 #include <inttypes.h>
39 #include <ctype.h>
40 #include <algorithm>
41
42 /* Command lists for btrace maintenance commands. */
43 static struct cmd_list_element *maint_btrace_cmdlist;
44 static struct cmd_list_element *maint_btrace_set_cmdlist;
45 static struct cmd_list_element *maint_btrace_show_cmdlist;
46 static struct cmd_list_element *maint_btrace_pt_set_cmdlist;
47 static struct cmd_list_element *maint_btrace_pt_show_cmdlist;
48
49 /* Control whether to skip PAD packets when computing the packet history. */
50 static int maint_btrace_pt_skip_pad = 1;
51
52 /* A vector of function segments. */
53 typedef struct btrace_function * bfun_s;
54 DEF_VEC_P (bfun_s);
55
56 static void btrace_add_pc (struct thread_info *tp);
57
58 /* Print a record debug message. Use do ... while (0) to avoid ambiguities
59 when used in if statements. */
60
61 #define DEBUG(msg, args...) \
62 do \
63 { \
64 if (record_debug != 0) \
65 fprintf_unfiltered (gdb_stdlog, \
66 "[btrace] " msg "\n", ##args); \
67 } \
68 while (0)
69
70 #define DEBUG_FTRACE(msg, args...) DEBUG ("[ftrace] " msg, ##args)
71
72 /* Return the function name of a recorded function segment for printing.
73 This function never returns NULL. */
74
75 static const char *
76 ftrace_print_function_name (const struct btrace_function *bfun)
77 {
78 struct minimal_symbol *msym;
79 struct symbol *sym;
80
81 msym = bfun->msym;
82 sym = bfun->sym;
83
84 if (sym != NULL)
85 return SYMBOL_PRINT_NAME (sym);
86
87 if (msym != NULL)
88 return MSYMBOL_PRINT_NAME (msym);
89
90 return "<unknown>";
91 }
92
93 /* Return the file name of a recorded function segment for printing.
94 This function never returns NULL. */
95
96 static const char *
97 ftrace_print_filename (const struct btrace_function *bfun)
98 {
99 struct symbol *sym;
100 const char *filename;
101
102 sym = bfun->sym;
103
104 if (sym != NULL)
105 filename = symtab_to_filename_for_display (symbol_symtab (sym));
106 else
107 filename = "<unknown>";
108
109 return filename;
110 }
111
112 /* Return a string representation of the address of an instruction.
113 This function never returns NULL. */
114
115 static const char *
116 ftrace_print_insn_addr (const struct btrace_insn *insn)
117 {
118 if (insn == NULL)
119 return "<nil>";
120
121 return core_addr_to_string_nz (insn->pc);
122 }
123
124 /* Print an ftrace debug status message. */
125
126 static void
127 ftrace_debug (const struct btrace_function *bfun, const char *prefix)
128 {
129 const char *fun, *file;
130 unsigned int ibegin, iend;
131 int level;
132
133 fun = ftrace_print_function_name (bfun);
134 file = ftrace_print_filename (bfun);
135 level = bfun->level;
136
137 ibegin = bfun->insn_offset;
138 iend = ibegin + VEC_length (btrace_insn_s, bfun->insn);
139
140 DEBUG_FTRACE ("%s: fun = %s, file = %s, level = %d, insn = [%u; %u)",
141 prefix, fun, file, level, ibegin, iend);
142 }
143
144 /* Return non-zero if BFUN does not match MFUN and FUN,
145 return zero otherwise. */
146
147 static int
148 ftrace_function_switched (const struct btrace_function *bfun,
149 const struct minimal_symbol *mfun,
150 const struct symbol *fun)
151 {
152 struct minimal_symbol *msym;
153 struct symbol *sym;
154
155 msym = bfun->msym;
156 sym = bfun->sym;
157
158 /* If the minimal symbol changed, we certainly switched functions. */
159 if (mfun != NULL && msym != NULL
160 && strcmp (MSYMBOL_LINKAGE_NAME (mfun), MSYMBOL_LINKAGE_NAME (msym)) != 0)
161 return 1;
162
163 /* If the symbol changed, we certainly switched functions. */
164 if (fun != NULL && sym != NULL)
165 {
166 const char *bfname, *fname;
167
168 /* Check the function name. */
169 if (strcmp (SYMBOL_LINKAGE_NAME (fun), SYMBOL_LINKAGE_NAME (sym)) != 0)
170 return 1;
171
172 /* Check the location of those functions, as well. */
173 bfname = symtab_to_fullname (symbol_symtab (sym));
174 fname = symtab_to_fullname (symbol_symtab (fun));
175 if (filename_cmp (fname, bfname) != 0)
176 return 1;
177 }
178
179 /* If we lost symbol information, we switched functions. */
180 if (!(msym == NULL && sym == NULL) && mfun == NULL && fun == NULL)
181 return 1;
182
183 /* If we gained symbol information, we switched functions. */
184 if (msym == NULL && sym == NULL && !(mfun == NULL && fun == NULL))
185 return 1;
186
187 return 0;
188 }
189
190 /* Allocate and initialize a new branch trace function segment.
191 PREV is the chronologically preceding function segment.
192 MFUN and FUN are the symbol information we have for this function. */
193
194 static struct btrace_function *
195 ftrace_new_function (struct btrace_function *prev,
196 struct minimal_symbol *mfun,
197 struct symbol *fun)
198 {
199 struct btrace_function *bfun;
200
201 bfun = XCNEW (struct btrace_function);
202
203 bfun->msym = mfun;
204 bfun->sym = fun;
205 bfun->flow.prev = prev;
206
207 if (prev == NULL)
208 {
209 /* Start counting at one. */
210 bfun->number = 1;
211 bfun->insn_offset = 1;
212 }
213 else
214 {
215 gdb_assert (prev->flow.next == NULL);
216 prev->flow.next = bfun;
217
218 bfun->number = prev->number + 1;
219 bfun->insn_offset = (prev->insn_offset
220 + VEC_length (btrace_insn_s, prev->insn));
221 bfun->level = prev->level;
222 }
223
224 return bfun;
225 }
226
227 /* Update the UP field of a function segment. */
228
229 static void
230 ftrace_update_caller (struct btrace_function *bfun,
231 struct btrace_function *caller,
232 enum btrace_function_flag flags)
233 {
234 if (bfun->up != NULL)
235 ftrace_debug (bfun, "updating caller");
236
237 bfun->up = caller;
238 bfun->flags = flags;
239
240 ftrace_debug (bfun, "set caller");
241 ftrace_debug (caller, "..to");
242 }
243
244 /* Fix up the caller for all segments of a function. */
245
246 static void
247 ftrace_fixup_caller (struct btrace_function *bfun,
248 struct btrace_function *caller,
249 enum btrace_function_flag flags)
250 {
251 struct btrace_function *prev, *next;
252
253 ftrace_update_caller (bfun, caller, flags);
254
255 /* Update all function segments belonging to the same function. */
256 for (prev = bfun->segment.prev; prev != NULL; prev = prev->segment.prev)
257 ftrace_update_caller (prev, caller, flags);
258
259 for (next = bfun->segment.next; next != NULL; next = next->segment.next)
260 ftrace_update_caller (next, caller, flags);
261 }
262
263 /* Add a new function segment for a call.
264 CALLER is the chronologically preceding function segment.
265 MFUN and FUN are the symbol information we have for this function. */
266
267 static struct btrace_function *
268 ftrace_new_call (struct btrace_function *caller,
269 struct minimal_symbol *mfun,
270 struct symbol *fun)
271 {
272 struct btrace_function *bfun;
273
274 bfun = ftrace_new_function (caller, mfun, fun);
275 bfun->up = caller;
276 bfun->level += 1;
277
278 ftrace_debug (bfun, "new call");
279
280 return bfun;
281 }
282
283 /* Add a new function segment for a tail call.
284 CALLER is the chronologically preceding function segment.
285 MFUN and FUN are the symbol information we have for this function. */
286
287 static struct btrace_function *
288 ftrace_new_tailcall (struct btrace_function *caller,
289 struct minimal_symbol *mfun,
290 struct symbol *fun)
291 {
292 struct btrace_function *bfun;
293
294 bfun = ftrace_new_function (caller, mfun, fun);
295 bfun->up = caller;
296 bfun->level += 1;
297 bfun->flags |= BFUN_UP_LINKS_TO_TAILCALL;
298
299 ftrace_debug (bfun, "new tail call");
300
301 return bfun;
302 }
303
304 /* Return the caller of BFUN or NULL if there is none. This function skips
305 tail calls in the call chain. */
306 static struct btrace_function *
307 ftrace_get_caller (struct btrace_function *bfun)
308 {
309 for (; bfun != NULL; bfun = bfun->up)
310 if ((bfun->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
311 return bfun->up;
312
313 return NULL;
314 }
315
316 /* Find the innermost caller in the back trace of BFUN with MFUN/FUN
317 symbol information. */
318
319 static struct btrace_function *
320 ftrace_find_caller (struct btrace_function *bfun,
321 struct minimal_symbol *mfun,
322 struct symbol *fun)
323 {
324 for (; bfun != NULL; bfun = bfun->up)
325 {
326 /* Skip functions with incompatible symbol information. */
327 if (ftrace_function_switched (bfun, mfun, fun))
328 continue;
329
330 /* This is the function segment we're looking for. */
331 break;
332 }
333
334 return bfun;
335 }
336
337 /* Find the innermost caller in the back trace of BFUN, skipping all
338 function segments that do not end with a call instruction (e.g.
339 tail calls ending with a jump). */
340
341 static struct btrace_function *
342 ftrace_find_call (struct btrace_function *bfun)
343 {
344 for (; bfun != NULL; bfun = bfun->up)
345 {
346 struct btrace_insn *last;
347
348 /* Skip gaps. */
349 if (bfun->errcode != 0)
350 continue;
351
352 last = VEC_last (btrace_insn_s, bfun->insn);
353
354 if (last->iclass == BTRACE_INSN_CALL)
355 break;
356 }
357
358 return bfun;
359 }
360
361 /* Add a continuation segment for a function into which we return.
362 PREV is the chronologically preceding function segment.
363 MFUN and FUN are the symbol information we have for this function. */
364
365 static struct btrace_function *
366 ftrace_new_return (struct btrace_function *prev,
367 struct minimal_symbol *mfun,
368 struct symbol *fun)
369 {
370 struct btrace_function *bfun, *caller;
371
372 bfun = ftrace_new_function (prev, mfun, fun);
373
374 /* It is important to start at PREV's caller. Otherwise, we might find
375 PREV itself, if PREV is a recursive function. */
376 caller = ftrace_find_caller (prev->up, mfun, fun);
377 if (caller != NULL)
378 {
379 /* The caller of PREV is the preceding btrace function segment in this
380 function instance. */
381 gdb_assert (caller->segment.next == NULL);
382
383 caller->segment.next = bfun;
384 bfun->segment.prev = caller;
385
386 /* Maintain the function level. */
387 bfun->level = caller->level;
388
389 /* Maintain the call stack. */
390 bfun->up = caller->up;
391 bfun->flags = caller->flags;
392
393 ftrace_debug (bfun, "new return");
394 }
395 else
396 {
397 /* We did not find a caller. This could mean that something went
398 wrong or that the call is simply not included in the trace. */
399
400 /* Let's search for some actual call. */
401 caller = ftrace_find_call (prev->up);
402 if (caller == NULL)
403 {
404 /* There is no call in PREV's back trace. We assume that the
405 branch trace did not include it. */
406
407 /* Let's find the topmost function and add a new caller for it.
408 This should handle a series of initial tail calls. */
409 while (prev->up != NULL)
410 prev = prev->up;
411
412 bfun->level = prev->level - 1;
413
414 /* Fix up the call stack for PREV. */
415 ftrace_fixup_caller (prev, bfun, BFUN_UP_LINKS_TO_RET);
416
417 ftrace_debug (bfun, "new return - no caller");
418 }
419 else
420 {
421 /* There is a call in PREV's back trace to which we should have
422 returned but didn't. Let's start a new, separate back trace
423 from PREV's level. */
424 bfun->level = prev->level - 1;
425
426 /* We fix up the back trace for PREV but leave other function segments
427 on the same level as they are.
428 This should handle things like schedule () correctly where we're
429 switching contexts. */
430 prev->up = bfun;
431 prev->flags = BFUN_UP_LINKS_TO_RET;
432
433 ftrace_debug (bfun, "new return - unknown caller");
434 }
435 }
436
437 return bfun;
438 }
439
440 /* Add a new function segment for a function switch.
441 PREV is the chronologically preceding function segment.
442 MFUN and FUN are the symbol information we have for this function. */
443
444 static struct btrace_function *
445 ftrace_new_switch (struct btrace_function *prev,
446 struct minimal_symbol *mfun,
447 struct symbol *fun)
448 {
449 struct btrace_function *bfun;
450
451 /* This is an unexplained function switch. The call stack will likely
452 be wrong at this point. */
453 bfun = ftrace_new_function (prev, mfun, fun);
454
455 ftrace_debug (bfun, "new switch");
456
457 return bfun;
458 }
459
460 /* Add a new function segment for a gap in the trace due to a decode error.
461 PREV is the chronologically preceding function segment.
462 ERRCODE is the format-specific error code. */
463
464 static struct btrace_function *
465 ftrace_new_gap (struct btrace_function *prev, int errcode)
466 {
467 struct btrace_function *bfun;
468
469 /* We hijack prev if it was empty. */
470 if (prev != NULL && prev->errcode == 0
471 && VEC_empty (btrace_insn_s, prev->insn))
472 bfun = prev;
473 else
474 bfun = ftrace_new_function (prev, NULL, NULL);
475
476 bfun->errcode = errcode;
477
478 ftrace_debug (bfun, "new gap");
479
480 return bfun;
481 }
482
483 /* Update BFUN with respect to the instruction at PC. This may create new
484 function segments.
485 Return the chronologically latest function segment, never NULL. */
486
487 static struct btrace_function *
488 ftrace_update_function (struct btrace_function *bfun, CORE_ADDR pc)
489 {
490 struct bound_minimal_symbol bmfun;
491 struct minimal_symbol *mfun;
492 struct symbol *fun;
493 struct btrace_insn *last;
494
495 /* Try to determine the function we're in. We use both types of symbols
496 to avoid surprises when we sometimes get a full symbol and sometimes
497 only a minimal symbol. */
498 fun = find_pc_function (pc);
499 bmfun = lookup_minimal_symbol_by_pc (pc);
500 mfun = bmfun.minsym;
501
502 if (fun == NULL && mfun == NULL)
503 DEBUG_FTRACE ("no symbol at %s", core_addr_to_string_nz (pc));
504
505 /* If we didn't have a function or if we had a gap before, we create one. */
506 if (bfun == NULL || bfun->errcode != 0)
507 return ftrace_new_function (bfun, mfun, fun);
508
509 /* Check the last instruction, if we have one.
510 We do this check first, since it allows us to fill in the call stack
511 links in addition to the normal flow links. */
512 last = NULL;
513 if (!VEC_empty (btrace_insn_s, bfun->insn))
514 last = VEC_last (btrace_insn_s, bfun->insn);
515
516 if (last != NULL)
517 {
518 switch (last->iclass)
519 {
520 case BTRACE_INSN_RETURN:
521 {
522 const char *fname;
523
524 /* On some systems, _dl_runtime_resolve returns to the resolved
525 function instead of jumping to it. From our perspective,
526 however, this is a tailcall.
527 If we treated it as return, we wouldn't be able to find the
528 resolved function in our stack back trace. Hence, we would
529 lose the current stack back trace and start anew with an empty
530 back trace. When the resolved function returns, we would then
531 create a stack back trace with the same function names but
532 different frame id's. This will confuse stepping. */
533 fname = ftrace_print_function_name (bfun);
534 if (strcmp (fname, "_dl_runtime_resolve") == 0)
535 return ftrace_new_tailcall (bfun, mfun, fun);
536
537 return ftrace_new_return (bfun, mfun, fun);
538 }
539
540 case BTRACE_INSN_CALL:
541 /* Ignore calls to the next instruction. They are used for PIC. */
542 if (last->pc + last->size == pc)
543 break;
544
545 return ftrace_new_call (bfun, mfun, fun);
546
547 case BTRACE_INSN_JUMP:
548 {
549 CORE_ADDR start;
550
551 start = get_pc_function_start (pc);
552
553 /* A jump to the start of a function is (typically) a tail call. */
554 if (start == pc)
555 return ftrace_new_tailcall (bfun, mfun, fun);
556
557 /* If we can't determine the function for PC, we treat a jump at
558 the end of the block as tail call if we're switching functions
559 and as an intra-function branch if we don't. */
560 if (start == 0 && ftrace_function_switched (bfun, mfun, fun))
561 return ftrace_new_tailcall (bfun, mfun, fun);
562
563 break;
564 }
565 }
566 }
567
568 /* Check if we're switching functions for some other reason. */
569 if (ftrace_function_switched (bfun, mfun, fun))
570 {
571 DEBUG_FTRACE ("switching from %s in %s at %s",
572 ftrace_print_insn_addr (last),
573 ftrace_print_function_name (bfun),
574 ftrace_print_filename (bfun));
575
576 return ftrace_new_switch (bfun, mfun, fun);
577 }
578
579 return bfun;
580 }
581
582 /* Add the instruction at PC to BFUN's instructions. */
583
584 static void
585 ftrace_update_insns (struct btrace_function *bfun,
586 const struct btrace_insn *insn)
587 {
588 VEC_safe_push (btrace_insn_s, bfun->insn, insn);
589
590 if (record_debug > 1)
591 ftrace_debug (bfun, "update insn");
592 }
593
594 /* Classify the instruction at PC. */
595
596 static enum btrace_insn_class
597 ftrace_classify_insn (struct gdbarch *gdbarch, CORE_ADDR pc)
598 {
599 enum btrace_insn_class iclass;
600
601 iclass = BTRACE_INSN_OTHER;
602 TRY
603 {
604 if (gdbarch_insn_is_call (gdbarch, pc))
605 iclass = BTRACE_INSN_CALL;
606 else if (gdbarch_insn_is_ret (gdbarch, pc))
607 iclass = BTRACE_INSN_RETURN;
608 else if (gdbarch_insn_is_jump (gdbarch, pc))
609 iclass = BTRACE_INSN_JUMP;
610 }
611 CATCH (error, RETURN_MASK_ERROR)
612 {
613 }
614 END_CATCH
615
616 return iclass;
617 }
618
619 /* Try to match the back trace at LHS to the back trace at RHS. Returns the
620 number of matching function segments or zero if the back traces do not
621 match. */
622
623 static int
624 ftrace_match_backtrace (struct btrace_function *lhs,
625 struct btrace_function *rhs)
626 {
627 int matches;
628
629 for (matches = 0; lhs != NULL && rhs != NULL; ++matches)
630 {
631 if (ftrace_function_switched (lhs, rhs->msym, rhs->sym))
632 return 0;
633
634 lhs = ftrace_get_caller (lhs);
635 rhs = ftrace_get_caller (rhs);
636 }
637
638 return matches;
639 }
640
641 /* Add ADJUSTMENT to the level of BFUN and succeeding function segments. */
642
643 static void
644 ftrace_fixup_level (struct btrace_function *bfun, int adjustment)
645 {
646 if (adjustment == 0)
647 return;
648
649 DEBUG_FTRACE ("fixup level (%+d)", adjustment);
650 ftrace_debug (bfun, "..bfun");
651
652 for (; bfun != NULL; bfun = bfun->flow.next)
653 bfun->level += adjustment;
654 }
655
656 /* Recompute the global level offset. Traverse the function trace and compute
657 the global level offset as the negative of the minimal function level. */
658
659 static void
660 ftrace_compute_global_level_offset (struct btrace_thread_info *btinfo)
661 {
662 struct btrace_function *bfun, *end;
663 int level;
664
665 if (btinfo == NULL)
666 return;
667
668 bfun = btinfo->begin;
669 if (bfun == NULL)
670 return;
671
672 /* The last function segment contains the current instruction, which is not
673 really part of the trace. If it contains just this one instruction, we
674 stop when we reach it; otherwise, we let the below loop run to the end. */
675 end = btinfo->end;
676 if (VEC_length (btrace_insn_s, end->insn) > 1)
677 end = NULL;
678
679 level = INT_MAX;
680 for (; bfun != end; bfun = bfun->flow.next)
681 level = std::min (level, bfun->level);
682
683 DEBUG_FTRACE ("setting global level offset: %d", -level);
684 btinfo->level = -level;
685 }
686
687 /* Connect the function segments PREV and NEXT in a bottom-to-top walk as in
688 ftrace_connect_backtrace. */
689
690 static void
691 ftrace_connect_bfun (struct btrace_function *prev,
692 struct btrace_function *next)
693 {
694 DEBUG_FTRACE ("connecting...");
695 ftrace_debug (prev, "..prev");
696 ftrace_debug (next, "..next");
697
698 /* The function segments are not yet connected. */
699 gdb_assert (prev->segment.next == NULL);
700 gdb_assert (next->segment.prev == NULL);
701
702 prev->segment.next = next;
703 next->segment.prev = prev;
704
705 /* We may have moved NEXT to a different function level. */
706 ftrace_fixup_level (next, prev->level - next->level);
707
708 /* If we run out of back trace for one, let's use the other's. */
709 if (prev->up == NULL)
710 {
711 if (next->up != NULL)
712 {
713 DEBUG_FTRACE ("using next's callers");
714 ftrace_fixup_caller (prev, next->up, next->flags);
715 }
716 }
717 else if (next->up == NULL)
718 {
719 if (prev->up != NULL)
720 {
721 DEBUG_FTRACE ("using prev's callers");
722 ftrace_fixup_caller (next, prev->up, prev->flags);
723 }
724 }
725 else
726 {
727 /* PREV may have a tailcall caller, NEXT can't. If it does, fixup the up
728 link to add the tail callers to NEXT's back trace.
729
730 This removes NEXT->UP from NEXT's back trace. It will be added back
731 when connecting NEXT and PREV's callers - provided they exist.
732
733 If PREV's back trace consists of a series of tail calls without an
734 actual call, there will be no further connection and NEXT's caller will
735 be removed for good. To catch this case, we handle it here and connect
736 the top of PREV's back trace to NEXT's caller. */
737 if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) != 0)
738 {
739 struct btrace_function *caller;
740 btrace_function_flags flags;
741
742 /* We checked NEXT->UP above so CALLER can't be NULL. */
743 caller = next->up;
744 flags = next->flags;
745
746 DEBUG_FTRACE ("adding prev's tail calls to next");
747
748 ftrace_fixup_caller (next, prev->up, prev->flags);
749
750 for (prev = prev->up; prev != NULL; prev = prev->up)
751 {
752 /* At the end of PREV's back trace, continue with CALLER. */
753 if (prev->up == NULL)
754 {
755 DEBUG_FTRACE ("fixing up link for tailcall chain");
756 ftrace_debug (prev, "..top");
757 ftrace_debug (caller, "..up");
758
759 ftrace_fixup_caller (prev, caller, flags);
760
761 /* If we skipped any tail calls, this may move CALLER to a
762 different function level.
763
764 Note that changing CALLER's level is only OK because we
765 know that this is the last iteration of the bottom-to-top
766 walk in ftrace_connect_backtrace.
767
768 Otherwise we will fix up CALLER's level when we connect it
769 to PREV's caller in the next iteration. */
770 ftrace_fixup_level (caller, prev->level - caller->level - 1);
771 break;
772 }
773
774 /* There's nothing to do if we find a real call. */
775 if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
776 {
777 DEBUG_FTRACE ("will fix up link in next iteration");
778 break;
779 }
780 }
781 }
782 }
783 }
784
785 /* Connect function segments on the same level in the back trace at LHS and RHS.
786 The back traces at LHS and RHS are expected to match according to
787 ftrace_match_backtrace. */
788
789 static void
790 ftrace_connect_backtrace (struct btrace_function *lhs,
791 struct btrace_function *rhs)
792 {
793 while (lhs != NULL && rhs != NULL)
794 {
795 struct btrace_function *prev, *next;
796
797 gdb_assert (!ftrace_function_switched (lhs, rhs->msym, rhs->sym));
798
799 /* Connecting LHS and RHS may change the up link. */
800 prev = lhs;
801 next = rhs;
802
803 lhs = ftrace_get_caller (lhs);
804 rhs = ftrace_get_caller (rhs);
805
806 ftrace_connect_bfun (prev, next);
807 }
808 }
809
810 /* Bridge the gap between two function segments left and right of a gap if their
811 respective back traces match in at least MIN_MATCHES functions.
812
813 Returns non-zero if the gap could be bridged, zero otherwise. */
814
815 static int
816 ftrace_bridge_gap (struct btrace_function *lhs, struct btrace_function *rhs,
817 int min_matches)
818 {
819 struct btrace_function *best_l, *best_r, *cand_l, *cand_r;
820 int best_matches;
821
822 DEBUG_FTRACE ("checking gap at insn %u (req matches: %d)",
823 rhs->insn_offset - 1, min_matches);
824
825 best_matches = 0;
826 best_l = NULL;
827 best_r = NULL;
828
829 /* We search the back traces of LHS and RHS for valid connections and connect
830 the two functon segments that give the longest combined back trace. */
831
832 for (cand_l = lhs; cand_l != NULL; cand_l = ftrace_get_caller (cand_l))
833 for (cand_r = rhs; cand_r != NULL; cand_r = ftrace_get_caller (cand_r))
834 {
835 int matches;
836
837 matches = ftrace_match_backtrace (cand_l, cand_r);
838 if (best_matches < matches)
839 {
840 best_matches = matches;
841 best_l = cand_l;
842 best_r = cand_r;
843 }
844 }
845
846 /* We need at least MIN_MATCHES matches. */
847 gdb_assert (min_matches > 0);
848 if (best_matches < min_matches)
849 return 0;
850
851 DEBUG_FTRACE ("..matches: %d", best_matches);
852
853 /* We will fix up the level of BEST_R and succeeding function segments such
854 that BEST_R's level matches BEST_L's when we connect BEST_L to BEST_R.
855
856 This will ignore the level of RHS and following if BEST_R != RHS. I.e. if
857 BEST_R is a successor of RHS in the back trace of RHS (phases 1 and 3).
858
859 To catch this, we already fix up the level here where we can start at RHS
860 instead of at BEST_R. We will ignore the level fixup when connecting
861 BEST_L to BEST_R as they will already be on the same level. */
862 ftrace_fixup_level (rhs, best_l->level - best_r->level);
863
864 ftrace_connect_backtrace (best_l, best_r);
865
866 return best_matches;
867 }
868
869 /* Try to bridge gaps due to overflow or decode errors by connecting the
870 function segments that are separated by the gap. */
871
872 static void
873 btrace_bridge_gaps (struct thread_info *tp, VEC (bfun_s) **gaps)
874 {
875 VEC (bfun_s) *remaining;
876 struct cleanup *old_chain;
877 int min_matches;
878
879 DEBUG ("bridge gaps");
880
881 remaining = NULL;
882 old_chain = make_cleanup (VEC_cleanup (bfun_s), &remaining);
883
884 /* We require a minimum amount of matches for bridging a gap. The number of
885 required matches will be lowered with each iteration.
886
887 The more matches the higher our confidence that the bridging is correct.
888 For big gaps or small traces, however, it may not be feasible to require a
889 high number of matches. */
890 for (min_matches = 5; min_matches > 0; --min_matches)
891 {
892 /* Let's try to bridge as many gaps as we can. In some cases, we need to
893 skip a gap and revisit it again after we closed later gaps. */
894 while (!VEC_empty (bfun_s, *gaps))
895 {
896 struct btrace_function *gap;
897 unsigned int idx;
898
899 for (idx = 0; VEC_iterate (bfun_s, *gaps, idx, gap); ++idx)
900 {
901 struct btrace_function *lhs, *rhs;
902 int bridged;
903
904 /* We may have a sequence of gaps if we run from one error into
905 the next as we try to re-sync onto the trace stream. Ignore
906 all but the leftmost gap in such a sequence.
907
908 Also ignore gaps at the beginning of the trace. */
909 lhs = gap->flow.prev;
910 if (lhs == NULL || lhs->errcode != 0)
911 continue;
912
913 /* Skip gaps to the right. */
914 for (rhs = gap->flow.next; rhs != NULL; rhs = rhs->flow.next)
915 if (rhs->errcode == 0)
916 break;
917
918 /* Ignore gaps at the end of the trace. */
919 if (rhs == NULL)
920 continue;
921
922 bridged = ftrace_bridge_gap (lhs, rhs, min_matches);
923
924 /* Keep track of gaps we were not able to bridge and try again.
925 If we just pushed them to the end of GAPS we would risk an
926 infinite loop in case we simply cannot bridge a gap. */
927 if (bridged == 0)
928 VEC_safe_push (bfun_s, remaining, gap);
929 }
930
931 /* Let's see if we made any progress. */
932 if (VEC_length (bfun_s, remaining) == VEC_length (bfun_s, *gaps))
933 break;
934
935 VEC_free (bfun_s, *gaps);
936
937 *gaps = remaining;
938 remaining = NULL;
939 }
940
941 /* We get here if either GAPS is empty or if GAPS equals REMAINING. */
942 if (VEC_empty (bfun_s, *gaps))
943 break;
944
945 VEC_free (bfun_s, remaining);
946 }
947
948 do_cleanups (old_chain);
949
950 /* We may omit this in some cases. Not sure it is worth the extra
951 complication, though. */
952 ftrace_compute_global_level_offset (&tp->btrace);
953 }
954
955 /* Compute the function branch trace from BTS trace. */
956
957 static void
958 btrace_compute_ftrace_bts (struct thread_info *tp,
959 const struct btrace_data_bts *btrace,
960 VEC (bfun_s) **gaps)
961 {
962 struct btrace_thread_info *btinfo;
963 struct btrace_function *begin, *end;
964 struct gdbarch *gdbarch;
965 unsigned int blk;
966 int level;
967
968 gdbarch = target_gdbarch ();
969 btinfo = &tp->btrace;
970 begin = btinfo->begin;
971 end = btinfo->end;
972 level = begin != NULL ? -btinfo->level : INT_MAX;
973 blk = VEC_length (btrace_block_s, btrace->blocks);
974
975 while (blk != 0)
976 {
977 btrace_block_s *block;
978 CORE_ADDR pc;
979
980 blk -= 1;
981
982 block = VEC_index (btrace_block_s, btrace->blocks, blk);
983 pc = block->begin;
984
985 for (;;)
986 {
987 struct btrace_insn insn;
988 int size;
989
990 /* We should hit the end of the block. Warn if we went too far. */
991 if (block->end < pc)
992 {
993 /* Indicate the gap in the trace. */
994 end = ftrace_new_gap (end, BDE_BTS_OVERFLOW);
995 if (begin == NULL)
996 begin = end;
997
998 VEC_safe_push (bfun_s, *gaps, end);
999
1000 warning (_("Recorded trace may be corrupted at instruction "
1001 "%u (pc = %s)."), end->insn_offset - 1,
1002 core_addr_to_string_nz (pc));
1003
1004 break;
1005 }
1006
1007 end = ftrace_update_function (end, pc);
1008 if (begin == NULL)
1009 begin = end;
1010
1011 /* Maintain the function level offset.
1012 For all but the last block, we do it here. */
1013 if (blk != 0)
1014 level = std::min (level, end->level);
1015
1016 size = 0;
1017 TRY
1018 {
1019 size = gdb_insn_length (gdbarch, pc);
1020 }
1021 CATCH (error, RETURN_MASK_ERROR)
1022 {
1023 }
1024 END_CATCH
1025
1026 insn.pc = pc;
1027 insn.size = size;
1028 insn.iclass = ftrace_classify_insn (gdbarch, pc);
1029 insn.flags = 0;
1030
1031 ftrace_update_insns (end, &insn);
1032
1033 /* We're done once we pushed the instruction at the end. */
1034 if (block->end == pc)
1035 break;
1036
1037 /* We can't continue if we fail to compute the size. */
1038 if (size <= 0)
1039 {
1040 /* Indicate the gap in the trace. We just added INSN so we're
1041 not at the beginning. */
1042 end = ftrace_new_gap (end, BDE_BTS_INSN_SIZE);
1043
1044 VEC_safe_push (bfun_s, *gaps, end);
1045
1046 warning (_("Recorded trace may be incomplete at instruction %u "
1047 "(pc = %s)."), end->insn_offset - 1,
1048 core_addr_to_string_nz (pc));
1049
1050 break;
1051 }
1052
1053 pc += size;
1054
1055 /* Maintain the function level offset.
1056 For the last block, we do it here to not consider the last
1057 instruction.
1058 Since the last instruction corresponds to the current instruction
1059 and is not really part of the execution history, it shouldn't
1060 affect the level. */
1061 if (blk == 0)
1062 level = std::min (level, end->level);
1063 }
1064 }
1065
1066 btinfo->begin = begin;
1067 btinfo->end = end;
1068
1069 /* LEVEL is the minimal function level of all btrace function segments.
1070 Define the global level offset to -LEVEL so all function levels are
1071 normalized to start at zero. */
1072 btinfo->level = -level;
1073 }
1074
1075 #if defined (HAVE_LIBIPT)
1076
1077 static enum btrace_insn_class
1078 pt_reclassify_insn (enum pt_insn_class iclass)
1079 {
1080 switch (iclass)
1081 {
1082 case ptic_call:
1083 return BTRACE_INSN_CALL;
1084
1085 case ptic_return:
1086 return BTRACE_INSN_RETURN;
1087
1088 case ptic_jump:
1089 return BTRACE_INSN_JUMP;
1090
1091 default:
1092 return BTRACE_INSN_OTHER;
1093 }
1094 }
1095
1096 /* Return the btrace instruction flags for INSN. */
1097
1098 static btrace_insn_flags
1099 pt_btrace_insn_flags (const struct pt_insn *insn)
1100 {
1101 btrace_insn_flags flags = 0;
1102
1103 if (insn->speculative)
1104 flags |= BTRACE_INSN_FLAG_SPECULATIVE;
1105
1106 return flags;
1107 }
1108
1109 /* Add function branch trace using DECODER. */
1110
1111 static void
1112 ftrace_add_pt (struct pt_insn_decoder *decoder,
1113 struct btrace_function **pbegin,
1114 struct btrace_function **pend, int *plevel,
1115 VEC (bfun_s) **gaps)
1116 {
1117 struct btrace_function *begin, *end, *upd;
1118 uint64_t offset;
1119 int errcode;
1120
1121 begin = *pbegin;
1122 end = *pend;
1123 for (;;)
1124 {
1125 struct btrace_insn btinsn;
1126 struct pt_insn insn;
1127
1128 errcode = pt_insn_sync_forward (decoder);
1129 if (errcode < 0)
1130 {
1131 if (errcode != -pte_eos)
1132 warning (_("Failed to synchronize onto the Intel Processor "
1133 "Trace stream: %s."), pt_errstr (pt_errcode (errcode)));
1134 break;
1135 }
1136
1137 memset (&btinsn, 0, sizeof (btinsn));
1138 for (;;)
1139 {
1140 errcode = pt_insn_next (decoder, &insn, sizeof(insn));
1141 if (errcode < 0)
1142 break;
1143
1144 /* Look for gaps in the trace - unless we're at the beginning. */
1145 if (begin != NULL)
1146 {
1147 /* Tracing is disabled and re-enabled each time we enter the
1148 kernel. Most times, we continue from the same instruction we
1149 stopped before. This is indicated via the RESUMED instruction
1150 flag. The ENABLED instruction flag means that we continued
1151 from some other instruction. Indicate this as a trace gap. */
1152 if (insn.enabled)
1153 {
1154 *pend = end = ftrace_new_gap (end, BDE_PT_DISABLED);
1155
1156 VEC_safe_push (bfun_s, *gaps, end);
1157
1158 pt_insn_get_offset (decoder, &offset);
1159
1160 warning (_("Non-contiguous trace at instruction %u (offset "
1161 "= 0x%" PRIx64 ", pc = 0x%" PRIx64 ")."),
1162 end->insn_offset - 1, offset, insn.ip);
1163 }
1164 }
1165
1166 /* Indicate trace overflows. */
1167 if (insn.resynced)
1168 {
1169 *pend = end = ftrace_new_gap (end, BDE_PT_OVERFLOW);
1170 if (begin == NULL)
1171 *pbegin = begin = end;
1172
1173 VEC_safe_push (bfun_s, *gaps, end);
1174
1175 pt_insn_get_offset (decoder, &offset);
1176
1177 warning (_("Overflow at instruction %u (offset = 0x%" PRIx64
1178 ", pc = 0x%" PRIx64 ")."), end->insn_offset - 1,
1179 offset, insn.ip);
1180 }
1181
1182 upd = ftrace_update_function (end, insn.ip);
1183 if (upd != end)
1184 {
1185 *pend = end = upd;
1186
1187 if (begin == NULL)
1188 *pbegin = begin = upd;
1189 }
1190
1191 /* Maintain the function level offset. */
1192 *plevel = std::min (*plevel, end->level);
1193
1194 btinsn.pc = (CORE_ADDR) insn.ip;
1195 btinsn.size = (gdb_byte) insn.size;
1196 btinsn.iclass = pt_reclassify_insn (insn.iclass);
1197 btinsn.flags = pt_btrace_insn_flags (&insn);
1198
1199 ftrace_update_insns (end, &btinsn);
1200 }
1201
1202 if (errcode == -pte_eos)
1203 break;
1204
1205 /* Indicate the gap in the trace. */
1206 *pend = end = ftrace_new_gap (end, errcode);
1207 if (begin == NULL)
1208 *pbegin = begin = end;
1209
1210 VEC_safe_push (bfun_s, *gaps, end);
1211
1212 pt_insn_get_offset (decoder, &offset);
1213
1214 warning (_("Decode error (%d) at instruction %u (offset = 0x%" PRIx64
1215 ", pc = 0x%" PRIx64 "): %s."), errcode, end->insn_offset - 1,
1216 offset, insn.ip, pt_errstr (pt_errcode (errcode)));
1217 }
1218 }
1219
1220 /* A callback function to allow the trace decoder to read the inferior's
1221 memory. */
1222
1223 static int
1224 btrace_pt_readmem_callback (gdb_byte *buffer, size_t size,
1225 const struct pt_asid *asid, uint64_t pc,
1226 void *context)
1227 {
1228 int result, errcode;
1229
1230 result = (int) size;
1231 TRY
1232 {
1233 errcode = target_read_code ((CORE_ADDR) pc, buffer, size);
1234 if (errcode != 0)
1235 result = -pte_nomap;
1236 }
1237 CATCH (error, RETURN_MASK_ERROR)
1238 {
1239 result = -pte_nomap;
1240 }
1241 END_CATCH
1242
1243 return result;
1244 }
1245
1246 /* Translate the vendor from one enum to another. */
1247
1248 static enum pt_cpu_vendor
1249 pt_translate_cpu_vendor (enum btrace_cpu_vendor vendor)
1250 {
1251 switch (vendor)
1252 {
1253 default:
1254 return pcv_unknown;
1255
1256 case CV_INTEL:
1257 return pcv_intel;
1258 }
1259 }
1260
1261 /* Finalize the function branch trace after decode. */
1262
1263 static void btrace_finalize_ftrace_pt (struct pt_insn_decoder *decoder,
1264 struct thread_info *tp, int level)
1265 {
1266 pt_insn_free_decoder (decoder);
1267
1268 /* LEVEL is the minimal function level of all btrace function segments.
1269 Define the global level offset to -LEVEL so all function levels are
1270 normalized to start at zero. */
1271 tp->btrace.level = -level;
1272
1273 /* Add a single last instruction entry for the current PC.
1274 This allows us to compute the backtrace at the current PC using both
1275 standard unwind and btrace unwind.
1276 This extra entry is ignored by all record commands. */
1277 btrace_add_pc (tp);
1278 }
1279
1280 /* Compute the function branch trace from Intel Processor Trace
1281 format. */
1282
1283 static void
1284 btrace_compute_ftrace_pt (struct thread_info *tp,
1285 const struct btrace_data_pt *btrace,
1286 VEC (bfun_s) **gaps)
1287 {
1288 struct btrace_thread_info *btinfo;
1289 struct pt_insn_decoder *decoder;
1290 struct pt_config config;
1291 int level, errcode;
1292
1293 if (btrace->size == 0)
1294 return;
1295
1296 btinfo = &tp->btrace;
1297 level = btinfo->begin != NULL ? -btinfo->level : INT_MAX;
1298
1299 pt_config_init(&config);
1300 config.begin = btrace->data;
1301 config.end = btrace->data + btrace->size;
1302
1303 config.cpu.vendor = pt_translate_cpu_vendor (btrace->config.cpu.vendor);
1304 config.cpu.family = btrace->config.cpu.family;
1305 config.cpu.model = btrace->config.cpu.model;
1306 config.cpu.stepping = btrace->config.cpu.stepping;
1307
1308 errcode = pt_cpu_errata (&config.errata, &config.cpu);
1309 if (errcode < 0)
1310 error (_("Failed to configure the Intel Processor Trace decoder: %s."),
1311 pt_errstr (pt_errcode (errcode)));
1312
1313 decoder = pt_insn_alloc_decoder (&config);
1314 if (decoder == NULL)
1315 error (_("Failed to allocate the Intel Processor Trace decoder."));
1316
1317 TRY
1318 {
1319 struct pt_image *image;
1320
1321 image = pt_insn_get_image(decoder);
1322 if (image == NULL)
1323 error (_("Failed to configure the Intel Processor Trace decoder."));
1324
1325 errcode = pt_image_set_callback(image, btrace_pt_readmem_callback, NULL);
1326 if (errcode < 0)
1327 error (_("Failed to configure the Intel Processor Trace decoder: "
1328 "%s."), pt_errstr (pt_errcode (errcode)));
1329
1330 ftrace_add_pt (decoder, &btinfo->begin, &btinfo->end, &level, gaps);
1331 }
1332 CATCH (error, RETURN_MASK_ALL)
1333 {
1334 /* Indicate a gap in the trace if we quit trace processing. */
1335 if (error.reason == RETURN_QUIT && btinfo->end != NULL)
1336 {
1337 btinfo->end = ftrace_new_gap (btinfo->end, BDE_PT_USER_QUIT);
1338
1339 VEC_safe_push (bfun_s, *gaps, btinfo->end);
1340 }
1341
1342 btrace_finalize_ftrace_pt (decoder, tp, level);
1343
1344 throw_exception (error);
1345 }
1346 END_CATCH
1347
1348 btrace_finalize_ftrace_pt (decoder, tp, level);
1349 }
1350
1351 #else /* defined (HAVE_LIBIPT) */
1352
1353 static void
1354 btrace_compute_ftrace_pt (struct thread_info *tp,
1355 const struct btrace_data_pt *btrace,
1356 VEC (bfun_s) **gaps)
1357 {
1358 internal_error (__FILE__, __LINE__, _("Unexpected branch trace format."));
1359 }
1360
1361 #endif /* defined (HAVE_LIBIPT) */
1362
1363 /* Compute the function branch trace from a block branch trace BTRACE for
1364 a thread given by BTINFO. */
1365
1366 static void
1367 btrace_compute_ftrace_1 (struct thread_info *tp, struct btrace_data *btrace,
1368 VEC (bfun_s) **gaps)
1369 {
1370 DEBUG ("compute ftrace");
1371
1372 switch (btrace->format)
1373 {
1374 case BTRACE_FORMAT_NONE:
1375 return;
1376
1377 case BTRACE_FORMAT_BTS:
1378 btrace_compute_ftrace_bts (tp, &btrace->variant.bts, gaps);
1379 return;
1380
1381 case BTRACE_FORMAT_PT:
1382 btrace_compute_ftrace_pt (tp, &btrace->variant.pt, gaps);
1383 return;
1384 }
1385
1386 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
1387 }
1388
1389 static void
1390 btrace_finalize_ftrace (struct thread_info *tp, VEC (bfun_s) **gaps)
1391 {
1392 if (!VEC_empty (bfun_s, *gaps))
1393 {
1394 tp->btrace.ngaps += VEC_length (bfun_s, *gaps);
1395 btrace_bridge_gaps (tp, gaps);
1396 }
1397 }
1398
1399 static void
1400 btrace_compute_ftrace (struct thread_info *tp, struct btrace_data *btrace)
1401 {
1402 VEC (bfun_s) *gaps;
1403 struct cleanup *old_chain;
1404
1405 gaps = NULL;
1406 old_chain = make_cleanup (VEC_cleanup (bfun_s), &gaps);
1407
1408 TRY
1409 {
1410 btrace_compute_ftrace_1 (tp, btrace, &gaps);
1411 }
1412 CATCH (error, RETURN_MASK_ALL)
1413 {
1414 btrace_finalize_ftrace (tp, &gaps);
1415
1416 throw_exception (error);
1417 }
1418 END_CATCH
1419
1420 btrace_finalize_ftrace (tp, &gaps);
1421
1422 do_cleanups (old_chain);
1423 }
1424
1425 /* Add an entry for the current PC. */
1426
1427 static void
1428 btrace_add_pc (struct thread_info *tp)
1429 {
1430 struct btrace_data btrace;
1431 struct btrace_block *block;
1432 struct regcache *regcache;
1433 struct cleanup *cleanup;
1434 CORE_ADDR pc;
1435
1436 regcache = get_thread_regcache (tp->ptid);
1437 pc = regcache_read_pc (regcache);
1438
1439 btrace_data_init (&btrace);
1440 btrace.format = BTRACE_FORMAT_BTS;
1441 btrace.variant.bts.blocks = NULL;
1442
1443 cleanup = make_cleanup_btrace_data (&btrace);
1444
1445 block = VEC_safe_push (btrace_block_s, btrace.variant.bts.blocks, NULL);
1446 block->begin = pc;
1447 block->end = pc;
1448
1449 btrace_compute_ftrace (tp, &btrace);
1450
1451 do_cleanups (cleanup);
1452 }
1453
1454 /* See btrace.h. */
1455
1456 void
1457 btrace_enable (struct thread_info *tp, const struct btrace_config *conf)
1458 {
1459 if (tp->btrace.target != NULL)
1460 return;
1461
1462 #if !defined (HAVE_LIBIPT)
1463 if (conf->format == BTRACE_FORMAT_PT)
1464 error (_("GDB does not support Intel Processor Trace."));
1465 #endif /* !defined (HAVE_LIBIPT) */
1466
1467 if (!target_supports_btrace (conf->format))
1468 error (_("Target does not support branch tracing."));
1469
1470 DEBUG ("enable thread %s (%s)", print_thread_id (tp),
1471 target_pid_to_str (tp->ptid));
1472
1473 tp->btrace.target = target_enable_btrace (tp->ptid, conf);
1474
1475 /* We're done if we failed to enable tracing. */
1476 if (tp->btrace.target == NULL)
1477 return;
1478
1479 /* We need to undo the enable in case of errors. */
1480 TRY
1481 {
1482 /* Add an entry for the current PC so we start tracing from where we
1483 enabled it.
1484
1485 If we can't access TP's registers, TP is most likely running. In this
1486 case, we can't really say where tracing was enabled so it should be
1487 safe to simply skip this step.
1488
1489 This is not relevant for BTRACE_FORMAT_PT since the trace will already
1490 start at the PC at which tracing was enabled. */
1491 if (conf->format != BTRACE_FORMAT_PT
1492 && can_access_registers_ptid (tp->ptid))
1493 btrace_add_pc (tp);
1494 }
1495 CATCH (exception, RETURN_MASK_ALL)
1496 {
1497 btrace_disable (tp);
1498
1499 throw_exception (exception);
1500 }
1501 END_CATCH
1502 }
1503
1504 /* See btrace.h. */
1505
1506 const struct btrace_config *
1507 btrace_conf (const struct btrace_thread_info *btinfo)
1508 {
1509 if (btinfo->target == NULL)
1510 return NULL;
1511
1512 return target_btrace_conf (btinfo->target);
1513 }
1514
1515 /* See btrace.h. */
1516
1517 void
1518 btrace_disable (struct thread_info *tp)
1519 {
1520 struct btrace_thread_info *btp = &tp->btrace;
1521 int errcode = 0;
1522
1523 if (btp->target == NULL)
1524 return;
1525
1526 DEBUG ("disable thread %s (%s)", print_thread_id (tp),
1527 target_pid_to_str (tp->ptid));
1528
1529 target_disable_btrace (btp->target);
1530 btp->target = NULL;
1531
1532 btrace_clear (tp);
1533 }
1534
1535 /* See btrace.h. */
1536
1537 void
1538 btrace_teardown (struct thread_info *tp)
1539 {
1540 struct btrace_thread_info *btp = &tp->btrace;
1541 int errcode = 0;
1542
1543 if (btp->target == NULL)
1544 return;
1545
1546 DEBUG ("teardown thread %s (%s)", print_thread_id (tp),
1547 target_pid_to_str (tp->ptid));
1548
1549 target_teardown_btrace (btp->target);
1550 btp->target = NULL;
1551
1552 btrace_clear (tp);
1553 }
1554
1555 /* Stitch branch trace in BTS format. */
1556
1557 static int
1558 btrace_stitch_bts (struct btrace_data_bts *btrace, struct thread_info *tp)
1559 {
1560 struct btrace_thread_info *btinfo;
1561 struct btrace_function *last_bfun;
1562 struct btrace_insn *last_insn;
1563 btrace_block_s *first_new_block;
1564
1565 btinfo = &tp->btrace;
1566 last_bfun = btinfo->end;
1567 gdb_assert (last_bfun != NULL);
1568 gdb_assert (!VEC_empty (btrace_block_s, btrace->blocks));
1569
1570 /* If the existing trace ends with a gap, we just glue the traces
1571 together. We need to drop the last (i.e. chronologically first) block
1572 of the new trace, though, since we can't fill in the start address.*/
1573 if (VEC_empty (btrace_insn_s, last_bfun->insn))
1574 {
1575 VEC_pop (btrace_block_s, btrace->blocks);
1576 return 0;
1577 }
1578
1579 /* Beware that block trace starts with the most recent block, so the
1580 chronologically first block in the new trace is the last block in
1581 the new trace's block vector. */
1582 first_new_block = VEC_last (btrace_block_s, btrace->blocks);
1583 last_insn = VEC_last (btrace_insn_s, last_bfun->insn);
1584
1585 /* If the current PC at the end of the block is the same as in our current
1586 trace, there are two explanations:
1587 1. we executed the instruction and some branch brought us back.
1588 2. we have not made any progress.
1589 In the first case, the delta trace vector should contain at least two
1590 entries.
1591 In the second case, the delta trace vector should contain exactly one
1592 entry for the partial block containing the current PC. Remove it. */
1593 if (first_new_block->end == last_insn->pc
1594 && VEC_length (btrace_block_s, btrace->blocks) == 1)
1595 {
1596 VEC_pop (btrace_block_s, btrace->blocks);
1597 return 0;
1598 }
1599
1600 DEBUG ("stitching %s to %s", ftrace_print_insn_addr (last_insn),
1601 core_addr_to_string_nz (first_new_block->end));
1602
1603 /* Do a simple sanity check to make sure we don't accidentally end up
1604 with a bad block. This should not occur in practice. */
1605 if (first_new_block->end < last_insn->pc)
1606 {
1607 warning (_("Error while trying to read delta trace. Falling back to "
1608 "a full read."));
1609 return -1;
1610 }
1611
1612 /* We adjust the last block to start at the end of our current trace. */
1613 gdb_assert (first_new_block->begin == 0);
1614 first_new_block->begin = last_insn->pc;
1615
1616 /* We simply pop the last insn so we can insert it again as part of
1617 the normal branch trace computation.
1618 Since instruction iterators are based on indices in the instructions
1619 vector, we don't leave any pointers dangling. */
1620 DEBUG ("pruning insn at %s for stitching",
1621 ftrace_print_insn_addr (last_insn));
1622
1623 VEC_pop (btrace_insn_s, last_bfun->insn);
1624
1625 /* The instructions vector may become empty temporarily if this has
1626 been the only instruction in this function segment.
1627 This violates the invariant but will be remedied shortly by
1628 btrace_compute_ftrace when we add the new trace. */
1629
1630 /* The only case where this would hurt is if the entire trace consisted
1631 of just that one instruction. If we remove it, we might turn the now
1632 empty btrace function segment into a gap. But we don't want gaps at
1633 the beginning. To avoid this, we remove the entire old trace. */
1634 if (last_bfun == btinfo->begin && VEC_empty (btrace_insn_s, last_bfun->insn))
1635 btrace_clear (tp);
1636
1637 return 0;
1638 }
1639
1640 /* Adjust the block trace in order to stitch old and new trace together.
1641 BTRACE is the new delta trace between the last and the current stop.
1642 TP is the traced thread.
1643 May modifx BTRACE as well as the existing trace in TP.
1644 Return 0 on success, -1 otherwise. */
1645
1646 static int
1647 btrace_stitch_trace (struct btrace_data *btrace, struct thread_info *tp)
1648 {
1649 /* If we don't have trace, there's nothing to do. */
1650 if (btrace_data_empty (btrace))
1651 return 0;
1652
1653 switch (btrace->format)
1654 {
1655 case BTRACE_FORMAT_NONE:
1656 return 0;
1657
1658 case BTRACE_FORMAT_BTS:
1659 return btrace_stitch_bts (&btrace->variant.bts, tp);
1660
1661 case BTRACE_FORMAT_PT:
1662 /* Delta reads are not supported. */
1663 return -1;
1664 }
1665
1666 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
1667 }
1668
1669 /* Clear the branch trace histories in BTINFO. */
1670
1671 static void
1672 btrace_clear_history (struct btrace_thread_info *btinfo)
1673 {
1674 xfree (btinfo->insn_history);
1675 xfree (btinfo->call_history);
1676 xfree (btinfo->replay);
1677
1678 btinfo->insn_history = NULL;
1679 btinfo->call_history = NULL;
1680 btinfo->replay = NULL;
1681 }
1682
1683 /* Clear the branch trace maintenance histories in BTINFO. */
1684
1685 static void
1686 btrace_maint_clear (struct btrace_thread_info *btinfo)
1687 {
1688 switch (btinfo->data.format)
1689 {
1690 default:
1691 break;
1692
1693 case BTRACE_FORMAT_BTS:
1694 btinfo->maint.variant.bts.packet_history.begin = 0;
1695 btinfo->maint.variant.bts.packet_history.end = 0;
1696 break;
1697
1698 #if defined (HAVE_LIBIPT)
1699 case BTRACE_FORMAT_PT:
1700 xfree (btinfo->maint.variant.pt.packets);
1701
1702 btinfo->maint.variant.pt.packets = NULL;
1703 btinfo->maint.variant.pt.packet_history.begin = 0;
1704 btinfo->maint.variant.pt.packet_history.end = 0;
1705 break;
1706 #endif /* defined (HAVE_LIBIPT) */
1707 }
1708 }
1709
1710 /* See btrace.h. */
1711
1712 void
1713 btrace_fetch (struct thread_info *tp)
1714 {
1715 struct btrace_thread_info *btinfo;
1716 struct btrace_target_info *tinfo;
1717 struct btrace_data btrace;
1718 struct cleanup *cleanup;
1719 int errcode;
1720
1721 DEBUG ("fetch thread %s (%s)", print_thread_id (tp),
1722 target_pid_to_str (tp->ptid));
1723
1724 btinfo = &tp->btrace;
1725 tinfo = btinfo->target;
1726 if (tinfo == NULL)
1727 return;
1728
1729 /* There's no way we could get new trace while replaying.
1730 On the other hand, delta trace would return a partial record with the
1731 current PC, which is the replay PC, not the last PC, as expected. */
1732 if (btinfo->replay != NULL)
1733 return;
1734
1735 /* We should not be called on running or exited threads. */
1736 gdb_assert (can_access_registers_ptid (tp->ptid));
1737
1738 btrace_data_init (&btrace);
1739 cleanup = make_cleanup_btrace_data (&btrace);
1740
1741 /* Let's first try to extend the trace we already have. */
1742 if (btinfo->end != NULL)
1743 {
1744 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_DELTA);
1745 if (errcode == 0)
1746 {
1747 /* Success. Let's try to stitch the traces together. */
1748 errcode = btrace_stitch_trace (&btrace, tp);
1749 }
1750 else
1751 {
1752 /* We failed to read delta trace. Let's try to read new trace. */
1753 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_NEW);
1754
1755 /* If we got any new trace, discard what we have. */
1756 if (errcode == 0 && !btrace_data_empty (&btrace))
1757 btrace_clear (tp);
1758 }
1759
1760 /* If we were not able to read the trace, we start over. */
1761 if (errcode != 0)
1762 {
1763 btrace_clear (tp);
1764 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL);
1765 }
1766 }
1767 else
1768 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL);
1769
1770 /* If we were not able to read the branch trace, signal an error. */
1771 if (errcode != 0)
1772 error (_("Failed to read branch trace."));
1773
1774 /* Compute the trace, provided we have any. */
1775 if (!btrace_data_empty (&btrace))
1776 {
1777 /* Store the raw trace data. The stored data will be cleared in
1778 btrace_clear, so we always append the new trace. */
1779 btrace_data_append (&btinfo->data, &btrace);
1780 btrace_maint_clear (btinfo);
1781
1782 btrace_clear_history (btinfo);
1783 btrace_compute_ftrace (tp, &btrace);
1784 }
1785
1786 do_cleanups (cleanup);
1787 }
1788
1789 /* See btrace.h. */
1790
1791 void
1792 btrace_clear (struct thread_info *tp)
1793 {
1794 struct btrace_thread_info *btinfo;
1795 struct btrace_function *it, *trash;
1796
1797 DEBUG ("clear thread %s (%s)", print_thread_id (tp),
1798 target_pid_to_str (tp->ptid));
1799
1800 /* Make sure btrace frames that may hold a pointer into the branch
1801 trace data are destroyed. */
1802 reinit_frame_cache ();
1803
1804 btinfo = &tp->btrace;
1805
1806 it = btinfo->begin;
1807 while (it != NULL)
1808 {
1809 trash = it;
1810 it = it->flow.next;
1811
1812 xfree (trash);
1813 }
1814
1815 btinfo->begin = NULL;
1816 btinfo->end = NULL;
1817 btinfo->ngaps = 0;
1818
1819 /* Must clear the maint data before - it depends on BTINFO->DATA. */
1820 btrace_maint_clear (btinfo);
1821 btrace_data_clear (&btinfo->data);
1822 btrace_clear_history (btinfo);
1823 }
1824
1825 /* See btrace.h. */
1826
1827 void
1828 btrace_free_objfile (struct objfile *objfile)
1829 {
1830 struct thread_info *tp;
1831
1832 DEBUG ("free objfile");
1833
1834 ALL_NON_EXITED_THREADS (tp)
1835 btrace_clear (tp);
1836 }
1837
1838 #if defined (HAVE_LIBEXPAT)
1839
1840 /* Check the btrace document version. */
1841
1842 static void
1843 check_xml_btrace_version (struct gdb_xml_parser *parser,
1844 const struct gdb_xml_element *element,
1845 void *user_data, VEC (gdb_xml_value_s) *attributes)
1846 {
1847 const char *version
1848 = (const char *) xml_find_attribute (attributes, "version")->value;
1849
1850 if (strcmp (version, "1.0") != 0)
1851 gdb_xml_error (parser, _("Unsupported btrace version: \"%s\""), version);
1852 }
1853
1854 /* Parse a btrace "block" xml record. */
1855
1856 static void
1857 parse_xml_btrace_block (struct gdb_xml_parser *parser,
1858 const struct gdb_xml_element *element,
1859 void *user_data, VEC (gdb_xml_value_s) *attributes)
1860 {
1861 struct btrace_data *btrace;
1862 struct btrace_block *block;
1863 ULONGEST *begin, *end;
1864
1865 btrace = (struct btrace_data *) user_data;
1866
1867 switch (btrace->format)
1868 {
1869 case BTRACE_FORMAT_BTS:
1870 break;
1871
1872 case BTRACE_FORMAT_NONE:
1873 btrace->format = BTRACE_FORMAT_BTS;
1874 btrace->variant.bts.blocks = NULL;
1875 break;
1876
1877 default:
1878 gdb_xml_error (parser, _("Btrace format error."));
1879 }
1880
1881 begin = (ULONGEST *) xml_find_attribute (attributes, "begin")->value;
1882 end = (ULONGEST *) xml_find_attribute (attributes, "end")->value;
1883
1884 block = VEC_safe_push (btrace_block_s, btrace->variant.bts.blocks, NULL);
1885 block->begin = *begin;
1886 block->end = *end;
1887 }
1888
1889 /* Parse a "raw" xml record. */
1890
1891 static void
1892 parse_xml_raw (struct gdb_xml_parser *parser, const char *body_text,
1893 gdb_byte **pdata, size_t *psize)
1894 {
1895 struct cleanup *cleanup;
1896 gdb_byte *data, *bin;
1897 size_t len, size;
1898
1899 len = strlen (body_text);
1900 if (len % 2 != 0)
1901 gdb_xml_error (parser, _("Bad raw data size."));
1902
1903 size = len / 2;
1904
1905 bin = data = (gdb_byte *) xmalloc (size);
1906 cleanup = make_cleanup (xfree, data);
1907
1908 /* We use hex encoding - see common/rsp-low.h. */
1909 while (len > 0)
1910 {
1911 char hi, lo;
1912
1913 hi = *body_text++;
1914 lo = *body_text++;
1915
1916 if (hi == 0 || lo == 0)
1917 gdb_xml_error (parser, _("Bad hex encoding."));
1918
1919 *bin++ = fromhex (hi) * 16 + fromhex (lo);
1920 len -= 2;
1921 }
1922
1923 discard_cleanups (cleanup);
1924
1925 *pdata = data;
1926 *psize = size;
1927 }
1928
1929 /* Parse a btrace pt-config "cpu" xml record. */
1930
1931 static void
1932 parse_xml_btrace_pt_config_cpu (struct gdb_xml_parser *parser,
1933 const struct gdb_xml_element *element,
1934 void *user_data,
1935 VEC (gdb_xml_value_s) *attributes)
1936 {
1937 struct btrace_data *btrace;
1938 const char *vendor;
1939 ULONGEST *family, *model, *stepping;
1940
1941 vendor = (const char *) xml_find_attribute (attributes, "vendor")->value;
1942 family = (ULONGEST *) xml_find_attribute (attributes, "family")->value;
1943 model = (ULONGEST *) xml_find_attribute (attributes, "model")->value;
1944 stepping = (ULONGEST *) xml_find_attribute (attributes, "stepping")->value;
1945
1946 btrace = (struct btrace_data *) user_data;
1947
1948 if (strcmp (vendor, "GenuineIntel") == 0)
1949 btrace->variant.pt.config.cpu.vendor = CV_INTEL;
1950
1951 btrace->variant.pt.config.cpu.family = *family;
1952 btrace->variant.pt.config.cpu.model = *model;
1953 btrace->variant.pt.config.cpu.stepping = *stepping;
1954 }
1955
1956 /* Parse a btrace pt "raw" xml record. */
1957
1958 static void
1959 parse_xml_btrace_pt_raw (struct gdb_xml_parser *parser,
1960 const struct gdb_xml_element *element,
1961 void *user_data, const char *body_text)
1962 {
1963 struct btrace_data *btrace;
1964
1965 btrace = (struct btrace_data *) user_data;
1966 parse_xml_raw (parser, body_text, &btrace->variant.pt.data,
1967 &btrace->variant.pt.size);
1968 }
1969
1970 /* Parse a btrace "pt" xml record. */
1971
1972 static void
1973 parse_xml_btrace_pt (struct gdb_xml_parser *parser,
1974 const struct gdb_xml_element *element,
1975 void *user_data, VEC (gdb_xml_value_s) *attributes)
1976 {
1977 struct btrace_data *btrace;
1978
1979 btrace = (struct btrace_data *) user_data;
1980 btrace->format = BTRACE_FORMAT_PT;
1981 btrace->variant.pt.config.cpu.vendor = CV_UNKNOWN;
1982 btrace->variant.pt.data = NULL;
1983 btrace->variant.pt.size = 0;
1984 }
1985
1986 static const struct gdb_xml_attribute block_attributes[] = {
1987 { "begin", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1988 { "end", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1989 { NULL, GDB_XML_AF_NONE, NULL, NULL }
1990 };
1991
1992 static const struct gdb_xml_attribute btrace_pt_config_cpu_attributes[] = {
1993 { "vendor", GDB_XML_AF_NONE, NULL, NULL },
1994 { "family", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1995 { "model", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1996 { "stepping", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1997 { NULL, GDB_XML_AF_NONE, NULL, NULL }
1998 };
1999
2000 static const struct gdb_xml_element btrace_pt_config_children[] = {
2001 { "cpu", btrace_pt_config_cpu_attributes, NULL, GDB_XML_EF_OPTIONAL,
2002 parse_xml_btrace_pt_config_cpu, NULL },
2003 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2004 };
2005
2006 static const struct gdb_xml_element btrace_pt_children[] = {
2007 { "pt-config", NULL, btrace_pt_config_children, GDB_XML_EF_OPTIONAL, NULL,
2008 NULL },
2009 { "raw", NULL, NULL, GDB_XML_EF_OPTIONAL, NULL, parse_xml_btrace_pt_raw },
2010 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2011 };
2012
2013 static const struct gdb_xml_attribute btrace_attributes[] = {
2014 { "version", GDB_XML_AF_NONE, NULL, NULL },
2015 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2016 };
2017
2018 static const struct gdb_xml_element btrace_children[] = {
2019 { "block", block_attributes, NULL,
2020 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, parse_xml_btrace_block, NULL },
2021 { "pt", NULL, btrace_pt_children, GDB_XML_EF_OPTIONAL, parse_xml_btrace_pt,
2022 NULL },
2023 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2024 };
2025
2026 static const struct gdb_xml_element btrace_elements[] = {
2027 { "btrace", btrace_attributes, btrace_children, GDB_XML_EF_NONE,
2028 check_xml_btrace_version, NULL },
2029 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2030 };
2031
2032 #endif /* defined (HAVE_LIBEXPAT) */
2033
2034 /* See btrace.h. */
2035
2036 void
2037 parse_xml_btrace (struct btrace_data *btrace, const char *buffer)
2038 {
2039 struct cleanup *cleanup;
2040 int errcode;
2041
2042 #if defined (HAVE_LIBEXPAT)
2043
2044 btrace->format = BTRACE_FORMAT_NONE;
2045
2046 cleanup = make_cleanup_btrace_data (btrace);
2047 errcode = gdb_xml_parse_quick (_("btrace"), "btrace.dtd", btrace_elements,
2048 buffer, btrace);
2049 if (errcode != 0)
2050 error (_("Error parsing branch trace."));
2051
2052 /* Keep parse results. */
2053 discard_cleanups (cleanup);
2054
2055 #else /* !defined (HAVE_LIBEXPAT) */
2056
2057 error (_("Cannot process branch trace. XML parsing is not supported."));
2058
2059 #endif /* !defined (HAVE_LIBEXPAT) */
2060 }
2061
2062 #if defined (HAVE_LIBEXPAT)
2063
2064 /* Parse a btrace-conf "bts" xml record. */
2065
2066 static void
2067 parse_xml_btrace_conf_bts (struct gdb_xml_parser *parser,
2068 const struct gdb_xml_element *element,
2069 void *user_data, VEC (gdb_xml_value_s) *attributes)
2070 {
2071 struct btrace_config *conf;
2072 struct gdb_xml_value *size;
2073
2074 conf = (struct btrace_config *) user_data;
2075 conf->format = BTRACE_FORMAT_BTS;
2076 conf->bts.size = 0;
2077
2078 size = xml_find_attribute (attributes, "size");
2079 if (size != NULL)
2080 conf->bts.size = (unsigned int) *(ULONGEST *) size->value;
2081 }
2082
2083 /* Parse a btrace-conf "pt" xml record. */
2084
2085 static void
2086 parse_xml_btrace_conf_pt (struct gdb_xml_parser *parser,
2087 const struct gdb_xml_element *element,
2088 void *user_data, VEC (gdb_xml_value_s) *attributes)
2089 {
2090 struct btrace_config *conf;
2091 struct gdb_xml_value *size;
2092
2093 conf = (struct btrace_config *) user_data;
2094 conf->format = BTRACE_FORMAT_PT;
2095 conf->pt.size = 0;
2096
2097 size = xml_find_attribute (attributes, "size");
2098 if (size != NULL)
2099 conf->pt.size = (unsigned int) *(ULONGEST *) size->value;
2100 }
2101
2102 static const struct gdb_xml_attribute btrace_conf_pt_attributes[] = {
2103 { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2104 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2105 };
2106
2107 static const struct gdb_xml_attribute btrace_conf_bts_attributes[] = {
2108 { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2109 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2110 };
2111
2112 static const struct gdb_xml_element btrace_conf_children[] = {
2113 { "bts", btrace_conf_bts_attributes, NULL, GDB_XML_EF_OPTIONAL,
2114 parse_xml_btrace_conf_bts, NULL },
2115 { "pt", btrace_conf_pt_attributes, NULL, GDB_XML_EF_OPTIONAL,
2116 parse_xml_btrace_conf_pt, NULL },
2117 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2118 };
2119
2120 static const struct gdb_xml_attribute btrace_conf_attributes[] = {
2121 { "version", GDB_XML_AF_NONE, NULL, NULL },
2122 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2123 };
2124
2125 static const struct gdb_xml_element btrace_conf_elements[] = {
2126 { "btrace-conf", btrace_conf_attributes, btrace_conf_children,
2127 GDB_XML_EF_NONE, NULL, NULL },
2128 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2129 };
2130
2131 #endif /* defined (HAVE_LIBEXPAT) */
2132
2133 /* See btrace.h. */
2134
2135 void
2136 parse_xml_btrace_conf (struct btrace_config *conf, const char *xml)
2137 {
2138 int errcode;
2139
2140 #if defined (HAVE_LIBEXPAT)
2141
2142 errcode = gdb_xml_parse_quick (_("btrace-conf"), "btrace-conf.dtd",
2143 btrace_conf_elements, xml, conf);
2144 if (errcode != 0)
2145 error (_("Error parsing branch trace configuration."));
2146
2147 #else /* !defined (HAVE_LIBEXPAT) */
2148
2149 error (_("XML parsing is not supported."));
2150
2151 #endif /* !defined (HAVE_LIBEXPAT) */
2152 }
2153
2154 /* See btrace.h. */
2155
2156 const struct btrace_insn *
2157 btrace_insn_get (const struct btrace_insn_iterator *it)
2158 {
2159 const struct btrace_function *bfun;
2160 unsigned int index, end;
2161
2162 index = it->index;
2163 bfun = it->function;
2164
2165 /* Check if the iterator points to a gap in the trace. */
2166 if (bfun->errcode != 0)
2167 return NULL;
2168
2169 /* The index is within the bounds of this function's instruction vector. */
2170 end = VEC_length (btrace_insn_s, bfun->insn);
2171 gdb_assert (0 < end);
2172 gdb_assert (index < end);
2173
2174 return VEC_index (btrace_insn_s, bfun->insn, index);
2175 }
2176
2177 /* See btrace.h. */
2178
2179 unsigned int
2180 btrace_insn_number (const struct btrace_insn_iterator *it)
2181 {
2182 const struct btrace_function *bfun;
2183
2184 bfun = it->function;
2185
2186 /* Return zero if the iterator points to a gap in the trace. */
2187 if (bfun->errcode != 0)
2188 return 0;
2189
2190 return bfun->insn_offset + it->index;
2191 }
2192
2193 /* See btrace.h. */
2194
2195 void
2196 btrace_insn_begin (struct btrace_insn_iterator *it,
2197 const struct btrace_thread_info *btinfo)
2198 {
2199 const struct btrace_function *bfun;
2200
2201 bfun = btinfo->begin;
2202 if (bfun == NULL)
2203 error (_("No trace."));
2204
2205 it->function = bfun;
2206 it->index = 0;
2207 }
2208
2209 /* See btrace.h. */
2210
2211 void
2212 btrace_insn_end (struct btrace_insn_iterator *it,
2213 const struct btrace_thread_info *btinfo)
2214 {
2215 const struct btrace_function *bfun;
2216 unsigned int length;
2217
2218 bfun = btinfo->end;
2219 if (bfun == NULL)
2220 error (_("No trace."));
2221
2222 length = VEC_length (btrace_insn_s, bfun->insn);
2223
2224 /* The last function may either be a gap or it contains the current
2225 instruction, which is one past the end of the execution trace; ignore
2226 it. */
2227 if (length > 0)
2228 length -= 1;
2229
2230 it->function = bfun;
2231 it->index = length;
2232 }
2233
2234 /* See btrace.h. */
2235
2236 unsigned int
2237 btrace_insn_next (struct btrace_insn_iterator *it, unsigned int stride)
2238 {
2239 const struct btrace_function *bfun;
2240 unsigned int index, steps;
2241
2242 bfun = it->function;
2243 steps = 0;
2244 index = it->index;
2245
2246 while (stride != 0)
2247 {
2248 unsigned int end, space, adv;
2249
2250 end = VEC_length (btrace_insn_s, bfun->insn);
2251
2252 /* An empty function segment represents a gap in the trace. We count
2253 it as one instruction. */
2254 if (end == 0)
2255 {
2256 const struct btrace_function *next;
2257
2258 next = bfun->flow.next;
2259 if (next == NULL)
2260 break;
2261
2262 stride -= 1;
2263 steps += 1;
2264
2265 bfun = next;
2266 index = 0;
2267
2268 continue;
2269 }
2270
2271 gdb_assert (0 < end);
2272 gdb_assert (index < end);
2273
2274 /* Compute the number of instructions remaining in this segment. */
2275 space = end - index;
2276
2277 /* Advance the iterator as far as possible within this segment. */
2278 adv = std::min (space, stride);
2279 stride -= adv;
2280 index += adv;
2281 steps += adv;
2282
2283 /* Move to the next function if we're at the end of this one. */
2284 if (index == end)
2285 {
2286 const struct btrace_function *next;
2287
2288 next = bfun->flow.next;
2289 if (next == NULL)
2290 {
2291 /* We stepped past the last function.
2292
2293 Let's adjust the index to point to the last instruction in
2294 the previous function. */
2295 index -= 1;
2296 steps -= 1;
2297 break;
2298 }
2299
2300 /* We now point to the first instruction in the new function. */
2301 bfun = next;
2302 index = 0;
2303 }
2304
2305 /* We did make progress. */
2306 gdb_assert (adv > 0);
2307 }
2308
2309 /* Update the iterator. */
2310 it->function = bfun;
2311 it->index = index;
2312
2313 return steps;
2314 }
2315
2316 /* See btrace.h. */
2317
2318 unsigned int
2319 btrace_insn_prev (struct btrace_insn_iterator *it, unsigned int stride)
2320 {
2321 const struct btrace_function *bfun;
2322 unsigned int index, steps;
2323
2324 bfun = it->function;
2325 steps = 0;
2326 index = it->index;
2327
2328 while (stride != 0)
2329 {
2330 unsigned int adv;
2331
2332 /* Move to the previous function if we're at the start of this one. */
2333 if (index == 0)
2334 {
2335 const struct btrace_function *prev;
2336
2337 prev = bfun->flow.prev;
2338 if (prev == NULL)
2339 break;
2340
2341 /* We point to one after the last instruction in the new function. */
2342 bfun = prev;
2343 index = VEC_length (btrace_insn_s, bfun->insn);
2344
2345 /* An empty function segment represents a gap in the trace. We count
2346 it as one instruction. */
2347 if (index == 0)
2348 {
2349 stride -= 1;
2350 steps += 1;
2351
2352 continue;
2353 }
2354 }
2355
2356 /* Advance the iterator as far as possible within this segment. */
2357 adv = std::min (index, stride);
2358
2359 stride -= adv;
2360 index -= adv;
2361 steps += adv;
2362
2363 /* We did make progress. */
2364 gdb_assert (adv > 0);
2365 }
2366
2367 /* Update the iterator. */
2368 it->function = bfun;
2369 it->index = index;
2370
2371 return steps;
2372 }
2373
2374 /* See btrace.h. */
2375
2376 int
2377 btrace_insn_cmp (const struct btrace_insn_iterator *lhs,
2378 const struct btrace_insn_iterator *rhs)
2379 {
2380 unsigned int lnum, rnum;
2381
2382 lnum = btrace_insn_number (lhs);
2383 rnum = btrace_insn_number (rhs);
2384
2385 /* A gap has an instruction number of zero. Things are getting more
2386 complicated if gaps are involved.
2387
2388 We take the instruction number offset from the iterator's function.
2389 This is the number of the first instruction after the gap.
2390
2391 This is OK as long as both lhs and rhs point to gaps. If only one of
2392 them does, we need to adjust the number based on the other's regular
2393 instruction number. Otherwise, a gap might compare equal to an
2394 instruction. */
2395
2396 if (lnum == 0 && rnum == 0)
2397 {
2398 lnum = lhs->function->insn_offset;
2399 rnum = rhs->function->insn_offset;
2400 }
2401 else if (lnum == 0)
2402 {
2403 lnum = lhs->function->insn_offset;
2404
2405 if (lnum == rnum)
2406 lnum -= 1;
2407 }
2408 else if (rnum == 0)
2409 {
2410 rnum = rhs->function->insn_offset;
2411
2412 if (rnum == lnum)
2413 rnum -= 1;
2414 }
2415
2416 return (int) (lnum - rnum);
2417 }
2418
2419 /* See btrace.h. */
2420
2421 int
2422 btrace_find_insn_by_number (struct btrace_insn_iterator *it,
2423 const struct btrace_thread_info *btinfo,
2424 unsigned int number)
2425 {
2426 const struct btrace_function *bfun;
2427 unsigned int end, length;
2428
2429 for (bfun = btinfo->end; bfun != NULL; bfun = bfun->flow.prev)
2430 {
2431 /* Skip gaps. */
2432 if (bfun->errcode != 0)
2433 continue;
2434
2435 if (bfun->insn_offset <= number)
2436 break;
2437 }
2438
2439 if (bfun == NULL)
2440 return 0;
2441
2442 length = VEC_length (btrace_insn_s, bfun->insn);
2443 gdb_assert (length > 0);
2444
2445 end = bfun->insn_offset + length;
2446 if (end <= number)
2447 return 0;
2448
2449 it->function = bfun;
2450 it->index = number - bfun->insn_offset;
2451
2452 return 1;
2453 }
2454
2455 /* See btrace.h. */
2456
2457 const struct btrace_function *
2458 btrace_call_get (const struct btrace_call_iterator *it)
2459 {
2460 return it->function;
2461 }
2462
2463 /* See btrace.h. */
2464
2465 unsigned int
2466 btrace_call_number (const struct btrace_call_iterator *it)
2467 {
2468 const struct btrace_thread_info *btinfo;
2469 const struct btrace_function *bfun;
2470 unsigned int insns;
2471
2472 btinfo = it->btinfo;
2473 bfun = it->function;
2474 if (bfun != NULL)
2475 return bfun->number;
2476
2477 /* For the end iterator, i.e. bfun == NULL, we return one more than the
2478 number of the last function. */
2479 bfun = btinfo->end;
2480 insns = VEC_length (btrace_insn_s, bfun->insn);
2481
2482 /* If the function contains only a single instruction (i.e. the current
2483 instruction), it will be skipped and its number is already the number
2484 we seek. */
2485 if (insns == 1)
2486 return bfun->number;
2487
2488 /* Otherwise, return one more than the number of the last function. */
2489 return bfun->number + 1;
2490 }
2491
2492 /* See btrace.h. */
2493
2494 void
2495 btrace_call_begin (struct btrace_call_iterator *it,
2496 const struct btrace_thread_info *btinfo)
2497 {
2498 const struct btrace_function *bfun;
2499
2500 bfun = btinfo->begin;
2501 if (bfun == NULL)
2502 error (_("No trace."));
2503
2504 it->btinfo = btinfo;
2505 it->function = bfun;
2506 }
2507
2508 /* See btrace.h. */
2509
2510 void
2511 btrace_call_end (struct btrace_call_iterator *it,
2512 const struct btrace_thread_info *btinfo)
2513 {
2514 const struct btrace_function *bfun;
2515
2516 bfun = btinfo->end;
2517 if (bfun == NULL)
2518 error (_("No trace."));
2519
2520 it->btinfo = btinfo;
2521 it->function = NULL;
2522 }
2523
2524 /* See btrace.h. */
2525
2526 unsigned int
2527 btrace_call_next (struct btrace_call_iterator *it, unsigned int stride)
2528 {
2529 const struct btrace_function *bfun;
2530 unsigned int steps;
2531
2532 bfun = it->function;
2533 steps = 0;
2534 while (bfun != NULL)
2535 {
2536 const struct btrace_function *next;
2537 unsigned int insns;
2538
2539 next = bfun->flow.next;
2540 if (next == NULL)
2541 {
2542 /* Ignore the last function if it only contains a single
2543 (i.e. the current) instruction. */
2544 insns = VEC_length (btrace_insn_s, bfun->insn);
2545 if (insns == 1)
2546 steps -= 1;
2547 }
2548
2549 if (stride == steps)
2550 break;
2551
2552 bfun = next;
2553 steps += 1;
2554 }
2555
2556 it->function = bfun;
2557 return steps;
2558 }
2559
2560 /* See btrace.h. */
2561
2562 unsigned int
2563 btrace_call_prev (struct btrace_call_iterator *it, unsigned int stride)
2564 {
2565 const struct btrace_thread_info *btinfo;
2566 const struct btrace_function *bfun;
2567 unsigned int steps;
2568
2569 bfun = it->function;
2570 steps = 0;
2571
2572 if (bfun == NULL)
2573 {
2574 unsigned int insns;
2575
2576 btinfo = it->btinfo;
2577 bfun = btinfo->end;
2578 if (bfun == NULL)
2579 return 0;
2580
2581 /* Ignore the last function if it only contains a single
2582 (i.e. the current) instruction. */
2583 insns = VEC_length (btrace_insn_s, bfun->insn);
2584 if (insns == 1)
2585 bfun = bfun->flow.prev;
2586
2587 if (bfun == NULL)
2588 return 0;
2589
2590 steps += 1;
2591 }
2592
2593 while (steps < stride)
2594 {
2595 const struct btrace_function *prev;
2596
2597 prev = bfun->flow.prev;
2598 if (prev == NULL)
2599 break;
2600
2601 bfun = prev;
2602 steps += 1;
2603 }
2604
2605 it->function = bfun;
2606 return steps;
2607 }
2608
2609 /* See btrace.h. */
2610
2611 int
2612 btrace_call_cmp (const struct btrace_call_iterator *lhs,
2613 const struct btrace_call_iterator *rhs)
2614 {
2615 unsigned int lnum, rnum;
2616
2617 lnum = btrace_call_number (lhs);
2618 rnum = btrace_call_number (rhs);
2619
2620 return (int) (lnum - rnum);
2621 }
2622
2623 /* See btrace.h. */
2624
2625 int
2626 btrace_find_call_by_number (struct btrace_call_iterator *it,
2627 const struct btrace_thread_info *btinfo,
2628 unsigned int number)
2629 {
2630 const struct btrace_function *bfun;
2631
2632 for (bfun = btinfo->end; bfun != NULL; bfun = bfun->flow.prev)
2633 {
2634 unsigned int bnum;
2635
2636 bnum = bfun->number;
2637 if (number == bnum)
2638 {
2639 it->btinfo = btinfo;
2640 it->function = bfun;
2641 return 1;
2642 }
2643
2644 /* Functions are ordered and numbered consecutively. We could bail out
2645 earlier. On the other hand, it is very unlikely that we search for
2646 a nonexistent function. */
2647 }
2648
2649 return 0;
2650 }
2651
2652 /* See btrace.h. */
2653
2654 void
2655 btrace_set_insn_history (struct btrace_thread_info *btinfo,
2656 const struct btrace_insn_iterator *begin,
2657 const struct btrace_insn_iterator *end)
2658 {
2659 if (btinfo->insn_history == NULL)
2660 btinfo->insn_history = XCNEW (struct btrace_insn_history);
2661
2662 btinfo->insn_history->begin = *begin;
2663 btinfo->insn_history->end = *end;
2664 }
2665
2666 /* See btrace.h. */
2667
2668 void
2669 btrace_set_call_history (struct btrace_thread_info *btinfo,
2670 const struct btrace_call_iterator *begin,
2671 const struct btrace_call_iterator *end)
2672 {
2673 gdb_assert (begin->btinfo == end->btinfo);
2674
2675 if (btinfo->call_history == NULL)
2676 btinfo->call_history = XCNEW (struct btrace_call_history);
2677
2678 btinfo->call_history->begin = *begin;
2679 btinfo->call_history->end = *end;
2680 }
2681
2682 /* See btrace.h. */
2683
2684 int
2685 btrace_is_replaying (struct thread_info *tp)
2686 {
2687 return tp->btrace.replay != NULL;
2688 }
2689
2690 /* See btrace.h. */
2691
2692 int
2693 btrace_is_empty (struct thread_info *tp)
2694 {
2695 struct btrace_insn_iterator begin, end;
2696 struct btrace_thread_info *btinfo;
2697
2698 btinfo = &tp->btrace;
2699
2700 if (btinfo->begin == NULL)
2701 return 1;
2702
2703 btrace_insn_begin (&begin, btinfo);
2704 btrace_insn_end (&end, btinfo);
2705
2706 return btrace_insn_cmp (&begin, &end) == 0;
2707 }
2708
2709 /* Forward the cleanup request. */
2710
2711 static void
2712 do_btrace_data_cleanup (void *arg)
2713 {
2714 btrace_data_fini ((struct btrace_data *) arg);
2715 }
2716
2717 /* See btrace.h. */
2718
2719 struct cleanup *
2720 make_cleanup_btrace_data (struct btrace_data *data)
2721 {
2722 return make_cleanup (do_btrace_data_cleanup, data);
2723 }
2724
2725 #if defined (HAVE_LIBIPT)
2726
2727 /* Print a single packet. */
2728
2729 static void
2730 pt_print_packet (const struct pt_packet *packet)
2731 {
2732 switch (packet->type)
2733 {
2734 default:
2735 printf_unfiltered (("[??: %x]"), packet->type);
2736 break;
2737
2738 case ppt_psb:
2739 printf_unfiltered (("psb"));
2740 break;
2741
2742 case ppt_psbend:
2743 printf_unfiltered (("psbend"));
2744 break;
2745
2746 case ppt_pad:
2747 printf_unfiltered (("pad"));
2748 break;
2749
2750 case ppt_tip:
2751 printf_unfiltered (("tip %u: 0x%" PRIx64 ""),
2752 packet->payload.ip.ipc,
2753 packet->payload.ip.ip);
2754 break;
2755
2756 case ppt_tip_pge:
2757 printf_unfiltered (("tip.pge %u: 0x%" PRIx64 ""),
2758 packet->payload.ip.ipc,
2759 packet->payload.ip.ip);
2760 break;
2761
2762 case ppt_tip_pgd:
2763 printf_unfiltered (("tip.pgd %u: 0x%" PRIx64 ""),
2764 packet->payload.ip.ipc,
2765 packet->payload.ip.ip);
2766 break;
2767
2768 case ppt_fup:
2769 printf_unfiltered (("fup %u: 0x%" PRIx64 ""),
2770 packet->payload.ip.ipc,
2771 packet->payload.ip.ip);
2772 break;
2773
2774 case ppt_tnt_8:
2775 printf_unfiltered (("tnt-8 %u: 0x%" PRIx64 ""),
2776 packet->payload.tnt.bit_size,
2777 packet->payload.tnt.payload);
2778 break;
2779
2780 case ppt_tnt_64:
2781 printf_unfiltered (("tnt-64 %u: 0x%" PRIx64 ""),
2782 packet->payload.tnt.bit_size,
2783 packet->payload.tnt.payload);
2784 break;
2785
2786 case ppt_pip:
2787 printf_unfiltered (("pip %" PRIx64 "%s"), packet->payload.pip.cr3,
2788 packet->payload.pip.nr ? (" nr") : (""));
2789 break;
2790
2791 case ppt_tsc:
2792 printf_unfiltered (("tsc %" PRIx64 ""), packet->payload.tsc.tsc);
2793 break;
2794
2795 case ppt_cbr:
2796 printf_unfiltered (("cbr %u"), packet->payload.cbr.ratio);
2797 break;
2798
2799 case ppt_mode:
2800 switch (packet->payload.mode.leaf)
2801 {
2802 default:
2803 printf_unfiltered (("mode %u"), packet->payload.mode.leaf);
2804 break;
2805
2806 case pt_mol_exec:
2807 printf_unfiltered (("mode.exec%s%s"),
2808 packet->payload.mode.bits.exec.csl
2809 ? (" cs.l") : (""),
2810 packet->payload.mode.bits.exec.csd
2811 ? (" cs.d") : (""));
2812 break;
2813
2814 case pt_mol_tsx:
2815 printf_unfiltered (("mode.tsx%s%s"),
2816 packet->payload.mode.bits.tsx.intx
2817 ? (" intx") : (""),
2818 packet->payload.mode.bits.tsx.abrt
2819 ? (" abrt") : (""));
2820 break;
2821 }
2822 break;
2823
2824 case ppt_ovf:
2825 printf_unfiltered (("ovf"));
2826 break;
2827
2828 case ppt_stop:
2829 printf_unfiltered (("stop"));
2830 break;
2831
2832 case ppt_vmcs:
2833 printf_unfiltered (("vmcs %" PRIx64 ""), packet->payload.vmcs.base);
2834 break;
2835
2836 case ppt_tma:
2837 printf_unfiltered (("tma %x %x"), packet->payload.tma.ctc,
2838 packet->payload.tma.fc);
2839 break;
2840
2841 case ppt_mtc:
2842 printf_unfiltered (("mtc %x"), packet->payload.mtc.ctc);
2843 break;
2844
2845 case ppt_cyc:
2846 printf_unfiltered (("cyc %" PRIx64 ""), packet->payload.cyc.value);
2847 break;
2848
2849 case ppt_mnt:
2850 printf_unfiltered (("mnt %" PRIx64 ""), packet->payload.mnt.payload);
2851 break;
2852 }
2853 }
2854
2855 /* Decode packets into MAINT using DECODER. */
2856
2857 static void
2858 btrace_maint_decode_pt (struct btrace_maint_info *maint,
2859 struct pt_packet_decoder *decoder)
2860 {
2861 int errcode;
2862
2863 for (;;)
2864 {
2865 struct btrace_pt_packet packet;
2866
2867 errcode = pt_pkt_sync_forward (decoder);
2868 if (errcode < 0)
2869 break;
2870
2871 for (;;)
2872 {
2873 pt_pkt_get_offset (decoder, &packet.offset);
2874
2875 errcode = pt_pkt_next (decoder, &packet.packet,
2876 sizeof(packet.packet));
2877 if (errcode < 0)
2878 break;
2879
2880 if (maint_btrace_pt_skip_pad == 0 || packet.packet.type != ppt_pad)
2881 {
2882 packet.errcode = pt_errcode (errcode);
2883 VEC_safe_push (btrace_pt_packet_s, maint->variant.pt.packets,
2884 &packet);
2885 }
2886 }
2887
2888 if (errcode == -pte_eos)
2889 break;
2890
2891 packet.errcode = pt_errcode (errcode);
2892 VEC_safe_push (btrace_pt_packet_s, maint->variant.pt.packets,
2893 &packet);
2894
2895 warning (_("Error at trace offset 0x%" PRIx64 ": %s."),
2896 packet.offset, pt_errstr (packet.errcode));
2897 }
2898
2899 if (errcode != -pte_eos)
2900 warning (_("Failed to synchronize onto the Intel Processor Trace "
2901 "stream: %s."), pt_errstr (pt_errcode (errcode)));
2902 }
2903
2904 /* Update the packet history in BTINFO. */
2905
2906 static void
2907 btrace_maint_update_pt_packets (struct btrace_thread_info *btinfo)
2908 {
2909 volatile struct gdb_exception except;
2910 struct pt_packet_decoder *decoder;
2911 struct btrace_data_pt *pt;
2912 struct pt_config config;
2913 int errcode;
2914
2915 pt = &btinfo->data.variant.pt;
2916
2917 /* Nothing to do if there is no trace. */
2918 if (pt->size == 0)
2919 return;
2920
2921 memset (&config, 0, sizeof(config));
2922
2923 config.size = sizeof (config);
2924 config.begin = pt->data;
2925 config.end = pt->data + pt->size;
2926
2927 config.cpu.vendor = pt_translate_cpu_vendor (pt->config.cpu.vendor);
2928 config.cpu.family = pt->config.cpu.family;
2929 config.cpu.model = pt->config.cpu.model;
2930 config.cpu.stepping = pt->config.cpu.stepping;
2931
2932 errcode = pt_cpu_errata (&config.errata, &config.cpu);
2933 if (errcode < 0)
2934 error (_("Failed to configure the Intel Processor Trace decoder: %s."),
2935 pt_errstr (pt_errcode (errcode)));
2936
2937 decoder = pt_pkt_alloc_decoder (&config);
2938 if (decoder == NULL)
2939 error (_("Failed to allocate the Intel Processor Trace decoder."));
2940
2941 TRY
2942 {
2943 btrace_maint_decode_pt (&btinfo->maint, decoder);
2944 }
2945 CATCH (except, RETURN_MASK_ALL)
2946 {
2947 pt_pkt_free_decoder (decoder);
2948
2949 if (except.reason < 0)
2950 throw_exception (except);
2951 }
2952 END_CATCH
2953
2954 pt_pkt_free_decoder (decoder);
2955 }
2956
2957 #endif /* !defined (HAVE_LIBIPT) */
2958
2959 /* Update the packet maintenance information for BTINFO and store the
2960 low and high bounds into BEGIN and END, respectively.
2961 Store the current iterator state into FROM and TO. */
2962
2963 static void
2964 btrace_maint_update_packets (struct btrace_thread_info *btinfo,
2965 unsigned int *begin, unsigned int *end,
2966 unsigned int *from, unsigned int *to)
2967 {
2968 switch (btinfo->data.format)
2969 {
2970 default:
2971 *begin = 0;
2972 *end = 0;
2973 *from = 0;
2974 *to = 0;
2975 break;
2976
2977 case BTRACE_FORMAT_BTS:
2978 /* Nothing to do - we operate directly on BTINFO->DATA. */
2979 *begin = 0;
2980 *end = VEC_length (btrace_block_s, btinfo->data.variant.bts.blocks);
2981 *from = btinfo->maint.variant.bts.packet_history.begin;
2982 *to = btinfo->maint.variant.bts.packet_history.end;
2983 break;
2984
2985 #if defined (HAVE_LIBIPT)
2986 case BTRACE_FORMAT_PT:
2987 if (VEC_empty (btrace_pt_packet_s, btinfo->maint.variant.pt.packets))
2988 btrace_maint_update_pt_packets (btinfo);
2989
2990 *begin = 0;
2991 *end = VEC_length (btrace_pt_packet_s, btinfo->maint.variant.pt.packets);
2992 *from = btinfo->maint.variant.pt.packet_history.begin;
2993 *to = btinfo->maint.variant.pt.packet_history.end;
2994 break;
2995 #endif /* defined (HAVE_LIBIPT) */
2996 }
2997 }
2998
2999 /* Print packets in BTINFO from BEGIN (inclusive) until END (exclusive) and
3000 update the current iterator position. */
3001
3002 static void
3003 btrace_maint_print_packets (struct btrace_thread_info *btinfo,
3004 unsigned int begin, unsigned int end)
3005 {
3006 switch (btinfo->data.format)
3007 {
3008 default:
3009 break;
3010
3011 case BTRACE_FORMAT_BTS:
3012 {
3013 VEC (btrace_block_s) *blocks;
3014 unsigned int blk;
3015
3016 blocks = btinfo->data.variant.bts.blocks;
3017 for (blk = begin; blk < end; ++blk)
3018 {
3019 const btrace_block_s *block;
3020
3021 block = VEC_index (btrace_block_s, blocks, blk);
3022
3023 printf_unfiltered ("%u\tbegin: %s, end: %s\n", blk,
3024 core_addr_to_string_nz (block->begin),
3025 core_addr_to_string_nz (block->end));
3026 }
3027
3028 btinfo->maint.variant.bts.packet_history.begin = begin;
3029 btinfo->maint.variant.bts.packet_history.end = end;
3030 }
3031 break;
3032
3033 #if defined (HAVE_LIBIPT)
3034 case BTRACE_FORMAT_PT:
3035 {
3036 VEC (btrace_pt_packet_s) *packets;
3037 unsigned int pkt;
3038
3039 packets = btinfo->maint.variant.pt.packets;
3040 for (pkt = begin; pkt < end; ++pkt)
3041 {
3042 const struct btrace_pt_packet *packet;
3043
3044 packet = VEC_index (btrace_pt_packet_s, packets, pkt);
3045
3046 printf_unfiltered ("%u\t", pkt);
3047 printf_unfiltered ("0x%" PRIx64 "\t", packet->offset);
3048
3049 if (packet->errcode == pte_ok)
3050 pt_print_packet (&packet->packet);
3051 else
3052 printf_unfiltered ("[error: %s]", pt_errstr (packet->errcode));
3053
3054 printf_unfiltered ("\n");
3055 }
3056
3057 btinfo->maint.variant.pt.packet_history.begin = begin;
3058 btinfo->maint.variant.pt.packet_history.end = end;
3059 }
3060 break;
3061 #endif /* defined (HAVE_LIBIPT) */
3062 }
3063 }
3064
3065 /* Read a number from an argument string. */
3066
3067 static unsigned int
3068 get_uint (char **arg)
3069 {
3070 char *begin, *end, *pos;
3071 unsigned long number;
3072
3073 begin = *arg;
3074 pos = skip_spaces (begin);
3075
3076 if (!isdigit (*pos))
3077 error (_("Expected positive number, got: %s."), pos);
3078
3079 number = strtoul (pos, &end, 10);
3080 if (number > UINT_MAX)
3081 error (_("Number too big."));
3082
3083 *arg += (end - begin);
3084
3085 return (unsigned int) number;
3086 }
3087
3088 /* Read a context size from an argument string. */
3089
3090 static int
3091 get_context_size (char **arg)
3092 {
3093 char *pos;
3094 int number;
3095
3096 pos = skip_spaces (*arg);
3097
3098 if (!isdigit (*pos))
3099 error (_("Expected positive number, got: %s."), pos);
3100
3101 return strtol (pos, arg, 10);
3102 }
3103
3104 /* Complain about junk at the end of an argument string. */
3105
3106 static void
3107 no_chunk (char *arg)
3108 {
3109 if (*arg != 0)
3110 error (_("Junk after argument: %s."), arg);
3111 }
3112
3113 /* The "maintenance btrace packet-history" command. */
3114
3115 static void
3116 maint_btrace_packet_history_cmd (char *arg, int from_tty)
3117 {
3118 struct btrace_thread_info *btinfo;
3119 struct thread_info *tp;
3120 unsigned int size, begin, end, from, to;
3121
3122 tp = find_thread_ptid (inferior_ptid);
3123 if (tp == NULL)
3124 error (_("No thread."));
3125
3126 size = 10;
3127 btinfo = &tp->btrace;
3128
3129 btrace_maint_update_packets (btinfo, &begin, &end, &from, &to);
3130 if (begin == end)
3131 {
3132 printf_unfiltered (_("No trace.\n"));
3133 return;
3134 }
3135
3136 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
3137 {
3138 from = to;
3139
3140 if (end - from < size)
3141 size = end - from;
3142 to = from + size;
3143 }
3144 else if (strcmp (arg, "-") == 0)
3145 {
3146 to = from;
3147
3148 if (to - begin < size)
3149 size = to - begin;
3150 from = to - size;
3151 }
3152 else
3153 {
3154 from = get_uint (&arg);
3155 if (end <= from)
3156 error (_("'%u' is out of range."), from);
3157
3158 arg = skip_spaces (arg);
3159 if (*arg == ',')
3160 {
3161 arg = skip_spaces (++arg);
3162
3163 if (*arg == '+')
3164 {
3165 arg += 1;
3166 size = get_context_size (&arg);
3167
3168 no_chunk (arg);
3169
3170 if (end - from < size)
3171 size = end - from;
3172 to = from + size;
3173 }
3174 else if (*arg == '-')
3175 {
3176 arg += 1;
3177 size = get_context_size (&arg);
3178
3179 no_chunk (arg);
3180
3181 /* Include the packet given as first argument. */
3182 from += 1;
3183 to = from;
3184
3185 if (to - begin < size)
3186 size = to - begin;
3187 from = to - size;
3188 }
3189 else
3190 {
3191 to = get_uint (&arg);
3192
3193 /* Include the packet at the second argument and silently
3194 truncate the range. */
3195 if (to < end)
3196 to += 1;
3197 else
3198 to = end;
3199
3200 no_chunk (arg);
3201 }
3202 }
3203 else
3204 {
3205 no_chunk (arg);
3206
3207 if (end - from < size)
3208 size = end - from;
3209 to = from + size;
3210 }
3211
3212 dont_repeat ();
3213 }
3214
3215 btrace_maint_print_packets (btinfo, from, to);
3216 }
3217
3218 /* The "maintenance btrace clear-packet-history" command. */
3219
3220 static void
3221 maint_btrace_clear_packet_history_cmd (char *args, int from_tty)
3222 {
3223 struct btrace_thread_info *btinfo;
3224 struct thread_info *tp;
3225
3226 if (args != NULL && *args != 0)
3227 error (_("Invalid argument."));
3228
3229 tp = find_thread_ptid (inferior_ptid);
3230 if (tp == NULL)
3231 error (_("No thread."));
3232
3233 btinfo = &tp->btrace;
3234
3235 /* Must clear the maint data before - it depends on BTINFO->DATA. */
3236 btrace_maint_clear (btinfo);
3237 btrace_data_clear (&btinfo->data);
3238 }
3239
3240 /* The "maintenance btrace clear" command. */
3241
3242 static void
3243 maint_btrace_clear_cmd (char *args, int from_tty)
3244 {
3245 struct btrace_thread_info *btinfo;
3246 struct thread_info *tp;
3247
3248 if (args != NULL && *args != 0)
3249 error (_("Invalid argument."));
3250
3251 tp = find_thread_ptid (inferior_ptid);
3252 if (tp == NULL)
3253 error (_("No thread."));
3254
3255 btrace_clear (tp);
3256 }
3257
3258 /* The "maintenance btrace" command. */
3259
3260 static void
3261 maint_btrace_cmd (char *args, int from_tty)
3262 {
3263 help_list (maint_btrace_cmdlist, "maintenance btrace ", all_commands,
3264 gdb_stdout);
3265 }
3266
3267 /* The "maintenance set btrace" command. */
3268
3269 static void
3270 maint_btrace_set_cmd (char *args, int from_tty)
3271 {
3272 help_list (maint_btrace_set_cmdlist, "maintenance set btrace ", all_commands,
3273 gdb_stdout);
3274 }
3275
3276 /* The "maintenance show btrace" command. */
3277
3278 static void
3279 maint_btrace_show_cmd (char *args, int from_tty)
3280 {
3281 help_list (maint_btrace_show_cmdlist, "maintenance show btrace ",
3282 all_commands, gdb_stdout);
3283 }
3284
3285 /* The "maintenance set btrace pt" command. */
3286
3287 static void
3288 maint_btrace_pt_set_cmd (char *args, int from_tty)
3289 {
3290 help_list (maint_btrace_pt_set_cmdlist, "maintenance set btrace pt ",
3291 all_commands, gdb_stdout);
3292 }
3293
3294 /* The "maintenance show btrace pt" command. */
3295
3296 static void
3297 maint_btrace_pt_show_cmd (char *args, int from_tty)
3298 {
3299 help_list (maint_btrace_pt_show_cmdlist, "maintenance show btrace pt ",
3300 all_commands, gdb_stdout);
3301 }
3302
3303 /* The "maintenance info btrace" command. */
3304
3305 static void
3306 maint_info_btrace_cmd (char *args, int from_tty)
3307 {
3308 struct btrace_thread_info *btinfo;
3309 struct thread_info *tp;
3310 const struct btrace_config *conf;
3311
3312 if (args != NULL && *args != 0)
3313 error (_("Invalid argument."));
3314
3315 tp = find_thread_ptid (inferior_ptid);
3316 if (tp == NULL)
3317 error (_("No thread."));
3318
3319 btinfo = &tp->btrace;
3320
3321 conf = btrace_conf (btinfo);
3322 if (conf == NULL)
3323 error (_("No btrace configuration."));
3324
3325 printf_unfiltered (_("Format: %s.\n"),
3326 btrace_format_string (conf->format));
3327
3328 switch (conf->format)
3329 {
3330 default:
3331 break;
3332
3333 case BTRACE_FORMAT_BTS:
3334 printf_unfiltered (_("Number of packets: %u.\n"),
3335 VEC_length (btrace_block_s,
3336 btinfo->data.variant.bts.blocks));
3337 break;
3338
3339 #if defined (HAVE_LIBIPT)
3340 case BTRACE_FORMAT_PT:
3341 {
3342 struct pt_version version;
3343
3344 version = pt_library_version ();
3345 printf_unfiltered (_("Version: %u.%u.%u%s.\n"), version.major,
3346 version.minor, version.build,
3347 version.ext != NULL ? version.ext : "");
3348
3349 btrace_maint_update_pt_packets (btinfo);
3350 printf_unfiltered (_("Number of packets: %u.\n"),
3351 VEC_length (btrace_pt_packet_s,
3352 btinfo->maint.variant.pt.packets));
3353 }
3354 break;
3355 #endif /* defined (HAVE_LIBIPT) */
3356 }
3357 }
3358
3359 /* The "maint show btrace pt skip-pad" show value function. */
3360
3361 static void
3362 show_maint_btrace_pt_skip_pad (struct ui_file *file, int from_tty,
3363 struct cmd_list_element *c,
3364 const char *value)
3365 {
3366 fprintf_filtered (file, _("Skip PAD packets is %s.\n"), value);
3367 }
3368
3369
3370 /* Initialize btrace maintenance commands. */
3371
3372 void _initialize_btrace (void);
3373 void
3374 _initialize_btrace (void)
3375 {
3376 add_cmd ("btrace", class_maintenance, maint_info_btrace_cmd,
3377 _("Info about branch tracing data."), &maintenanceinfolist);
3378
3379 add_prefix_cmd ("btrace", class_maintenance, maint_btrace_cmd,
3380 _("Branch tracing maintenance commands."),
3381 &maint_btrace_cmdlist, "maintenance btrace ",
3382 0, &maintenancelist);
3383
3384 add_prefix_cmd ("btrace", class_maintenance, maint_btrace_set_cmd, _("\
3385 Set branch tracing specific variables."),
3386 &maint_btrace_set_cmdlist, "maintenance set btrace ",
3387 0, &maintenance_set_cmdlist);
3388
3389 add_prefix_cmd ("pt", class_maintenance, maint_btrace_pt_set_cmd, _("\
3390 Set Intel Processor Trace specific variables."),
3391 &maint_btrace_pt_set_cmdlist, "maintenance set btrace pt ",
3392 0, &maint_btrace_set_cmdlist);
3393
3394 add_prefix_cmd ("btrace", class_maintenance, maint_btrace_show_cmd, _("\
3395 Show branch tracing specific variables."),
3396 &maint_btrace_show_cmdlist, "maintenance show btrace ",
3397 0, &maintenance_show_cmdlist);
3398
3399 add_prefix_cmd ("pt", class_maintenance, maint_btrace_pt_show_cmd, _("\
3400 Show Intel Processor Trace specific variables."),
3401 &maint_btrace_pt_show_cmdlist, "maintenance show btrace pt ",
3402 0, &maint_btrace_show_cmdlist);
3403
3404 add_setshow_boolean_cmd ("skip-pad", class_maintenance,
3405 &maint_btrace_pt_skip_pad, _("\
3406 Set whether PAD packets should be skipped in the btrace packet history."), _("\
3407 Show whether PAD packets should be skipped in the btrace packet history."),_("\
3408 When enabled, PAD packets are ignored in the btrace packet history."),
3409 NULL, show_maint_btrace_pt_skip_pad,
3410 &maint_btrace_pt_set_cmdlist,
3411 &maint_btrace_pt_show_cmdlist);
3412
3413 add_cmd ("packet-history", class_maintenance, maint_btrace_packet_history_cmd,
3414 _("Print the raw branch tracing data.\n\
3415 With no argument, print ten more packets after the previous ten-line print.\n\
3416 With '-' as argument print ten packets before a previous ten-line print.\n\
3417 One argument specifies the starting packet of a ten-line print.\n\
3418 Two arguments with comma between specify starting and ending packets to \
3419 print.\n\
3420 Preceded with '+'/'-' the second argument specifies the distance from the \
3421 first.\n"),
3422 &maint_btrace_cmdlist);
3423
3424 add_cmd ("clear-packet-history", class_maintenance,
3425 maint_btrace_clear_packet_history_cmd,
3426 _("Clears the branch tracing packet history.\n\
3427 Discards the raw branch tracing data but not the execution history data.\n\
3428 "),
3429 &maint_btrace_cmdlist);
3430
3431 add_cmd ("clear", class_maintenance, maint_btrace_clear_cmd,
3432 _("Clears the branch tracing data.\n\
3433 Discards the raw branch tracing data and the execution history data.\n\
3434 The next 'record' command will fetch the branch tracing data anew.\n\
3435 "),
3436 &maint_btrace_cmdlist);
3437
3438 }
This page took 0.095278 seconds and 5 git commands to generate.