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