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