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