1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2004, 2007, 2008 Free Software Foundation, Inc.
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.
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.
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/>.
18 Please email any bugs, comments, and/or additions to this file to:
19 bug-gdb@prep.ai.mit.edu */
21 /* Get 64-bit stuff if on a GNU system. */
24 #include <sys/types.h>
26 #include <sys/resource.h>
33 /* This test was written for >2GB core files on 32-bit systems. On
34 current 64-bit systems, generating a >4EB (2 ** 63) core file is
35 not practical, and getting as close as we can takes a lot of
36 useless CPU time. So limit ourselves to a bit bigger than
37 32-bit, which is still a useful test. */
38 #define RLIMIT_CAP (1ULL << 34)
42 The following are so that printf et.al. can be avoided. Those
43 might try to use malloc() and that, for this code, would be a
46 #define printf do not use
48 const char digit
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
53 write (1, &c
, sizeof (c
));
57 print_unsigned (unsigned long long u
)
60 print_unsigned (u
/ 10);
61 print_char (digit
[u
% 10]);
65 print_hex (unsigned long long u
)
69 print_char (digit
[u
% 16]);
73 print_string (const char *s
)
75 for (; (*s
) != '\0'; s
++)
80 print_address (const void *a
)
83 print_hex ((unsigned long) a
);
87 print_byte_count (unsigned long long u
)
93 print_string (") bytes");
96 /* Print the current values of RESOURCE. */
99 print_rlimit (int resource
)
102 getrlimit (resource
, &rl
);
103 print_string ("cur=0x");
104 print_hex (rl
.rlim_cur
);
105 print_string (" max=0x");
106 print_hex (rl
.rlim_max
);
110 maximize_rlimit (int resource
, const char *prefix
)
114 print_string (prefix
);
116 print_rlimit (resource
);
117 getrlimit (resource
, &rl
);
118 rl
.rlim_cur
= rl
.rlim_max
;
119 if (sizeof (rl
.rlim_cur
) >= sizeof (RLIMIT_CAP
))
120 rl
.rlim_cur
= (rlim_t
) RLIMIT_CAP
;
121 setrlimit (resource
, &rl
);
122 print_string (" -> ");
123 print_rlimit (resource
);
127 /* Maintain a doublely linked list. */
135 /* Put the "heap" in the DATA section. That way it is more likely
136 that the variable will occur early in the core file (an address
137 before the heap) and hence more likely that GDB will at least get
140 To simplify the list append logic, start the heap out with one
141 entry (that lives in the BSS section). */
143 static struct list dummy
;
144 static struct list heap
= { &dummy
, &dummy
};
146 static unsigned long bytes_allocated
;
149 #define large_off_t off64_t
150 #define large_lseek lseek64
152 #define large_off_t off_t
153 #define O_LARGEFILE 0
154 #define large_lseek lseek
160 size_t max_chunk_size
;
161 large_off_t max_core_size
;
163 /* Try to expand all the resource limits beyond the point of sanity
164 - we're after the biggest possible core file. */
166 print_string ("Maximize resource limits ...\n");
168 maximize_rlimit (RLIMIT_CORE
, "core");
171 maximize_rlimit (RLIMIT_DATA
, "data");
174 maximize_rlimit (RLIMIT_STACK
, "stack");
177 maximize_rlimit (RLIMIT_AS
, "stack");
180 print_string ("Maximize allocation limits ...\n");
182 /* Compute the largest possible corefile size. No point in trying
183 to create a corefile larger than the largest file supported by
184 the file system. What about 64-bit lseek64? */
188 unlink ("bigcore.corefile");
189 fd
= open ("bigcore.corefile", O_RDWR
| O_CREAT
| O_TRUNC
| O_LARGEFILE
,
191 for (tmp
= 1; tmp
> 0; tmp
<<= 1)
193 if (large_lseek (fd
, tmp
, SEEK_SET
) > 0)
199 /* Compute an initial chunk size. The math is dodgy but it works
200 for the moment. Perhaphs there's a constant around somewhere.
201 Limit this to max_core_size bytes - no point in trying to
202 allocate more than can be written to the corefile. */
205 for (tmp
= 1; tmp
> 0 && tmp
< max_core_size
; tmp
<<= 1)
206 max_chunk_size
= tmp
;
209 print_string (" core: ");
210 print_byte_count (max_core_size
);
212 print_string (" chunk: ");
213 print_byte_count (max_chunk_size
);
215 print_string (" large? ");
217 print_string ("yes\n");
219 print_string ("no\n");
221 /* Allocate as much memory as possible creating a linked list of
222 each section. The linking ensures that some, but not all, the
223 memory is allocated. NB: Some kernels handle this efficiently -
224 only allocating and writing out referenced pages leaving holes in
225 the file for unmodified pages - while others handle this poorly -
226 writing out all pages including those that weren't modified. */
228 print_string ("Alocating the entire heap ...\n");
231 unsigned long chunks_allocated
= 0;
232 /* Create a linked list of memory chunks. Start with
233 MAX_CHUNK_SIZE blocks of memory and then try allocating smaller
234 and smaller amounts until all (well at least most) memory has
236 for (chunk_size
= max_chunk_size
;
237 chunk_size
>= sizeof (struct list
);
240 unsigned long count
= 0;
242 print_byte_count (chunk_size
);
243 print_string (" ... ");
244 while (bytes_allocated
+ (1 + count
) * chunk_size
247 struct list
*chunk
= malloc (chunk_size
);
250 chunk
->size
= chunk_size
;
253 chunk
->prev
= heap
.prev
;
254 heap
.prev
->next
= chunk
;
258 print_unsigned (count
);
259 print_string (" chunks\n");
260 chunks_allocated
+= count
;
261 bytes_allocated
+= chunk_size
* count
;
263 print_string ("Total of ");
264 print_byte_count (bytes_allocated
);
265 print_string (" bytes ");
266 print_unsigned (chunks_allocated
);
267 print_string (" chunks\n");
270 /* Push everything out to disk. */
272 print_string ("Dump core ....\n");