1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2004 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 2 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, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 Please email any bugs, comments, and/or additions to this file to:
20 bug-gdb@prep.ai.mit.edu */
22 /* Get 64-bit stuff if on a GNU system. */
25 #include <sys/types.h>
27 #include <sys/resource.h>
36 The following are so that printf et.al. can be avoided. Those
37 might try to use malloc() and that, for this code, would be a
40 #define printf do not use
42 const char digit
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
47 write (1, &c
, sizeof (c
));
51 print_unsigned (unsigned long long u
)
54 print_unsigned (u
/ 10);
55 print_char (digit
[u
% 10]);
59 print_hex (unsigned long long u
)
63 print_char (digit
[u
% 16]);
67 print_string (const char *s
)
69 for (; (*s
) != '\0'; s
++)
74 print_address (const void *a
)
77 print_hex ((unsigned long) a
);
81 print_byte_count (unsigned long long u
)
87 print_string (") bytes");
90 /* Print the current values of RESOURCE. */
93 print_rlimit (int resource
)
96 getrlimit (resource
, &rl
);
97 print_string ("cur=0x");
98 print_hex (rl
.rlim_cur
);
99 print_string (" max=0x");
100 print_hex (rl
.rlim_max
);
104 maximize_rlimit (int resource
, const char *prefix
)
108 print_string (prefix
);
110 print_rlimit (resource
);
111 getrlimit (resource
, &rl
);
112 rl
.rlim_cur
= rl
.rlim_max
;
113 setrlimit (resource
, &rl
);
114 print_string (" -> ");
115 print_rlimit (resource
);
119 /* Maintain a doublely linked list. */
127 /* Put the "heap" in the DATA section. That way it is more likely
128 that the variable will occur early in the core file (an address
129 before the heap) and hence more likely that GDB will at least get
132 To simplify the list append logic, start the heap out with one
133 entry (that lives in the BSS section). */
135 static struct list dummy
;
136 static struct list heap
= { &dummy
, &dummy
};
138 static unsigned long bytes_allocated
;
141 #define large_off_t off64_t
142 #define large_lseek lseek64
144 #define large_off_t off_t
145 #define O_LARGEFILE 0
146 #define large_lseek lseek
152 size_t max_chunk_size
;
153 large_off_t max_core_size
;
155 /* Try to expand all the resource limits beyond the point of sanity
156 - we're after the biggest possible core file. */
158 print_string ("Maximize resource limits ...\n");
160 maximize_rlimit (RLIMIT_CORE
, "core");
163 maximize_rlimit (RLIMIT_DATA
, "data");
166 maximize_rlimit (RLIMIT_STACK
, "stack");
169 maximize_rlimit (RLIMIT_AS
, "stack");
172 print_string ("Maximize allocation limits ...\n");
174 /* Compute the largest possible corefile size. No point in trying
175 to create a corefile larger than the largest file supported by
176 the file system. What about 64-bit lseek64? */
180 unlink ("bigcore.corefile");
181 fd
= open ("bigcore.corefile", O_RDWR
| O_CREAT
| O_TRUNC
| O_LARGEFILE
);
182 for (tmp
= 1; tmp
> 0; tmp
<<= 1)
184 if (large_lseek (fd
, tmp
, SEEK_SET
) > 0)
190 /* Compute an initial chunk size. The math is dodgy but it works
191 for the moment. Perhaphs there's a constant around somewhere.
192 Limit this to max_core_size bytes - no point in trying to
193 allocate more than can be written to the corefile. */
196 for (tmp
= 1; tmp
> 0 && tmp
< max_core_size
; tmp
<<= 1)
197 max_chunk_size
= tmp
;
200 print_string (" core: ");
201 print_byte_count (max_core_size
);
203 print_string (" chunk: ");
204 print_byte_count (max_chunk_size
);
206 print_string (" large? ");
208 print_string ("yes\n");
210 print_string ("no\n");
212 /* Allocate as much memory as possible creating a linked list of
213 each section. The linking ensures that some, but not all, the
214 memory is allocated. NB: Some kernels handle this efficiently -
215 only allocating and writing out referenced pages leaving holes in
216 the file for unmodified pages - while others handle this poorly -
217 writing out all pages including those that weren't modified. */
219 print_string ("Alocating the entire heap ...\n");
222 unsigned long chunks_allocated
= 0;
223 /* Create a linked list of memory chunks. Start with
224 MAX_CHUNK_SIZE blocks of memory and then try allocating smaller
225 and smaller amounts until all (well at least most) memory has
227 for (chunk_size
= max_chunk_size
;
228 chunk_size
>= sizeof (struct list
);
231 unsigned long count
= 0;
233 print_byte_count (chunk_size
);
234 print_string (" ... ");
235 while (bytes_allocated
+ (1 + count
) * chunk_size
238 struct list
*chunk
= malloc (chunk_size
);
241 chunk
->size
= chunk_size
;
244 chunk
->prev
= heap
.prev
;
245 heap
.prev
->next
= chunk
;
249 print_unsigned (count
);
250 print_string (" chunks\n");
251 chunks_allocated
+= count
;
252 bytes_allocated
+= chunk_size
* count
;
254 print_string ("Total of ");
255 print_byte_count (bytes_allocated
);
256 print_string (" bytes ");
257 print_unsigned (chunks_allocated
);
258 print_string (" chunks\n");
261 /* Push everything out to disk. */
263 print_string ("Dump core ....\n");