1 /* Branch trace support for GDB, the GNU debugger.
3 Copyright (C) 2013-2020 Free Software Foundation, Inc.
5 Contributed by Intel Corp. <markus.t.metzger@intel.com>
7 This file is part of GDB.
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.
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.
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/>. */
24 #include "record-btrace.h"
25 #include "gdbthread.h"
29 #include "observable.h"
30 #include "cli/cli-utils.h"
34 #include "filenames.h"
36 #include "frame-unwind.h"
39 #include "event-loop.h"
44 #include "cli/cli-style.h"
46 static const target_info record_btrace_target_info
= {
48 N_("Branch tracing target"),
49 N_("Collect control-flow trace and provide the execution history.")
52 /* The target_ops of record-btrace. */
54 class record_btrace_target final
: public target_ops
57 const target_info
&info () const override
58 { return record_btrace_target_info
; }
60 strata
stratum () const override
{ return record_stratum
; }
62 void close () override
;
63 void async (int) override
;
65 void detach (inferior
*inf
, int from_tty
) override
66 { record_detach (this, inf
, from_tty
); }
68 void disconnect (const char *, int) override
;
70 void mourn_inferior () override
71 { record_mourn_inferior (this); }
74 { record_kill (this); }
76 enum record_method
record_method (ptid_t ptid
) override
;
78 void stop_recording () override
;
79 void info_record () override
;
81 void insn_history (int size
, gdb_disassembly_flags flags
) override
;
82 void insn_history_from (ULONGEST from
, int size
,
83 gdb_disassembly_flags flags
) override
;
84 void insn_history_range (ULONGEST begin
, ULONGEST end
,
85 gdb_disassembly_flags flags
) override
;
86 void call_history (int size
, record_print_flags flags
) override
;
87 void call_history_from (ULONGEST begin
, int size
, record_print_flags flags
)
89 void call_history_range (ULONGEST begin
, ULONGEST end
, record_print_flags flags
)
92 bool record_is_replaying (ptid_t ptid
) override
;
93 bool record_will_replay (ptid_t ptid
, int dir
) override
;
94 void record_stop_replaying () override
;
96 enum target_xfer_status
xfer_partial (enum target_object object
,
99 const gdb_byte
*writebuf
,
100 ULONGEST offset
, ULONGEST len
,
101 ULONGEST
*xfered_len
) override
;
103 int insert_breakpoint (struct gdbarch
*,
104 struct bp_target_info
*) override
;
105 int remove_breakpoint (struct gdbarch
*, struct bp_target_info
*,
106 enum remove_bp_reason
) override
;
108 void fetch_registers (struct regcache
*, int) override
;
110 void store_registers (struct regcache
*, int) override
;
111 void prepare_to_store (struct regcache
*) override
;
113 const struct frame_unwind
*get_unwinder () override
;
115 const struct frame_unwind
*get_tailcall_unwinder () override
;
117 void commit_resume () override
;
118 void resume (ptid_t
, int, enum gdb_signal
) override
;
119 ptid_t
wait (ptid_t
, struct target_waitstatus
*, int) override
;
121 void stop (ptid_t
) override
;
122 void update_thread_list () override
;
123 bool thread_alive (ptid_t ptid
) override
;
124 void goto_record_begin () override
;
125 void goto_record_end () override
;
126 void goto_record (ULONGEST insn
) override
;
128 bool can_execute_reverse () override
;
130 bool stopped_by_sw_breakpoint () override
;
131 bool supports_stopped_by_sw_breakpoint () override
;
133 bool stopped_by_hw_breakpoint () override
;
134 bool supports_stopped_by_hw_breakpoint () override
;
136 enum exec_direction_kind
execution_direction () override
;
137 void prepare_to_generate_core () override
;
138 void done_generating_core () override
;
141 static record_btrace_target record_btrace_ops
;
143 /* Initialize the record-btrace target ops. */
145 /* Token associated with a new-thread observer enabling branch tracing
146 for the new thread. */
147 static const gdb::observers::token record_btrace_thread_observer_token
{};
149 /* Memory access types used in set/show record btrace replay-memory-access. */
150 static const char replay_memory_access_read_only
[] = "read-only";
151 static const char replay_memory_access_read_write
[] = "read-write";
152 static const char *const replay_memory_access_types
[] =
154 replay_memory_access_read_only
,
155 replay_memory_access_read_write
,
159 /* The currently allowed replay memory access type. */
160 static const char *replay_memory_access
= replay_memory_access_read_only
;
162 /* The cpu state kinds. */
163 enum record_btrace_cpu_state_kind
170 /* The current cpu state. */
171 static enum record_btrace_cpu_state_kind record_btrace_cpu_state
= CS_AUTO
;
173 /* The current cpu for trace decode. */
174 static struct btrace_cpu record_btrace_cpu
;
176 /* Command lists for "set/show record btrace". */
177 static struct cmd_list_element
*set_record_btrace_cmdlist
;
178 static struct cmd_list_element
*show_record_btrace_cmdlist
;
180 /* The execution direction of the last resume we got. See record-full.c. */
181 static enum exec_direction_kind record_btrace_resume_exec_dir
= EXEC_FORWARD
;
183 /* The async event handler for reverse/replay execution. */
184 static struct async_event_handler
*record_btrace_async_inferior_event_handler
;
186 /* A flag indicating that we are currently generating a core file. */
187 static int record_btrace_generating_corefile
;
189 /* The current branch trace configuration. */
190 static struct btrace_config record_btrace_conf
;
192 /* Command list for "record btrace". */
193 static struct cmd_list_element
*record_btrace_cmdlist
;
195 /* Command lists for "set/show record btrace bts". */
196 static struct cmd_list_element
*set_record_btrace_bts_cmdlist
;
197 static struct cmd_list_element
*show_record_btrace_bts_cmdlist
;
199 /* Command lists for "set/show record btrace pt". */
200 static struct cmd_list_element
*set_record_btrace_pt_cmdlist
;
201 static struct cmd_list_element
*show_record_btrace_pt_cmdlist
;
203 /* Command list for "set record btrace cpu". */
204 static struct cmd_list_element
*set_record_btrace_cpu_cmdlist
;
206 /* Print a record-btrace debug message. Use do ... while (0) to avoid
207 ambiguities when used in if statements. */
209 #define DEBUG(msg, args...) \
212 if (record_debug != 0) \
213 fprintf_unfiltered (gdb_stdlog, \
214 "[record-btrace] " msg "\n", ##args); \
219 /* Return the cpu configured by the user. Returns NULL if the cpu was
220 configured as auto. */
221 const struct btrace_cpu
*
222 record_btrace_get_cpu (void)
224 switch (record_btrace_cpu_state
)
230 record_btrace_cpu
.vendor
= CV_UNKNOWN
;
233 return &record_btrace_cpu
;
236 error (_("Internal error: bad record btrace cpu state."));
239 /* Update the branch trace for the current thread and return a pointer to its
242 Throws an error if there is no thread or no trace. This function never
245 static struct thread_info
*
246 require_btrace_thread (void)
250 if (inferior_ptid
== null_ptid
)
251 error (_("No thread."));
253 thread_info
*tp
= inferior_thread ();
255 validate_registers_access ();
257 btrace_fetch (tp
, record_btrace_get_cpu ());
259 if (btrace_is_empty (tp
))
260 error (_("No trace."));
265 /* Update the branch trace for the current thread and return a pointer to its
266 branch trace information struct.
268 Throws an error if there is no thread or no trace. This function never
271 static struct btrace_thread_info
*
272 require_btrace (void)
274 struct thread_info
*tp
;
276 tp
= require_btrace_thread ();
281 /* Enable branch tracing for one thread. Warn on errors. */
284 record_btrace_enable_warn (struct thread_info
*tp
)
288 btrace_enable (tp
, &record_btrace_conf
);
290 catch (const gdb_exception_error
&error
)
292 warning ("%s", error
.what ());
296 /* Enable automatic tracing of new threads. */
299 record_btrace_auto_enable (void)
301 DEBUG ("attach thread observer");
303 gdb::observers::new_thread
.attach (record_btrace_enable_warn
,
304 record_btrace_thread_observer_token
);
307 /* Disable automatic tracing of new threads. */
310 record_btrace_auto_disable (void)
312 DEBUG ("detach thread observer");
314 gdb::observers::new_thread
.detach (record_btrace_thread_observer_token
);
317 /* The record-btrace async event handler function. */
320 record_btrace_handle_async_inferior_event (gdb_client_data data
)
322 inferior_event_handler (INF_REG_EVENT
, NULL
);
325 /* See record-btrace.h. */
328 record_btrace_push_target (void)
332 record_btrace_auto_enable ();
334 push_target (&record_btrace_ops
);
336 record_btrace_async_inferior_event_handler
337 = create_async_event_handler (record_btrace_handle_async_inferior_event
,
339 record_btrace_generating_corefile
= 0;
341 format
= btrace_format_short_string (record_btrace_conf
.format
);
342 gdb::observers::record_changed
.notify (current_inferior (), 1, "btrace", format
);
345 /* Disable btrace on a set of threads on scope exit. */
347 struct scoped_btrace_disable
349 scoped_btrace_disable () = default;
351 DISABLE_COPY_AND_ASSIGN (scoped_btrace_disable
);
353 ~scoped_btrace_disable ()
355 for (thread_info
*tp
: m_threads
)
359 void add_thread (thread_info
*thread
)
361 m_threads
.push_front (thread
);
370 std::forward_list
<thread_info
*> m_threads
;
373 /* Open target record-btrace. */
376 record_btrace_target_open (const char *args
, int from_tty
)
378 /* If we fail to enable btrace for one thread, disable it for the threads for
379 which it was successfully enabled. */
380 scoped_btrace_disable btrace_disable
;
386 if (!target_has_execution
)
387 error (_("The program is not being run."));
389 for (thread_info
*tp
: all_non_exited_threads ())
390 if (args
== NULL
|| *args
== 0 || number_is_in_list (args
, tp
->global_num
))
392 btrace_enable (tp
, &record_btrace_conf
);
394 btrace_disable
.add_thread (tp
);
397 record_btrace_push_target ();
399 btrace_disable
.discard ();
402 /* The stop_recording method of target record-btrace. */
405 record_btrace_target::stop_recording ()
407 DEBUG ("stop recording");
409 record_btrace_auto_disable ();
411 for (thread_info
*tp
: all_non_exited_threads ())
412 if (tp
->btrace
.target
!= NULL
)
416 /* The disconnect method of target record-btrace. */
419 record_btrace_target::disconnect (const char *args
,
422 struct target_ops
*beneath
= this->beneath ();
424 /* Do not stop recording, just clean up GDB side. */
425 unpush_target (this);
427 /* Forward disconnect. */
428 beneath
->disconnect (args
, from_tty
);
431 /* The close method of target record-btrace. */
434 record_btrace_target::close ()
436 if (record_btrace_async_inferior_event_handler
!= NULL
)
437 delete_async_event_handler (&record_btrace_async_inferior_event_handler
);
439 /* Make sure automatic recording gets disabled even if we did not stop
440 recording before closing the record-btrace target. */
441 record_btrace_auto_disable ();
443 /* We should have already stopped recording.
444 Tear down btrace in case we have not. */
445 for (thread_info
*tp
: all_non_exited_threads ())
446 btrace_teardown (tp
);
449 /* The async method of target record-btrace. */
452 record_btrace_target::async (int enable
)
455 mark_async_event_handler (record_btrace_async_inferior_event_handler
);
457 clear_async_event_handler (record_btrace_async_inferior_event_handler
);
459 this->beneath ()->async (enable
);
462 /* Adjusts the size and returns a human readable size suffix. */
465 record_btrace_adjust_size (unsigned int *size
)
471 if ((sz
& ((1u << 30) - 1)) == 0)
476 else if ((sz
& ((1u << 20) - 1)) == 0)
481 else if ((sz
& ((1u << 10) - 1)) == 0)
490 /* Print a BTS configuration. */
493 record_btrace_print_bts_conf (const struct btrace_config_bts
*conf
)
501 suffix
= record_btrace_adjust_size (&size
);
502 printf_unfiltered (_("Buffer size: %u%s.\n"), size
, suffix
);
506 /* Print an Intel Processor Trace configuration. */
509 record_btrace_print_pt_conf (const struct btrace_config_pt
*conf
)
517 suffix
= record_btrace_adjust_size (&size
);
518 printf_unfiltered (_("Buffer size: %u%s.\n"), size
, suffix
);
522 /* Print a branch tracing configuration. */
525 record_btrace_print_conf (const struct btrace_config
*conf
)
527 printf_unfiltered (_("Recording format: %s.\n"),
528 btrace_format_string (conf
->format
));
530 switch (conf
->format
)
532 case BTRACE_FORMAT_NONE
:
535 case BTRACE_FORMAT_BTS
:
536 record_btrace_print_bts_conf (&conf
->bts
);
539 case BTRACE_FORMAT_PT
:
540 record_btrace_print_pt_conf (&conf
->pt
);
544 internal_error (__FILE__
, __LINE__
, _("Unkown branch trace format."));
547 /* The info_record method of target record-btrace. */
550 record_btrace_target::info_record ()
552 struct btrace_thread_info
*btinfo
;
553 const struct btrace_config
*conf
;
554 struct thread_info
*tp
;
555 unsigned int insns
, calls
, gaps
;
559 tp
= find_thread_ptid (inferior_ptid
);
561 error (_("No thread."));
563 validate_registers_access ();
565 btinfo
= &tp
->btrace
;
567 conf
= ::btrace_conf (btinfo
);
569 record_btrace_print_conf (conf
);
571 btrace_fetch (tp
, record_btrace_get_cpu ());
577 if (!btrace_is_empty (tp
))
579 struct btrace_call_iterator call
;
580 struct btrace_insn_iterator insn
;
582 btrace_call_end (&call
, btinfo
);
583 btrace_call_prev (&call
, 1);
584 calls
= btrace_call_number (&call
);
586 btrace_insn_end (&insn
, btinfo
);
587 insns
= btrace_insn_number (&insn
);
589 /* If the last instruction is not a gap, it is the current instruction
590 that is not actually part of the record. */
591 if (btrace_insn_get (&insn
) != NULL
)
594 gaps
= btinfo
->ngaps
;
597 printf_unfiltered (_("Recorded %u instructions in %u functions (%u gaps) "
598 "for thread %s (%s).\n"), insns
, calls
, gaps
,
599 print_thread_id (tp
),
600 target_pid_to_str (tp
->ptid
).c_str ());
602 if (btrace_is_replaying (tp
))
603 printf_unfiltered (_("Replay in progress. At instruction %u.\n"),
604 btrace_insn_number (btinfo
->replay
));
607 /* Print a decode error. */
610 btrace_ui_out_decode_error (struct ui_out
*uiout
, int errcode
,
611 enum btrace_format format
)
613 const char *errstr
= btrace_decode_error (format
, errcode
);
615 uiout
->text (_("["));
616 /* ERRCODE > 0 indicates notifications on BTRACE_FORMAT_PT. */
617 if (!(format
== BTRACE_FORMAT_PT
&& errcode
> 0))
619 uiout
->text (_("decode error ("));
620 uiout
->field_signed ("errcode", errcode
);
621 uiout
->text (_("): "));
623 uiout
->text (errstr
);
624 uiout
->text (_("]\n"));
627 /* A range of source lines. */
629 struct btrace_line_range
631 /* The symtab this line is from. */
632 struct symtab
*symtab
;
634 /* The first line (inclusive). */
637 /* The last line (exclusive). */
641 /* Construct a line range. */
643 static struct btrace_line_range
644 btrace_mk_line_range (struct symtab
*symtab
, int begin
, int end
)
646 struct btrace_line_range range
;
648 range
.symtab
= symtab
;
655 /* Add a line to a line range. */
657 static struct btrace_line_range
658 btrace_line_range_add (struct btrace_line_range range
, int line
)
660 if (range
.end
<= range
.begin
)
662 /* This is the first entry. */
664 range
.end
= line
+ 1;
666 else if (line
< range
.begin
)
668 else if (range
.end
< line
)
674 /* Return non-zero if RANGE is empty, zero otherwise. */
677 btrace_line_range_is_empty (struct btrace_line_range range
)
679 return range
.end
<= range
.begin
;
682 /* Return non-zero if LHS contains RHS, zero otherwise. */
685 btrace_line_range_contains_range (struct btrace_line_range lhs
,
686 struct btrace_line_range rhs
)
688 return ((lhs
.symtab
== rhs
.symtab
)
689 && (lhs
.begin
<= rhs
.begin
)
690 && (rhs
.end
<= lhs
.end
));
693 /* Find the line range associated with PC. */
695 static struct btrace_line_range
696 btrace_find_line_range (CORE_ADDR pc
)
698 struct btrace_line_range range
;
699 struct linetable_entry
*lines
;
700 struct linetable
*ltable
;
701 struct symtab
*symtab
;
704 symtab
= find_pc_line_symtab (pc
);
706 return btrace_mk_line_range (NULL
, 0, 0);
708 ltable
= SYMTAB_LINETABLE (symtab
);
710 return btrace_mk_line_range (symtab
, 0, 0);
712 nlines
= ltable
->nitems
;
713 lines
= ltable
->item
;
715 return btrace_mk_line_range (symtab
, 0, 0);
717 range
= btrace_mk_line_range (symtab
, 0, 0);
718 for (i
= 0; i
< nlines
- 1; i
++)
720 if ((lines
[i
].pc
== pc
) && (lines
[i
].line
!= 0))
721 range
= btrace_line_range_add (range
, lines
[i
].line
);
727 /* Print source lines in LINES to UIOUT.
729 UI_ITEM_CHAIN is a cleanup chain for the last source line and the
730 instructions corresponding to that source line. When printing a new source
731 line, we do the cleanups for the open chain and open a new cleanup chain for
732 the new source line. If the source line range in LINES is not empty, this
733 function will leave the cleanup chain for the last printed source line open
734 so instructions can be added to it. */
737 btrace_print_lines (struct btrace_line_range lines
, struct ui_out
*uiout
,
738 gdb::optional
<ui_out_emit_tuple
> *src_and_asm_tuple
,
739 gdb::optional
<ui_out_emit_list
> *asm_list
,
740 gdb_disassembly_flags flags
)
742 print_source_lines_flags psl_flags
;
744 if (flags
& DISASSEMBLY_FILENAME
)
745 psl_flags
|= PRINT_SOURCE_LINES_FILENAME
;
747 for (int line
= lines
.begin
; line
< lines
.end
; ++line
)
751 src_and_asm_tuple
->emplace (uiout
, "src_and_asm_line");
753 print_source_lines (lines
.symtab
, line
, line
+ 1, psl_flags
);
755 asm_list
->emplace (uiout
, "line_asm_insn");
759 /* Disassemble a section of the recorded instruction trace. */
762 btrace_insn_history (struct ui_out
*uiout
,
763 const struct btrace_thread_info
*btinfo
,
764 const struct btrace_insn_iterator
*begin
,
765 const struct btrace_insn_iterator
*end
,
766 gdb_disassembly_flags flags
)
768 DEBUG ("itrace (0x%x): [%u; %u)", (unsigned) flags
,
769 btrace_insn_number (begin
), btrace_insn_number (end
));
771 flags
|= DISASSEMBLY_SPECULATIVE
;
773 struct gdbarch
*gdbarch
= target_gdbarch ();
774 btrace_line_range last_lines
= btrace_mk_line_range (NULL
, 0, 0);
776 ui_out_emit_list
list_emitter (uiout
, "asm_insns");
778 gdb::optional
<ui_out_emit_tuple
> src_and_asm_tuple
;
779 gdb::optional
<ui_out_emit_list
> asm_list
;
781 gdb_pretty_print_disassembler
disasm (gdbarch
, uiout
);
783 for (btrace_insn_iterator it
= *begin
; btrace_insn_cmp (&it
, end
) != 0;
784 btrace_insn_next (&it
, 1))
786 const struct btrace_insn
*insn
;
788 insn
= btrace_insn_get (&it
);
790 /* A NULL instruction indicates a gap in the trace. */
793 const struct btrace_config
*conf
;
795 conf
= btrace_conf (btinfo
);
797 /* We have trace so we must have a configuration. */
798 gdb_assert (conf
!= NULL
);
800 uiout
->field_fmt ("insn-number", "%u",
801 btrace_insn_number (&it
));
804 btrace_ui_out_decode_error (uiout
, btrace_insn_get_error (&it
),
809 struct disasm_insn dinsn
;
811 if ((flags
& DISASSEMBLY_SOURCE
) != 0)
813 struct btrace_line_range lines
;
815 lines
= btrace_find_line_range (insn
->pc
);
816 if (!btrace_line_range_is_empty (lines
)
817 && !btrace_line_range_contains_range (last_lines
, lines
))
819 btrace_print_lines (lines
, uiout
, &src_and_asm_tuple
, &asm_list
,
823 else if (!src_and_asm_tuple
.has_value ())
825 gdb_assert (!asm_list
.has_value ());
827 src_and_asm_tuple
.emplace (uiout
, "src_and_asm_line");
829 /* No source information. */
830 asm_list
.emplace (uiout
, "line_asm_insn");
833 gdb_assert (src_and_asm_tuple
.has_value ());
834 gdb_assert (asm_list
.has_value ());
837 memset (&dinsn
, 0, sizeof (dinsn
));
838 dinsn
.number
= btrace_insn_number (&it
);
839 dinsn
.addr
= insn
->pc
;
841 if ((insn
->flags
& BTRACE_INSN_FLAG_SPECULATIVE
) != 0)
842 dinsn
.is_speculative
= 1;
844 disasm
.pretty_print_insn (&dinsn
, flags
);
849 /* The insn_history method of target record-btrace. */
852 record_btrace_target::insn_history (int size
, gdb_disassembly_flags flags
)
854 struct btrace_thread_info
*btinfo
;
855 struct btrace_insn_history
*history
;
856 struct btrace_insn_iterator begin
, end
;
857 struct ui_out
*uiout
;
858 unsigned int context
, covered
;
860 uiout
= current_uiout
;
861 ui_out_emit_tuple
tuple_emitter (uiout
, "insn history");
862 context
= abs (size
);
864 error (_("Bad record instruction-history-size."));
866 btinfo
= require_btrace ();
867 history
= btinfo
->insn_history
;
870 struct btrace_insn_iterator
*replay
;
872 DEBUG ("insn-history (0x%x): %d", (unsigned) flags
, size
);
874 /* If we're replaying, we start at the replay position. Otherwise, we
875 start at the tail of the trace. */
876 replay
= btinfo
->replay
;
880 btrace_insn_end (&begin
, btinfo
);
882 /* We start from here and expand in the requested direction. Then we
883 expand in the other direction, as well, to fill up any remaining
888 /* We want the current position covered, as well. */
889 covered
= btrace_insn_next (&end
, 1);
890 covered
+= btrace_insn_prev (&begin
, context
- covered
);
891 covered
+= btrace_insn_next (&end
, context
- covered
);
895 covered
= btrace_insn_next (&end
, context
);
896 covered
+= btrace_insn_prev (&begin
, context
- covered
);
901 begin
= history
->begin
;
904 DEBUG ("insn-history (0x%x): %d, prev: [%u; %u)", (unsigned) flags
, size
,
905 btrace_insn_number (&begin
), btrace_insn_number (&end
));
910 covered
= btrace_insn_prev (&begin
, context
);
915 covered
= btrace_insn_next (&end
, context
);
920 btrace_insn_history (uiout
, btinfo
, &begin
, &end
, flags
);
924 printf_unfiltered (_("At the start of the branch trace record.\n"));
926 printf_unfiltered (_("At the end of the branch trace record.\n"));
929 btrace_set_insn_history (btinfo
, &begin
, &end
);
932 /* The insn_history_range method of target record-btrace. */
935 record_btrace_target::insn_history_range (ULONGEST from
, ULONGEST to
,
936 gdb_disassembly_flags flags
)
938 struct btrace_thread_info
*btinfo
;
939 struct btrace_insn_iterator begin
, end
;
940 struct ui_out
*uiout
;
941 unsigned int low
, high
;
944 uiout
= current_uiout
;
945 ui_out_emit_tuple
tuple_emitter (uiout
, "insn history");
949 DEBUG ("insn-history (0x%x): [%u; %u)", (unsigned) flags
, low
, high
);
951 /* Check for wrap-arounds. */
952 if (low
!= from
|| high
!= to
)
953 error (_("Bad range."));
956 error (_("Bad range."));
958 btinfo
= require_btrace ();
960 found
= btrace_find_insn_by_number (&begin
, btinfo
, low
);
962 error (_("Range out of bounds."));
964 found
= btrace_find_insn_by_number (&end
, btinfo
, high
);
967 /* Silently truncate the range. */
968 btrace_insn_end (&end
, btinfo
);
972 /* We want both begin and end to be inclusive. */
973 btrace_insn_next (&end
, 1);
976 btrace_insn_history (uiout
, btinfo
, &begin
, &end
, flags
);
977 btrace_set_insn_history (btinfo
, &begin
, &end
);
980 /* The insn_history_from method of target record-btrace. */
983 record_btrace_target::insn_history_from (ULONGEST from
, int size
,
984 gdb_disassembly_flags flags
)
986 ULONGEST begin
, end
, context
;
988 context
= abs (size
);
990 error (_("Bad record instruction-history-size."));
999 begin
= from
- context
+ 1;
1004 end
= from
+ context
- 1;
1006 /* Check for wrap-around. */
1011 insn_history_range (begin
, end
, flags
);
1014 /* Print the instruction number range for a function call history line. */
1017 btrace_call_history_insn_range (struct ui_out
*uiout
,
1018 const struct btrace_function
*bfun
)
1020 unsigned int begin
, end
, size
;
1022 size
= bfun
->insn
.size ();
1023 gdb_assert (size
> 0);
1025 begin
= bfun
->insn_offset
;
1026 end
= begin
+ size
- 1;
1028 uiout
->field_unsigned ("insn begin", begin
);
1030 uiout
->field_unsigned ("insn end", end
);
1033 /* Compute the lowest and highest source line for the instructions in BFUN
1034 and return them in PBEGIN and PEND.
1035 Ignore instructions that can't be mapped to BFUN, e.g. instructions that
1036 result from inlining or macro expansion. */
1039 btrace_compute_src_line_range (const struct btrace_function
*bfun
,
1040 int *pbegin
, int *pend
)
1042 struct symtab
*symtab
;
1053 symtab
= symbol_symtab (sym
);
1055 for (const btrace_insn
&insn
: bfun
->insn
)
1057 struct symtab_and_line sal
;
1059 sal
= find_pc_line (insn
.pc
, 0);
1060 if (sal
.symtab
!= symtab
|| sal
.line
== 0)
1063 begin
= std::min (begin
, sal
.line
);
1064 end
= std::max (end
, sal
.line
);
1072 /* Print the source line information for a function call history line. */
1075 btrace_call_history_src_line (struct ui_out
*uiout
,
1076 const struct btrace_function
*bfun
)
1085 uiout
->field_string ("file",
1086 symtab_to_filename_for_display (symbol_symtab (sym
)),
1087 file_name_style
.style ());
1089 btrace_compute_src_line_range (bfun
, &begin
, &end
);
1094 uiout
->field_signed ("min line", begin
);
1100 uiout
->field_signed ("max line", end
);
1103 /* Get the name of a branch trace function. */
1106 btrace_get_bfun_name (const struct btrace_function
*bfun
)
1108 struct minimal_symbol
*msym
;
1118 return sym
->print_name ();
1119 else if (msym
!= NULL
)
1120 return msym
->print_name ();
1125 /* Disassemble a section of the recorded function trace. */
1128 btrace_call_history (struct ui_out
*uiout
,
1129 const struct btrace_thread_info
*btinfo
,
1130 const struct btrace_call_iterator
*begin
,
1131 const struct btrace_call_iterator
*end
,
1134 struct btrace_call_iterator it
;
1135 record_print_flags flags
= (enum record_print_flag
) int_flags
;
1137 DEBUG ("ftrace (0x%x): [%u; %u)", int_flags
, btrace_call_number (begin
),
1138 btrace_call_number (end
));
1140 for (it
= *begin
; btrace_call_cmp (&it
, end
) < 0; btrace_call_next (&it
, 1))
1142 const struct btrace_function
*bfun
;
1143 struct minimal_symbol
*msym
;
1146 bfun
= btrace_call_get (&it
);
1150 /* Print the function index. */
1151 uiout
->field_unsigned ("index", bfun
->number
);
1154 /* Indicate gaps in the trace. */
1155 if (bfun
->errcode
!= 0)
1157 const struct btrace_config
*conf
;
1159 conf
= btrace_conf (btinfo
);
1161 /* We have trace so we must have a configuration. */
1162 gdb_assert (conf
!= NULL
);
1164 btrace_ui_out_decode_error (uiout
, bfun
->errcode
, conf
->format
);
1169 if ((flags
& RECORD_PRINT_INDENT_CALLS
) != 0)
1171 int level
= bfun
->level
+ btinfo
->level
, i
;
1173 for (i
= 0; i
< level
; ++i
)
1178 uiout
->field_string ("function", sym
->print_name (),
1179 function_name_style
.style ());
1180 else if (msym
!= NULL
)
1181 uiout
->field_string ("function", msym
->print_name (),
1182 function_name_style
.style ());
1183 else if (!uiout
->is_mi_like_p ())
1184 uiout
->field_string ("function", "??",
1185 function_name_style
.style ());
1187 if ((flags
& RECORD_PRINT_INSN_RANGE
) != 0)
1189 uiout
->text (_("\tinst "));
1190 btrace_call_history_insn_range (uiout
, bfun
);
1193 if ((flags
& RECORD_PRINT_SRC_LINE
) != 0)
1195 uiout
->text (_("\tat "));
1196 btrace_call_history_src_line (uiout
, bfun
);
1203 /* The call_history method of target record-btrace. */
1206 record_btrace_target::call_history (int size
, record_print_flags flags
)
1208 struct btrace_thread_info
*btinfo
;
1209 struct btrace_call_history
*history
;
1210 struct btrace_call_iterator begin
, end
;
1211 struct ui_out
*uiout
;
1212 unsigned int context
, covered
;
1214 uiout
= current_uiout
;
1215 ui_out_emit_tuple
tuple_emitter (uiout
, "insn history");
1216 context
= abs (size
);
1218 error (_("Bad record function-call-history-size."));
1220 btinfo
= require_btrace ();
1221 history
= btinfo
->call_history
;
1222 if (history
== NULL
)
1224 struct btrace_insn_iterator
*replay
;
1226 DEBUG ("call-history (0x%x): %d", (int) flags
, size
);
1228 /* If we're replaying, we start at the replay position. Otherwise, we
1229 start at the tail of the trace. */
1230 replay
= btinfo
->replay
;
1233 begin
.btinfo
= btinfo
;
1234 begin
.index
= replay
->call_index
;
1237 btrace_call_end (&begin
, btinfo
);
1239 /* We start from here and expand in the requested direction. Then we
1240 expand in the other direction, as well, to fill up any remaining
1245 /* We want the current position covered, as well. */
1246 covered
= btrace_call_next (&end
, 1);
1247 covered
+= btrace_call_prev (&begin
, context
- covered
);
1248 covered
+= btrace_call_next (&end
, context
- covered
);
1252 covered
= btrace_call_next (&end
, context
);
1253 covered
+= btrace_call_prev (&begin
, context
- covered
);
1258 begin
= history
->begin
;
1261 DEBUG ("call-history (0x%x): %d, prev: [%u; %u)", (int) flags
, size
,
1262 btrace_call_number (&begin
), btrace_call_number (&end
));
1267 covered
= btrace_call_prev (&begin
, context
);
1272 covered
= btrace_call_next (&end
, context
);
1277 btrace_call_history (uiout
, btinfo
, &begin
, &end
, flags
);
1281 printf_unfiltered (_("At the start of the branch trace record.\n"));
1283 printf_unfiltered (_("At the end of the branch trace record.\n"));
1286 btrace_set_call_history (btinfo
, &begin
, &end
);
1289 /* The call_history_range method of target record-btrace. */
1292 record_btrace_target::call_history_range (ULONGEST from
, ULONGEST to
,
1293 record_print_flags flags
)
1295 struct btrace_thread_info
*btinfo
;
1296 struct btrace_call_iterator begin
, end
;
1297 struct ui_out
*uiout
;
1298 unsigned int low
, high
;
1301 uiout
= current_uiout
;
1302 ui_out_emit_tuple
tuple_emitter (uiout
, "func history");
1306 DEBUG ("call-history (0x%x): [%u; %u)", (int) flags
, low
, high
);
1308 /* Check for wrap-arounds. */
1309 if (low
!= from
|| high
!= to
)
1310 error (_("Bad range."));
1313 error (_("Bad range."));
1315 btinfo
= require_btrace ();
1317 found
= btrace_find_call_by_number (&begin
, btinfo
, low
);
1319 error (_("Range out of bounds."));
1321 found
= btrace_find_call_by_number (&end
, btinfo
, high
);
1324 /* Silently truncate the range. */
1325 btrace_call_end (&end
, btinfo
);
1329 /* We want both begin and end to be inclusive. */
1330 btrace_call_next (&end
, 1);
1333 btrace_call_history (uiout
, btinfo
, &begin
, &end
, flags
);
1334 btrace_set_call_history (btinfo
, &begin
, &end
);
1337 /* The call_history_from method of target record-btrace. */
1340 record_btrace_target::call_history_from (ULONGEST from
, int size
,
1341 record_print_flags flags
)
1343 ULONGEST begin
, end
, context
;
1345 context
= abs (size
);
1347 error (_("Bad record function-call-history-size."));
1356 begin
= from
- context
+ 1;
1361 end
= from
+ context
- 1;
1363 /* Check for wrap-around. */
1368 call_history_range ( begin
, end
, flags
);
1371 /* The record_method method of target record-btrace. */
1374 record_btrace_target::record_method (ptid_t ptid
)
1376 struct thread_info
* const tp
= find_thread_ptid (ptid
);
1379 error (_("No thread."));
1381 if (tp
->btrace
.target
== NULL
)
1382 return RECORD_METHOD_NONE
;
1384 return RECORD_METHOD_BTRACE
;
1387 /* The record_is_replaying method of target record-btrace. */
1390 record_btrace_target::record_is_replaying (ptid_t ptid
)
1392 for (thread_info
*tp
: all_non_exited_threads (ptid
))
1393 if (btrace_is_replaying (tp
))
1399 /* The record_will_replay method of target record-btrace. */
1402 record_btrace_target::record_will_replay (ptid_t ptid
, int dir
)
1404 return dir
== EXEC_REVERSE
|| record_is_replaying (ptid
);
1407 /* The xfer_partial method of target record-btrace. */
1409 enum target_xfer_status
1410 record_btrace_target::xfer_partial (enum target_object object
,
1411 const char *annex
, gdb_byte
*readbuf
,
1412 const gdb_byte
*writebuf
, ULONGEST offset
,
1413 ULONGEST len
, ULONGEST
*xfered_len
)
1415 /* Filter out requests that don't make sense during replay. */
1416 if (replay_memory_access
== replay_memory_access_read_only
1417 && !record_btrace_generating_corefile
1418 && record_is_replaying (inferior_ptid
))
1422 case TARGET_OBJECT_MEMORY
:
1424 struct target_section
*section
;
1426 /* We do not allow writing memory in general. */
1427 if (writebuf
!= NULL
)
1430 return TARGET_XFER_UNAVAILABLE
;
1433 /* We allow reading readonly memory. */
1434 section
= target_section_by_addr (this, offset
);
1435 if (section
!= NULL
)
1437 /* Check if the section we found is readonly. */
1438 if ((bfd_section_flags (section
->the_bfd_section
)
1439 & SEC_READONLY
) != 0)
1441 /* Truncate the request to fit into this section. */
1442 len
= std::min (len
, section
->endaddr
- offset
);
1448 return TARGET_XFER_UNAVAILABLE
;
1453 /* Forward the request. */
1454 return this->beneath ()->xfer_partial (object
, annex
, readbuf
, writebuf
,
1455 offset
, len
, xfered_len
);
1458 /* The insert_breakpoint method of target record-btrace. */
1461 record_btrace_target::insert_breakpoint (struct gdbarch
*gdbarch
,
1462 struct bp_target_info
*bp_tgt
)
1467 /* Inserting breakpoints requires accessing memory. Allow it for the
1468 duration of this function. */
1469 old
= replay_memory_access
;
1470 replay_memory_access
= replay_memory_access_read_write
;
1475 ret
= this->beneath ()->insert_breakpoint (gdbarch
, bp_tgt
);
1477 catch (const gdb_exception
&except
)
1479 replay_memory_access
= old
;
1482 replay_memory_access
= old
;
1487 /* The remove_breakpoint method of target record-btrace. */
1490 record_btrace_target::remove_breakpoint (struct gdbarch
*gdbarch
,
1491 struct bp_target_info
*bp_tgt
,
1492 enum remove_bp_reason reason
)
1497 /* Removing breakpoints requires accessing memory. Allow it for the
1498 duration of this function. */
1499 old
= replay_memory_access
;
1500 replay_memory_access
= replay_memory_access_read_write
;
1505 ret
= this->beneath ()->remove_breakpoint (gdbarch
, bp_tgt
, reason
);
1507 catch (const gdb_exception
&except
)
1509 replay_memory_access
= old
;
1512 replay_memory_access
= old
;
1517 /* The fetch_registers method of target record-btrace. */
1520 record_btrace_target::fetch_registers (struct regcache
*regcache
, int regno
)
1522 struct btrace_insn_iterator
*replay
;
1523 struct thread_info
*tp
;
1525 tp
= find_thread_ptid (regcache
->ptid ());
1526 gdb_assert (tp
!= NULL
);
1528 replay
= tp
->btrace
.replay
;
1529 if (replay
!= NULL
&& !record_btrace_generating_corefile
)
1531 const struct btrace_insn
*insn
;
1532 struct gdbarch
*gdbarch
;
1535 gdbarch
= regcache
->arch ();
1536 pcreg
= gdbarch_pc_regnum (gdbarch
);
1540 /* We can only provide the PC register. */
1541 if (regno
>= 0 && regno
!= pcreg
)
1544 insn
= btrace_insn_get (replay
);
1545 gdb_assert (insn
!= NULL
);
1547 regcache
->raw_supply (regno
, &insn
->pc
);
1550 this->beneath ()->fetch_registers (regcache
, regno
);
1553 /* The store_registers method of target record-btrace. */
1556 record_btrace_target::store_registers (struct regcache
*regcache
, int regno
)
1558 if (!record_btrace_generating_corefile
1559 && record_is_replaying (regcache
->ptid ()))
1560 error (_("Cannot write registers while replaying."));
1562 gdb_assert (may_write_registers
);
1564 this->beneath ()->store_registers (regcache
, regno
);
1567 /* The prepare_to_store method of target record-btrace. */
1570 record_btrace_target::prepare_to_store (struct regcache
*regcache
)
1572 if (!record_btrace_generating_corefile
1573 && record_is_replaying (regcache
->ptid ()))
1576 this->beneath ()->prepare_to_store (regcache
);
1579 /* The branch trace frame cache. */
1581 struct btrace_frame_cache
1584 struct thread_info
*tp
;
1586 /* The frame info. */
1587 struct frame_info
*frame
;
1589 /* The branch trace function segment. */
1590 const struct btrace_function
*bfun
;
1593 /* A struct btrace_frame_cache hash table indexed by NEXT. */
1595 static htab_t bfcache
;
1597 /* hash_f for htab_create_alloc of bfcache. */
1600 bfcache_hash (const void *arg
)
1602 const struct btrace_frame_cache
*cache
1603 = (const struct btrace_frame_cache
*) arg
;
1605 return htab_hash_pointer (cache
->frame
);
1608 /* eq_f for htab_create_alloc of bfcache. */
1611 bfcache_eq (const void *arg1
, const void *arg2
)
1613 const struct btrace_frame_cache
*cache1
1614 = (const struct btrace_frame_cache
*) arg1
;
1615 const struct btrace_frame_cache
*cache2
1616 = (const struct btrace_frame_cache
*) arg2
;
1618 return cache1
->frame
== cache2
->frame
;
1621 /* Create a new btrace frame cache. */
1623 static struct btrace_frame_cache
*
1624 bfcache_new (struct frame_info
*frame
)
1626 struct btrace_frame_cache
*cache
;
1629 cache
= FRAME_OBSTACK_ZALLOC (struct btrace_frame_cache
);
1630 cache
->frame
= frame
;
1632 slot
= htab_find_slot (bfcache
, cache
, INSERT
);
1633 gdb_assert (*slot
== NULL
);
1639 /* Extract the branch trace function from a branch trace frame. */
1641 static const struct btrace_function
*
1642 btrace_get_frame_function (struct frame_info
*frame
)
1644 const struct btrace_frame_cache
*cache
;
1645 struct btrace_frame_cache pattern
;
1648 pattern
.frame
= frame
;
1650 slot
= htab_find_slot (bfcache
, &pattern
, NO_INSERT
);
1654 cache
= (const struct btrace_frame_cache
*) *slot
;
1658 /* Implement stop_reason method for record_btrace_frame_unwind. */
1660 static enum unwind_stop_reason
1661 record_btrace_frame_unwind_stop_reason (struct frame_info
*this_frame
,
1664 const struct btrace_frame_cache
*cache
;
1665 const struct btrace_function
*bfun
;
1667 cache
= (const struct btrace_frame_cache
*) *this_cache
;
1669 gdb_assert (bfun
!= NULL
);
1672 return UNWIND_UNAVAILABLE
;
1674 return UNWIND_NO_REASON
;
1677 /* Implement this_id method for record_btrace_frame_unwind. */
1680 record_btrace_frame_this_id (struct frame_info
*this_frame
, void **this_cache
,
1681 struct frame_id
*this_id
)
1683 const struct btrace_frame_cache
*cache
;
1684 const struct btrace_function
*bfun
;
1685 struct btrace_call_iterator it
;
1686 CORE_ADDR code
, special
;
1688 cache
= (const struct btrace_frame_cache
*) *this_cache
;
1691 gdb_assert (bfun
!= NULL
);
1693 while (btrace_find_call_by_number (&it
, &cache
->tp
->btrace
, bfun
->prev
) != 0)
1694 bfun
= btrace_call_get (&it
);
1696 code
= get_frame_func (this_frame
);
1697 special
= bfun
->number
;
1699 *this_id
= frame_id_build_unavailable_stack_special (code
, special
);
1701 DEBUG ("[frame] %s id: (!stack, pc=%s, special=%s)",
1702 btrace_get_bfun_name (cache
->bfun
),
1703 core_addr_to_string_nz (this_id
->code_addr
),
1704 core_addr_to_string_nz (this_id
->special_addr
));
1707 /* Implement prev_register method for record_btrace_frame_unwind. */
1709 static struct value
*
1710 record_btrace_frame_prev_register (struct frame_info
*this_frame
,
1714 const struct btrace_frame_cache
*cache
;
1715 const struct btrace_function
*bfun
, *caller
;
1716 struct btrace_call_iterator it
;
1717 struct gdbarch
*gdbarch
;
1721 gdbarch
= get_frame_arch (this_frame
);
1722 pcreg
= gdbarch_pc_regnum (gdbarch
);
1723 if (pcreg
< 0 || regnum
!= pcreg
)
1724 throw_error (NOT_AVAILABLE_ERROR
,
1725 _("Registers are not available in btrace record history"));
1727 cache
= (const struct btrace_frame_cache
*) *this_cache
;
1729 gdb_assert (bfun
!= NULL
);
1731 if (btrace_find_call_by_number (&it
, &cache
->tp
->btrace
, bfun
->up
) == 0)
1732 throw_error (NOT_AVAILABLE_ERROR
,
1733 _("No caller in btrace record history"));
1735 caller
= btrace_call_get (&it
);
1737 if ((bfun
->flags
& BFUN_UP_LINKS_TO_RET
) != 0)
1738 pc
= caller
->insn
.front ().pc
;
1741 pc
= caller
->insn
.back ().pc
;
1742 pc
+= gdb_insn_length (gdbarch
, pc
);
1745 DEBUG ("[frame] unwound PC in %s on level %d: %s",
1746 btrace_get_bfun_name (bfun
), bfun
->level
,
1747 core_addr_to_string_nz (pc
));
1749 return frame_unwind_got_address (this_frame
, regnum
, pc
);
1752 /* Implement sniffer method for record_btrace_frame_unwind. */
1755 record_btrace_frame_sniffer (const struct frame_unwind
*self
,
1756 struct frame_info
*this_frame
,
1759 const struct btrace_function
*bfun
;
1760 struct btrace_frame_cache
*cache
;
1761 struct thread_info
*tp
;
1762 struct frame_info
*next
;
1764 /* THIS_FRAME does not contain a reference to its thread. */
1765 tp
= inferior_thread ();
1768 next
= get_next_frame (this_frame
);
1771 const struct btrace_insn_iterator
*replay
;
1773 replay
= tp
->btrace
.replay
;
1775 bfun
= &replay
->btinfo
->functions
[replay
->call_index
];
1779 const struct btrace_function
*callee
;
1780 struct btrace_call_iterator it
;
1782 callee
= btrace_get_frame_function (next
);
1783 if (callee
== NULL
|| (callee
->flags
& BFUN_UP_LINKS_TO_TAILCALL
) != 0)
1786 if (btrace_find_call_by_number (&it
, &tp
->btrace
, callee
->up
) == 0)
1789 bfun
= btrace_call_get (&it
);
1795 DEBUG ("[frame] sniffed frame for %s on level %d",
1796 btrace_get_bfun_name (bfun
), bfun
->level
);
1798 /* This is our frame. Initialize the frame cache. */
1799 cache
= bfcache_new (this_frame
);
1803 *this_cache
= cache
;
1807 /* Implement sniffer method for record_btrace_tailcall_frame_unwind. */
1810 record_btrace_tailcall_frame_sniffer (const struct frame_unwind
*self
,
1811 struct frame_info
*this_frame
,
1814 const struct btrace_function
*bfun
, *callee
;
1815 struct btrace_frame_cache
*cache
;
1816 struct btrace_call_iterator it
;
1817 struct frame_info
*next
;
1818 struct thread_info
*tinfo
;
1820 next
= get_next_frame (this_frame
);
1824 callee
= btrace_get_frame_function (next
);
1828 if ((callee
->flags
& BFUN_UP_LINKS_TO_TAILCALL
) == 0)
1831 tinfo
= inferior_thread ();
1832 if (btrace_find_call_by_number (&it
, &tinfo
->btrace
, callee
->up
) == 0)
1835 bfun
= btrace_call_get (&it
);
1837 DEBUG ("[frame] sniffed tailcall frame for %s on level %d",
1838 btrace_get_bfun_name (bfun
), bfun
->level
);
1840 /* This is our frame. Initialize the frame cache. */
1841 cache
= bfcache_new (this_frame
);
1845 *this_cache
= cache
;
1850 record_btrace_frame_dealloc_cache (struct frame_info
*self
, void *this_cache
)
1852 struct btrace_frame_cache
*cache
;
1855 cache
= (struct btrace_frame_cache
*) this_cache
;
1857 slot
= htab_find_slot (bfcache
, cache
, NO_INSERT
);
1858 gdb_assert (slot
!= NULL
);
1860 htab_remove_elt (bfcache
, cache
);
1863 /* btrace recording does not store previous memory content, neither the stack
1864 frames content. Any unwinding would return erroneous results as the stack
1865 contents no longer matches the changed PC value restored from history.
1866 Therefore this unwinder reports any possibly unwound registers as
1869 const struct frame_unwind record_btrace_frame_unwind
=
1872 record_btrace_frame_unwind_stop_reason
,
1873 record_btrace_frame_this_id
,
1874 record_btrace_frame_prev_register
,
1876 record_btrace_frame_sniffer
,
1877 record_btrace_frame_dealloc_cache
1880 const struct frame_unwind record_btrace_tailcall_frame_unwind
=
1883 record_btrace_frame_unwind_stop_reason
,
1884 record_btrace_frame_this_id
,
1885 record_btrace_frame_prev_register
,
1887 record_btrace_tailcall_frame_sniffer
,
1888 record_btrace_frame_dealloc_cache
1891 /* Implement the get_unwinder method. */
1893 const struct frame_unwind
*
1894 record_btrace_target::get_unwinder ()
1896 return &record_btrace_frame_unwind
;
1899 /* Implement the get_tailcall_unwinder method. */
1901 const struct frame_unwind
*
1902 record_btrace_target::get_tailcall_unwinder ()
1904 return &record_btrace_tailcall_frame_unwind
;
1907 /* Return a human-readable string for FLAG. */
1910 btrace_thread_flag_to_str (enum btrace_thread_flag flag
)
1918 return "reverse-step";
1924 return "reverse-cont";
1933 /* Indicate that TP should be resumed according to FLAG. */
1936 record_btrace_resume_thread (struct thread_info
*tp
,
1937 enum btrace_thread_flag flag
)
1939 struct btrace_thread_info
*btinfo
;
1941 DEBUG ("resuming thread %s (%s): %x (%s)", print_thread_id (tp
),
1942 target_pid_to_str (tp
->ptid
).c_str (), flag
,
1943 btrace_thread_flag_to_str (flag
));
1945 btinfo
= &tp
->btrace
;
1947 /* Fetch the latest branch trace. */
1948 btrace_fetch (tp
, record_btrace_get_cpu ());
1950 /* A resume request overwrites a preceding resume or stop request. */
1951 btinfo
->flags
&= ~(BTHR_MOVE
| BTHR_STOP
);
1952 btinfo
->flags
|= flag
;
1955 /* Get the current frame for TP. */
1957 static struct frame_id
1958 get_thread_current_frame_id (struct thread_info
*tp
)
1963 /* Set current thread, which is implicitly used by
1964 get_current_frame. */
1965 scoped_restore_current_thread restore_thread
;
1967 switch_to_thread (tp
);
1969 /* Clear the executing flag to allow changes to the current frame.
1970 We are not actually running, yet. We just started a reverse execution
1971 command or a record goto command.
1972 For the latter, EXECUTING is false and this has no effect.
1973 For the former, EXECUTING is true and we're in wait, about to
1974 move the thread. Since we need to recompute the stack, we temporarily
1975 set EXECUTING to false. */
1976 executing
= tp
->executing
;
1977 set_executing (inferior_ptid
, false);
1982 id
= get_frame_id (get_current_frame ());
1984 catch (const gdb_exception
&except
)
1986 /* Restore the previous execution state. */
1987 set_executing (inferior_ptid
, executing
);
1992 /* Restore the previous execution state. */
1993 set_executing (inferior_ptid
, executing
);
1998 /* Start replaying a thread. */
2000 static struct btrace_insn_iterator
*
2001 record_btrace_start_replaying (struct thread_info
*tp
)
2003 struct btrace_insn_iterator
*replay
;
2004 struct btrace_thread_info
*btinfo
;
2006 btinfo
= &tp
->btrace
;
2009 /* We can't start replaying without trace. */
2010 if (btinfo
->functions
.empty ())
2013 /* GDB stores the current frame_id when stepping in order to detects steps
2015 Since frames are computed differently when we're replaying, we need to
2016 recompute those stored frames and fix them up so we can still detect
2017 subroutines after we started replaying. */
2020 struct frame_id frame_id
;
2021 int upd_step_frame_id
, upd_step_stack_frame_id
;
2023 /* The current frame without replaying - computed via normal unwind. */
2024 frame_id
= get_thread_current_frame_id (tp
);
2026 /* Check if we need to update any stepping-related frame id's. */
2027 upd_step_frame_id
= frame_id_eq (frame_id
,
2028 tp
->control
.step_frame_id
);
2029 upd_step_stack_frame_id
= frame_id_eq (frame_id
,
2030 tp
->control
.step_stack_frame_id
);
2032 /* We start replaying at the end of the branch trace. This corresponds
2033 to the current instruction. */
2034 replay
= XNEW (struct btrace_insn_iterator
);
2035 btrace_insn_end (replay
, btinfo
);
2037 /* Skip gaps at the end of the trace. */
2038 while (btrace_insn_get (replay
) == NULL
)
2042 steps
= btrace_insn_prev (replay
, 1);
2044 error (_("No trace."));
2047 /* We're not replaying, yet. */
2048 gdb_assert (btinfo
->replay
== NULL
);
2049 btinfo
->replay
= replay
;
2051 /* Make sure we're not using any stale registers. */
2052 registers_changed_thread (tp
);
2054 /* The current frame with replaying - computed via btrace unwind. */
2055 frame_id
= get_thread_current_frame_id (tp
);
2057 /* Replace stepping related frames where necessary. */
2058 if (upd_step_frame_id
)
2059 tp
->control
.step_frame_id
= frame_id
;
2060 if (upd_step_stack_frame_id
)
2061 tp
->control
.step_stack_frame_id
= frame_id
;
2063 catch (const gdb_exception
&except
)
2065 xfree (btinfo
->replay
);
2066 btinfo
->replay
= NULL
;
2068 registers_changed_thread (tp
);
2076 /* Stop replaying a thread. */
2079 record_btrace_stop_replaying (struct thread_info
*tp
)
2081 struct btrace_thread_info
*btinfo
;
2083 btinfo
= &tp
->btrace
;
2085 xfree (btinfo
->replay
);
2086 btinfo
->replay
= NULL
;
2088 /* Make sure we're not leaving any stale registers. */
2089 registers_changed_thread (tp
);
2092 /* Stop replaying TP if it is at the end of its execution history. */
2095 record_btrace_stop_replaying_at_end (struct thread_info
*tp
)
2097 struct btrace_insn_iterator
*replay
, end
;
2098 struct btrace_thread_info
*btinfo
;
2100 btinfo
= &tp
->btrace
;
2101 replay
= btinfo
->replay
;
2106 btrace_insn_end (&end
, btinfo
);
2108 if (btrace_insn_cmp (replay
, &end
) == 0)
2109 record_btrace_stop_replaying (tp
);
2112 /* The resume method of target record-btrace. */
2115 record_btrace_target::resume (ptid_t ptid
, int step
, enum gdb_signal signal
)
2117 enum btrace_thread_flag flag
, cflag
;
2119 DEBUG ("resume %s: %s%s", target_pid_to_str (ptid
).c_str (),
2120 ::execution_direction
== EXEC_REVERSE
? "reverse-" : "",
2121 step
? "step" : "cont");
2123 /* Store the execution direction of the last resume.
2125 If there is more than one resume call, we have to rely on infrun
2126 to not change the execution direction in-between. */
2127 record_btrace_resume_exec_dir
= ::execution_direction
;
2129 /* As long as we're not replaying, just forward the request.
2131 For non-stop targets this means that no thread is replaying. In order to
2132 make progress, we may need to explicitly move replaying threads to the end
2133 of their execution history. */
2134 if ((::execution_direction
!= EXEC_REVERSE
)
2135 && !record_is_replaying (minus_one_ptid
))
2137 this->beneath ()->resume (ptid
, step
, signal
);
2141 /* Compute the btrace thread flag for the requested move. */
2142 if (::execution_direction
== EXEC_REVERSE
)
2144 flag
= step
== 0 ? BTHR_RCONT
: BTHR_RSTEP
;
2149 flag
= step
== 0 ? BTHR_CONT
: BTHR_STEP
;
2153 /* We just indicate the resume intent here. The actual stepping happens in
2154 record_btrace_wait below.
2156 For all-stop targets, we only step INFERIOR_PTID and continue others. */
2157 if (!target_is_non_stop_p ())
2159 gdb_assert (inferior_ptid
.matches (ptid
));
2161 for (thread_info
*tp
: all_non_exited_threads (ptid
))
2163 if (tp
->ptid
.matches (inferior_ptid
))
2164 record_btrace_resume_thread (tp
, flag
);
2166 record_btrace_resume_thread (tp
, cflag
);
2171 for (thread_info
*tp
: all_non_exited_threads (ptid
))
2172 record_btrace_resume_thread (tp
, flag
);
2175 /* Async support. */
2176 if (target_can_async_p ())
2179 mark_async_event_handler (record_btrace_async_inferior_event_handler
);
2183 /* The commit_resume method of target record-btrace. */
2186 record_btrace_target::commit_resume ()
2188 if ((::execution_direction
!= EXEC_REVERSE
)
2189 && !record_is_replaying (minus_one_ptid
))
2190 beneath ()->commit_resume ();
2193 /* Cancel resuming TP. */
2196 record_btrace_cancel_resume (struct thread_info
*tp
)
2198 enum btrace_thread_flag flags
;
2200 flags
= tp
->btrace
.flags
& (BTHR_MOVE
| BTHR_STOP
);
2204 DEBUG ("cancel resume thread %s (%s): %x (%s)",
2205 print_thread_id (tp
),
2206 target_pid_to_str (tp
->ptid
).c_str (), flags
,
2207 btrace_thread_flag_to_str (flags
));
2209 tp
->btrace
.flags
&= ~(BTHR_MOVE
| BTHR_STOP
);
2210 record_btrace_stop_replaying_at_end (tp
);
2213 /* Return a target_waitstatus indicating that we ran out of history. */
2215 static struct target_waitstatus
2216 btrace_step_no_history (void)
2218 struct target_waitstatus status
;
2220 status
.kind
= TARGET_WAITKIND_NO_HISTORY
;
2225 /* Return a target_waitstatus indicating that a step finished. */
2227 static struct target_waitstatus
2228 btrace_step_stopped (void)
2230 struct target_waitstatus status
;
2232 status
.kind
= TARGET_WAITKIND_STOPPED
;
2233 status
.value
.sig
= GDB_SIGNAL_TRAP
;
2238 /* Return a target_waitstatus indicating that a thread was stopped as
2241 static struct target_waitstatus
2242 btrace_step_stopped_on_request (void)
2244 struct target_waitstatus status
;
2246 status
.kind
= TARGET_WAITKIND_STOPPED
;
2247 status
.value
.sig
= GDB_SIGNAL_0
;
2252 /* Return a target_waitstatus indicating a spurious stop. */
2254 static struct target_waitstatus
2255 btrace_step_spurious (void)
2257 struct target_waitstatus status
;
2259 status
.kind
= TARGET_WAITKIND_SPURIOUS
;
2264 /* Return a target_waitstatus indicating that the thread was not resumed. */
2266 static struct target_waitstatus
2267 btrace_step_no_resumed (void)
2269 struct target_waitstatus status
;
2271 status
.kind
= TARGET_WAITKIND_NO_RESUMED
;
2276 /* Return a target_waitstatus indicating that we should wait again. */
2278 static struct target_waitstatus
2279 btrace_step_again (void)
2281 struct target_waitstatus status
;
2283 status
.kind
= TARGET_WAITKIND_IGNORE
;
2288 /* Clear the record histories. */
2291 record_btrace_clear_histories (struct btrace_thread_info
*btinfo
)
2293 xfree (btinfo
->insn_history
);
2294 xfree (btinfo
->call_history
);
2296 btinfo
->insn_history
= NULL
;
2297 btinfo
->call_history
= NULL
;
2300 /* Check whether TP's current replay position is at a breakpoint. */
2303 record_btrace_replay_at_breakpoint (struct thread_info
*tp
)
2305 struct btrace_insn_iterator
*replay
;
2306 struct btrace_thread_info
*btinfo
;
2307 const struct btrace_insn
*insn
;
2309 btinfo
= &tp
->btrace
;
2310 replay
= btinfo
->replay
;
2315 insn
= btrace_insn_get (replay
);
2319 return record_check_stopped_by_breakpoint (tp
->inf
->aspace
, insn
->pc
,
2320 &btinfo
->stop_reason
);
2323 /* Step one instruction in forward direction. */
2325 static struct target_waitstatus
2326 record_btrace_single_step_forward (struct thread_info
*tp
)
2328 struct btrace_insn_iterator
*replay
, end
, start
;
2329 struct btrace_thread_info
*btinfo
;
2331 btinfo
= &tp
->btrace
;
2332 replay
= btinfo
->replay
;
2334 /* We're done if we're not replaying. */
2336 return btrace_step_no_history ();
2338 /* Check if we're stepping a breakpoint. */
2339 if (record_btrace_replay_at_breakpoint (tp
))
2340 return btrace_step_stopped ();
2342 /* Skip gaps during replay. If we end up at a gap (at the end of the trace),
2343 jump back to the instruction at which we started. */
2349 /* We will bail out here if we continue stepping after reaching the end
2350 of the execution history. */
2351 steps
= btrace_insn_next (replay
, 1);
2355 return btrace_step_no_history ();
2358 while (btrace_insn_get (replay
) == NULL
);
2360 /* Determine the end of the instruction trace. */
2361 btrace_insn_end (&end
, btinfo
);
2363 /* The execution trace contains (and ends with) the current instruction.
2364 This instruction has not been executed, yet, so the trace really ends
2365 one instruction earlier. */
2366 if (btrace_insn_cmp (replay
, &end
) == 0)
2367 return btrace_step_no_history ();
2369 return btrace_step_spurious ();
2372 /* Step one instruction in backward direction. */
2374 static struct target_waitstatus
2375 record_btrace_single_step_backward (struct thread_info
*tp
)
2377 struct btrace_insn_iterator
*replay
, start
;
2378 struct btrace_thread_info
*btinfo
;
2380 btinfo
= &tp
->btrace
;
2381 replay
= btinfo
->replay
;
2383 /* Start replaying if we're not already doing so. */
2385 replay
= record_btrace_start_replaying (tp
);
2387 /* If we can't step any further, we reached the end of the history.
2388 Skip gaps during replay. If we end up at a gap (at the beginning of
2389 the trace), jump back to the instruction at which we started. */
2395 steps
= btrace_insn_prev (replay
, 1);
2399 return btrace_step_no_history ();
2402 while (btrace_insn_get (replay
) == NULL
);
2404 /* Check if we're stepping a breakpoint.
2406 For reverse-stepping, this check is after the step. There is logic in
2407 infrun.c that handles reverse-stepping separately. See, for example,
2408 proceed and adjust_pc_after_break.
2410 This code assumes that for reverse-stepping, PC points to the last
2411 de-executed instruction, whereas for forward-stepping PC points to the
2412 next to-be-executed instruction. */
2413 if (record_btrace_replay_at_breakpoint (tp
))
2414 return btrace_step_stopped ();
2416 return btrace_step_spurious ();
2419 /* Step a single thread. */
2421 static struct target_waitstatus
2422 record_btrace_step_thread (struct thread_info
*tp
)
2424 struct btrace_thread_info
*btinfo
;
2425 struct target_waitstatus status
;
2426 enum btrace_thread_flag flags
;
2428 btinfo
= &tp
->btrace
;
2430 flags
= btinfo
->flags
& (BTHR_MOVE
| BTHR_STOP
);
2431 btinfo
->flags
&= ~(BTHR_MOVE
| BTHR_STOP
);
2433 DEBUG ("stepping thread %s (%s): %x (%s)", print_thread_id (tp
),
2434 target_pid_to_str (tp
->ptid
).c_str (), flags
,
2435 btrace_thread_flag_to_str (flags
));
2437 /* We can't step without an execution history. */
2438 if ((flags
& BTHR_MOVE
) != 0 && btrace_is_empty (tp
))
2439 return btrace_step_no_history ();
2444 internal_error (__FILE__
, __LINE__
, _("invalid stepping type."));
2447 return btrace_step_stopped_on_request ();
2450 status
= record_btrace_single_step_forward (tp
);
2451 if (status
.kind
!= TARGET_WAITKIND_SPURIOUS
)
2454 return btrace_step_stopped ();
2457 status
= record_btrace_single_step_backward (tp
);
2458 if (status
.kind
!= TARGET_WAITKIND_SPURIOUS
)
2461 return btrace_step_stopped ();
2464 status
= record_btrace_single_step_forward (tp
);
2465 if (status
.kind
!= TARGET_WAITKIND_SPURIOUS
)
2468 btinfo
->flags
|= flags
;
2469 return btrace_step_again ();
2472 status
= record_btrace_single_step_backward (tp
);
2473 if (status
.kind
!= TARGET_WAITKIND_SPURIOUS
)
2476 btinfo
->flags
|= flags
;
2477 return btrace_step_again ();
2480 /* We keep threads moving at the end of their execution history. The wait
2481 method will stop the thread for whom the event is reported. */
2482 if (status
.kind
== TARGET_WAITKIND_NO_HISTORY
)
2483 btinfo
->flags
|= flags
;
2488 /* Announce further events if necessary. */
2491 record_btrace_maybe_mark_async_event
2492 (const std::vector
<thread_info
*> &moving
,
2493 const std::vector
<thread_info
*> &no_history
)
2495 bool more_moving
= !moving
.empty ();
2496 bool more_no_history
= !no_history
.empty ();;
2498 if (!more_moving
&& !more_no_history
)
2502 DEBUG ("movers pending");
2504 if (more_no_history
)
2505 DEBUG ("no-history pending");
2507 mark_async_event_handler (record_btrace_async_inferior_event_handler
);
2510 /* The wait method of target record-btrace. */
2513 record_btrace_target::wait (ptid_t ptid
, struct target_waitstatus
*status
,
2516 std::vector
<thread_info
*> moving
;
2517 std::vector
<thread_info
*> no_history
;
2519 DEBUG ("wait %s (0x%x)", target_pid_to_str (ptid
).c_str (), options
);
2521 /* As long as we're not replaying, just forward the request. */
2522 if ((::execution_direction
!= EXEC_REVERSE
)
2523 && !record_is_replaying (minus_one_ptid
))
2525 return this->beneath ()->wait (ptid
, status
, options
);
2528 /* Keep a work list of moving threads. */
2529 for (thread_info
*tp
: all_non_exited_threads (ptid
))
2530 if ((tp
->btrace
.flags
& (BTHR_MOVE
| BTHR_STOP
)) != 0)
2531 moving
.push_back (tp
);
2533 if (moving
.empty ())
2535 *status
= btrace_step_no_resumed ();
2537 DEBUG ("wait ended by %s: %s", target_pid_to_str (null_ptid
).c_str (),
2538 target_waitstatus_to_string (status
).c_str ());
2543 /* Step moving threads one by one, one step each, until either one thread
2544 reports an event or we run out of threads to step.
2546 When stepping more than one thread, chances are that some threads reach
2547 the end of their execution history earlier than others. If we reported
2548 this immediately, all-stop on top of non-stop would stop all threads and
2549 resume the same threads next time. And we would report the same thread
2550 having reached the end of its execution history again.
2552 In the worst case, this would starve the other threads. But even if other
2553 threads would be allowed to make progress, this would result in far too
2554 many intermediate stops.
2556 We therefore delay the reporting of "no execution history" until we have
2557 nothing else to report. By this time, all threads should have moved to
2558 either the beginning or the end of their execution history. There will
2559 be a single user-visible stop. */
2560 struct thread_info
*eventing
= NULL
;
2561 while ((eventing
== NULL
) && !moving
.empty ())
2563 for (unsigned int ix
= 0; eventing
== NULL
&& ix
< moving
.size ();)
2565 thread_info
*tp
= moving
[ix
];
2567 *status
= record_btrace_step_thread (tp
);
2569 switch (status
->kind
)
2571 case TARGET_WAITKIND_IGNORE
:
2575 case TARGET_WAITKIND_NO_HISTORY
:
2576 no_history
.push_back (ordered_remove (moving
, ix
));
2580 eventing
= unordered_remove (moving
, ix
);
2586 if (eventing
== NULL
)
2588 /* We started with at least one moving thread. This thread must have
2589 either stopped or reached the end of its execution history.
2591 In the former case, EVENTING must not be NULL.
2592 In the latter case, NO_HISTORY must not be empty. */
2593 gdb_assert (!no_history
.empty ());
2595 /* We kept threads moving at the end of their execution history. Stop
2596 EVENTING now that we are going to report its stop. */
2597 eventing
= unordered_remove (no_history
, 0);
2598 eventing
->btrace
.flags
&= ~BTHR_MOVE
;
2600 *status
= btrace_step_no_history ();
2603 gdb_assert (eventing
!= NULL
);
2605 /* We kept threads replaying at the end of their execution history. Stop
2606 replaying EVENTING now that we are going to report its stop. */
2607 record_btrace_stop_replaying_at_end (eventing
);
2609 /* Stop all other threads. */
2610 if (!target_is_non_stop_p ())
2612 for (thread_info
*tp
: all_non_exited_threads ())
2613 record_btrace_cancel_resume (tp
);
2616 /* In async mode, we need to announce further events. */
2617 if (target_is_async_p ())
2618 record_btrace_maybe_mark_async_event (moving
, no_history
);
2620 /* Start record histories anew from the current position. */
2621 record_btrace_clear_histories (&eventing
->btrace
);
2623 /* We moved the replay position but did not update registers. */
2624 registers_changed_thread (eventing
);
2626 DEBUG ("wait ended by thread %s (%s): %s",
2627 print_thread_id (eventing
),
2628 target_pid_to_str (eventing
->ptid
).c_str (),
2629 target_waitstatus_to_string (status
).c_str ());
2631 return eventing
->ptid
;
2634 /* The stop method of target record-btrace. */
2637 record_btrace_target::stop (ptid_t ptid
)
2639 DEBUG ("stop %s", target_pid_to_str (ptid
).c_str ());
2641 /* As long as we're not replaying, just forward the request. */
2642 if ((::execution_direction
!= EXEC_REVERSE
)
2643 && !record_is_replaying (minus_one_ptid
))
2645 this->beneath ()->stop (ptid
);
2649 for (thread_info
*tp
: all_non_exited_threads (ptid
))
2651 tp
->btrace
.flags
&= ~BTHR_MOVE
;
2652 tp
->btrace
.flags
|= BTHR_STOP
;
2657 /* The can_execute_reverse method of target record-btrace. */
2660 record_btrace_target::can_execute_reverse ()
2665 /* The stopped_by_sw_breakpoint method of target record-btrace. */
2668 record_btrace_target::stopped_by_sw_breakpoint ()
2670 if (record_is_replaying (minus_one_ptid
))
2672 struct thread_info
*tp
= inferior_thread ();
2674 return tp
->btrace
.stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
;
2677 return this->beneath ()->stopped_by_sw_breakpoint ();
2680 /* The supports_stopped_by_sw_breakpoint method of target
2684 record_btrace_target::supports_stopped_by_sw_breakpoint ()
2686 if (record_is_replaying (minus_one_ptid
))
2689 return this->beneath ()->supports_stopped_by_sw_breakpoint ();
2692 /* The stopped_by_sw_breakpoint method of target record-btrace. */
2695 record_btrace_target::stopped_by_hw_breakpoint ()
2697 if (record_is_replaying (minus_one_ptid
))
2699 struct thread_info
*tp
= inferior_thread ();
2701 return tp
->btrace
.stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
;
2704 return this->beneath ()->stopped_by_hw_breakpoint ();
2707 /* The supports_stopped_by_hw_breakpoint method of target
2711 record_btrace_target::supports_stopped_by_hw_breakpoint ()
2713 if (record_is_replaying (minus_one_ptid
))
2716 return this->beneath ()->supports_stopped_by_hw_breakpoint ();
2719 /* The update_thread_list method of target record-btrace. */
2722 record_btrace_target::update_thread_list ()
2724 /* We don't add or remove threads during replay. */
2725 if (record_is_replaying (minus_one_ptid
))
2728 /* Forward the request. */
2729 this->beneath ()->update_thread_list ();
2732 /* The thread_alive method of target record-btrace. */
2735 record_btrace_target::thread_alive (ptid_t ptid
)
2737 /* We don't add or remove threads during replay. */
2738 if (record_is_replaying (minus_one_ptid
))
2741 /* Forward the request. */
2742 return this->beneath ()->thread_alive (ptid
);
2745 /* Set the replay branch trace instruction iterator. If IT is NULL, replay
2749 record_btrace_set_replay (struct thread_info
*tp
,
2750 const struct btrace_insn_iterator
*it
)
2752 struct btrace_thread_info
*btinfo
;
2754 btinfo
= &tp
->btrace
;
2757 record_btrace_stop_replaying (tp
);
2760 if (btinfo
->replay
== NULL
)
2761 record_btrace_start_replaying (tp
);
2762 else if (btrace_insn_cmp (btinfo
->replay
, it
) == 0)
2765 *btinfo
->replay
= *it
;
2766 registers_changed_thread (tp
);
2769 /* Start anew from the new replay position. */
2770 record_btrace_clear_histories (btinfo
);
2772 inferior_thread ()->suspend
.stop_pc
2773 = regcache_read_pc (get_current_regcache ());
2774 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
2777 /* The goto_record_begin method of target record-btrace. */
2780 record_btrace_target::goto_record_begin ()
2782 struct thread_info
*tp
;
2783 struct btrace_insn_iterator begin
;
2785 tp
= require_btrace_thread ();
2787 btrace_insn_begin (&begin
, &tp
->btrace
);
2789 /* Skip gaps at the beginning of the trace. */
2790 while (btrace_insn_get (&begin
) == NULL
)
2794 steps
= btrace_insn_next (&begin
, 1);
2796 error (_("No trace."));
2799 record_btrace_set_replay (tp
, &begin
);
2802 /* The goto_record_end method of target record-btrace. */
2805 record_btrace_target::goto_record_end ()
2807 struct thread_info
*tp
;
2809 tp
= require_btrace_thread ();
2811 record_btrace_set_replay (tp
, NULL
);
2814 /* The goto_record method of target record-btrace. */
2817 record_btrace_target::goto_record (ULONGEST insn
)
2819 struct thread_info
*tp
;
2820 struct btrace_insn_iterator it
;
2821 unsigned int number
;
2826 /* Check for wrap-arounds. */
2828 error (_("Instruction number out of range."));
2830 tp
= require_btrace_thread ();
2832 found
= btrace_find_insn_by_number (&it
, &tp
->btrace
, number
);
2834 /* Check if the instruction could not be found or is a gap. */
2835 if (found
== 0 || btrace_insn_get (&it
) == NULL
)
2836 error (_("No such instruction."));
2838 record_btrace_set_replay (tp
, &it
);
2841 /* The record_stop_replaying method of target record-btrace. */
2844 record_btrace_target::record_stop_replaying ()
2846 for (thread_info
*tp
: all_non_exited_threads ())
2847 record_btrace_stop_replaying (tp
);
2850 /* The execution_direction target method. */
2852 enum exec_direction_kind
2853 record_btrace_target::execution_direction ()
2855 return record_btrace_resume_exec_dir
;
2858 /* The prepare_to_generate_core target method. */
2861 record_btrace_target::prepare_to_generate_core ()
2863 record_btrace_generating_corefile
= 1;
2866 /* The done_generating_core target method. */
2869 record_btrace_target::done_generating_core ()
2871 record_btrace_generating_corefile
= 0;
2874 /* Start recording in BTS format. */
2877 cmd_record_btrace_bts_start (const char *args
, int from_tty
)
2879 if (args
!= NULL
&& *args
!= 0)
2880 error (_("Invalid argument."));
2882 record_btrace_conf
.format
= BTRACE_FORMAT_BTS
;
2886 execute_command ("target record-btrace", from_tty
);
2888 catch (const gdb_exception
&exception
)
2890 record_btrace_conf
.format
= BTRACE_FORMAT_NONE
;
2895 /* Start recording in Intel Processor Trace format. */
2898 cmd_record_btrace_pt_start (const char *args
, int from_tty
)
2900 if (args
!= NULL
&& *args
!= 0)
2901 error (_("Invalid argument."));
2903 record_btrace_conf
.format
= BTRACE_FORMAT_PT
;
2907 execute_command ("target record-btrace", from_tty
);
2909 catch (const gdb_exception
&exception
)
2911 record_btrace_conf
.format
= BTRACE_FORMAT_NONE
;
2916 /* Alias for "target record". */
2919 cmd_record_btrace_start (const char *args
, int from_tty
)
2921 if (args
!= NULL
&& *args
!= 0)
2922 error (_("Invalid argument."));
2924 record_btrace_conf
.format
= BTRACE_FORMAT_PT
;
2928 execute_command ("target record-btrace", from_tty
);
2930 catch (const gdb_exception
&exception
)
2932 record_btrace_conf
.format
= BTRACE_FORMAT_BTS
;
2936 execute_command ("target record-btrace", from_tty
);
2938 catch (const gdb_exception
&ex
)
2940 record_btrace_conf
.format
= BTRACE_FORMAT_NONE
;
2946 /* The "set record btrace" command. */
2949 cmd_set_record_btrace (const char *args
, int from_tty
)
2951 printf_unfiltered (_("\"set record btrace\" must be followed "
2952 "by an appropriate subcommand.\n"));
2953 help_list (set_record_btrace_cmdlist
, "set record btrace ",
2954 all_commands
, gdb_stdout
);
2957 /* The "show record btrace" command. */
2960 cmd_show_record_btrace (const char *args
, int from_tty
)
2962 cmd_show_list (show_record_btrace_cmdlist
, from_tty
, "");
2965 /* The "show record btrace replay-memory-access" command. */
2968 cmd_show_replay_memory_access (struct ui_file
*file
, int from_tty
,
2969 struct cmd_list_element
*c
, const char *value
)
2971 fprintf_filtered (gdb_stdout
, _("Replay memory access is %s.\n"),
2972 replay_memory_access
);
2975 /* The "set record btrace cpu none" command. */
2978 cmd_set_record_btrace_cpu_none (const char *args
, int from_tty
)
2980 if (args
!= nullptr && *args
!= 0)
2981 error (_("Trailing junk: '%s'."), args
);
2983 record_btrace_cpu_state
= CS_NONE
;
2986 /* The "set record btrace cpu auto" command. */
2989 cmd_set_record_btrace_cpu_auto (const char *args
, int from_tty
)
2991 if (args
!= nullptr && *args
!= 0)
2992 error (_("Trailing junk: '%s'."), args
);
2994 record_btrace_cpu_state
= CS_AUTO
;
2997 /* The "set record btrace cpu" command. */
3000 cmd_set_record_btrace_cpu (const char *args
, int from_tty
)
3002 if (args
== nullptr)
3005 /* We use a hard-coded vendor string for now. */
3006 unsigned int family
, model
, stepping
;
3007 int l1
, l2
, matches
= sscanf (args
, "intel: %u/%u%n/%u%n", &family
,
3008 &model
, &l1
, &stepping
, &l2
);
3011 if (strlen (args
) != l2
)
3012 error (_("Trailing junk: '%s'."), args
+ l2
);
3014 else if (matches
== 2)
3016 if (strlen (args
) != l1
)
3017 error (_("Trailing junk: '%s'."), args
+ l1
);
3022 error (_("Bad format. See \"help set record btrace cpu\"."));
3024 if (USHRT_MAX
< family
)
3025 error (_("Cpu family too big."));
3027 if (UCHAR_MAX
< model
)
3028 error (_("Cpu model too big."));
3030 if (UCHAR_MAX
< stepping
)
3031 error (_("Cpu stepping too big."));
3033 record_btrace_cpu
.vendor
= CV_INTEL
;
3034 record_btrace_cpu
.family
= family
;
3035 record_btrace_cpu
.model
= model
;
3036 record_btrace_cpu
.stepping
= stepping
;
3038 record_btrace_cpu_state
= CS_CPU
;
3041 /* The "show record btrace cpu" command. */
3044 cmd_show_record_btrace_cpu (const char *args
, int from_tty
)
3046 if (args
!= nullptr && *args
!= 0)
3047 error (_("Trailing junk: '%s'."), args
);
3049 switch (record_btrace_cpu_state
)
3052 printf_unfiltered (_("btrace cpu is 'auto'.\n"));
3056 printf_unfiltered (_("btrace cpu is 'none'.\n"));
3060 switch (record_btrace_cpu
.vendor
)
3063 if (record_btrace_cpu
.stepping
== 0)
3064 printf_unfiltered (_("btrace cpu is 'intel: %u/%u'.\n"),
3065 record_btrace_cpu
.family
,
3066 record_btrace_cpu
.model
);
3068 printf_unfiltered (_("btrace cpu is 'intel: %u/%u/%u'.\n"),
3069 record_btrace_cpu
.family
,
3070 record_btrace_cpu
.model
,
3071 record_btrace_cpu
.stepping
);
3076 error (_("Internal error: bad cpu state."));
3079 /* The "s record btrace bts" command. */
3082 cmd_set_record_btrace_bts (const char *args
, int from_tty
)
3084 printf_unfiltered (_("\"set record btrace bts\" must be followed "
3085 "by an appropriate subcommand.\n"));
3086 help_list (set_record_btrace_bts_cmdlist
, "set record btrace bts ",
3087 all_commands
, gdb_stdout
);
3090 /* The "show record btrace bts" command. */
3093 cmd_show_record_btrace_bts (const char *args
, int from_tty
)
3095 cmd_show_list (show_record_btrace_bts_cmdlist
, from_tty
, "");
3098 /* The "set record btrace pt" command. */
3101 cmd_set_record_btrace_pt (const char *args
, int from_tty
)
3103 printf_unfiltered (_("\"set record btrace pt\" must be followed "
3104 "by an appropriate subcommand.\n"));
3105 help_list (set_record_btrace_pt_cmdlist
, "set record btrace pt ",
3106 all_commands
, gdb_stdout
);
3109 /* The "show record btrace pt" command. */
3112 cmd_show_record_btrace_pt (const char *args
, int from_tty
)
3114 cmd_show_list (show_record_btrace_pt_cmdlist
, from_tty
, "");
3117 /* The "record bts buffer-size" show value function. */
3120 show_record_bts_buffer_size_value (struct ui_file
*file
, int from_tty
,
3121 struct cmd_list_element
*c
,
3124 fprintf_filtered (file
, _("The record/replay bts buffer size is %s.\n"),
3128 /* The "record pt buffer-size" show value function. */
3131 show_record_pt_buffer_size_value (struct ui_file
*file
, int from_tty
,
3132 struct cmd_list_element
*c
,
3135 fprintf_filtered (file
, _("The record/replay pt buffer size is %s.\n"),
3139 /* Initialize btrace commands. */
3142 _initialize_record_btrace (void)
3144 add_prefix_cmd ("btrace", class_obscure
, cmd_record_btrace_start
,
3145 _("Start branch trace recording."), &record_btrace_cmdlist
,
3146 "record btrace ", 0, &record_cmdlist
);
3147 add_alias_cmd ("b", "btrace", class_obscure
, 1, &record_cmdlist
);
3149 add_cmd ("bts", class_obscure
, cmd_record_btrace_bts_start
,
3151 Start branch trace recording in Branch Trace Store (BTS) format.\n\n\
3152 The processor stores a from/to record for each branch into a cyclic buffer.\n\
3153 This format may not be available on all processors."),
3154 &record_btrace_cmdlist
);
3155 add_alias_cmd ("bts", "btrace bts", class_obscure
, 1, &record_cmdlist
);
3157 add_cmd ("pt", class_obscure
, cmd_record_btrace_pt_start
,
3159 Start branch trace recording in Intel Processor Trace format.\n\n\
3160 This format may not be available on all processors."),
3161 &record_btrace_cmdlist
);
3162 add_alias_cmd ("pt", "btrace pt", class_obscure
, 1, &record_cmdlist
);
3164 add_prefix_cmd ("btrace", class_support
, cmd_set_record_btrace
,
3165 _("Set record options."), &set_record_btrace_cmdlist
,
3166 "set record btrace ", 0, &set_record_cmdlist
);
3168 add_prefix_cmd ("btrace", class_support
, cmd_show_record_btrace
,
3169 _("Show record options."), &show_record_btrace_cmdlist
,
3170 "show record btrace ", 0, &show_record_cmdlist
);
3172 add_setshow_enum_cmd ("replay-memory-access", no_class
,
3173 replay_memory_access_types
, &replay_memory_access
, _("\
3174 Set what memory accesses are allowed during replay."), _("\
3175 Show what memory accesses are allowed during replay."),
3176 _("Default is READ-ONLY.\n\n\
3177 The btrace record target does not trace data.\n\
3178 The memory therefore corresponds to the live target and not \
3179 to the current replay position.\n\n\
3180 When READ-ONLY, allow accesses to read-only memory during replay.\n\
3181 When READ-WRITE, allow accesses to read-only and read-write memory during \
3183 NULL
, cmd_show_replay_memory_access
,
3184 &set_record_btrace_cmdlist
,
3185 &show_record_btrace_cmdlist
);
3187 add_prefix_cmd ("cpu", class_support
, cmd_set_record_btrace_cpu
,
3189 Set the cpu to be used for trace decode.\n\n\
3190 The format is \"VENDOR:IDENTIFIER\" or \"none\" or \"auto\" (default).\n\
3191 For vendor \"intel\" the format is \"FAMILY/MODEL[/STEPPING]\".\n\n\
3192 When decoding branch trace, enable errata workarounds for the specified cpu.\n\
3193 The default is \"auto\", which uses the cpu on which the trace was recorded.\n\
3194 When GDB does not support that cpu, this option can be used to enable\n\
3195 workarounds for a similar cpu that GDB supports.\n\n\
3196 When set to \"none\", errata workarounds are disabled."),
3197 &set_record_btrace_cpu_cmdlist
,
3198 "set record btrace cpu ", 1,
3199 &set_record_btrace_cmdlist
);
3201 add_cmd ("auto", class_support
, cmd_set_record_btrace_cpu_auto
, _("\
3202 Automatically determine the cpu to be used for trace decode."),
3203 &set_record_btrace_cpu_cmdlist
);
3205 add_cmd ("none", class_support
, cmd_set_record_btrace_cpu_none
, _("\
3206 Do not enable errata workarounds for trace decode."),
3207 &set_record_btrace_cpu_cmdlist
);
3209 add_cmd ("cpu", class_support
, cmd_show_record_btrace_cpu
, _("\
3210 Show the cpu to be used for trace decode."),
3211 &show_record_btrace_cmdlist
);
3213 add_prefix_cmd ("bts", class_support
, cmd_set_record_btrace_bts
,
3214 _("Set record btrace bts options."),
3215 &set_record_btrace_bts_cmdlist
,
3216 "set record btrace bts ", 0, &set_record_btrace_cmdlist
);
3218 add_prefix_cmd ("bts", class_support
, cmd_show_record_btrace_bts
,
3219 _("Show record btrace bts options."),
3220 &show_record_btrace_bts_cmdlist
,
3221 "show record btrace bts ", 0, &show_record_btrace_cmdlist
);
3223 add_setshow_uinteger_cmd ("buffer-size", no_class
,
3224 &record_btrace_conf
.bts
.size
,
3225 _("Set the record/replay bts buffer size."),
3226 _("Show the record/replay bts buffer size."), _("\
3227 When starting recording request a trace buffer of this size. \
3228 The actual buffer size may differ from the requested size. \
3229 Use \"info record\" to see the actual buffer size.\n\n\
3230 Bigger buffers allow longer recording but also take more time to process \
3231 the recorded execution trace.\n\n\
3232 The trace buffer size may not be changed while recording."), NULL
,
3233 show_record_bts_buffer_size_value
,
3234 &set_record_btrace_bts_cmdlist
,
3235 &show_record_btrace_bts_cmdlist
);
3237 add_prefix_cmd ("pt", class_support
, cmd_set_record_btrace_pt
,
3238 _("Set record btrace pt options."),
3239 &set_record_btrace_pt_cmdlist
,
3240 "set record btrace pt ", 0, &set_record_btrace_cmdlist
);
3242 add_prefix_cmd ("pt", class_support
, cmd_show_record_btrace_pt
,
3243 _("Show record btrace pt options."),
3244 &show_record_btrace_pt_cmdlist
,
3245 "show record btrace pt ", 0, &show_record_btrace_cmdlist
);
3247 add_setshow_uinteger_cmd ("buffer-size", no_class
,
3248 &record_btrace_conf
.pt
.size
,
3249 _("Set the record/replay pt buffer size."),
3250 _("Show the record/replay pt buffer size."), _("\
3251 Bigger buffers allow longer recording but also take more time to process \
3252 the recorded execution.\n\
3253 The actual buffer size may differ from the requested size. Use \"info record\" \
3254 to see the actual buffer size."), NULL
, show_record_pt_buffer_size_value
,
3255 &set_record_btrace_pt_cmdlist
,
3256 &show_record_btrace_pt_cmdlist
);
3258 add_target (record_btrace_target_info
, record_btrace_target_open
);
3260 bfcache
= htab_create_alloc (50, bfcache_hash
, bfcache_eq
, NULL
,
3263 record_btrace_conf
.bts
.size
= 64 * 1024;
3264 record_btrace_conf
.pt
.size
= 16 * 1024;