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 static struct gdbarch_data *fbsd_gdbarch_data_handle;
35 struct fbsd_gdbarch_data
37 struct type *siginfo_type;
41 init_fbsd_gdbarch_data (struct gdbarch *gdbarch)
43 return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct fbsd_gdbarch_data);
46 static struct fbsd_gdbarch_data *
47 get_fbsd_gdbarch_data (struct gdbarch *gdbarch)
49 return ((struct fbsd_gdbarch_data *)
50 gdbarch_data (gdbarch, fbsd_gdbarch_data_handle));
53 /* This is how we want PTIDs from core files to be printed. */
56 fbsd_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
60 if (ptid_get_lwp (ptid) != 0)
62 xsnprintf (buf, sizeof buf, "LWP %ld", ptid_get_lwp (ptid));
66 return normal_pid_to_str (ptid);
69 /* Extract the name assigned to a thread from a core. Returns the
70 string in a static buffer. */
73 fbsd_core_thread_name (struct gdbarch *gdbarch, struct thread_info *thr)
76 struct bfd_section *section;
80 if (ptid_get_lwp (thr->ptid) != 0)
82 /* FreeBSD includes a NT_FREEBSD_THRMISC note for each thread
83 whose contents are defined by a "struct thrmisc" declared in
84 <sys/procfs.h> on FreeBSD. The per-thread name is stored as
85 a null-terminated string as the first member of the
86 structure. Rather than define the full structure here, just
87 extract the null-terminated name from the start of the
89 xsnprintf (sectionstr, sizeof sectionstr, ".thrmisc/%ld",
90 ptid_get_lwp (thr->ptid));
91 section = bfd_get_section_by_name (core_bfd, sectionstr);
92 if (section != NULL && bfd_section_size (core_bfd, section) > 0)
94 /* Truncate the name if it is longer than "buf". */
95 size = bfd_section_size (core_bfd, section);
96 if (size > sizeof buf - 1)
97 size = sizeof buf - 1;
98 if (bfd_get_section_contents (core_bfd, section, buf, (file_ptr) 0,
104 /* Note that each thread will report the process command
105 as its thread name instead of an empty name if a name
106 has not been set explicitly. Return a NULL name in
108 if (strcmp (buf, elf_tdata (core_bfd)->core->program) != 0)
118 find_signalled_thread (struct thread_info *info, void *data)
120 if (info->suspend.stop_signal != GDB_SIGNAL_0
121 && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
127 /* Structure for passing information from
128 fbsd_collect_thread_registers via an iterator to
129 fbsd_collect_regset_section_cb. */
131 struct fbsd_collect_regset_section_cb_data
133 const struct regcache *regcache;
138 enum gdb_signal stop_signal;
143 fbsd_collect_regset_section_cb (const char *sect_name, int size,
144 const struct regset *regset,
145 const char *human_name, void *cb_data)
148 struct fbsd_collect_regset_section_cb_data *data
149 = (struct fbsd_collect_regset_section_cb_data *) cb_data;
151 if (data->abort_iteration)
154 gdb_assert (regset->collect_regset);
156 buf = (char *) xmalloc (size);
157 regset->collect_regset (regset, data->regcache, -1, buf, size);
159 /* PRSTATUS still needs to be treated specially. */
160 if (strcmp (sect_name, ".reg") == 0)
161 data->note_data = (char *) elfcore_write_prstatus
162 (data->obfd, data->note_data, data->note_size, data->lwp,
163 gdb_signal_to_host (data->stop_signal), buf);
165 data->note_data = (char *) elfcore_write_register_note
166 (data->obfd, data->note_data, data->note_size,
167 sect_name, buf, size);
170 if (data->note_data == NULL)
171 data->abort_iteration = 1;
174 /* Records the thread's register state for the corefile note
178 fbsd_collect_thread_registers (const struct regcache *regcache,
179 ptid_t ptid, bfd *obfd,
180 char *note_data, int *note_size,
181 enum gdb_signal stop_signal)
183 struct gdbarch *gdbarch = get_regcache_arch (regcache);
184 struct fbsd_collect_regset_section_cb_data data;
186 data.regcache = regcache;
188 data.note_data = note_data;
189 data.note_size = note_size;
190 data.stop_signal = stop_signal;
191 data.abort_iteration = 0;
192 data.lwp = ptid_get_lwp (ptid);
194 gdbarch_iterate_over_regset_sections (gdbarch,
195 fbsd_collect_regset_section_cb,
197 return data.note_data;
200 struct fbsd_corefile_thread_data
202 struct gdbarch *gdbarch;
206 enum gdb_signal stop_signal;
209 /* Records the thread's register state for the corefile note
213 fbsd_corefile_thread (struct thread_info *info,
214 struct fbsd_corefile_thread_data *args)
216 struct regcache *regcache;
218 regcache = get_thread_arch_regcache (info->ptid, args->gdbarch);
220 target_fetch_registers (regcache, -1);
222 args->note_data = fbsd_collect_thread_registers
223 (regcache, info->ptid, args->obfd, args->note_data,
224 args->note_size, args->stop_signal);
227 /* Create appropriate note sections for a corefile, returning them in
231 fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
233 struct fbsd_corefile_thread_data thread_args;
234 char *note_data = NULL;
235 Elf_Internal_Ehdr *i_ehdrp;
236 struct thread_info *curr_thr, *signalled_thr, *thr;
238 /* Put a "FreeBSD" label in the ELF header. */
239 i_ehdrp = elf_elfheader (obfd);
240 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
242 gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch));
244 if (get_exec_file (0))
246 const char *fname = lbasename (get_exec_file (0));
247 char *psargs = xstrdup (fname);
249 if (get_inferior_args ())
250 psargs = reconcat (psargs, psargs, " ", get_inferior_args (),
253 note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
257 /* Thread register information. */
260 update_thread_list ();
262 CATCH (e, RETURN_MASK_ERROR)
264 exception_print (gdb_stderr, e);
268 /* Like the kernel, prefer dumping the signalled thread first.
269 "First thread" is what tools use to infer the signalled thread.
270 In case there's more than one signalled thread, prefer the
271 current thread, if it is signalled. */
272 curr_thr = inferior_thread ();
273 if (curr_thr->suspend.stop_signal != GDB_SIGNAL_0)
274 signalled_thr = curr_thr;
277 signalled_thr = iterate_over_threads (find_signalled_thread, NULL);
278 if (signalled_thr == NULL)
279 signalled_thr = curr_thr;
282 thread_args.gdbarch = gdbarch;
283 thread_args.obfd = obfd;
284 thread_args.note_data = note_data;
285 thread_args.note_size = note_size;
286 thread_args.stop_signal = signalled_thr->suspend.stop_signal;
288 fbsd_corefile_thread (signalled_thr, &thread_args);
289 ALL_NON_EXITED_THREADS (thr)
291 if (thr == signalled_thr)
293 if (ptid_get_pid (thr->ptid) != ptid_get_pid (inferior_ptid))
296 fbsd_corefile_thread (thr, &thread_args);
299 note_data = thread_args.note_data;
304 /* Print descriptions of FreeBSD-specific AUXV entries to FILE. */
307 fbsd_print_auxv_entry (struct gdbarch *gdbarch, struct ui_file *file,
308 CORE_ADDR type, CORE_ADDR val)
311 const char *description;
312 enum auxv_format format;
316 #define _TAGNAME(tag) #tag
317 #define TAGNAME(tag) _TAGNAME(AT_##tag)
318 #define TAG(tag, text, kind) \
319 case AT_FREEBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
320 TAG (EXECPATH, _("Executable path"), AUXV_FORMAT_STR);
321 TAG (CANARY, _("Canary for SSP"), AUXV_FORMAT_HEX);
322 TAG (CANARYLEN, ("Length of the SSP canary"), AUXV_FORMAT_DEC);
323 TAG (OSRELDATE, _("OSRELDATE"), AUXV_FORMAT_DEC);
324 TAG (NCPUS, _("Number of CPUs"), AUXV_FORMAT_DEC);
325 TAG (PAGESIZES, _("Pagesizes"), AUXV_FORMAT_HEX);
326 TAG (PAGESIZESLEN, _("Number of pagesizes"), AUXV_FORMAT_DEC);
327 TAG (TIMEKEEP, _("Pointer to timehands"), AUXV_FORMAT_HEX);
328 TAG (STACKPROT, _("Initial stack protection"), AUXV_FORMAT_HEX);
330 default_print_auxv_entry (gdbarch, file, type, val);
334 fprint_auxv_entry (file, name, description, format, type, val);
337 /* Implement the "get_siginfo_type" gdbarch method. */
340 fbsd_get_siginfo_type (struct gdbarch *gdbarch)
342 struct fbsd_gdbarch_data *fbsd_gdbarch_data;
343 struct type *int_type, *int32_type, *uint32_type, *long_type, *void_ptr_type;
344 struct type *uid_type, *pid_type;
345 struct type *sigval_type, *reason_type;
346 struct type *siginfo_type;
349 fbsd_gdbarch_data = get_fbsd_gdbarch_data (gdbarch);
350 if (fbsd_gdbarch_data->siginfo_type != NULL)
351 return fbsd_gdbarch_data->siginfo_type;
353 int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
355 int32_type = arch_integer_type (gdbarch, 32, 0, "int32_t");
356 uint32_type = arch_integer_type (gdbarch, 32, 1, "uint32_t");
357 long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
359 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
362 sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
363 TYPE_NAME (sigval_type) = xstrdup ("sigval");
364 append_composite_type_field (sigval_type, "sival_int", int_type);
365 append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
368 pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
369 TYPE_LENGTH (int32_type), "__pid_t");
370 TYPE_TARGET_TYPE (pid_type) = int32_type;
371 TYPE_TARGET_STUB (pid_type) = 1;
374 uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
375 TYPE_LENGTH (uint32_type), "__uid_t");
376 TYPE_TARGET_TYPE (uid_type) = uint32_type;
377 TYPE_TARGET_STUB (uid_type) = 1;
380 reason_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
383 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
384 append_composite_type_field (type, "si_trapno", int_type);
385 append_composite_type_field (reason_type, "_fault", type);
388 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
389 append_composite_type_field (type, "si_timerid", int_type);
390 append_composite_type_field (type, "si_overrun", int_type);
391 append_composite_type_field (reason_type, "_timer", type);
394 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
395 append_composite_type_field (type, "si_mqd", int_type);
396 append_composite_type_field (reason_type, "_mesgq", type);
399 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
400 append_composite_type_field (type, "si_band", long_type);
401 append_composite_type_field (reason_type, "_poll", type);
404 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
405 append_composite_type_field (type, "__spare1__", long_type);
406 append_composite_type_field (type, "__spare2__",
407 init_vector_type (int_type, 7));
408 append_composite_type_field (reason_type, "__spare__", type);
411 siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
412 TYPE_NAME (siginfo_type) = xstrdup ("siginfo");
413 append_composite_type_field (siginfo_type, "si_signo", int_type);
414 append_composite_type_field (siginfo_type, "si_errno", int_type);
415 append_composite_type_field (siginfo_type, "si_code", int_type);
416 append_composite_type_field (siginfo_type, "si_pid", pid_type);
417 append_composite_type_field (siginfo_type, "si_uid", uid_type);
418 append_composite_type_field (siginfo_type, "si_status", int_type);
419 append_composite_type_field (siginfo_type, "si_addr", void_ptr_type);
420 append_composite_type_field (siginfo_type, "si_value", sigval_type);
421 append_composite_type_field (siginfo_type, "_reason", reason_type);
423 fbsd_gdbarch_data->siginfo_type = siginfo_type;
428 /* Implement the "get_syscall_number" gdbarch method. */
431 fbsd_get_syscall_number (struct gdbarch *gdbarch,
435 /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
436 native targets fetch the system call number from the
437 'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
438 However, system call catching requires this function to be
441 internal_error (__FILE__, __LINE__, _("fbsd_get_sycall_number called"));
444 /* To be called from GDB_OSABI_FREEBSD handlers. */
447 fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
449 set_gdbarch_core_pid_to_str (gdbarch, fbsd_core_pid_to_str);
450 set_gdbarch_core_thread_name (gdbarch, fbsd_core_thread_name);
451 set_gdbarch_make_corefile_notes (gdbarch, fbsd_make_corefile_notes);
452 set_gdbarch_print_auxv_entry (gdbarch, fbsd_print_auxv_entry);
453 set_gdbarch_get_siginfo_type (gdbarch, fbsd_get_siginfo_type);
455 /* `catch syscall' */
456 set_xml_syscall_file_name (gdbarch, "syscalls/freebsd.xml");
457 set_gdbarch_get_syscall_number (gdbarch, fbsd_get_syscall_number);
460 /* Provide a prototype to silence -Wmissing-prototypes. */
461 extern initialize_file_ftype _initialize_fbsd_tdep;
464 _initialize_fbsd_tdep (void)
466 fbsd_gdbarch_data_handle =
467 gdbarch_data_register_post_init (init_fbsd_gdbarch_data);