1 /* Generate a core file for the inferior process.
2 Copyright 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include "cli/cli-decode.h"
29 static char *default_gcore_target (void);
30 static enum bfd_architecture default_gcore_arch (void);
31 static unsigned long default_gcore_mach (void);
32 static int gcore_memory_sections (bfd *);
34 /* Function: gcore_command
35 Generate a core file from the inferior process. */
38 gcore_command (char *args, int from_tty)
40 struct cleanup *old_chain;
41 char *corefilename, corefilename_buffer[40];
42 asection *note_sec = NULL;
44 void *note_data = NULL;
47 /* No use generating a corefile without a target process. */
48 if (!(target_has_execution))
55 /* Default corefile name is "core.PID". */
56 sprintf (corefilename_buffer, "core.%d", PIDGET (inferior_ptid));
57 corefilename = corefilename_buffer;
61 fprintf_filtered (gdb_stdout,
62 "Opening corefile '%s' for output.\n", corefilename);
64 /* Open the output file. */
65 if (!(obfd = bfd_openw (corefilename, default_gcore_target ())))
67 error ("Failed to open '%s' for output.", corefilename);
70 /* Need a cleanup that will close the file (FIXME: delete it?). */
71 old_chain = make_cleanup_bfd_close (obfd);
73 bfd_set_format (obfd, bfd_core);
74 bfd_set_arch_mach (obfd, default_gcore_arch (), default_gcore_mach ());
76 /* An external target method must build the notes section. */
77 note_data = (char *) target_make_corefile_notes (obfd, ¬e_size);
79 /* Create the note section. */
80 if (note_data != NULL && note_size != 0)
82 if ((note_sec = bfd_make_section_anyway (obfd, "note0")) == NULL)
83 error ("Failed to create 'note' section for corefile: %s",
84 bfd_errmsg (bfd_get_error ()));
86 bfd_set_section_vma (obfd, note_sec, 0);
87 bfd_set_section_flags (obfd, note_sec,
88 SEC_HAS_CONTENTS | SEC_READONLY | SEC_ALLOC);
89 bfd_set_section_alignment (obfd, note_sec, 0);
90 bfd_set_section_size (obfd, note_sec, note_size);
93 /* Now create the memory/load sections. */
94 if (gcore_memory_sections (obfd) == 0)
95 error ("gcore: failed to get corefile memory sections from target.");
97 /* Write out the contents of the note section. */
98 if (note_data != NULL && note_size != 0)
100 if (!bfd_set_section_contents (obfd, note_sec, note_data, 0, note_size))
102 warning ("writing note section (%s)",
103 bfd_errmsg (bfd_get_error ()));
108 fprintf_filtered (gdb_stdout,
109 "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);
161 * Default method for stack segment (preemptable by target).
164 static int (*override_derive_stack_segment) (bfd_vma *, bfd_vma *);
167 preempt_derive_stack_segment (int (*override_func) (bfd_vma *, bfd_vma *))
169 override_derive_stack_segment = override_func;
172 /* Function: default_derive_stack_segment
173 Derive a reasonable stack segment by unwinding the target stack.
175 Returns 0 for failure, 1 for success. */
178 default_derive_stack_segment (bfd_vma *bottom, bfd_vma *top)
181 struct frame_info *fi, *tmp_fi;
183 if (bottom == NULL || top == NULL)
184 return 0; /* Paranoia. */
186 if (!target_has_stack || !target_has_registers)
187 return 0; /* Can't succeed without stack and registers. */
189 if ((fi = get_current_frame ()) == NULL)
190 return 0; /* Can't succeed without current frame. */
192 /* Save frame pointer of TOS frame. */
194 /* If current stack pointer is more "inner", use that instead. */
195 if (INNER_THAN (read_sp (), *top))
198 /* Find prev-most frame. */
199 while ((tmp_fi = get_prev_frame (fi)) != NULL)
202 /* Save frame pointer of prev-most frame. */
205 /* Now canonicalize their order, so that 'bottom' is a lower address
206 (as opposed to a lower stack frame). */
214 return 1; /* success */
218 derive_stack_segment (bfd_vma *bottom, bfd_vma *top)
220 if (override_derive_stack_segment)
221 return override_derive_stack_segment (bottom, top);
223 return default_derive_stack_segment (bottom, top);
227 * Default method for heap segment (preemptable by target).
230 static int (*override_derive_heap_segment) (bfd *, bfd_vma *, bfd_vma *);
233 preempt_derive_heap_segment (int (*override_func) (bfd *,
234 bfd_vma *, bfd_vma *))
236 override_derive_heap_segment = override_func;
239 /* Function: default_derive_heap_segment
240 Derive a reasonable heap segment by looking at sbrk and
241 the static data sections.
243 Returns 0 for failure, 1 for success. */
246 default_derive_heap_segment (bfd *abfd, bfd_vma *bottom, bfd_vma *top)
248 bfd_vma top_of_data_memory = 0;
249 bfd_vma top_of_heap = 0;
250 bfd_size_type sec_size;
251 struct value *zero, *sbrk;
255 if (bottom == NULL || top == NULL)
256 return 0; /* Paranoia. */
258 if (!target_has_execution)
259 return 0; /* This function depends on being able
260 to call a function in the inferior. */
262 /* Assumption: link map is arranged as follows (low to high addresses):
264 data sections (including bss)
268 for (sec = abfd->sections; sec; sec = sec->next)
270 if (bfd_get_section_flags (abfd, sec) & SEC_DATA ||
271 strcmp (".bss", bfd_section_name (abfd, sec)) == 0)
273 sec_vaddr = bfd_get_section_vma (abfd, sec);
274 sec_size = bfd_get_section_size_before_reloc (sec);
275 if (sec_vaddr + sec_size > top_of_data_memory)
276 top_of_data_memory = sec_vaddr + sec_size;
279 /* Now get the top-of-heap by calling sbrk in the inferior. */
280 if (lookup_minimal_symbol ("sbrk", NULL, NULL) != NULL)
282 if ((sbrk = find_function_in_inferior ("sbrk")) == NULL)
285 else if (lookup_minimal_symbol ("_sbrk", NULL, NULL) != NULL)
287 if ((sbrk = find_function_in_inferior ("_sbrk")) == NULL)
293 if ((zero = value_from_longest (builtin_type_int, (LONGEST) 0)) == NULL)
295 if ((sbrk = call_function_by_hand (sbrk, 1, &zero)) == NULL)
297 top_of_heap = value_as_long (sbrk);
299 /* Return results. */
300 if (top_of_heap > top_of_data_memory)
302 *bottom = top_of_data_memory;
304 return 1; /* success */
307 return 0; /* No additional heap space needs to be saved. */
311 derive_heap_segment (bfd *abfd, bfd_vma *bottom, bfd_vma *top)
313 if (override_derive_heap_segment)
314 return override_derive_heap_segment (abfd, bottom, top);
316 return default_derive_heap_segment (abfd, bottom, top);
321 make_output_phdrs (bfd *obfd, asection *osec, void *ignored)
326 /* FIXME: these constants may only be applicable for ELF. */
327 if (strncmp (bfd_section_name (obfd, osec), "load", 4) == 0)
332 p_flags |= PF_R; /* Segment is readable. */
333 if (!(bfd_get_section_flags (obfd, osec) & SEC_READONLY))
334 p_flags |= PF_W; /* Segment is writable. */
335 if (bfd_get_section_flags (obfd, osec) & SEC_CODE)
336 p_flags |= PF_X; /* Segment is executable. */
338 bfd_record_phdr (obfd, p_type, 1, p_flags, 0, 0,
343 make_mem_sec (bfd *obfd,
347 unsigned int alignment)
351 if ((osec = bfd_make_section_anyway (obfd, "load")) == NULL)
353 warning ("Couldn't make gcore segment: %s",
354 bfd_errmsg (bfd_get_error ()));
360 fprintf_filtered (gdb_stdout,
361 "Save segment, %lld bytes at 0x%s\n",
362 (long long) size, paddr_nz (addr));
365 bfd_set_section_size (obfd, osec, size);
366 bfd_set_section_vma (obfd, osec, addr);
367 osec->lma = 0; /* FIXME: there should be a macro for this! */
368 bfd_set_section_alignment (obfd, osec, alignment);
369 bfd_set_section_flags (obfd, osec,
370 flags | SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS);
375 gcore_create_callback (CORE_ADDR vaddr,
377 int read, int write, int exec,
384 flags |= SEC_READONLY;
385 /* Set size == zero for readonly sections. */
397 return ((make_mem_sec ((bfd *) data, vaddr, size, flags, 0)) == NULL);
401 objfile_find_memory_regions (int (*func) (CORE_ADDR,
407 /* Use objfile data to create memory sections. */
408 struct objfile *objfile;
409 struct obj_section *objsec;
410 bfd_vma temp_bottom, temp_top;
412 /* Call callback function for each objfile section. */
413 ALL_OBJSECTIONS (objfile, objsec)
415 bfd *ibfd = objfile->obfd;
416 asection *isec = objsec->the_bfd_section;
417 flagword flags = bfd_get_section_flags (ibfd, isec);
420 if ((flags & SEC_ALLOC) || (flags & SEC_LOAD))
422 int size = bfd_section_size (ibfd, isec);
425 if ((ret = (*func) (objsec->addr,
426 bfd_section_size (ibfd, isec),
427 1, /* All sections will be readable. */
428 (flags & SEC_READONLY) == 0, /* writable */
429 (flags & SEC_CODE) != 0, /* executable */
435 /* Make a stack segment. */
436 if (derive_stack_segment (&temp_bottom, &temp_top))
437 (*func) (temp_bottom,
438 temp_top - temp_bottom,
439 1, /* Stack section will be readable */
440 1, /* Stack section will be writable */
441 0, /* Stack section will not be executable */
444 /* Make a heap segment. */
445 if (derive_heap_segment (exec_bfd, &temp_bottom, &temp_top))
446 (*func) (temp_bottom,
447 temp_top - temp_bottom,
448 1, /* Heap section will be readable */
449 1, /* Heap section will be writable */
450 0, /* Heap section will not be executable */
456 gcore_copy_callback (bfd *obfd, asection *osec, void *ignored)
458 bfd_size_type size = bfd_section_size (obfd, osec);
459 struct cleanup *old_chain = NULL;
463 return; /* Read-only sections are marked as zero-size.
464 We don't have to copy their contents. */
465 if (strncmp ("load", bfd_section_name (obfd, osec), 4) != 0)
466 return; /* Only interested in "load" sections. */
468 if ((memhunk = xmalloc (size)) == NULL)
469 error ("Not enough memory to create corefile.");
470 old_chain = make_cleanup (xfree, memhunk);
472 if (target_read_memory (bfd_section_vma (obfd, osec),
474 warning ("Memory read failed for corefile section, %ld bytes at 0x%s\n",
475 (long) size, paddr (bfd_section_vma (obfd, osec)));
476 if (!bfd_set_section_contents (obfd, osec, memhunk, 0, size))
477 warning ("Failed to write corefile contents (%s).",
478 bfd_errmsg (bfd_get_error ()));
480 do_cleanups (old_chain); /* frees the xmalloc buffer */
484 gcore_memory_sections (bfd *obfd)
486 if (target_find_memory_regions (gcore_create_callback, obfd) != 0)
487 return 0; /* FIXME error return/msg? */
489 /* Record phdrs for section-to-segment mapping. */
490 bfd_map_over_sections (obfd, make_output_phdrs, NULL);
492 /* Copy memory region contents. */
493 bfd_map_over_sections (obfd, gcore_copy_callback, NULL);
495 return 1; /* success */
499 _initialize_gcore (void)
501 add_com ("generate-core-file", class_files, gcore_command,
502 "Save a core file with the current state of the debugged process.\n\
503 Argument is optional filename. Default filename is 'core.<process_id>'.");
505 add_com_alias ("gcore", "generate-core-file", class_files, 1);
506 exec_set_find_memory_regions (objfile_find_memory_regions);