Make init_type/arch_type take a size in bits
[external/binutils.git] / gdb / fbsd-tdep.c
1 /* Target-dependent code for FreeBSD, architecture-independent.
2
3    Copyright (C) 2002-2017 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 "auxv.h"
22 #include "gdbcore.h"
23 #include "inferior.h"
24 #include "regcache.h"
25 #include "regset.h"
26 #include "gdbthread.h"
27 #include "xml-syscall.h"
28
29 #include "elf-bfd.h"
30 #include "fbsd-tdep.h"
31
32
33 /* FreeBSD kernels 12.0 and later include a copy of the
34    'ptrace_lwpinfo' structure returned by the PT_LWPINFO ptrace
35    operation in an ELF core note (NT_FREEBSD_PTLWPINFO) for each LWP.
36    The constants below define the offset of field members and flags in
37    this structure used by methods in this file.  Note that the
38    'ptrace_lwpinfo' struct in the note is preceded by a 4 byte integer
39    containing the size of the structure.  */
40
41 #define LWPINFO_OFFSET          0x4
42
43 /* Offsets in ptrace_lwpinfo.  */
44 #define LWPINFO_PL_FLAGS        0x8
45 #define LWPINFO64_PL_SIGINFO    0x30
46 #define LWPINFO32_PL_SIGINFO    0x2c
47
48 /* Flags in pl_flags.  */
49 #define PL_FLAG_SI      0x20    /* siginfo is valid */
50
51 /* Sizes of siginfo_t.  */
52 #define SIZE64_SIGINFO_T        80
53 #define SIZE32_SIGINFO_T        64
54
55 static struct gdbarch_data *fbsd_gdbarch_data_handle;
56
57 struct fbsd_gdbarch_data
58   {
59     struct type *siginfo_type;
60   };
61
62 static void *
63 init_fbsd_gdbarch_data (struct gdbarch *gdbarch)
64 {
65   return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct fbsd_gdbarch_data);
66 }
67
68 static struct fbsd_gdbarch_data *
69 get_fbsd_gdbarch_data (struct gdbarch *gdbarch)
70 {
71   return ((struct fbsd_gdbarch_data *)
72           gdbarch_data (gdbarch, fbsd_gdbarch_data_handle));
73 }
74
75 /* This is how we want PTIDs from core files to be printed.  */
76
77 static const char *
78 fbsd_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
79 {
80   static char buf[80];
81
82   if (ptid_get_lwp (ptid) != 0)
83     {
84       xsnprintf (buf, sizeof buf, "LWP %ld", ptid_get_lwp (ptid));
85       return buf;
86     }
87
88   return normal_pid_to_str (ptid);
89 }
90
91 /* Extract the name assigned to a thread from a core.  Returns the
92    string in a static buffer.  */
93
94 static const char *
95 fbsd_core_thread_name (struct gdbarch *gdbarch, struct thread_info *thr)
96 {
97   static char buf[80];
98   struct bfd_section *section;
99   bfd_size_type size;
100
101   if (ptid_get_lwp (thr->ptid) != 0)
102     {
103       /* FreeBSD includes a NT_FREEBSD_THRMISC note for each thread
104          whose contents are defined by a "struct thrmisc" declared in
105          <sys/procfs.h> on FreeBSD.  The per-thread name is stored as
106          a null-terminated string as the first member of the
107          structure.  Rather than define the full structure here, just
108          extract the null-terminated name from the start of the
109          note.  */
110       thread_section_name section_name (".thrmisc", thr->ptid);
111
112       section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
113       if (section != NULL && bfd_section_size (core_bfd, section) > 0)
114         {
115           /* Truncate the name if it is longer than "buf".  */
116           size = bfd_section_size (core_bfd, section);
117           if (size > sizeof buf - 1)
118             size = sizeof buf - 1;
119           if (bfd_get_section_contents (core_bfd, section, buf, (file_ptr) 0,
120                                         size)
121               && buf[0] != '\0')
122             {
123               buf[size] = '\0';
124
125               /* Note that each thread will report the process command
126                  as its thread name instead of an empty name if a name
127                  has not been set explicitly.  Return a NULL name in
128                  that case.  */
129               if (strcmp (buf, elf_tdata (core_bfd)->core->program) != 0)
130                 return buf;
131             }
132         }
133     }
134
135   return NULL;
136 }
137
138 /* Implement the "core_xfer_siginfo" gdbarch method.  */
139
140 static LONGEST
141 fbsd_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
142                         ULONGEST offset, ULONGEST len)
143 {
144   size_t siginfo_size;
145
146   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
147     siginfo_size = SIZE32_SIGINFO_T;
148   else
149     siginfo_size = SIZE64_SIGINFO_T;
150   if (offset > siginfo_size)
151     return -1;
152
153   thread_section_name section_name (".note.freebsdcore.lwpinfo", inferior_ptid);
154   asection *section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
155   if (section == NULL)
156     return -1;
157
158   gdb_byte buf[4];
159   if (!bfd_get_section_contents (core_bfd, section, buf,
160                                  LWPINFO_OFFSET + LWPINFO_PL_FLAGS, 4))
161     return -1;
162
163   int pl_flags = extract_signed_integer (buf, 4, gdbarch_byte_order (gdbarch));
164   if (!(pl_flags & PL_FLAG_SI))
165     return -1;
166
167   if (offset + len > siginfo_size)
168     len = siginfo_size - offset;
169
170   ULONGEST siginfo_offset;
171   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
172     siginfo_offset = LWPINFO_OFFSET + LWPINFO32_PL_SIGINFO;
173   else
174     siginfo_offset = LWPINFO_OFFSET + LWPINFO64_PL_SIGINFO;
175
176   if (!bfd_get_section_contents (core_bfd, section, readbuf,
177                                  siginfo_offset + offset, len))
178     return -1;
179
180   return len;
181 }
182
183 static int
184 find_signalled_thread (struct thread_info *info, void *data)
185 {
186   if (info->suspend.stop_signal != GDB_SIGNAL_0
187       && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
188     return 1;
189
190   return 0;
191 }
192
193 /* Structure for passing information from
194    fbsd_collect_thread_registers via an iterator to
195    fbsd_collect_regset_section_cb. */
196
197 struct fbsd_collect_regset_section_cb_data
198 {
199   const struct regcache *regcache;
200   bfd *obfd;
201   char *note_data;
202   int *note_size;
203   unsigned long lwp;
204   enum gdb_signal stop_signal;
205   int abort_iteration;
206 };
207
208 static void
209 fbsd_collect_regset_section_cb (const char *sect_name, int size,
210                                 const struct regset *regset,
211                                 const char *human_name, void *cb_data)
212 {
213   char *buf;
214   struct fbsd_collect_regset_section_cb_data *data
215     = (struct fbsd_collect_regset_section_cb_data *) cb_data;
216
217   if (data->abort_iteration)
218     return;
219
220   gdb_assert (regset->collect_regset);
221
222   buf = (char *) xmalloc (size);
223   regset->collect_regset (regset, data->regcache, -1, buf, size);
224
225   /* PRSTATUS still needs to be treated specially.  */
226   if (strcmp (sect_name, ".reg") == 0)
227     data->note_data = (char *) elfcore_write_prstatus
228       (data->obfd, data->note_data, data->note_size, data->lwp,
229        gdb_signal_to_host (data->stop_signal), buf);
230   else
231     data->note_data = (char *) elfcore_write_register_note
232       (data->obfd, data->note_data, data->note_size,
233        sect_name, buf, size);
234   xfree (buf);
235
236   if (data->note_data == NULL)
237     data->abort_iteration = 1;
238 }
239
240 /* Records the thread's register state for the corefile note
241    section.  */
242
243 static char *
244 fbsd_collect_thread_registers (const struct regcache *regcache,
245                                ptid_t ptid, bfd *obfd,
246                                char *note_data, int *note_size,
247                                enum gdb_signal stop_signal)
248 {
249   struct gdbarch *gdbarch = get_regcache_arch (regcache);
250   struct fbsd_collect_regset_section_cb_data data;
251
252   data.regcache = regcache;
253   data.obfd = obfd;
254   data.note_data = note_data;
255   data.note_size = note_size;
256   data.stop_signal = stop_signal;
257   data.abort_iteration = 0;
258   data.lwp = ptid_get_lwp (ptid);
259
260   gdbarch_iterate_over_regset_sections (gdbarch,
261                                         fbsd_collect_regset_section_cb,
262                                         &data, regcache);
263   return data.note_data;
264 }
265
266 struct fbsd_corefile_thread_data
267 {
268   struct gdbarch *gdbarch;
269   bfd *obfd;
270   char *note_data;
271   int *note_size;
272   enum gdb_signal stop_signal;
273 };
274
275 /* Records the thread's register state for the corefile note
276    section.  */
277
278 static void
279 fbsd_corefile_thread (struct thread_info *info,
280                       struct fbsd_corefile_thread_data *args)
281 {
282   struct regcache *regcache;
283
284   regcache = get_thread_arch_regcache (info->ptid, args->gdbarch);
285
286   target_fetch_registers (regcache, -1);
287
288   args->note_data = fbsd_collect_thread_registers
289     (regcache, info->ptid, args->obfd, args->note_data,
290      args->note_size, args->stop_signal);
291 }
292
293 /* Create appropriate note sections for a corefile, returning them in
294    allocated memory.  */
295
296 static char *
297 fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
298 {
299   struct fbsd_corefile_thread_data thread_args;
300   char *note_data = NULL;
301   Elf_Internal_Ehdr *i_ehdrp;
302   struct thread_info *curr_thr, *signalled_thr, *thr;
303
304   /* Put a "FreeBSD" label in the ELF header.  */
305   i_ehdrp = elf_elfheader (obfd);
306   i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
307
308   gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch));
309
310   if (get_exec_file (0))
311     {
312       const char *fname = lbasename (get_exec_file (0));
313       char *psargs = xstrdup (fname);
314
315       if (get_inferior_args ())
316         psargs = reconcat (psargs, psargs, " ", get_inferior_args (),
317                            (char *) NULL);
318
319       note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
320                                           fname, psargs);
321     }
322
323   /* Thread register information.  */
324   TRY
325     {
326       update_thread_list ();
327     }
328   CATCH (e, RETURN_MASK_ERROR)
329     {
330       exception_print (gdb_stderr, e);
331     }
332   END_CATCH
333
334   /* Like the kernel, prefer dumping the signalled thread first.
335      "First thread" is what tools use to infer the signalled thread.
336      In case there's more than one signalled thread, prefer the
337      current thread, if it is signalled.  */
338   curr_thr = inferior_thread ();
339   if (curr_thr->suspend.stop_signal != GDB_SIGNAL_0)
340     signalled_thr = curr_thr;
341   else
342     {
343       signalled_thr = iterate_over_threads (find_signalled_thread, NULL);
344       if (signalled_thr == NULL)
345         signalled_thr = curr_thr;
346     }
347
348   thread_args.gdbarch = gdbarch;
349   thread_args.obfd = obfd;
350   thread_args.note_data = note_data;
351   thread_args.note_size = note_size;
352   thread_args.stop_signal = signalled_thr->suspend.stop_signal;
353
354   fbsd_corefile_thread (signalled_thr, &thread_args);
355   ALL_NON_EXITED_THREADS (thr)
356     {
357       if (thr == signalled_thr)
358         continue;
359       if (ptid_get_pid (thr->ptid) != ptid_get_pid (inferior_ptid))
360         continue;
361
362       fbsd_corefile_thread (thr, &thread_args);
363     }
364
365   note_data = thread_args.note_data;
366
367   return note_data;
368 }
369
370 /* Print descriptions of FreeBSD-specific AUXV entries to FILE.  */
371
372 static void
373 fbsd_print_auxv_entry (struct gdbarch *gdbarch, struct ui_file *file,
374                        CORE_ADDR type, CORE_ADDR val)
375 {
376   const char *name;
377   const char *description;
378   enum auxv_format format;
379
380   switch (type)
381     {
382 #define _TAGNAME(tag) #tag
383 #define TAGNAME(tag) _TAGNAME(AT_##tag)
384 #define TAG(tag, text, kind) \
385       case AT_FREEBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
386       TAG (EXECPATH, _("Executable path"), AUXV_FORMAT_STR);
387       TAG (CANARY, _("Canary for SSP"), AUXV_FORMAT_HEX);
388       TAG (CANARYLEN, ("Length of the SSP canary"), AUXV_FORMAT_DEC);
389       TAG (OSRELDATE, _("OSRELDATE"), AUXV_FORMAT_DEC);
390       TAG (NCPUS, _("Number of CPUs"), AUXV_FORMAT_DEC);
391       TAG (PAGESIZES, _("Pagesizes"), AUXV_FORMAT_HEX);
392       TAG (PAGESIZESLEN, _("Number of pagesizes"), AUXV_FORMAT_DEC);
393       TAG (TIMEKEEP, _("Pointer to timehands"), AUXV_FORMAT_HEX);
394       TAG (STACKPROT, _("Initial stack protection"), AUXV_FORMAT_HEX);
395     default:
396       default_print_auxv_entry (gdbarch, file, type, val);
397       return;
398     }
399
400   fprint_auxv_entry (file, name, description, format, type, val);
401 }
402
403 /* Implement the "get_siginfo_type" gdbarch method.  */
404
405 static struct type *
406 fbsd_get_siginfo_type (struct gdbarch *gdbarch)
407 {
408   struct fbsd_gdbarch_data *fbsd_gdbarch_data;
409   struct type *int_type, *int32_type, *uint32_type, *long_type, *void_ptr_type;
410   struct type *uid_type, *pid_type;
411   struct type *sigval_type, *reason_type;
412   struct type *siginfo_type;
413   struct type *type;
414
415   fbsd_gdbarch_data = get_fbsd_gdbarch_data (gdbarch);
416   if (fbsd_gdbarch_data->siginfo_type != NULL)
417     return fbsd_gdbarch_data->siginfo_type;
418
419   int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
420                                 0, "int");
421   int32_type = arch_integer_type (gdbarch, 32, 0, "int32_t");
422   uint32_type = arch_integer_type (gdbarch, 32, 1, "uint32_t");
423   long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
424                                  0, "long");
425   void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
426
427   /* union sigval */
428   sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
429   TYPE_NAME (sigval_type) = xstrdup ("sigval");
430   append_composite_type_field (sigval_type, "sival_int", int_type);
431   append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
432
433   /* __pid_t */
434   pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
435                         TYPE_LENGTH (int32_type) * TARGET_CHAR_BIT, "__pid_t");
436   TYPE_TARGET_TYPE (pid_type) = int32_type;
437   TYPE_TARGET_STUB (pid_type) = 1;
438
439   /* __uid_t */
440   uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
441                         TYPE_LENGTH (uint32_type) * TARGET_CHAR_BIT,
442                         "__uid_t");
443   TYPE_TARGET_TYPE (uid_type) = uint32_type;
444   TYPE_TARGET_STUB (uid_type) = 1;
445
446   /* _reason */
447   reason_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
448
449   /* _fault */
450   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
451   append_composite_type_field (type, "si_trapno", int_type);
452   append_composite_type_field (reason_type, "_fault", type);
453
454   /* _timer */
455   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
456   append_composite_type_field (type, "si_timerid", int_type);
457   append_composite_type_field (type, "si_overrun", int_type);
458   append_composite_type_field (reason_type, "_timer", type);
459
460   /* _mesgq */
461   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
462   append_composite_type_field (type, "si_mqd", int_type);
463   append_composite_type_field (reason_type, "_mesgq", type);
464
465   /* _poll */
466   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
467   append_composite_type_field (type, "si_band", long_type);
468   append_composite_type_field (reason_type, "_poll", type);
469
470   /* __spare__ */
471   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
472   append_composite_type_field (type, "__spare1__", long_type);
473   append_composite_type_field (type, "__spare2__",
474                                init_vector_type (int_type, 7));
475   append_composite_type_field (reason_type, "__spare__", type);
476
477   /* struct siginfo */
478   siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
479   TYPE_NAME (siginfo_type) = xstrdup ("siginfo");
480   append_composite_type_field (siginfo_type, "si_signo", int_type);
481   append_composite_type_field (siginfo_type, "si_errno", int_type);
482   append_composite_type_field (siginfo_type, "si_code", int_type);
483   append_composite_type_field (siginfo_type, "si_pid", pid_type);
484   append_composite_type_field (siginfo_type, "si_uid", uid_type);
485   append_composite_type_field (siginfo_type, "si_status", int_type);
486   append_composite_type_field (siginfo_type, "si_addr", void_ptr_type);
487   append_composite_type_field (siginfo_type, "si_value", sigval_type);
488   append_composite_type_field (siginfo_type, "_reason", reason_type);
489
490   fbsd_gdbarch_data->siginfo_type = siginfo_type;
491
492   return siginfo_type;
493 }
494
495 /* Implement the "get_syscall_number" gdbarch method.  */
496
497 static LONGEST
498 fbsd_get_syscall_number (struct gdbarch *gdbarch,
499                          ptid_t ptid)
500 {
501
502   /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
503      native targets fetch the system call number from the
504      'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
505      However, system call catching requires this function to be
506      set.  */
507
508   internal_error (__FILE__, __LINE__, _("fbsd_get_sycall_number called"));
509 }
510
511 /* To be called from GDB_OSABI_FREEBSD handlers. */
512
513 void
514 fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
515 {
516   set_gdbarch_core_pid_to_str (gdbarch, fbsd_core_pid_to_str);
517   set_gdbarch_core_thread_name (gdbarch, fbsd_core_thread_name);
518   set_gdbarch_core_xfer_siginfo (gdbarch, fbsd_core_xfer_siginfo);
519   set_gdbarch_make_corefile_notes (gdbarch, fbsd_make_corefile_notes);
520   set_gdbarch_print_auxv_entry (gdbarch, fbsd_print_auxv_entry);
521   set_gdbarch_get_siginfo_type (gdbarch, fbsd_get_siginfo_type);
522
523   /* `catch syscall' */
524   set_xml_syscall_file_name (gdbarch, "syscalls/freebsd.xml");
525   set_gdbarch_get_syscall_number (gdbarch, fbsd_get_syscall_number);
526 }
527
528 void
529 _initialize_fbsd_tdep (void)
530 {
531   fbsd_gdbarch_data_handle =
532     gdbarch_data_register_post_init (init_fbsd_gdbarch_data);
533 }