1
1
/* Generate a core file for the inferior process.
3
Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3
Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4
4
Free Software Foundation, Inc.
6
6
This file is part of GDB.
40
45
static unsigned long default_gcore_mach (void);
41
46
static int gcore_memory_sections (bfd *);
43
/* Generate a core file from the inferior process. */
48
/* create_gcore_bfd -- helper for gcore_command (exported).
49
Open a new bfd core file for output, and return the handle. */
46
gcore_command (char *args, int from_tty)
52
create_gcore_bfd (char *filename)
48
struct cleanup *old_chain;
49
char *corefilename, corefilename_buffer[40];
50
asection *note_sec = NULL;
52
void *note_data = NULL;
55
/* No use generating a corefile without a target process. */
56
if (!target_has_execution)
63
/* Default corefile name is "core.PID". */
64
sprintf (corefilename_buffer, "core.%d", PIDGET (inferior_ptid));
65
corefilename = corefilename_buffer;
69
fprintf_filtered (gdb_stdout,
70
"Opening corefile '%s' for output.\n", corefilename);
72
/* Open the output file. */
73
obfd = bfd_openw (corefilename, default_gcore_target ());
54
bfd *obfd = bfd_openw (filename, default_gcore_target ());
75
error (_("Failed to open '%s' for output."), corefilename);
77
/* Need a cleanup that will close the file (FIXME: delete it?). */
78
old_chain = make_cleanup_bfd_close (obfd);
56
error (_("Failed to open '%s' for output."), filename);
80
57
bfd_set_format (obfd, bfd_core);
81
58
bfd_set_arch_mach (obfd, default_gcore_arch (), default_gcore_mach ());
62
/* write_gcore_file -- helper for gcore_command (exported).
63
Compose and write the corefile data to the core file. */
67
write_gcore_file (bfd *obfd)
69
void *note_data = NULL;
71
asection *note_sec = NULL;
83
73
/* An external target method must build the notes section. */
84
74
note_data = target_make_corefile_notes (obfd, ¬e_size);
107
97
if (note_data != NULL && note_size != 0)
109
99
if (!bfd_set_section_contents (obfd, note_sec, note_data, 0, note_size))
110
warning (_("writing note section (%s)"), bfd_errmsg (bfd_get_error ()));
100
warning (_("writing note section (%s)"),
101
bfd_errmsg (bfd_get_error ()));
106
do_bfd_delete_cleanup (void *arg)
109
const char *filename = obfd->filename;
115
/* gcore_command -- implements the 'gcore' command.
116
Generate a core file from the inferior process. */
119
gcore_command (char *args, int from_tty)
121
struct cleanup *old_chain;
122
char *corefilename, corefilename_buffer[40];
125
/* No use generating a corefile without a target process. */
126
if (!target_has_execution)
133
/* Default corefile name is "core.PID". */
134
sprintf (corefilename_buffer, "core.%d", PIDGET (inferior_ptid));
135
corefilename = corefilename_buffer;
139
fprintf_filtered (gdb_stdout,
140
"Opening corefile '%s' for output.\n", corefilename);
142
/* Open the output file. */
143
obfd = create_gcore_bfd (corefilename);
145
/* Need a cleanup that will close and delete the file. */
146
old_chain = make_cleanup (do_bfd_delete_cleanup, obfd);
148
/* Call worker function. */
149
write_gcore_file (obfd);
114
152
fprintf_filtered (gdb_stdout, "Saved corefile %s\n", corefilename);
116
/* Clean-ups will close the output file and free malloc memory. */
117
do_cleanups (old_chain);
154
discard_cleanups (old_chain);
121
158
static unsigned long
252
/* call_target_sbrk --
253
helper function for derive_heap_segment. */
256
call_target_sbrk (int sbrk_arg)
258
struct objfile *sbrk_objf;
259
struct gdbarch *gdbarch;
261
struct value *target_sbrk_arg;
262
struct value *sbrk_fn, *ret;
265
if (lookup_minimal_symbol ("sbrk", NULL, NULL) != NULL)
267
sbrk_fn = find_function_in_inferior ("sbrk", &sbrk_objf);
271
else if (lookup_minimal_symbol ("_sbrk", NULL, NULL) != NULL)
273
sbrk_fn = find_function_in_inferior ("_sbrk", &sbrk_objf);
280
gdbarch = get_objfile_arch (sbrk_objf);
281
target_sbrk_arg = value_from_longest (builtin_type (gdbarch)->builtin_int,
283
gdb_assert (target_sbrk_arg);
284
ret = call_function_by_hand (sbrk_fn, 1, &target_sbrk_arg);
288
tmp = value_as_long (ret);
289
if ((LONGEST) tmp <= 0 || (LONGEST) tmp == 0xffffffff)
215
296
/* Derive a reasonable heap segment for ABFD by looking at sbrk and
216
297
the static data sections. Store its limits in *BOTTOM and *TOP.
217
298
Return non-zero if successful. */
220
301
derive_heap_segment (bfd *abfd, bfd_vma *bottom, bfd_vma *top)
222
struct objfile *sbrk_objf;
223
303
struct gdbarch *gdbarch;
224
304
bfd_vma top_of_data_memory = 0;
225
305
bfd_vma top_of_heap = 0;
226
306
bfd_size_type sec_size;
227
struct value *zero, *sbrk;
228
307
bfd_vma sec_vaddr;
262
/* Now get the top-of-heap by calling sbrk in the inferior. */
263
if (lookup_minimal_symbol ("sbrk", NULL, NULL) != NULL)
265
sbrk = find_function_in_inferior ("sbrk", &sbrk_objf);
269
else if (lookup_minimal_symbol ("_sbrk", NULL, NULL) != NULL)
271
sbrk = find_function_in_inferior ("_sbrk", &sbrk_objf);
278
gdbarch = get_objfile_arch (sbrk_objf);
279
zero = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
281
sbrk = call_function_by_hand (sbrk, 1, &zero);
284
top_of_heap = value_as_long (sbrk);
341
top_of_heap = call_target_sbrk (0);
342
if (top_of_heap == (bfd_vma) 0)
286
345
/* Return results. */
287
346
if (top_of_heap > top_of_data_memory)
299
358
make_output_phdrs (bfd *obfd, asection *osec, void *ignored)
304
363
/* FIXME: these constants may only be applicable for ELF. */
305
364
if (strncmp (bfd_section_name (obfd, osec), "load", 4) == 0)
306
365
p_type = PT_LOAD;
366
else if (strncmp (bfd_section_name (obfd, osec), "note", 4) == 0)
308
367
p_type = PT_NOTE;
310
371
p_flags |= PF_R; /* Segment is readable. */
311
372
if (!(bfd_get_section_flags (obfd, osec) & SEC_READONLY))
470
531
size = min (total_size, MAX_COPY_BYTES);
471
532
memhunk = xmalloc (size);
472
/* ??? This is crap since xmalloc should never return NULL. */
474
error (_("Not enough memory to create corefile."));
475
533
old_chain = make_cleanup (xfree, memhunk);
477
535
while (total_size > 0)