1 /* Generate a core file for the inferior process.
3 Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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. */
30 #include "cli/cli-decode.h"
32 #include "gdb_assert.h"
34 static char *default_gcore_target (void);
35 static enum bfd_architecture default_gcore_arch (void);
36 static unsigned long default_gcore_mach (void);
37 static int gcore_memory_sections (bfd *);
39 /* Generate a core file from the inferior process. */
42 gcore_command (char *args, int from_tty)
44 struct cleanup *old_chain;
45 char *corefilename, corefilename_buffer[40];
46 asection *note_sec = NULL;
48 void *note_data = NULL;
51 /* No use generating a corefile without a target process. */
52 if (!target_has_execution)
59 /* Default corefile name is "core.PID". */
60 sprintf (corefilename_buffer, "core.%d", PIDGET (inferior_ptid));
61 corefilename = corefilename_buffer;
65 fprintf_filtered (gdb_stdout,
66 "Opening corefile '%s' for output.\n", corefilename);
68 /* Open the output file. */
69 obfd = bfd_openw (corefilename, default_gcore_target ());
71 error ("Failed to open '%s' for output.", corefilename);
73 /* Need a cleanup that will close the file (FIXME: delete it?). */
74 old_chain = make_cleanup_bfd_close (obfd);
76 bfd_set_format (obfd, bfd_core);
77 bfd_set_arch_mach (obfd, default_gcore_arch (), default_gcore_mach ());
79 /* An external target method must build the notes section. */
80 note_data = target_make_corefile_notes (obfd, ¬e_size);
82 /* Create the note section. */
83 if (note_data != NULL && note_size != 0)
85 note_sec = bfd_make_section_anyway (obfd, "note0");
87 error ("Failed to create 'note' section for corefile: %s",
88 bfd_errmsg (bfd_get_error ()));
90 bfd_set_section_vma (obfd, note_sec, 0);
91 bfd_set_section_flags (obfd, note_sec,
92 SEC_HAS_CONTENTS | SEC_READONLY | SEC_ALLOC);
93 bfd_set_section_alignment (obfd, note_sec, 0);
94 bfd_set_section_size (obfd, note_sec, note_size);
97 /* Now create the memory/load sections. */
98 if (gcore_memory_sections (obfd) == 0)
99 error ("gcore: failed to get corefile memory sections from target.");
101 /* Write out the contents of the note section. */
102 if (note_data != NULL && note_size != 0)
104 if (!bfd_set_section_contents (obfd, note_sec, note_data, 0, note_size))
105 warning ("writing note section (%s)", bfd_errmsg (bfd_get_error ()));
109 fprintf_filtered (gdb_stdout, "Saved corefile %s\n", corefilename);
111 /* Clean-ups will close the output file and free malloc memory. */
112 do_cleanups (old_chain);
117 default_gcore_mach (void)
119 #if 1 /* See if this even matters... */
122 #ifdef TARGET_ARCHITECTURE
123 const struct bfd_arch_info *bfdarch = TARGET_ARCHITECTURE;
126 return bfdarch->mach;
127 #endif /* TARGET_ARCHITECTURE */
128 if (exec_bfd == NULL)
129 error ("Can't find default bfd machine type (need execfile).");
131 return bfd_get_mach (exec_bfd);
135 static enum bfd_architecture
136 default_gcore_arch (void)
138 #ifdef TARGET_ARCHITECTURE
139 const struct bfd_arch_info * bfdarch = TARGET_ARCHITECTURE;
142 return bfdarch->arch;
144 if (exec_bfd == NULL)
145 error ("Can't find bfd architecture for corefile (need execfile).");
147 return bfd_get_arch (exec_bfd);
151 default_gcore_target (void)
153 /* FIXME: This may only work for ELF targets. */
154 if (exec_bfd == NULL)
157 return bfd_get_target (exec_bfd);
160 /* Derive a reasonable stack segment by unwinding the target stack,
161 and store its limits in *BOTTOM and *TOP. Return non-zero if
165 derive_stack_segment (bfd_vma *bottom, bfd_vma *top)
167 struct frame_info *fi, *tmp_fi;
172 /* Can't succeed without stack and registers. */
173 if (!target_has_stack || !target_has_registers)
176 /* Can't succeed without current frame. */
177 fi = get_current_frame ();
181 /* Save frame pointer of TOS frame. */
182 *top = get_frame_base (fi);
183 /* If current stack pointer is more "inner", use that instead. */
184 if (INNER_THAN (read_sp (), *top))
187 /* Find prev-most frame. */
188 while ((tmp_fi = get_prev_frame (fi)) != NULL)
191 /* Save frame pointer of prev-most frame. */
192 *bottom = get_frame_base (fi);
194 /* Now canonicalize their order, so that BOTTOM is a lower address
195 (as opposed to a lower stack frame). */
208 /* Derive a reasonable heap segment for ABFD by looking at sbrk and
209 the static data sections. Store its limits in *BOTTOM and *TOP.
210 Return non-zero if successful. */
213 derive_heap_segment (bfd *abfd, bfd_vma *bottom, bfd_vma *top)
215 bfd_vma top_of_data_memory = 0;
216 bfd_vma top_of_heap = 0;
217 bfd_size_type sec_size;
218 struct value *zero, *sbrk;
225 /* This function depends on being able to call a function in the
227 if (!target_has_execution)
230 /* The following code assumes that the link map is arranged as
231 follows (low to high addresses):
233 ---------------------------------
235 ---------------------------------
236 | data sections (including bss) |
237 ---------------------------------
239 --------------------------------- */
241 for (sec = abfd->sections; sec; sec = sec->next)
243 if (bfd_get_section_flags (abfd, sec) & SEC_DATA
244 || strcmp (".bss", bfd_section_name (abfd, sec)) == 0)
246 sec_vaddr = bfd_get_section_vma (abfd, sec);
247 sec_size = bfd_get_section_size_before_reloc (sec);
248 if (sec_vaddr + sec_size > top_of_data_memory)
249 top_of_data_memory = sec_vaddr + sec_size;
253 /* Now get the top-of-heap by calling sbrk in the inferior. */
254 if (lookup_minimal_symbol ("sbrk", NULL, NULL) != NULL)
256 sbrk = find_function_in_inferior ("sbrk");
260 else if (lookup_minimal_symbol ("_sbrk", NULL, NULL) != NULL)
262 sbrk = find_function_in_inferior ("_sbrk");
269 zero = value_from_longest (builtin_type_int, 0);
271 sbrk = call_function_by_hand (sbrk, 1, &zero);
274 top_of_heap = value_as_long (sbrk);
276 /* Return results. */
277 if (top_of_heap > top_of_data_memory)
279 *bottom = top_of_data_memory;
284 /* No additional heap space needs to be saved. */
289 make_output_phdrs (bfd *obfd, asection *osec, void *ignored)
294 /* FIXME: these constants may only be applicable for ELF. */
295 if (strncmp (bfd_section_name (obfd, osec), "load", 4) == 0)
300 p_flags |= PF_R; /* Segment is readable. */
301 if (!(bfd_get_section_flags (obfd, osec) & SEC_READONLY))
302 p_flags |= PF_W; /* Segment is writable. */
303 if (bfd_get_section_flags (obfd, osec) & SEC_CODE)
304 p_flags |= PF_X; /* Segment is executable. */
306 bfd_record_phdr (obfd, p_type, 1, p_flags, 0, 0, 0, 0, 1, &osec);
310 make_mem_sec (bfd *obfd, bfd_vma addr, bfd_size_type size,
311 unsigned int flags, unsigned int alignment)
315 osec = bfd_make_section_anyway (obfd, "load");
318 warning ("Couldn't make gcore segment: %s",
319 bfd_errmsg (bfd_get_error ()));
325 fprintf_filtered (gdb_stdout, "Save segment, %lld bytes at 0x%s\n",
326 (long long) size, paddr_nz (addr));
329 bfd_set_section_size (obfd, osec, size);
330 bfd_set_section_vma (obfd, osec, addr);
331 bfd_section_lma (obfd, osec) = 0; /* ??? bfd_set_section_lma? */
332 bfd_set_section_alignment (obfd, osec, alignment);
333 bfd_set_section_flags (obfd, osec,
334 flags | SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS);
339 gcore_create_callback (CORE_ADDR vaddr, unsigned long size,
340 int read, int write, int exec, void *data)
346 flags |= SEC_READONLY;
347 /* Mark readonly sections as zero-sized, such that we can avoid
348 copying their contents. */
357 return ((make_mem_sec (data, vaddr, size, flags, 0)) == NULL);
361 objfile_find_memory_regions (int (*func) (CORE_ADDR, unsigned long,
362 int, int, int, void *),
365 /* Use objfile data to create memory sections. */
366 struct objfile *objfile;
367 struct obj_section *objsec;
368 bfd_vma temp_bottom, temp_top;
370 /* Call callback function for each objfile section. */
371 ALL_OBJSECTIONS (objfile, objsec)
373 bfd *ibfd = objfile->obfd;
374 asection *isec = objsec->the_bfd_section;
375 flagword flags = bfd_get_section_flags (ibfd, isec);
378 if ((flags & SEC_ALLOC) || (flags & SEC_LOAD))
380 int size = bfd_section_size (ibfd, isec);
383 ret = (*func) (objsec->addr, bfd_section_size (ibfd, isec),
384 1, /* All sections will be readable. */
385 (flags & SEC_READONLY) == 0, /* Writable. */
386 (flags & SEC_CODE) != 0, /* Executable. */
393 /* Make a stack segment. */
394 if (derive_stack_segment (&temp_bottom, &temp_top))
395 (*func) (temp_bottom, temp_top - temp_bottom,
396 1, /* Stack section will be readable. */
397 1, /* Stack section will be writable. */
398 0, /* Stack section will not be executable. */
401 /* Make a heap segment. */
402 if (derive_heap_segment (exec_bfd, &temp_bottom, &temp_top))
403 (*func) (temp_bottom, temp_top - temp_bottom,
404 1, /* Heap section will be readable. */
405 1, /* Heap section will be writable. */
406 0, /* Heap section will not be executable. */
413 gcore_copy_callback (bfd *obfd, asection *osec, void *ignored)
415 bfd_size_type size = bfd_section_size (obfd, osec);
416 struct cleanup *old_chain = NULL;
419 /* Read-only sections are marked as zero-size. We don't have to
420 copy their contents. */
424 /* Only interested in "load" sections. */
425 if (strncmp ("load", bfd_section_name (obfd, osec), 4) != 0)
428 memhunk = xmalloc (size);
429 /* ??? This is crap since xmalloc should never return NULL. */
431 error ("Not enough memory to create corefile.");
432 old_chain = make_cleanup (xfree, memhunk);
434 if (target_read_memory (bfd_section_vma (obfd, osec),
436 warning ("Memory read failed for corefile section, %ld bytes at 0x%s\n",
437 (long) size, paddr (bfd_section_vma (obfd, osec)));
438 if (!bfd_set_section_contents (obfd, osec, memhunk, 0, size))
439 warning ("Failed to write corefile contents (%s).",
440 bfd_errmsg (bfd_get_error ()));
442 do_cleanups (old_chain); /* Frees MEMHUNK. */
446 gcore_memory_sections (bfd *obfd)
448 if (target_find_memory_regions (gcore_create_callback, obfd) != 0)
449 return 0; /* FIXME: error return/msg? */
451 /* Record phdrs for section-to-segment mapping. */
452 bfd_map_over_sections (obfd, make_output_phdrs, NULL);
454 /* Copy memory region contents. */
455 bfd_map_over_sections (obfd, gcore_copy_callback, NULL);
461 _initialize_gcore (void)
463 add_com ("generate-core-file", class_files, gcore_command,
465 Save a core file with the current state of the debugged process.\n\
466 Argument is optional filename. Default filename is 'core.<process_id>'.");
468 add_com_alias ("gcore", "generate-core-file", class_files, 1);
469 exec_set_find_memory_regions (objfile_find_memory_regions);