Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / coremaker2.c
CommitLineData
88b9d363 1/* Copyright 1992-2022 Free Software Foundation, Inc.
7da515fd
KB
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>
0245e136 42#include <inttypes.h>
7da515fd
KB
43
44/* These are globals so that we can find them easily when debugging
45 the core file. */
46long pagesize;
0245e136 47uintptr_t addr;
7da515fd
KB
48char *mbuf_ro;
49char *mbuf_rw;
50
9ef1ec5d
KB
51/* 256 KiB buffer. */
52char buf_rw[256 * 1024];
7da515fd
KB
53
54#define C5_16 \
55 0xc5, 0xc5, 0xc5, 0xc5, \
56 0xc5, 0xc5, 0xc5, 0xc5, \
57 0xc5, 0xc5, 0xc5, 0xc5, \
58 0xc5, 0xc5, 0xc5, 0xc5
59
60#define C5_256 \
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 C5_16, C5_16, C5_16, C5_16
65
66#define C5_1k \
67 C5_256, C5_256, C5_256, C5_256
68
9ef1ec5d 69#define C5_8k \
7da515fd
KB
70 C5_1k, C5_1k, C5_1k, C5_1k, \
71 C5_1k, C5_1k, C5_1k, C5_1k
72
9ef1ec5d
KB
73#define C5_64k \
74 C5_8k, C5_8k, C5_8k, C5_8k, \
75 C5_8k, C5_8k, C5_8k, C5_8k
76
77#define C5_256k \
78 C5_64k, C5_64k, C5_64k, C5_64k
79
80/* 256 KiB worth of data. For this test case, we can't allocate a
81 buffer and then fill it; we want GDB to have to read this data
82 from the executable; it should NOT find it in the core file. */
83
84const char buf_ro[] = { C5_256k };
7da515fd
KB
85
86int
87main (int argc, char **argv)
88{
89 int i, bitcount;
90
91#ifdef _SC_PAGESIZE
92 pagesize = sysconf (_SC_PAGESIZE);
93#else
94 pagesize = 8192;
95#endif
96
97 /* Verify that pagesize is a power of 2. */
98 bitcount = 0;
99 for (i = 0; i < 4 * sizeof (pagesize); i++)
100 if (pagesize & (1 << i))
101 bitcount++;
102
103 if (bitcount != 1)
104 {
105 fprintf (stderr, "pagesize is not a power of 2.\n");
106 exit (1);
107 }
108
109 /* Compute an address that should be within buf_ro. Complain if not. */
0245e136 110 addr = ((uintptr_t) buf_ro + pagesize) & ~(pagesize - 1);
7da515fd 111
0245e136
KB
112 if (addr <= (uintptr_t) buf_ro
113 || addr >= (uintptr_t) buf_ro + sizeof (buf_ro))
7da515fd
KB
114 {
115 fprintf (stderr, "Unable to compute a suitable address within buf_ro.\n");
116 exit (1);
117 }
118
119 mbuf_ro = mmap ((void *) addr, pagesize, PROT_READ,
120 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
121
122 if (mbuf_ro == MAP_FAILED)
123 {
124 fprintf (stderr, "mmap #1 failed: %s.\n", strerror (errno));
125 exit (1);
126 }
127
128 /* Write (and fill) the R/W region. */
129 for (i = 0; i < sizeof (buf_rw); i++)
130 buf_rw[i] = 0x6b;
131
132 /* Compute an mmap address within buf_rw. Complain if it's somewhere
133 else. */
0245e136 134 addr = ((uintptr_t) buf_rw + pagesize) & ~(pagesize - 1);
7da515fd 135
0245e136
KB
136 if (addr <= (uintptr_t) buf_rw
137 || addr >= (uintptr_t) buf_rw + sizeof (buf_rw))
7da515fd
KB
138 {
139 fprintf (stderr, "Unable to compute a suitable address within buf_rw.\n");
140 exit (1);
141 }
142
143 mbuf_rw = mmap ((void *) addr, pagesize, PROT_READ,
144 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
145
146 if (mbuf_rw == MAP_FAILED)
147 {
148 fprintf (stderr, "mmap #2 failed: %s.\n", strerror (errno));
149 exit (1);
150 }
151
152 /* With correct ulimit, etc. this should cause a core dump. */
153 abort ();
154}
This page took 0.178969 seconds and 4 git commands to generate.