Automatic date update in version.in
[deliverable/binutils-gdb.git] / gdb / record-btrace.c
CommitLineData
afedecd3
MM
1/* Branch trace support for GDB, the GNU debugger.
2
3666a048 3 Copyright (C) 2013-2021 Free Software Foundation, Inc.
afedecd3
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
22#include "defs.h"
23#include "record.h"
c0272db5 24#include "record-btrace.h"
afedecd3
MM
25#include "gdbthread.h"
26#include "target.h"
27#include "gdbcmd.h"
28#include "disasm.h"
76727919 29#include "observable.h"
afedecd3
MM
30#include "cli/cli-utils.h"
31#include "source.h"
32#include "ui-out.h"
33#include "symtab.h"
34#include "filenames.h"
1f3ef581 35#include "regcache.h"
cecac1ab 36#include "frame-unwind.h"
0b722aec 37#include "hashtab.h"
45741a9c 38#include "infrun.h"
400b5eca 39#include "gdbsupport/event-loop.h"
70ad5bff 40#include "inf-loop.h"
00431a78 41#include "inferior.h"
325fac50 42#include <algorithm>
0d12e84c 43#include "gdbarch.h"
e43b10e1 44#include "cli/cli-style.h"
93b54c8e 45#include "async-event.h"
159ed7d9 46#include <forward_list>
afedecd3 47
d9f719f1
PA
48static const target_info record_btrace_target_info = {
49 "record-btrace",
50 N_("Branch tracing target"),
51 N_("Collect control-flow trace and provide the execution history.")
52};
53
afedecd3 54/* The target_ops of record-btrace. */
f6ac5f3d
PA
55
56class record_btrace_target final : public target_ops
57{
58public:
d9f719f1
PA
59 const target_info &info () const override
60 { return record_btrace_target_info; }
f6ac5f3d 61
66b4deae
PA
62 strata stratum () const override { return record_stratum; }
63
f6ac5f3d
PA
64 void close () override;
65 void async (int) override;
66
67 void detach (inferior *inf, int from_tty) override
68 { record_detach (this, inf, from_tty); }
69
70 void disconnect (const char *, int) override;
71
72 void mourn_inferior () override
73 { record_mourn_inferior (this); }
74
75 void kill () override
76 { record_kill (this); }
77
78 enum record_method record_method (ptid_t ptid) override;
79
80 void stop_recording () override;
81 void info_record () override;
82
83 void insn_history (int size, gdb_disassembly_flags flags) override;
84 void insn_history_from (ULONGEST from, int size,
85 gdb_disassembly_flags flags) override;
86 void insn_history_range (ULONGEST begin, ULONGEST end,
87 gdb_disassembly_flags flags) override;
88 void call_history (int size, record_print_flags flags) override;
89 void call_history_from (ULONGEST begin, int size, record_print_flags flags)
90 override;
91 void call_history_range (ULONGEST begin, ULONGEST end, record_print_flags flags)
92 override;
93
57810aa7
PA
94 bool record_is_replaying (ptid_t ptid) override;
95 bool record_will_replay (ptid_t ptid, int dir) override;
f6ac5f3d
PA
96 void record_stop_replaying () override;
97
98 enum target_xfer_status xfer_partial (enum target_object object,
99 const char *annex,
100 gdb_byte *readbuf,
101 const gdb_byte *writebuf,
102 ULONGEST offset, ULONGEST len,
103 ULONGEST *xfered_len) override;
104
105 int insert_breakpoint (struct gdbarch *,
106 struct bp_target_info *) override;
107 int remove_breakpoint (struct gdbarch *, struct bp_target_info *,
108 enum remove_bp_reason) override;
109
110 void fetch_registers (struct regcache *, int) override;
111
112 void store_registers (struct regcache *, int) override;
113 void prepare_to_store (struct regcache *) override;
114
115 const struct frame_unwind *get_unwinder () override;
116
117 const struct frame_unwind *get_tailcall_unwinder () override;
118
f6ac5f3d 119 void resume (ptid_t, int, enum gdb_signal) override;
b60cea74 120 ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
f6ac5f3d
PA
121
122 void stop (ptid_t) override;
123 void update_thread_list () override;
57810aa7 124 bool thread_alive (ptid_t ptid) override;
f6ac5f3d
PA
125 void goto_record_begin () override;
126 void goto_record_end () override;
127 void goto_record (ULONGEST insn) override;
128
57810aa7 129 bool can_execute_reverse () override;
f6ac5f3d 130
57810aa7
PA
131 bool stopped_by_sw_breakpoint () override;
132 bool supports_stopped_by_sw_breakpoint () override;
f6ac5f3d 133
57810aa7
PA
134 bool stopped_by_hw_breakpoint () override;
135 bool supports_stopped_by_hw_breakpoint () override;
f6ac5f3d
PA
136
137 enum exec_direction_kind execution_direction () override;
138 void prepare_to_generate_core () override;
139 void done_generating_core () override;
140};
141
142static record_btrace_target record_btrace_ops;
143
144/* Initialize the record-btrace target ops. */
afedecd3 145
76727919
TT
146/* Token associated with a new-thread observer enabling branch tracing
147 for the new thread. */
3dcfdc58 148static const gdb::observers::token record_btrace_thread_observer_token {};
afedecd3 149
67b5c0c1
MM
150/* Memory access types used in set/show record btrace replay-memory-access. */
151static const char replay_memory_access_read_only[] = "read-only";
152static const char replay_memory_access_read_write[] = "read-write";
153static const char *const replay_memory_access_types[] =
154{
155 replay_memory_access_read_only,
156 replay_memory_access_read_write,
157 NULL
158};
159
160/* The currently allowed replay memory access type. */
161static const char *replay_memory_access = replay_memory_access_read_only;
162
4a4495d6
MM
163/* The cpu state kinds. */
164enum record_btrace_cpu_state_kind
165{
166 CS_AUTO,
167 CS_NONE,
168 CS_CPU
169};
170
171/* The current cpu state. */
172static enum record_btrace_cpu_state_kind record_btrace_cpu_state = CS_AUTO;
173
174/* The current cpu for trace decode. */
175static struct btrace_cpu record_btrace_cpu;
176
67b5c0c1
MM
177/* Command lists for "set/show record btrace". */
178static struct cmd_list_element *set_record_btrace_cmdlist;
179static struct cmd_list_element *show_record_btrace_cmdlist;
633785ff 180
70ad5bff
MM
181/* The execution direction of the last resume we got. See record-full.c. */
182static enum exec_direction_kind record_btrace_resume_exec_dir = EXEC_FORWARD;
183
184/* The async event handler for reverse/replay execution. */
185static struct async_event_handler *record_btrace_async_inferior_event_handler;
186
aef92902
MM
187/* A flag indicating that we are currently generating a core file. */
188static int record_btrace_generating_corefile;
189
f4abbc16
MM
190/* The current branch trace configuration. */
191static struct btrace_config record_btrace_conf;
192
193/* Command list for "record btrace". */
194static struct cmd_list_element *record_btrace_cmdlist;
195
d33501a5
MM
196/* Command lists for "set/show record btrace bts". */
197static struct cmd_list_element *set_record_btrace_bts_cmdlist;
198static struct cmd_list_element *show_record_btrace_bts_cmdlist;
199
b20a6524
MM
200/* Command lists for "set/show record btrace pt". */
201static struct cmd_list_element *set_record_btrace_pt_cmdlist;
202static struct cmd_list_element *show_record_btrace_pt_cmdlist;
203
4a4495d6
MM
204/* Command list for "set record btrace cpu". */
205static struct cmd_list_element *set_record_btrace_cpu_cmdlist;
206
afedecd3
MM
207/* Print a record-btrace debug message. Use do ... while (0) to avoid
208 ambiguities when used in if statements. */
209
210#define DEBUG(msg, args...) \
211 do \
212 { \
213 if (record_debug != 0) \
dda83cd7 214 fprintf_unfiltered (gdb_stdlog, \
afedecd3
MM
215 "[record-btrace] " msg "\n", ##args); \
216 } \
217 while (0)
218
219
4a4495d6
MM
220/* Return the cpu configured by the user. Returns NULL if the cpu was
221 configured as auto. */
222const struct btrace_cpu *
223record_btrace_get_cpu (void)
224{
225 switch (record_btrace_cpu_state)
226 {
227 case CS_AUTO:
228 return nullptr;
229
230 case CS_NONE:
231 record_btrace_cpu.vendor = CV_UNKNOWN;
232 /* Fall through. */
233 case CS_CPU:
234 return &record_btrace_cpu;
235 }
236
237 error (_("Internal error: bad record btrace cpu state."));
238}
239
afedecd3 240/* Update the branch trace for the current thread and return a pointer to its
066ce621 241 thread_info.
afedecd3
MM
242
243 Throws an error if there is no thread or no trace. This function never
244 returns NULL. */
245
066ce621
MM
246static struct thread_info *
247require_btrace_thread (void)
afedecd3 248{
afedecd3
MM
249 DEBUG ("require");
250
00431a78 251 if (inferior_ptid == null_ptid)
afedecd3
MM
252 error (_("No thread."));
253
00431a78
PA
254 thread_info *tp = inferior_thread ();
255
cd4007e4
MM
256 validate_registers_access ();
257
4a4495d6 258 btrace_fetch (tp, record_btrace_get_cpu ());
afedecd3 259
6e07b1d2 260 if (btrace_is_empty (tp))
afedecd3
MM
261 error (_("No trace."));
262
066ce621
MM
263 return tp;
264}
265
266/* Update the branch trace for the current thread and return a pointer to its
267 branch trace information struct.
268
269 Throws an error if there is no thread or no trace. This function never
270 returns NULL. */
271
272static struct btrace_thread_info *
273require_btrace (void)
274{
275 struct thread_info *tp;
276
277 tp = require_btrace_thread ();
278
279 return &tp->btrace;
afedecd3
MM
280}
281
282/* Enable branch tracing for one thread. Warn on errors. */
283
284static void
285record_btrace_enable_warn (struct thread_info *tp)
286{
d89edf9b
MM
287 /* Ignore this thread if its inferior is not recorded by us. */
288 target_ops *rec = tp->inf->target_at (record_stratum);
289 if (rec != &record_btrace_ops)
290 return;
291
a70b8144 292 try
492d29ea
PA
293 {
294 btrace_enable (tp, &record_btrace_conf);
295 }
230d2906 296 catch (const gdb_exception_error &error)
492d29ea 297 {
3d6e9d23 298 warning ("%s", error.what ());
492d29ea 299 }
afedecd3
MM
300}
301
afedecd3
MM
302/* Enable automatic tracing of new threads. */
303
304static void
305record_btrace_auto_enable (void)
306{
307 DEBUG ("attach thread observer");
308
76727919 309 gdb::observers::new_thread.attach (record_btrace_enable_warn,
c90e7d63
SM
310 record_btrace_thread_observer_token,
311 "record-btrace");
afedecd3
MM
312}
313
314/* Disable automatic tracing of new threads. */
315
316static void
317record_btrace_auto_disable (void)
318{
afedecd3
MM
319 DEBUG ("detach thread observer");
320
76727919 321 gdb::observers::new_thread.detach (record_btrace_thread_observer_token);
afedecd3
MM
322}
323
70ad5bff
MM
324/* The record-btrace async event handler function. */
325
326static void
327record_btrace_handle_async_inferior_event (gdb_client_data data)
328{
b1a35af2 329 inferior_event_handler (INF_REG_EVENT);
70ad5bff
MM
330}
331
c0272db5
TW
332/* See record-btrace.h. */
333
334void
335record_btrace_push_target (void)
336{
337 const char *format;
338
339 record_btrace_auto_enable ();
340
02980c56 341 current_inferior ()->push_target (&record_btrace_ops);
c0272db5
TW
342
343 record_btrace_async_inferior_event_handler
344 = create_async_event_handler (record_btrace_handle_async_inferior_event,
db20ebdf 345 NULL, "record-btrace");
c0272db5
TW
346 record_btrace_generating_corefile = 0;
347
348 format = btrace_format_short_string (record_btrace_conf.format);
76727919 349 gdb::observers::record_changed.notify (current_inferior (), 1, "btrace", format);
c0272db5
TW
350}
351
228f1508
SM
352/* Disable btrace on a set of threads on scope exit. */
353
354struct scoped_btrace_disable
355{
356 scoped_btrace_disable () = default;
357
358 DISABLE_COPY_AND_ASSIGN (scoped_btrace_disable);
359
360 ~scoped_btrace_disable ()
361 {
362 for (thread_info *tp : m_threads)
363 btrace_disable (tp);
364 }
365
366 void add_thread (thread_info *thread)
367 {
368 m_threads.push_front (thread);
369 }
370
371 void discard ()
372 {
373 m_threads.clear ();
374 }
375
376private:
377 std::forward_list<thread_info *> m_threads;
378};
379
d9f719f1 380/* Open target record-btrace. */
afedecd3 381
d9f719f1
PA
382static void
383record_btrace_target_open (const char *args, int from_tty)
afedecd3 384{
228f1508
SM
385 /* If we fail to enable btrace for one thread, disable it for the threads for
386 which it was successfully enabled. */
387 scoped_btrace_disable btrace_disable;
afedecd3
MM
388
389 DEBUG ("open");
390
8213266a 391 record_preopen ();
afedecd3 392
55f6301a 393 if (!target_has_execution ())
afedecd3
MM
394 error (_("The program is not being run."));
395
d89edf9b 396 for (thread_info *tp : current_inferior ()->non_exited_threads ())
5d5658a1 397 if (args == NULL || *args == 0 || number_is_in_list (args, tp->global_num))
afedecd3 398 {
f4abbc16 399 btrace_enable (tp, &record_btrace_conf);
afedecd3 400
228f1508 401 btrace_disable.add_thread (tp);
afedecd3
MM
402 }
403
c0272db5 404 record_btrace_push_target ();
afedecd3 405
228f1508 406 btrace_disable.discard ();
afedecd3
MM
407}
408
f6ac5f3d 409/* The stop_recording method of target record-btrace. */
afedecd3 410
f6ac5f3d
PA
411void
412record_btrace_target::stop_recording ()
afedecd3 413{
afedecd3
MM
414 DEBUG ("stop recording");
415
416 record_btrace_auto_disable ();
417
d89edf9b 418 for (thread_info *tp : current_inferior ()->non_exited_threads ())
afedecd3
MM
419 if (tp->btrace.target != NULL)
420 btrace_disable (tp);
421}
422
f6ac5f3d 423/* The disconnect method of target record-btrace. */
c0272db5 424
f6ac5f3d
PA
425void
426record_btrace_target::disconnect (const char *args,
427 int from_tty)
c0272db5 428{
b6a8c27b 429 struct target_ops *beneath = this->beneath ();
c0272db5
TW
430
431 /* Do not stop recording, just clean up GDB side. */
fadf6add 432 current_inferior ()->unpush_target (this);
c0272db5
TW
433
434 /* Forward disconnect. */
f6ac5f3d 435 beneath->disconnect (args, from_tty);
c0272db5
TW
436}
437
f6ac5f3d 438/* The close method of target record-btrace. */
afedecd3 439
f6ac5f3d
PA
440void
441record_btrace_target::close ()
afedecd3 442{
70ad5bff
MM
443 if (record_btrace_async_inferior_event_handler != NULL)
444 delete_async_event_handler (&record_btrace_async_inferior_event_handler);
445
99c819ee
MM
446 /* Make sure automatic recording gets disabled even if we did not stop
447 recording before closing the record-btrace target. */
448 record_btrace_auto_disable ();
449
568e808b
MM
450 /* We should have already stopped recording.
451 Tear down btrace in case we have not. */
d89edf9b 452 for (thread_info *tp : current_inferior ()->non_exited_threads ())
568e808b 453 btrace_teardown (tp);
afedecd3
MM
454}
455
f6ac5f3d 456/* The async method of target record-btrace. */
b7d2e916 457
f6ac5f3d
PA
458void
459record_btrace_target::async (int enable)
b7d2e916 460{
6a3753b3 461 if (enable)
b7d2e916
PA
462 mark_async_event_handler (record_btrace_async_inferior_event_handler);
463 else
464 clear_async_event_handler (record_btrace_async_inferior_event_handler);
465
b6a8c27b 466 this->beneath ()->async (enable);
b7d2e916
PA
467}
468
d33501a5
MM
469/* Adjusts the size and returns a human readable size suffix. */
470
471static const char *
472record_btrace_adjust_size (unsigned int *size)
473{
474 unsigned int sz;
475
476 sz = *size;
477
478 if ((sz & ((1u << 30) - 1)) == 0)
479 {
480 *size = sz >> 30;
481 return "GB";
482 }
483 else if ((sz & ((1u << 20) - 1)) == 0)
484 {
485 *size = sz >> 20;
486 return "MB";
487 }
488 else if ((sz & ((1u << 10) - 1)) == 0)
489 {
490 *size = sz >> 10;
491 return "kB";
492 }
493 else
494 return "";
495}
496
497/* Print a BTS configuration. */
498
499static void
500record_btrace_print_bts_conf (const struct btrace_config_bts *conf)
501{
502 const char *suffix;
503 unsigned int size;
504
505 size = conf->size;
506 if (size > 0)
507 {
508 suffix = record_btrace_adjust_size (&size);
509 printf_unfiltered (_("Buffer size: %u%s.\n"), size, suffix);
510 }
511}
512
bc504a31 513/* Print an Intel Processor Trace configuration. */
b20a6524
MM
514
515static void
516record_btrace_print_pt_conf (const struct btrace_config_pt *conf)
517{
518 const char *suffix;
519 unsigned int size;
520
521 size = conf->size;
522 if (size > 0)
523 {
524 suffix = record_btrace_adjust_size (&size);
525 printf_unfiltered (_("Buffer size: %u%s.\n"), size, suffix);
526 }
527}
528
d33501a5
MM
529/* Print a branch tracing configuration. */
530
531static void
532record_btrace_print_conf (const struct btrace_config *conf)
533{
534 printf_unfiltered (_("Recording format: %s.\n"),
535 btrace_format_string (conf->format));
536
537 switch (conf->format)
538 {
539 case BTRACE_FORMAT_NONE:
540 return;
541
542 case BTRACE_FORMAT_BTS:
543 record_btrace_print_bts_conf (&conf->bts);
544 return;
b20a6524
MM
545
546 case BTRACE_FORMAT_PT:
547 record_btrace_print_pt_conf (&conf->pt);
548 return;
d33501a5
MM
549 }
550
40c94099 551 internal_error (__FILE__, __LINE__, _("Unknown branch trace format."));
d33501a5
MM
552}
553
f6ac5f3d 554/* The info_record method of target record-btrace. */
afedecd3 555
f6ac5f3d
PA
556void
557record_btrace_target::info_record ()
afedecd3
MM
558{
559 struct btrace_thread_info *btinfo;
f4abbc16 560 const struct btrace_config *conf;
afedecd3 561 struct thread_info *tp;
31fd9caa 562 unsigned int insns, calls, gaps;
afedecd3
MM
563
564 DEBUG ("info");
565
5b6d1e4f 566 if (inferior_ptid == null_ptid)
afedecd3
MM
567 error (_("No thread."));
568
5b6d1e4f
PA
569 tp = inferior_thread ();
570
cd4007e4
MM
571 validate_registers_access ();
572
f4abbc16
MM
573 btinfo = &tp->btrace;
574
f6ac5f3d 575 conf = ::btrace_conf (btinfo);
f4abbc16 576 if (conf != NULL)
d33501a5 577 record_btrace_print_conf (conf);
f4abbc16 578
4a4495d6 579 btrace_fetch (tp, record_btrace_get_cpu ());
afedecd3 580
23a7fe75
MM
581 insns = 0;
582 calls = 0;
31fd9caa 583 gaps = 0;
23a7fe75 584
6e07b1d2 585 if (!btrace_is_empty (tp))
23a7fe75
MM
586 {
587 struct btrace_call_iterator call;
588 struct btrace_insn_iterator insn;
589
590 btrace_call_end (&call, btinfo);
591 btrace_call_prev (&call, 1);
5de9129b 592 calls = btrace_call_number (&call);
23a7fe75
MM
593
594 btrace_insn_end (&insn, btinfo);
5de9129b 595 insns = btrace_insn_number (&insn);
31fd9caa 596
69090cee
TW
597 /* If the last instruction is not a gap, it is the current instruction
598 that is not actually part of the record. */
599 if (btrace_insn_get (&insn) != NULL)
600 insns -= 1;
31fd9caa
MM
601
602 gaps = btinfo->ngaps;
23a7fe75 603 }
afedecd3 604
31fd9caa 605 printf_unfiltered (_("Recorded %u instructions in %u functions (%u gaps) "
43792cf0 606 "for thread %s (%s).\n"), insns, calls, gaps,
a068643d
TT
607 print_thread_id (tp),
608 target_pid_to_str (tp->ptid).c_str ());
07bbe694
MM
609
610 if (btrace_is_replaying (tp))
611 printf_unfiltered (_("Replay in progress. At instruction %u.\n"),
612 btrace_insn_number (btinfo->replay));
afedecd3
MM
613}
614
31fd9caa
MM
615/* Print a decode error. */
616
617static void
618btrace_ui_out_decode_error (struct ui_out *uiout, int errcode,
619 enum btrace_format format)
620{
508352a9 621 const char *errstr = btrace_decode_error (format, errcode);
31fd9caa 622
112e8700 623 uiout->text (_("["));
508352a9
TW
624 /* ERRCODE > 0 indicates notifications on BTRACE_FORMAT_PT. */
625 if (!(format == BTRACE_FORMAT_PT && errcode > 0))
31fd9caa 626 {
112e8700 627 uiout->text (_("decode error ("));
381befee 628 uiout->field_signed ("errcode", errcode);
112e8700 629 uiout->text (_("): "));
31fd9caa 630 }
112e8700
SM
631 uiout->text (errstr);
632 uiout->text (_("]\n"));
31fd9caa
MM
633}
634
f94cc897
MM
635/* A range of source lines. */
636
637struct btrace_line_range
638{
639 /* The symtab this line is from. */
640 struct symtab *symtab;
641
642 /* The first line (inclusive). */
643 int begin;
644
645 /* The last line (exclusive). */
646 int end;
647};
648
649/* Construct a line range. */
650
651static struct btrace_line_range
652btrace_mk_line_range (struct symtab *symtab, int begin, int end)
653{
654 struct btrace_line_range range;
655
656 range.symtab = symtab;
657 range.begin = begin;
658 range.end = end;
659
660 return range;
661}
662
663/* Add a line to a line range. */
664
665static struct btrace_line_range
666btrace_line_range_add (struct btrace_line_range range, int line)
667{
668 if (range.end <= range.begin)
669 {
670 /* This is the first entry. */
671 range.begin = line;
672 range.end = line + 1;
673 }
674 else if (line < range.begin)
675 range.begin = line;
676 else if (range.end < line)
677 range.end = line;
678
679 return range;
680}
681
682/* Return non-zero if RANGE is empty, zero otherwise. */
683
684static int
685btrace_line_range_is_empty (struct btrace_line_range range)
686{
687 return range.end <= range.begin;
688}
689
690/* Return non-zero if LHS contains RHS, zero otherwise. */
691
692static int
693btrace_line_range_contains_range (struct btrace_line_range lhs,
694 struct btrace_line_range rhs)
695{
696 return ((lhs.symtab == rhs.symtab)
697 && (lhs.begin <= rhs.begin)
698 && (rhs.end <= lhs.end));
699}
700
701/* Find the line range associated with PC. */
702
703static struct btrace_line_range
704btrace_find_line_range (CORE_ADDR pc)
705{
706 struct btrace_line_range range;
707 struct linetable_entry *lines;
708 struct linetable *ltable;
709 struct symtab *symtab;
710 int nlines, i;
711
712 symtab = find_pc_line_symtab (pc);
713 if (symtab == NULL)
714 return btrace_mk_line_range (NULL, 0, 0);
715
716 ltable = SYMTAB_LINETABLE (symtab);
717 if (ltable == NULL)
718 return btrace_mk_line_range (symtab, 0, 0);
719
720 nlines = ltable->nitems;
721 lines = ltable->item;
722 if (nlines <= 0)
723 return btrace_mk_line_range (symtab, 0, 0);
724
725 range = btrace_mk_line_range (symtab, 0, 0);
726 for (i = 0; i < nlines - 1; i++)
727 {
8c95582d
AB
728 /* The test of is_stmt here was added when the is_stmt field was
729 introduced to the 'struct linetable_entry' structure. This
730 ensured that this loop maintained the same behaviour as before we
731 introduced is_stmt. That said, it might be that we would be
732 better off not checking is_stmt here, this would lead to us
733 possibly adding more line numbers to the range. At the time this
734 change was made I was unsure how to test this so chose to go with
735 maintaining the existing experience. */
736 if ((lines[i].pc == pc) && (lines[i].line != 0)
737 && (lines[i].is_stmt == 1))
f94cc897
MM
738 range = btrace_line_range_add (range, lines[i].line);
739 }
740
741 return range;
742}
743
744/* Print source lines in LINES to UIOUT.
745
746 UI_ITEM_CHAIN is a cleanup chain for the last source line and the
747 instructions corresponding to that source line. When printing a new source
748 line, we do the cleanups for the open chain and open a new cleanup chain for
749 the new source line. If the source line range in LINES is not empty, this
750 function will leave the cleanup chain for the last printed source line open
751 so instructions can be added to it. */
752
753static void
754btrace_print_lines (struct btrace_line_range lines, struct ui_out *uiout,
7ea78b59
SM
755 gdb::optional<ui_out_emit_tuple> *src_and_asm_tuple,
756 gdb::optional<ui_out_emit_list> *asm_list,
757 gdb_disassembly_flags flags)
f94cc897 758{
8d297bbf 759 print_source_lines_flags psl_flags;
f94cc897 760
f94cc897
MM
761 if (flags & DISASSEMBLY_FILENAME)
762 psl_flags |= PRINT_SOURCE_LINES_FILENAME;
763
7ea78b59 764 for (int line = lines.begin; line < lines.end; ++line)
f94cc897 765 {
7ea78b59 766 asm_list->reset ();
f94cc897 767
7ea78b59 768 src_and_asm_tuple->emplace (uiout, "src_and_asm_line");
f94cc897
MM
769
770 print_source_lines (lines.symtab, line, line + 1, psl_flags);
771
7ea78b59 772 asm_list->emplace (uiout, "line_asm_insn");
f94cc897
MM
773 }
774}
775
afedecd3
MM
776/* Disassemble a section of the recorded instruction trace. */
777
778static void
23a7fe75 779btrace_insn_history (struct ui_out *uiout,
31fd9caa 780 const struct btrace_thread_info *btinfo,
23a7fe75 781 const struct btrace_insn_iterator *begin,
9a24775b
PA
782 const struct btrace_insn_iterator *end,
783 gdb_disassembly_flags flags)
afedecd3 784{
9a24775b
PA
785 DEBUG ("itrace (0x%x): [%u; %u)", (unsigned) flags,
786 btrace_insn_number (begin), btrace_insn_number (end));
afedecd3 787
f94cc897
MM
788 flags |= DISASSEMBLY_SPECULATIVE;
789
7ea78b59
SM
790 struct gdbarch *gdbarch = target_gdbarch ();
791 btrace_line_range last_lines = btrace_mk_line_range (NULL, 0, 0);
f94cc897 792
7ea78b59 793 ui_out_emit_list list_emitter (uiout, "asm_insns");
f94cc897 794
7ea78b59
SM
795 gdb::optional<ui_out_emit_tuple> src_and_asm_tuple;
796 gdb::optional<ui_out_emit_list> asm_list;
afedecd3 797
046bebe1 798 gdb_pretty_print_disassembler disasm (gdbarch, uiout);
8b172ce7 799
7ea78b59 800 for (btrace_insn_iterator it = *begin; btrace_insn_cmp (&it, end) != 0;
dda83cd7 801 btrace_insn_next (&it, 1))
afedecd3 802 {
23a7fe75
MM
803 const struct btrace_insn *insn;
804
805 insn = btrace_insn_get (&it);
806
31fd9caa
MM
807 /* A NULL instruction indicates a gap in the trace. */
808 if (insn == NULL)
809 {
810 const struct btrace_config *conf;
811
812 conf = btrace_conf (btinfo);
afedecd3 813
31fd9caa
MM
814 /* We have trace so we must have a configuration. */
815 gdb_assert (conf != NULL);
816
69090cee
TW
817 uiout->field_fmt ("insn-number", "%u",
818 btrace_insn_number (&it));
819 uiout->text ("\t");
820
821 btrace_ui_out_decode_error (uiout, btrace_insn_get_error (&it),
31fd9caa
MM
822 conf->format);
823 }
824 else
825 {
f94cc897 826 struct disasm_insn dinsn;
da8c46d2 827
f94cc897 828 if ((flags & DISASSEMBLY_SOURCE) != 0)
da8c46d2 829 {
f94cc897
MM
830 struct btrace_line_range lines;
831
832 lines = btrace_find_line_range (insn->pc);
833 if (!btrace_line_range_is_empty (lines)
834 && !btrace_line_range_contains_range (last_lines, lines))
835 {
7ea78b59
SM
836 btrace_print_lines (lines, uiout, &src_and_asm_tuple, &asm_list,
837 flags);
f94cc897
MM
838 last_lines = lines;
839 }
7ea78b59 840 else if (!src_and_asm_tuple.has_value ())
f94cc897 841 {
7ea78b59
SM
842 gdb_assert (!asm_list.has_value ());
843
844 src_and_asm_tuple.emplace (uiout, "src_and_asm_line");
845
f94cc897 846 /* No source information. */
7ea78b59 847 asm_list.emplace (uiout, "line_asm_insn");
f94cc897
MM
848 }
849
7ea78b59
SM
850 gdb_assert (src_and_asm_tuple.has_value ());
851 gdb_assert (asm_list.has_value ());
da8c46d2 852 }
da8c46d2 853
f94cc897
MM
854 memset (&dinsn, 0, sizeof (dinsn));
855 dinsn.number = btrace_insn_number (&it);
856 dinsn.addr = insn->pc;
31fd9caa 857
da8c46d2 858 if ((insn->flags & BTRACE_INSN_FLAG_SPECULATIVE) != 0)
f94cc897 859 dinsn.is_speculative = 1;
da8c46d2 860
046bebe1 861 disasm.pretty_print_insn (&dinsn, flags);
31fd9caa 862 }
afedecd3
MM
863 }
864}
865
f6ac5f3d 866/* The insn_history method of target record-btrace. */
afedecd3 867
f6ac5f3d
PA
868void
869record_btrace_target::insn_history (int size, gdb_disassembly_flags flags)
afedecd3
MM
870{
871 struct btrace_thread_info *btinfo;
23a7fe75
MM
872 struct btrace_insn_history *history;
873 struct btrace_insn_iterator begin, end;
afedecd3 874 struct ui_out *uiout;
23a7fe75 875 unsigned int context, covered;
afedecd3
MM
876
877 uiout = current_uiout;
2e783024 878 ui_out_emit_tuple tuple_emitter (uiout, "insn history");
afedecd3 879 context = abs (size);
afedecd3
MM
880 if (context == 0)
881 error (_("Bad record instruction-history-size."));
882
23a7fe75
MM
883 btinfo = require_btrace ();
884 history = btinfo->insn_history;
885 if (history == NULL)
afedecd3 886 {
07bbe694 887 struct btrace_insn_iterator *replay;
afedecd3 888
9a24775b 889 DEBUG ("insn-history (0x%x): %d", (unsigned) flags, size);
afedecd3 890
07bbe694
MM
891 /* If we're replaying, we start at the replay position. Otherwise, we
892 start at the tail of the trace. */
893 replay = btinfo->replay;
894 if (replay != NULL)
895 begin = *replay;
896 else
897 btrace_insn_end (&begin, btinfo);
898
899 /* We start from here and expand in the requested direction. Then we
900 expand in the other direction, as well, to fill up any remaining
901 context. */
902 end = begin;
903 if (size < 0)
904 {
905 /* We want the current position covered, as well. */
906 covered = btrace_insn_next (&end, 1);
907 covered += btrace_insn_prev (&begin, context - covered);
908 covered += btrace_insn_next (&end, context - covered);
909 }
910 else
911 {
912 covered = btrace_insn_next (&end, context);
913 covered += btrace_insn_prev (&begin, context - covered);
914 }
afedecd3
MM
915 }
916 else
917 {
23a7fe75
MM
918 begin = history->begin;
919 end = history->end;
afedecd3 920
9a24775b 921 DEBUG ("insn-history (0x%x): %d, prev: [%u; %u)", (unsigned) flags, size,
23a7fe75 922 btrace_insn_number (&begin), btrace_insn_number (&end));
afedecd3 923
23a7fe75
MM
924 if (size < 0)
925 {
926 end = begin;
927 covered = btrace_insn_prev (&begin, context);
928 }
929 else
930 {
931 begin = end;
932 covered = btrace_insn_next (&end, context);
933 }
afedecd3
MM
934 }
935
23a7fe75 936 if (covered > 0)
31fd9caa 937 btrace_insn_history (uiout, btinfo, &begin, &end, flags);
23a7fe75
MM
938 else
939 {
940 if (size < 0)
941 printf_unfiltered (_("At the start of the branch trace record.\n"));
942 else
943 printf_unfiltered (_("At the end of the branch trace record.\n"));
944 }
afedecd3 945
23a7fe75 946 btrace_set_insn_history (btinfo, &begin, &end);
afedecd3
MM
947}
948
f6ac5f3d 949/* The insn_history_range method of target record-btrace. */
afedecd3 950
f6ac5f3d
PA
951void
952record_btrace_target::insn_history_range (ULONGEST from, ULONGEST to,
953 gdb_disassembly_flags flags)
afedecd3
MM
954{
955 struct btrace_thread_info *btinfo;
23a7fe75 956 struct btrace_insn_iterator begin, end;
afedecd3 957 struct ui_out *uiout;
23a7fe75
MM
958 unsigned int low, high;
959 int found;
afedecd3
MM
960
961 uiout = current_uiout;
2e783024 962 ui_out_emit_tuple tuple_emitter (uiout, "insn history");
23a7fe75
MM
963 low = from;
964 high = to;
afedecd3 965
9a24775b 966 DEBUG ("insn-history (0x%x): [%u; %u)", (unsigned) flags, low, high);
afedecd3
MM
967
968 /* Check for wrap-arounds. */
23a7fe75 969 if (low != from || high != to)
afedecd3
MM
970 error (_("Bad range."));
971
0688d04e 972 if (high < low)
afedecd3
MM
973 error (_("Bad range."));
974
23a7fe75 975 btinfo = require_btrace ();
afedecd3 976
23a7fe75
MM
977 found = btrace_find_insn_by_number (&begin, btinfo, low);
978 if (found == 0)
979 error (_("Range out of bounds."));
afedecd3 980
23a7fe75
MM
981 found = btrace_find_insn_by_number (&end, btinfo, high);
982 if (found == 0)
0688d04e
MM
983 {
984 /* Silently truncate the range. */
985 btrace_insn_end (&end, btinfo);
986 }
987 else
988 {
989 /* We want both begin and end to be inclusive. */
990 btrace_insn_next (&end, 1);
991 }
afedecd3 992
31fd9caa 993 btrace_insn_history (uiout, btinfo, &begin, &end, flags);
23a7fe75 994 btrace_set_insn_history (btinfo, &begin, &end);
afedecd3
MM
995}
996
f6ac5f3d 997/* The insn_history_from method of target record-btrace. */
afedecd3 998
f6ac5f3d
PA
999void
1000record_btrace_target::insn_history_from (ULONGEST from, int size,
1001 gdb_disassembly_flags flags)
afedecd3
MM
1002{
1003 ULONGEST begin, end, context;
1004
1005 context = abs (size);
0688d04e
MM
1006 if (context == 0)
1007 error (_("Bad record instruction-history-size."));
afedecd3
MM
1008
1009 if (size < 0)
1010 {
1011 end = from;
1012
1013 if (from < context)
1014 begin = 0;
1015 else
0688d04e 1016 begin = from - context + 1;
afedecd3
MM
1017 }
1018 else
1019 {
1020 begin = from;
0688d04e 1021 end = from + context - 1;
afedecd3
MM
1022
1023 /* Check for wrap-around. */
1024 if (end < begin)
1025 end = ULONGEST_MAX;
1026 }
1027
f6ac5f3d 1028 insn_history_range (begin, end, flags);
afedecd3
MM
1029}
1030
1031/* Print the instruction number range for a function call history line. */
1032
1033static void
23a7fe75
MM
1034btrace_call_history_insn_range (struct ui_out *uiout,
1035 const struct btrace_function *bfun)
afedecd3 1036{
7acbe133
MM
1037 unsigned int begin, end, size;
1038
0860c437 1039 size = bfun->insn.size ();
7acbe133 1040 gdb_assert (size > 0);
afedecd3 1041
23a7fe75 1042 begin = bfun->insn_offset;
7acbe133 1043 end = begin + size - 1;
afedecd3 1044
1f77b012 1045 uiout->field_unsigned ("insn begin", begin);
112e8700 1046 uiout->text (",");
1f77b012 1047 uiout->field_unsigned ("insn end", end);
afedecd3
MM
1048}
1049
ce0dfbea
MM
1050/* Compute the lowest and highest source line for the instructions in BFUN
1051 and return them in PBEGIN and PEND.
1052 Ignore instructions that can't be mapped to BFUN, e.g. instructions that
1053 result from inlining or macro expansion. */
1054
1055static void
1056btrace_compute_src_line_range (const struct btrace_function *bfun,
1057 int *pbegin, int *pend)
1058{
ce0dfbea
MM
1059 struct symtab *symtab;
1060 struct symbol *sym;
ce0dfbea
MM
1061 int begin, end;
1062
1063 begin = INT_MAX;
1064 end = INT_MIN;
1065
1066 sym = bfun->sym;
1067 if (sym == NULL)
1068 goto out;
1069
1070 symtab = symbol_symtab (sym);
1071
0860c437 1072 for (const btrace_insn &insn : bfun->insn)
ce0dfbea
MM
1073 {
1074 struct symtab_and_line sal;
1075
0860c437 1076 sal = find_pc_line (insn.pc, 0);
ce0dfbea
MM
1077 if (sal.symtab != symtab || sal.line == 0)
1078 continue;
1079
325fac50
PA
1080 begin = std::min (begin, sal.line);
1081 end = std::max (end, sal.line);
ce0dfbea
MM
1082 }
1083
1084 out:
1085 *pbegin = begin;
1086 *pend = end;
1087}
1088
afedecd3
MM
1089/* Print the source line information for a function call history line. */
1090
1091static void
23a7fe75
MM
1092btrace_call_history_src_line (struct ui_out *uiout,
1093 const struct btrace_function *bfun)
afedecd3
MM
1094{
1095 struct symbol *sym;
23a7fe75 1096 int begin, end;
afedecd3
MM
1097
1098 sym = bfun->sym;
1099 if (sym == NULL)
1100 return;
1101
112e8700 1102 uiout->field_string ("file",
cbe56571 1103 symtab_to_filename_for_display (symbol_symtab (sym)),
e43b10e1 1104 file_name_style.style ());
afedecd3 1105
ce0dfbea 1106 btrace_compute_src_line_range (bfun, &begin, &end);
23a7fe75 1107 if (end < begin)
afedecd3
MM
1108 return;
1109
112e8700 1110 uiout->text (":");
381befee 1111 uiout->field_signed ("min line", begin);
afedecd3 1112
23a7fe75 1113 if (end == begin)
afedecd3
MM
1114 return;
1115
112e8700 1116 uiout->text (",");
381befee 1117 uiout->field_signed ("max line", end);
afedecd3
MM
1118}
1119
0b722aec
MM
1120/* Get the name of a branch trace function. */
1121
1122static const char *
1123btrace_get_bfun_name (const struct btrace_function *bfun)
1124{
1125 struct minimal_symbol *msym;
1126 struct symbol *sym;
1127
1128 if (bfun == NULL)
1129 return "??";
1130
1131 msym = bfun->msym;
1132 sym = bfun->sym;
1133
1134 if (sym != NULL)
987012b8 1135 return sym->print_name ();
0b722aec 1136 else if (msym != NULL)
c9d95fa3 1137 return msym->print_name ();
0b722aec
MM
1138 else
1139 return "??";
1140}
1141
afedecd3
MM
1142/* Disassemble a section of the recorded function trace. */
1143
1144static void
23a7fe75 1145btrace_call_history (struct ui_out *uiout,
8710b709 1146 const struct btrace_thread_info *btinfo,
23a7fe75
MM
1147 const struct btrace_call_iterator *begin,
1148 const struct btrace_call_iterator *end,
8d297bbf 1149 int int_flags)
afedecd3 1150{
23a7fe75 1151 struct btrace_call_iterator it;
8d297bbf 1152 record_print_flags flags = (enum record_print_flag) int_flags;
afedecd3 1153
8d297bbf 1154 DEBUG ("ftrace (0x%x): [%u; %u)", int_flags, btrace_call_number (begin),
23a7fe75 1155 btrace_call_number (end));
afedecd3 1156
23a7fe75 1157 for (it = *begin; btrace_call_cmp (&it, end) < 0; btrace_call_next (&it, 1))
afedecd3 1158 {
23a7fe75
MM
1159 const struct btrace_function *bfun;
1160 struct minimal_symbol *msym;
1161 struct symbol *sym;
1162
1163 bfun = btrace_call_get (&it);
23a7fe75 1164 sym = bfun->sym;
0b722aec 1165 msym = bfun->msym;
23a7fe75 1166
afedecd3 1167 /* Print the function index. */
1f77b012 1168 uiout->field_unsigned ("index", bfun->number);
112e8700 1169 uiout->text ("\t");
afedecd3 1170
31fd9caa
MM
1171 /* Indicate gaps in the trace. */
1172 if (bfun->errcode != 0)
1173 {
1174 const struct btrace_config *conf;
1175
1176 conf = btrace_conf (btinfo);
1177
1178 /* We have trace so we must have a configuration. */
1179 gdb_assert (conf != NULL);
1180
1181 btrace_ui_out_decode_error (uiout, bfun->errcode, conf->format);
1182
1183 continue;
1184 }
1185
8710b709
MM
1186 if ((flags & RECORD_PRINT_INDENT_CALLS) != 0)
1187 {
1188 int level = bfun->level + btinfo->level, i;
1189
1190 for (i = 0; i < level; ++i)
112e8700 1191 uiout->text (" ");
8710b709
MM
1192 }
1193
1194 if (sym != NULL)
987012b8 1195 uiout->field_string ("function", sym->print_name (),
e43b10e1 1196 function_name_style.style ());
8710b709 1197 else if (msym != NULL)
c9d95fa3 1198 uiout->field_string ("function", msym->print_name (),
e43b10e1 1199 function_name_style.style ());
112e8700 1200 else if (!uiout->is_mi_like_p ())
cbe56571 1201 uiout->field_string ("function", "??",
e43b10e1 1202 function_name_style.style ());
8710b709 1203
1e038f67 1204 if ((flags & RECORD_PRINT_INSN_RANGE) != 0)
afedecd3 1205 {
112e8700 1206 uiout->text (_("\tinst "));
23a7fe75 1207 btrace_call_history_insn_range (uiout, bfun);
afedecd3
MM
1208 }
1209
1e038f67 1210 if ((flags & RECORD_PRINT_SRC_LINE) != 0)
afedecd3 1211 {
112e8700 1212 uiout->text (_("\tat "));
23a7fe75 1213 btrace_call_history_src_line (uiout, bfun);
afedecd3
MM
1214 }
1215
112e8700 1216 uiout->text ("\n");
afedecd3
MM
1217 }
1218}
1219
f6ac5f3d 1220/* The call_history method of target record-btrace. */
afedecd3 1221
f6ac5f3d
PA
1222void
1223record_btrace_target::call_history (int size, record_print_flags flags)
afedecd3
MM
1224{
1225 struct btrace_thread_info *btinfo;
23a7fe75
MM
1226 struct btrace_call_history *history;
1227 struct btrace_call_iterator begin, end;
afedecd3 1228 struct ui_out *uiout;
23a7fe75 1229 unsigned int context, covered;
afedecd3
MM
1230
1231 uiout = current_uiout;
2e783024 1232 ui_out_emit_tuple tuple_emitter (uiout, "insn history");
afedecd3 1233 context = abs (size);
afedecd3
MM
1234 if (context == 0)
1235 error (_("Bad record function-call-history-size."));
1236
23a7fe75
MM
1237 btinfo = require_btrace ();
1238 history = btinfo->call_history;
1239 if (history == NULL)
afedecd3 1240 {
07bbe694 1241 struct btrace_insn_iterator *replay;
afedecd3 1242
0cb7c7b0 1243 DEBUG ("call-history (0x%x): %d", (int) flags, size);
afedecd3 1244
07bbe694
MM
1245 /* If we're replaying, we start at the replay position. Otherwise, we
1246 start at the tail of the trace. */
1247 replay = btinfo->replay;
1248 if (replay != NULL)
1249 {
07bbe694 1250 begin.btinfo = btinfo;
a0f1b963 1251 begin.index = replay->call_index;
07bbe694
MM
1252 }
1253 else
1254 btrace_call_end (&begin, btinfo);
1255
1256 /* We start from here and expand in the requested direction. Then we
1257 expand in the other direction, as well, to fill up any remaining
1258 context. */
1259 end = begin;
1260 if (size < 0)
1261 {
1262 /* We want the current position covered, as well. */
1263 covered = btrace_call_next (&end, 1);
1264 covered += btrace_call_prev (&begin, context - covered);
1265 covered += btrace_call_next (&end, context - covered);
1266 }
1267 else
1268 {
1269 covered = btrace_call_next (&end, context);
1270 covered += btrace_call_prev (&begin, context- covered);
1271 }
afedecd3
MM
1272 }
1273 else
1274 {
23a7fe75
MM
1275 begin = history->begin;
1276 end = history->end;
afedecd3 1277
0cb7c7b0 1278 DEBUG ("call-history (0x%x): %d, prev: [%u; %u)", (int) flags, size,
23a7fe75 1279 btrace_call_number (&begin), btrace_call_number (&end));
afedecd3 1280
23a7fe75
MM
1281 if (size < 0)
1282 {
1283 end = begin;
1284 covered = btrace_call_prev (&begin, context);
1285 }
1286 else
1287 {
1288 begin = end;
1289 covered = btrace_call_next (&end, context);
1290 }
afedecd3
MM
1291 }
1292
23a7fe75 1293 if (covered > 0)
8710b709 1294 btrace_call_history (uiout, btinfo, &begin, &end, flags);
23a7fe75
MM
1295 else
1296 {
1297 if (size < 0)
1298 printf_unfiltered (_("At the start of the branch trace record.\n"));
1299 else
1300 printf_unfiltered (_("At the end of the branch trace record.\n"));
1301 }
afedecd3 1302
23a7fe75 1303 btrace_set_call_history (btinfo, &begin, &end);
afedecd3
MM
1304}
1305
f6ac5f3d 1306/* The call_history_range method of target record-btrace. */
afedecd3 1307
f6ac5f3d
PA
1308void
1309record_btrace_target::call_history_range (ULONGEST from, ULONGEST to,
1310 record_print_flags flags)
afedecd3
MM
1311{
1312 struct btrace_thread_info *btinfo;
23a7fe75 1313 struct btrace_call_iterator begin, end;
afedecd3 1314 struct ui_out *uiout;
23a7fe75
MM
1315 unsigned int low, high;
1316 int found;
afedecd3
MM
1317
1318 uiout = current_uiout;
2e783024 1319 ui_out_emit_tuple tuple_emitter (uiout, "func history");
23a7fe75
MM
1320 low = from;
1321 high = to;
afedecd3 1322
0cb7c7b0 1323 DEBUG ("call-history (0x%x): [%u; %u)", (int) flags, low, high);
afedecd3
MM
1324
1325 /* Check for wrap-arounds. */
23a7fe75 1326 if (low != from || high != to)
afedecd3
MM
1327 error (_("Bad range."));
1328
0688d04e 1329 if (high < low)
afedecd3
MM
1330 error (_("Bad range."));
1331
23a7fe75 1332 btinfo = require_btrace ();
afedecd3 1333
23a7fe75
MM
1334 found = btrace_find_call_by_number (&begin, btinfo, low);
1335 if (found == 0)
1336 error (_("Range out of bounds."));
afedecd3 1337
23a7fe75
MM
1338 found = btrace_find_call_by_number (&end, btinfo, high);
1339 if (found == 0)
0688d04e
MM
1340 {
1341 /* Silently truncate the range. */
1342 btrace_call_end (&end, btinfo);
1343 }
1344 else
1345 {
1346 /* We want both begin and end to be inclusive. */
1347 btrace_call_next (&end, 1);
1348 }
afedecd3 1349
8710b709 1350 btrace_call_history (uiout, btinfo, &begin, &end, flags);
23a7fe75 1351 btrace_set_call_history (btinfo, &begin, &end);
afedecd3
MM
1352}
1353
f6ac5f3d 1354/* The call_history_from method of target record-btrace. */
afedecd3 1355
f6ac5f3d
PA
1356void
1357record_btrace_target::call_history_from (ULONGEST from, int size,
1358 record_print_flags flags)
afedecd3
MM
1359{
1360 ULONGEST begin, end, context;
1361
1362 context = abs (size);
0688d04e
MM
1363 if (context == 0)
1364 error (_("Bad record function-call-history-size."));
afedecd3
MM
1365
1366 if (size < 0)
1367 {
1368 end = from;
1369
1370 if (from < context)
1371 begin = 0;
1372 else
0688d04e 1373 begin = from - context + 1;
afedecd3
MM
1374 }
1375 else
1376 {
1377 begin = from;
0688d04e 1378 end = from + context - 1;
afedecd3
MM
1379
1380 /* Check for wrap-around. */
1381 if (end < begin)
1382 end = ULONGEST_MAX;
1383 }
1384
f6ac5f3d 1385 call_history_range ( begin, end, flags);
afedecd3
MM
1386}
1387
f6ac5f3d 1388/* The record_method method of target record-btrace. */
b158a20f 1389
f6ac5f3d
PA
1390enum record_method
1391record_btrace_target::record_method (ptid_t ptid)
b158a20f 1392{
5b6d1e4f
PA
1393 process_stratum_target *proc_target = current_inferior ()->process_target ();
1394 thread_info *const tp = find_thread_ptid (proc_target, ptid);
b158a20f
TW
1395
1396 if (tp == NULL)
1397 error (_("No thread."));
1398
1399 if (tp->btrace.target == NULL)
1400 return RECORD_METHOD_NONE;
1401
1402 return RECORD_METHOD_BTRACE;
1403}
1404
f6ac5f3d 1405/* The record_is_replaying method of target record-btrace. */
07bbe694 1406
57810aa7 1407bool
f6ac5f3d 1408record_btrace_target::record_is_replaying (ptid_t ptid)
07bbe694 1409{
5b6d1e4f
PA
1410 process_stratum_target *proc_target = current_inferior ()->process_target ();
1411 for (thread_info *tp : all_non_exited_threads (proc_target, ptid))
08036331 1412 if (btrace_is_replaying (tp))
57810aa7 1413 return true;
07bbe694 1414
57810aa7 1415 return false;
07bbe694
MM
1416}
1417
f6ac5f3d 1418/* The record_will_replay method of target record-btrace. */
7ff27e9b 1419
57810aa7 1420bool
f6ac5f3d 1421record_btrace_target::record_will_replay (ptid_t ptid, int dir)
7ff27e9b 1422{
f6ac5f3d 1423 return dir == EXEC_REVERSE || record_is_replaying (ptid);
7ff27e9b
MM
1424}
1425
f6ac5f3d 1426/* The xfer_partial method of target record-btrace. */
633785ff 1427
f6ac5f3d
PA
1428enum target_xfer_status
1429record_btrace_target::xfer_partial (enum target_object object,
1430 const char *annex, gdb_byte *readbuf,
1431 const gdb_byte *writebuf, ULONGEST offset,
1432 ULONGEST len, ULONGEST *xfered_len)
633785ff 1433{
633785ff 1434 /* Filter out requests that don't make sense during replay. */
67b5c0c1 1435 if (replay_memory_access == replay_memory_access_read_only
aef92902 1436 && !record_btrace_generating_corefile
f6ac5f3d 1437 && record_is_replaying (inferior_ptid))
633785ff
MM
1438 {
1439 switch (object)
1440 {
1441 case TARGET_OBJECT_MEMORY:
1442 {
19cf757a 1443 const struct target_section *section;
633785ff
MM
1444
1445 /* We do not allow writing memory in general. */
1446 if (writebuf != NULL)
9b409511
YQ
1447 {
1448 *xfered_len = len;
bc113b4e 1449 return TARGET_XFER_UNAVAILABLE;
9b409511 1450 }
633785ff
MM
1451
1452 /* We allow reading readonly memory. */
f6ac5f3d 1453 section = target_section_by_addr (this, offset);
633785ff
MM
1454 if (section != NULL)
1455 {
1456 /* Check if the section we found is readonly. */
fd361982 1457 if ((bfd_section_flags (section->the_bfd_section)
633785ff
MM
1458 & SEC_READONLY) != 0)
1459 {
1460 /* Truncate the request to fit into this section. */
325fac50 1461 len = std::min (len, section->endaddr - offset);
633785ff
MM
1462 break;
1463 }
1464 }
1465
9b409511 1466 *xfered_len = len;
bc113b4e 1467 return TARGET_XFER_UNAVAILABLE;
633785ff
MM
1468 }
1469 }
1470 }
1471
1472 /* Forward the request. */
b6a8c27b
PA
1473 return this->beneath ()->xfer_partial (object, annex, readbuf, writebuf,
1474 offset, len, xfered_len);
633785ff
MM
1475}
1476
f6ac5f3d 1477/* The insert_breakpoint method of target record-btrace. */
633785ff 1478
f6ac5f3d
PA
1479int
1480record_btrace_target::insert_breakpoint (struct gdbarch *gdbarch,
1481 struct bp_target_info *bp_tgt)
633785ff 1482{
67b5c0c1
MM
1483 const char *old;
1484 int ret;
633785ff
MM
1485
1486 /* Inserting breakpoints requires accessing memory. Allow it for the
1487 duration of this function. */
67b5c0c1
MM
1488 old = replay_memory_access;
1489 replay_memory_access = replay_memory_access_read_write;
633785ff
MM
1490
1491 ret = 0;
a70b8144 1492 try
492d29ea 1493 {
b6a8c27b 1494 ret = this->beneath ()->insert_breakpoint (gdbarch, bp_tgt);
492d29ea 1495 }
230d2906 1496 catch (const gdb_exception &except)
492d29ea 1497 {
6c63c96a 1498 replay_memory_access = old;
eedc3f4f 1499 throw;
492d29ea 1500 }
6c63c96a 1501 replay_memory_access = old;
633785ff
MM
1502
1503 return ret;
1504}
1505
f6ac5f3d 1506/* The remove_breakpoint method of target record-btrace. */
633785ff 1507
f6ac5f3d
PA
1508int
1509record_btrace_target::remove_breakpoint (struct gdbarch *gdbarch,
1510 struct bp_target_info *bp_tgt,
1511 enum remove_bp_reason reason)
633785ff 1512{
67b5c0c1
MM
1513 const char *old;
1514 int ret;
633785ff
MM
1515
1516 /* Removing breakpoints requires accessing memory. Allow it for the
1517 duration of this function. */
67b5c0c1
MM
1518 old = replay_memory_access;
1519 replay_memory_access = replay_memory_access_read_write;
633785ff
MM
1520
1521 ret = 0;
a70b8144 1522 try
492d29ea 1523 {
b6a8c27b 1524 ret = this->beneath ()->remove_breakpoint (gdbarch, bp_tgt, reason);
492d29ea 1525 }
230d2906 1526 catch (const gdb_exception &except)
492d29ea 1527 {
6c63c96a 1528 replay_memory_access = old;
eedc3f4f 1529 throw;
492d29ea 1530 }
6c63c96a 1531 replay_memory_access = old;
633785ff
MM
1532
1533 return ret;
1534}
1535
f6ac5f3d 1536/* The fetch_registers method of target record-btrace. */
1f3ef581 1537
f6ac5f3d
PA
1538void
1539record_btrace_target::fetch_registers (struct regcache *regcache, int regno)
1f3ef581 1540{
1a476b6d
MM
1541 btrace_insn_iterator *replay = nullptr;
1542
1543 /* Thread-db may ask for a thread's registers before GDB knows about the
1544 thread. We forward the request to the target beneath in this
1545 case. */
5b6d1e4f 1546 thread_info *tp = find_thread_ptid (regcache->target (), regcache->ptid ());
1a476b6d
MM
1547 if (tp != nullptr)
1548 replay = tp->btrace.replay;
1f3ef581 1549
1a476b6d 1550 if (replay != nullptr && !record_btrace_generating_corefile)
1f3ef581
MM
1551 {
1552 const struct btrace_insn *insn;
1553 struct gdbarch *gdbarch;
1554 int pcreg;
1555
ac7936df 1556 gdbarch = regcache->arch ();
1f3ef581
MM
1557 pcreg = gdbarch_pc_regnum (gdbarch);
1558 if (pcreg < 0)
1559 return;
1560
1561 /* We can only provide the PC register. */
1562 if (regno >= 0 && regno != pcreg)
1563 return;
1564
1565 insn = btrace_insn_get (replay);
1566 gdb_assert (insn != NULL);
1567
73e1c03f 1568 regcache->raw_supply (regno, &insn->pc);
1f3ef581
MM
1569 }
1570 else
b6a8c27b 1571 this->beneath ()->fetch_registers (regcache, regno);
1f3ef581
MM
1572}
1573
f6ac5f3d 1574/* The store_registers method of target record-btrace. */
1f3ef581 1575
f6ac5f3d
PA
1576void
1577record_btrace_target::store_registers (struct regcache *regcache, int regno)
1f3ef581 1578{
a52eab48 1579 if (!record_btrace_generating_corefile
222312d3 1580 && record_is_replaying (regcache->ptid ()))
4d10e986 1581 error (_("Cannot write registers while replaying."));
1f3ef581 1582
491144b5 1583 gdb_assert (may_write_registers);
1f3ef581 1584
b6a8c27b 1585 this->beneath ()->store_registers (regcache, regno);
1f3ef581
MM
1586}
1587
f6ac5f3d 1588/* The prepare_to_store method of target record-btrace. */
1f3ef581 1589
f6ac5f3d
PA
1590void
1591record_btrace_target::prepare_to_store (struct regcache *regcache)
1f3ef581 1592{
a52eab48 1593 if (!record_btrace_generating_corefile
222312d3 1594 && record_is_replaying (regcache->ptid ()))
1f3ef581
MM
1595 return;
1596
b6a8c27b 1597 this->beneath ()->prepare_to_store (regcache);
1f3ef581
MM
1598}
1599
0b722aec
MM
1600/* The branch trace frame cache. */
1601
1602struct btrace_frame_cache
1603{
1604 /* The thread. */
1605 struct thread_info *tp;
1606
1607 /* The frame info. */
1608 struct frame_info *frame;
1609
1610 /* The branch trace function segment. */
1611 const struct btrace_function *bfun;
1612};
1613
1614/* A struct btrace_frame_cache hash table indexed by NEXT. */
1615
1616static htab_t bfcache;
1617
1618/* hash_f for htab_create_alloc of bfcache. */
1619
1620static hashval_t
1621bfcache_hash (const void *arg)
1622{
19ba03f4
SM
1623 const struct btrace_frame_cache *cache
1624 = (const struct btrace_frame_cache *) arg;
0b722aec
MM
1625
1626 return htab_hash_pointer (cache->frame);
1627}
1628
1629/* eq_f for htab_create_alloc of bfcache. */
1630
1631static int
1632bfcache_eq (const void *arg1, const void *arg2)
1633{
19ba03f4
SM
1634 const struct btrace_frame_cache *cache1
1635 = (const struct btrace_frame_cache *) arg1;
1636 const struct btrace_frame_cache *cache2
1637 = (const struct btrace_frame_cache *) arg2;
0b722aec
MM
1638
1639 return cache1->frame == cache2->frame;
1640}
1641
1642/* Create a new btrace frame cache. */
1643
1644static struct btrace_frame_cache *
1645bfcache_new (struct frame_info *frame)
1646{
1647 struct btrace_frame_cache *cache;
1648 void **slot;
1649
1650 cache = FRAME_OBSTACK_ZALLOC (struct btrace_frame_cache);
1651 cache->frame = frame;
1652
1653 slot = htab_find_slot (bfcache, cache, INSERT);
1654 gdb_assert (*slot == NULL);
1655 *slot = cache;
1656
1657 return cache;
1658}
1659
1660/* Extract the branch trace function from a branch trace frame. */
1661
1662static const struct btrace_function *
1663btrace_get_frame_function (struct frame_info *frame)
1664{
1665 const struct btrace_frame_cache *cache;
0b722aec
MM
1666 struct btrace_frame_cache pattern;
1667 void **slot;
1668
1669 pattern.frame = frame;
1670
1671 slot = htab_find_slot (bfcache, &pattern, NO_INSERT);
1672 if (slot == NULL)
1673 return NULL;
1674
19ba03f4 1675 cache = (const struct btrace_frame_cache *) *slot;
0b722aec
MM
1676 return cache->bfun;
1677}
1678
cecac1ab
MM
1679/* Implement stop_reason method for record_btrace_frame_unwind. */
1680
1681static enum unwind_stop_reason
1682record_btrace_frame_unwind_stop_reason (struct frame_info *this_frame,
1683 void **this_cache)
1684{
0b722aec
MM
1685 const struct btrace_frame_cache *cache;
1686 const struct btrace_function *bfun;
1687
19ba03f4 1688 cache = (const struct btrace_frame_cache *) *this_cache;
0b722aec
MM
1689 bfun = cache->bfun;
1690 gdb_assert (bfun != NULL);
1691
42bfe59e 1692 if (bfun->up == 0)
0b722aec
MM
1693 return UNWIND_UNAVAILABLE;
1694
1695 return UNWIND_NO_REASON;
cecac1ab
MM
1696}
1697
1698/* Implement this_id method for record_btrace_frame_unwind. */
1699
1700static void
1701record_btrace_frame_this_id (struct frame_info *this_frame, void **this_cache,
1702 struct frame_id *this_id)
1703{
0b722aec
MM
1704 const struct btrace_frame_cache *cache;
1705 const struct btrace_function *bfun;
4aeb0dfc 1706 struct btrace_call_iterator it;
0b722aec
MM
1707 CORE_ADDR code, special;
1708
19ba03f4 1709 cache = (const struct btrace_frame_cache *) *this_cache;
0b722aec
MM
1710
1711 bfun = cache->bfun;
1712 gdb_assert (bfun != NULL);
1713
4aeb0dfc
TW
1714 while (btrace_find_call_by_number (&it, &cache->tp->btrace, bfun->prev) != 0)
1715 bfun = btrace_call_get (&it);
0b722aec
MM
1716
1717 code = get_frame_func (this_frame);
1718 special = bfun->number;
1719
1720 *this_id = frame_id_build_unavailable_stack_special (code, special);
1721
1722 DEBUG ("[frame] %s id: (!stack, pc=%s, special=%s)",
1723 btrace_get_bfun_name (cache->bfun),
1724 core_addr_to_string_nz (this_id->code_addr),
1725 core_addr_to_string_nz (this_id->special_addr));
cecac1ab
MM
1726}
1727
1728/* Implement prev_register method for record_btrace_frame_unwind. */
1729
1730static struct value *
1731record_btrace_frame_prev_register (struct frame_info *this_frame,
1732 void **this_cache,
1733 int regnum)
1734{
0b722aec
MM
1735 const struct btrace_frame_cache *cache;
1736 const struct btrace_function *bfun, *caller;
42bfe59e 1737 struct btrace_call_iterator it;
0b722aec
MM
1738 struct gdbarch *gdbarch;
1739 CORE_ADDR pc;
1740 int pcreg;
1741
1742 gdbarch = get_frame_arch (this_frame);
1743 pcreg = gdbarch_pc_regnum (gdbarch);
1744 if (pcreg < 0 || regnum != pcreg)
1745 throw_error (NOT_AVAILABLE_ERROR,
1746 _("Registers are not available in btrace record history"));
1747
19ba03f4 1748 cache = (const struct btrace_frame_cache *) *this_cache;
0b722aec
MM
1749 bfun = cache->bfun;
1750 gdb_assert (bfun != NULL);
1751
42bfe59e 1752 if (btrace_find_call_by_number (&it, &cache->tp->btrace, bfun->up) == 0)
0b722aec
MM
1753 throw_error (NOT_AVAILABLE_ERROR,
1754 _("No caller in btrace record history"));
1755
42bfe59e
TW
1756 caller = btrace_call_get (&it);
1757
0b722aec 1758 if ((bfun->flags & BFUN_UP_LINKS_TO_RET) != 0)
0860c437 1759 pc = caller->insn.front ().pc;
0b722aec
MM
1760 else
1761 {
0860c437 1762 pc = caller->insn.back ().pc;
0b722aec
MM
1763 pc += gdb_insn_length (gdbarch, pc);
1764 }
1765
1766 DEBUG ("[frame] unwound PC in %s on level %d: %s",
1767 btrace_get_bfun_name (bfun), bfun->level,
1768 core_addr_to_string_nz (pc));
1769
1770 return frame_unwind_got_address (this_frame, regnum, pc);
cecac1ab
MM
1771}
1772
1773/* Implement sniffer method for record_btrace_frame_unwind. */
1774
1775static int
1776record_btrace_frame_sniffer (const struct frame_unwind *self,
1777 struct frame_info *this_frame,
1778 void **this_cache)
1779{
0b722aec
MM
1780 const struct btrace_function *bfun;
1781 struct btrace_frame_cache *cache;
cecac1ab 1782 struct thread_info *tp;
0b722aec 1783 struct frame_info *next;
cecac1ab
MM
1784
1785 /* THIS_FRAME does not contain a reference to its thread. */
00431a78 1786 tp = inferior_thread ();
cecac1ab 1787
0b722aec
MM
1788 bfun = NULL;
1789 next = get_next_frame (this_frame);
1790 if (next == NULL)
1791 {
1792 const struct btrace_insn_iterator *replay;
1793
1794 replay = tp->btrace.replay;
1795 if (replay != NULL)
08c3f6d2 1796 bfun = &replay->btinfo->functions[replay->call_index];
0b722aec
MM
1797 }
1798 else
1799 {
1800 const struct btrace_function *callee;
42bfe59e 1801 struct btrace_call_iterator it;
0b722aec
MM
1802
1803 callee = btrace_get_frame_function (next);
42bfe59e
TW
1804 if (callee == NULL || (callee->flags & BFUN_UP_LINKS_TO_TAILCALL) != 0)
1805 return 0;
1806
1807 if (btrace_find_call_by_number (&it, &tp->btrace, callee->up) == 0)
1808 return 0;
1809
1810 bfun = btrace_call_get (&it);
0b722aec
MM
1811 }
1812
1813 if (bfun == NULL)
1814 return 0;
1815
1816 DEBUG ("[frame] sniffed frame for %s on level %d",
1817 btrace_get_bfun_name (bfun), bfun->level);
1818
1819 /* This is our frame. Initialize the frame cache. */
1820 cache = bfcache_new (this_frame);
1821 cache->tp = tp;
1822 cache->bfun = bfun;
1823
1824 *this_cache = cache;
1825 return 1;
1826}
1827
1828/* Implement sniffer method for record_btrace_tailcall_frame_unwind. */
1829
1830static int
1831record_btrace_tailcall_frame_sniffer (const struct frame_unwind *self,
1832 struct frame_info *this_frame,
1833 void **this_cache)
1834{
1835 const struct btrace_function *bfun, *callee;
1836 struct btrace_frame_cache *cache;
42bfe59e 1837 struct btrace_call_iterator it;
0b722aec 1838 struct frame_info *next;
42bfe59e 1839 struct thread_info *tinfo;
0b722aec
MM
1840
1841 next = get_next_frame (this_frame);
1842 if (next == NULL)
1843 return 0;
1844
1845 callee = btrace_get_frame_function (next);
1846 if (callee == NULL)
1847 return 0;
1848
1849 if ((callee->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
1850 return 0;
1851
00431a78 1852 tinfo = inferior_thread ();
42bfe59e 1853 if (btrace_find_call_by_number (&it, &tinfo->btrace, callee->up) == 0)
0b722aec
MM
1854 return 0;
1855
42bfe59e
TW
1856 bfun = btrace_call_get (&it);
1857
0b722aec
MM
1858 DEBUG ("[frame] sniffed tailcall frame for %s on level %d",
1859 btrace_get_bfun_name (bfun), bfun->level);
1860
1861 /* This is our frame. Initialize the frame cache. */
1862 cache = bfcache_new (this_frame);
42bfe59e 1863 cache->tp = tinfo;
0b722aec
MM
1864 cache->bfun = bfun;
1865
1866 *this_cache = cache;
1867 return 1;
1868}
1869
1870static void
1871record_btrace_frame_dealloc_cache (struct frame_info *self, void *this_cache)
1872{
1873 struct btrace_frame_cache *cache;
1874 void **slot;
1875
19ba03f4 1876 cache = (struct btrace_frame_cache *) this_cache;
0b722aec
MM
1877
1878 slot = htab_find_slot (bfcache, cache, NO_INSERT);
1879 gdb_assert (slot != NULL);
1880
1881 htab_remove_elt (bfcache, cache);
cecac1ab
MM
1882}
1883
1884/* btrace recording does not store previous memory content, neither the stack
30baf67b 1885 frames content. Any unwinding would return erroneous results as the stack
cecac1ab
MM
1886 contents no longer matches the changed PC value restored from history.
1887 Therefore this unwinder reports any possibly unwound registers as
1888 <unavailable>. */
1889
0b722aec 1890const struct frame_unwind record_btrace_frame_unwind =
cecac1ab 1891{
a154d838 1892 "record-btrace",
cecac1ab
MM
1893 NORMAL_FRAME,
1894 record_btrace_frame_unwind_stop_reason,
1895 record_btrace_frame_this_id,
1896 record_btrace_frame_prev_register,
1897 NULL,
0b722aec
MM
1898 record_btrace_frame_sniffer,
1899 record_btrace_frame_dealloc_cache
1900};
1901
1902const struct frame_unwind record_btrace_tailcall_frame_unwind =
1903{
a154d838 1904 "record-btrace tailcall",
0b722aec
MM
1905 TAILCALL_FRAME,
1906 record_btrace_frame_unwind_stop_reason,
1907 record_btrace_frame_this_id,
1908 record_btrace_frame_prev_register,
1909 NULL,
1910 record_btrace_tailcall_frame_sniffer,
1911 record_btrace_frame_dealloc_cache
cecac1ab 1912};
b2f4cfde 1913
f6ac5f3d 1914/* Implement the get_unwinder method. */
ac01945b 1915
f6ac5f3d
PA
1916const struct frame_unwind *
1917record_btrace_target::get_unwinder ()
ac01945b
TT
1918{
1919 return &record_btrace_frame_unwind;
1920}
1921
f6ac5f3d 1922/* Implement the get_tailcall_unwinder method. */
ac01945b 1923
f6ac5f3d
PA
1924const struct frame_unwind *
1925record_btrace_target::get_tailcall_unwinder ()
ac01945b
TT
1926{
1927 return &record_btrace_tailcall_frame_unwind;
1928}
1929
987e68b1
MM
1930/* Return a human-readable string for FLAG. */
1931
1932static const char *
04902b09 1933btrace_thread_flag_to_str (btrace_thread_flags flag)
987e68b1
MM
1934{
1935 switch (flag)
1936 {
1937 case BTHR_STEP:
1938 return "step";
1939
1940 case BTHR_RSTEP:
1941 return "reverse-step";
1942
1943 case BTHR_CONT:
1944 return "cont";
1945
1946 case BTHR_RCONT:
1947 return "reverse-cont";
1948
1949 case BTHR_STOP:
1950 return "stop";
1951 }
1952
1953 return "<invalid>";
1954}
1955
52834460
MM
1956/* Indicate that TP should be resumed according to FLAG. */
1957
1958static void
1959record_btrace_resume_thread (struct thread_info *tp,
1960 enum btrace_thread_flag flag)
1961{
1962 struct btrace_thread_info *btinfo;
1963
43792cf0 1964 DEBUG ("resuming thread %s (%s): %x (%s)", print_thread_id (tp),
a068643d
TT
1965 target_pid_to_str (tp->ptid).c_str (), flag,
1966 btrace_thread_flag_to_str (flag));
52834460
MM
1967
1968 btinfo = &tp->btrace;
1969
52834460 1970 /* Fetch the latest branch trace. */
4a4495d6 1971 btrace_fetch (tp, record_btrace_get_cpu ());
52834460 1972
0ca912df
MM
1973 /* A resume request overwrites a preceding resume or stop request. */
1974 btinfo->flags &= ~(BTHR_MOVE | BTHR_STOP);
52834460
MM
1975 btinfo->flags |= flag;
1976}
1977
ec71cc2f
MM
1978/* Get the current frame for TP. */
1979
79b8d3b0
TT
1980static struct frame_id
1981get_thread_current_frame_id (struct thread_info *tp)
ec71cc2f 1982{
79b8d3b0 1983 struct frame_id id;
719546c4 1984 bool executing;
ec71cc2f 1985
00431a78
PA
1986 /* Set current thread, which is implicitly used by
1987 get_current_frame. */
1988 scoped_restore_current_thread restore_thread;
1989
1990 switch_to_thread (tp);
ec71cc2f 1991
5b6d1e4f
PA
1992 process_stratum_target *proc_target = tp->inf->process_target ();
1993
ec71cc2f
MM
1994 /* Clear the executing flag to allow changes to the current frame.
1995 We are not actually running, yet. We just started a reverse execution
1996 command or a record goto command.
1997 For the latter, EXECUTING is false and this has no effect.
f6ac5f3d 1998 For the former, EXECUTING is true and we're in wait, about to
ec71cc2f 1999 move the thread. Since we need to recompute the stack, we temporarily
85102364 2000 set EXECUTING to false. */
00431a78 2001 executing = tp->executing;
5b6d1e4f 2002 set_executing (proc_target, inferior_ptid, false);
ec71cc2f 2003
79b8d3b0 2004 id = null_frame_id;
a70b8144 2005 try
ec71cc2f 2006 {
79b8d3b0 2007 id = get_frame_id (get_current_frame ());
ec71cc2f 2008 }
230d2906 2009 catch (const gdb_exception &except)
ec71cc2f
MM
2010 {
2011 /* Restore the previous execution state. */
5b6d1e4f 2012 set_executing (proc_target, inferior_ptid, executing);
ec71cc2f 2013
eedc3f4f 2014 throw;
ec71cc2f 2015 }
ec71cc2f
MM
2016
2017 /* Restore the previous execution state. */
5b6d1e4f 2018 set_executing (proc_target, inferior_ptid, executing);
ec71cc2f 2019
79b8d3b0 2020 return id;
ec71cc2f
MM
2021}
2022
52834460
MM
2023/* Start replaying a thread. */
2024
2025static struct btrace_insn_iterator *
2026record_btrace_start_replaying (struct thread_info *tp)
2027{
52834460
MM
2028 struct btrace_insn_iterator *replay;
2029 struct btrace_thread_info *btinfo;
52834460
MM
2030
2031 btinfo = &tp->btrace;
2032 replay = NULL;
2033
2034 /* We can't start replaying without trace. */
b54b03bd 2035 if (btinfo->functions.empty ())
52834460
MM
2036 return NULL;
2037
52834460
MM
2038 /* GDB stores the current frame_id when stepping in order to detects steps
2039 into subroutines.
2040 Since frames are computed differently when we're replaying, we need to
2041 recompute those stored frames and fix them up so we can still detect
2042 subroutines after we started replaying. */
a70b8144 2043 try
52834460 2044 {
52834460
MM
2045 struct frame_id frame_id;
2046 int upd_step_frame_id, upd_step_stack_frame_id;
2047
2048 /* The current frame without replaying - computed via normal unwind. */
79b8d3b0 2049 frame_id = get_thread_current_frame_id (tp);
52834460
MM
2050
2051 /* Check if we need to update any stepping-related frame id's. */
2052 upd_step_frame_id = frame_id_eq (frame_id,
2053 tp->control.step_frame_id);
2054 upd_step_stack_frame_id = frame_id_eq (frame_id,
2055 tp->control.step_stack_frame_id);
2056
2057 /* We start replaying at the end of the branch trace. This corresponds
2058 to the current instruction. */
8d749320 2059 replay = XNEW (struct btrace_insn_iterator);
52834460
MM
2060 btrace_insn_end (replay, btinfo);
2061
31fd9caa
MM
2062 /* Skip gaps at the end of the trace. */
2063 while (btrace_insn_get (replay) == NULL)
2064 {
2065 unsigned int steps;
2066
2067 steps = btrace_insn_prev (replay, 1);
2068 if (steps == 0)
2069 error (_("No trace."));
2070 }
2071
52834460
MM
2072 /* We're not replaying, yet. */
2073 gdb_assert (btinfo->replay == NULL);
2074 btinfo->replay = replay;
2075
2076 /* Make sure we're not using any stale registers. */
00431a78 2077 registers_changed_thread (tp);
52834460
MM
2078
2079 /* The current frame with replaying - computed via btrace unwind. */
79b8d3b0 2080 frame_id = get_thread_current_frame_id (tp);
52834460
MM
2081
2082 /* Replace stepping related frames where necessary. */
2083 if (upd_step_frame_id)
2084 tp->control.step_frame_id = frame_id;
2085 if (upd_step_stack_frame_id)
2086 tp->control.step_stack_frame_id = frame_id;
2087 }
230d2906 2088 catch (const gdb_exception &except)
52834460
MM
2089 {
2090 xfree (btinfo->replay);
2091 btinfo->replay = NULL;
2092
00431a78 2093 registers_changed_thread (tp);
52834460 2094
eedc3f4f 2095 throw;
52834460
MM
2096 }
2097
2098 return replay;
2099}
2100
2101/* Stop replaying a thread. */
2102
2103static void
2104record_btrace_stop_replaying (struct thread_info *tp)
2105{
2106 struct btrace_thread_info *btinfo;
2107
2108 btinfo = &tp->btrace;
2109
2110 xfree (btinfo->replay);
2111 btinfo->replay = NULL;
2112
2113 /* Make sure we're not leaving any stale registers. */
00431a78 2114 registers_changed_thread (tp);
52834460
MM
2115}
2116
e3cfc1c7
MM
2117/* Stop replaying TP if it is at the end of its execution history. */
2118
2119static void
2120record_btrace_stop_replaying_at_end (struct thread_info *tp)
2121{
2122 struct btrace_insn_iterator *replay, end;
2123 struct btrace_thread_info *btinfo;
2124
2125 btinfo = &tp->btrace;
2126 replay = btinfo->replay;
2127
2128 if (replay == NULL)
2129 return;
2130
2131 btrace_insn_end (&end, btinfo);
2132
2133 if (btrace_insn_cmp (replay, &end) == 0)
2134 record_btrace_stop_replaying (tp);
2135}
2136
f6ac5f3d 2137/* The resume method of target record-btrace. */
b2f4cfde 2138
f6ac5f3d
PA
2139void
2140record_btrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
b2f4cfde 2141{
d2939ba2 2142 enum btrace_thread_flag flag, cflag;
52834460 2143
a068643d 2144 DEBUG ("resume %s: %s%s", target_pid_to_str (ptid).c_str (),
f6ac5f3d 2145 ::execution_direction == EXEC_REVERSE ? "reverse-" : "",
987e68b1 2146 step ? "step" : "cont");
52834460 2147
0ca912df
MM
2148 /* Store the execution direction of the last resume.
2149
f6ac5f3d 2150 If there is more than one resume call, we have to rely on infrun
0ca912df 2151 to not change the execution direction in-between. */
f6ac5f3d 2152 record_btrace_resume_exec_dir = ::execution_direction;
70ad5bff 2153
0ca912df 2154 /* As long as we're not replaying, just forward the request.
52834460 2155
0ca912df
MM
2156 For non-stop targets this means that no thread is replaying. In order to
2157 make progress, we may need to explicitly move replaying threads to the end
2158 of their execution history. */
f6ac5f3d
PA
2159 if ((::execution_direction != EXEC_REVERSE)
2160 && !record_is_replaying (minus_one_ptid))
b2f4cfde 2161 {
b6a8c27b 2162 this->beneath ()->resume (ptid, step, signal);
04c4fe8c 2163 return;
b2f4cfde
MM
2164 }
2165
52834460 2166 /* Compute the btrace thread flag for the requested move. */
f6ac5f3d 2167 if (::execution_direction == EXEC_REVERSE)
d2939ba2
MM
2168 {
2169 flag = step == 0 ? BTHR_RCONT : BTHR_RSTEP;
2170 cflag = BTHR_RCONT;
2171 }
52834460 2172 else
d2939ba2
MM
2173 {
2174 flag = step == 0 ? BTHR_CONT : BTHR_STEP;
2175 cflag = BTHR_CONT;
2176 }
52834460 2177
52834460 2178 /* We just indicate the resume intent here. The actual stepping happens in
d2939ba2
MM
2179 record_btrace_wait below.
2180
2181 For all-stop targets, we only step INFERIOR_PTID and continue others. */
5b6d1e4f
PA
2182
2183 process_stratum_target *proc_target = current_inferior ()->process_target ();
2184
d2939ba2
MM
2185 if (!target_is_non_stop_p ())
2186 {
26a57c92 2187 gdb_assert (inferior_ptid.matches (ptid));
d2939ba2 2188
5b6d1e4f 2189 for (thread_info *tp : all_non_exited_threads (proc_target, ptid))
08036331
PA
2190 {
2191 if (tp->ptid.matches (inferior_ptid))
2192 record_btrace_resume_thread (tp, flag);
2193 else
2194 record_btrace_resume_thread (tp, cflag);
2195 }
d2939ba2
MM
2196 }
2197 else
2198 {
5b6d1e4f 2199 for (thread_info *tp : all_non_exited_threads (proc_target, ptid))
08036331 2200 record_btrace_resume_thread (tp, flag);
d2939ba2 2201 }
70ad5bff
MM
2202
2203 /* Async support. */
2204 if (target_can_async_p ())
2205 {
6a3753b3 2206 target_async (1);
70ad5bff
MM
2207 mark_async_event_handler (record_btrace_async_inferior_event_handler);
2208 }
52834460
MM
2209}
2210
987e68b1
MM
2211/* Cancel resuming TP. */
2212
2213static void
2214record_btrace_cancel_resume (struct thread_info *tp)
2215{
04902b09 2216 btrace_thread_flags flags;
987e68b1
MM
2217
2218 flags = tp->btrace.flags & (BTHR_MOVE | BTHR_STOP);
2219 if (flags == 0)
2220 return;
2221
43792cf0
PA
2222 DEBUG ("cancel resume thread %s (%s): %x (%s)",
2223 print_thread_id (tp),
04902b09 2224 target_pid_to_str (tp->ptid).c_str (), flags.raw (),
987e68b1
MM
2225 btrace_thread_flag_to_str (flags));
2226
2227 tp->btrace.flags &= ~(BTHR_MOVE | BTHR_STOP);
e3cfc1c7 2228 record_btrace_stop_replaying_at_end (tp);
52834460
MM
2229}
2230
2231/* Return a target_waitstatus indicating that we ran out of history. */
2232
2233static struct target_waitstatus
2234btrace_step_no_history (void)
2235{
2236 struct target_waitstatus status;
2237
2238 status.kind = TARGET_WAITKIND_NO_HISTORY;
2239
2240 return status;
2241}
2242
2243/* Return a target_waitstatus indicating that a step finished. */
2244
2245static struct target_waitstatus
2246btrace_step_stopped (void)
2247{
2248 struct target_waitstatus status;
2249
2250 status.kind = TARGET_WAITKIND_STOPPED;
2251 status.value.sig = GDB_SIGNAL_TRAP;
2252
2253 return status;
2254}
2255
6e4879f0
MM
2256/* Return a target_waitstatus indicating that a thread was stopped as
2257 requested. */
2258
2259static struct target_waitstatus
2260btrace_step_stopped_on_request (void)
2261{
2262 struct target_waitstatus status;
2263
2264 status.kind = TARGET_WAITKIND_STOPPED;
2265 status.value.sig = GDB_SIGNAL_0;
2266
2267 return status;
2268}
2269
d825d248
MM
2270/* Return a target_waitstatus indicating a spurious stop. */
2271
2272static struct target_waitstatus
2273btrace_step_spurious (void)
2274{
2275 struct target_waitstatus status;
2276
2277 status.kind = TARGET_WAITKIND_SPURIOUS;
2278
2279 return status;
2280}
2281
e3cfc1c7
MM
2282/* Return a target_waitstatus indicating that the thread was not resumed. */
2283
2284static struct target_waitstatus
2285btrace_step_no_resumed (void)
2286{
2287 struct target_waitstatus status;
2288
2289 status.kind = TARGET_WAITKIND_NO_RESUMED;
2290
2291 return status;
2292}
2293
2294/* Return a target_waitstatus indicating that we should wait again. */
2295
2296static struct target_waitstatus
2297btrace_step_again (void)
2298{
2299 struct target_waitstatus status;
2300
2301 status.kind = TARGET_WAITKIND_IGNORE;
2302
2303 return status;
2304}
2305
52834460
MM
2306/* Clear the record histories. */
2307
2308static void
2309record_btrace_clear_histories (struct btrace_thread_info *btinfo)
2310{
2311 xfree (btinfo->insn_history);
2312 xfree (btinfo->call_history);
2313
2314 btinfo->insn_history = NULL;
2315 btinfo->call_history = NULL;
2316}
2317
3c615f99
MM
2318/* Check whether TP's current replay position is at a breakpoint. */
2319
2320static int
2321record_btrace_replay_at_breakpoint (struct thread_info *tp)
2322{
2323 struct btrace_insn_iterator *replay;
2324 struct btrace_thread_info *btinfo;
2325 const struct btrace_insn *insn;
3c615f99
MM
2326
2327 btinfo = &tp->btrace;
2328 replay = btinfo->replay;
2329
2330 if (replay == NULL)
2331 return 0;
2332
2333 insn = btrace_insn_get (replay);
2334 if (insn == NULL)
2335 return 0;
2336
00431a78 2337 return record_check_stopped_by_breakpoint (tp->inf->aspace, insn->pc,
3c615f99
MM
2338 &btinfo->stop_reason);
2339}
2340
d825d248 2341/* Step one instruction in forward direction. */
52834460
MM
2342
2343static struct target_waitstatus
d825d248 2344record_btrace_single_step_forward (struct thread_info *tp)
52834460 2345{
b61ce85c 2346 struct btrace_insn_iterator *replay, end, start;
52834460 2347 struct btrace_thread_info *btinfo;
52834460 2348
d825d248
MM
2349 btinfo = &tp->btrace;
2350 replay = btinfo->replay;
2351
2352 /* We're done if we're not replaying. */
2353 if (replay == NULL)
2354 return btrace_step_no_history ();
2355
011c71b6
MM
2356 /* Check if we're stepping a breakpoint. */
2357 if (record_btrace_replay_at_breakpoint (tp))
2358 return btrace_step_stopped ();
2359
b61ce85c
MM
2360 /* Skip gaps during replay. If we end up at a gap (at the end of the trace),
2361 jump back to the instruction at which we started. */
2362 start = *replay;
d825d248
MM
2363 do
2364 {
2365 unsigned int steps;
2366
e3cfc1c7
MM
2367 /* We will bail out here if we continue stepping after reaching the end
2368 of the execution history. */
d825d248
MM
2369 steps = btrace_insn_next (replay, 1);
2370 if (steps == 0)
b61ce85c
MM
2371 {
2372 *replay = start;
2373 return btrace_step_no_history ();
2374 }
d825d248
MM
2375 }
2376 while (btrace_insn_get (replay) == NULL);
2377
2378 /* Determine the end of the instruction trace. */
2379 btrace_insn_end (&end, btinfo);
2380
e3cfc1c7
MM
2381 /* The execution trace contains (and ends with) the current instruction.
2382 This instruction has not been executed, yet, so the trace really ends
2383 one instruction earlier. */
d825d248 2384 if (btrace_insn_cmp (replay, &end) == 0)
e3cfc1c7 2385 return btrace_step_no_history ();
d825d248
MM
2386
2387 return btrace_step_spurious ();
2388}
2389
2390/* Step one instruction in backward direction. */
2391
2392static struct target_waitstatus
2393record_btrace_single_step_backward (struct thread_info *tp)
2394{
b61ce85c 2395 struct btrace_insn_iterator *replay, start;
d825d248 2396 struct btrace_thread_info *btinfo;
e59fa00f 2397
52834460
MM
2398 btinfo = &tp->btrace;
2399 replay = btinfo->replay;
2400
d825d248
MM
2401 /* Start replaying if we're not already doing so. */
2402 if (replay == NULL)
2403 replay = record_btrace_start_replaying (tp);
2404
2405 /* If we can't step any further, we reached the end of the history.
b61ce85c
MM
2406 Skip gaps during replay. If we end up at a gap (at the beginning of
2407 the trace), jump back to the instruction at which we started. */
2408 start = *replay;
d825d248
MM
2409 do
2410 {
2411 unsigned int steps;
2412
2413 steps = btrace_insn_prev (replay, 1);
2414 if (steps == 0)
b61ce85c
MM
2415 {
2416 *replay = start;
2417 return btrace_step_no_history ();
2418 }
d825d248
MM
2419 }
2420 while (btrace_insn_get (replay) == NULL);
2421
011c71b6
MM
2422 /* Check if we're stepping a breakpoint.
2423
2424 For reverse-stepping, this check is after the step. There is logic in
2425 infrun.c that handles reverse-stepping separately. See, for example,
2426 proceed and adjust_pc_after_break.
2427
2428 This code assumes that for reverse-stepping, PC points to the last
2429 de-executed instruction, whereas for forward-stepping PC points to the
2430 next to-be-executed instruction. */
2431 if (record_btrace_replay_at_breakpoint (tp))
2432 return btrace_step_stopped ();
2433
d825d248
MM
2434 return btrace_step_spurious ();
2435}
2436
2437/* Step a single thread. */
2438
2439static struct target_waitstatus
2440record_btrace_step_thread (struct thread_info *tp)
2441{
2442 struct btrace_thread_info *btinfo;
2443 struct target_waitstatus status;
04902b09 2444 btrace_thread_flags flags;
d825d248
MM
2445
2446 btinfo = &tp->btrace;
2447
6e4879f0
MM
2448 flags = btinfo->flags & (BTHR_MOVE | BTHR_STOP);
2449 btinfo->flags &= ~(BTHR_MOVE | BTHR_STOP);
52834460 2450
43792cf0 2451 DEBUG ("stepping thread %s (%s): %x (%s)", print_thread_id (tp),
04902b09 2452 target_pid_to_str (tp->ptid).c_str (), flags.raw (),
987e68b1 2453 btrace_thread_flag_to_str (flags));
52834460 2454
6e4879f0
MM
2455 /* We can't step without an execution history. */
2456 if ((flags & BTHR_MOVE) != 0 && btrace_is_empty (tp))
2457 return btrace_step_no_history ();
2458
52834460
MM
2459 switch (flags)
2460 {
2461 default:
2462 internal_error (__FILE__, __LINE__, _("invalid stepping type."));
2463
6e4879f0
MM
2464 case BTHR_STOP:
2465 return btrace_step_stopped_on_request ();
2466
52834460 2467 case BTHR_STEP:
d825d248
MM
2468 status = record_btrace_single_step_forward (tp);
2469 if (status.kind != TARGET_WAITKIND_SPURIOUS)
e3cfc1c7 2470 break;
52834460
MM
2471
2472 return btrace_step_stopped ();
2473
2474 case BTHR_RSTEP:
d825d248
MM
2475 status = record_btrace_single_step_backward (tp);
2476 if (status.kind != TARGET_WAITKIND_SPURIOUS)
e3cfc1c7 2477 break;
52834460
MM
2478
2479 return btrace_step_stopped ();
2480
2481 case BTHR_CONT:
e3cfc1c7
MM
2482 status = record_btrace_single_step_forward (tp);
2483 if (status.kind != TARGET_WAITKIND_SPURIOUS)
2484 break;
52834460 2485
e3cfc1c7
MM
2486 btinfo->flags |= flags;
2487 return btrace_step_again ();
52834460
MM
2488
2489 case BTHR_RCONT:
e3cfc1c7
MM
2490 status = record_btrace_single_step_backward (tp);
2491 if (status.kind != TARGET_WAITKIND_SPURIOUS)
2492 break;
52834460 2493
e3cfc1c7
MM
2494 btinfo->flags |= flags;
2495 return btrace_step_again ();
2496 }
d825d248 2497
f6ac5f3d 2498 /* We keep threads moving at the end of their execution history. The wait
e3cfc1c7
MM
2499 method will stop the thread for whom the event is reported. */
2500 if (status.kind == TARGET_WAITKIND_NO_HISTORY)
2501 btinfo->flags |= flags;
52834460 2502
e3cfc1c7 2503 return status;
b2f4cfde
MM
2504}
2505
a6b5be76
MM
2506/* Announce further events if necessary. */
2507
2508static void
53127008
SM
2509record_btrace_maybe_mark_async_event
2510 (const std::vector<thread_info *> &moving,
2511 const std::vector<thread_info *> &no_history)
a6b5be76 2512{
53127008
SM
2513 bool more_moving = !moving.empty ();
2514 bool more_no_history = !no_history.empty ();;
a6b5be76
MM
2515
2516 if (!more_moving && !more_no_history)
2517 return;
2518
2519 if (more_moving)
2520 DEBUG ("movers pending");
2521
2522 if (more_no_history)
2523 DEBUG ("no-history pending");
2524
2525 mark_async_event_handler (record_btrace_async_inferior_event_handler);
2526}
2527
f6ac5f3d 2528/* The wait method of target record-btrace. */
b2f4cfde 2529
f6ac5f3d
PA
2530ptid_t
2531record_btrace_target::wait (ptid_t ptid, struct target_waitstatus *status,
b60cea74 2532 target_wait_flags options)
b2f4cfde 2533{
53127008
SM
2534 std::vector<thread_info *> moving;
2535 std::vector<thread_info *> no_history;
52834460 2536
85d3ad8e
SM
2537 /* Clear this, if needed we'll re-mark it below. */
2538 clear_async_event_handler (record_btrace_async_inferior_event_handler);
2539
b60cea74
TT
2540 DEBUG ("wait %s (0x%x)", target_pid_to_str (ptid).c_str (),
2541 (unsigned) options);
52834460 2542
b2f4cfde 2543 /* As long as we're not replaying, just forward the request. */
f6ac5f3d
PA
2544 if ((::execution_direction != EXEC_REVERSE)
2545 && !record_is_replaying (minus_one_ptid))
b2f4cfde 2546 {
b6a8c27b 2547 return this->beneath ()->wait (ptid, status, options);
b2f4cfde
MM
2548 }
2549
e3cfc1c7 2550 /* Keep a work list of moving threads. */
5b6d1e4f
PA
2551 process_stratum_target *proc_target = current_inferior ()->process_target ();
2552 for (thread_info *tp : all_non_exited_threads (proc_target, ptid))
08036331
PA
2553 if ((tp->btrace.flags & (BTHR_MOVE | BTHR_STOP)) != 0)
2554 moving.push_back (tp);
e3cfc1c7 2555
53127008 2556 if (moving.empty ())
52834460 2557 {
e3cfc1c7 2558 *status = btrace_step_no_resumed ();
52834460 2559
a068643d 2560 DEBUG ("wait ended by %s: %s", target_pid_to_str (null_ptid).c_str (),
23fdd69e 2561 target_waitstatus_to_string (status).c_str ());
e3cfc1c7 2562
e3cfc1c7 2563 return null_ptid;
52834460
MM
2564 }
2565
e3cfc1c7
MM
2566 /* Step moving threads one by one, one step each, until either one thread
2567 reports an event or we run out of threads to step.
2568
2569 When stepping more than one thread, chances are that some threads reach
2570 the end of their execution history earlier than others. If we reported
2571 this immediately, all-stop on top of non-stop would stop all threads and
2572 resume the same threads next time. And we would report the same thread
2573 having reached the end of its execution history again.
2574
2575 In the worst case, this would starve the other threads. But even if other
2576 threads would be allowed to make progress, this would result in far too
2577 many intermediate stops.
2578
2579 We therefore delay the reporting of "no execution history" until we have
2580 nothing else to report. By this time, all threads should have moved to
2581 either the beginning or the end of their execution history. There will
2582 be a single user-visible stop. */
53127008
SM
2583 struct thread_info *eventing = NULL;
2584 while ((eventing == NULL) && !moving.empty ())
e3cfc1c7 2585 {
53127008 2586 for (unsigned int ix = 0; eventing == NULL && ix < moving.size ();)
e3cfc1c7 2587 {
53127008
SM
2588 thread_info *tp = moving[ix];
2589
e3cfc1c7
MM
2590 *status = record_btrace_step_thread (tp);
2591
2592 switch (status->kind)
2593 {
2594 case TARGET_WAITKIND_IGNORE:
2595 ix++;
2596 break;
2597
2598 case TARGET_WAITKIND_NO_HISTORY:
53127008 2599 no_history.push_back (ordered_remove (moving, ix));
e3cfc1c7
MM
2600 break;
2601
2602 default:
53127008 2603 eventing = unordered_remove (moving, ix);
e3cfc1c7
MM
2604 break;
2605 }
2606 }
2607 }
2608
2609 if (eventing == NULL)
2610 {
2611 /* We started with at least one moving thread. This thread must have
2612 either stopped or reached the end of its execution history.
2613
2614 In the former case, EVENTING must not be NULL.
2615 In the latter case, NO_HISTORY must not be empty. */
53127008 2616 gdb_assert (!no_history.empty ());
e3cfc1c7
MM
2617
2618 /* We kept threads moving at the end of their execution history. Stop
2619 EVENTING now that we are going to report its stop. */
53127008 2620 eventing = unordered_remove (no_history, 0);
e3cfc1c7
MM
2621 eventing->btrace.flags &= ~BTHR_MOVE;
2622
2623 *status = btrace_step_no_history ();
2624 }
2625
2626 gdb_assert (eventing != NULL);
2627
2628 /* We kept threads replaying at the end of their execution history. Stop
2629 replaying EVENTING now that we are going to report its stop. */
2630 record_btrace_stop_replaying_at_end (eventing);
52834460
MM
2631
2632 /* Stop all other threads. */
5953356c 2633 if (!target_is_non_stop_p ())
53127008 2634 {
d89edf9b 2635 for (thread_info *tp : current_inferior ()->non_exited_threads ())
53127008
SM
2636 record_btrace_cancel_resume (tp);
2637 }
52834460 2638
a6b5be76
MM
2639 /* In async mode, we need to announce further events. */
2640 if (target_is_async_p ())
2641 record_btrace_maybe_mark_async_event (moving, no_history);
2642
52834460 2643 /* Start record histories anew from the current position. */
e3cfc1c7 2644 record_btrace_clear_histories (&eventing->btrace);
52834460
MM
2645
2646 /* We moved the replay position but did not update registers. */
00431a78 2647 registers_changed_thread (eventing);
e3cfc1c7 2648
43792cf0
PA
2649 DEBUG ("wait ended by thread %s (%s): %s",
2650 print_thread_id (eventing),
a068643d 2651 target_pid_to_str (eventing->ptid).c_str (),
23fdd69e 2652 target_waitstatus_to_string (status).c_str ());
52834460 2653
e3cfc1c7 2654 return eventing->ptid;
52834460
MM
2655}
2656
f6ac5f3d 2657/* The stop method of target record-btrace. */
6e4879f0 2658
f6ac5f3d
PA
2659void
2660record_btrace_target::stop (ptid_t ptid)
6e4879f0 2661{
a068643d 2662 DEBUG ("stop %s", target_pid_to_str (ptid).c_str ());
6e4879f0
MM
2663
2664 /* As long as we're not replaying, just forward the request. */
f6ac5f3d
PA
2665 if ((::execution_direction != EXEC_REVERSE)
2666 && !record_is_replaying (minus_one_ptid))
6e4879f0 2667 {
b6a8c27b 2668 this->beneath ()->stop (ptid);
6e4879f0
MM
2669 }
2670 else
2671 {
5b6d1e4f
PA
2672 process_stratum_target *proc_target
2673 = current_inferior ()->process_target ();
2674
2675 for (thread_info *tp : all_non_exited_threads (proc_target, ptid))
08036331
PA
2676 {
2677 tp->btrace.flags &= ~BTHR_MOVE;
2678 tp->btrace.flags |= BTHR_STOP;
2679 }
6e4879f0
MM
2680 }
2681 }
2682
f6ac5f3d 2683/* The can_execute_reverse method of target record-btrace. */
52834460 2684
57810aa7 2685bool
f6ac5f3d 2686record_btrace_target::can_execute_reverse ()
52834460 2687{
57810aa7 2688 return true;
52834460
MM
2689}
2690
f6ac5f3d 2691/* The stopped_by_sw_breakpoint method of target record-btrace. */
52834460 2692
57810aa7 2693bool
f6ac5f3d 2694record_btrace_target::stopped_by_sw_breakpoint ()
52834460 2695{
f6ac5f3d 2696 if (record_is_replaying (minus_one_ptid))
9e8915c6
PA
2697 {
2698 struct thread_info *tp = inferior_thread ();
2699
2700 return tp->btrace.stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
2701 }
2702
b6a8c27b 2703 return this->beneath ()->stopped_by_sw_breakpoint ();
9e8915c6
PA
2704}
2705
f6ac5f3d 2706/* The supports_stopped_by_sw_breakpoint method of target
9e8915c6
PA
2707 record-btrace. */
2708
57810aa7 2709bool
f6ac5f3d 2710record_btrace_target::supports_stopped_by_sw_breakpoint ()
9e8915c6 2711{
f6ac5f3d 2712 if (record_is_replaying (minus_one_ptid))
57810aa7 2713 return true;
9e8915c6 2714
b6a8c27b 2715 return this->beneath ()->supports_stopped_by_sw_breakpoint ();
9e8915c6
PA
2716}
2717
f6ac5f3d 2718/* The stopped_by_sw_breakpoint method of target record-btrace. */
9e8915c6 2719
57810aa7 2720bool
f6ac5f3d 2721record_btrace_target::stopped_by_hw_breakpoint ()
9e8915c6 2722{
f6ac5f3d 2723 if (record_is_replaying (minus_one_ptid))
9e8915c6
PA
2724 {
2725 struct thread_info *tp = inferior_thread ();
2726
2727 return tp->btrace.stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
2728 }
2729
b6a8c27b 2730 return this->beneath ()->stopped_by_hw_breakpoint ();
9e8915c6
PA
2731}
2732
f6ac5f3d 2733/* The supports_stopped_by_hw_breakpoint method of target
9e8915c6
PA
2734 record-btrace. */
2735
57810aa7 2736bool
f6ac5f3d 2737record_btrace_target::supports_stopped_by_hw_breakpoint ()
9e8915c6 2738{
f6ac5f3d 2739 if (record_is_replaying (minus_one_ptid))
57810aa7 2740 return true;
52834460 2741
b6a8c27b 2742 return this->beneath ()->supports_stopped_by_hw_breakpoint ();
b2f4cfde
MM
2743}
2744
f6ac5f3d 2745/* The update_thread_list method of target record-btrace. */
e2887aa3 2746
f6ac5f3d
PA
2747void
2748record_btrace_target::update_thread_list ()
e2887aa3 2749{
e8032dde 2750 /* We don't add or remove threads during replay. */
f6ac5f3d 2751 if (record_is_replaying (minus_one_ptid))
e2887aa3
MM
2752 return;
2753
2754 /* Forward the request. */
b6a8c27b 2755 this->beneath ()->update_thread_list ();
e2887aa3
MM
2756}
2757
f6ac5f3d 2758/* The thread_alive method of target record-btrace. */
e2887aa3 2759
57810aa7 2760bool
f6ac5f3d 2761record_btrace_target::thread_alive (ptid_t ptid)
e2887aa3
MM
2762{
2763 /* We don't add or remove threads during replay. */
f6ac5f3d 2764 if (record_is_replaying (minus_one_ptid))
00431a78 2765 return true;
e2887aa3
MM
2766
2767 /* Forward the request. */
b6a8c27b 2768 return this->beneath ()->thread_alive (ptid);
e2887aa3
MM
2769}
2770
066ce621
MM
2771/* Set the replay branch trace instruction iterator. If IT is NULL, replay
2772 is stopped. */
2773
2774static void
2775record_btrace_set_replay (struct thread_info *tp,
2776 const struct btrace_insn_iterator *it)
2777{
2778 struct btrace_thread_info *btinfo;
2779
2780 btinfo = &tp->btrace;
2781
a0f1b963 2782 if (it == NULL)
52834460 2783 record_btrace_stop_replaying (tp);
066ce621
MM
2784 else
2785 {
2786 if (btinfo->replay == NULL)
52834460 2787 record_btrace_start_replaying (tp);
066ce621
MM
2788 else if (btrace_insn_cmp (btinfo->replay, it) == 0)
2789 return;
2790
2791 *btinfo->replay = *it;
00431a78 2792 registers_changed_thread (tp);
066ce621
MM
2793 }
2794
52834460
MM
2795 /* Start anew from the new replay position. */
2796 record_btrace_clear_histories (btinfo);
485668e5 2797
f2ffa92b
PA
2798 inferior_thread ()->suspend.stop_pc
2799 = regcache_read_pc (get_current_regcache ());
485668e5 2800 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
066ce621
MM
2801}
2802
f6ac5f3d 2803/* The goto_record_begin method of target record-btrace. */
066ce621 2804
f6ac5f3d
PA
2805void
2806record_btrace_target::goto_record_begin ()
066ce621
MM
2807{
2808 struct thread_info *tp;
2809 struct btrace_insn_iterator begin;
2810
2811 tp = require_btrace_thread ();
2812
2813 btrace_insn_begin (&begin, &tp->btrace);
b61ce85c
MM
2814
2815 /* Skip gaps at the beginning of the trace. */
2816 while (btrace_insn_get (&begin) == NULL)
2817 {
2818 unsigned int steps;
2819
2820 steps = btrace_insn_next (&begin, 1);
2821 if (steps == 0)
2822 error (_("No trace."));
2823 }
2824
066ce621 2825 record_btrace_set_replay (tp, &begin);
066ce621
MM
2826}
2827
f6ac5f3d 2828/* The goto_record_end method of target record-btrace. */
066ce621 2829
f6ac5f3d
PA
2830void
2831record_btrace_target::goto_record_end ()
066ce621
MM
2832{
2833 struct thread_info *tp;
2834
2835 tp = require_btrace_thread ();
2836
2837 record_btrace_set_replay (tp, NULL);
066ce621
MM
2838}
2839
f6ac5f3d 2840/* The goto_record method of target record-btrace. */
066ce621 2841
f6ac5f3d
PA
2842void
2843record_btrace_target::goto_record (ULONGEST insn)
066ce621
MM
2844{
2845 struct thread_info *tp;
2846 struct btrace_insn_iterator it;
2847 unsigned int number;
2848 int found;
2849
2850 number = insn;
2851
2852 /* Check for wrap-arounds. */
2853 if (number != insn)
2854 error (_("Instruction number out of range."));
2855
2856 tp = require_btrace_thread ();
2857
2858 found = btrace_find_insn_by_number (&it, &tp->btrace, number);
69090cee
TW
2859
2860 /* Check if the instruction could not be found or is a gap. */
2861 if (found == 0 || btrace_insn_get (&it) == NULL)
066ce621
MM
2862 error (_("No such instruction."));
2863
2864 record_btrace_set_replay (tp, &it);
066ce621
MM
2865}
2866
f6ac5f3d 2867/* The record_stop_replaying method of target record-btrace. */
797094dd 2868
f6ac5f3d
PA
2869void
2870record_btrace_target::record_stop_replaying ()
797094dd 2871{
d89edf9b 2872 for (thread_info *tp : current_inferior ()->non_exited_threads ())
797094dd
MM
2873 record_btrace_stop_replaying (tp);
2874}
2875
f6ac5f3d 2876/* The execution_direction target method. */
70ad5bff 2877
f6ac5f3d
PA
2878enum exec_direction_kind
2879record_btrace_target::execution_direction ()
70ad5bff
MM
2880{
2881 return record_btrace_resume_exec_dir;
2882}
2883
f6ac5f3d 2884/* The prepare_to_generate_core target method. */
aef92902 2885
f6ac5f3d
PA
2886void
2887record_btrace_target::prepare_to_generate_core ()
aef92902
MM
2888{
2889 record_btrace_generating_corefile = 1;
2890}
2891
f6ac5f3d 2892/* The done_generating_core target method. */
aef92902 2893
f6ac5f3d
PA
2894void
2895record_btrace_target::done_generating_core ()
aef92902
MM
2896{
2897 record_btrace_generating_corefile = 0;
2898}
2899
f4abbc16
MM
2900/* Start recording in BTS format. */
2901
2902static void
cdb34d4a 2903cmd_record_btrace_bts_start (const char *args, int from_tty)
f4abbc16 2904{
f4abbc16
MM
2905 if (args != NULL && *args != 0)
2906 error (_("Invalid argument."));
2907
2908 record_btrace_conf.format = BTRACE_FORMAT_BTS;
2909
a70b8144 2910 try
492d29ea 2911 {
95a6b0a1 2912 execute_command ("target record-btrace", from_tty);
492d29ea 2913 }
230d2906 2914 catch (const gdb_exception &exception)
f4abbc16
MM
2915 {
2916 record_btrace_conf.format = BTRACE_FORMAT_NONE;
eedc3f4f 2917 throw;
f4abbc16
MM
2918 }
2919}
2920
bc504a31 2921/* Start recording in Intel Processor Trace format. */
afedecd3
MM
2922
2923static void
cdb34d4a 2924cmd_record_btrace_pt_start (const char *args, int from_tty)
afedecd3
MM
2925{
2926 if (args != NULL && *args != 0)
2927 error (_("Invalid argument."));
2928
b20a6524 2929 record_btrace_conf.format = BTRACE_FORMAT_PT;
f4abbc16 2930
a70b8144 2931 try
492d29ea 2932 {
95a6b0a1 2933 execute_command ("target record-btrace", from_tty);
492d29ea 2934 }
230d2906 2935 catch (const gdb_exception &exception)
492d29ea
PA
2936 {
2937 record_btrace_conf.format = BTRACE_FORMAT_NONE;
eedc3f4f 2938 throw;
492d29ea 2939 }
afedecd3
MM
2940}
2941
b20a6524
MM
2942/* Alias for "target record". */
2943
2944static void
981a3fb3 2945cmd_record_btrace_start (const char *args, int from_tty)
b20a6524
MM
2946{
2947 if (args != NULL && *args != 0)
2948 error (_("Invalid argument."));
2949
2950 record_btrace_conf.format = BTRACE_FORMAT_PT;
2951
a70b8144 2952 try
b20a6524 2953 {
95a6b0a1 2954 execute_command ("target record-btrace", from_tty);
b20a6524 2955 }
230d2906 2956 catch (const gdb_exception &exception)
b20a6524
MM
2957 {
2958 record_btrace_conf.format = BTRACE_FORMAT_BTS;
2959
a70b8144 2960 try
b20a6524 2961 {
95a6b0a1 2962 execute_command ("target record-btrace", from_tty);
b20a6524 2963 }
230d2906 2964 catch (const gdb_exception &ex)
b20a6524
MM
2965 {
2966 record_btrace_conf.format = BTRACE_FORMAT_NONE;
eedc3f4f 2967 throw;
b20a6524 2968 }
b20a6524 2969 }
b20a6524
MM
2970}
2971
67b5c0c1
MM
2972/* The "show record btrace replay-memory-access" command. */
2973
2974static void
2975cmd_show_replay_memory_access (struct ui_file *file, int from_tty,
2976 struct cmd_list_element *c, const char *value)
2977{
2978 fprintf_filtered (gdb_stdout, _("Replay memory access is %s.\n"),
2979 replay_memory_access);
2980}
2981
4a4495d6
MM
2982/* The "set record btrace cpu none" command. */
2983
2984static void
2985cmd_set_record_btrace_cpu_none (const char *args, int from_tty)
2986{
2987 if (args != nullptr && *args != 0)
2988 error (_("Trailing junk: '%s'."), args);
2989
2990 record_btrace_cpu_state = CS_NONE;
2991}
2992
2993/* The "set record btrace cpu auto" command. */
2994
2995static void
2996cmd_set_record_btrace_cpu_auto (const char *args, int from_tty)
2997{
2998 if (args != nullptr && *args != 0)
2999 error (_("Trailing junk: '%s'."), args);
3000
3001 record_btrace_cpu_state = CS_AUTO;
3002}
3003
3004/* The "set record btrace cpu" command. */
3005
3006static void
3007cmd_set_record_btrace_cpu (const char *args, int from_tty)
3008{
3009 if (args == nullptr)
3010 args = "";
3011
3012 /* We use a hard-coded vendor string for now. */
3013 unsigned int family, model, stepping;
3014 int l1, l2, matches = sscanf (args, "intel: %u/%u%n/%u%n", &family,
3015 &model, &l1, &stepping, &l2);
3016 if (matches == 3)
3017 {
3018 if (strlen (args) != l2)
3019 error (_("Trailing junk: '%s'."), args + l2);
3020 }
3021 else if (matches == 2)
3022 {
3023 if (strlen (args) != l1)
3024 error (_("Trailing junk: '%s'."), args + l1);
3025
3026 stepping = 0;
3027 }
3028 else
3029 error (_("Bad format. See \"help set record btrace cpu\"."));
3030
3031 if (USHRT_MAX < family)
3032 error (_("Cpu family too big."));
3033
3034 if (UCHAR_MAX < model)
3035 error (_("Cpu model too big."));
3036
3037 if (UCHAR_MAX < stepping)
3038 error (_("Cpu stepping too big."));
3039
3040 record_btrace_cpu.vendor = CV_INTEL;
3041 record_btrace_cpu.family = family;
3042 record_btrace_cpu.model = model;
3043 record_btrace_cpu.stepping = stepping;
3044
3045 record_btrace_cpu_state = CS_CPU;
3046}
3047
3048/* The "show record btrace cpu" command. */
3049
3050static void
3051cmd_show_record_btrace_cpu (const char *args, int from_tty)
3052{
4a4495d6
MM
3053 if (args != nullptr && *args != 0)
3054 error (_("Trailing junk: '%s'."), args);
3055
3056 switch (record_btrace_cpu_state)
3057 {
3058 case CS_AUTO:
3059 printf_unfiltered (_("btrace cpu is 'auto'.\n"));
3060 return;
3061
3062 case CS_NONE:
3063 printf_unfiltered (_("btrace cpu is 'none'.\n"));
3064 return;
3065
3066 case CS_CPU:
3067 switch (record_btrace_cpu.vendor)
3068 {
3069 case CV_INTEL:
3070 if (record_btrace_cpu.stepping == 0)
3071 printf_unfiltered (_("btrace cpu is 'intel: %u/%u'.\n"),
3072 record_btrace_cpu.family,
3073 record_btrace_cpu.model);
3074 else
3075 printf_unfiltered (_("btrace cpu is 'intel: %u/%u/%u'.\n"),
3076 record_btrace_cpu.family,
3077 record_btrace_cpu.model,
3078 record_btrace_cpu.stepping);
3079 return;
3080 }
3081 }
3082
3083 error (_("Internal error: bad cpu state."));
3084}
3085
b20a6524
MM
3086/* The "record bts buffer-size" show value function. */
3087
3088static void
3089show_record_bts_buffer_size_value (struct ui_file *file, int from_tty,
3090 struct cmd_list_element *c,
3091 const char *value)
3092{
3093 fprintf_filtered (file, _("The record/replay bts buffer size is %s.\n"),
3094 value);
3095}
3096
3097/* The "record pt buffer-size" show value function. */
3098
3099static void
3100show_record_pt_buffer_size_value (struct ui_file *file, int from_tty,
3101 struct cmd_list_element *c,
3102 const char *value)
3103{
3104 fprintf_filtered (file, _("The record/replay pt buffer size is %s.\n"),
3105 value);
3106}
3107
afedecd3
MM
3108/* Initialize btrace commands. */
3109
6c265988 3110void _initialize_record_btrace ();
afedecd3 3111void
6c265988 3112_initialize_record_btrace ()
afedecd3 3113{
5e84b7ee
SM
3114 cmd_list_element *record_btrace_cmd
3115 = add_prefix_cmd ("btrace", class_obscure, cmd_record_btrace_start,
3116 _("Start branch trace recording."),
3117 &record_btrace_cmdlist, 0, &record_cmdlist);
3118 add_alias_cmd ("b", record_btrace_cmd, class_obscure, 1, &record_cmdlist);
3119
3120 cmd_list_element *record_btrace_bts_cmd
3121 = add_cmd ("bts", class_obscure, cmd_record_btrace_bts_start,
3122 _("\
f4abbc16
MM
3123Start branch trace recording in Branch Trace Store (BTS) format.\n\n\
3124The processor stores a from/to record for each branch into a cyclic buffer.\n\
3125This format may not be available on all processors."),
5e84b7ee
SM
3126 &record_btrace_cmdlist);
3127 add_alias_cmd ("bts", record_btrace_bts_cmd, class_obscure, 1,
3128 &record_cmdlist);
f4abbc16 3129
5e84b7ee
SM
3130 cmd_list_element *record_btrace_pt_cmd
3131 = add_cmd ("pt", class_obscure, cmd_record_btrace_pt_start,
3132 _("\
bc504a31 3133Start branch trace recording in Intel Processor Trace format.\n\n\
b20a6524 3134This format may not be available on all processors."),
5e84b7ee
SM
3135 &record_btrace_cmdlist);
3136 add_alias_cmd ("pt", record_btrace_pt_cmd, class_obscure, 1, &record_cmdlist);
b20a6524 3137
0743fc83
TT
3138 add_basic_prefix_cmd ("btrace", class_support,
3139 _("Set record options."), &set_record_btrace_cmdlist,
2f822da5 3140 0, &set_record_cmdlist);
67b5c0c1 3141
0743fc83
TT
3142 add_show_prefix_cmd ("btrace", class_support,
3143 _("Show record options."), &show_record_btrace_cmdlist,
2f822da5 3144 0, &show_record_cmdlist);
67b5c0c1
MM
3145
3146 add_setshow_enum_cmd ("replay-memory-access", no_class,
3147 replay_memory_access_types, &replay_memory_access, _("\
3148Set what memory accesses are allowed during replay."), _("\
3149Show what memory accesses are allowed during replay."),
3150 _("Default is READ-ONLY.\n\n\
3151The btrace record target does not trace data.\n\
3152The memory therefore corresponds to the live target and not \
3153to the current replay position.\n\n\
3154When READ-ONLY, allow accesses to read-only memory during replay.\n\
3155When READ-WRITE, allow accesses to read-only and read-write memory during \
3156replay."),
3157 NULL, cmd_show_replay_memory_access,
3158 &set_record_btrace_cmdlist,
3159 &show_record_btrace_cmdlist);
3160
4a4495d6
MM
3161 add_prefix_cmd ("cpu", class_support, cmd_set_record_btrace_cpu,
3162 _("\
3163Set the cpu to be used for trace decode.\n\n\
55063ddb
TT
3164The format is \"VENDOR:IDENTIFIER\" or \"none\" or \"auto\" (default).\n\
3165For vendor \"intel\" the format is \"FAMILY/MODEL[/STEPPING]\".\n\n\
4a4495d6
MM
3166When decoding branch trace, enable errata workarounds for the specified cpu.\n\
3167The default is \"auto\", which uses the cpu on which the trace was recorded.\n\
3168When GDB does not support that cpu, this option can be used to enable\n\
3169workarounds for a similar cpu that GDB supports.\n\n\
3170When set to \"none\", errata workarounds are disabled."),
3171 &set_record_btrace_cpu_cmdlist,
2f822da5 3172 1,
4a4495d6
MM
3173 &set_record_btrace_cmdlist);
3174
3175 add_cmd ("auto", class_support, cmd_set_record_btrace_cpu_auto, _("\
3176Automatically determine the cpu to be used for trace decode."),
3177 &set_record_btrace_cpu_cmdlist);
3178
3179 add_cmd ("none", class_support, cmd_set_record_btrace_cpu_none, _("\
3180Do not enable errata workarounds for trace decode."),
3181 &set_record_btrace_cpu_cmdlist);
3182
3183 add_cmd ("cpu", class_support, cmd_show_record_btrace_cpu, _("\
3184Show the cpu to be used for trace decode."),
3185 &show_record_btrace_cmdlist);
3186
0743fc83
TT
3187 add_basic_prefix_cmd ("bts", class_support,
3188 _("Set record btrace bts options."),
3189 &set_record_btrace_bts_cmdlist,
2f822da5 3190 0,
0743fc83 3191 &set_record_btrace_cmdlist);
d33501a5 3192
0743fc83
TT
3193 add_show_prefix_cmd ("bts", class_support,
3194 _("Show record btrace bts options."),
3195 &show_record_btrace_bts_cmdlist,
2f822da5 3196 0,
0743fc83 3197 &show_record_btrace_cmdlist);
d33501a5
MM
3198
3199 add_setshow_uinteger_cmd ("buffer-size", no_class,
3200 &record_btrace_conf.bts.size,
3201 _("Set the record/replay bts buffer size."),
3202 _("Show the record/replay bts buffer size."), _("\
3203When starting recording request a trace buffer of this size. \
3204The actual buffer size may differ from the requested size. \
3205Use \"info record\" to see the actual buffer size.\n\n\
3206Bigger buffers allow longer recording but also take more time to process \
3207the recorded execution trace.\n\n\
b20a6524
MM
3208The trace buffer size may not be changed while recording."), NULL,
3209 show_record_bts_buffer_size_value,
d33501a5
MM
3210 &set_record_btrace_bts_cmdlist,
3211 &show_record_btrace_bts_cmdlist);
3212
0743fc83
TT
3213 add_basic_prefix_cmd ("pt", class_support,
3214 _("Set record btrace pt options."),
3215 &set_record_btrace_pt_cmdlist,
2f822da5 3216 0,
0743fc83
TT
3217 &set_record_btrace_cmdlist);
3218
3219 add_show_prefix_cmd ("pt", class_support,
3220 _("Show record btrace pt options."),
3221 &show_record_btrace_pt_cmdlist,
2f822da5 3222 0,
0743fc83 3223 &show_record_btrace_cmdlist);
b20a6524
MM
3224
3225 add_setshow_uinteger_cmd ("buffer-size", no_class,
3226 &record_btrace_conf.pt.size,
3227 _("Set the record/replay pt buffer size."),
3228 _("Show the record/replay pt buffer size."), _("\
3229Bigger buffers allow longer recording but also take more time to process \
3230the recorded execution.\n\
3231The actual buffer size may differ from the requested size. Use \"info record\" \
3232to see the actual buffer size."), NULL, show_record_pt_buffer_size_value,
3233 &set_record_btrace_pt_cmdlist,
3234 &show_record_btrace_pt_cmdlist);
3235
d9f719f1 3236 add_target (record_btrace_target_info, record_btrace_target_open);
0b722aec
MM
3237
3238 bfcache = htab_create_alloc (50, bfcache_hash, bfcache_eq, NULL,
3239 xcalloc, xfree);
d33501a5
MM
3240
3241 record_btrace_conf.bts.size = 64 * 1024;
b20a6524 3242 record_btrace_conf.pt.size = 16 * 1024;
afedecd3 3243}
This page took 1.121286 seconds and 4 git commands to generate.