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