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