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