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
,
183 for (tmp
= 1; tmp
> 0; tmp
<<= 1)
185 if (large_lseek (fd
, tmp
, SEEK_SET
) > 0)
191 /* Compute an initial chunk size. The math is dodgy but it works
192 for the moment. Perhaphs there's a constant around somewhere.
193 Limit this to max_core_size bytes - no point in trying to
194 allocate more than can be written to the corefile. */
197 for (tmp
= 1; tmp
> 0 && tmp
< max_core_size
; tmp
<<= 1)
198 max_chunk_size
= tmp
;
201 print_string (" core: ");
202 print_byte_count (max_core_size
);
204 print_string (" chunk: ");
205 print_byte_count (max_chunk_size
);
207 print_string (" large? ");
209 print_string ("yes\n");
211 print_string ("no\n");
213 /* Allocate as much memory as possible creating a linked list of
214 each section. The linking ensures that some, but not all, the
215 memory is allocated. NB: Some kernels handle this efficiently -
216 only allocating and writing out referenced pages leaving holes in
217 the file for unmodified pages - while others handle this poorly -
218 writing out all pages including those that weren't modified. */
220 print_string ("Alocating the entire heap ...\n");
223 unsigned long chunks_allocated
= 0;
224 /* Create a linked list of memory chunks. Start with
225 MAX_CHUNK_SIZE blocks of memory and then try allocating smaller
226 and smaller amounts until all (well at least most) memory has
228 for (chunk_size
= max_chunk_size
;
229 chunk_size
>= sizeof (struct list
);
232 unsigned long count
= 0;
234 print_byte_count (chunk_size
);
235 print_string (" ... ");
236 while (bytes_allocated
+ (1 + count
) * chunk_size
239 struct list
*chunk
= malloc (chunk_size
);
242 chunk
->size
= chunk_size
;
245 chunk
->prev
= heap
.prev
;
246 heap
.prev
->next
= chunk
;
250 print_unsigned (count
);
251 print_string (" chunks\n");
252 chunks_allocated
+= count
;
253 bytes_allocated
+= chunk_size
* count
;
255 print_string ("Total of ");
256 print_byte_count (bytes_allocated
);
257 print_string (" bytes ");
258 print_unsigned (chunks_allocated
);
259 print_string (" chunks\n");
262 /* Push everything out to disk. */
264 print_string ("Dump core ....\n");