Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-unwind.c
1 /* This test program is part of GDB, the GNU debugger.
2
3 Copyright 2015-2022 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18 /* This is the test program loaded into GDB by the py-unwind test. */
19
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 static void *
25 swap_value (void **location, void *new_value)
26 {
27 void *old_value = *location;
28 *location = new_value;
29 return old_value;
30 }
31
32 static void
33 bad_layout(void **variable_ptr, void *fp)
34 {
35 fprintf (stderr, "First variable should be allocated one word below "
36 "the frame. Got variable's address %p, frame at %p instead.\n",
37 variable_ptr, fp);
38 abort();
39 }
40
41 #define MY_FRAME (__builtin_frame_address (0))
42
43 static void
44 corrupt_frame_inner (void)
45 {
46 /* Save outer frame address, then corrupt the unwind chain by
47 setting the outer frame address in it to self. This is
48 ABI-specific: the first word of the frame contains previous frame
49 address in amd64. */
50 void *previous_fp = swap_value ((void **) MY_FRAME, MY_FRAME);
51
52 /* Verify the compiler allocates the first local variable one word
53 below frame. This is where the test unwinder expects to find the
54 correct outer frame address. */
55 if (&previous_fp + 1 != (void **) MY_FRAME)
56 bad_layout (&previous_fp + 1, MY_FRAME);
57
58 /* Now restore it so that we can return. The test sets the
59 breakpoint just before this happens, and GDB will not be able to
60 show the backtrace without JIT reader. */
61 swap_value ((void **) MY_FRAME, previous_fp); /* break backtrace-broken */
62 }
63
64 static void
65 corrupt_frame_outer (void)
66 {
67 /* See above for the explanation of the code here. This function
68 corrupts its frame, too, and then calls the inner one. */
69 void *previous_fp = swap_value ((void **) MY_FRAME, MY_FRAME);
70 if (&previous_fp + 1 != (void **) MY_FRAME)
71 bad_layout (&previous_fp, MY_FRAME);
72 corrupt_frame_inner ();
73 swap_value ((void **) MY_FRAME, previous_fp);
74 }
75
76 int
77 main ()
78 {
79 corrupt_frame_outer ();
80 return 0;
81 }
This page took 0.031487 seconds and 4 git commands to generate.