]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/coremaker2.c
Fix gdb.base/corefile2.exp test case for ppc64le
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / coremaker2.c
1 /* Copyright 1992-2020 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 /* This test has two large memory areas buf_rw and buf_ro.
19
20 buf_rw is written to by the program while buf_ro is initialized at
21 compile / load time. Thus, when a core file is created, buf_rw's
22 memory should reside in the core file, but buf_ro probably won't be.
23 Instead, the contents of buf_ro are available from the executable.
24
25 Now, for the wrinkle: We create a one page read-only mapping over
26 both of these areas. This will create a one page "hole" of all
27 zeros in each area.
28
29 Will GDB be able to correctly read memory from each of the four
30 (or six, if you count the regions on the other side of each hole)
31 memory regions? */
32
33 #include <stdio.h>
34 #include <sys/types.h>
35 #include <fcntl.h>
36 #include <sys/mman.h>
37 #include <signal.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <errno.h>
42
43 /* These are globals so that we can find them easily when debugging
44 the core file. */
45 long pagesize;
46 unsigned long long addr;
47 char *mbuf_ro;
48 char *mbuf_rw;
49
50 /* 256 KiB buffer. */
51 char buf_rw[256 * 1024];
52
53 #define C5_16 \
54 0xc5, 0xc5, 0xc5, 0xc5, \
55 0xc5, 0xc5, 0xc5, 0xc5, \
56 0xc5, 0xc5, 0xc5, 0xc5, \
57 0xc5, 0xc5, 0xc5, 0xc5
58
59 #define C5_256 \
60 C5_16, C5_16, C5_16, C5_16, \
61 C5_16, C5_16, C5_16, C5_16, \
62 C5_16, C5_16, C5_16, C5_16, \
63 C5_16, C5_16, C5_16, C5_16
64
65 #define C5_1k \
66 C5_256, C5_256, C5_256, C5_256
67
68 #define C5_8k \
69 C5_1k, C5_1k, C5_1k, C5_1k, \
70 C5_1k, C5_1k, C5_1k, C5_1k
71
72 #define C5_64k \
73 C5_8k, C5_8k, C5_8k, C5_8k, \
74 C5_8k, C5_8k, C5_8k, C5_8k
75
76 #define C5_256k \
77 C5_64k, C5_64k, C5_64k, C5_64k
78
79 /* 256 KiB worth of data. For this test case, we can't allocate a
80 buffer and then fill it; we want GDB to have to read this data
81 from the executable; it should NOT find it in the core file. */
82
83 const char buf_ro[] = { C5_256k };
84
85 int
86 main (int argc, char **argv)
87 {
88 int i, bitcount;
89
90 #ifdef _SC_PAGESIZE
91 pagesize = sysconf (_SC_PAGESIZE);
92 #else
93 pagesize = 8192;
94 #endif
95
96 /* Verify that pagesize is a power of 2. */
97 bitcount = 0;
98 for (i = 0; i < 4 * sizeof (pagesize); i++)
99 if (pagesize & (1 << i))
100 bitcount++;
101
102 if (bitcount != 1)
103 {
104 fprintf (stderr, "pagesize is not a power of 2.\n");
105 exit (1);
106 }
107
108 /* Compute an address that should be within buf_ro. Complain if not. */
109 addr = ((unsigned long long) buf_ro + pagesize) & ~(pagesize - 1);
110
111 if (addr <= (unsigned long long) buf_ro
112 || addr >= (unsigned long long) buf_ro + sizeof (buf_ro))
113 {
114 fprintf (stderr, "Unable to compute a suitable address within buf_ro.\n");
115 exit (1);
116 }
117
118 mbuf_ro = mmap ((void *) addr, pagesize, PROT_READ,
119 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
120
121 if (mbuf_ro == MAP_FAILED)
122 {
123 fprintf (stderr, "mmap #1 failed: %s.\n", strerror (errno));
124 exit (1);
125 }
126
127 /* Write (and fill) the R/W region. */
128 for (i = 0; i < sizeof (buf_rw); i++)
129 buf_rw[i] = 0x6b;
130
131 /* Compute an mmap address within buf_rw. Complain if it's somewhere
132 else. */
133 addr = ((unsigned long long) buf_rw + pagesize) & ~(pagesize - 1);
134
135 if (addr <= (unsigned long long) buf_rw
136 || addr >= (unsigned long long) buf_rw + sizeof (buf_rw))
137 {
138 fprintf (stderr, "Unable to compute a suitable address within buf_rw.\n");
139 exit (1);
140 }
141
142 mbuf_rw = mmap ((void *) addr, pagesize, PROT_READ,
143 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
144
145 if (mbuf_rw == MAP_FAILED)
146 {
147 fprintf (stderr, "mmap #2 failed: %s.\n", strerror (errno));
148 exit (1);
149 }
150
151 /* With correct ulimit, etc. this should cause a core dump. */
152 abort ();
153 }