gdb/fbsd-tdep.c - gdb

Data types defined

Functions defined

Source code

  1. /* Target-dependent code for FreeBSD, architecture-independent.

  2.    Copyright (C) 2002-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "gdbcore.h"
  16. #include "inferior.h"
  17. #include "regcache.h"
  18. #include "regset.h"
  19. #include "gdbthread.h"

  20. #include "elf-bfd.h"
  21. #include "fbsd-tdep.h"


  22. static int
  23. find_signalled_thread (struct thread_info *info, void *data)
  24. {
  25.   if (info->suspend.stop_signal != GDB_SIGNAL_0
  26.       && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
  27.     return 1;

  28.   return 0;
  29. }

  30. static enum gdb_signal
  31. find_stop_signal (void)
  32. {
  33.   struct thread_info *info =
  34.     iterate_over_threads (find_signalled_thread, NULL);

  35.   if (info)
  36.     return info->suspend.stop_signal;
  37.   else
  38.     return GDB_SIGNAL_0;
  39. }

  40. struct fbsd_collect_regset_section_cb_data
  41. {
  42.   const struct regcache *regcache;
  43.   bfd *obfd;
  44.   char *note_data;
  45.   int *note_size;
  46. };

  47. static void
  48. fbsd_collect_regset_section_cb (const char *sect_name, int size,
  49.                                 const struct regset *regset,
  50.                                 const char *human_name, void *cb_data)
  51. {
  52.   char *buf;
  53.   struct fbsd_collect_regset_section_cb_data *data = cb_data;

  54.   gdb_assert (regset->collect_regset);

  55.   buf = xmalloc (size);
  56.   regset->collect_regset (regset, data->regcache, -1, buf, size);

  57.   /* PRSTATUS still needs to be treated specially.  */
  58.   if (strcmp (sect_name, ".reg") == 0)
  59.     data->note_data = (char *) elfcore_write_prstatus
  60.       (data->obfd, data->note_data, data->note_size,
  61.        ptid_get_pid (inferior_ptid), find_stop_signal (), buf);
  62.   else
  63.     data->note_data = (char *) elfcore_write_register_note
  64.       (data->obfd, data->note_data, data->note_size,
  65.        sect_name, buf, size);
  66.   xfree (buf);
  67. }

  68. /* Create appropriate note sections for a corefile, returning them in
  69.    allocated memory.  */

  70. static char *
  71. fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
  72. {
  73.   const struct regcache *regcache = get_current_regcache ();
  74.   char *note_data;
  75.   Elf_Internal_Ehdr *i_ehdrp;
  76.   struct fbsd_collect_regset_section_cb_data data;

  77.   /* Put a "FreeBSD" label in the ELF header.  */
  78.   i_ehdrp = elf_elfheader (obfd);
  79.   i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;

  80.   gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch));

  81.   data.regcache = regcache;
  82.   data.obfd = obfd;
  83.   data.note_data = NULL;
  84.   data.note_size = note_size;
  85.   gdbarch_iterate_over_regset_sections (gdbarch,
  86.                                         fbsd_collect_regset_section_cb,
  87.                                         &data, regcache);
  88.   note_data = data.note_data;

  89.   if (get_exec_file (0))
  90.     {
  91.       const char *fname = lbasename (get_exec_file (0));
  92.       char *psargs = xstrdup (fname);

  93.       if (get_inferior_args ())
  94.         psargs = reconcat (psargs, psargs, " ", get_inferior_args (),
  95.                            (char *) NULL);

  96.       note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
  97.                                           fname, psargs);
  98.     }

  99.   return note_data;
  100. }

  101. /* To be called from GDB_OSABI_FREEBSD_ELF handlers. */

  102. void
  103. fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  104. {
  105.   set_gdbarch_make_corefile_notes (gdbarch, fbsd_make_corefile_notes);
  106. }