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