~ubuntu-branches/debian/jessie/gdb/jessie

« back to all changes in this revision

Viewing changes to gdb/gcore.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Jacobowitz
  • Date: 2010-03-20 01:21:29 UTC
  • mfrom: (1.3.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20100320012129-t7h25y8zgr8c2369
Tags: 7.1-1
* New upstream release, including:
  - PIE support (Closes: #346409).
  - C++ improvements, including static_cast<> et al, namespace imports,
    and bug fixes in printing virtual base classes.
  - Multi-program debugging.  One GDB can now debug multiple programs
    at the same time.
  - Python scripting improvements, including gdb.parse_and_eval.
  - Updated MIPS Linux signal frame layout (Closes: #570875).
  - No internal error stepping over _dl_debug_state (Closes: #569551).
* Update to Standards-Version: 3.8.4 (no changes required).
* Include more relevant (and smaller) docs in the gdbserver package
  (Closes: #571132).
* Do not duplicate documentation in gdb64, gdb-source, and libgdb-dev.
* Fix crash when switching into TUI mode (Closes: #568489).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Generate a core file for the inferior process.
2
2
 
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.
5
5
 
6
6
   This file is part of GDB.
24
24
#include "inferior.h"
25
25
#include "gdbcore.h"
26
26
#include "objfiles.h"
 
27
#include "solib.h"
27
28
#include "symfile.h"
28
 
 
 
29
#include "arch-utils.h"
 
30
#include "completer.h"
 
31
#include "gcore.h"
29
32
#include "cli/cli-decode.h"
30
 
 
31
33
#include "gdb_assert.h"
 
34
#include <fcntl.h>
 
35
#include "regcache.h"
 
36
#include "regset.h"
32
37
 
33
38
/* The largest amount of memory to read from the target at once.  We
34
39
   must throttle it to limit the amount of memory used by GDB during
40
45
static unsigned long default_gcore_mach (void);
41
46
static int gcore_memory_sections (bfd *);
42
47
 
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.  */
44
50
 
45
 
static void
46
 
gcore_command (char *args, int from_tty)
 
51
bfd *
 
52
create_gcore_bfd (char *filename)
47
53
{
48
 
  struct cleanup *old_chain;
49
 
  char *corefilename, corefilename_buffer[40];
50
 
  asection *note_sec = NULL;
51
 
  bfd *obfd;
52
 
  void *note_data = NULL;
53
 
  int note_size = 0;
54
 
 
55
 
  /* No use generating a corefile without a target process.  */
56
 
  if (!target_has_execution)
57
 
    noprocess ();
58
 
 
59
 
  if (args && *args)
60
 
    corefilename = args;
61
 
  else
62
 
    {
63
 
      /* Default corefile name is "core.PID".  */
64
 
      sprintf (corefilename_buffer, "core.%d", PIDGET (inferior_ptid));
65
 
      corefilename = corefilename_buffer;
66
 
    }
67
 
 
68
 
  if (info_verbose)
69
 
    fprintf_filtered (gdb_stdout,
70
 
                      "Opening corefile '%s' for output.\n", corefilename);
71
 
 
72
 
  /* Open the output file.  */
73
 
  obfd = bfd_openw (corefilename, default_gcore_target ());
 
54
  bfd *obfd = bfd_openw (filename, default_gcore_target ());
74
55
  if (!obfd)
75
 
    error (_("Failed to open '%s' for output."), corefilename);
76
 
 
77
 
  /* Need a cleanup that will close the file (FIXME: delete it?).  */
78
 
  old_chain = make_cleanup_bfd_close (obfd);
79
 
 
 
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 ());
 
59
  return obfd;
 
60
}
 
61
 
 
62
/* write_gcore_file -- helper for gcore_command (exported).
 
63
   Compose and write the corefile data to the core file.  */
 
64
 
 
65
 
 
66
void
 
67
write_gcore_file (bfd *obfd)
 
68
{
 
69
  void *note_data = NULL;
 
70
  int note_size = 0;
 
71
  asection *note_sec = NULL;
82
72
 
83
73
  /* An external target method must build the notes section.  */
84
74
  note_data = target_make_corefile_notes (obfd, &note_size);
107
97
  if (note_data != NULL && note_size != 0)
108
98
    {
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 ()));
111
 
    }
 
100
        warning (_("writing note section (%s)"), 
 
101
                 bfd_errmsg (bfd_get_error ()));
 
102
    }
 
103
}
 
104
 
 
105
static void
 
106
do_bfd_delete_cleanup (void *arg)
 
107
{
 
108
  bfd *obfd = arg;
 
109
  const char *filename = obfd->filename;
 
110
 
 
111
  bfd_close (arg);
 
112
  unlink (filename);
 
113
}
 
114
 
 
115
/* gcore_command -- implements the 'gcore' command.
 
116
   Generate a core file from the inferior process.  */
 
117
 
 
118
static void
 
119
gcore_command (char *args, int from_tty)
 
120
{
 
121
  struct cleanup *old_chain;
 
122
  char *corefilename, corefilename_buffer[40];
 
123
  bfd *obfd;
 
124
 
 
125
  /* No use generating a corefile without a target process.  */
 
126
  if (!target_has_execution)
 
127
    noprocess ();
 
128
 
 
129
  if (args && *args)
 
130
    corefilename = args;
 
131
  else
 
132
    {
 
133
      /* Default corefile name is "core.PID".  */
 
134
      sprintf (corefilename_buffer, "core.%d", PIDGET (inferior_ptid));
 
135
      corefilename = corefilename_buffer;
 
136
    }
 
137
 
 
138
  if (info_verbose)
 
139
    fprintf_filtered (gdb_stdout,
 
140
                      "Opening corefile '%s' for output.\n", corefilename);
 
141
 
 
142
  /* Open the output file.  */
 
143
  obfd = create_gcore_bfd (corefilename);
 
144
 
 
145
  /* Need a cleanup that will close and delete the file.  */
 
146
  old_chain = make_cleanup (do_bfd_delete_cleanup, obfd);
 
147
 
 
148
  /* Call worker function.  */
 
149
  write_gcore_file (obfd);
112
150
 
113
151
  /* Succeeded.  */
114
152
  fprintf_filtered (gdb_stdout, "Saved corefile %s\n", corefilename);
115
153
 
116
 
  /* Clean-ups will close the output file and free malloc memory.  */
117
 
  do_cleanups (old_chain);
118
 
  return;
 
154
  discard_cleanups (old_chain);
 
155
  bfd_close (obfd);
119
156
}
120
157
 
121
158
static unsigned long
212
249
  return 1;
213
250
}
214
251
 
 
252
/* call_target_sbrk --
 
253
   helper function for derive_heap_segment.  */
 
254
 
 
255
static bfd_vma
 
256
call_target_sbrk (int sbrk_arg)
 
257
{
 
258
  struct objfile *sbrk_objf;
 
259
  struct gdbarch *gdbarch;
 
260
  bfd_vma top_of_heap;
 
261
  struct value *target_sbrk_arg;
 
262
  struct value *sbrk_fn, *ret;
 
263
  bfd_vma tmp;
 
264
 
 
265
  if (lookup_minimal_symbol ("sbrk", NULL, NULL) != NULL)
 
266
    {
 
267
      sbrk_fn = find_function_in_inferior ("sbrk", &sbrk_objf);
 
268
      if (sbrk_fn == NULL)
 
269
        return (bfd_vma) 0;
 
270
    }
 
271
  else if (lookup_minimal_symbol ("_sbrk", NULL, NULL) != NULL)
 
272
    {
 
273
      sbrk_fn = find_function_in_inferior ("_sbrk", &sbrk_objf);
 
274
      if (sbrk_fn == NULL)
 
275
        return (bfd_vma) 0;
 
276
    }
 
277
  else
 
278
    return (bfd_vma) 0;
 
279
 
 
280
  gdbarch = get_objfile_arch (sbrk_objf);
 
281
  target_sbrk_arg = value_from_longest (builtin_type (gdbarch)->builtin_int, 
 
282
                                        sbrk_arg);
 
283
  gdb_assert (target_sbrk_arg);
 
284
  ret = call_function_by_hand (sbrk_fn, 1, &target_sbrk_arg);
 
285
  if (ret == NULL)
 
286
    return (bfd_vma) 0;
 
287
 
 
288
  tmp = value_as_long (ret);
 
289
  if ((LONGEST) tmp <= 0 || (LONGEST) tmp == 0xffffffff)
 
290
    return (bfd_vma) 0;
 
291
 
 
292
  top_of_heap = tmp;
 
293
  return top_of_heap;
 
294
}
 
295
 
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.  */
219
300
static int
220
301
derive_heap_segment (bfd *abfd, bfd_vma *bottom, bfd_vma *top)
221
302
{
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;
229
308
  asection *sec;
230
309
 
259
338
        }
260
339
    }
261
340
 
262
 
  /* Now get the top-of-heap by calling sbrk in the inferior.  */
263
 
  if (lookup_minimal_symbol ("sbrk", NULL, NULL) != NULL)
264
 
    {
265
 
      sbrk = find_function_in_inferior ("sbrk", &sbrk_objf);
266
 
      if (sbrk == NULL)
267
 
        return 0;
268
 
    }
269
 
  else if (lookup_minimal_symbol ("_sbrk", NULL, NULL) != NULL)
270
 
    {
271
 
      sbrk = find_function_in_inferior ("_sbrk", &sbrk_objf);
272
 
      if (sbrk == NULL)
273
 
        return 0;
274
 
    }
275
 
  else
276
 
    return 0;
277
 
 
278
 
  gdbarch = get_objfile_arch (sbrk_objf);
279
 
  zero = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
280
 
  gdb_assert (zero);
281
 
  sbrk = call_function_by_hand (sbrk, 1, &zero);
282
 
  if (sbrk == NULL)
283
 
    return 0;
284
 
  top_of_heap = value_as_long (sbrk);
 
341
  top_of_heap = call_target_sbrk (0);
 
342
  if (top_of_heap == (bfd_vma) 0)
 
343
    return 0;
285
344
 
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)
300
359
{
301
360
  int p_flags = 0;
302
 
  int p_type;
 
361
  int p_type = 0;
303
362
 
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;
307
 
  else
 
366
  else if (strncmp (bfd_section_name (obfd, osec), "note", 4) == 0)
308
367
    p_type = PT_NOTE;
 
368
  else
 
369
    p_type = PT_NULL;
309
370
 
310
371
  p_flags |= PF_R;      /* Segment is readable.  */
311
372
  if (!(bfd_get_section_flags (obfd, osec) & SEC_READONLY))
338
399
      return 0;
339
400
    }
340
401
 
341
 
  if (write == 0)
 
402
  if (write == 0 && !solib_keep_data_in_core (vaddr, size))
342
403
    {
343
404
      /* See if this region of memory lies inside a known file on disk.
344
405
         If so, we can avoid copying its contents by clearing SEC_LOAD.  */
469
530
 
470
531
  size = min (total_size, MAX_COPY_BYTES);
471
532
  memhunk = xmalloc (size);
472
 
  /* ??? This is crap since xmalloc should never return NULL.  */
473
 
  if (memhunk == NULL)
474
 
    error (_("Not enough memory to create corefile."));
475
533
  old_chain = make_cleanup (xfree, memhunk);
476
534
 
477
535
  while (total_size > 0)