2003-01-13 Andrew Cagney <ac131313@redhat.com>
[deliverable/binutils-gdb.git] / gdb / gcore.c
1 /* Generate a core file for the inferior process.
2
3 Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "cli/cli-decode.h"
24 #include "inferior.h"
25 #include "gdbcore.h"
26 #include "elf-bfd.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29
30 static char *default_gcore_target (void);
31 static enum bfd_architecture default_gcore_arch (void);
32 static unsigned long default_gcore_mach (void);
33 static int gcore_memory_sections (bfd *);
34
35 /* Function: gcore_command
36 Generate a core file from the inferior process. */
37
38 static void
39 gcore_command (char *args, int from_tty)
40 {
41 struct cleanup *old_chain;
42 char *corefilename, corefilename_buffer[40];
43 asection *note_sec = NULL;
44 bfd *obfd;
45 void *note_data = NULL;
46 int note_size = 0;
47
48 /* No use generating a corefile without a target process. */
49 if (!(target_has_execution))
50 noprocess ();
51
52 if (args && *args)
53 corefilename = args;
54 else
55 {
56 /* Default corefile name is "core.PID". */
57 sprintf (corefilename_buffer, "core.%d", PIDGET (inferior_ptid));
58 corefilename = corefilename_buffer;
59 }
60
61 if (info_verbose)
62 fprintf_filtered (gdb_stdout,
63 "Opening corefile '%s' for output.\n", corefilename);
64
65 /* Open the output file. */
66 if (!(obfd = bfd_openw (corefilename, default_gcore_target ())))
67 {
68 error ("Failed to open '%s' for output.", corefilename);
69 }
70
71 /* Need a cleanup that will close the file (FIXME: delete it?). */
72 old_chain = make_cleanup_bfd_close (obfd);
73
74 bfd_set_format (obfd, bfd_core);
75 bfd_set_arch_mach (obfd, default_gcore_arch (), default_gcore_mach ());
76
77 /* An external target method must build the notes section. */
78 note_data = (char *) target_make_corefile_notes (obfd, &note_size);
79
80 /* Create the note section. */
81 if (note_data != NULL && note_size != 0)
82 {
83 if ((note_sec = bfd_make_section_anyway (obfd, "note0")) == NULL)
84 error ("Failed to create 'note' section for corefile: %s",
85 bfd_errmsg (bfd_get_error ()));
86
87 bfd_set_section_vma (obfd, note_sec, 0);
88 bfd_set_section_flags (obfd, note_sec,
89 SEC_HAS_CONTENTS | SEC_READONLY | SEC_ALLOC);
90 bfd_set_section_alignment (obfd, note_sec, 0);
91 bfd_set_section_size (obfd, note_sec, note_size);
92 }
93
94 /* Now create the memory/load sections. */
95 if (gcore_memory_sections (obfd) == 0)
96 error ("gcore: failed to get corefile memory sections from target.");
97
98 /* Write out the contents of the note section. */
99 if (note_data != NULL && note_size != 0)
100 {
101 if (!bfd_set_section_contents (obfd, note_sec, note_data, 0, note_size))
102 {
103 warning ("writing note section (%s)",
104 bfd_errmsg (bfd_get_error ()));
105 }
106 }
107
108 /* Succeeded. */
109 fprintf_filtered (gdb_stdout,
110 "Saved corefile %s\n", corefilename);
111
112 /* Clean-ups will close the output file and free malloc memory. */
113 do_cleanups (old_chain);
114 return;
115 }
116
117 static unsigned long
118 default_gcore_mach (void)
119 {
120 #if 1 /* See if this even matters... */
121 return 0;
122 #else
123 #ifdef TARGET_ARCHITECTURE
124 const struct bfd_arch_info * bfdarch = TARGET_ARCHITECTURE;
125
126 if (bfdarch != NULL)
127 return bfdarch->mach;
128 #endif /* TARGET_ARCHITECTURE */
129 if (exec_bfd == NULL)
130 error ("Can't find default bfd machine type (need execfile).");
131
132 return bfd_get_mach (exec_bfd);
133 #endif /* 1 */
134 }
135
136 static enum bfd_architecture
137 default_gcore_arch (void)
138 {
139 #ifdef TARGET_ARCHITECTURE
140 const struct bfd_arch_info * bfdarch = TARGET_ARCHITECTURE;
141
142 if (bfdarch != NULL)
143 return bfdarch->arch;
144 #endif
145 if (exec_bfd == NULL)
146 error ("Can't find bfd architecture for corefile (need execfile).");
147
148 return bfd_get_arch (exec_bfd);
149 }
150
151 static char *
152 default_gcore_target (void)
153 {
154 /* FIXME -- this may only work for ELF targets. */
155 if (exec_bfd == NULL)
156 return NULL;
157 else
158 return bfd_get_target (exec_bfd);
159 }
160
161 /* Function: derive_stack_segment
162
163 Derive a reasonable stack segment by unwinding the target stack.
164
165 Returns 0 for failure, 1 for success. */
166
167 static int
168 derive_stack_segment (bfd_vma *bottom, bfd_vma *top)
169 {
170 bfd_vma tmp_vma;
171 struct frame_info *fi, *tmp_fi;
172
173 if (bottom == NULL || top == NULL)
174 return 0; /* Paranoia. */
175
176 if (!target_has_stack || !target_has_registers)
177 return 0; /* Can't succeed without stack and registers. */
178
179 if ((fi = get_current_frame ()) == NULL)
180 return 0; /* Can't succeed without current frame. */
181
182 /* Save frame pointer of TOS frame. */
183 *top = get_frame_base (fi);
184 /* If current stack pointer is more "inner", use that instead. */
185 if (INNER_THAN (read_sp (), *top))
186 *top = read_sp ();
187
188 /* Find prev-most frame. */
189 while ((tmp_fi = get_prev_frame (fi)) != NULL)
190 fi = tmp_fi;
191
192 /* Save frame pointer of prev-most frame. */
193 *bottom = get_frame_base (fi);
194
195 /* Now canonicalize their order, so that 'bottom' is a lower address
196 (as opposed to a lower stack frame). */
197 if (*bottom > *top)
198 {
199 tmp_vma = *top;
200 *top = *bottom;
201 *bottom = tmp_vma;
202 }
203
204 return 1; /* success */
205 }
206
207 /* Function: derive_heap_segment
208
209 Derive a reasonable heap segment by looking at sbrk and
210 the static data sections.
211
212 Returns 0 for failure, 1 for success. */
213
214 static int
215 derive_heap_segment (bfd *abfd, bfd_vma *bottom, bfd_vma *top)
216 {
217 bfd_vma top_of_data_memory = 0;
218 bfd_vma top_of_heap = 0;
219 bfd_size_type sec_size;
220 struct value *zero, *sbrk;
221 bfd_vma sec_vaddr;
222 asection *sec;
223
224 if (bottom == NULL || top == NULL)
225 return 0; /* Paranoia. */
226
227 if (!target_has_execution)
228 return 0; /* This function depends on being able
229 to call a function in the inferior. */
230
231 /* Assumption: link map is arranged as follows (low to high addresses):
232 text sections
233 data sections (including bss)
234 heap
235 */
236
237 for (sec = abfd->sections; sec; sec = sec->next)
238 {
239 if (bfd_get_section_flags (abfd, sec) & SEC_DATA ||
240 strcmp (".bss", bfd_section_name (abfd, sec)) == 0)
241 {
242 sec_vaddr = bfd_get_section_vma (abfd, sec);
243 sec_size = bfd_get_section_size_before_reloc (sec);
244 if (sec_vaddr + sec_size > top_of_data_memory)
245 top_of_data_memory = sec_vaddr + sec_size;
246 }
247 }
248 /* Now get the top-of-heap by calling sbrk in the inferior. */
249 if (lookup_minimal_symbol ("sbrk", NULL, NULL) != NULL)
250 {
251 if ((sbrk = find_function_in_inferior ("sbrk")) == NULL)
252 return 0;
253 }
254 else if (lookup_minimal_symbol ("_sbrk", NULL, NULL) != NULL)
255 {
256 if ((sbrk = find_function_in_inferior ("_sbrk")) == NULL)
257 return 0;
258 }
259 else
260 return 0;
261
262 if ((zero = value_from_longest (builtin_type_int, (LONGEST) 0)) == NULL)
263 return 0;
264 if ((sbrk = call_function_by_hand (sbrk, 1, &zero)) == NULL)
265 return 0;
266 top_of_heap = value_as_long (sbrk);
267
268 /* Return results. */
269 if (top_of_heap > top_of_data_memory)
270 {
271 *bottom = top_of_data_memory;
272 *top = top_of_heap;
273 return 1; /* success */
274 }
275 else
276 return 0; /* No additional heap space needs to be saved. */
277 }
278
279 /* ARGSUSED */
280 static void
281 make_output_phdrs (bfd *obfd, asection *osec, void *ignored)
282 {
283 int p_flags = 0;
284 int p_type;
285
286 /* FIXME: these constants may only be applicable for ELF. */
287 if (strncmp (bfd_section_name (obfd, osec), "load", 4) == 0)
288 p_type = PT_LOAD;
289 else
290 p_type = PT_NOTE;
291
292 p_flags |= PF_R; /* Segment is readable. */
293 if (!(bfd_get_section_flags (obfd, osec) & SEC_READONLY))
294 p_flags |= PF_W; /* Segment is writable. */
295 if (bfd_get_section_flags (obfd, osec) & SEC_CODE)
296 p_flags |= PF_X; /* Segment is executable. */
297
298 bfd_record_phdr (obfd, p_type, 1, p_flags, 0, 0,
299 0, 0, 1, &osec);
300 }
301
302 static asection *
303 make_mem_sec (bfd *obfd,
304 bfd_vma addr,
305 bfd_size_type size,
306 unsigned int flags,
307 unsigned int alignment)
308 {
309 asection *osec;
310
311 if ((osec = bfd_make_section_anyway (obfd, "load")) == NULL)
312 {
313 warning ("Couldn't make gcore segment: %s",
314 bfd_errmsg (bfd_get_error ()));
315 return NULL;
316 }
317
318 if (info_verbose)
319 {
320 fprintf_filtered (gdb_stdout,
321 "Save segment, %lld bytes at 0x%s\n",
322 (long long) size, paddr_nz (addr));
323 }
324
325 bfd_set_section_size (obfd, osec, size);
326 bfd_set_section_vma (obfd, osec, addr);
327 osec->lma = 0; /* FIXME: there should be a macro for this! */
328 bfd_set_section_alignment (obfd, osec, alignment);
329 bfd_set_section_flags (obfd, osec,
330 flags | SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS);
331 return osec;
332 }
333
334 static int
335 gcore_create_callback (CORE_ADDR vaddr,
336 unsigned long size,
337 int read, int write, int exec,
338 void *data)
339 {
340 flagword flags = 0;
341
342 if (write == 0)
343 {
344 flags |= SEC_READONLY;
345 /* Set size == zero for readonly sections. */
346 size = 0;
347 }
348 if (exec)
349 {
350 flags |= SEC_CODE;
351 }
352 else
353 {
354 flags |= SEC_DATA;
355 }
356
357 return ((make_mem_sec ((bfd *) data, vaddr, size, flags, 0)) == NULL);
358 }
359
360 static int
361 objfile_find_memory_regions (int (*func) (CORE_ADDR,
362 unsigned long,
363 int, int, int,
364 void *),
365 void *obfd)
366 {
367 /* Use objfile data to create memory sections. */
368 struct objfile *objfile;
369 struct obj_section *objsec;
370 bfd_vma temp_bottom, temp_top;
371
372 /* Call callback function for each objfile section. */
373 ALL_OBJSECTIONS (objfile, objsec)
374 {
375 bfd *ibfd = objfile->obfd;
376 asection *isec = objsec->the_bfd_section;
377 flagword flags = bfd_get_section_flags (ibfd, isec);
378 int ret;
379
380 if ((flags & SEC_ALLOC) || (flags & SEC_LOAD))
381 {
382 int size = bfd_section_size (ibfd, isec);
383 int ret;
384
385 if ((ret = (*func) (objsec->addr,
386 bfd_section_size (ibfd, isec),
387 1, /* All sections will be readable. */
388 (flags & SEC_READONLY) == 0, /* writable */
389 (flags & SEC_CODE) != 0, /* executable */
390 obfd)) != 0)
391 return ret;
392 }
393 }
394
395 /* Make a stack segment. */
396 if (derive_stack_segment (&temp_bottom, &temp_top))
397 (*func) (temp_bottom,
398 temp_top - temp_bottom,
399 1, /* Stack section will be readable */
400 1, /* Stack section will be writable */
401 0, /* Stack section will not be executable */
402 obfd);
403
404 /* Make a heap segment. */
405 if (derive_heap_segment (exec_bfd, &temp_bottom, &temp_top))
406 (*func) (temp_bottom,
407 temp_top - temp_bottom,
408 1, /* Heap section will be readable */
409 1, /* Heap section will be writable */
410 0, /* Heap section will not be executable */
411 obfd);
412 return 0;
413 }
414
415 static void
416 gcore_copy_callback (bfd *obfd, asection *osec, void *ignored)
417 {
418 bfd_size_type size = bfd_section_size (obfd, osec);
419 struct cleanup *old_chain = NULL;
420 void *memhunk;
421
422 if (size == 0)
423 return; /* Read-only sections are marked as zero-size.
424 We don't have to copy their contents. */
425 if (strncmp ("load", bfd_section_name (obfd, osec), 4) != 0)
426 return; /* Only interested in "load" sections. */
427
428 if ((memhunk = xmalloc (size)) == NULL)
429 error ("Not enough memory to create corefile.");
430 old_chain = make_cleanup (xfree, memhunk);
431
432 if (target_read_memory (bfd_section_vma (obfd, osec),
433 memhunk, size) != 0)
434 warning ("Memory read failed for corefile section, %ld bytes at 0x%s\n",
435 (long) size, paddr (bfd_section_vma (obfd, osec)));
436 if (!bfd_set_section_contents (obfd, osec, memhunk, 0, size))
437 warning ("Failed to write corefile contents (%s).",
438 bfd_errmsg (bfd_get_error ()));
439
440 do_cleanups (old_chain); /* frees the xmalloc buffer */
441 }
442
443 static int
444 gcore_memory_sections (bfd *obfd)
445 {
446 if (target_find_memory_regions (gcore_create_callback, obfd) != 0)
447 return 0; /* FIXME error return/msg? */
448
449 /* Record phdrs for section-to-segment mapping. */
450 bfd_map_over_sections (obfd, make_output_phdrs, NULL);
451
452 /* Copy memory region contents. */
453 bfd_map_over_sections (obfd, gcore_copy_callback, NULL);
454
455 return 1; /* success */
456 }
457
458 void
459 _initialize_gcore (void)
460 {
461 add_com ("generate-core-file", class_files, gcore_command,
462 "Save a core file with the current state of the debugged process.\n\
463 Argument is optional filename. Default filename is 'core.<process_id>'.");
464
465 add_com_alias ("gcore", "generate-core-file", class_files, 1);
466 exec_set_find_memory_regions (objfile_find_memory_regions);
467 }
This page took 0.038797 seconds and 5 git commands to generate.