1 /* Target-dependent code for FreeBSD, architecture-independent.
3 Copyright (C) 2002-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
26 #include "gdbthread.h"
27 #include "xml-syscall.h"
30 #include "fbsd-tdep.h"
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. */
41 #define LWPINFO_OFFSET 0x4
43 /* Offsets in ptrace_lwpinfo. */
44 #define LWPINFO_PL_FLAGS 0x8
45 #define LWPINFO64_PL_SIGINFO 0x30
46 #define LWPINFO32_PL_SIGINFO 0x2c
48 /* Flags in pl_flags. */
49 #define PL_FLAG_SI 0x20 /* siginfo is valid */
51 /* Sizes of siginfo_t. */
52 #define SIZE64_SIGINFO_T 80
53 #define SIZE32_SIGINFO_T 64
55 static struct gdbarch_data *fbsd_gdbarch_data_handle;
57 struct fbsd_gdbarch_data
59 struct type *siginfo_type;
63 init_fbsd_gdbarch_data (struct gdbarch *gdbarch)
65 return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct fbsd_gdbarch_data);
68 static struct fbsd_gdbarch_data *
69 get_fbsd_gdbarch_data (struct gdbarch *gdbarch)
71 return ((struct fbsd_gdbarch_data *)
72 gdbarch_data (gdbarch, fbsd_gdbarch_data_handle));
75 /* This is how we want PTIDs from core files to be printed. */
78 fbsd_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
82 if (ptid_get_lwp (ptid) != 0)
84 xsnprintf (buf, sizeof buf, "LWP %ld", ptid_get_lwp (ptid));
88 return normal_pid_to_str (ptid);
91 /* Extract the name assigned to a thread from a core. Returns the
92 string in a static buffer. */
95 fbsd_core_thread_name (struct gdbarch *gdbarch, struct thread_info *thr)
98 struct bfd_section *section;
101 if (ptid_get_lwp (thr->ptid) != 0)
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
110 thread_section_name section_name (".thrmisc", thr->ptid);
112 section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
113 if (section != NULL && bfd_section_size (core_bfd, section) > 0)
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,
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
129 if (strcmp (buf, elf_tdata (core_bfd)->core->program) != 0)
138 /* Implement the "core_xfer_siginfo" gdbarch method. */
141 fbsd_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
142 ULONGEST offset, ULONGEST len)
146 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
147 siginfo_size = SIZE32_SIGINFO_T;
149 siginfo_size = SIZE64_SIGINFO_T;
150 if (offset > siginfo_size)
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 ());
159 if (!bfd_get_section_contents (core_bfd, section, buf,
160 LWPINFO_OFFSET + LWPINFO_PL_FLAGS, 4))
163 int pl_flags = extract_signed_integer (buf, 4, gdbarch_byte_order (gdbarch));
164 if (!(pl_flags & PL_FLAG_SI))
167 if (offset + len > siginfo_size)
168 len = siginfo_size - offset;
170 ULONGEST siginfo_offset;
171 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
172 siginfo_offset = LWPINFO_OFFSET + LWPINFO32_PL_SIGINFO;
174 siginfo_offset = LWPINFO_OFFSET + LWPINFO64_PL_SIGINFO;
176 if (!bfd_get_section_contents (core_bfd, section, readbuf,
177 siginfo_offset + offset, len))
184 find_signalled_thread (struct thread_info *info, void *data)
186 if (info->suspend.stop_signal != GDB_SIGNAL_0
187 && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
193 /* Structure for passing information from
194 fbsd_collect_thread_registers via an iterator to
195 fbsd_collect_regset_section_cb. */
197 struct fbsd_collect_regset_section_cb_data
199 const struct regcache *regcache;
204 enum gdb_signal stop_signal;
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)
214 struct fbsd_collect_regset_section_cb_data *data
215 = (struct fbsd_collect_regset_section_cb_data *) cb_data;
217 if (data->abort_iteration)
220 gdb_assert (regset->collect_regset);
222 buf = (char *) xmalloc (size);
223 regset->collect_regset (regset, data->regcache, -1, buf, size);
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);
231 data->note_data = (char *) elfcore_write_register_note
232 (data->obfd, data->note_data, data->note_size,
233 sect_name, buf, size);
236 if (data->note_data == NULL)
237 data->abort_iteration = 1;
240 /* Records the thread's register state for the corefile note
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)
249 struct gdbarch *gdbarch = get_regcache_arch (regcache);
250 struct fbsd_collect_regset_section_cb_data data;
252 data.regcache = regcache;
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);
260 gdbarch_iterate_over_regset_sections (gdbarch,
261 fbsd_collect_regset_section_cb,
263 return data.note_data;
266 struct fbsd_corefile_thread_data
268 struct gdbarch *gdbarch;
272 enum gdb_signal stop_signal;
275 /* Records the thread's register state for the corefile note
279 fbsd_corefile_thread (struct thread_info *info,
280 struct fbsd_corefile_thread_data *args)
282 struct regcache *regcache;
284 regcache = get_thread_arch_regcache (info->ptid, args->gdbarch);
286 target_fetch_registers (regcache, -1);
288 args->note_data = fbsd_collect_thread_registers
289 (regcache, info->ptid, args->obfd, args->note_data,
290 args->note_size, args->stop_signal);
293 /* Create appropriate note sections for a corefile, returning them in
297 fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
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;
304 /* Put a "FreeBSD" label in the ELF header. */
305 i_ehdrp = elf_elfheader (obfd);
306 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
308 gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch));
310 if (get_exec_file (0))
312 const char *fname = lbasename (get_exec_file (0));
313 char *psargs = xstrdup (fname);
315 if (get_inferior_args ())
316 psargs = reconcat (psargs, psargs, " ", get_inferior_args (),
319 note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
323 /* Thread register information. */
326 update_thread_list ();
328 CATCH (e, RETURN_MASK_ERROR)
330 exception_print (gdb_stderr, e);
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;
343 signalled_thr = iterate_over_threads (find_signalled_thread, NULL);
344 if (signalled_thr == NULL)
345 signalled_thr = curr_thr;
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;
354 fbsd_corefile_thread (signalled_thr, &thread_args);
355 ALL_NON_EXITED_THREADS (thr)
357 if (thr == signalled_thr)
359 if (ptid_get_pid (thr->ptid) != ptid_get_pid (inferior_ptid))
362 fbsd_corefile_thread (thr, &thread_args);
365 note_data = thread_args.note_data;
370 /* Print descriptions of FreeBSD-specific AUXV entries to FILE. */
373 fbsd_print_auxv_entry (struct gdbarch *gdbarch, struct ui_file *file,
374 CORE_ADDR type, CORE_ADDR val)
377 const char *description;
378 enum auxv_format format;
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);
396 default_print_auxv_entry (gdbarch, file, type, val);
400 fprint_auxv_entry (file, name, description, format, type, val);
403 /* Implement the "get_siginfo_type" gdbarch method. */
406 fbsd_get_siginfo_type (struct gdbarch *gdbarch)
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;
415 fbsd_gdbarch_data = get_fbsd_gdbarch_data (gdbarch);
416 if (fbsd_gdbarch_data->siginfo_type != NULL)
417 return fbsd_gdbarch_data->siginfo_type;
419 int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
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),
425 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
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);
434 pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
435 TYPE_LENGTH (int32_type), "__pid_t");
436 TYPE_TARGET_TYPE (pid_type) = int32_type;
437 TYPE_TARGET_STUB (pid_type) = 1;
440 uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
441 TYPE_LENGTH (uint32_type), "__uid_t");
442 TYPE_TARGET_TYPE (uid_type) = uint32_type;
443 TYPE_TARGET_STUB (uid_type) = 1;
446 reason_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
449 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
450 append_composite_type_field (type, "si_trapno", int_type);
451 append_composite_type_field (reason_type, "_fault", type);
454 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
455 append_composite_type_field (type, "si_timerid", int_type);
456 append_composite_type_field (type, "si_overrun", int_type);
457 append_composite_type_field (reason_type, "_timer", type);
460 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
461 append_composite_type_field (type, "si_mqd", int_type);
462 append_composite_type_field (reason_type, "_mesgq", type);
465 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
466 append_composite_type_field (type, "si_band", long_type);
467 append_composite_type_field (reason_type, "_poll", type);
470 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
471 append_composite_type_field (type, "__spare1__", long_type);
472 append_composite_type_field (type, "__spare2__",
473 init_vector_type (int_type, 7));
474 append_composite_type_field (reason_type, "__spare__", type);
477 siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
478 TYPE_NAME (siginfo_type) = xstrdup ("siginfo");
479 append_composite_type_field (siginfo_type, "si_signo", int_type);
480 append_composite_type_field (siginfo_type, "si_errno", int_type);
481 append_composite_type_field (siginfo_type, "si_code", int_type);
482 append_composite_type_field (siginfo_type, "si_pid", pid_type);
483 append_composite_type_field (siginfo_type, "si_uid", uid_type);
484 append_composite_type_field (siginfo_type, "si_status", int_type);
485 append_composite_type_field (siginfo_type, "si_addr", void_ptr_type);
486 append_composite_type_field (siginfo_type, "si_value", sigval_type);
487 append_composite_type_field (siginfo_type, "_reason", reason_type);
489 fbsd_gdbarch_data->siginfo_type = siginfo_type;
494 /* Implement the "get_syscall_number" gdbarch method. */
497 fbsd_get_syscall_number (struct gdbarch *gdbarch,
501 /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
502 native targets fetch the system call number from the
503 'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
504 However, system call catching requires this function to be
507 internal_error (__FILE__, __LINE__, _("fbsd_get_sycall_number called"));
510 /* To be called from GDB_OSABI_FREEBSD handlers. */
513 fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
515 set_gdbarch_core_pid_to_str (gdbarch, fbsd_core_pid_to_str);
516 set_gdbarch_core_thread_name (gdbarch, fbsd_core_thread_name);
517 set_gdbarch_core_xfer_siginfo (gdbarch, fbsd_core_xfer_siginfo);
518 set_gdbarch_make_corefile_notes (gdbarch, fbsd_make_corefile_notes);
519 set_gdbarch_print_auxv_entry (gdbarch, fbsd_print_auxv_entry);
520 set_gdbarch_get_siginfo_type (gdbarch, fbsd_get_siginfo_type);
522 /* `catch syscall' */
523 set_xml_syscall_file_name (gdbarch, "syscalls/freebsd.xml");
524 set_gdbarch_get_syscall_number (gdbarch, fbsd_get_syscall_number);
527 /* Provide a prototype to silence -Wmissing-prototypes. */
528 extern initialize_file_ftype _initialize_fbsd_tdep;
531 _initialize_fbsd_tdep (void)
533 fbsd_gdbarch_data_handle =
534 gdbarch_data_register_post_init (init_fbsd_gdbarch_data);