forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord-btrace.c
3359 lines (2626 loc) · 85.9 KB
/
record-btrace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Branch trace support for GDB, the GNU debugger.
Copyright (C) 2013-2024 Free Software Foundation, Inc.
Contributed by Intel Corp. <[email protected]>
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "gdbsupport/gdb_vecs.h"
#include "record.h"
#include "record-btrace.h"
#include "gdbthread.h"
#include "target.h"
#include "cli/cli-cmds.h"
#include "disasm.h"
#include "observable.h"
#include "cli/cli-utils.h"
#include "source.h"
#include "top.h"
#include "ui-out.h"
#include "symtab.h"
#include "filenames.h"
#include "regcache.h"
#include "frame-unwind.h"
#include "hashtab.h"
#include "infrun.h"
#include "gdbsupport/event-loop.h"
#include "inf-loop.h"
#include "inferior.h"
#include <algorithm>
#include "gdbarch.h"
#include "cli/cli-style.h"
#include "async-event.h"
#include <forward_list>
#include "objfiles.h"
#include "interps.h"
static const target_info record_btrace_target_info = {
"record-btrace",
N_("Branch tracing target"),
N_("Collect control-flow trace and provide the execution history.")
};
/* The target_ops of record-btrace. */
class record_btrace_target final : public target_ops
{
public:
const target_info &info () const override
{ return record_btrace_target_info; }
strata stratum () const override { return record_stratum; }
void close () override;
void async (bool) override;
void detach (inferior *inf, int from_tty) override
{ record_detach (this, inf, from_tty); }
void disconnect (const char *, int) override;
void mourn_inferior () override
{ record_mourn_inferior (this); }
void kill () override
{ record_kill (this); }
enum record_method record_method (ptid_t ptid) override;
void stop_recording () override;
void info_record () override;
void insn_history (int size, gdb_disassembly_flags flags) override;
void insn_history_from (ULONGEST from, int size,
gdb_disassembly_flags flags) override;
void insn_history_range (ULONGEST begin, ULONGEST end,
gdb_disassembly_flags flags) override;
void call_history (int size, record_print_flags flags) override;
void call_history_from (ULONGEST begin, int size, record_print_flags flags)
override;
void call_history_range (ULONGEST begin, ULONGEST end, record_print_flags flags)
override;
bool record_is_replaying (ptid_t ptid) override;
bool record_will_replay (ptid_t ptid, int dir) override;
void record_stop_replaying () override;
enum target_xfer_status xfer_partial (enum target_object object,
const char *annex,
gdb_byte *readbuf,
const gdb_byte *writebuf,
ULONGEST offset, ULONGEST len,
ULONGEST *xfered_len) override;
int insert_breakpoint (struct gdbarch *,
struct bp_target_info *) override;
int remove_breakpoint (struct gdbarch *, struct bp_target_info *,
enum remove_bp_reason) override;
void fetch_registers (struct regcache *, int) override;
void store_registers (struct regcache *, int) override;
void prepare_to_store (struct regcache *) override;
const struct frame_unwind *get_unwinder () override;
const struct frame_unwind *get_tailcall_unwinder () override;
void resume (ptid_t, int, enum gdb_signal) override;
ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
void stop (ptid_t) override;
void update_thread_list () override;
bool thread_alive (ptid_t ptid) override;
void goto_record_begin () override;
void goto_record_end () override;
void goto_record (ULONGEST insn) override;
bool can_execute_reverse () override;
bool stopped_by_sw_breakpoint () override;
bool supports_stopped_by_sw_breakpoint () override;
bool stopped_by_hw_breakpoint () override;
bool supports_stopped_by_hw_breakpoint () override;
enum exec_direction_kind execution_direction () override;
void prepare_to_generate_core () override;
void done_generating_core () override;
};
static record_btrace_target record_btrace_ops;
/* Initialize the record-btrace target ops. */
/* Token associated with a new-thread observer enabling branch tracing
for the new thread. */
static const gdb::observers::token record_btrace_thread_observer_token {};
/* Memory access types used in set/show record btrace replay-memory-access. */
static const char replay_memory_access_read_only[] = "read-only";
static const char replay_memory_access_read_write[] = "read-write";
static const char *const replay_memory_access_types[] =
{
replay_memory_access_read_only,
replay_memory_access_read_write,
NULL
};
/* The currently allowed replay memory access type. */
static const char *replay_memory_access = replay_memory_access_read_only;
/* The cpu state kinds. */
enum record_btrace_cpu_state_kind
{
CS_AUTO,
CS_NONE,
CS_CPU
};
/* The current cpu state. */
static enum record_btrace_cpu_state_kind record_btrace_cpu_state = CS_AUTO;
/* The current cpu for trace decode. */
static struct btrace_cpu record_btrace_cpu;
/* Command lists for "set/show record btrace". */
static struct cmd_list_element *set_record_btrace_cmdlist;
static struct cmd_list_element *show_record_btrace_cmdlist;
/* The execution direction of the last resume we got. See record-full.c. */
static enum exec_direction_kind record_btrace_resume_exec_dir = EXEC_FORWARD;
/* The async event handler for reverse/replay execution. */
static struct async_event_handler *record_btrace_async_inferior_event_handler;
/* A flag indicating that we are currently generating a core file. */
static int record_btrace_generating_corefile;
/* The current branch trace configuration. */
static struct btrace_config record_btrace_conf;
/* Command list for "record btrace". */
static struct cmd_list_element *record_btrace_cmdlist;
/* Command lists for "set/show record btrace bts". */
static struct cmd_list_element *set_record_btrace_bts_cmdlist;
static struct cmd_list_element *show_record_btrace_bts_cmdlist;
/* Command lists for "set/show record btrace pt". */
static struct cmd_list_element *set_record_btrace_pt_cmdlist;
static struct cmd_list_element *show_record_btrace_pt_cmdlist;
/* Command list for "set record btrace cpu". */
static struct cmd_list_element *set_record_btrace_cpu_cmdlist;
/* Print a record-btrace debug message. Use do ... while (0) to avoid
ambiguities when used in if statements. */
#define DEBUG(msg, args...) \
do \
{ \
if (record_debug != 0) \
gdb_printf (gdb_stdlog, \
"[record-btrace] " msg "\n", ##args); \
} \
while (0)
/* Return the cpu configured by the user. Returns NULL if the cpu was
configured as auto. */
const struct btrace_cpu *
record_btrace_get_cpu (void)
{
switch (record_btrace_cpu_state)
{
case CS_AUTO:
return nullptr;
case CS_NONE:
record_btrace_cpu.vendor = CV_UNKNOWN;
[[fallthrough]];
case CS_CPU:
return &record_btrace_cpu;
}
error (_("Internal error: bad record btrace cpu state."));
}
/* Update the branch trace for the current thread and return a pointer to its
thread_info.
Throws an error if there is no thread or no trace. This function never
returns NULL. */
static struct thread_info *
require_btrace_thread (void)
{
DEBUG ("require");
if (inferior_ptid == null_ptid)
error (_("No thread."));
thread_info *tp = inferior_thread ();
validate_registers_access ();
btrace_fetch (tp, record_btrace_get_cpu ());
if (btrace_is_empty (tp))
error (_("No trace."));
return tp;
}
/* Update the branch trace for the current thread and return a pointer to its
branch trace information struct.
Throws an error if there is no thread or no trace. This function never
returns NULL. */
static struct btrace_thread_info *
require_btrace (void)
{
struct thread_info *tp;
tp = require_btrace_thread ();
return &tp->btrace;
}
/* The new thread observer. */
static void
record_btrace_on_new_thread (struct thread_info *tp)
{
/* Ignore this thread if its inferior is not recorded by us. */
target_ops *rec = tp->inf->target_at (record_stratum);
if (rec != &record_btrace_ops)
return;
try
{
btrace_enable (tp, &record_btrace_conf);
}
catch (const gdb_exception_error &error)
{
warning ("%s", error.what ());
}
}
/* Enable automatic tracing of new threads. */
static void
record_btrace_auto_enable (void)
{
DEBUG ("attach thread observer");
gdb::observers::new_thread.attach (record_btrace_on_new_thread,
record_btrace_thread_observer_token,
"record-btrace");
}
/* Disable automatic tracing of new threads. */
static void
record_btrace_auto_disable (void)
{
DEBUG ("detach thread observer");
gdb::observers::new_thread.detach (record_btrace_thread_observer_token);
}
/* The record-btrace async event handler function. */
static void
record_btrace_handle_async_inferior_event (gdb_client_data data)
{
inferior_event_handler (INF_REG_EVENT);
}
/* See record-btrace.h. */
void
record_btrace_push_target (void)
{
const char *format;
record_btrace_auto_enable ();
current_inferior ()->push_target (&record_btrace_ops);
record_btrace_async_inferior_event_handler
= create_async_event_handler (record_btrace_handle_async_inferior_event,
NULL, "record-btrace");
record_btrace_generating_corefile = 0;
format = btrace_format_short_string (record_btrace_conf.format);
interps_notify_record_changed (current_inferior (), 1, "btrace", format);
}
/* Disable btrace on a set of threads on scope exit. */
struct scoped_btrace_disable
{
scoped_btrace_disable () = default;
DISABLE_COPY_AND_ASSIGN (scoped_btrace_disable);
~scoped_btrace_disable ()
{
for (thread_info *tp : m_threads)
btrace_disable (tp);
}
void add_thread (thread_info *thread)
{
m_threads.push_front (thread);
}
void discard ()
{
m_threads.clear ();
}
private:
std::forward_list<thread_info *> m_threads;
};
/* Open target record-btrace. */
static void
record_btrace_target_open (const char *args, int from_tty)
{
/* If we fail to enable btrace for one thread, disable it for the threads for
which it was successfully enabled. */
scoped_btrace_disable btrace_disable;
DEBUG ("open");
record_preopen ();
if (!target_has_execution ())
error (_("The program is not being run."));
for (thread_info *tp : current_inferior ()->non_exited_threads ())
if (args == NULL || *args == 0 || number_is_in_list (args, tp->global_num))
{
btrace_enable (tp, &record_btrace_conf);
btrace_disable.add_thread (tp);
}
record_btrace_push_target ();
btrace_disable.discard ();
}
/* The stop_recording method of target record-btrace. */
void
record_btrace_target::stop_recording ()
{
DEBUG ("stop recording");
record_btrace_auto_disable ();
for (thread_info *tp : current_inferior ()->non_exited_threads ())
if (tp->btrace.target != NULL)
btrace_disable (tp);
}
/* The disconnect method of target record-btrace. */
void
record_btrace_target::disconnect (const char *args,
int from_tty)
{
struct target_ops *beneath = this->beneath ();
/* Do not stop recording, just clean up GDB side. */
current_inferior ()->unpush_target (this);
/* Forward disconnect. */
beneath->disconnect (args, from_tty);
}
/* The close method of target record-btrace. */
void
record_btrace_target::close ()
{
if (record_btrace_async_inferior_event_handler != NULL)
delete_async_event_handler (&record_btrace_async_inferior_event_handler);
/* Make sure automatic recording gets disabled even if we did not stop
recording before closing the record-btrace target. */
record_btrace_auto_disable ();
/* We should have already stopped recording.
Tear down btrace in case we have not. */
for (thread_info *tp : current_inferior ()->non_exited_threads ())
btrace_teardown (tp);
}
/* The async method of target record-btrace. */
void
record_btrace_target::async (bool enable)
{
if (enable)
mark_async_event_handler (record_btrace_async_inferior_event_handler);
else
clear_async_event_handler (record_btrace_async_inferior_event_handler);
this->beneath ()->async (enable);
}
/* Adjusts the size and returns a human readable size suffix. */
static const char *
record_btrace_adjust_size (unsigned int *size)
{
unsigned int sz;
sz = *size;
if ((sz & ((1u << 30) - 1)) == 0)
{
*size = sz >> 30;
return "GB";
}
else if ((sz & ((1u << 20) - 1)) == 0)
{
*size = sz >> 20;
return "MB";
}
else if ((sz & ((1u << 10) - 1)) == 0)
{
*size = sz >> 10;
return "kB";
}
else
return "";
}
/* Print a BTS configuration. */
static void
record_btrace_print_bts_conf (const struct btrace_config_bts *conf)
{
const char *suffix;
unsigned int size;
size = conf->size;
if (size > 0)
{
suffix = record_btrace_adjust_size (&size);
gdb_printf (_("Buffer size: %u%s.\n"), size, suffix);
}
}
/* Print an Intel Processor Trace configuration. */
static void
record_btrace_print_pt_conf (const struct btrace_config_pt *conf)
{
const char *suffix;
unsigned int size;
size = conf->size;
if (size > 0)
{
suffix = record_btrace_adjust_size (&size);
gdb_printf (_("Buffer size: %u%s.\n"), size, suffix);
}
}
/* Print a branch tracing configuration. */
static void
record_btrace_print_conf (const struct btrace_config *conf)
{
gdb_printf (_("Recording format: %s.\n"),
btrace_format_string (conf->format));
switch (conf->format)
{
case BTRACE_FORMAT_NONE:
return;
case BTRACE_FORMAT_BTS:
record_btrace_print_bts_conf (&conf->bts);
return;
case BTRACE_FORMAT_PT:
record_btrace_print_pt_conf (&conf->pt);
return;
}
internal_error (_("Unknown branch trace format."));
}
/* The info_record method of target record-btrace. */
void
record_btrace_target::info_record ()
{
struct btrace_thread_info *btinfo;
const struct btrace_config *conf;
struct thread_info *tp;
unsigned int insns, calls, gaps;
DEBUG ("info");
if (inferior_ptid == null_ptid)
error (_("No thread."));
tp = inferior_thread ();
validate_registers_access ();
btinfo = &tp->btrace;
conf = ::btrace_conf (btinfo);
if (conf != NULL)
record_btrace_print_conf (conf);
btrace_fetch (tp, record_btrace_get_cpu ());
insns = 0;
calls = 0;
gaps = 0;
if (!btrace_is_empty (tp))
{
struct btrace_call_iterator call;
struct btrace_insn_iterator insn;
btrace_call_end (&call, btinfo);
btrace_call_prev (&call, 1);
calls = btrace_call_number (&call);
btrace_insn_end (&insn, btinfo);
insns = btrace_insn_number (&insn);
/* If the last instruction is not a gap, it is the current instruction
that is not actually part of the record. */
if (btrace_insn_get (&insn) != NULL)
insns -= 1;
gaps = btinfo->ngaps;
}
gdb_printf (_("Recorded %u instructions in %u functions (%u gaps) "
"for thread %s (%s).\n"), insns, calls, gaps,
print_thread_id (tp),
target_pid_to_str (tp->ptid).c_str ());
if (btrace_is_replaying (tp))
gdb_printf (_("Replay in progress. At instruction %u.\n"),
btrace_insn_number (btinfo->replay));
}
/* Print a decode error. */
static void
btrace_ui_out_decode_error (struct ui_out *uiout, int errcode,
enum btrace_format format)
{
const char *errstr = btrace_decode_error (format, errcode);
uiout->text (_("["));
/* ERRCODE > 0 indicates notifications on BTRACE_FORMAT_PT. */
if (!(format == BTRACE_FORMAT_PT && errcode > 0))
{
uiout->text (_("decode error ("));
uiout->field_signed ("errcode", errcode);
uiout->text (_("): "));
}
uiout->text (errstr);
uiout->text (_("]\n"));
}
/* A range of source lines. */
struct btrace_line_range
{
/* The symtab this line is from. */
struct symtab *symtab;
/* The first line (inclusive). */
int begin;
/* The last line (exclusive). */
int end;
};
/* Construct a line range. */
static struct btrace_line_range
btrace_mk_line_range (struct symtab *symtab, int begin, int end)
{
struct btrace_line_range range;
range.symtab = symtab;
range.begin = begin;
range.end = end;
return range;
}
/* Add a line to a line range. */
static struct btrace_line_range
btrace_line_range_add (struct btrace_line_range range, int line)
{
if (range.end <= range.begin)
{
/* This is the first entry. */
range.begin = line;
range.end = line + 1;
}
else if (line < range.begin)
range.begin = line;
else if (range.end < line)
range.end = line;
return range;
}
/* Return non-zero if RANGE is empty, zero otherwise. */
static int
btrace_line_range_is_empty (struct btrace_line_range range)
{
return range.end <= range.begin;
}
/* Return non-zero if LHS contains RHS, zero otherwise. */
static int
btrace_line_range_contains_range (struct btrace_line_range lhs,
struct btrace_line_range rhs)
{
return ((lhs.symtab == rhs.symtab)
&& (lhs.begin <= rhs.begin)
&& (rhs.end <= lhs.end));
}
/* Find the line range associated with PC. */
static struct btrace_line_range
btrace_find_line_range (CORE_ADDR pc)
{
struct btrace_line_range range;
const linetable_entry *lines;
const linetable *ltable;
struct symtab *symtab;
int nlines, i;
symtab = find_pc_line_symtab (pc);
if (symtab == NULL)
return btrace_mk_line_range (NULL, 0, 0);
ltable = symtab->linetable ();
if (ltable == NULL)
return btrace_mk_line_range (symtab, 0, 0);
nlines = ltable->nitems;
lines = ltable->item;
if (nlines <= 0)
return btrace_mk_line_range (symtab, 0, 0);
struct objfile *objfile = symtab->compunit ()->objfile ();
unrelocated_addr unrel_pc
= unrelocated_addr (pc - objfile->text_section_offset ());
range = btrace_mk_line_range (symtab, 0, 0);
for (i = 0; i < nlines - 1; i++)
{
/* The test of is_stmt here was added when the is_stmt field was
introduced to the 'struct linetable_entry' structure. This
ensured that this loop maintained the same behavior as before we
introduced is_stmt. That said, it might be that we would be
better off not checking is_stmt here, this would lead to us
possibly adding more line numbers to the range. At the time this
change was made I was unsure how to test this so chose to go with
maintaining the existing experience. */
if (lines[i].unrelocated_pc () == unrel_pc && lines[i].line != 0
&& lines[i].is_stmt)
range = btrace_line_range_add (range, lines[i].line);
}
return range;
}
/* Print source lines in LINES to UIOUT.
UI_ITEM_CHAIN is a cleanup chain for the last source line and the
instructions corresponding to that source line. When printing a new source
line, we do the cleanups for the open chain and open a new cleanup chain for
the new source line. If the source line range in LINES is not empty, this
function will leave the cleanup chain for the last printed source line open
so instructions can be added to it. */
static void
btrace_print_lines (struct btrace_line_range lines, struct ui_out *uiout,
std::optional<ui_out_emit_tuple> *src_and_asm_tuple,
std::optional<ui_out_emit_list> *asm_list,
gdb_disassembly_flags flags)
{
print_source_lines_flags psl_flags;
if (flags & DISASSEMBLY_FILENAME)
psl_flags |= PRINT_SOURCE_LINES_FILENAME;
for (int line = lines.begin; line < lines.end; ++line)
{
asm_list->reset ();
src_and_asm_tuple->emplace (uiout, "src_and_asm_line");
print_source_lines (lines.symtab, line, line + 1, psl_flags);
asm_list->emplace (uiout, "line_asm_insn");
}
}
/* Disassemble a section of the recorded instruction trace. */
static void
btrace_insn_history (struct ui_out *uiout,
const struct btrace_thread_info *btinfo,
const struct btrace_insn_iterator *begin,
const struct btrace_insn_iterator *end,
gdb_disassembly_flags flags)
{
DEBUG ("itrace (0x%x): [%u; %u)", (unsigned) flags,
btrace_insn_number (begin), btrace_insn_number (end));
flags |= DISASSEMBLY_SPECULATIVE;
gdbarch *gdbarch = current_inferior ()->arch ();
btrace_line_range last_lines = btrace_mk_line_range (NULL, 0, 0);
ui_out_emit_list list_emitter (uiout, "asm_insns");
std::optional<ui_out_emit_tuple> src_and_asm_tuple;
std::optional<ui_out_emit_list> asm_list;
gdb_pretty_print_disassembler disasm (gdbarch, uiout);
for (btrace_insn_iterator it = *begin; btrace_insn_cmp (&it, end) != 0;
btrace_insn_next (&it, 1))
{
const struct btrace_insn *insn;
insn = btrace_insn_get (&it);
/* A NULL instruction indicates a gap in the trace. */
if (insn == NULL)
{
const struct btrace_config *conf;
conf = btrace_conf (btinfo);
/* We have trace so we must have a configuration. */
gdb_assert (conf != NULL);
uiout->field_fmt ("insn-number", "%u",
btrace_insn_number (&it));
uiout->text ("\t");
btrace_ui_out_decode_error (uiout, btrace_insn_get_error (&it),
conf->format);
}
else if (insn->iclass == BTRACE_INSN_AUX)
{
if ((flags & DISASSEMBLY_OMIT_AUX_INSN) != 0)
continue;
uiout->field_fmt ("insn-number", "%u", btrace_insn_number (&it));
uiout->text ("\t");
/* Add 3 spaces to match the instructions and 2 to indent the aux
string to make it more visible. */
uiout->spaces (5);
uiout->text ("[");
uiout->field_fmt ("aux-data", "%s",
it.btinfo->aux_data.at
(insn->aux_data_index).c_str ());
uiout->text ("]\n");
}
else
{
struct disasm_insn dinsn;
if ((flags & DISASSEMBLY_SOURCE) != 0)
{
struct btrace_line_range lines;
lines = btrace_find_line_range (insn->pc);
if (!btrace_line_range_is_empty (lines)
&& !btrace_line_range_contains_range (last_lines, lines))
{
btrace_print_lines (lines, uiout, &src_and_asm_tuple, &asm_list,
flags);
last_lines = lines;
}
else if (!src_and_asm_tuple.has_value ())
{
gdb_assert (!asm_list.has_value ());
src_and_asm_tuple.emplace (uiout, "src_and_asm_line");
/* No source information. */
asm_list.emplace (uiout, "line_asm_insn");
}
gdb_assert (src_and_asm_tuple.has_value ());
gdb_assert (asm_list.has_value ());
}
memset (&dinsn, 0, sizeof (dinsn));
dinsn.number = btrace_insn_number (&it);
dinsn.addr = insn->pc;
if ((insn->flags & BTRACE_INSN_FLAG_SPECULATIVE) != 0)
dinsn.is_speculative = 1;
disasm.pretty_print_insn (&dinsn, flags);
}
}
}
/* The insn_history method of target record-btrace. */
void
record_btrace_target::insn_history (int size, gdb_disassembly_flags flags)
{
struct btrace_thread_info *btinfo;
struct btrace_insn_history *history;
struct btrace_insn_iterator begin, end;
struct ui_out *uiout;
unsigned int context, covered;
uiout = current_uiout;
ui_out_emit_tuple tuple_emitter (uiout, "insn history");
context = abs (size);
if (context == 0)
error (_("Bad record instruction-history-size."));
btinfo = require_btrace ();
history = btinfo->insn_history;
if (history == NULL)
{
struct btrace_insn_iterator *replay;
DEBUG ("insn-history (0x%x): %d", (unsigned) flags, size);
/* If we're replaying, we start at the replay position. Otherwise, we
start at the tail of the trace. */
replay = btinfo->replay;
if (replay != NULL)
begin = *replay;
else
btrace_insn_end (&begin, btinfo);
/* We start from here and expand in the requested direction. Then we
expand in the other direction, as well, to fill up any remaining
context. */
end = begin;
if (size < 0)
{
/* We want the current position covered, as well. */
covered = btrace_insn_next (&end, 1);
covered += btrace_insn_prev (&begin, context - covered);
covered += btrace_insn_next (&end, context - covered);
}
else
{
covered = btrace_insn_next (&end, context);
covered += btrace_insn_prev (&begin, context - covered);
}
}
else
{
begin = history->begin;
end = history->end;
DEBUG ("insn-history (0x%x): %d, prev: [%u; %u)", (unsigned) flags, size,
btrace_insn_number (&begin), btrace_insn_number (&end));
if (size < 0)
{
end = begin;
covered = btrace_insn_prev (&begin, context);
}
else
{
begin = end;
covered = btrace_insn_next (&end, context);
}
}
if (covered > 0)
btrace_insn_history (uiout, btinfo, &begin, &end, flags);
else
{
if (size < 0)
gdb_printf (_("At the start of the branch trace record.\n"));
else
gdb_printf (_("At the end of the branch trace record.\n"));
}
btrace_set_insn_history (btinfo, &begin, &end);
}
/* The insn_history_range method of target record-btrace. */
void
record_btrace_target::insn_history_range (ULONGEST from, ULONGEST to,
gdb_disassembly_flags flags)
{
struct btrace_thread_info *btinfo;
struct btrace_insn_iterator begin, end;
struct ui_out *uiout;
unsigned int low, high;
int found;
uiout = current_uiout;
ui_out_emit_tuple tuple_emitter (uiout, "insn history");
low = from;
high = to;
DEBUG ("insn-history (0x%x): [%u; %u)", (unsigned) flags, low, high);
/* Check for wrap-arounds. */
if (low != from || high != to)
error (_("Bad range."));
if (high < low)
error (_("Bad range."));
btinfo = require_btrace ();
found = btrace_find_insn_by_number (&begin, btinfo, low);