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. */
23 #include "cli/cli-decode.h"
31 static char *default_gcore_target (void);
32 static enum bfd_architecture
default_gcore_arch (void);
33 static unsigned long default_gcore_mach (void);
34 static int gcore_memory_sections (bfd
*);
36 /* Function: gcore_command
37 Generate a core file from the inferior process. */
40 gcore_command (char *args
, int from_tty
)
42 struct cleanup
*old_chain
;
43 char *corefilename
, corefilename_buffer
[40];
44 asection
*note_sec
= NULL
;
46 void *note_data
= NULL
;
49 /* No use generating a corefile without a target process. */
50 if (!(target_has_execution
))
57 /* Default corefile name is "core.PID". */
58 sprintf (corefilename_buffer
, "core.%d", PIDGET (inferior_ptid
));
59 corefilename
= corefilename_buffer
;
63 fprintf_filtered (gdb_stdout
,
64 "Opening corefile '%s' for output.\n", corefilename
);
66 /* Open the output file. */
67 if (!(obfd
= bfd_openw (corefilename
, default_gcore_target ())))
69 error ("Failed to open '%s' for output.", corefilename
);
72 /* Need a cleanup that will close the file (FIXME: delete it?). */
73 old_chain
= make_cleanup_bfd_close (obfd
);
75 bfd_set_format (obfd
, bfd_core
);
76 bfd_set_arch_mach (obfd
, default_gcore_arch (), default_gcore_mach ());
78 /* An external target method must build the notes section. */
79 note_data
= (char *) target_make_corefile_notes (obfd
, ¬e_size
);
81 /* Create the note section. */
82 if (note_data
!= NULL
&& note_size
!= 0)
84 if ((note_sec
= bfd_make_section_anyway (obfd
, "note0")) == NULL
)
85 error ("Failed to create 'note' section for corefile: %s",
86 bfd_errmsg (bfd_get_error ()));
88 bfd_set_section_vma (obfd
, note_sec
, 0);
89 bfd_set_section_flags (obfd
, note_sec
,
90 SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_ALLOC
);
91 bfd_set_section_alignment (obfd
, note_sec
, 0);
92 bfd_set_section_size (obfd
, note_sec
, note_size
);
95 /* Now create the memory/load sections. */
96 if (gcore_memory_sections (obfd
) == 0)
97 error ("gcore: failed to get corefile memory sections from target.");
99 /* Write out the contents of the note section. */
100 if (note_data
!= NULL
&& note_size
!= 0)
102 if (!bfd_set_section_contents (obfd
, note_sec
, note_data
, 0, note_size
))
104 warning ("writing note section (%s)",
105 bfd_errmsg (bfd_get_error ()));
110 fprintf_filtered (gdb_stdout
,
111 "Saved corefile %s\n", corefilename
);
113 /* Clean-ups will close the output file and free malloc memory. */
114 do_cleanups (old_chain
);
119 default_gcore_mach (void)
121 #if 1 /* See if this even matters... */
124 #ifdef TARGET_ARCHITECTURE
125 const struct bfd_arch_info
* bfdarch
= TARGET_ARCHITECTURE
;
128 return bfdarch
->mach
;
129 #endif /* TARGET_ARCHITECTURE */
130 if (exec_bfd
== NULL
)
131 error ("Can't find default bfd machine type (need execfile).");
133 return bfd_get_mach (exec_bfd
);
137 static enum bfd_architecture
138 default_gcore_arch (void)
140 #ifdef TARGET_ARCHITECTURE
141 const struct bfd_arch_info
* bfdarch
= TARGET_ARCHITECTURE
;
144 return bfdarch
->arch
;
146 if (exec_bfd
== NULL
)
147 error ("Can't find bfd architecture for corefile (need execfile).");
149 return bfd_get_arch (exec_bfd
);
153 default_gcore_target (void)
155 /* FIXME -- this may only work for ELF targets. */
156 if (exec_bfd
== NULL
)
159 return bfd_get_target (exec_bfd
);
162 /* Function: derive_stack_segment
164 Derive a reasonable stack segment by unwinding the target stack.
166 Returns 0 for failure, 1 for success. */
169 derive_stack_segment (bfd_vma
*bottom
, bfd_vma
*top
)
172 struct frame_info
*fi
, *tmp_fi
;
174 if (bottom
== NULL
|| top
== NULL
)
175 return 0; /* Paranoia. */
177 if (!target_has_stack
|| !target_has_registers
)
178 return 0; /* Can't succeed without stack and registers. */
180 if ((fi
= get_current_frame ()) == NULL
)
181 return 0; /* Can't succeed without current frame. */
183 /* Save frame pointer of TOS frame. */
184 *top
= get_frame_base (fi
);
185 /* If current stack pointer is more "inner", use that instead. */
186 if (INNER_THAN (read_sp (), *top
))
189 /* Find prev-most frame. */
190 while ((tmp_fi
= get_prev_frame (fi
)) != NULL
)
193 /* Save frame pointer of prev-most frame. */
194 *bottom
= get_frame_base (fi
);
196 /* Now canonicalize their order, so that 'bottom' is a lower address
197 (as opposed to a lower stack frame). */
205 return 1; /* success */
208 /* Function: derive_heap_segment
210 Derive a reasonable heap segment by looking at sbrk and
211 the static data sections.
213 Returns 0 for failure, 1 for success. */
216 derive_heap_segment (bfd
*abfd
, bfd_vma
*bottom
, bfd_vma
*top
)
218 bfd_vma top_of_data_memory
= 0;
219 bfd_vma top_of_heap
= 0;
220 bfd_size_type sec_size
;
221 struct value
*zero
, *sbrk
;
225 if (bottom
== NULL
|| top
== NULL
)
226 return 0; /* Paranoia. */
228 if (!target_has_execution
)
229 return 0; /* This function depends on being able
230 to call a function in the inferior. */
232 /* Assumption: link map is arranged as follows (low to high addresses):
234 data sections (including bss)
238 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
240 if (bfd_get_section_flags (abfd
, sec
) & SEC_DATA
||
241 strcmp (".bss", bfd_section_name (abfd
, sec
)) == 0)
243 sec_vaddr
= bfd_get_section_vma (abfd
, sec
);
244 sec_size
= bfd_get_section_size_before_reloc (sec
);
245 if (sec_vaddr
+ sec_size
> top_of_data_memory
)
246 top_of_data_memory
= sec_vaddr
+ sec_size
;
249 /* Now get the top-of-heap by calling sbrk in the inferior. */
250 if (lookup_minimal_symbol ("sbrk", NULL
, NULL
) != NULL
)
252 if ((sbrk
= find_function_in_inferior ("sbrk")) == NULL
)
255 else if (lookup_minimal_symbol ("_sbrk", NULL
, NULL
) != NULL
)
257 if ((sbrk
= find_function_in_inferior ("_sbrk")) == NULL
)
263 if ((zero
= value_from_longest (builtin_type_int
, (LONGEST
) 0)) == NULL
)
265 if ((sbrk
= call_function_by_hand (sbrk
, 1, &zero
)) == NULL
)
267 top_of_heap
= value_as_long (sbrk
);
269 /* Return results. */
270 if (top_of_heap
> top_of_data_memory
)
272 *bottom
= top_of_data_memory
;
274 return 1; /* success */
277 return 0; /* No additional heap space needs to be saved. */
282 make_output_phdrs (bfd
*obfd
, asection
*osec
, void *ignored
)
287 /* FIXME: these constants may only be applicable for ELF. */
288 if (strncmp (bfd_section_name (obfd
, osec
), "load", 4) == 0)
293 p_flags
|= PF_R
; /* Segment is readable. */
294 if (!(bfd_get_section_flags (obfd
, osec
) & SEC_READONLY
))
295 p_flags
|= PF_W
; /* Segment is writable. */
296 if (bfd_get_section_flags (obfd
, osec
) & SEC_CODE
)
297 p_flags
|= PF_X
; /* Segment is executable. */
299 bfd_record_phdr (obfd
, p_type
, 1, p_flags
, 0, 0,
304 make_mem_sec (bfd
*obfd
,
308 unsigned int alignment
)
312 if ((osec
= bfd_make_section_anyway (obfd
, "load")) == NULL
)
314 warning ("Couldn't make gcore segment: %s",
315 bfd_errmsg (bfd_get_error ()));
321 fprintf_filtered (gdb_stdout
,
322 "Save segment, %lld bytes at 0x%s\n",
323 (long long) size
, paddr_nz (addr
));
326 bfd_set_section_size (obfd
, osec
, size
);
327 bfd_set_section_vma (obfd
, osec
, addr
);
328 osec
->lma
= 0; /* FIXME: there should be a macro for this! */
329 bfd_set_section_alignment (obfd
, osec
, alignment
);
330 bfd_set_section_flags (obfd
, osec
,
331 flags
| SEC_LOAD
| SEC_ALLOC
| SEC_HAS_CONTENTS
);
336 gcore_create_callback (CORE_ADDR vaddr
,
338 int read
, int write
, int exec
,
345 flags
|= SEC_READONLY
;
346 /* Set size == zero for readonly sections. */
358 return ((make_mem_sec ((bfd
*) data
, vaddr
, size
, flags
, 0)) == NULL
);
362 objfile_find_memory_regions (int (*func
) (CORE_ADDR
,
368 /* Use objfile data to create memory sections. */
369 struct objfile
*objfile
;
370 struct obj_section
*objsec
;
371 bfd_vma temp_bottom
, temp_top
;
373 /* Call callback function for each objfile section. */
374 ALL_OBJSECTIONS (objfile
, objsec
)
376 bfd
*ibfd
= objfile
->obfd
;
377 asection
*isec
= objsec
->the_bfd_section
;
378 flagword flags
= bfd_get_section_flags (ibfd
, isec
);
381 if ((flags
& SEC_ALLOC
) || (flags
& SEC_LOAD
))
383 int size
= bfd_section_size (ibfd
, isec
);
386 if ((ret
= (*func
) (objsec
->addr
,
387 bfd_section_size (ibfd
, isec
),
388 1, /* All sections will be readable. */
389 (flags
& SEC_READONLY
) == 0, /* writable */
390 (flags
& SEC_CODE
) != 0, /* executable */
396 /* Make a stack segment. */
397 if (derive_stack_segment (&temp_bottom
, &temp_top
))
398 (*func
) (temp_bottom
,
399 temp_top
- temp_bottom
,
400 1, /* Stack section will be readable */
401 1, /* Stack section will be writable */
402 0, /* Stack section will not be executable */
405 /* Make a heap segment. */
406 if (derive_heap_segment (exec_bfd
, &temp_bottom
, &temp_top
))
407 (*func
) (temp_bottom
,
408 temp_top
- temp_bottom
,
409 1, /* Heap section will be readable */
410 1, /* Heap section will be writable */
411 0, /* Heap section will not be executable */
417 gcore_copy_callback (bfd
*obfd
, asection
*osec
, void *ignored
)
419 bfd_size_type size
= bfd_section_size (obfd
, osec
);
420 struct cleanup
*old_chain
= NULL
;
424 return; /* Read-only sections are marked as zero-size.
425 We don't have to copy their contents. */
426 if (strncmp ("load", bfd_section_name (obfd
, osec
), 4) != 0)
427 return; /* Only interested in "load" sections. */
429 if ((memhunk
= xmalloc (size
)) == NULL
)
430 error ("Not enough memory to create corefile.");
431 old_chain
= make_cleanup (xfree
, memhunk
);
433 if (target_read_memory (bfd_section_vma (obfd
, osec
),
435 warning ("Memory read failed for corefile section, %ld bytes at 0x%s\n",
436 (long) size
, paddr (bfd_section_vma (obfd
, osec
)));
437 if (!bfd_set_section_contents (obfd
, osec
, memhunk
, 0, size
))
438 warning ("Failed to write corefile contents (%s).",
439 bfd_errmsg (bfd_get_error ()));
441 do_cleanups (old_chain
); /* frees the xmalloc buffer */
445 gcore_memory_sections (bfd
*obfd
)
447 if (target_find_memory_regions (gcore_create_callback
, obfd
) != 0)
448 return 0; /* FIXME error return/msg? */
450 /* Record phdrs for section-to-segment mapping. */
451 bfd_map_over_sections (obfd
, make_output_phdrs
, NULL
);
453 /* Copy memory region contents. */
454 bfd_map_over_sections (obfd
, gcore_copy_callback
, NULL
);
456 return 1; /* success */
460 _initialize_gcore (void)
462 add_com ("generate-core-file", class_files
, gcore_command
,
463 "Save a core file with the current state of the debugged process.\n\
464 Argument is optional filename. Default filename is 'core.<process_id>'.");
466 add_com_alias ("gcore", "generate-core-file", class_files
, 1);
467 exec_set_find_memory_regions (objfile_find_memory_regions
);