Commit | Line | Data |
---|---|---|
b811d2c2 | 1 | /* Copyright 1992-2020 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; | |
94c265d7 | 41 | char *buf3; |
c906108c SS |
42 | |
43 | int coremaker_data = 1; /* In Data section */ | |
44 | int coremaker_bss; /* In BSS section */ | |
45 | ||
678c7a56 KB |
46 | /* Place a chunk of memory before coremaker_ro to improve the chances |
47 | that coremaker_ro will end up on it's own page. See: | |
48 | ||
49 | https://sourceware.org/pipermail/gdb-patches/2020-May/168168.html | |
50 | https://sourceware.org/pipermail/gdb-patches/2020-May/168170.html */ | |
51 | const unsigned char filler_ro[MAPSIZE] = {1, 2, 3, 4, 5, 6, 7, 8}; | |
c906108c SS |
52 | const int coremaker_ro = 201; /* In Read-Only Data section */ |
53 | ||
54 | /* Note that if the mapping fails for any reason, we set buf2 | |
55 | to -1 and the testsuite notices this and reports it as | |
56 | a failure due to a mapping error. This way we don't have | |
57 | to test for specific errors when running the core maker. */ | |
58 | ||
59 | void | |
60 | mmapdata () | |
61 | { | |
62 | int j, fd; | |
c906108c SS |
63 | |
64 | /* Allocate and initialize a buffer that will be used to write | |
65 | the file that is later mapped in. */ | |
66 | ||
67 | buf1 = (char *) malloc (MAPSIZE); | |
68 | for (j = 0; j < MAPSIZE; ++j) | |
69 | { | |
70 | buf1[j] = j; | |
71 | } | |
72 | ||
73 | /* Write the file to map in */ | |
74 | ||
75 | fd = open ("coremmap.data", O_CREAT | O_RDWR, 0666); | |
76 | if (fd == -1) | |
77 | { | |
78 | perror ("coremmap.data open failed"); | |
79 | buf2 = (char *) -1; | |
80 | return; | |
81 | } | |
82 | write (fd, buf1, MAPSIZE); | |
83 | ||
84 | /* Now map the file into our address space as buf2 */ | |
85 | ||
86 | buf2 = (char *) mmap (0, MAPSIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); | |
41977d16 | 87 | if (buf2 == (char *) MAP_FAILED) |
c906108c SS |
88 | { |
89 | perror ("mmap failed"); | |
90 | return; | |
91 | } | |
92 | ||
93 | /* Verify that the original data and the mapped data are identical. | |
94 | If not, we'd rather fail now than when trying to access the mapped | |
95 | data from the core file. */ | |
96 | ||
97 | for (j = 0; j < MAPSIZE; ++j) | |
98 | { | |
99 | if (buf1[j] != buf2[j]) | |
100 | { | |
101 | fprintf (stderr, "mapped data is incorrect"); | |
102 | buf2 = (char *) -1; | |
103 | return; | |
104 | } | |
105 | } | |
becf6477 PP |
106 | /* Touch buf2 so kernel writes it out into 'core'. */ |
107 | buf2[0] = buf1[0]; | |
94c265d7 KB |
108 | |
109 | /* Create yet another region which is allocated, but not written to. */ | |
110 | buf3 = mmap (NULL, MAPSIZE, PROT_READ | PROT_WRITE, | |
111 | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); | |
112 | if (buf3 == (char *) -1) | |
113 | { | |
114 | perror ("mmap failed"); | |
115 | return; | |
116 | } | |
c906108c SS |
117 | } |
118 | ||
119 | void | |
120 | func2 () | |
121 | { | |
122 | int coremaker_local[5]; | |
123 | int i; | |
124 | ||
125 | #ifdef SA_FULLDUMP | |
126 | /* Force a corefile that includes the data section for AIX. */ | |
127 | { | |
128 | struct sigaction sa; | |
129 | ||
130 | sigaction (SIGABRT, (struct sigaction *)0, &sa); | |
131 | sa.sa_flags |= SA_FULLDUMP; | |
132 | sigaction (SIGABRT, &sa, (struct sigaction *)0); | |
133 | } | |
134 | #endif | |
135 | ||
136 | /* Make sure that coremaker_local doesn't get optimized away. */ | |
137 | for (i = 0; i < 5; i++) | |
138 | coremaker_local[i] = i; | |
139 | coremaker_bss = 0; | |
140 | for (i = 0; i < 5; i++) | |
141 | coremaker_bss += coremaker_local[i]; | |
142 | coremaker_data = coremaker_ro + 1; | |
143 | abort (); | |
144 | } | |
145 | ||
146 | void | |
147 | func1 () | |
148 | { | |
149 | func2 (); | |
150 | } | |
151 | ||
c0edd9ed JK |
152 | int |
153 | main (int argc, char **argv) | |
c906108c | 154 | { |
c0edd9ed JK |
155 | if (argc == 2 && strcmp (argv[1], "sleep") == 0) |
156 | { | |
157 | sleep (60); | |
158 | return 0; | |
159 | } | |
c906108c SS |
160 | mmapdata (); |
161 | func1 (); | |
085dd6e6 | 162 | return 0; |
c906108c | 163 | } |