]>
Commit | Line | Data |
---|---|---|
1d506c26 | 1 | /* Copyright 1992-2024 Free Software Foundation, Inc. |
3426d5ec MC |
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 | |
a9762ec7 JB |
7 | the Free Software Foundation; either version 3 of the License, or |
8 | (at your option) any later version. | |
3426d5ec | 9 | |
a9762ec7 JB |
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. | |
3426d5ec MC |
14 | |
15 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
3426d5ec | 17 | |
c906108c SS |
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> | |
085dd6e6 JM |
26 | #include <stdlib.h> |
27 | #include <unistd.h> | |
a59add0c | 28 | #include <string.h> |
c906108c SS |
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; | |
f9e233c9 | 41 | char *buf2ro; |
94c265d7 | 42 | char *buf3; |
c906108c SS |
43 | |
44 | int coremaker_data = 1; /* In Data section */ | |
45 | int coremaker_bss; /* In BSS section */ | |
46 | ||
678c7a56 | 47 | /* Place a chunk of memory before coremaker_ro to improve the chances |
f4afd6cb | 48 | that coremaker_ro will end up on its own page. See: |
678c7a56 KB |
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}; | |
c906108c SS |
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; | |
c906108c SS |
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); | |
41977d16 | 88 | if (buf2 == (char *) MAP_FAILED) |
c906108c SS |
89 | { |
90 | perror ("mmap failed"); | |
91 | return; | |
92 | } | |
93 | ||
f9e233c9 KB |
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 | ||
c906108c SS |
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 | { | |
f9e233c9 | 109 | if (buf1[j] != buf2[j] || buf1[j] != buf2ro[j]) |
c906108c SS |
110 | { |
111 | fprintf (stderr, "mapped data is incorrect"); | |
f9e233c9 | 112 | buf2 = buf2ro = (char *) -1; |
c906108c SS |
113 | return; |
114 | } | |
115 | } | |
becf6477 PP |
116 | /* Touch buf2 so kernel writes it out into 'core'. */ |
117 | buf2[0] = buf1[0]; | |
94c265d7 KB |
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 | } | |
c906108c SS |
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 | ||
c0edd9ed JK |
162 | int |
163 | main (int argc, char **argv) | |
c906108c | 164 | { |
c0edd9ed JK |
165 | if (argc == 2 && strcmp (argv[1], "sleep") == 0) |
166 | { | |
167 | sleep (60); | |
168 | return 0; | |
169 | } | |
c906108c SS |
170 | mmapdata (); |
171 | func1 (); | |
085dd6e6 | 172 | return 0; |
c906108c | 173 | } |