This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btrace.c
3187 lines (2507 loc) · 82.5 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-2024 Free Software Foundation, Inc.
Contributed by Intel Corp. <markus.t.metzger@intel.com>
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 "regcache.h"
#include "gdbsupport/rsp-low.h"
#include "gdbcmd.h"
#include "cli/cli-utils.h"
#include "gdbarch.h"
/* For maintenance commands. */
#include "record-btrace.h"
#include <inttypes.h>
#include <ctype.h>
#include <algorithm>
/* 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 bool maint_btrace_pt_skip_pad = true;
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) \
gdb_printf (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 sym->print_name ();
if (msym != NULL)
return msym->print_name ();
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 (sym->symtab ());
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 + bfun->insn.size ();
DEBUG_FTRACE ("%s: fun = %s, file = %s, level = %d, insn = [%u; %u)",
prefix, fun, file, level, ibegin, iend);
}
/* Return the number of instructions in a given function call segment. */
static unsigned int
ftrace_call_num_insn (const struct btrace_function* bfun)
{
if (bfun == NULL)
return 0;
/* A gap is always counted as one instruction. */
if (bfun->errcode != 0)
return 1;
return bfun->insn.size ();
}
/* Return the function segment with the given NUMBER or NULL if no such segment
exists. BTINFO is the branch trace information for the current thread. */
static struct btrace_function *
ftrace_find_call_by_number (struct btrace_thread_info *btinfo,
unsigned int number)
{
if (number == 0 || number > btinfo->functions.size ())
return NULL;
return &btinfo->functions[number - 1];
}
/* A const version of the function above. */
static const struct btrace_function *
ftrace_find_call_by_number (const struct btrace_thread_info *btinfo,
unsigned int number)
{
if (number == 0 || number > btinfo->functions.size ())
return NULL;
return &btinfo->functions[number - 1];
}
/* 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 (mfun->linkage_name (), msym->linkage_name ()) != 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 (fun->linkage_name (), sym->linkage_name ()) != 0)
return 1;
/* Check the location of those functions, as well. */
bfname = symtab_to_fullname (sym->symtab ());
fname = symtab_to_fullname (fun->symtab ());
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 at the end of
the trace.
BTINFO is the branch trace information for the current thread.
MFUN and FUN are the symbol information we have for this function.
This invalidates all struct btrace_function pointer currently held. */
static struct btrace_function *
ftrace_new_function (struct btrace_thread_info *btinfo,
struct minimal_symbol *mfun,
struct symbol *fun)
{
int level;
unsigned int number, insn_offset;
if (btinfo->functions.empty ())
{
/* Start counting NUMBER and INSN_OFFSET at one. */
level = 0;
number = 1;
insn_offset = 1;
}
else
{
const struct btrace_function *prev = &btinfo->functions.back ();
level = prev->level;
number = prev->number + 1;
insn_offset = prev->insn_offset + ftrace_call_num_insn (prev);
}
return &btinfo->functions.emplace_back (mfun, fun, number, insn_offset,
level);
}
/* Update the UP field of a function segment. */
static void
ftrace_update_caller (struct btrace_function *bfun,
struct btrace_function *caller,
btrace_function_flags flags)
{
if (bfun->up != 0)
ftrace_debug (bfun, "updating caller");
bfun->up = caller->number;
bfun->flags = flags;
ftrace_debug (bfun, "set caller");
ftrace_debug (caller, "..to");
}
/* Fix up the caller for all segments of a function. */
static void
ftrace_fixup_caller (struct btrace_thread_info *btinfo,
struct btrace_function *bfun,
struct btrace_function *caller,
btrace_function_flags flags)
{
unsigned int prev, next;
prev = bfun->prev;
next = bfun->next;
ftrace_update_caller (bfun, caller, flags);
/* Update all function segments belonging to the same function. */
for (; prev != 0; prev = bfun->prev)
{
bfun = ftrace_find_call_by_number (btinfo, prev);
ftrace_update_caller (bfun, caller, flags);
}
for (; next != 0; next = bfun->next)
{
bfun = ftrace_find_call_by_number (btinfo, next);
ftrace_update_caller (bfun, caller, flags);
}
}
/* Add a new function segment for a call at the end of the trace.
BTINFO is the branch trace information for the current thread.
MFUN and FUN are the symbol information we have for this function. */
static struct btrace_function *
ftrace_new_call (struct btrace_thread_info *btinfo,
struct minimal_symbol *mfun,
struct symbol *fun)
{
const unsigned int length = btinfo->functions.size ();
struct btrace_function *bfun = ftrace_new_function (btinfo, mfun, fun);
bfun->up = length;
bfun->level += 1;
ftrace_debug (bfun, "new call");
return bfun;
}
/* Add a new function segment for a tail call at the end of the trace.
BTINFO is the branch trace information for the current thread.
MFUN and FUN are the symbol information we have for this function. */
static struct btrace_function *
ftrace_new_tailcall (struct btrace_thread_info *btinfo,
struct minimal_symbol *mfun,
struct symbol *fun)
{
const unsigned int length = btinfo->functions.size ();
struct btrace_function *bfun = ftrace_new_function (btinfo, mfun, fun);
bfun->up = length;
bfun->level += 1;
bfun->flags |= BFUN_UP_LINKS_TO_TAILCALL;
ftrace_debug (bfun, "new tail call");
return bfun;
}
/* Return the caller of BFUN or NULL if there is none. This function skips
tail calls in the call chain. BTINFO is the branch trace information for
the current thread. */
static struct btrace_function *
ftrace_get_caller (struct btrace_thread_info *btinfo,
struct btrace_function *bfun)
{
for (; bfun != NULL; bfun = ftrace_find_call_by_number (btinfo, bfun->up))
if ((bfun->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
return ftrace_find_call_by_number (btinfo, bfun->up);
return NULL;
}
/* Find the innermost caller in the back trace of BFUN with MFUN/FUN
symbol information. BTINFO is the branch trace information for the current
thread. */
static struct btrace_function *
ftrace_find_caller (struct btrace_thread_info *btinfo,
struct btrace_function *bfun,
struct minimal_symbol *mfun,
struct symbol *fun)
{
for (; bfun != NULL; bfun = ftrace_find_call_by_number (btinfo, 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). BTINFO is the branch trace information for
the current thread. */
static struct btrace_function *
ftrace_find_call (struct btrace_thread_info *btinfo,
struct btrace_function *bfun)
{
for (; bfun != NULL; bfun = ftrace_find_call_by_number (btinfo, bfun->up))
{
/* Skip gaps. */
if (bfun->errcode != 0)
continue;
btrace_insn &last = bfun->insn.back ();
if (last.iclass == BTRACE_INSN_CALL)
break;
}
return bfun;
}
/* Add a continuation segment for a function into which we return at the end of
the trace.
BTINFO is the branch trace information for the current thread.
MFUN and FUN are the symbol information we have for this function. */
static struct btrace_function *
ftrace_new_return (struct btrace_thread_info *btinfo,
struct minimal_symbol *mfun,
struct symbol *fun)
{
struct btrace_function *prev, *bfun, *caller;
bfun = ftrace_new_function (btinfo, mfun, fun);
prev = ftrace_find_call_by_number (btinfo, bfun->number - 1);
/* It is important to start at PREV's caller. Otherwise, we might find
PREV itself, if PREV is a recursive function. */
caller = ftrace_find_call_by_number (btinfo, prev->up);
caller = ftrace_find_caller (btinfo, caller, mfun, fun);
if (caller != NULL)
{
/* The caller of PREV is the preceding btrace function segment in this
function instance. */
gdb_assert (caller->next == 0);
caller->next = bfun->number;
bfun->prev = caller->number;
/* 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_by_number (btinfo, prev->up);
caller = ftrace_find_call (btinfo, caller);
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 function and add a new caller for it.
This should handle a series of initial tail calls. */
while (prev->up != 0)
prev = ftrace_find_call_by_number (btinfo, prev->up);
bfun->level = prev->level - 1;
/* Fix up the call stack for PREV. */
ftrace_fixup_caller (btinfo, 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 but didn't. Let's start a new, separate back trace
from PREV's level. */
bfun->level = prev->level - 1;
/* We fix up the back trace for PREV but leave other function segments
on the same level as they are.
This should handle things like schedule () correctly where we're
switching contexts. */
prev->up = bfun->number;
prev->flags = BFUN_UP_LINKS_TO_RET;
ftrace_debug (bfun, "new return - unknown caller");
}
}
return bfun;
}
/* Add a new function segment for a function switch at the end of the trace.
BTINFO is the branch trace information for the current thread.
MFUN and FUN are the symbol information we have for this function. */
static struct btrace_function *
ftrace_new_switch (struct btrace_thread_info *btinfo,
struct minimal_symbol *mfun,
struct symbol *fun)
{
struct btrace_function *prev, *bfun;
/* This is an unexplained function switch. We can't really be sure about the
call stack, yet the best I can think of right now is to preserve it. */
bfun = ftrace_new_function (btinfo, mfun, fun);
prev = ftrace_find_call_by_number (btinfo, bfun->number - 1);
bfun->up = prev->up;
bfun->flags = prev->flags;
ftrace_debug (bfun, "new switch");
return bfun;
}
/* Add a new function segment for a gap in the trace due to a decode error at
the end of the trace.
BTINFO is the branch trace information for the current thread.
ERRCODE is the format-specific error code. */
static struct btrace_function *
ftrace_new_gap (struct btrace_thread_info *btinfo, int errcode,
std::vector<unsigned int> &gaps)
{
struct btrace_function *bfun;
if (btinfo->functions.empty ())
bfun = ftrace_new_function (btinfo, NULL, NULL);
else
{
/* We hijack the previous function segment if it was empty. */
bfun = &btinfo->functions.back ();
if (bfun->errcode != 0 || !bfun->insn.empty ())
bfun = ftrace_new_function (btinfo, NULL, NULL);
}
bfun->errcode = errcode;
gaps.push_back (bfun->number);
ftrace_debug (bfun, "new gap");
return bfun;
}
/* Update the current function segment at the end of the trace in BTINFO 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_thread_info *btinfo, CORE_ADDR pc)
{
struct bound_minimal_symbol bmfun;
struct minimal_symbol *mfun;
struct symbol *fun;
struct btrace_function *bfun;
/* 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, we create one. */
if (btinfo->functions.empty ())
return ftrace_new_function (btinfo, mfun, fun);
/* If we had a gap before, we create a function. */
bfun = &btinfo->functions.back ();
if (bfun->errcode != 0)
return ftrace_new_function (btinfo, 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. */
btrace_insn *last = NULL;
if (!bfun->insn.empty ())
last = &bfun->insn.back ();
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 (btinfo, mfun, fun);
return ftrace_new_return (btinfo, 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 (btinfo, mfun, fun);
case BTRACE_INSN_JUMP:
{
CORE_ADDR start;
start = get_pc_function_start (pc);
/* A jump to the start of a function is (typically) a tail call. */
if (start == pc)
return ftrace_new_tailcall (btinfo, mfun, fun);
/* Some versions of _Unwind_RaiseException use an indirect
jump to 'return' to the exception handler of the caller
handling the exception instead of a return. Let's restrict
this heuristic to that and related functions. */
const char *fname = ftrace_print_function_name (bfun);
if (strncmp (fname, "_Unwind_", strlen ("_Unwind_")) == 0)
{
struct btrace_function *caller
= ftrace_find_call_by_number (btinfo, bfun->up);
caller = ftrace_find_caller (btinfo, caller, mfun, fun);
if (caller != NULL)
return ftrace_new_return (btinfo, mfun, fun);
}
/* If we can't determine the function for PC, we treat a jump at
the end of the block as tail call if we're switching functions
and as an intra-function branch if we don't. */
if (start == 0 && ftrace_function_switched (bfun, mfun, fun))
return ftrace_new_tailcall (btinfo, mfun, fun);
break;
}
}
}
/* 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 (btinfo, mfun, fun);
}
return bfun;
}
/* Add the instruction at PC to BFUN's instructions. */
static void
ftrace_update_insns (struct btrace_function *bfun, const btrace_insn &insn)
{
bfun->insn.push_back (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 (const gdb_exception_error &error)
{
}
return iclass;
}
/* Try to match the back trace at LHS to the back trace at RHS. Returns the
number of matching function segments or zero if the back traces do not
match. BTINFO is the branch trace information for the current thread. */
static int
ftrace_match_backtrace (struct btrace_thread_info *btinfo,
struct btrace_function *lhs,
struct btrace_function *rhs)
{
int matches;
for (matches = 0; lhs != NULL && rhs != NULL; ++matches)
{
if (ftrace_function_switched (lhs, rhs->msym, rhs->sym))
return 0;
lhs = ftrace_get_caller (btinfo, lhs);
rhs = ftrace_get_caller (btinfo, rhs);
}
return matches;
}
/* Add ADJUSTMENT to the level of BFUN and succeeding function segments.
BTINFO is the branch trace information for the current thread. */
static void
ftrace_fixup_level (struct btrace_thread_info *btinfo,
struct btrace_function *bfun, int adjustment)
{
if (adjustment == 0)
return;
DEBUG_FTRACE ("fixup level (%+d)", adjustment);
ftrace_debug (bfun, "..bfun");
while (bfun != NULL)
{
bfun->level += adjustment;
bfun = ftrace_find_call_by_number (btinfo, bfun->number + 1);
}
}
/* Recompute the global level offset. Traverse the function trace and compute
the global level offset as the negative of the minimal function level. */
static void
ftrace_compute_global_level_offset (struct btrace_thread_info *btinfo)
{
int level = INT_MAX;
if (btinfo == NULL)
return;
if (btinfo->functions.empty ())
return;
unsigned int length = btinfo->functions.size() - 1;
for (unsigned int i = 0; i < length; ++i)
level = std::min (level, btinfo->functions[i].level);
/* The last function segment contains the current instruction, which is not
really part of the trace. If it contains just this one instruction, we
ignore the segment. */
struct btrace_function *last = &btinfo->functions.back();
if (last->insn.size () != 1)
level = std::min (level, last->level);
DEBUG_FTRACE ("setting global level offset: %d", -level);
btinfo->level = -level;
}
/* Connect the function segments PREV and NEXT in a bottom-to-top walk as in
ftrace_connect_backtrace. BTINFO is the branch trace information for the
current thread. */
static void
ftrace_connect_bfun (struct btrace_thread_info *btinfo,
struct btrace_function *prev,
struct btrace_function *next)
{
DEBUG_FTRACE ("connecting...");
ftrace_debug (prev, "..prev");
ftrace_debug (next, "..next");
/* The function segments are not yet connected. */
gdb_assert (prev->next == 0);
gdb_assert (next->prev == 0);
prev->next = next->number;
next->prev = prev->number;
/* We may have moved NEXT to a different function level. */
ftrace_fixup_level (btinfo, next, prev->level - next->level);
/* If we run out of back trace for one, let's use the other's. */
if (prev->up == 0)
{
const btrace_function_flags flags = next->flags;
next = ftrace_find_call_by_number (btinfo, next->up);
if (next != NULL)
{
DEBUG_FTRACE ("using next's callers");
ftrace_fixup_caller (btinfo, prev, next, flags);
}
}
else if (next->up == 0)
{
const btrace_function_flags flags = prev->flags;
prev = ftrace_find_call_by_number (btinfo, prev->up);
if (prev != NULL)
{
DEBUG_FTRACE ("using prev's callers");
ftrace_fixup_caller (btinfo, next, prev, flags);
}
}
else
{
/* PREV may have a tailcall caller, NEXT can't. If it does, fixup the up
link to add the tail callers to NEXT's back trace.
This removes NEXT->UP from NEXT's back trace. It will be added back
when connecting NEXT and PREV's callers - provided they exist.
If PREV's back trace consists of a series of tail calls without an
actual call, there will be no further connection and NEXT's caller will
be removed for good. To catch this case, we handle it here and connect
the top of PREV's back trace to NEXT's caller. */
if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) != 0)
{
struct btrace_function *caller;
btrace_function_flags next_flags, prev_flags;
/* We checked NEXT->UP above so CALLER can't be NULL. */
caller = ftrace_find_call_by_number (btinfo, next->up);
next_flags = next->flags;
prev_flags = prev->flags;
DEBUG_FTRACE ("adding prev's tail calls to next");
prev = ftrace_find_call_by_number (btinfo, prev->up);
ftrace_fixup_caller (btinfo, next, prev, prev_flags);
for (; prev != NULL; prev = ftrace_find_call_by_number (btinfo,
prev->up))
{
/* At the end of PREV's back trace, continue with CALLER. */
if (prev->up == 0)
{
DEBUG_FTRACE ("fixing up link for tailcall chain");
ftrace_debug (prev, "..top");
ftrace_debug (caller, "..up");
ftrace_fixup_caller (btinfo, prev, caller, next_flags);
/* If we skipped any tail calls, this may move CALLER to a
different function level.
Note that changing CALLER's level is only OK because we
know that this is the last iteration of the bottom-to-top
walk in ftrace_connect_backtrace.
Otherwise we will fix up CALLER's level when we connect it
to PREV's caller in the next iteration. */
ftrace_fixup_level (btinfo, caller,
prev->level - caller->level - 1);
break;
}
/* There's nothing to do if we find a real call. */
if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
{
DEBUG_FTRACE ("will fix up link in next iteration");
break;
}
}
}
}
}
/* Connect function segments on the same level in the back trace at LHS and RHS.
The back traces at LHS and RHS are expected to match according to
ftrace_match_backtrace. BTINFO is the branch trace information for the
current thread. */
static void
ftrace_connect_backtrace (struct btrace_thread_info *btinfo,
struct btrace_function *lhs,
struct btrace_function *rhs)
{
while (lhs != NULL && rhs != NULL)
{
struct btrace_function *prev, *next;
gdb_assert (!ftrace_function_switched (lhs, rhs->msym, rhs->sym));
/* Connecting LHS and RHS may change the up link. */
prev = lhs;
next = rhs;
lhs = ftrace_get_caller (btinfo, lhs);
rhs = ftrace_get_caller (btinfo, rhs);
ftrace_connect_bfun (btinfo, prev, next);
}
}
/* Bridge the gap between two function segments left and right of a gap if their
respective back traces match in at least MIN_MATCHES functions. BTINFO is
the branch trace information for the current thread.
Returns non-zero if the gap could be bridged, zero otherwise. */
static int
ftrace_bridge_gap (struct btrace_thread_info *btinfo,
struct btrace_function *lhs, struct btrace_function *rhs,
int min_matches)
{
struct btrace_function *best_l, *best_r, *cand_l, *cand_r;
int best_matches;
DEBUG_FTRACE ("checking gap at insn %u (req matches: %d)",
rhs->insn_offset - 1, min_matches);
best_matches = 0;
best_l = NULL;
best_r = NULL;
/* We search the back traces of LHS and RHS for valid connections and connect
the two function segments that give the longest combined back trace. */
for (cand_l = lhs; cand_l != NULL;
cand_l = ftrace_get_caller (btinfo, cand_l))
for (cand_r = rhs; cand_r != NULL;
cand_r = ftrace_get_caller (btinfo, cand_r))
{
int matches;
matches = ftrace_match_backtrace (btinfo, cand_l, cand_r);
if (best_matches < matches)
{
best_matches = matches;
best_l = cand_l;
best_r = cand_r;
}
}
/* We need at least MIN_MATCHES matches. */
gdb_assert (min_matches > 0);
if (best_matches < min_matches)
return 0;
DEBUG_FTRACE ("..matches: %d", best_matches);
/* We will fix up the level of BEST_R and succeeding function segments such
that BEST_R's level matches BEST_L's when we connect BEST_L to BEST_R.
This will ignore the level of RHS and following if BEST_R != RHS. I.e. if
BEST_R is a successor of RHS in the back trace of RHS (phases 1 and 3).
To catch this, we already fix up the level here where we can start at RHS
instead of at BEST_R. We will ignore the level fixup when connecting
BEST_L to BEST_R as they will already be on the same level. */
ftrace_fixup_level (btinfo, rhs, best_l->level - best_r->level);
ftrace_connect_backtrace (btinfo, best_l, best_r);
return best_matches;
}
/* Try to bridge gaps due to overflow or decode errors by connecting the
function segments that are separated by the gap. */
static void
btrace_bridge_gaps (struct thread_info *tp, std::vector<unsigned int> &gaps)
{
struct btrace_thread_info *btinfo = &tp->btrace;
std::vector<unsigned int> remaining;
int min_matches;
DEBUG ("bridge gaps");
/* We require a minimum amount of matches for bridging a gap. The number of
required matches will be lowered with each iteration.
The more matches the higher our confidence that the bridging is correct.
For big gaps or small traces, however, it may not be feasible to require a
high number of matches. */
for (min_matches = 5; min_matches > 0; --min_matches)
{
/* Let's try to bridge as many gaps as we can. In some cases, we need to
skip a gap and revisit it again after we closed later gaps. */
while (!gaps.empty ())
{
for (const unsigned int number : gaps)
{
struct btrace_function *gap, *lhs, *rhs;
int bridged;
gap = ftrace_find_call_by_number (btinfo, number);
/* We may have a sequence of gaps if we run from one error into