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