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