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
);
160 /* Function: derive_stack_segment
162 Derive a reasonable stack segment by unwinding the target stack.
164 Returns 0 for failure, 1 for success. */
167 derive_stack_segment (bfd_vma
*bottom
, bfd_vma
*top
)
170 struct frame_info
*fi
, *tmp_fi
;
172 if (bottom
== NULL
|| top
== NULL
)
173 return 0; /* Paranoia. */
175 if (!target_has_stack
|| !target_has_registers
)
176 return 0; /* Can't succeed without stack and registers. */
178 if ((fi
= get_current_frame ()) == NULL
)
179 return 0; /* Can't succeed without current frame. */
181 /* Save frame pointer of TOS frame. */
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. */
194 /* Now canonicalize their order, so that 'bottom' is a lower address
195 (as opposed to a lower stack frame). */
203 return 1; /* success */
206 /* Function: derive_heap_segment
208 Derive a reasonable heap segment by looking at sbrk and
209 the static data sections.
211 Returns 0 for failure, 1 for success. */
214 derive_heap_segment (bfd
*abfd
, bfd_vma
*bottom
, bfd_vma
*top
)
216 bfd_vma top_of_data_memory
= 0;
217 bfd_vma top_of_heap
= 0;
218 bfd_size_type sec_size
;
219 struct value
*zero
, *sbrk
;
223 if (bottom
== NULL
|| top
== NULL
)
224 return 0; /* Paranoia. */
226 if (!target_has_execution
)
227 return 0; /* This function depends on being able
228 to call a function in the inferior. */
230 /* Assumption: link map is arranged as follows (low to high addresses):
232 data sections (including bss)
236 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
238 if (bfd_get_section_flags (abfd
, sec
) & SEC_DATA
||
239 strcmp (".bss", bfd_section_name (abfd
, sec
)) == 0)
241 sec_vaddr
= bfd_get_section_vma (abfd
, sec
);
242 sec_size
= bfd_get_section_size_before_reloc (sec
);
243 if (sec_vaddr
+ sec_size
> top_of_data_memory
)
244 top_of_data_memory
= sec_vaddr
+ sec_size
;
247 /* Now get the top-of-heap by calling sbrk in the inferior. */
248 if (lookup_minimal_symbol ("sbrk", NULL
, NULL
) != NULL
)
250 if ((sbrk
= find_function_in_inferior ("sbrk")) == NULL
)
253 else if (lookup_minimal_symbol ("_sbrk", NULL
, NULL
) != NULL
)
255 if ((sbrk
= find_function_in_inferior ("_sbrk")) == NULL
)
261 if ((zero
= value_from_longest (builtin_type_int
, (LONGEST
) 0)) == NULL
)
263 if ((sbrk
= call_function_by_hand (sbrk
, 1, &zero
)) == NULL
)
265 top_of_heap
= value_as_long (sbrk
);
267 /* Return results. */
268 if (top_of_heap
> top_of_data_memory
)
270 *bottom
= top_of_data_memory
;
272 return 1; /* success */
275 return 0; /* No additional heap space needs to be saved. */
280 make_output_phdrs (bfd
*obfd
, asection
*osec
, void *ignored
)
285 /* FIXME: these constants may only be applicable for ELF. */
286 if (strncmp (bfd_section_name (obfd
, osec
), "load", 4) == 0)
291 p_flags
|= PF_R
; /* Segment is readable. */
292 if (!(bfd_get_section_flags (obfd
, osec
) & SEC_READONLY
))
293 p_flags
|= PF_W
; /* Segment is writable. */
294 if (bfd_get_section_flags (obfd
, osec
) & SEC_CODE
)
295 p_flags
|= PF_X
; /* Segment is executable. */
297 bfd_record_phdr (obfd
, p_type
, 1, p_flags
, 0, 0,
302 make_mem_sec (bfd
*obfd
,
306 unsigned int alignment
)
310 if ((osec
= bfd_make_section_anyway (obfd
, "load")) == NULL
)
312 warning ("Couldn't make gcore segment: %s",
313 bfd_errmsg (bfd_get_error ()));
319 fprintf_filtered (gdb_stdout
,
320 "Save segment, %lld bytes at 0x%s\n",
321 (long long) size
, paddr_nz (addr
));
324 bfd_set_section_size (obfd
, osec
, size
);
325 bfd_set_section_vma (obfd
, osec
, addr
);
326 osec
->lma
= 0; /* FIXME: there should be a macro for this! */
327 bfd_set_section_alignment (obfd
, osec
, alignment
);
328 bfd_set_section_flags (obfd
, osec
,
329 flags
| SEC_LOAD
| SEC_ALLOC
| SEC_HAS_CONTENTS
);
334 gcore_create_callback (CORE_ADDR vaddr
,
336 int read
, int write
, int exec
,
343 flags
|= SEC_READONLY
;
344 /* Set size == zero for readonly sections. */
356 return ((make_mem_sec ((bfd
*) data
, vaddr
, size
, flags
, 0)) == NULL
);
360 objfile_find_memory_regions (int (*func
) (CORE_ADDR
,
366 /* Use objfile data to create memory sections. */
367 struct objfile
*objfile
;
368 struct obj_section
*objsec
;
369 bfd_vma temp_bottom
, temp_top
;
371 /* Call callback function for each objfile section. */
372 ALL_OBJSECTIONS (objfile
, objsec
)
374 bfd
*ibfd
= objfile
->obfd
;
375 asection
*isec
= objsec
->the_bfd_section
;
376 flagword flags
= bfd_get_section_flags (ibfd
, isec
);
379 if ((flags
& SEC_ALLOC
) || (flags
& SEC_LOAD
))
381 int size
= bfd_section_size (ibfd
, isec
);
384 if ((ret
= (*func
) (objsec
->addr
,
385 bfd_section_size (ibfd
, isec
),
386 1, /* All sections will be readable. */
387 (flags
& SEC_READONLY
) == 0, /* writable */
388 (flags
& SEC_CODE
) != 0, /* executable */
394 /* Make a stack segment. */
395 if (derive_stack_segment (&temp_bottom
, &temp_top
))
396 (*func
) (temp_bottom
,
397 temp_top
- temp_bottom
,
398 1, /* Stack section will be readable */
399 1, /* Stack section will be writable */
400 0, /* Stack section will not be executable */
403 /* Make a heap segment. */
404 if (derive_heap_segment (exec_bfd
, &temp_bottom
, &temp_top
))
405 (*func
) (temp_bottom
,
406 temp_top
- temp_bottom
,
407 1, /* Heap section will be readable */
408 1, /* Heap section will be writable */
409 0, /* Heap section will not be executable */
415 gcore_copy_callback (bfd
*obfd
, asection
*osec
, void *ignored
)
417 bfd_size_type size
= bfd_section_size (obfd
, osec
);
418 struct cleanup
*old_chain
= NULL
;
422 return; /* Read-only sections are marked as zero-size.
423 We don't have to copy their contents. */
424 if (strncmp ("load", bfd_section_name (obfd
, osec
), 4) != 0)
425 return; /* Only interested in "load" sections. */
427 if ((memhunk
= xmalloc (size
)) == NULL
)
428 error ("Not enough memory to create corefile.");
429 old_chain
= make_cleanup (xfree
, memhunk
);
431 if (target_read_memory (bfd_section_vma (obfd
, osec
),
433 warning ("Memory read failed for corefile section, %ld bytes at 0x%s\n",
434 (long) size
, paddr (bfd_section_vma (obfd
, osec
)));
435 if (!bfd_set_section_contents (obfd
, osec
, memhunk
, 0, size
))
436 warning ("Failed to write corefile contents (%s).",
437 bfd_errmsg (bfd_get_error ()));
439 do_cleanups (old_chain
); /* frees the xmalloc buffer */
443 gcore_memory_sections (bfd
*obfd
)
445 if (target_find_memory_regions (gcore_create_callback
, obfd
) != 0)
446 return 0; /* FIXME error return/msg? */
448 /* Record phdrs for section-to-segment mapping. */
449 bfd_map_over_sections (obfd
, make_output_phdrs
, NULL
);
451 /* Copy memory region contents. */
452 bfd_map_over_sections (obfd
, gcore_copy_callback
, NULL
);
454 return 1; /* success */
458 _initialize_gcore (void)
460 add_com ("generate-core-file", class_files
, gcore_command
,
461 "Save a core file with the current state of the debugged process.\n\
462 Argument is optional filename. Default filename is 'core.<process_id>'.");
464 add_com_alias ("gcore", "generate-core-file", class_files
, 1);
465 exec_set_find_memory_regions (objfile_find_memory_regions
);