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