* gdb.base/interp.exp: Remove trailing n in gdb_test commands.
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / bigcore.c
1 /* This testcase is part of GDB, the GNU debugger.
2
3 Copyright 2004 Free Software Foundation, Inc.
4
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.
9
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.
14
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.
18
19 Please email any bugs, comments, and/or additions to this file to:
20 bug-gdb@prep.ai.mit.edu */
21
22 /* Get 64-bit stuff if on a GNU system. */
23 #define _GNU_SOURCE
24
25 #include <sys/types.h>
26 #include <sys/time.h>
27 #include <sys/resource.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 /* Print routines:
35
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
38 disaster. */
39
40 #define printf do not use
41
42 const char digit[] = "0123456789abcdefghijklmnopqrstuvwxyz";
43
44 static void
45 print_char (char c)
46 {
47 write (1, &c, sizeof (c));
48 }
49
50 static void
51 print_unsigned (unsigned long long u)
52 {
53 if (u >= 10)
54 print_unsigned (u / 10);
55 print_char (digit[u % 10]);
56 }
57
58 static void
59 print_hex (unsigned long long u)
60 {
61 if (u >= 16)
62 print_hex (u / 16);
63 print_char (digit[u % 16]);
64 }
65
66 static void
67 print_string (const char *s)
68 {
69 for (; (*s) != '\0'; s++)
70 print_char ((*s));
71 }
72
73 static void
74 print_address (const void *a)
75 {
76 print_string ("0x");
77 print_hex ((unsigned long) a);
78 }
79
80 static void
81 print_byte_count (unsigned long long u)
82 {
83 print_unsigned (u);
84 print_string (" (");
85 print_string ("0x");
86 print_hex (u);
87 print_string (") bytes");
88 }
89
90 /* Print the current values of RESOURCE. */
91
92 static void
93 print_rlimit (int resource)
94 {
95 struct rlimit rl;
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);
101 }
102
103 static void
104 maximize_rlimit (int resource, const char *prefix)
105 {
106 struct rlimit rl;
107 print_string (" ");
108 print_string (prefix);
109 print_string (": ");
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);
116 print_string ("\n");
117 }
118
119 /* Maintain a doublely linked list. */
120 struct list
121 {
122 struct list *next;
123 struct list *prev;
124 size_t size;
125 };
126
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
130 its value right.
131
132 To simplify the list append logic, start the heap out with one
133 entry (that lives in the BSS section). */
134
135 static struct list dummy;
136 static struct list heap = { &dummy, &dummy };
137
138 static unsigned long bytes_allocated;
139
140 #ifdef O_LARGEFILE
141 #define large_off_t off64_t
142 #define large_lseek lseek64
143 #else
144 #define large_off_t off_t
145 #define O_LARGEFILE 0
146 #define large_lseek lseek
147 #endif
148
149 int
150 main ()
151 {
152 size_t max_chunk_size;
153 large_off_t max_core_size;
154
155 /* Try to expand all the resource limits beyond the point of sanity
156 - we're after the biggest possible core file. */
157
158 print_string ("Maximize resource limits ...\n");
159 #ifdef RLIMIT_CORE
160 maximize_rlimit (RLIMIT_CORE, "core");
161 #endif
162 #ifdef RLIMIT_DATA
163 maximize_rlimit (RLIMIT_DATA, "data");
164 #endif
165 #ifdef RLIMIT_STACK
166 maximize_rlimit (RLIMIT_STACK, "stack");
167 #endif
168 #ifdef RLIMIT_AS
169 maximize_rlimit (RLIMIT_AS, "stack");
170 #endif
171
172 print_string ("Maximize allocation limits ...\n");
173
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? */
177 {
178 int fd;
179 large_off_t tmp;
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)
183 {
184 if (large_lseek (fd, tmp, SEEK_SET) > 0)
185 max_core_size = tmp;
186 }
187 close (fd);
188 }
189
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. */
194 {
195 size_t tmp;
196 for (tmp = 1; tmp > 0 && tmp < max_core_size; tmp <<= 1)
197 max_chunk_size = tmp;
198 }
199
200 print_string (" core: ");
201 print_byte_count (max_core_size);
202 print_string ("\n");
203 print_string (" chunk: ");
204 print_byte_count (max_chunk_size);
205 print_string ("\n");
206 print_string (" large? ");
207 if (O_LARGEFILE)
208 print_string ("yes\n");
209 else
210 print_string ("no\n");
211
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. */
218
219 print_string ("Alocating the entire heap ...\n");
220 {
221 size_t chunk_size;
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
226 been allocated. */
227 for (chunk_size = max_chunk_size;
228 chunk_size >= sizeof (struct list);
229 chunk_size >>= 1)
230 {
231 unsigned long count = 0;
232 print_string (" ");
233 print_byte_count (chunk_size);
234 print_string (" ... ");
235 while (bytes_allocated + (1 + count) * chunk_size
236 < max_core_size)
237 {
238 struct list *chunk = malloc (chunk_size);
239 if (chunk == NULL)
240 break;
241 chunk->size = chunk_size;
242 /* Link it in. */
243 chunk->next = NULL;
244 chunk->prev = heap.prev;
245 heap.prev->next = chunk;
246 heap.prev = chunk;
247 count++;
248 }
249 print_unsigned (count);
250 print_string (" chunks\n");
251 chunks_allocated += count;
252 bytes_allocated += chunk_size * count;
253 }
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");
259 }
260
261 /* Push everything out to disk. */
262
263 print_string ("Dump core ....\n");
264 *(char*)0 = 0;
265 }
This page took 0.057633 seconds and 4 git commands to generate.