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