forked from gbenson/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframe.c
2827 lines (2394 loc) · 83.7 KB
/
frame.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
/* Cache and manage frames for GDB, the GNU debugger.
Copyright (C) 1986-2015 Free Software Foundation, Inc.
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 "frame.h"
#include "target.h"
#include "value.h"
#include "inferior.h" /* for inferior_ptid */
#include "regcache.h"
#include "user-regs.h"
#include "gdb_obstack.h"
#include "dummy-frame.h"
#include "sentinel-frame.h"
#include "gdbcore.h"
#include "annotate.h"
#include "language.h"
#include "frame-unwind.h"
#include "frame-base.h"
#include "command.h"
#include "gdbcmd.h"
#include "observer.h"
#include "objfiles.h"
#include "gdbthread.h"
#include "block.h"
#include "inline-frame.h"
#include "tracepoint.h"
#include "hashtab.h"
#include "valprint.h"
static struct frame_info *get_prev_frame_raw (struct frame_info *this_frame);
static const char *frame_stop_reason_symbol_string (enum unwind_stop_reason reason);
/* Status of some values cached in the frame_info object. */
enum cached_copy_status
{
/* Value is unknown. */
CC_UNKNOWN,
/* We have a value. */
CC_VALUE,
/* Value was not saved. */
CC_NOT_SAVED,
/* Value is unavailable. */
CC_UNAVAILABLE
};
/* We keep a cache of stack frames, each of which is a "struct
frame_info". The innermost one gets allocated (in
wait_for_inferior) each time the inferior stops; current_frame
points to it. Additional frames get allocated (in get_prev_frame)
as needed, and are chained through the next and prev fields. Any
time that the frame cache becomes invalid (most notably when we
execute something, but also if we change how we interpret the
frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
which reads new symbols)), we should call reinit_frame_cache. */
struct frame_info
{
/* Level of this frame. The inner-most (youngest) frame is at level
0. As you move towards the outer-most (oldest) frame, the level
increases. This is a cached value. It could just as easily be
computed by counting back from the selected frame to the inner
most frame. */
/* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
reserved to indicate a bogus frame - one that has been created
just to keep GDB happy (GDB always needs a frame). For the
moment leave this as speculation. */
int level;
/* The frame's program space. */
struct program_space *pspace;
/* The frame's address space. */
struct address_space *aspace;
/* The frame's low-level unwinder and corresponding cache. The
low-level unwinder is responsible for unwinding register values
for the previous frame. The low-level unwind methods are
selected based on the presence, or otherwise, of register unwind
information such as CFI. */
void *prologue_cache;
const struct frame_unwind *unwind;
/* Cached copy of the previous frame's architecture. */
struct
{
int p;
struct gdbarch *arch;
} prev_arch;
/* Cached copy of the previous frame's resume address. */
struct {
enum cached_copy_status status;
CORE_ADDR value;
} prev_pc;
/* Cached copy of the previous frame's function address. */
struct
{
CORE_ADDR addr;
int p;
} prev_func;
/* This frame's ID. */
struct
{
int p;
struct frame_id value;
} this_id;
/* The frame's high-level base methods, and corresponding cache.
The high level base methods are selected based on the frame's
debug info. */
const struct frame_base *base;
void *base_cache;
/* Pointers to the next (down, inner, younger) and previous (up,
outer, older) frame_info's in the frame cache. */
struct frame_info *next; /* down, inner, younger */
int prev_p;
struct frame_info *prev; /* up, outer, older */
/* The reason why we could not set PREV, or UNWIND_NO_REASON if we
could. Only valid when PREV_P is set. */
enum unwind_stop_reason stop_reason;
/* A frame specific string describing the STOP_REASON in more detail.
Only valid when PREV_P is set, but even then may still be NULL. */
const char *stop_string;
};
/* A frame stash used to speed up frame lookups. Create a hash table
to stash frames previously accessed from the frame cache for
quicker subsequent retrieval. The hash table is emptied whenever
the frame cache is invalidated. */
static htab_t frame_stash;
/* Internal function to calculate a hash from the frame_id addresses,
using as many valid addresses as possible. Frames below level 0
are not stored in the hash table. */
static hashval_t
frame_addr_hash (const void *ap)
{
const struct frame_info *frame = ap;
const struct frame_id f_id = frame->this_id.value;
hashval_t hash = 0;
gdb_assert (f_id.stack_status != FID_STACK_INVALID
|| f_id.code_addr_p
|| f_id.special_addr_p);
if (f_id.stack_status == FID_STACK_VALID)
hash = iterative_hash (&f_id.stack_addr,
sizeof (f_id.stack_addr), hash);
if (f_id.code_addr_p)
hash = iterative_hash (&f_id.code_addr,
sizeof (f_id.code_addr), hash);
if (f_id.special_addr_p)
hash = iterative_hash (&f_id.special_addr,
sizeof (f_id.special_addr), hash);
return hash;
}
/* Internal equality function for the hash table. This function
defers equality operations to frame_id_eq. */
static int
frame_addr_hash_eq (const void *a, const void *b)
{
const struct frame_info *f_entry = a;
const struct frame_info *f_element = b;
return frame_id_eq (f_entry->this_id.value,
f_element->this_id.value);
}
/* Internal function to create the frame_stash hash table. 100 seems
to be a good compromise to start the hash table at. */
static void
frame_stash_create (void)
{
frame_stash = htab_create (100,
frame_addr_hash,
frame_addr_hash_eq,
NULL);
}
/* Internal function to add a frame to the frame_stash hash table.
Returns false if a frame with the same ID was already stashed, true
otherwise. */
static int
frame_stash_add (struct frame_info *frame)
{
struct frame_info **slot;
/* Do not try to stash the sentinel frame. */
gdb_assert (frame->level >= 0);
slot = (struct frame_info **) htab_find_slot (frame_stash,
frame,
INSERT);
/* If we already have a frame in the stack with the same id, we
either have a stack cycle (corrupted stack?), or some bug
elsewhere in GDB. In any case, ignore the duplicate and return
an indication to the caller. */
if (*slot != NULL)
return 0;
*slot = frame;
return 1;
}
/* Internal function to search the frame stash for an entry with the
given frame ID. If found, return that frame. Otherwise return
NULL. */
static struct frame_info *
frame_stash_find (struct frame_id id)
{
struct frame_info dummy;
struct frame_info *frame;
dummy.this_id.value = id;
frame = htab_find (frame_stash, &dummy);
return frame;
}
/* Internal function to invalidate the frame stash by removing all
entries in it. This only occurs when the frame cache is
invalidated. */
static void
frame_stash_invalidate (void)
{
htab_empty (frame_stash);
}
/* Flag to control debugging. */
unsigned int frame_debug;
static void
show_frame_debug (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file, _("Frame debugging is %s.\n"), value);
}
/* Flag to indicate whether backtraces should stop at main et.al. */
static int backtrace_past_main;
static void
show_backtrace_past_main (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file,
_("Whether backtraces should "
"continue past \"main\" is %s.\n"),
value);
}
static int backtrace_past_entry;
static void
show_backtrace_past_entry (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file, _("Whether backtraces should continue past the "
"entry point of a program is %s.\n"),
value);
}
static unsigned int backtrace_limit = UINT_MAX;
static void
show_backtrace_limit (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file,
_("An upper bound on the number "
"of backtrace levels is %s.\n"),
value);
}
static void
fprint_field (struct ui_file *file, const char *name, int p, CORE_ADDR addr)
{
if (p)
fprintf_unfiltered (file, "%s=%s", name, hex_string (addr));
else
fprintf_unfiltered (file, "!%s", name);
}
void
fprint_frame_id (struct ui_file *file, struct frame_id id)
{
fprintf_unfiltered (file, "{");
if (id.stack_status == FID_STACK_INVALID)
fprintf_unfiltered (file, "!stack");
else if (id.stack_status == FID_STACK_UNAVAILABLE)
fprintf_unfiltered (file, "stack=<unavailable>");
else
fprintf_unfiltered (file, "stack=%s", hex_string (id.stack_addr));
fprintf_unfiltered (file, ",");
fprint_field (file, "code", id.code_addr_p, id.code_addr);
fprintf_unfiltered (file, ",");
fprint_field (file, "special", id.special_addr_p, id.special_addr);
if (id.artificial_depth)
fprintf_unfiltered (file, ",artificial=%d", id.artificial_depth);
fprintf_unfiltered (file, "}");
}
static void
fprint_frame_type (struct ui_file *file, enum frame_type type)
{
switch (type)
{
case NORMAL_FRAME:
fprintf_unfiltered (file, "NORMAL_FRAME");
return;
case DUMMY_FRAME:
fprintf_unfiltered (file, "DUMMY_FRAME");
return;
case INLINE_FRAME:
fprintf_unfiltered (file, "INLINE_FRAME");
return;
case TAILCALL_FRAME:
fprintf_unfiltered (file, "TAILCALL_FRAME");
return;
case SIGTRAMP_FRAME:
fprintf_unfiltered (file, "SIGTRAMP_FRAME");
return;
case ARCH_FRAME:
fprintf_unfiltered (file, "ARCH_FRAME");
return;
case SENTINEL_FRAME:
fprintf_unfiltered (file, "SENTINEL_FRAME");
return;
default:
fprintf_unfiltered (file, "<unknown type>");
return;
};
}
static void
fprint_frame (struct ui_file *file, struct frame_info *fi)
{
if (fi == NULL)
{
fprintf_unfiltered (file, "<NULL frame>");
return;
}
fprintf_unfiltered (file, "{");
fprintf_unfiltered (file, "level=%d", fi->level);
fprintf_unfiltered (file, ",");
fprintf_unfiltered (file, "type=");
if (fi->unwind != NULL)
fprint_frame_type (file, fi->unwind->type);
else
fprintf_unfiltered (file, "<unknown>");
fprintf_unfiltered (file, ",");
fprintf_unfiltered (file, "unwind=");
if (fi->unwind != NULL)
gdb_print_host_address (fi->unwind, file);
else
fprintf_unfiltered (file, "<unknown>");
fprintf_unfiltered (file, ",");
fprintf_unfiltered (file, "pc=");
if (fi->next == NULL || fi->next->prev_pc.status == CC_UNKNOWN)
fprintf_unfiltered (file, "<unknown>");
else if (fi->next->prev_pc.status == CC_VALUE)
fprintf_unfiltered (file, "%s",
hex_string (fi->next->prev_pc.value));
else if (fi->next->prev_pc.status == CC_NOT_SAVED)
val_print_not_saved (file);
else if (fi->next->prev_pc.status == CC_UNAVAILABLE)
val_print_unavailable (file);
fprintf_unfiltered (file, ",");
fprintf_unfiltered (file, "id=");
if (fi->this_id.p)
fprint_frame_id (file, fi->this_id.value);
else
fprintf_unfiltered (file, "<unknown>");
fprintf_unfiltered (file, ",");
fprintf_unfiltered (file, "func=");
if (fi->next != NULL && fi->next->prev_func.p)
fprintf_unfiltered (file, "%s", hex_string (fi->next->prev_func.addr));
else
fprintf_unfiltered (file, "<unknown>");
fprintf_unfiltered (file, "}");
}
/* Given FRAME, return the enclosing frame as found in real frames read-in from
inferior memory. Skip any previous frames which were made up by GDB.
Return the original frame if no immediate previous frames exist. */
static struct frame_info *
skip_artificial_frames (struct frame_info *frame)
{
/* Note we use get_prev_frame_always, and not get_prev_frame. The
latter will truncate the frame chain, leading to this function
unintentionally returning a null_frame_id (e.g., when the user
sets a backtrace limit). This is safe, because as these frames
are made up by GDB, there must be a real frame in the chain
below. */
while (get_frame_type (frame) == INLINE_FRAME
|| get_frame_type (frame) == TAILCALL_FRAME)
frame = get_prev_frame_always (frame);
return frame;
}
/* Compute the frame's uniq ID that can be used to, later, re-find the
frame. */
static void
compute_frame_id (struct frame_info *fi)
{
gdb_assert (!fi->this_id.p);
if (frame_debug)
fprintf_unfiltered (gdb_stdlog, "{ compute_frame_id (fi=%d) ",
fi->level);
/* Find the unwinder. */
if (fi->unwind == NULL)
frame_unwind_find_by_frame (fi, &fi->prologue_cache);
/* Find THIS frame's ID. */
/* Default to outermost if no ID is found. */
fi->this_id.value = outer_frame_id;
fi->unwind->this_id (fi, &fi->prologue_cache, &fi->this_id.value);
gdb_assert (frame_id_p (fi->this_id.value));
fi->this_id.p = 1;
if (frame_debug)
{
fprintf_unfiltered (gdb_stdlog, "-> ");
fprint_frame_id (gdb_stdlog, fi->this_id.value);
fprintf_unfiltered (gdb_stdlog, " }\n");
}
}
/* Return a frame uniq ID that can be used to, later, re-find the
frame. */
struct frame_id
get_frame_id (struct frame_info *fi)
{
if (fi == NULL)
return null_frame_id;
gdb_assert (fi->this_id.p);
return fi->this_id.value;
}
struct frame_id
get_stack_frame_id (struct frame_info *next_frame)
{
return get_frame_id (skip_artificial_frames (next_frame));
}
struct frame_id
frame_unwind_caller_id (struct frame_info *next_frame)
{
struct frame_info *this_frame;
/* Use get_prev_frame_always, and not get_prev_frame. The latter
will truncate the frame chain, leading to this function
unintentionally returning a null_frame_id (e.g., when a caller
requests the frame ID of "main()"s caller. */
next_frame = skip_artificial_frames (next_frame);
this_frame = get_prev_frame_always (next_frame);
if (this_frame)
return get_frame_id (skip_artificial_frames (this_frame));
else
return null_frame_id;
}
const struct frame_id null_frame_id = { 0 }; /* All zeros. */
const struct frame_id outer_frame_id = { 0, 0, 0, FID_STACK_INVALID, 0, 1, 0 };
struct frame_id
frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
CORE_ADDR special_addr)
{
struct frame_id id = null_frame_id;
id.stack_addr = stack_addr;
id.stack_status = FID_STACK_VALID;
id.code_addr = code_addr;
id.code_addr_p = 1;
id.special_addr = special_addr;
id.special_addr_p = 1;
return id;
}
/* See frame.h. */
struct frame_id
frame_id_build_unavailable_stack (CORE_ADDR code_addr)
{
struct frame_id id = null_frame_id;
id.stack_status = FID_STACK_UNAVAILABLE;
id.code_addr = code_addr;
id.code_addr_p = 1;
return id;
}
/* See frame.h. */
struct frame_id
frame_id_build_unavailable_stack_special (CORE_ADDR code_addr,
CORE_ADDR special_addr)
{
struct frame_id id = null_frame_id;
id.stack_status = FID_STACK_UNAVAILABLE;
id.code_addr = code_addr;
id.code_addr_p = 1;
id.special_addr = special_addr;
id.special_addr_p = 1;
return id;
}
struct frame_id
frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
{
struct frame_id id = null_frame_id;
id.stack_addr = stack_addr;
id.stack_status = FID_STACK_VALID;
id.code_addr = code_addr;
id.code_addr_p = 1;
return id;
}
struct frame_id
frame_id_build_wild (CORE_ADDR stack_addr)
{
struct frame_id id = null_frame_id;
id.stack_addr = stack_addr;
id.stack_status = FID_STACK_VALID;
return id;
}
int
frame_id_p (struct frame_id l)
{
int p;
/* The frame is valid iff it has a valid stack address. */
p = l.stack_status != FID_STACK_INVALID;
/* outer_frame_id is also valid. */
if (!p && memcmp (&l, &outer_frame_id, sizeof (l)) == 0)
p = 1;
if (frame_debug)
{
fprintf_unfiltered (gdb_stdlog, "{ frame_id_p (l=");
fprint_frame_id (gdb_stdlog, l);
fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", p);
}
return p;
}
int
frame_id_artificial_p (struct frame_id l)
{
if (!frame_id_p (l))
return 0;
return (l.artificial_depth != 0);
}
int
frame_id_eq (struct frame_id l, struct frame_id r)
{
int eq;
if (l.stack_status == FID_STACK_INVALID && l.special_addr_p
&& r.stack_status == FID_STACK_INVALID && r.special_addr_p)
/* The outermost frame marker is equal to itself. This is the
dodgy thing about outer_frame_id, since between execution steps
we might step into another function - from which we can't
unwind either. More thought required to get rid of
outer_frame_id. */
eq = 1;
else if (l.stack_status == FID_STACK_INVALID
|| r.stack_status == FID_STACK_INVALID)
/* Like a NaN, if either ID is invalid, the result is false.
Note that a frame ID is invalid iff it is the null frame ID. */
eq = 0;
else if (l.stack_status != r.stack_status || l.stack_addr != r.stack_addr)
/* If .stack addresses are different, the frames are different. */
eq = 0;
else if (l.code_addr_p && r.code_addr_p && l.code_addr != r.code_addr)
/* An invalid code addr is a wild card. If .code addresses are
different, the frames are different. */
eq = 0;
else if (l.special_addr_p && r.special_addr_p
&& l.special_addr != r.special_addr)
/* An invalid special addr is a wild card (or unused). Otherwise
if special addresses are different, the frames are different. */
eq = 0;
else if (l.artificial_depth != r.artificial_depth)
/* If artifical depths are different, the frames must be different. */
eq = 0;
else
/* Frames are equal. */
eq = 1;
if (frame_debug)
{
fprintf_unfiltered (gdb_stdlog, "{ frame_id_eq (l=");
fprint_frame_id (gdb_stdlog, l);
fprintf_unfiltered (gdb_stdlog, ",r=");
fprint_frame_id (gdb_stdlog, r);
fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", eq);
}
return eq;
}
/* Safety net to check whether frame ID L should be inner to
frame ID R, according to their stack addresses.
This method cannot be used to compare arbitrary frames, as the
ranges of valid stack addresses may be discontiguous (e.g. due
to sigaltstack).
However, it can be used as safety net to discover invalid frame
IDs in certain circumstances. Assuming that NEXT is the immediate
inner frame to THIS and that NEXT and THIS are both NORMAL frames:
* The stack address of NEXT must be inner-than-or-equal to the stack
address of THIS.
Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
error has occurred.
* If NEXT and THIS have different stack addresses, no other frame
in the frame chain may have a stack address in between.
Therefore, if frame_id_inner (TEST, THIS) holds, but
frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
to a valid frame in the frame chain.
The sanity checks above cannot be performed when a SIGTRAMP frame
is involved, because signal handlers might be executed on a different
stack than the stack used by the routine that caused the signal
to be raised. This can happen for instance when a thread exceeds
its maximum stack size. In this case, certain compilers implement
a stack overflow strategy that cause the handler to be run on a
different stack. */
static int
frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
{
int inner;
if (l.stack_status != FID_STACK_VALID || r.stack_status != FID_STACK_VALID)
/* Like NaN, any operation involving an invalid ID always fails.
Likewise if either ID has an unavailable stack address. */
inner = 0;
else if (l.artificial_depth > r.artificial_depth
&& l.stack_addr == r.stack_addr
&& l.code_addr_p == r.code_addr_p
&& l.special_addr_p == r.special_addr_p
&& l.special_addr == r.special_addr)
{
/* Same function, different inlined functions. */
const struct block *lb, *rb;
gdb_assert (l.code_addr_p && r.code_addr_p);
lb = block_for_pc (l.code_addr);
rb = block_for_pc (r.code_addr);
if (lb == NULL || rb == NULL)
/* Something's gone wrong. */
inner = 0;
else
/* This will return true if LB and RB are the same block, or
if the block with the smaller depth lexically encloses the
block with the greater depth. */
inner = contained_in (lb, rb);
}
else
/* Only return non-zero when strictly inner than. Note that, per
comment in "frame.h", there is some fuzz here. Frameless
functions are not strictly inner than (same .stack but
different .code and/or .special address). */
inner = gdbarch_inner_than (gdbarch, l.stack_addr, r.stack_addr);
if (frame_debug)
{
fprintf_unfiltered (gdb_stdlog, "{ frame_id_inner (l=");
fprint_frame_id (gdb_stdlog, l);
fprintf_unfiltered (gdb_stdlog, ",r=");
fprint_frame_id (gdb_stdlog, r);
fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", inner);
}
return inner;
}
struct frame_info *
frame_find_by_id (struct frame_id id)
{
struct frame_info *frame, *prev_frame;
/* ZERO denotes the null frame, let the caller decide what to do
about it. Should it instead return get_current_frame()? */
if (!frame_id_p (id))
return NULL;
/* Try using the frame stash first. Finding it there removes the need
to perform the search by looping over all frames, which can be very
CPU-intensive if the number of frames is very high (the loop is O(n)
and get_prev_frame performs a series of checks that are relatively
expensive). This optimization is particularly useful when this function
is called from another function (such as value_fetch_lazy, case
VALUE_LVAL (val) == lval_register) which already loops over all frames,
making the overall behavior O(n^2). */
frame = frame_stash_find (id);
if (frame)
return frame;
for (frame = get_current_frame (); ; frame = prev_frame)
{
struct frame_id self = get_frame_id (frame);
if (frame_id_eq (id, self))
/* An exact match. */
return frame;
prev_frame = get_prev_frame (frame);
if (!prev_frame)
return NULL;
/* As a safety net to avoid unnecessary backtracing while trying
to find an invalid ID, we check for a common situation where
we can detect from comparing stack addresses that no other
frame in the current frame chain can have this ID. See the
comment at frame_id_inner for details. */
if (get_frame_type (frame) == NORMAL_FRAME
&& !frame_id_inner (get_frame_arch (frame), id, self)
&& frame_id_inner (get_frame_arch (prev_frame), id,
get_frame_id (prev_frame)))
return NULL;
}
return NULL;
}
static CORE_ADDR
frame_unwind_pc (struct frame_info *this_frame)
{
if (this_frame->prev_pc.status == CC_UNKNOWN)
{
if (gdbarch_unwind_pc_p (frame_unwind_arch (this_frame)))
{
struct gdbarch *prev_gdbarch;
CORE_ADDR pc = 0;
int pc_p = 0;
/* The right way. The `pure' way. The one true way. This
method depends solely on the register-unwind code to
determine the value of registers in THIS frame, and hence
the value of this frame's PC (resume address). A typical
implementation is no more than:
frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
Note: this method is very heavily dependent on a correct
register-unwind implementation, it pays to fix that
method first; this method is frame type agnostic, since
it only deals with register values, it works with any
frame. This is all in stark contrast to the old
FRAME_SAVED_PC which would try to directly handle all the
different ways that a PC could be unwound. */
prev_gdbarch = frame_unwind_arch (this_frame);
TRY
{
pc = gdbarch_unwind_pc (prev_gdbarch, this_frame);
pc_p = 1;
}
CATCH (ex, RETURN_MASK_ERROR)
{
if (ex.error == NOT_AVAILABLE_ERROR)
{
this_frame->prev_pc.status = CC_UNAVAILABLE;
if (frame_debug)
fprintf_unfiltered (gdb_stdlog,
"{ frame_unwind_pc (this_frame=%d)"
" -> <unavailable> }\n",
this_frame->level);
}
else if (ex.error == OPTIMIZED_OUT_ERROR)
{
this_frame->prev_pc.status = CC_NOT_SAVED;
if (frame_debug)
fprintf_unfiltered (gdb_stdlog,
"{ frame_unwind_pc (this_frame=%d)"
" -> <not saved> }\n",
this_frame->level);
}
else
throw_exception (ex);
}
END_CATCH
if (pc_p)
{
this_frame->prev_pc.value = pc;
this_frame->prev_pc.status = CC_VALUE;
if (frame_debug)
fprintf_unfiltered (gdb_stdlog,
"{ frame_unwind_pc (this_frame=%d) "
"-> %s }\n",
this_frame->level,
hex_string (this_frame->prev_pc.value));
}
}
else
internal_error (__FILE__, __LINE__, _("No unwind_pc method"));
}
if (this_frame->prev_pc.status == CC_VALUE)
return this_frame->prev_pc.value;
else if (this_frame->prev_pc.status == CC_UNAVAILABLE)
throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
else if (this_frame->prev_pc.status == CC_NOT_SAVED)
throw_error (OPTIMIZED_OUT_ERROR, _("PC not saved"));
else
internal_error (__FILE__, __LINE__,
"unexpected prev_pc status: %d",
(int) this_frame->prev_pc.status);
}
CORE_ADDR
frame_unwind_caller_pc (struct frame_info *this_frame)
{
return frame_unwind_pc (skip_artificial_frames (this_frame));
}
int
get_frame_func_if_available (struct frame_info *this_frame, CORE_ADDR *pc)
{
struct frame_info *next_frame = this_frame->next;
if (!next_frame->prev_func.p)
{
CORE_ADDR addr_in_block;
/* Make certain that this, and not the adjacent, function is
found. */
if (!get_frame_address_in_block_if_available (this_frame, &addr_in_block))
{
next_frame->prev_func.p = -1;
if (frame_debug)
fprintf_unfiltered (gdb_stdlog,
"{ get_frame_func (this_frame=%d)"
" -> unavailable }\n",
this_frame->level);
}
else
{
next_frame->prev_func.p = 1;
next_frame->prev_func.addr = get_pc_function_start (addr_in_block);
if (frame_debug)
fprintf_unfiltered (gdb_stdlog,
"{ get_frame_func (this_frame=%d) -> %s }\n",
this_frame->level,
hex_string (next_frame->prev_func.addr));
}
}
if (next_frame->prev_func.p < 0)
{
*pc = -1;
return 0;
}
else
{
*pc = next_frame->prev_func.addr;
return 1;
}
}
CORE_ADDR
get_frame_func (struct frame_info *this_frame)
{
CORE_ADDR pc;
if (!get_frame_func_if_available (this_frame, &pc))
throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
return pc;
}
static enum register_status
do_frame_register_read (void *src, int regnum, gdb_byte *buf)
{
if (!deprecated_frame_register_read (src, regnum, buf))
return REG_UNAVAILABLE;
else
return REG_VALID;
}
struct regcache *
frame_save_as_regcache (struct frame_info *this_frame)
{
struct address_space *aspace = get_frame_address_space (this_frame);
struct regcache *regcache = regcache_xmalloc (get_frame_arch (this_frame),
aspace);
struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache);
regcache_save (regcache, do_frame_register_read, this_frame);
discard_cleanups (cleanups);
return regcache;
}
void
frame_pop (struct frame_info *this_frame)
{
struct frame_info *prev_frame;
struct regcache *scratch;
struct cleanup *cleanups;
if (get_frame_type (this_frame) == DUMMY_FRAME)
{
/* Popping a dummy frame involves restoring more than just registers.
dummy_frame_pop does all the work. */
dummy_frame_pop (get_frame_id (this_frame), inferior_ptid);
return;
}
/* Ensure that we have a frame to pop to. */
prev_frame = get_prev_frame_always (this_frame);
if (!prev_frame)
error (_("Cannot pop the initial frame."));
/* Ignore TAILCALL_FRAME type frames, they were executed already before
entering THISFRAME. */
while (get_frame_type (prev_frame) == TAILCALL_FRAME)
prev_frame = get_prev_frame (prev_frame);
/* Make a copy of all the register values unwound from this frame.
Save them in a scratch buffer so that there isn't a race between
trying to extract the old values from the current regcache while
at the same time writing new values into that same cache. */
scratch = frame_save_as_regcache (prev_frame);
cleanups = make_cleanup_regcache_xfree (scratch);
/* FIXME: cagney/2003-03-16: It should be possible to tell the
target's register cache that it is about to be hit with a burst
register transfer and that the sequence of register writes should
be batched. The pair target_prepare_to_store() and
target_store_registers() kind of suggest this functionality.
Unfortunately, they don't implement it. Their lack of a formal
definition can lead to targets writing back bogus values
(arguably a bug in the target code mind). */
/* Now copy those saved registers into the current regcache.
Here, regcache_cpy() calls regcache_restore(). */
regcache_cpy (get_current_regcache (), scratch);
do_cleanups (cleanups);
/* We've made right mess of GDB's local state, just discard
everything. */
reinit_frame_cache ();