Add multi-arch capable 'fbsd_make_corefile_notes' variant
[external/binutils.git] / gdb / fbsd-tdep.c
1 /* Target-dependent code for FreeBSD, architecture-independent.
2
3    Copyright (C) 2002-2014 Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "gdbcore.h"
22 #include "inferior.h"
23 #include "regcache.h"
24 #include "regset.h"
25 #include "gdbthread.h"
26
27 #include "gdb_assert.h"
28 #include <string.h>
29
30 #include "elf-bfd.h"
31 #include "fbsd-tdep.h"
32
33
34 static int
35 find_signalled_thread (struct thread_info *info, void *data)
36 {
37   if (info->suspend.stop_signal != GDB_SIGNAL_0
38       && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
39     return 1;
40
41   return 0;
42 }
43
44 static enum gdb_signal
45 find_stop_signal (void)
46 {
47   struct thread_info *info =
48     iterate_over_threads (find_signalled_thread, NULL);
49
50   if (info)
51     return info->suspend.stop_signal;
52   else
53     return GDB_SIGNAL_0;
54 }
55
56 struct fbsd_collect_regset_section_cb_data
57 {
58   const struct regcache *regcache;
59   bfd *obfd;
60   char *note_data;
61   int *note_size;
62 };
63
64 static void
65 fbsd_collect_regset_section_cb (const char *sect_name, int size,
66                                 const struct regset *regset,
67                                 const char *human_name, void *cb_data)
68 {
69   char *buf;
70   struct fbsd_collect_regset_section_cb_data *data = cb_data;
71
72   gdb_assert (regset->collect_regset);
73
74   buf = xmalloc (size);
75   regset->collect_regset (regset, data->regcache, -1, buf, size);
76
77   /* PRSTATUS still needs to be treated specially.  */
78   if (strcmp (sect_name, ".reg") == 0)
79     data->note_data = (char *) elfcore_write_prstatus
80       (data->obfd, data->note_data, data->note_size,
81        ptid_get_pid (inferior_ptid), find_stop_signal (), buf);
82   else
83     data->note_data = (char *) elfcore_write_register_note
84       (data->obfd, data->note_data, data->note_size,
85        sect_name, buf, size);
86   xfree (buf);
87 }
88
89 /* Create appropriate note sections for a corefile, returning them in
90    allocated memory.  */
91
92 static char *
93 fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
94 {
95   const struct regcache *regcache = get_current_regcache ();
96   char *note_data;
97   Elf_Internal_Ehdr *i_ehdrp;
98   struct fbsd_collect_regset_section_cb_data data;
99
100   /* Put a "FreeBSD" label in the ELF header.  */
101   i_ehdrp = elf_elfheader (obfd);
102   i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
103
104   gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch));
105
106   data.regcache = regcache;
107   data.obfd = obfd;
108   data.note_data = NULL;
109   data.note_size = note_size;
110   gdbarch_iterate_over_regset_sections (gdbarch,
111                                         fbsd_collect_regset_section_cb,
112                                         &data, regcache);
113   note_data = data.note_data;
114
115   if (get_exec_file (0))
116     {
117       const char *fname = lbasename (get_exec_file (0));
118       char *psargs = xstrdup (fname);
119
120       if (get_inferior_args ())
121         psargs = reconcat (psargs, psargs, " ", get_inferior_args (),
122                            (char *) NULL);
123
124       note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
125                                           fname, psargs);
126     }
127
128   return note_data;
129 }
130
131 /* To be called from GDB_OSABI_FREEBSD_ELF handlers. */
132
133 void
134 fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
135 {
136   set_gdbarch_make_corefile_notes (gdbarch, fbsd_make_corefile_notes);
137 }