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