forked from gbenson/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtrace.c
2977 lines (2319 loc) · 73.4 KB
/
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 "btrace.h"
#include "gdbthread.h"
#include "inferior.h"
#include "target.h"
#include "record.h"
#include "symtab.h"
#include "disasm.h"
#include "source.h"
#include "filenames.h"
#include "xml-support.h"
#include "regcache.h"
#include "rsp-low.h"
#include "gdbcmd.h"
#include "cli/cli-utils.h"
#include <inttypes.h>
#include <ctype.h>
/* Command lists for btrace maintenance commands. */
static struct cmd_list_element *maint_btrace_cmdlist;
static struct cmd_list_element *maint_btrace_set_cmdlist;
static struct cmd_list_element *maint_btrace_show_cmdlist;
static struct cmd_list_element *maint_btrace_pt_set_cmdlist;
static struct cmd_list_element *maint_btrace_pt_show_cmdlist;
/* Control whether to skip PAD packets when computing the packet history. */
static int maint_btrace_pt_skip_pad = 1;
static void btrace_add_pc (struct thread_info *tp);
/* Print a record 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, \
"[btrace] " msg "\n", ##args); \
} \
while (0)
#define DEBUG_FTRACE(msg, args...) DEBUG ("[ftrace] " msg, ##args)
/* Return the function name of a recorded function segment for printing.
This function never returns NULL. */
static const char *
ftrace_print_function_name (const struct btrace_function *bfun)
{
struct minimal_symbol *msym;
struct symbol *sym;
msym = bfun->msym;
sym = bfun->sym;
if (sym != NULL)
return SYMBOL_PRINT_NAME (sym);
if (msym != NULL)
return MSYMBOL_PRINT_NAME (msym);
return "<unknown>";
}
/* Return the file name of a recorded function segment for printing.
This function never returns NULL. */
static const char *
ftrace_print_filename (const struct btrace_function *bfun)
{
struct symbol *sym;
const char *filename;
sym = bfun->sym;
if (sym != NULL)
filename = symtab_to_filename_for_display (symbol_symtab (sym));
else
filename = "<unknown>";
return filename;
}
/* Return a string representation of the address of an instruction.
This function never returns NULL. */
static const char *
ftrace_print_insn_addr (const struct btrace_insn *insn)
{
if (insn == NULL)
return "<nil>";
return core_addr_to_string_nz (insn->pc);
}
/* Print an ftrace debug status message. */
static void
ftrace_debug (const struct btrace_function *bfun, const char *prefix)
{
const char *fun, *file;
unsigned int ibegin, iend;
int level;
fun = ftrace_print_function_name (bfun);
file = ftrace_print_filename (bfun);
level = bfun->level;
ibegin = bfun->insn_offset;
iend = ibegin + VEC_length (btrace_insn_s, bfun->insn);
DEBUG_FTRACE ("%s: fun = %s, file = %s, level = %d, insn = [%u; %u)",
prefix, fun, file, level, ibegin, iend);
}
/* Return non-zero if BFUN does not match MFUN and FUN,
return zero otherwise. */
static int
ftrace_function_switched (const struct btrace_function *bfun,
const struct minimal_symbol *mfun,
const struct symbol *fun)
{
struct minimal_symbol *msym;
struct symbol *sym;
msym = bfun->msym;
sym = bfun->sym;
/* If the minimal symbol changed, we certainly switched functions. */
if (mfun != NULL && msym != NULL
&& strcmp (MSYMBOL_LINKAGE_NAME (mfun), MSYMBOL_LINKAGE_NAME (msym)) != 0)
return 1;
/* If the symbol changed, we certainly switched functions. */
if (fun != NULL && sym != NULL)
{
const char *bfname, *fname;
/* Check the function name. */
if (strcmp (SYMBOL_LINKAGE_NAME (fun), SYMBOL_LINKAGE_NAME (sym)) != 0)
return 1;
/* Check the location of those functions, as well. */
bfname = symtab_to_fullname (symbol_symtab (sym));
fname = symtab_to_fullname (symbol_symtab (fun));
if (filename_cmp (fname, bfname) != 0)
return 1;
}
/* If we lost symbol information, we switched functions. */
if (!(msym == NULL && sym == NULL) && mfun == NULL && fun == NULL)
return 1;
/* If we gained symbol information, we switched functions. */
if (msym == NULL && sym == NULL && !(mfun == NULL && fun == NULL))
return 1;
return 0;
}
/* Allocate and initialize a new branch trace function segment.
PREV is the chronologically preceding function segment.
MFUN and FUN are the symbol information we have for this function. */
static struct btrace_function *
ftrace_new_function (struct btrace_function *prev,
struct minimal_symbol *mfun,
struct symbol *fun)
{
struct btrace_function *bfun;
bfun = XCNEW (struct btrace_function);
bfun->msym = mfun;
bfun->sym = fun;
bfun->flow.prev = prev;
if (prev == NULL)
{
/* Start counting at one. */
bfun->number = 1;
bfun->insn_offset = 1;
}
else
{
gdb_assert (prev->flow.next == NULL);
prev->flow.next = bfun;
bfun->number = prev->number + 1;
bfun->insn_offset = (prev->insn_offset
+ VEC_length (btrace_insn_s, prev->insn));
bfun->level = prev->level;
}
return bfun;
}
/* Update the UP field of a function segment. */
static void
ftrace_update_caller (struct btrace_function *bfun,
struct btrace_function *caller,
enum btrace_function_flag flags)
{
if (bfun->up != NULL)
ftrace_debug (bfun, "updating caller");
bfun->up = caller;
bfun->flags = flags;
ftrace_debug (bfun, "set caller");
}
/* Fix up the caller for all segments of a function. */
static void
ftrace_fixup_caller (struct btrace_function *bfun,
struct btrace_function *caller,
enum btrace_function_flag flags)
{
struct btrace_function *prev, *next;
ftrace_update_caller (bfun, caller, flags);
/* Update all function segments belonging to the same function. */
for (prev = bfun->segment.prev; prev != NULL; prev = prev->segment.prev)
ftrace_update_caller (prev, caller, flags);
for (next = bfun->segment.next; next != NULL; next = next->segment.next)
ftrace_update_caller (next, caller, flags);
}
/* Add a new function segment for a call.
CALLER is the chronologically preceding function segment.
MFUN and FUN are the symbol information we have for this function. */
static struct btrace_function *
ftrace_new_call (struct btrace_function *caller,
struct minimal_symbol *mfun,
struct symbol *fun)
{
struct btrace_function *bfun;
bfun = ftrace_new_function (caller, mfun, fun);
bfun->up = caller;
bfun->level += 1;
ftrace_debug (bfun, "new call");
return bfun;
}
/* Add a new function segment for a tail call.
CALLER is the chronologically preceding function segment.
MFUN and FUN are the symbol information we have for this function. */
static struct btrace_function *
ftrace_new_tailcall (struct btrace_function *caller,
struct minimal_symbol *mfun,
struct symbol *fun)
{
struct btrace_function *bfun;
bfun = ftrace_new_function (caller, mfun, fun);
bfun->up = caller;
bfun->level += 1;
bfun->flags |= BFUN_UP_LINKS_TO_TAILCALL;
ftrace_debug (bfun, "new tail call");
return bfun;
}
/* Find the innermost caller in the back trace of BFUN with MFUN/FUN
symbol information. */
static struct btrace_function *
ftrace_find_caller (struct btrace_function *bfun,
struct minimal_symbol *mfun,
struct symbol *fun)
{
for (; bfun != NULL; bfun = bfun->up)
{
/* Skip functions with incompatible symbol information. */
if (ftrace_function_switched (bfun, mfun, fun))
continue;
/* This is the function segment we're looking for. */
break;
}
return bfun;
}
/* Find the innermost caller in the back trace of BFUN, skipping all
function segments that do not end with a call instruction (e.g.
tail calls ending with a jump). */
static struct btrace_function *
ftrace_find_call (struct btrace_function *bfun)
{
for (; bfun != NULL; bfun = bfun->up)
{
struct btrace_insn *last;
/* Skip gaps. */
if (bfun->errcode != 0)
continue;
last = VEC_last (btrace_insn_s, bfun->insn);
if (last->iclass == BTRACE_INSN_CALL)
break;
}
return bfun;
}
/* Add a continuation segment for a function into which we return.
PREV is the chronologically preceding function segment.
MFUN and FUN are the symbol information we have for this function. */
static struct btrace_function *
ftrace_new_return (struct btrace_function *prev,
struct minimal_symbol *mfun,
struct symbol *fun)
{
struct btrace_function *bfun, *caller;
bfun = ftrace_new_function (prev, mfun, fun);
/* It is important to start at PREV's caller. Otherwise, we might find
PREV itself, if PREV is a recursive function. */
caller = ftrace_find_caller (prev->up, mfun, fun);
if (caller != NULL)
{
/* The caller of PREV is the preceding btrace function segment in this
function instance. */
gdb_assert (caller->segment.next == NULL);
caller->segment.next = bfun;
bfun->segment.prev = caller;
/* Maintain the function level. */
bfun->level = caller->level;
/* Maintain the call stack. */
bfun->up = caller->up;
bfun->flags = caller->flags;
ftrace_debug (bfun, "new return");
}
else
{
/* We did not find a caller. This could mean that something went
wrong or that the call is simply not included in the trace. */
/* Let's search for some actual call. */
caller = ftrace_find_call (prev->up);
if (caller == NULL)
{
/* There is no call in PREV's back trace. We assume that the
branch trace did not include it. */
/* Let's find the topmost call function - this skips tail calls. */
while (prev->up != NULL)
prev = prev->up;
/* We maintain levels for a series of returns for which we have
not seen the calls.
We start at the preceding function's level in case this has
already been a return for which we have not seen the call.
We start at level 0 otherwise, to handle tail calls correctly. */
bfun->level = min (0, prev->level) - 1;
/* Fix up the call stack for PREV. */
ftrace_fixup_caller (prev, bfun, BFUN_UP_LINKS_TO_RET);
ftrace_debug (bfun, "new return - no caller");
}
else
{
/* There is a call in PREV's back trace to which we should have
returned. Let's remain at this level. */
bfun->level = prev->level;
ftrace_debug (bfun, "new return - unknown caller");
}
}
return bfun;
}
/* Add a new function segment for a function switch.
PREV is the chronologically preceding function segment.
MFUN and FUN are the symbol information we have for this function. */
static struct btrace_function *
ftrace_new_switch (struct btrace_function *prev,
struct minimal_symbol *mfun,
struct symbol *fun)
{
struct btrace_function *bfun;
/* This is an unexplained function switch. The call stack will likely
be wrong at this point. */
bfun = ftrace_new_function (prev, mfun, fun);
ftrace_debug (bfun, "new switch");
return bfun;
}
/* Add a new function segment for a gap in the trace due to a decode error.
PREV is the chronologically preceding function segment.
ERRCODE is the format-specific error code. */
static struct btrace_function *
ftrace_new_gap (struct btrace_function *prev, int errcode)
{
struct btrace_function *bfun;
/* We hijack prev if it was empty. */
if (prev != NULL && prev->errcode == 0
&& VEC_empty (btrace_insn_s, prev->insn))
bfun = prev;
else
bfun = ftrace_new_function (prev, NULL, NULL);
bfun->errcode = errcode;
ftrace_debug (bfun, "new gap");
return bfun;
}
/* Update BFUN with respect to the instruction at PC. This may create new
function segments.
Return the chronologically latest function segment, never NULL. */
static struct btrace_function *
ftrace_update_function (struct btrace_function *bfun, CORE_ADDR pc)
{
struct bound_minimal_symbol bmfun;
struct minimal_symbol *mfun;
struct symbol *fun;
struct btrace_insn *last;
/* Try to determine the function we're in. We use both types of symbols
to avoid surprises when we sometimes get a full symbol and sometimes
only a minimal symbol. */
fun = find_pc_function (pc);
bmfun = lookup_minimal_symbol_by_pc (pc);
mfun = bmfun.minsym;
if (fun == NULL && mfun == NULL)
DEBUG_FTRACE ("no symbol at %s", core_addr_to_string_nz (pc));
/* If we didn't have a function or if we had a gap before, we create one. */
if (bfun == NULL || bfun->errcode != 0)
return ftrace_new_function (bfun, mfun, fun);
/* Check the last instruction, if we have one.
We do this check first, since it allows us to fill in the call stack
links in addition to the normal flow links. */
last = NULL;
if (!VEC_empty (btrace_insn_s, bfun->insn))
last = VEC_last (btrace_insn_s, bfun->insn);
if (last != NULL)
{
switch (last->iclass)
{
case BTRACE_INSN_RETURN:
{
const char *fname;
/* On some systems, _dl_runtime_resolve returns to the resolved
function instead of jumping to it. From our perspective,
however, this is a tailcall.
If we treated it as return, we wouldn't be able to find the
resolved function in our stack back trace. Hence, we would
lose the current stack back trace and start anew with an empty
back trace. When the resolved function returns, we would then
create a stack back trace with the same function names but
different frame id's. This will confuse stepping. */
fname = ftrace_print_function_name (bfun);
if (strcmp (fname, "_dl_runtime_resolve") == 0)
return ftrace_new_tailcall (bfun, mfun, fun);
return ftrace_new_return (bfun, mfun, fun);
}
case BTRACE_INSN_CALL:
/* Ignore calls to the next instruction. They are used for PIC. */
if (last->pc + last->size == pc)
break;
return ftrace_new_call (bfun, mfun, fun);
case BTRACE_INSN_JUMP:
{
CORE_ADDR start;
start = get_pc_function_start (pc);
/* If we can't determine the function for PC, we treat a jump at
the end of the block as tail call. */
if (start == 0 || start == pc)
return ftrace_new_tailcall (bfun, mfun, fun);
}
}
}
/* Check if we're switching functions for some other reason. */
if (ftrace_function_switched (bfun, mfun, fun))
{
DEBUG_FTRACE ("switching from %s in %s at %s",
ftrace_print_insn_addr (last),
ftrace_print_function_name (bfun),
ftrace_print_filename (bfun));
return ftrace_new_switch (bfun, mfun, fun);
}
return bfun;
}
/* Add the instruction at PC to BFUN's instructions. */
static void
ftrace_update_insns (struct btrace_function *bfun,
const struct btrace_insn *insn)
{
VEC_safe_push (btrace_insn_s, bfun->insn, insn);
if (record_debug > 1)
ftrace_debug (bfun, "update insn");
}
/* Classify the instruction at PC. */
static enum btrace_insn_class
ftrace_classify_insn (struct gdbarch *gdbarch, CORE_ADDR pc)
{
enum btrace_insn_class iclass;
iclass = BTRACE_INSN_OTHER;
TRY
{
if (gdbarch_insn_is_call (gdbarch, pc))
iclass = BTRACE_INSN_CALL;
else if (gdbarch_insn_is_ret (gdbarch, pc))
iclass = BTRACE_INSN_RETURN;
else if (gdbarch_insn_is_jump (gdbarch, pc))
iclass = BTRACE_INSN_JUMP;
}
CATCH (error, RETURN_MASK_ERROR)
{
}
END_CATCH
return iclass;
}
/* Compute the function branch trace from BTS trace. */
static void
btrace_compute_ftrace_bts (struct thread_info *tp,
const struct btrace_data_bts *btrace)
{
struct btrace_thread_info *btinfo;
struct btrace_function *begin, *end;
struct gdbarch *gdbarch;
unsigned int blk, ngaps;
int level;
gdbarch = target_gdbarch ();
btinfo = &tp->btrace;
begin = btinfo->begin;
end = btinfo->end;
ngaps = btinfo->ngaps;
level = begin != NULL ? -btinfo->level : INT_MAX;
blk = VEC_length (btrace_block_s, btrace->blocks);
while (blk != 0)
{
btrace_block_s *block;
CORE_ADDR pc;
blk -= 1;
block = VEC_index (btrace_block_s, btrace->blocks, blk);
pc = block->begin;
for (;;)
{
struct btrace_insn insn;
int size;
/* We should hit the end of the block. Warn if we went too far. */
if (block->end < pc)
{
/* Indicate the gap in the trace - unless we're at the
beginning. */
if (begin != NULL)
{
warning (_("Recorded trace may be corrupted around %s."),
core_addr_to_string_nz (pc));
end = ftrace_new_gap (end, BDE_BTS_OVERFLOW);
ngaps += 1;
}
break;
}
end = ftrace_update_function (end, pc);
if (begin == NULL)
begin = end;
/* Maintain the function level offset.
For all but the last block, we do it here. */
if (blk != 0)
level = min (level, end->level);
size = 0;
TRY
{
size = gdb_insn_length (gdbarch, pc);
}
CATCH (error, RETURN_MASK_ERROR)
{
}
END_CATCH
insn.pc = pc;
insn.size = size;
insn.iclass = ftrace_classify_insn (gdbarch, pc);
insn.flags = 0;
ftrace_update_insns (end, &insn);
/* We're done once we pushed the instruction at the end. */
if (block->end == pc)
break;
/* We can't continue if we fail to compute the size. */
if (size <= 0)
{
warning (_("Recorded trace may be incomplete around %s."),
core_addr_to_string_nz (pc));
/* Indicate the gap in the trace. We just added INSN so we're
not at the beginning. */
end = ftrace_new_gap (end, BDE_BTS_INSN_SIZE);
ngaps += 1;
break;
}
pc += size;
/* Maintain the function level offset.
For the last block, we do it here to not consider the last
instruction.
Since the last instruction corresponds to the current instruction
and is not really part of the execution history, it shouldn't
affect the level. */
if (blk == 0)
level = min (level, end->level);
}
}
btinfo->begin = begin;
btinfo->end = end;
btinfo->ngaps = ngaps;
/* LEVEL is the minimal function level of all btrace function segments.
Define the global level offset to -LEVEL so all function levels are
normalized to start at zero. */
btinfo->level = -level;
}
#if defined (HAVE_LIBIPT)
static enum btrace_insn_class
pt_reclassify_insn (enum pt_insn_class iclass)
{
switch (iclass)
{
case ptic_call:
return BTRACE_INSN_CALL;
case ptic_return:
return BTRACE_INSN_RETURN;
case ptic_jump:
return BTRACE_INSN_JUMP;
default:
return BTRACE_INSN_OTHER;
}
}
/* Return the btrace instruction flags for INSN. */
static enum btrace_insn_flag
pt_btrace_insn_flags (const struct pt_insn *insn)
{
enum btrace_insn_flag flags = 0;
if (insn->speculative)
flags |= BTRACE_INSN_FLAG_SPECULATIVE;
return flags;
}
/* Add function branch trace using DECODER. */
static void
ftrace_add_pt (struct pt_insn_decoder *decoder,
struct btrace_function **pbegin,
struct btrace_function **pend, int *plevel,
unsigned int *ngaps)
{
struct btrace_function *begin, *end, *upd;
uint64_t offset;
int errcode, nerrors;
begin = *pbegin;
end = *pend;
nerrors = 0;
for (;;)
{
struct btrace_insn btinsn;
struct pt_insn insn;
errcode = pt_insn_sync_forward (decoder);
if (errcode < 0)
{
if (errcode != -pte_eos)
warning (_("Failed to synchronize onto the Intel(R) Processor "
"Trace stream: %s."), pt_errstr (pt_errcode (errcode)));
break;
}
memset (&btinsn, 0, sizeof (btinsn));
for (;;)
{
errcode = pt_insn_next (decoder, &insn, sizeof(insn));
if (errcode < 0)
break;
/* Look for gaps in the trace - unless we're at the beginning. */
if (begin != NULL)
{
/* Tracing is disabled and re-enabled each time we enter the
kernel. Most times, we continue from the same instruction we
stopped before. This is indicated via the RESUMED instruction
flag. The ENABLED instruction flag means that we continued
from some other instruction. Indicate this as a trace gap. */
if (insn.enabled)
*pend = end = ftrace_new_gap (end, BDE_PT_DISABLED);
/* Indicate trace overflows. */
if (insn.resynced)
*pend = end = ftrace_new_gap (end, BDE_PT_OVERFLOW);
}
upd = ftrace_update_function (end, insn.ip);
if (upd != end)
{
*pend = end = upd;
if (begin == NULL)
*pbegin = begin = upd;
}
/* Maintain the function level offset. */
*plevel = min (*plevel, end->level);
btinsn.pc = (CORE_ADDR) insn.ip;
btinsn.size = (gdb_byte) insn.size;
btinsn.iclass = pt_reclassify_insn (insn.iclass);
btinsn.flags = pt_btrace_insn_flags (&insn);
ftrace_update_insns (end, &btinsn);
}
if (errcode == -pte_eos)
break;
/* If the gap is at the very beginning, we ignore it - we will have
less trace, but we won't have any holes in the trace. */
if (begin == NULL)
continue;
pt_insn_get_offset (decoder, &offset);
warning (_("Failed to decode Intel(R) Processor Trace near trace "
"offset 0x%" PRIx64 " near recorded PC 0x%" PRIx64 ": %s."),
offset, insn.ip, pt_errstr (pt_errcode (errcode)));
/* Indicate the gap in the trace. */
*pend = end = ftrace_new_gap (end, errcode);
*ngaps += 1;
}
if (nerrors > 0)
warning (_("The recorded execution trace may have gaps."));
}
/* A callback function to allow the trace decoder to read the inferior's
memory. */
static int
btrace_pt_readmem_callback (gdb_byte *buffer, size_t size,
const struct pt_asid *asid, uint64_t pc,
void *context)
{
int errcode;
TRY
{
errcode = target_read_code ((CORE_ADDR) pc, buffer, size);
if (errcode != 0)
return -pte_nomap;
}
CATCH (error, RETURN_MASK_ERROR)
{
return -pte_nomap;
}
END_CATCH
return size;
}
/* Translate the vendor from one enum to another. */
static enum pt_cpu_vendor
pt_translate_cpu_vendor (enum btrace_cpu_vendor vendor)
{
switch (vendor)
{
default:
return pcv_unknown;
case CV_INTEL:
return pcv_intel;
}
}
/* Finalize the function branch trace after decode. */
static void btrace_finalize_ftrace_pt (struct pt_insn_decoder *decoder,
struct thread_info *tp, int level)
{
pt_insn_free_decoder (decoder);
/* LEVEL is the minimal function level of all btrace function segments.
Define the global level offset to -LEVEL so all function levels are
normalized to start at zero. */
tp->btrace.level = -level;
/* Add a single last instruction entry for the current PC.
This allows us to compute the backtrace at the current PC using both
standard unwind and btrace unwind.
This extra entry is ignored by all record commands. */
btrace_add_pc (tp);
}
/* Compute the function branch trace from Intel(R) Processor Trace. */
static void
btrace_compute_ftrace_pt (struct thread_info *tp,
const struct btrace_data_pt *btrace)
{
struct btrace_thread_info *btinfo;
struct pt_insn_decoder *decoder;
struct pt_config config;
int level, errcode;
if (btrace->size == 0)
return;
btinfo = &tp->btrace;
level = btinfo->begin != NULL ? -btinfo->level : INT_MAX;
pt_config_init(&config);
config.begin = btrace->data;
config.end = btrace->data + btrace->size;
config.cpu.vendor = pt_translate_cpu_vendor (btrace->config.cpu.vendor);
config.cpu.family = btrace->config.cpu.family;
config.cpu.model = btrace->config.cpu.model;
config.cpu.stepping = btrace->config.cpu.stepping;
errcode = pt_cpu_errata (&config.errata, &config.cpu);
if (errcode < 0)
error (_("Failed to configure the Intel(R) Processor Trace decoder: %s."),
pt_errstr (pt_errcode (errcode)));
decoder = pt_insn_alloc_decoder (&config);
if (decoder == NULL)
error (_("Failed to allocate the Intel(R) Processor Trace decoder."));
TRY
{
struct pt_image *image;
image = pt_insn_get_image(decoder);
if (image == NULL)
error (_("Failed to configure the Intel(R) Processor Trace decoder."));
errcode = pt_image_set_callback(image, btrace_pt_readmem_callback, NULL);
if (errcode < 0)
error (_("Failed to configure the Intel(R) Processor Trace decoder: "
"%s."), pt_errstr (pt_errcode (errcode)));
ftrace_add_pt (decoder, &btinfo->begin, &btinfo->end, &level,
&btinfo->ngaps);
}
CATCH (error, RETURN_MASK_ALL)
{
/* Indicate a gap in the trace if we quit trace processing. */
if (error.reason == RETURN_QUIT && btinfo->end != NULL)
{
btinfo->end = ftrace_new_gap (btinfo->end, BDE_PT_USER_QUIT);
btinfo->ngaps++;
}
btrace_finalize_ftrace_pt (decoder, tp, level);
throw_exception (error);
}
END_CATCH
btrace_finalize_ftrace_pt (decoder, tp, level);
}
#else /* defined (HAVE_LIBIPT) */
static void
btrace_compute_ftrace_pt (struct thread_info *tp,
const struct btrace_data_pt *btrace)
{
internal_error (__FILE__, __LINE__, _("Unexpected branch trace format."));
}
#endif /* defined (HAVE_LIBIPT) */
/* Compute the function branch trace from a block branch trace BTRACE for
a thread given by BTINFO. */
static void
btrace_compute_ftrace (struct thread_info *tp, struct btrace_data *btrace)
{
DEBUG ("compute ftrace");
switch (btrace->format)
{
case BTRACE_FORMAT_NONE:
return;
case BTRACE_FORMAT_BTS:
btrace_compute_ftrace_bts (tp, &btrace->variant.bts);
return;
case BTRACE_FORMAT_PT:
btrace_compute_ftrace_pt (tp, &btrace->variant.pt);
return;
}
internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
}