| 1 | /* Copyright 1992, 1993, 1994, 1995, 1996, 1999, 2007, 2008, 2009, 2010 |
| 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 | /* Touch buf2 so kernel writes it out into 'core'. */ |
| 100 | buf2[0] = buf1[0]; |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | func2 () |
| 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 | |
| 130 | void |
| 131 | func1 () |
| 132 | { |
| 133 | func2 (); |
| 134 | } |
| 135 | |
| 136 | int |
| 137 | main (int argc, char **argv) |
| 138 | { |
| 139 | if (argc == 2 && strcmp (argv[1], "sleep") == 0) |
| 140 | { |
| 141 | sleep (60); |
| 142 | return 0; |
| 143 | } |
| 144 | mmapdata (); |
| 145 | func1 (); |
| 146 | return 0; |
| 147 | } |