2005-02-15 Andrew Cagney <cagney@gnu.org>
[deliverable/binutils-gdb.git] / gdb / gcore.c
CommitLineData
be4d1333 1/* Generate a core file for the inferior process.
1bac305b 2
86fbe6cc 3 Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
be4d1333
MS
4
5 This file is part of GDB.
6
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.
11
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.
16
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. */
21
22#include "defs.h"
d3420b2f
MK
23#include "elf-bfd.h"
24#include "infcall.h"
be4d1333
MS
25#include "inferior.h"
26#include "gdbcore.h"
be4d1333 27#include "objfiles.h"
d3420b2f
MK
28#include "symfile.h"
29
30#include "cli/cli-decode.h"
be4d1333 31
d3420b2f 32#include "gdb_assert.h"
be4d1333 33
d3420b2f
MK
34static char *default_gcore_target (void);
35static enum bfd_architecture default_gcore_arch (void);
36static unsigned long default_gcore_mach (void);
37static int gcore_memory_sections (bfd *);
38
39/* Generate a core file from the inferior process. */
be4d1333
MS
40
41static void
42gcore_command (char *args, int from_tty)
43{
44 struct cleanup *old_chain;
45 char *corefilename, corefilename_buffer[40];
356ae49d 46 asection *note_sec = NULL;
be4d1333
MS
47 bfd *obfd;
48 void *note_data = NULL;
49 int note_size = 0;
50
51 /* No use generating a corefile without a target process. */
d3420b2f 52 if (!target_has_execution)
be4d1333
MS
53 noprocess ();
54
55 if (args && *args)
56 corefilename = args;
57 else
58 {
59 /* Default corefile name is "core.PID". */
60 sprintf (corefilename_buffer, "core.%d", PIDGET (inferior_ptid));
61 corefilename = corefilename_buffer;
62 }
63
64 if (info_verbose)
cbb83bd1 65 fprintf_filtered (gdb_stdout,
be4d1333
MS
66 "Opening corefile '%s' for output.\n", corefilename);
67
d3420b2f
MK
68 /* Open the output file. */
69 obfd = bfd_openw (corefilename, default_gcore_target ());
70 if (!obfd)
8a3fe4f8 71 error (_("Failed to open '%s' for output."), corefilename);
be4d1333 72
d3420b2f 73 /* Need a cleanup that will close the file (FIXME: delete it?). */
be4d1333
MS
74 old_chain = make_cleanup_bfd_close (obfd);
75
76 bfd_set_format (obfd, bfd_core);
77 bfd_set_arch_mach (obfd, default_gcore_arch (), default_gcore_mach ());
78
d3420b2f
MK
79 /* An external target method must build the notes section. */
80 note_data = target_make_corefile_notes (obfd, &note_size);
be4d1333 81
d3420b2f 82 /* Create the note section. */
be4d1333
MS
83 if (note_data != NULL && note_size != 0)
84 {
d3420b2f
MK
85 note_sec = bfd_make_section_anyway (obfd, "note0");
86 if (note_sec == NULL)
8a3fe4f8 87 error (_("Failed to create 'note' section for corefile: %s"),
be4d1333
MS
88 bfd_errmsg (bfd_get_error ()));
89
90 bfd_set_section_vma (obfd, note_sec, 0);
cbb83bd1 91 bfd_set_section_flags (obfd, note_sec,
be4d1333
MS
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);
95 }
96
d3420b2f 97 /* Now create the memory/load sections. */
be4d1333 98 if (gcore_memory_sections (obfd) == 0)
8a3fe4f8 99 error (_("gcore: failed to get corefile memory sections from target."));
be4d1333 100
d3420b2f 101 /* Write out the contents of the note section. */
be4d1333
MS
102 if (note_data != NULL && note_size != 0)
103 {
104 if (!bfd_set_section_contents (obfd, note_sec, note_data, 0, note_size))
8a3fe4f8 105 warning (_("writing note section (%s)"), bfd_errmsg (bfd_get_error ()));
be4d1333
MS
106 }
107
d3420b2f
MK
108 /* Succeeded. */
109 fprintf_filtered (gdb_stdout, "Saved corefile %s\n", corefilename);
be4d1333 110
d3420b2f 111 /* Clean-ups will close the output file and free malloc memory. */
be4d1333
MS
112 do_cleanups (old_chain);
113 return;
114}
115
116static unsigned long
117default_gcore_mach (void)
118{
d3420b2f 119#if 1 /* See if this even matters... */
6dbdc4a3
MS
120 return 0;
121#else
be4d1333 122#ifdef TARGET_ARCHITECTURE
d3420b2f 123 const struct bfd_arch_info *bfdarch = TARGET_ARCHITECTURE;
be4d1333
MS
124
125 if (bfdarch != NULL)
126 return bfdarch->mach;
6dbdc4a3 127#endif /* TARGET_ARCHITECTURE */
be4d1333 128 if (exec_bfd == NULL)
8a3fe4f8 129 error (_("Can't find default bfd machine type (need execfile)."));
be4d1333
MS
130
131 return bfd_get_mach (exec_bfd);
6dbdc4a3 132#endif /* 1 */
be4d1333
MS
133}
134
135static enum bfd_architecture
136default_gcore_arch (void)
137{
138#ifdef TARGET_ARCHITECTURE
139 const struct bfd_arch_info * bfdarch = TARGET_ARCHITECTURE;
140
141 if (bfdarch != NULL)
142 return bfdarch->arch;
143#endif
144 if (exec_bfd == NULL)
8a3fe4f8 145 error (_("Can't find bfd architecture for corefile (need execfile)."));
be4d1333
MS
146
147 return bfd_get_arch (exec_bfd);
148}
149
150static char *
151default_gcore_target (void)
152{
d3420b2f 153 /* FIXME: This may only work for ELF targets. */
be4d1333 154 if (exec_bfd == NULL)
6dbdc4a3
MS
155 return NULL;
156 else
157 return bfd_get_target (exec_bfd);
be4d1333
MS
158}
159
d3420b2f
MK
160/* Derive a reasonable stack segment by unwinding the target stack,
161 and store its limits in *BOTTOM and *TOP. Return non-zero if
162 successful. */
be4d1333 163
cbb83bd1 164static int
69db8bae 165derive_stack_segment (bfd_vma *bottom, bfd_vma *top)
be4d1333 166{
be4d1333
MS
167 struct frame_info *fi, *tmp_fi;
168
d3420b2f
MK
169 gdb_assert (bottom);
170 gdb_assert (top);
be4d1333 171
d3420b2f 172 /* Can't succeed without stack and registers. */
be4d1333 173 if (!target_has_stack || !target_has_registers)
d3420b2f 174 return 0;
be4d1333 175
d3420b2f
MK
176 /* Can't succeed without current frame. */
177 fi = get_current_frame ();
178 if (fi == NULL)
179 return 0;
be4d1333 180
d3420b2f 181 /* Save frame pointer of TOS frame. */
8d357cca 182 *top = get_frame_base (fi);
d3420b2f 183 /* If current stack pointer is more "inner", use that instead. */
be4d1333
MS
184 if (INNER_THAN (read_sp (), *top))
185 *top = read_sp ();
186
d3420b2f 187 /* Find prev-most frame. */
be4d1333
MS
188 while ((tmp_fi = get_prev_frame (fi)) != NULL)
189 fi = tmp_fi;
190
d3420b2f 191 /* Save frame pointer of prev-most frame. */
8d357cca 192 *bottom = get_frame_base (fi);
be4d1333 193
d3420b2f
MK
194 /* Now canonicalize their order, so that BOTTOM is a lower address
195 (as opposed to a lower stack frame). */
be4d1333
MS
196 if (*bottom > *top)
197 {
d3420b2f
MK
198 bfd_vma tmp_vma;
199
be4d1333
MS
200 tmp_vma = *top;
201 *top = *bottom;
202 *bottom = tmp_vma;
203 }
204
d3420b2f 205 return 1;
be4d1333
MS
206}
207
d3420b2f
MK
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. */
be4d1333 211
cbb83bd1 212static int
69db8bae 213derive_heap_segment (bfd *abfd, bfd_vma *bottom, bfd_vma *top)
be4d1333
MS
214{
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;
219 bfd_vma sec_vaddr;
220 asection *sec;
221
d3420b2f
MK
222 gdb_assert (bottom);
223 gdb_assert (top);
be4d1333 224
d3420b2f
MK
225 /* This function depends on being able to call a function in the
226 inferior. */
be4d1333 227 if (!target_has_execution)
d3420b2f
MK
228 return 0;
229
230 /* The following code assumes that the link map is arranged as
231 follows (low to high addresses):
be4d1333 232
d3420b2f
MK
233 ---------------------------------
234 | text sections |
235 ---------------------------------
236 | data sections (including bss) |
237 ---------------------------------
238 | heap |
239 --------------------------------- */
be4d1333
MS
240
241 for (sec = abfd->sections; sec; sec = sec->next)
242 {
d3420b2f
MK
243 if (bfd_get_section_flags (abfd, sec) & SEC_DATA
244 || strcmp (".bss", bfd_section_name (abfd, sec)) == 0)
be4d1333
MS
245 {
246 sec_vaddr = bfd_get_section_vma (abfd, sec);
2c500098 247 sec_size = bfd_get_section_size (sec);
be4d1333
MS
248 if (sec_vaddr + sec_size > top_of_data_memory)
249 top_of_data_memory = sec_vaddr + sec_size;
250 }
251 }
d3420b2f 252
be4d1333 253 /* Now get the top-of-heap by calling sbrk in the inferior. */
20fe79c8
MS
254 if (lookup_minimal_symbol ("sbrk", NULL, NULL) != NULL)
255 {
d3420b2f
MK
256 sbrk = find_function_in_inferior ("sbrk");
257 if (sbrk == NULL)
20fe79c8
MS
258 return 0;
259 }
260 else if (lookup_minimal_symbol ("_sbrk", NULL, NULL) != NULL)
261 {
d3420b2f
MK
262 sbrk = find_function_in_inferior ("_sbrk");
263 if (sbrk == NULL)
20fe79c8
MS
264 return 0;
265 }
266 else
be4d1333 267 return 0;
20fe79c8 268
d3420b2f
MK
269 zero = value_from_longest (builtin_type_int, 0);
270 gdb_assert (zero);
271 sbrk = call_function_by_hand (sbrk, 1, &zero);
272 if (sbrk == NULL)
be4d1333
MS
273 return 0;
274 top_of_heap = value_as_long (sbrk);
275
d3420b2f 276 /* Return results. */
be4d1333
MS
277 if (top_of_heap > top_of_data_memory)
278 {
279 *bottom = top_of_data_memory;
280 *top = top_of_heap;
d3420b2f 281 return 1;
be4d1333 282 }
d3420b2f
MK
283
284 /* No additional heap space needs to be saved. */
285 return 0;
be4d1333
MS
286}
287
be4d1333
MS
288static void
289make_output_phdrs (bfd *obfd, asection *osec, void *ignored)
290{
291 int p_flags = 0;
292 int p_type;
293
294 /* FIXME: these constants may only be applicable for ELF. */
20fe79c8 295 if (strncmp (bfd_section_name (obfd, osec), "load", 4) == 0)
be4d1333
MS
296 p_type = PT_LOAD;
297 else
298 p_type = PT_NOTE;
299
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. */
305
d3420b2f 306 bfd_record_phdr (obfd, p_type, 1, p_flags, 0, 0, 0, 0, 1, &osec);
be4d1333
MS
307}
308
cbb83bd1
RM
309static int
310gcore_create_callback (CORE_ADDR vaddr, unsigned long size,
311 int read, int write, int exec, void *data)
be4d1333 312{
cbb83bd1 313 bfd *obfd = data;
be4d1333 314 asection *osec;
cbb83bd1
RM
315 flagword flags = SEC_ALLOC | SEC_HAS_CONTENTS | SEC_LOAD;
316
86fbe6cc
EZ
317 /* If the memory segment has no permissions set, ignore it, otherwise
318 when we later try to access it for read/write, we'll get an error
319 or jam the kernel. */
320 if (read == 0 && write == 0 && exec == 0)
321 {
322 if (info_verbose)
323 {
324 fprintf_filtered (gdb_stdout, "Ignore segment, %s bytes at 0x%s\n",
325 paddr_d (size), paddr_nz (vaddr));
326 }
327
328 return 0;
329 }
330
cbb83bd1
RM
331 if (write == 0)
332 {
333 /* See if this region of memory lies inside a known file on disk.
334 If so, we can avoid copying its contents by clearing SEC_LOAD. */
335 struct objfile *objfile;
336 struct obj_section *objsec;
337
338 ALL_OBJSECTIONS (objfile, objsec)
339 {
340 bfd *abfd = objfile->obfd;
341 asection *asec = objsec->the_bfd_section;
342 bfd_vma align = (bfd_vma) 1 << bfd_get_section_alignment (abfd,
343 asec);
344 bfd_vma start = objsec->addr & -align;
345 bfd_vma end = (objsec->endaddr + align - 1) & -align;
346 /* Match if either the entire memory region lies inside the
347 section (i.e. a mapping covering some pages of a large
348 segment) or the entire section lies inside the memory region
349 (i.e. a mapping covering multiple small sections).
350
351 This BFD was synthesized from reading target memory,
352 we don't want to omit that. */
353 if (((vaddr >= start && vaddr + size <= end)
354 || (start >= vaddr && end <= vaddr + size))
355 && !(bfd_get_file_flags (abfd) & BFD_IN_MEMORY))
356 {
357 flags &= ~SEC_LOAD;
358 goto keep; /* break out of two nested for loops */
359 }
360 }
361
362 keep:
363 flags |= SEC_READONLY;
364 }
365
366 if (exec)
367 flags |= SEC_CODE;
368 else
369 flags |= SEC_DATA;
be4d1333 370
d3420b2f
MK
371 osec = bfd_make_section_anyway (obfd, "load");
372 if (osec == NULL)
be4d1333 373 {
8a3fe4f8 374 warning (_("Couldn't make gcore segment: %s"),
be4d1333 375 bfd_errmsg (bfd_get_error ()));
cbb83bd1 376 return 1;
be4d1333
MS
377 }
378
379 if (info_verbose)
380 {
86fbe6cc
EZ
381 fprintf_filtered (gdb_stdout, "Save segment, %s bytes at 0x%s\n",
382 paddr_d (size), paddr_nz (vaddr));
be4d1333
MS
383 }
384
385 bfd_set_section_size (obfd, osec, size);
cbb83bd1 386 bfd_set_section_vma (obfd, osec, vaddr);
d3420b2f 387 bfd_section_lma (obfd, osec) = 0; /* ??? bfd_set_section_lma? */
cbb83bd1
RM
388 bfd_set_section_flags (obfd, osec, flags);
389 return 0;
be4d1333
MS
390}
391
392static int
d3420b2f
MK
393objfile_find_memory_regions (int (*func) (CORE_ADDR, unsigned long,
394 int, int, int, void *),
be4d1333
MS
395 void *obfd)
396{
d3420b2f 397 /* Use objfile data to create memory sections. */
be4d1333
MS
398 struct objfile *objfile;
399 struct obj_section *objsec;
400 bfd_vma temp_bottom, temp_top;
401
d3420b2f 402 /* Call callback function for each objfile section. */
be4d1333
MS
403 ALL_OBJSECTIONS (objfile, objsec)
404 {
405 bfd *ibfd = objfile->obfd;
406 asection *isec = objsec->the_bfd_section;
407 flagword flags = bfd_get_section_flags (ibfd, isec);
408 int ret;
409
410 if ((flags & SEC_ALLOC) || (flags & SEC_LOAD))
411 {
412 int size = bfd_section_size (ibfd, isec);
413 int ret;
414
cbb83bd1 415 ret = (*func) (objsec->addr, bfd_section_size (ibfd, isec),
d3420b2f
MK
416 1, /* All sections will be readable. */
417 (flags & SEC_READONLY) == 0, /* Writable. */
418 (flags & SEC_CODE) != 0, /* Executable. */
419 obfd);
420 if (ret != 0)
be4d1333
MS
421 return ret;
422 }
423 }
424
d3420b2f 425 /* Make a stack segment. */
be4d1333 426 if (derive_stack_segment (&temp_bottom, &temp_top))
cbb83bd1 427 (*func) (temp_bottom, temp_top - temp_bottom,
d3420b2f
MK
428 1, /* Stack section will be readable. */
429 1, /* Stack section will be writable. */
430 0, /* Stack section will not be executable. */
be4d1333
MS
431 obfd);
432
433 /* Make a heap segment. */
434 if (derive_heap_segment (exec_bfd, &temp_bottom, &temp_top))
d3420b2f
MK
435 (*func) (temp_bottom, temp_top - temp_bottom,
436 1, /* Heap section will be readable. */
437 1, /* Heap section will be writable. */
438 0, /* Heap section will not be executable. */
be4d1333 439 obfd);
d3420b2f 440
be4d1333
MS
441 return 0;
442}
443
444static void
445gcore_copy_callback (bfd *obfd, asection *osec, void *ignored)
446{
447 bfd_size_type size = bfd_section_size (obfd, osec);
448 struct cleanup *old_chain = NULL;
449 void *memhunk;
450
cbb83bd1
RM
451 /* Read-only sections are marked; we don't have to copy their contents. */
452 if ((bfd_get_section_flags (obfd, osec) & SEC_LOAD) == 0)
d3420b2f
MK
453 return;
454
455 /* Only interested in "load" sections. */
20fe79c8 456 if (strncmp ("load", bfd_section_name (obfd, osec), 4) != 0)
d3420b2f 457 return;
be4d1333 458
d3420b2f
MK
459 memhunk = xmalloc (size);
460 /* ??? This is crap since xmalloc should never return NULL. */
461 if (memhunk == NULL)
8a3fe4f8 462 error (_("Not enough memory to create corefile."));
be4d1333
MS
463 old_chain = make_cleanup (xfree, memhunk);
464
cbb83bd1 465 if (target_read_memory (bfd_section_vma (obfd, osec),
be4d1333 466 memhunk, size) != 0)
8a3fe4f8 467 warning (_("Memory read failed for corefile section, %s bytes at 0x%s."),
86fbe6cc 468 paddr_d (size), paddr (bfd_section_vma (obfd, osec)));
be4d1333 469 if (!bfd_set_section_contents (obfd, osec, memhunk, 0, size))
8a3fe4f8 470 warning (_("Failed to write corefile contents (%s)."),
be4d1333
MS
471 bfd_errmsg (bfd_get_error ()));
472
d3420b2f 473 do_cleanups (old_chain); /* Frees MEMHUNK. */
be4d1333
MS
474}
475
476static int
477gcore_memory_sections (bfd *obfd)
478{
479 if (target_find_memory_regions (gcore_create_callback, obfd) != 0)
d3420b2f 480 return 0; /* FIXME: error return/msg? */
be4d1333 481
d3420b2f 482 /* Record phdrs for section-to-segment mapping. */
be4d1333
MS
483 bfd_map_over_sections (obfd, make_output_phdrs, NULL);
484
d3420b2f 485 /* Copy memory region contents. */
be4d1333
MS
486 bfd_map_over_sections (obfd, gcore_copy_callback, NULL);
487
d3420b2f 488 return 1;
be4d1333
MS
489}
490
491void
492_initialize_gcore (void)
493{
1bedd215 494 add_com ("generate-core-file", class_files, gcore_command, _("\
d3420b2f 495Save a core file with the current state of the debugged process.\n\
1bedd215 496Argument is optional filename. Default filename is 'core.<process_id>'."));
be4d1333
MS
497
498 add_com_alias ("gcore", "generate-core-file", class_files, 1);
499 exec_set_find_memory_regions (objfile_find_memory_regions);
500}
This page took 0.471986 seconds and 4 git commands to generate.