forked from gbenson/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord-btrace.c
2637 lines (2076 loc) · 66.5 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-2015 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 "defs.h"
#include "record.h"
#include "gdbthread.h"
#include "target.h"
#include "gdbcmd.h"
#include "disasm.h"
#include "observer.h"
#include "cli/cli-utils.h"
#include "source.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 "event-loop.h"
#include "inf-loop.h"
/* The target_ops of record-btrace. */
static struct target_ops record_btrace_ops;
/* A new thread observer enabling branch tracing for the new thread. */
static struct observer *record_btrace_thread_observer;
/* 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;
/* 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;
/* 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) \
fprintf_unfiltered (gdb_stdlog, \
"[record-btrace] " msg "\n", ##args); \
} \
while (0)
/* 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)
{
struct thread_info *tp;
DEBUG ("require");
tp = find_thread_ptid (inferior_ptid);
if (tp == NULL)
error (_("No thread."));
btrace_fetch (tp);
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;
}
/* Enable branch tracing for one thread. Warn on errors. */
static void
record_btrace_enable_warn (struct thread_info *tp)
{
TRY
{
btrace_enable (tp, &record_btrace_conf);
}
CATCH (error, RETURN_MASK_ERROR)
{
warning ("%s", error.message);
}
END_CATCH
}
/* Callback function to disable branch tracing for one thread. */
static void
record_btrace_disable_callback (void *arg)
{
struct thread_info *tp;
tp = arg;
btrace_disable (tp);
}
/* Enable automatic tracing of new threads. */
static void
record_btrace_auto_enable (void)
{
DEBUG ("attach thread observer");
record_btrace_thread_observer
= observer_attach_new_thread (record_btrace_enable_warn);
}
/* Disable automatic tracing of new threads. */
static void
record_btrace_auto_disable (void)
{
/* The observer may have been detached, already. */
if (record_btrace_thread_observer == NULL)
return;
DEBUG ("detach thread observer");
observer_detach_new_thread (record_btrace_thread_observer);
record_btrace_thread_observer = NULL;
}
/* 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, NULL);
}
/* The to_open method of target record-btrace. */
static void
record_btrace_open (const char *args, int from_tty)
{
struct cleanup *disable_chain;
struct thread_info *tp;
DEBUG ("open");
record_preopen ();
if (!target_has_execution)
error (_("The program is not being run."));
if (non_stop)
error (_("Record btrace can't debug inferior in non-stop mode."));
gdb_assert (record_btrace_thread_observer == NULL);
disable_chain = make_cleanup (null_cleanup, NULL);
ALL_NON_EXITED_THREADS (tp)
if (args == NULL || *args == 0 || number_is_in_list (args, tp->num))
{
btrace_enable (tp, &record_btrace_conf);
make_cleanup (record_btrace_disable_callback, tp);
}
record_btrace_auto_enable ();
push_target (&record_btrace_ops);
record_btrace_async_inferior_event_handler
= create_async_event_handler (record_btrace_handle_async_inferior_event,
NULL);
record_btrace_generating_corefile = 0;
observer_notify_record_changed (current_inferior (), 1);
discard_cleanups (disable_chain);
}
/* The to_stop_recording method of target record-btrace. */
static void
record_btrace_stop_recording (struct target_ops *self)
{
struct thread_info *tp;
DEBUG ("stop recording");
record_btrace_auto_disable ();
ALL_NON_EXITED_THREADS (tp)
if (tp->btrace.target != NULL)
btrace_disable (tp);
}
/* The to_close method of target record-btrace. */
static void
record_btrace_close (struct target_ops *self)
{
struct thread_info *tp;
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. */
ALL_NON_EXITED_THREADS (tp)
btrace_teardown (tp);
}
/* The to_async method of target record-btrace. */
static void
record_btrace_async (struct target_ops *ops, int enable)
{
if (enable)
mark_async_event_handler (record_btrace_async_inferior_event_handler);
else
clear_async_event_handler (record_btrace_async_inferior_event_handler);
ops->beneath->to_async (ops->beneath, 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);
printf_unfiltered (_("Buffer size: %u%s.\n"), size, suffix);
}
}
/* Print an Intel(R) 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);
printf_unfiltered (_("Buffer size: %u%s.\n"), size, suffix);
}
}
/* Print a branch tracing configuration. */
static void
record_btrace_print_conf (const struct btrace_config *conf)
{
printf_unfiltered (_("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 (__FILE__, __LINE__, _("Unkown branch trace format."));
}
/* The to_info_record method of target record-btrace. */
static void
record_btrace_info (struct target_ops *self)
{
struct btrace_thread_info *btinfo;
const struct btrace_config *conf;
struct thread_info *tp;
unsigned int insns, calls, gaps;
DEBUG ("info");
tp = find_thread_ptid (inferior_ptid);
if (tp == NULL)
error (_("No thread."));
btinfo = &tp->btrace;
conf = btrace_conf (btinfo);
if (conf != NULL)
record_btrace_print_conf (conf);
btrace_fetch (tp);
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 (insns != 0)
{
/* The last instruction does not really belong to the trace. */
insns -= 1;
}
else
{
unsigned int steps;
/* Skip gaps at the end. */
do
{
steps = btrace_insn_prev (&insn, 1);
if (steps == 0)
break;
insns = btrace_insn_number (&insn);
}
while (insns == 0);
}
gaps = btinfo->ngaps;
}
printf_unfiltered (_("Recorded %u instructions in %u functions (%u gaps) "
"for thread %d (%s).\n"), insns, calls, gaps,
tp->num, target_pid_to_str (tp->ptid));
if (btrace_is_replaying (tp))
printf_unfiltered (_("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;
int is_error;
errstr = _("unknown");
is_error = 1;
switch (format)
{
default:
break;
case BTRACE_FORMAT_BTS:
switch (errcode)
{
default:
break;
case BDE_BTS_OVERFLOW:
errstr = _("instruction overflow");
break;
case BDE_BTS_INSN_SIZE:
errstr = _("unknown instruction");
break;
}
break;
#if defined (HAVE_LIBIPT)
case BTRACE_FORMAT_PT:
switch (errcode)
{
case BDE_PT_USER_QUIT:
is_error = 0;
errstr = _("trace decode cancelled");
break;
case BDE_PT_DISABLED:
is_error = 0;
errstr = _("disabled");
break;
case BDE_PT_OVERFLOW:
is_error = 0;
errstr = _("overflow");
break;
default:
if (errcode < 0)
errstr = pt_errstr (pt_errcode (errcode));
break;
}
break;
#endif /* defined (HAVE_LIBIPT) */
}
ui_out_text (uiout, _("["));
if (is_error)
{
ui_out_text (uiout, _("decode error ("));
ui_out_field_int (uiout, "errcode", errcode);
ui_out_text (uiout, _("): "));
}
ui_out_text (uiout, errstr);
ui_out_text (uiout, _("]\n"));
}
/* Print an unsigned int. */
static void
ui_out_field_uint (struct ui_out *uiout, const char *fld, unsigned int val)
{
ui_out_field_fmt (uiout, fld, "%u", val);
}
/* 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, int flags)
{
struct gdbarch *gdbarch;
struct btrace_insn_iterator it;
DEBUG ("itrace (0x%x): [%u; %u)", flags, btrace_insn_number (begin),
btrace_insn_number (end));
gdbarch = target_gdbarch ();
for (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);
btrace_ui_out_decode_error (uiout, it.function->errcode,
conf->format);
}
else
{
char prefix[4];
/* We may add a speculation prefix later. We use the same space
that is used for the pc prefix. */
if ((flags & DISASSEMBLY_OMIT_PC) == 0)
strncpy (prefix, pc_prefix (insn->pc), 3);
else
{
prefix[0] = ' ';
prefix[1] = ' ';
prefix[2] = ' ';
}
prefix[3] = 0;
/* Print the instruction index. */
ui_out_field_uint (uiout, "index", btrace_insn_number (&it));
ui_out_text (uiout, "\t");
/* Indicate speculative execution by a leading '?'. */
if ((insn->flags & BTRACE_INSN_FLAG_SPECULATIVE) != 0)
prefix[0] = '?';
/* Print the prefix; we tell gdb_disassembly below to omit it. */
ui_out_field_fmt (uiout, "prefix", "%s", prefix);
/* Disassembly with '/m' flag may not produce the expected result.
See PR gdb/11833. */
gdb_disassembly (gdbarch, uiout, NULL, flags | DISASSEMBLY_OMIT_PC,
1, insn->pc, insn->pc + 1);
}
}
}
/* The to_insn_history method of target record-btrace. */
static void
record_btrace_insn_history (struct target_ops *self, int size, int flags)
{
struct btrace_thread_info *btinfo;
struct btrace_insn_history *history;
struct btrace_insn_iterator begin, end;
struct cleanup *uiout_cleanup;
struct ui_out *uiout;
unsigned int context, covered;
uiout = current_uiout;
uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (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", 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)", 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)
printf_unfiltered (_("At the start of the branch trace record.\n"));
else
printf_unfiltered (_("At the end of the branch trace record.\n"));
}
btrace_set_insn_history (btinfo, &begin, &end);
do_cleanups (uiout_cleanup);
}
/* The to_insn_history_range method of target record-btrace. */
static void
record_btrace_insn_history_range (struct target_ops *self,
ULONGEST from, ULONGEST to, int flags)
{
struct btrace_thread_info *btinfo;
struct btrace_insn_history *history;
struct btrace_insn_iterator begin, end;
struct cleanup *uiout_cleanup;
struct ui_out *uiout;
unsigned int low, high;
int found;
uiout = current_uiout;
uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
"insn history");
low = from;
high = to;
DEBUG ("insn-history (0x%x): [%u; %u)", 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);
if (found == 0)
error (_("Range out of bounds."));
found = btrace_find_insn_by_number (&end, btinfo, high);
if (found == 0)
{
/* Silently truncate the range. */
btrace_insn_end (&end, btinfo);
}
else
{
/* We want both begin and end to be inclusive. */
btrace_insn_next (&end, 1);
}
btrace_insn_history (uiout, btinfo, &begin, &end, flags);
btrace_set_insn_history (btinfo, &begin, &end);
do_cleanups (uiout_cleanup);
}
/* The to_insn_history_from method of target record-btrace. */
static void
record_btrace_insn_history_from (struct target_ops *self,
ULONGEST from, int size, int flags)
{
ULONGEST begin, end, context;
context = abs (size);
if (context == 0)
error (_("Bad record instruction-history-size."));
if (size < 0)
{
end = from;
if (from < context)
begin = 0;
else
begin = from - context + 1;
}
else
{
begin = from;
end = from + context - 1;
/* Check for wrap-around. */
if (end < begin)
end = ULONGEST_MAX;
}
record_btrace_insn_history_range (self, begin, end, flags);
}
/* Print the instruction number range for a function call history line. */
static void
btrace_call_history_insn_range (struct ui_out *uiout,
const struct btrace_function *bfun)
{
unsigned int begin, end, size;
size = VEC_length (btrace_insn_s, bfun->insn);
gdb_assert (size > 0);
begin = bfun->insn_offset;
end = begin + size - 1;
ui_out_field_uint (uiout, "insn begin", begin);
ui_out_text (uiout, ",");
ui_out_field_uint (uiout, "insn end", end);
}
/* Compute the lowest and highest source line for the instructions in BFUN
and return them in PBEGIN and PEND.
Ignore instructions that can't be mapped to BFUN, e.g. instructions that
result from inlining or macro expansion. */
static void
btrace_compute_src_line_range (const struct btrace_function *bfun,
int *pbegin, int *pend)
{
struct btrace_insn *insn;
struct symtab *symtab;
struct symbol *sym;
unsigned int idx;
int begin, end;
begin = INT_MAX;
end = INT_MIN;
sym = bfun->sym;
if (sym == NULL)
goto out;
symtab = symbol_symtab (sym);
for (idx = 0; VEC_iterate (btrace_insn_s, bfun->insn, idx, insn); ++idx)
{
struct symtab_and_line sal;
sal = find_pc_line (insn->pc, 0);
if (sal.symtab != symtab || sal.line == 0)
continue;
begin = min (begin, sal.line);
end = max (end, sal.line);
}
out:
*pbegin = begin;
*pend = end;
}
/* Print the source line information for a function call history line. */
static void
btrace_call_history_src_line (struct ui_out *uiout,
const struct btrace_function *bfun)
{
struct symbol *sym;
int begin, end;
sym = bfun->sym;
if (sym == NULL)
return;
ui_out_field_string (uiout, "file",
symtab_to_filename_for_display (symbol_symtab (sym)));
btrace_compute_src_line_range (bfun, &begin, &end);
if (end < begin)
return;
ui_out_text (uiout, ":");
ui_out_field_int (uiout, "min line", begin);
if (end == begin)
return;
ui_out_text (uiout, ",");
ui_out_field_int (uiout, "max line", end);
}
/* Get the name of a branch trace function. */
static const char *
btrace_get_bfun_name (const struct btrace_function *bfun)
{
struct minimal_symbol *msym;
struct symbol *sym;
if (bfun == NULL)
return "??";
msym = bfun->msym;
sym = bfun->sym;
if (sym != NULL)
return SYMBOL_PRINT_NAME (sym);
else if (msym != NULL)
return MSYMBOL_PRINT_NAME (msym);
else
return "??";
}
/* Disassemble a section of the recorded function trace. */
static void
btrace_call_history (struct ui_out *uiout,
const struct btrace_thread_info *btinfo,
const struct btrace_call_iterator *begin,
const struct btrace_call_iterator *end,
enum record_print_flag flags)
{
struct btrace_call_iterator it;
DEBUG ("ftrace (0x%x): [%u; %u)", flags, btrace_call_number (begin),
btrace_call_number (end));
for (it = *begin; btrace_call_cmp (&it, end) < 0; btrace_call_next (&it, 1))
{
const struct btrace_function *bfun;
struct minimal_symbol *msym;
struct symbol *sym;
bfun = btrace_call_get (&it);
sym = bfun->sym;
msym = bfun->msym;
/* Print the function index. */
ui_out_field_uint (uiout, "index", bfun->number);
ui_out_text (uiout, "\t");
/* Indicate gaps in the trace. */
if (bfun->errcode != 0)
{
const struct btrace_config *conf;
conf = btrace_conf (btinfo);
/* We have trace so we must have a configuration. */
gdb_assert (conf != NULL);
btrace_ui_out_decode_error (uiout, bfun->errcode, conf->format);
continue;
}
if ((flags & RECORD_PRINT_INDENT_CALLS) != 0)
{
int level = bfun->level + btinfo->level, i;
for (i = 0; i < level; ++i)
ui_out_text (uiout, " ");
}
if (sym != NULL)
ui_out_field_string (uiout, "function", SYMBOL_PRINT_NAME (sym));
else if (msym != NULL)
ui_out_field_string (uiout, "function", MSYMBOL_PRINT_NAME (msym));
else if (!ui_out_is_mi_like_p (uiout))
ui_out_field_string (uiout, "function", "??");
if ((flags & RECORD_PRINT_INSN_RANGE) != 0)
{
ui_out_text (uiout, _("\tinst "));
btrace_call_history_insn_range (uiout, bfun);
}
if ((flags & RECORD_PRINT_SRC_LINE) != 0)
{
ui_out_text (uiout, _("\tat "));
btrace_call_history_src_line (uiout, bfun);
}
ui_out_text (uiout, "\n");
}
}
/* The to_call_history method of target record-btrace. */
static void
record_btrace_call_history (struct target_ops *self, int size, int flags)
{
struct btrace_thread_info *btinfo;
struct btrace_call_history *history;
struct btrace_call_iterator begin, end;
struct cleanup *uiout_cleanup;
struct ui_out *uiout;
unsigned int context, covered;
uiout = current_uiout;
uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
"insn history");
context = abs (size);
if (context == 0)
error (_("Bad record function-call-history-size."));
btinfo = require_btrace ();
history = btinfo->call_history;
if (history == NULL)
{
struct btrace_insn_iterator *replay;
DEBUG ("call-history (0x%x): %d", 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.function = replay->function;
begin.btinfo = btinfo;
}
else
btrace_call_end (&begin, btinfo);
/* We start from here and expand in the requested direction. Then we