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