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"
26 #include <sys/procfs.h>
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
*);
35 /* Function: gcore_command
36 Generate a core file from the inferior process. */
39 gcore_command (char *args
, int from_tty
)
41 struct cleanup
*old_chain
;
42 char *corefilename
, corefilename_buffer
[40];
43 asection
*note_sec
= NULL
;
45 void *note_data
= NULL
;
48 /* No use generating a corefile without a target process. */
49 if (!(target_has_execution
))
56 /* Default corefile name is "core.PID". */
57 sprintf (corefilename_buffer
, "core.%d", PIDGET (inferior_ptid
));
58 corefilename
= corefilename_buffer
;
62 fprintf_filtered (gdb_stdout
,
63 "Opening corefile '%s' for output.\n", corefilename
);
65 /* Open the output file. */
66 if (!(obfd
= bfd_openw (corefilename
, default_gcore_target ())))
68 error ("Failed to open '%s' for output.", corefilename
);
71 /* Need a cleanup that will close the file (FIXME: delete it?). */
72 old_chain
= make_cleanup_bfd_close (obfd
);
74 bfd_set_format (obfd
, bfd_core
);
75 bfd_set_arch_mach (obfd
, default_gcore_arch (), default_gcore_mach ());
77 /* An external target method must build the notes section. */
78 note_data
= (char *) target_make_corefile_notes (obfd
, ¬e_size
);
80 /* Create the note section. */
81 if (note_data
!= NULL
&& note_size
!= 0)
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 ()));
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
);
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.");
98 /* Write out the contents of the note section. */
99 if (note_data
!= NULL
&& note_size
!= 0)
101 if (!bfd_set_section_contents (obfd
, note_sec
, note_data
, 0, note_size
))
103 warning ("writing note section (%s)",
104 bfd_errmsg (bfd_get_error ()));
109 fprintf_filtered (gdb_stdout
,
110 "Saved corefile %s\n", corefilename
);
112 /* Clean-ups will close the output file and free malloc memory. */
113 do_cleanups (old_chain
);
118 default_gcore_mach (void)
120 #if 1 /* See if this even matters... */
123 #ifdef TARGET_ARCHITECTURE
124 const struct bfd_arch_info
* bfdarch
= TARGET_ARCHITECTURE
;
127 return bfdarch
->mach
;
128 #endif /* TARGET_ARCHITECTURE */
129 if (exec_bfd
== NULL
)
130 error ("Can't find default bfd machine type (need execfile).");
132 return bfd_get_mach (exec_bfd
);
136 static enum bfd_architecture
137 default_gcore_arch (void)
139 #ifdef TARGET_ARCHITECTURE
140 const struct bfd_arch_info
* bfdarch
= TARGET_ARCHITECTURE
;
143 return bfdarch
->arch
;
145 if (exec_bfd
== NULL
)
146 error ("Can't find bfd architecture for corefile (need execfile).");
148 return bfd_get_arch (exec_bfd
);
152 default_gcore_target (void)
154 /* FIXME -- this may only work for ELF targets. */
155 if (exec_bfd
== NULL
)
158 return bfd_get_target (exec_bfd
);
162 * Default method for stack segment (preemptable by target).
165 static int (*override_derive_stack_segment
) (bfd_vma
*, bfd_vma
*);
168 preempt_derive_stack_segment (int (*override_func
) (bfd_vma
*, bfd_vma
*))
170 override_derive_stack_segment
= override_func
;
173 /* Function: default_derive_stack_segment
174 Derive a reasonable stack segment by unwinding the target stack.
176 Returns 0 for failure, 1 for success. */
179 default_derive_stack_segment (bfd_vma
*bottom
, bfd_vma
*top
)
182 struct frame_info
*fi
, *tmp_fi
;
184 if (bottom
== NULL
|| top
== NULL
)
185 return 0; /* Paranoia. */
187 if (!target_has_stack
|| !target_has_registers
)
188 return 0; /* Can't succeed without stack and registers. */
190 if ((fi
= get_current_frame ()) == NULL
)
191 return 0; /* Can't succeed without current frame. */
193 /* Save frame pointer of TOS frame. */
195 /* If current stack pointer is more "inner", use that instead. */
196 if (INNER_THAN (read_sp (), *top
))
199 /* Find prev-most frame. */
200 while ((tmp_fi
= get_prev_frame (fi
)) != NULL
)
203 /* Save frame pointer of prev-most frame. */
206 /* Now canonicalize their order, so that 'bottom' is a lower address
207 (as opposed to a lower stack frame). */
215 return 1; /* success */
219 derive_stack_segment (bfd_vma
*bottom
, bfd_vma
*top
)
221 if (override_derive_stack_segment
)
222 return override_derive_stack_segment (bottom
, top
);
224 return default_derive_stack_segment (bottom
, top
);
228 * Default method for heap segment (preemptable by target).
231 static int (*override_derive_heap_segment
) (bfd
*, bfd_vma
*, bfd_vma
*);
234 preempt_derive_heap_segment (int (*override_func
) (bfd
*,
235 bfd_vma
*, bfd_vma
*))
237 override_derive_heap_segment
= override_func
;
240 /* Function: default_derive_heap_segment
241 Derive a reasonable heap segment by looking at sbrk and
242 the static data sections.
244 Returns 0 for failure, 1 for success. */
247 default_derive_heap_segment (bfd
*abfd
, bfd_vma
*bottom
, bfd_vma
*top
)
249 bfd_vma top_of_data_memory
= 0;
250 bfd_vma top_of_heap
= 0;
251 bfd_size_type sec_size
;
252 struct value
*zero
, *sbrk
;
256 if (bottom
== NULL
|| top
== NULL
)
257 return 0; /* Paranoia. */
259 if (!target_has_execution
)
260 return 0; /* This function depends on being able
261 to call a function in the inferior. */
263 /* Assumption: link map is arranged as follows (low to high addresses):
265 data sections (including bss)
269 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
271 if (bfd_get_section_flags (abfd
, sec
) & SEC_DATA
||
272 strcmp (".bss", bfd_section_name (abfd
, sec
)) == 0)
274 sec_vaddr
= bfd_get_section_vma (abfd
, sec
);
275 sec_size
= bfd_get_section_size_before_reloc (sec
);
276 if (sec_vaddr
+ sec_size
> top_of_data_memory
)
277 top_of_data_memory
= sec_vaddr
+ sec_size
;
280 /* Now get the top-of-heap by calling sbrk in the inferior. */
281 if (lookup_minimal_symbol ("sbrk", NULL
, NULL
) != NULL
)
283 if ((sbrk
= find_function_in_inferior ("sbrk")) == NULL
)
286 else if (lookup_minimal_symbol ("_sbrk", NULL
, NULL
) != NULL
)
288 if ((sbrk
= find_function_in_inferior ("_sbrk")) == NULL
)
294 if ((zero
= value_from_longest (builtin_type_int
, (LONGEST
) 0)) == NULL
)
296 if ((sbrk
= call_function_by_hand (sbrk
, 1, &zero
)) == NULL
)
298 top_of_heap
= value_as_long (sbrk
);
300 /* Return results. */
301 if (top_of_heap
> top_of_data_memory
)
303 *bottom
= top_of_data_memory
;
305 return 1; /* success */
308 return 0; /* No additional heap space needs to be saved. */
312 derive_heap_segment (bfd
*abfd
, bfd_vma
*bottom
, bfd_vma
*top
)
314 if (override_derive_heap_segment
)
315 return override_derive_heap_segment (abfd
, bottom
, top
);
317 return default_derive_heap_segment (abfd
, bottom
, top
);
322 make_output_phdrs (bfd
*obfd
, asection
*osec
, void *ignored
)
327 /* FIXME: these constants may only be applicable for ELF. */
328 if (strncmp (bfd_section_name (obfd
, osec
), "load", 4) == 0)
333 p_flags
|= PF_R
; /* Segment is readable. */
334 if (!(bfd_get_section_flags (obfd
, osec
) & SEC_READONLY
))
335 p_flags
|= PF_W
; /* Segment is writable. */
336 if (bfd_get_section_flags (obfd
, osec
) & SEC_CODE
)
337 p_flags
|= PF_X
; /* Segment is executable. */
339 bfd_record_phdr (obfd
, p_type
, 1, p_flags
, 0, 0,
344 make_mem_sec (bfd
*obfd
,
348 unsigned int alignment
)
352 if ((osec
= bfd_make_section_anyway (obfd
, "load")) == NULL
)
354 warning ("Couldn't make gcore segment: %s",
355 bfd_errmsg (bfd_get_error ()));
361 fprintf_filtered (gdb_stdout
,
362 "Save segment, %lld bytes at 0x%s\n",
363 (long long) size
, paddr_nz (addr
));
366 bfd_set_section_size (obfd
, osec
, size
);
367 bfd_set_section_vma (obfd
, osec
, addr
);
368 osec
->lma
= 0; /* FIXME: there should be a macro for this! */
369 bfd_set_section_alignment (obfd
, osec
, alignment
);
370 bfd_set_section_flags (obfd
, osec
,
371 flags
| SEC_LOAD
| SEC_ALLOC
| SEC_HAS_CONTENTS
);
376 gcore_create_callback (CORE_ADDR vaddr
,
378 int read
, int write
, int exec
,
385 flags
|= SEC_READONLY
;
386 /* Set size == zero for readonly sections. */
398 return ((make_mem_sec ((bfd
*) data
, vaddr
, size
, flags
, 0)) == NULL
);
402 objfile_find_memory_regions (int (*func
) (CORE_ADDR
,
408 /* Use objfile data to create memory sections. */
409 struct objfile
*objfile
;
410 struct obj_section
*objsec
;
411 bfd_vma temp_bottom
, temp_top
;
413 /* Call callback function for each objfile section. */
414 ALL_OBJSECTIONS (objfile
, objsec
)
416 bfd
*ibfd
= objfile
->obfd
;
417 asection
*isec
= objsec
->the_bfd_section
;
418 flagword flags
= bfd_get_section_flags (ibfd
, isec
);
421 if ((flags
& SEC_ALLOC
) || (flags
& SEC_LOAD
))
423 int size
= bfd_section_size (ibfd
, isec
);
426 if ((ret
= (*func
) (objsec
->addr
,
427 bfd_section_size (ibfd
, isec
),
428 1, /* All sections will be readable. */
429 (flags
& SEC_READONLY
) == 0, /* writable */
430 (flags
& SEC_CODE
) != 0, /* executable */
436 /* Make a stack segment. */
437 if (derive_stack_segment (&temp_bottom
, &temp_top
))
438 (*func
) (temp_bottom
,
439 temp_top
- temp_bottom
,
440 1, /* Stack section will be readable */
441 1, /* Stack section will be writable */
442 0, /* Stack section will not be executable */
445 /* Make a heap segment. */
446 if (derive_heap_segment (exec_bfd
, &temp_bottom
, &temp_top
))
447 (*func
) (temp_bottom
,
448 temp_top
- temp_bottom
,
449 1, /* Heap section will be readable */
450 1, /* Heap section will be writable */
451 0, /* Heap section will not be executable */
457 gcore_copy_callback (bfd
*obfd
, asection
*osec
, void *ignored
)
459 bfd_size_type size
= bfd_section_size (obfd
, osec
);
460 struct cleanup
*old_chain
= NULL
;
464 return; /* Read-only sections are marked as zero-size.
465 We don't have to copy their contents. */
466 if (strncmp ("load", bfd_section_name (obfd
, osec
), 4) != 0)
467 return; /* Only interested in "load" sections. */
469 if ((memhunk
= xmalloc (size
)) == NULL
)
470 error ("Not enough memory to create corefile.");
471 old_chain
= make_cleanup (xfree
, memhunk
);
473 if (target_read_memory (bfd_section_vma (obfd
, osec
),
475 warning ("Memory read failed for corefile section, %ld bytes at 0x%s\n",
476 (long) size
, paddr (bfd_section_vma (obfd
, osec
)));
477 if (!bfd_set_section_contents (obfd
, osec
, memhunk
, 0, size
))
478 warning ("Failed to write corefile contents (%s).",
479 bfd_errmsg (bfd_get_error ()));
481 do_cleanups (old_chain
); /* frees the xmalloc buffer */
485 gcore_memory_sections (bfd
*obfd
)
487 if (target_find_memory_regions (gcore_create_callback
, obfd
) != 0)
488 return 0; /* FIXME error return/msg? */
490 /* Record phdrs for section-to-segment mapping. */
491 bfd_map_over_sections (obfd
, make_output_phdrs
, NULL
);
493 /* Copy memory region contents. */
494 bfd_map_over_sections (obfd
, gcore_copy_callback
, NULL
);
496 return 1; /* success */
500 _initialize_gcore (void)
502 add_com ("generate-core-file", class_files
, gcore_command
,
503 "Save a core file with the current state of the debugged process.\n\
504 Argument is optional filename. Default filename is 'core.<process_id>'.");
506 add_com_alias ("gcore", "generate-core-file", class_files
, 1);
507 exec_set_find_memory_regions (objfile_find_memory_regions
);