]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/coremaker.c
cbe9719e8db63d84e8b44eb02eb279ba3395ac9a
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / coremaker.c
1 /* Copyright 1992-2023 Free Software Foundation, Inc.
2
3 This file is part of GDB.
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 /* Simple little program that just generates a core dump from inside some
19 nested function calls. */
20
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <fcntl.h>
24 #include <sys/mman.h>
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29
30 #ifndef __STDC__
31 #define const /**/
32 #endif
33
34 #define MAPSIZE (8 * 1024)
35
36 /* Don't make these automatic vars or we will have to walk back up the
37 stack to access them. */
38
39 char *buf1;
40 char *buf2;
41 char *buf2ro;
42 char *buf3;
43
44 int coremaker_data = 1; /* In Data section */
45 int coremaker_bss; /* In BSS section */
46
47 /* Place a chunk of memory before coremaker_ro to improve the chances
48 that coremaker_ro will end up on its own page. See:
49
50 https://sourceware.org/pipermail/gdb-patches/2020-May/168168.html
51 https://sourceware.org/pipermail/gdb-patches/2020-May/168170.html */
52 const unsigned char filler_ro[MAPSIZE] = {1, 2, 3, 4, 5, 6, 7, 8};
53 const int coremaker_ro = 201; /* In Read-Only Data section */
54
55 /* Note that if the mapping fails for any reason, we set buf2
56 to -1 and the testsuite notices this and reports it as
57 a failure due to a mapping error. This way we don't have
58 to test for specific errors when running the core maker. */
59
60 void
61 mmapdata ()
62 {
63 int j, fd;
64
65 /* Allocate and initialize a buffer that will be used to write
66 the file that is later mapped in. */
67
68 buf1 = (char *) malloc (MAPSIZE);
69 for (j = 0; j < MAPSIZE; ++j)
70 {
71 buf1[j] = j;
72 }
73
74 /* Write the file to map in */
75
76 fd = open ("coremmap.data", O_CREAT | O_RDWR, 0666);
77 if (fd == -1)
78 {
79 perror ("coremmap.data open failed");
80 buf2 = (char *) -1;
81 return;
82 }
83 write (fd, buf1, MAPSIZE);
84
85 /* Now map the file into our address space as buf2 */
86
87 buf2 = (char *) mmap (0, MAPSIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
88 if (buf2 == (char *) MAP_FAILED)
89 {
90 perror ("mmap failed");
91 return;
92 }
93
94 /* Map in another copy, read-only. We won't write to this copy so it
95 will likely not end up in the core file. */
96 buf2ro = (char *) mmap (0, MAPSIZE, PROT_READ, MAP_PRIVATE, fd, 0);
97 if (buf2ro == (char *) -1)
98 {
99 perror ("mmap failed");
100 return;
101 }
102
103 /* Verify that the original data and the mapped data are identical.
104 If not, we'd rather fail now than when trying to access the mapped
105 data from the core file. */
106
107 for (j = 0; j < MAPSIZE; ++j)
108 {
109 if (buf1[j] != buf2[j] || buf1[j] != buf2ro[j])
110 {
111 fprintf (stderr, "mapped data is incorrect");
112 buf2 = buf2ro = (char *) -1;
113 return;
114 }
115 }
116 /* Touch buf2 so kernel writes it out into 'core'. */
117 buf2[0] = buf1[0];
118
119 /* Create yet another region which is allocated, but not written to. */
120 buf3 = mmap (NULL, MAPSIZE, PROT_READ | PROT_WRITE,
121 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
122 if (buf3 == (char *) -1)
123 {
124 perror ("mmap failed");
125 return;
126 }
127 }
128
129 void
130 func2 ()
131 {
132 int coremaker_local[5];
133 int i;
134
135 #ifdef SA_FULLDUMP
136 /* Force a corefile that includes the data section for AIX. */
137 {
138 struct sigaction sa;
139
140 sigaction (SIGABRT, (struct sigaction *)0, &sa);
141 sa.sa_flags |= SA_FULLDUMP;
142 sigaction (SIGABRT, &sa, (struct sigaction *)0);
143 }
144 #endif
145
146 /* Make sure that coremaker_local doesn't get optimized away. */
147 for (i = 0; i < 5; i++)
148 coremaker_local[i] = i;
149 coremaker_bss = 0;
150 for (i = 0; i < 5; i++)
151 coremaker_bss += coremaker_local[i];
152 coremaker_data = coremaker_ro + 1;
153 abort ();
154 }
155
156 void
157 func1 ()
158 {
159 func2 ();
160 }
161
162 int
163 main (int argc, char **argv)
164 {
165 if (argc == 2 && strcmp (argv[1], "sleep") == 0)
166 {
167 sleep (60);
168 return 0;
169 }
170 mmapdata ();
171 func1 ();
172 return 0;
173 }