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
;
79 if (ptid_get_lwp (thr
->ptid
) != 0)
81 /* FreeBSD includes a NT_FREEBSD_THRMISC note for each thread
82 whose contents are defined by a "struct thrmisc" declared in
83 <sys/procfs.h> on FreeBSD. The per-thread name is stored as
84 a null-terminated string as the first member of the
85 structure. Rather than define the full structure here, just
86 extract the null-terminated name from the start of the
88 thread_section_name
section_name (".thrmisc", thr
->ptid
);
90 section
= bfd_get_section_by_name (core_bfd
, section_name
.c_str ());
91 if (section
!= NULL
&& bfd_section_size (core_bfd
, section
) > 0)
93 /* Truncate the name if it is longer than "buf". */
94 size
= bfd_section_size (core_bfd
, section
);
95 if (size
> sizeof buf
- 1)
96 size
= sizeof buf
- 1;
97 if (bfd_get_section_contents (core_bfd
, section
, buf
, (file_ptr
) 0,
103 /* Note that each thread will report the process command
104 as its thread name instead of an empty name if a name
105 has not been set explicitly. Return a NULL name in
107 if (strcmp (buf
, elf_tdata (core_bfd
)->core
->program
) != 0)
117 find_signalled_thread (struct thread_info
*info
, void *data
)
119 if (info
->suspend
.stop_signal
!= GDB_SIGNAL_0
120 && ptid_get_pid (info
->ptid
) == ptid_get_pid (inferior_ptid
))
126 /* Structure for passing information from
127 fbsd_collect_thread_registers via an iterator to
128 fbsd_collect_regset_section_cb. */
130 struct fbsd_collect_regset_section_cb_data
132 const struct regcache
*regcache
;
137 enum gdb_signal stop_signal
;
142 fbsd_collect_regset_section_cb (const char *sect_name
, int size
,
143 const struct regset
*regset
,
144 const char *human_name
, void *cb_data
)
147 struct fbsd_collect_regset_section_cb_data
*data
148 = (struct fbsd_collect_regset_section_cb_data
*) cb_data
;
150 if (data
->abort_iteration
)
153 gdb_assert (regset
->collect_regset
);
155 buf
= (char *) xmalloc (size
);
156 regset
->collect_regset (regset
, data
->regcache
, -1, buf
, size
);
158 /* PRSTATUS still needs to be treated specially. */
159 if (strcmp (sect_name
, ".reg") == 0)
160 data
->note_data
= (char *) elfcore_write_prstatus
161 (data
->obfd
, data
->note_data
, data
->note_size
, data
->lwp
,
162 gdb_signal_to_host (data
->stop_signal
), buf
);
164 data
->note_data
= (char *) elfcore_write_register_note
165 (data
->obfd
, data
->note_data
, data
->note_size
,
166 sect_name
, buf
, size
);
169 if (data
->note_data
== NULL
)
170 data
->abort_iteration
= 1;
173 /* Records the thread's register state for the corefile note
177 fbsd_collect_thread_registers (const struct regcache
*regcache
,
178 ptid_t ptid
, bfd
*obfd
,
179 char *note_data
, int *note_size
,
180 enum gdb_signal stop_signal
)
182 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
183 struct fbsd_collect_regset_section_cb_data data
;
185 data
.regcache
= regcache
;
187 data
.note_data
= note_data
;
188 data
.note_size
= note_size
;
189 data
.stop_signal
= stop_signal
;
190 data
.abort_iteration
= 0;
191 data
.lwp
= ptid_get_lwp (ptid
);
193 gdbarch_iterate_over_regset_sections (gdbarch
,
194 fbsd_collect_regset_section_cb
,
196 return data
.note_data
;
199 struct fbsd_corefile_thread_data
201 struct gdbarch
*gdbarch
;
205 enum gdb_signal stop_signal
;
208 /* Records the thread's register state for the corefile note
212 fbsd_corefile_thread (struct thread_info
*info
,
213 struct fbsd_corefile_thread_data
*args
)
215 struct regcache
*regcache
;
217 regcache
= get_thread_arch_regcache (info
->ptid
, args
->gdbarch
);
219 target_fetch_registers (regcache
, -1);
221 args
->note_data
= fbsd_collect_thread_registers
222 (regcache
, info
->ptid
, args
->obfd
, args
->note_data
,
223 args
->note_size
, args
->stop_signal
);
226 /* Create appropriate note sections for a corefile, returning them in
230 fbsd_make_corefile_notes (struct gdbarch
*gdbarch
, bfd
*obfd
, int *note_size
)
232 struct fbsd_corefile_thread_data thread_args
;
233 char *note_data
= NULL
;
234 Elf_Internal_Ehdr
*i_ehdrp
;
235 struct thread_info
*curr_thr
, *signalled_thr
, *thr
;
237 /* Put a "FreeBSD" label in the ELF header. */
238 i_ehdrp
= elf_elfheader (obfd
);
239 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_FREEBSD
;
241 gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch
));
243 if (get_exec_file (0))
245 const char *fname
= lbasename (get_exec_file (0));
246 char *psargs
= xstrdup (fname
);
248 if (get_inferior_args ())
249 psargs
= reconcat (psargs
, psargs
, " ", get_inferior_args (),
252 note_data
= elfcore_write_prpsinfo (obfd
, note_data
, note_size
,
256 /* Thread register information. */
259 update_thread_list ();
261 CATCH (e
, RETURN_MASK_ERROR
)
263 exception_print (gdb_stderr
, e
);
267 /* Like the kernel, prefer dumping the signalled thread first.
268 "First thread" is what tools use to infer the signalled thread.
269 In case there's more than one signalled thread, prefer the
270 current thread, if it is signalled. */
271 curr_thr
= inferior_thread ();
272 if (curr_thr
->suspend
.stop_signal
!= GDB_SIGNAL_0
)
273 signalled_thr
= curr_thr
;
276 signalled_thr
= iterate_over_threads (find_signalled_thread
, NULL
);
277 if (signalled_thr
== NULL
)
278 signalled_thr
= curr_thr
;
281 thread_args
.gdbarch
= gdbarch
;
282 thread_args
.obfd
= obfd
;
283 thread_args
.note_data
= note_data
;
284 thread_args
.note_size
= note_size
;
285 thread_args
.stop_signal
= signalled_thr
->suspend
.stop_signal
;
287 fbsd_corefile_thread (signalled_thr
, &thread_args
);
288 ALL_NON_EXITED_THREADS (thr
)
290 if (thr
== signalled_thr
)
292 if (ptid_get_pid (thr
->ptid
) != ptid_get_pid (inferior_ptid
))
295 fbsd_corefile_thread (thr
, &thread_args
);
298 note_data
= thread_args
.note_data
;
303 /* Print descriptions of FreeBSD-specific AUXV entries to FILE. */
306 fbsd_print_auxv_entry (struct gdbarch
*gdbarch
, struct ui_file
*file
,
307 CORE_ADDR type
, CORE_ADDR val
)
310 const char *description
;
311 enum auxv_format format
;
315 #define _TAGNAME(tag) #tag
316 #define TAGNAME(tag) _TAGNAME(AT_##tag)
317 #define TAG(tag, text, kind) \
318 case AT_FREEBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
319 TAG (EXECPATH
, _("Executable path"), AUXV_FORMAT_STR
);
320 TAG (CANARY
, _("Canary for SSP"), AUXV_FORMAT_HEX
);
321 TAG (CANARYLEN
, ("Length of the SSP canary"), AUXV_FORMAT_DEC
);
322 TAG (OSRELDATE
, _("OSRELDATE"), AUXV_FORMAT_DEC
);
323 TAG (NCPUS
, _("Number of CPUs"), AUXV_FORMAT_DEC
);
324 TAG (PAGESIZES
, _("Pagesizes"), AUXV_FORMAT_HEX
);
325 TAG (PAGESIZESLEN
, _("Number of pagesizes"), AUXV_FORMAT_DEC
);
326 TAG (TIMEKEEP
, _("Pointer to timehands"), AUXV_FORMAT_HEX
);
327 TAG (STACKPROT
, _("Initial stack protection"), AUXV_FORMAT_HEX
);
329 default_print_auxv_entry (gdbarch
, file
, type
, val
);
333 fprint_auxv_entry (file
, name
, description
, format
, type
, val
);
336 /* Implement the "get_siginfo_type" gdbarch method. */
339 fbsd_get_siginfo_type (struct gdbarch
*gdbarch
)
341 struct fbsd_gdbarch_data
*fbsd_gdbarch_data
;
342 struct type
*int_type
, *int32_type
, *uint32_type
, *long_type
, *void_ptr_type
;
343 struct type
*uid_type
, *pid_type
;
344 struct type
*sigval_type
, *reason_type
;
345 struct type
*siginfo_type
;
348 fbsd_gdbarch_data
= get_fbsd_gdbarch_data (gdbarch
);
349 if (fbsd_gdbarch_data
->siginfo_type
!= NULL
)
350 return fbsd_gdbarch_data
->siginfo_type
;
352 int_type
= arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
354 int32_type
= arch_integer_type (gdbarch
, 32, 0, "int32_t");
355 uint32_type
= arch_integer_type (gdbarch
, 32, 1, "uint32_t");
356 long_type
= arch_integer_type (gdbarch
, gdbarch_long_bit (gdbarch
),
358 void_ptr_type
= lookup_pointer_type (builtin_type (gdbarch
)->builtin_void
);
361 sigval_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
362 TYPE_NAME (sigval_type
) = xstrdup ("sigval");
363 append_composite_type_field (sigval_type
, "sival_int", int_type
);
364 append_composite_type_field (sigval_type
, "sival_ptr", void_ptr_type
);
367 pid_type
= arch_type (gdbarch
, TYPE_CODE_TYPEDEF
,
368 TYPE_LENGTH (int32_type
), "__pid_t");
369 TYPE_TARGET_TYPE (pid_type
) = int32_type
;
370 TYPE_TARGET_STUB (pid_type
) = 1;
373 uid_type
= arch_type (gdbarch
, TYPE_CODE_TYPEDEF
,
374 TYPE_LENGTH (uint32_type
), "__uid_t");
375 TYPE_TARGET_TYPE (uid_type
) = uint32_type
;
376 TYPE_TARGET_STUB (uid_type
) = 1;
379 reason_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
382 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
383 append_composite_type_field (type
, "si_trapno", int_type
);
384 append_composite_type_field (reason_type
, "_fault", type
);
387 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
388 append_composite_type_field (type
, "si_timerid", int_type
);
389 append_composite_type_field (type
, "si_overrun", int_type
);
390 append_composite_type_field (reason_type
, "_timer", type
);
393 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
394 append_composite_type_field (type
, "si_mqd", int_type
);
395 append_composite_type_field (reason_type
, "_mesgq", type
);
398 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
399 append_composite_type_field (type
, "si_band", long_type
);
400 append_composite_type_field (reason_type
, "_poll", type
);
403 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
404 append_composite_type_field (type
, "__spare1__", long_type
);
405 append_composite_type_field (type
, "__spare2__",
406 init_vector_type (int_type
, 7));
407 append_composite_type_field (reason_type
, "__spare__", type
);
410 siginfo_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
411 TYPE_NAME (siginfo_type
) = xstrdup ("siginfo");
412 append_composite_type_field (siginfo_type
, "si_signo", int_type
);
413 append_composite_type_field (siginfo_type
, "si_errno", int_type
);
414 append_composite_type_field (siginfo_type
, "si_code", int_type
);
415 append_composite_type_field (siginfo_type
, "si_pid", pid_type
);
416 append_composite_type_field (siginfo_type
, "si_uid", uid_type
);
417 append_composite_type_field (siginfo_type
, "si_status", int_type
);
418 append_composite_type_field (siginfo_type
, "si_addr", void_ptr_type
);
419 append_composite_type_field (siginfo_type
, "si_value", sigval_type
);
420 append_composite_type_field (siginfo_type
, "_reason", reason_type
);
422 fbsd_gdbarch_data
->siginfo_type
= siginfo_type
;
427 /* Implement the "get_syscall_number" gdbarch method. */
430 fbsd_get_syscall_number (struct gdbarch
*gdbarch
,
434 /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
435 native targets fetch the system call number from the
436 'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
437 However, system call catching requires this function to be
440 internal_error (__FILE__
, __LINE__
, _("fbsd_get_sycall_number called"));
443 /* To be called from GDB_OSABI_FREEBSD handlers. */
446 fbsd_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
448 set_gdbarch_core_pid_to_str (gdbarch
, fbsd_core_pid_to_str
);
449 set_gdbarch_core_thread_name (gdbarch
, fbsd_core_thread_name
);
450 set_gdbarch_make_corefile_notes (gdbarch
, fbsd_make_corefile_notes
);
451 set_gdbarch_print_auxv_entry (gdbarch
, fbsd_print_auxv_entry
);
452 set_gdbarch_get_siginfo_type (gdbarch
, fbsd_get_siginfo_type
);
454 /* `catch syscall' */
455 set_xml_syscall_file_name (gdbarch
, "syscalls/freebsd.xml");
456 set_gdbarch_get_syscall_number (gdbarch
, fbsd_get_syscall_number
);
459 /* Provide a prototype to silence -Wmissing-prototypes. */
460 extern initialize_file_ftype _initialize_fbsd_tdep
;
463 _initialize_fbsd_tdep (void)
465 fbsd_gdbarch_data_handle
=
466 gdbarch_data_register_post_init (init_fbsd_gdbarch_data
);