Switch the license of all .c files to GPLv3.
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / coremaker.c
1 /* Copyright 1992, 1993, 1994, 1995, 1996, 1999, 2007
2 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 /* Simple little program that just generates a core dump from inside some
20 nested function calls. */
21
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <fcntl.h>
25 #include <sys/mman.h>
26 #include <signal.h>
27 #include <stdlib.h>
28 #include <unistd.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
42 int coremaker_data = 1; /* In Data section */
43 int coremaker_bss; /* In BSS section */
44
45 const int coremaker_ro = 201; /* In Read-Only Data section */
46
47 /* Note that if the mapping fails for any reason, we set buf2
48 to -1 and the testsuite notices this and reports it as
49 a failure due to a mapping error. This way we don't have
50 to test for specific errors when running the core maker. */
51
52 void
53 mmapdata ()
54 {
55 int j, fd;
56
57 /* Allocate and initialize a buffer that will be used to write
58 the file that is later mapped in. */
59
60 buf1 = (char *) malloc (MAPSIZE);
61 for (j = 0; j < MAPSIZE; ++j)
62 {
63 buf1[j] = j;
64 }
65
66 /* Write the file to map in */
67
68 fd = open ("coremmap.data", O_CREAT | O_RDWR, 0666);
69 if (fd == -1)
70 {
71 perror ("coremmap.data open failed");
72 buf2 = (char *) -1;
73 return;
74 }
75 write (fd, buf1, MAPSIZE);
76
77 /* Now map the file into our address space as buf2 */
78
79 buf2 = (char *) mmap (0, MAPSIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
80 if (buf2 == (char *) -1)
81 {
82 perror ("mmap failed");
83 return;
84 }
85
86 /* Verify that the original data and the mapped data are identical.
87 If not, we'd rather fail now than when trying to access the mapped
88 data from the core file. */
89
90 for (j = 0; j < MAPSIZE; ++j)
91 {
92 if (buf1[j] != buf2[j])
93 {
94 fprintf (stderr, "mapped data is incorrect");
95 buf2 = (char *) -1;
96 return;
97 }
98 }
99 }
100
101 void
102 func2 ()
103 {
104 int coremaker_local[5];
105 int i;
106
107 #ifdef SA_FULLDUMP
108 /* Force a corefile that includes the data section for AIX. */
109 {
110 struct sigaction sa;
111
112 sigaction (SIGABRT, (struct sigaction *)0, &sa);
113 sa.sa_flags |= SA_FULLDUMP;
114 sigaction (SIGABRT, &sa, (struct sigaction *)0);
115 }
116 #endif
117
118 /* Make sure that coremaker_local doesn't get optimized away. */
119 for (i = 0; i < 5; i++)
120 coremaker_local[i] = i;
121 coremaker_bss = 0;
122 for (i = 0; i < 5; i++)
123 coremaker_bss += coremaker_local[i];
124 coremaker_data = coremaker_ro + 1;
125 abort ();
126 }
127
128 void
129 func1 ()
130 {
131 func2 ();
132 }
133
134 int main ()
135 {
136 mmapdata ();
137 func1 ();
138 return 0;
139 }
140
This page took 0.048064 seconds and 4 git commands to generate.