]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/fbsd-tdep.c
compile: Remove non-const reference parameters
[thirdparty/binutils-gdb.git] / gdb / fbsd-tdep.c
CommitLineData
a904c024
AA
1/* Target-dependent code for FreeBSD, architecture-independent.
2
e2882c85 3 Copyright (C) 2002-2018 Free Software Foundation, Inc.
a904c024
AA
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"
82372b2f 21#include "auxv.h"
a904c024
AA
22#include "gdbcore.h"
23#include "inferior.h"
24#include "regcache.h"
25#include "regset.h"
26#include "gdbthread.h"
e6cdd38e 27#include "xml-syscall.h"
a904c024 28
a904c024
AA
29#include "elf-bfd.h"
30#include "fbsd-tdep.h"
31
32
4b654465
JB
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
d2176225
JB
55/* Offsets in data structure used in NT_FREEBSD_PROCSTAT_VMMAP core
56 dump notes. See <sys/user.h> for the definition of struct
57 kinfo_vmentry. This data structure should have the same layout on
58 all architectures. */
59
60#define KVE_STRUCTSIZE 0x0
61#define KVE_START 0x8
62#define KVE_END 0x10
63#define KVE_OFFSET 0x18
64#define KVE_FLAGS 0x2c
906b4aac 65#define KVE_PROTECTION 0x38
d2176225
JB
66#define KVE_PATH 0x88
67
68/* Flags in the 'kve_protection' field in struct kinfo_vmentry. These
69 match the KVME_PROT_* constants in <sys/user.h>. */
70
71#define KINFO_VME_PROT_READ 0x00000001
72#define KINFO_VME_PROT_WRITE 0x00000002
73#define KINFO_VME_PROT_EXEC 0x00000004
74
75/* Flags in the 'kve_flags' field in struct kinfo_vmentry. These
76 match the KVME_FLAG_* constants in <sys/user.h>. */
77
78#define KINFO_VME_FLAG_COW 0x00000001
79#define KINFO_VME_FLAG_NEEDS_COPY 0x00000002
80#define KINFO_VME_FLAG_NOCOREDUMP 0x00000004
81#define KINFO_VME_FLAG_SUPER 0x00000008
82#define KINFO_VME_FLAG_GROWS_UP 0x00000010
83#define KINFO_VME_FLAG_GROWS_DOWN 0x00000020
84
85/* Offsets in data structure used in NT_FREEBSD_PROCSTAT_FILES core
86 dump notes. See <sys/user.h> for the definition of struct
87 kinfo_file. This data structure should have the same layout on all
88 architectures. */
89
90#define KF_STRUCTSIZE 0x0
91#define KF_TYPE 0x4
92#define KF_FD 0x8
93#define KF_PATH 0x170
94
95/* Constants for the 'kf_type' field in struct kinfo_file. These
96 match the KF_TYPE_* constants in <sys/user.h>. */
97
98#define KINFO_FILE_TYPE_VNODE 1
99
100/* Special values for the 'kf_fd' field in struct kinfo_file. These
101 match the KF_FD_TYPE_* constants in <sys/user.h>. */
102
103#define KINFO_FILE_FD_TYPE_CWD -1
104#define KINFO_FILE_FD_TYPE_TEXT -5
105
106/* Number of 32-bit words in a signal set. This matches _SIG_WORDS in
107 <sys/_sigset.h> and is the same value on all architectures. */
108
109#define SIG_WORDS 4
110
111/* Offsets in data structure used in NT_FREEBSD_PROCSTAT_PROC core
112 dump notes. See <sys/user.h> for the definition of struct
113 kinfo_proc. This data structure has different layouts on different
114 architectures mostly due to ILP32 vs LP64. However, FreeBSD/i386
115 uses a 32-bit time_t while all other architectures use a 64-bit
116 time_t.
117
118 The core dump note actually contains one kinfo_proc structure for
119 each thread, but all of the process-wide data can be obtained from
120 the first structure. One result of this note's format is that some
121 of the process-wide status available in the native target method
122 from the kern.proc.pid.<pid> sysctl such as ki_stat and ki_siglist
123 is not available from a core dump. Instead, the per-thread data
124 structures contain the value of these fields for individual
125 threads. */
126
127struct kinfo_proc_layout
128{
129 /* Offsets of struct kinfo_proc members. */
130 int ki_layout;
131 int ki_pid;
132 int ki_ppid;
133 int ki_pgid;
134 int ki_tpgid;
135 int ki_sid;
136 int ki_tdev_freebsd11;
137 int ki_sigignore;
138 int ki_sigcatch;
139 int ki_uid;
140 int ki_ruid;
141 int ki_svuid;
142 int ki_rgid;
143 int ki_svgid;
144 int ki_ngroups;
145 int ki_groups;
146 int ki_size;
147 int ki_rssize;
148 int ki_tsize;
149 int ki_dsize;
150 int ki_ssize;
151 int ki_start;
152 int ki_nice;
153 int ki_comm;
154 int ki_tdev;
155 int ki_rusage;
156 int ki_rusage_ch;
157
158 /* Offsets of struct rusage members. */
159 int ru_utime;
160 int ru_stime;
161 int ru_maxrss;
162 int ru_minflt;
163 int ru_majflt;
164};
165
166const struct kinfo_proc_layout kinfo_proc_layout_32 =
167 {
168 .ki_layout = 0x4,
169 .ki_pid = 0x28,
170 .ki_ppid = 0x2c,
171 .ki_pgid = 0x30,
172 .ki_tpgid = 0x34,
173 .ki_sid = 0x38,
174 .ki_tdev_freebsd11 = 0x44,
175 .ki_sigignore = 0x68,
176 .ki_sigcatch = 0x78,
177 .ki_uid = 0x88,
178 .ki_ruid = 0x8c,
179 .ki_svuid = 0x90,
180 .ki_rgid = 0x94,
181 .ki_svgid = 0x98,
182 .ki_ngroups = 0x9c,
183 .ki_groups = 0xa0,
184 .ki_size = 0xe0,
185 .ki_rssize = 0xe4,
186 .ki_tsize = 0xec,
187 .ki_dsize = 0xf0,
188 .ki_ssize = 0xf4,
189 .ki_start = 0x118,
190 .ki_nice = 0x145,
191 .ki_comm = 0x17f,
192 .ki_tdev = 0x1f0,
193 .ki_rusage = 0x220,
194 .ki_rusage_ch = 0x278,
195
196 .ru_utime = 0x0,
197 .ru_stime = 0x10,
198 .ru_maxrss = 0x20,
199 .ru_minflt = 0x30,
200 .ru_majflt = 0x34,
201 };
202
203const struct kinfo_proc_layout kinfo_proc_layout_i386 =
204 {
205 .ki_layout = 0x4,
206 .ki_pid = 0x28,
207 .ki_ppid = 0x2c,
208 .ki_pgid = 0x30,
209 .ki_tpgid = 0x34,
210 .ki_sid = 0x38,
211 .ki_tdev_freebsd11 = 0x44,
212 .ki_sigignore = 0x68,
213 .ki_sigcatch = 0x78,
214 .ki_uid = 0x88,
215 .ki_ruid = 0x8c,
216 .ki_svuid = 0x90,
217 .ki_rgid = 0x94,
218 .ki_svgid = 0x98,
219 .ki_ngroups = 0x9c,
220 .ki_groups = 0xa0,
221 .ki_size = 0xe0,
222 .ki_rssize = 0xe4,
223 .ki_tsize = 0xec,
224 .ki_dsize = 0xf0,
225 .ki_ssize = 0xf4,
226 .ki_start = 0x118,
227 .ki_nice = 0x135,
228 .ki_comm = 0x16f,
229 .ki_tdev = 0x1e0,
230 .ki_rusage = 0x210,
231 .ki_rusage_ch = 0x258,
232
233 .ru_utime = 0x0,
234 .ru_stime = 0x8,
235 .ru_maxrss = 0x10,
236 .ru_minflt = 0x20,
237 .ru_majflt = 0x24,
238 };
239
240const struct kinfo_proc_layout kinfo_proc_layout_64 =
241 {
242 .ki_layout = 0x4,
243 .ki_pid = 0x48,
244 .ki_ppid = 0x4c,
245 .ki_pgid = 0x50,
246 .ki_tpgid = 0x54,
247 .ki_sid = 0x58,
248 .ki_tdev_freebsd11 = 0x64,
249 .ki_sigignore = 0x88,
250 .ki_sigcatch = 0x98,
251 .ki_uid = 0xa8,
252 .ki_ruid = 0xac,
253 .ki_svuid = 0xb0,
254 .ki_rgid = 0xb4,
255 .ki_svgid = 0xb8,
256 .ki_ngroups = 0xbc,
257 .ki_groups = 0xc0,
258 .ki_size = 0x100,
259 .ki_rssize = 0x108,
260 .ki_tsize = 0x118,
261 .ki_dsize = 0x120,
262 .ki_ssize = 0x128,
263 .ki_start = 0x150,
264 .ki_nice = 0x185,
265 .ki_comm = 0x1bf,
266 .ki_tdev = 0x230,
267 .ki_rusage = 0x260,
268 .ki_rusage_ch = 0x2f0,
269
270 .ru_utime = 0x0,
271 .ru_stime = 0x10,
272 .ru_maxrss = 0x20,
273 .ru_minflt = 0x40,
274 .ru_majflt = 0x48,
275 };
276
762c974a
JB
277static struct gdbarch_data *fbsd_gdbarch_data_handle;
278
279struct fbsd_gdbarch_data
280 {
281 struct type *siginfo_type;
282 };
283
284static void *
285init_fbsd_gdbarch_data (struct gdbarch *gdbarch)
286{
287 return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct fbsd_gdbarch_data);
288}
289
290static struct fbsd_gdbarch_data *
291get_fbsd_gdbarch_data (struct gdbarch *gdbarch)
292{
293 return ((struct fbsd_gdbarch_data *)
294 gdbarch_data (gdbarch, fbsd_gdbarch_data_handle));
295}
296
79117428
JB
297/* This is how we want PTIDs from core files to be printed. */
298
7a114964 299static const char *
79117428
JB
300fbsd_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
301{
302 static char buf[80];
303
e38504b3 304 if (ptid.lwp () != 0)
79117428 305 {
e38504b3 306 xsnprintf (buf, sizeof buf, "LWP %ld", ptid.lwp ());
79117428
JB
307 return buf;
308 }
309
310 return normal_pid_to_str (ptid);
311}
312
313/* Extract the name assigned to a thread from a core. Returns the
314 string in a static buffer. */
315
316static const char *
317fbsd_core_thread_name (struct gdbarch *gdbarch, struct thread_info *thr)
318{
319 static char buf[80];
320 struct bfd_section *section;
321 bfd_size_type size;
79117428 322
e38504b3 323 if (thr->ptid.lwp () != 0)
79117428
JB
324 {
325 /* FreeBSD includes a NT_FREEBSD_THRMISC note for each thread
326 whose contents are defined by a "struct thrmisc" declared in
327 <sys/procfs.h> on FreeBSD. The per-thread name is stored as
328 a null-terminated string as the first member of the
329 structure. Rather than define the full structure here, just
330 extract the null-terminated name from the start of the
331 note. */
2af9fc44
JB
332 thread_section_name section_name (".thrmisc", thr->ptid);
333
334 section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
79117428
JB
335 if (section != NULL && bfd_section_size (core_bfd, section) > 0)
336 {
337 /* Truncate the name if it is longer than "buf". */
338 size = bfd_section_size (core_bfd, section);
339 if (size > sizeof buf - 1)
340 size = sizeof buf - 1;
341 if (bfd_get_section_contents (core_bfd, section, buf, (file_ptr) 0,
342 size)
343 && buf[0] != '\0')
344 {
345 buf[size] = '\0';
346
347 /* Note that each thread will report the process command
348 as its thread name instead of an empty name if a name
349 has not been set explicitly. Return a NULL name in
350 that case. */
351 if (strcmp (buf, elf_tdata (core_bfd)->core->program) != 0)
352 return buf;
353 }
354 }
355 }
356
357 return NULL;
358}
359
4b654465
JB
360/* Implement the "core_xfer_siginfo" gdbarch method. */
361
362static LONGEST
363fbsd_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
364 ULONGEST offset, ULONGEST len)
365{
366 size_t siginfo_size;
367
a181c0bf 368 if (gdbarch_long_bit (gdbarch) == 32)
4b654465
JB
369 siginfo_size = SIZE32_SIGINFO_T;
370 else
371 siginfo_size = SIZE64_SIGINFO_T;
372 if (offset > siginfo_size)
373 return -1;
374
375 thread_section_name section_name (".note.freebsdcore.lwpinfo", inferior_ptid);
376 asection *section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
377 if (section == NULL)
378 return -1;
379
380 gdb_byte buf[4];
381 if (!bfd_get_section_contents (core_bfd, section, buf,
382 LWPINFO_OFFSET + LWPINFO_PL_FLAGS, 4))
383 return -1;
384
385 int pl_flags = extract_signed_integer (buf, 4, gdbarch_byte_order (gdbarch));
386 if (!(pl_flags & PL_FLAG_SI))
387 return -1;
388
389 if (offset + len > siginfo_size)
390 len = siginfo_size - offset;
391
392 ULONGEST siginfo_offset;
a181c0bf 393 if (gdbarch_long_bit (gdbarch) == 32)
4b654465
JB
394 siginfo_offset = LWPINFO_OFFSET + LWPINFO32_PL_SIGINFO;
395 else
396 siginfo_offset = LWPINFO_OFFSET + LWPINFO64_PL_SIGINFO;
397
398 if (!bfd_get_section_contents (core_bfd, section, readbuf,
399 siginfo_offset + offset, len))
400 return -1;
401
402 return len;
403}
404
a904c024
AA
405static int
406find_signalled_thread (struct thread_info *info, void *data)
407{
408 if (info->suspend.stop_signal != GDB_SIGNAL_0
e99b03dc 409 && info->ptid.pid () == inferior_ptid.pid ())
a904c024
AA
410 return 1;
411
412 return 0;
413}
414
20a0aab3
JB
415/* Structure for passing information from
416 fbsd_collect_thread_registers via an iterator to
417 fbsd_collect_regset_section_cb. */
a904c024
AA
418
419struct fbsd_collect_regset_section_cb_data
420{
421 const struct regcache *regcache;
422 bfd *obfd;
423 char *note_data;
424 int *note_size;
20a0aab3
JB
425 unsigned long lwp;
426 enum gdb_signal stop_signal;
427 int abort_iteration;
a904c024
AA
428};
429
430static void
a616bb94
AH
431fbsd_collect_regset_section_cb (const char *sect_name, int supply_size,
432 int collect_size, const struct regset *regset,
a904c024
AA
433 const char *human_name, void *cb_data)
434{
435 char *buf;
7567e115
SM
436 struct fbsd_collect_regset_section_cb_data *data
437 = (struct fbsd_collect_regset_section_cb_data *) cb_data;
a904c024 438
20a0aab3
JB
439 if (data->abort_iteration)
440 return;
441
a904c024
AA
442 gdb_assert (regset->collect_regset);
443
a616bb94
AH
444 buf = (char *) xmalloc (collect_size);
445 regset->collect_regset (regset, data->regcache, -1, buf, collect_size);
a904c024
AA
446
447 /* PRSTATUS still needs to be treated specially. */
448 if (strcmp (sect_name, ".reg") == 0)
449 data->note_data = (char *) elfcore_write_prstatus
20a0aab3
JB
450 (data->obfd, data->note_data, data->note_size, data->lwp,
451 gdb_signal_to_host (data->stop_signal), buf);
a904c024
AA
452 else
453 data->note_data = (char *) elfcore_write_register_note
454 (data->obfd, data->note_data, data->note_size,
a616bb94 455 sect_name, buf, collect_size);
a904c024 456 xfree (buf);
20a0aab3
JB
457
458 if (data->note_data == NULL)
459 data->abort_iteration = 1;
460}
461
462/* Records the thread's register state for the corefile note
463 section. */
464
465static char *
466fbsd_collect_thread_registers (const struct regcache *regcache,
467 ptid_t ptid, bfd *obfd,
468 char *note_data, int *note_size,
469 enum gdb_signal stop_signal)
470{
ac7936df 471 struct gdbarch *gdbarch = regcache->arch ();
20a0aab3
JB
472 struct fbsd_collect_regset_section_cb_data data;
473
474 data.regcache = regcache;
475 data.obfd = obfd;
476 data.note_data = note_data;
477 data.note_size = note_size;
478 data.stop_signal = stop_signal;
479 data.abort_iteration = 0;
e38504b3 480 data.lwp = ptid.lwp ();
20a0aab3
JB
481
482 gdbarch_iterate_over_regset_sections (gdbarch,
483 fbsd_collect_regset_section_cb,
484 &data, regcache);
485 return data.note_data;
486}
487
488struct fbsd_corefile_thread_data
489{
490 struct gdbarch *gdbarch;
491 bfd *obfd;
492 char *note_data;
493 int *note_size;
494 enum gdb_signal stop_signal;
495};
496
497/* Records the thread's register state for the corefile note
498 section. */
499
500static void
501fbsd_corefile_thread (struct thread_info *info,
502 struct fbsd_corefile_thread_data *args)
503{
20a0aab3
JB
504 struct regcache *regcache;
505
506 regcache = get_thread_arch_regcache (info->ptid, args->gdbarch);
507
20a0aab3 508 target_fetch_registers (regcache, -1);
20a0aab3
JB
509
510 args->note_data = fbsd_collect_thread_registers
511 (regcache, info->ptid, args->obfd, args->note_data,
512 args->note_size, args->stop_signal);
a904c024
AA
513}
514
515/* Create appropriate note sections for a corefile, returning them in
516 allocated memory. */
517
518static char *
519fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
520{
20a0aab3
JB
521 struct fbsd_corefile_thread_data thread_args;
522 char *note_data = NULL;
a904c024 523 Elf_Internal_Ehdr *i_ehdrp;
20a0aab3 524 struct thread_info *curr_thr, *signalled_thr, *thr;
a904c024
AA
525
526 /* Put a "FreeBSD" label in the ELF header. */
527 i_ehdrp = elf_elfheader (obfd);
528 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
529
530 gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch));
531
a904c024
AA
532 if (get_exec_file (0))
533 {
534 const char *fname = lbasename (get_exec_file (0));
535 char *psargs = xstrdup (fname);
536
537 if (get_inferior_args ())
538 psargs = reconcat (psargs, psargs, " ", get_inferior_args (),
539 (char *) NULL);
540
541 note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
542 fname, psargs);
543 }
544
20a0aab3
JB
545 /* Thread register information. */
546 TRY
547 {
548 update_thread_list ();
549 }
550 CATCH (e, RETURN_MASK_ERROR)
551 {
552 exception_print (gdb_stderr, e);
553 }
554 END_CATCH
555
556 /* Like the kernel, prefer dumping the signalled thread first.
557 "First thread" is what tools use to infer the signalled thread.
558 In case there's more than one signalled thread, prefer the
559 current thread, if it is signalled. */
560 curr_thr = inferior_thread ();
561 if (curr_thr->suspend.stop_signal != GDB_SIGNAL_0)
562 signalled_thr = curr_thr;
563 else
564 {
565 signalled_thr = iterate_over_threads (find_signalled_thread, NULL);
566 if (signalled_thr == NULL)
567 signalled_thr = curr_thr;
568 }
569
570 thread_args.gdbarch = gdbarch;
571 thread_args.obfd = obfd;
572 thread_args.note_data = note_data;
573 thread_args.note_size = note_size;
574 thread_args.stop_signal = signalled_thr->suspend.stop_signal;
575
576 fbsd_corefile_thread (signalled_thr, &thread_args);
577 ALL_NON_EXITED_THREADS (thr)
578 {
579 if (thr == signalled_thr)
580 continue;
e99b03dc 581 if (thr->ptid.pid () != inferior_ptid.pid ())
20a0aab3
JB
582 continue;
583
584 fbsd_corefile_thread (thr, &thread_args);
585 }
586
587 note_data = thread_args.note_data;
588
a904c024
AA
589 return note_data;
590}
591
d2176225
JB
592/* Helper function to generate mappings flags for a single VM map
593 entry in 'info proc mappings'. */
594
595const char *
596fbsd_vm_map_entry_flags (int kve_flags, int kve_protection)
597{
598 static char vm_flags[9];
599
600 vm_flags[0] = (kve_protection & KINFO_VME_PROT_READ) ? 'r' : '-';
601 vm_flags[1] = (kve_protection & KINFO_VME_PROT_WRITE) ? 'w' : '-';
602 vm_flags[2] = (kve_protection & KINFO_VME_PROT_EXEC) ? 'x' : '-';
603 vm_flags[3] = ' ';
604 vm_flags[4] = (kve_flags & KINFO_VME_FLAG_COW) ? 'C' : '-';
605 vm_flags[5] = (kve_flags & KINFO_VME_FLAG_NEEDS_COPY) ? 'N' : '-';
606 vm_flags[6] = (kve_flags & KINFO_VME_FLAG_SUPER) ? 'S' : '-';
607 vm_flags[7] = (kve_flags & KINFO_VME_FLAG_GROWS_UP) ? 'U'
608 : (kve_flags & KINFO_VME_FLAG_GROWS_DOWN) ? 'D' : '-';
609 vm_flags[8] = '\0';
610
611 return vm_flags;
612}
613
614/* Implement "info proc mappings" for a corefile. */
615
616static void
617fbsd_core_info_proc_mappings (struct gdbarch *gdbarch)
618{
619 asection *section;
620 unsigned char *descdata, *descend;
621 size_t note_size;
622
623 section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.vmmap");
624 if (section == NULL)
625 {
626 warning (_("unable to find mappings in core file"));
627 return;
628 }
629
630 note_size = bfd_get_section_size (section);
631 if (note_size < 4)
632 error (_("malformed core note - too short for header"));
633
634 gdb::def_vector<unsigned char> contents (note_size);
635 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
636 0, note_size))
637 error (_("could not get core note contents"));
638
639 descdata = contents.data ();
640 descend = descdata + note_size;
641
642 /* Skip over the structure size. */
643 descdata += 4;
644
645 printf_filtered (_("Mapped address spaces:\n\n"));
646 if (gdbarch_addr_bit (gdbarch) == 64)
647 {
648 printf_filtered (" %18s %18s %10s %10s %9s %s\n",
649 "Start Addr",
650 " End Addr",
651 " Size", " Offset", "Flags ", "File");
652 }
653 else
654 {
655 printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
656 "Start Addr",
657 " End Addr",
658 " Size", " Offset", "Flags ", "File");
659 }
660
661 while (descdata + KVE_PATH < descend)
662 {
663 ULONGEST start, end, offset, flags, prot, structsize;
664
665 structsize = bfd_get_32 (core_bfd, descdata + KVE_STRUCTSIZE);
666 if (structsize < KVE_PATH)
667 error (_("malformed core note - vmmap entry too small"));
668
669 start = bfd_get_64 (core_bfd, descdata + KVE_START);
670 end = bfd_get_64 (core_bfd, descdata + KVE_END);
671 offset = bfd_get_64 (core_bfd, descdata + KVE_OFFSET);
672 flags = bfd_get_32 (core_bfd, descdata + KVE_FLAGS);
673 prot = bfd_get_32 (core_bfd, descdata + KVE_PROTECTION);
674 if (gdbarch_addr_bit (gdbarch) == 64)
675 {
676 printf_filtered (" %18s %18s %10s %10s %9s %s\n",
677 paddress (gdbarch, start),
678 paddress (gdbarch, end),
679 hex_string (end - start),
680 hex_string (offset),
681 fbsd_vm_map_entry_flags (flags, prot),
682 descdata + KVE_PATH);
683 }
684 else
685 {
686 printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
687 paddress (gdbarch, start),
688 paddress (gdbarch, end),
689 hex_string (end - start),
690 hex_string (offset),
691 fbsd_vm_map_entry_flags (flags, prot),
692 descdata + KVE_PATH);
693 }
694
695 descdata += structsize;
696 }
697}
698
699/* Fetch the pathname of a vnode for a single file descriptor from the
700 file table core note. */
701
702static gdb::unique_xmalloc_ptr<char>
703fbsd_core_vnode_path (struct gdbarch *gdbarch, int fd)
704{
705 asection *section;
706 unsigned char *descdata, *descend;
707 size_t note_size;
708
709 section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.files");
710 if (section == NULL)
711 return nullptr;
712
713 note_size = bfd_get_section_size (section);
714 if (note_size < 4)
715 error (_("malformed core note - too short for header"));
716
717 gdb::def_vector<unsigned char> contents (note_size);
718 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
719 0, note_size))
720 error (_("could not get core note contents"));
721
722 descdata = contents.data ();
723 descend = descdata + note_size;
724
725 /* Skip over the structure size. */
726 descdata += 4;
727
728 while (descdata + KVE_PATH < descend)
729 {
730 ULONGEST structsize;
731
732 structsize = bfd_get_32 (core_bfd, descdata + KF_STRUCTSIZE);
733 if (structsize < KVE_PATH)
734 error (_("malformed core note - vmmap entry too small"));
735
736 if (bfd_get_32 (core_bfd, descdata + KF_TYPE) == KINFO_FILE_TYPE_VNODE
737 && bfd_get_signed_32 (core_bfd, descdata + KF_FD) == fd)
738 {
739 char *path = (char *) descdata + KF_PATH;
740 return gdb::unique_xmalloc_ptr<char> (xstrdup (path));
741 }
742
743 descdata += structsize;
744 }
745 return nullptr;
746}
747
748/* Helper function to read a struct timeval. */
749
750static void
751fbsd_core_fetch_timeval (struct gdbarch *gdbarch, unsigned char *data,
752 LONGEST &sec, ULONGEST &usec)
753{
754 if (gdbarch_addr_bit (gdbarch) == 64)
755 {
756 sec = bfd_get_signed_64 (core_bfd, data);
757 usec = bfd_get_64 (core_bfd, data + 8);
758 }
759 else if (bfd_get_arch (core_bfd) == bfd_arch_i386)
760 {
761 sec = bfd_get_signed_32 (core_bfd, data);
762 usec = bfd_get_32 (core_bfd, data + 4);
763 }
764 else
765 {
766 sec = bfd_get_signed_64 (core_bfd, data);
767 usec = bfd_get_32 (core_bfd, data + 8);
768 }
769}
770
771/* Print out the contents of a signal set. */
772
773static void
774fbsd_print_sigset (const char *descr, unsigned char *sigset)
775{
776 printf_filtered ("%s: ", descr);
777 for (int i = 0; i < SIG_WORDS; i++)
778 printf_filtered ("%08x ",
779 (unsigned int) bfd_get_32 (core_bfd, sigset + i * 4));
780 printf_filtered ("\n");
781}
782
783/* Implement "info proc status" for a corefile. */
784
785static void
786fbsd_core_info_proc_status (struct gdbarch *gdbarch)
787{
788 const struct kinfo_proc_layout *kp;
789 asection *section;
d2176225
JB
790 unsigned char *descdata;
791 int addr_bit, long_bit;
792 size_t note_size;
793 ULONGEST value;
794 LONGEST sec;
795
796 section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.proc");
797 if (section == NULL)
798 {
799 warning (_("unable to find process info in core file"));
800 return;
801 }
802
803 addr_bit = gdbarch_addr_bit (gdbarch);
804 if (addr_bit == 64)
805 kp = &kinfo_proc_layout_64;
806 else if (bfd_get_arch (core_bfd) == bfd_arch_i386)
807 kp = &kinfo_proc_layout_i386;
808 else
809 kp = &kinfo_proc_layout_32;
810 long_bit = gdbarch_long_bit (gdbarch);
811
812 /*
813 * Ensure that the note is large enough for all of the fields fetched
814 * by this function. In particular, the note must contain the 32-bit
815 * structure size, then it must be long enough to access the last
816 * field used (ki_rusage_ch.ru_majflt) which is the size of a long.
817 */
818 note_size = bfd_get_section_size (section);
819 if (note_size < (4 + kp->ki_rusage_ch + kp->ru_majflt
820 + long_bit / TARGET_CHAR_BIT))
821 error (_("malformed core note - too short"));
822
823 gdb::def_vector<unsigned char> contents (note_size);
824 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
825 0, note_size))
826 error (_("could not get core note contents"));
827
828 descdata = contents.data ();
829
830 /* Skip over the structure size. */
831 descdata += 4;
832
833 /* Verify 'ki_layout' is 0. */
834 if (bfd_get_32 (core_bfd, descdata + kp->ki_layout) != 0)
835 {
836 warning (_("unsupported process information in core file"));
837 return;
838 }
839
840 printf_filtered ("Name: %.19s\n", descdata + kp->ki_comm);
841 printf_filtered ("Process ID: %s\n",
842 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_pid)));
843 printf_filtered ("Parent process: %s\n",
844 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_ppid)));
845 printf_filtered ("Process group: %s\n",
846 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_pgid)));
847 printf_filtered ("Session id: %s\n",
848 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_sid)));
849
850 /* FreeBSD 12.0 and later store a 64-bit dev_t at 'ki_tdev'. Older
851 kernels store a 32-bit dev_t at 'ki_tdev_freebsd11'. In older
852 kernels the 64-bit 'ki_tdev' field is in a reserved section of
853 the structure that is cleared to zero. Assume that a zero value
854 in ki_tdev indicates a core dump from an older kernel and use the
855 value in 'ki_tdev_freebsd11' instead. */
856 value = bfd_get_64 (core_bfd, descdata + kp->ki_tdev);
857 if (value == 0)
858 value = bfd_get_32 (core_bfd, descdata + kp->ki_tdev_freebsd11);
859 printf_filtered ("TTY: %s\n", pulongest (value));
860 printf_filtered ("TTY owner process group: %s\n",
861 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_tpgid)));
862 printf_filtered ("User IDs (real, effective, saved): %s %s %s\n",
863 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_ruid)),
864 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_uid)),
865 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_svuid)));
866 printf_filtered ("Group IDs (real, effective, saved): %s %s %s\n",
867 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_rgid)),
868 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_groups)),
869 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_svgid)));
870 printf_filtered ("Groups: ");
871 uint16_t ngroups = bfd_get_16 (core_bfd, descdata + kp->ki_ngroups);
872 for (int i = 0; i < ngroups; i++)
873 printf_filtered ("%s ",
874 pulongest (bfd_get_32 (core_bfd,
875 descdata + kp->ki_groups + i * 4)));
876 printf_filtered ("\n");
877 value = bfd_get (long_bit, core_bfd,
878 descdata + kp->ki_rusage + kp->ru_minflt);
879 printf_filtered ("Minor faults (no memory page): %s\n", pulongest (value));
880 value = bfd_get (long_bit, core_bfd,
881 descdata + kp->ki_rusage_ch + kp->ru_minflt);
882 printf_filtered ("Minor faults, children: %s\n", pulongest (value));
883 value = bfd_get (long_bit, core_bfd,
884 descdata + kp->ki_rusage + kp->ru_majflt);
885 printf_filtered ("Major faults (memory page faults): %s\n",
886 pulongest (value));
887 value = bfd_get (long_bit, core_bfd,
888 descdata + kp->ki_rusage_ch + kp->ru_majflt);
889 printf_filtered ("Major faults, children: %s\n", pulongest (value));
890 fbsd_core_fetch_timeval (gdbarch,
891 descdata + kp->ki_rusage + kp->ru_utime,
892 sec, value);
893 printf_filtered ("utime: %s.%06d\n", plongest (sec), (int) value);
894 fbsd_core_fetch_timeval (gdbarch,
895 descdata + kp->ki_rusage + kp->ru_stime,
896 sec, value);
897 printf_filtered ("stime: %s.%06d\n", plongest (sec), (int) value);
898 fbsd_core_fetch_timeval (gdbarch,
899 descdata + kp->ki_rusage_ch + kp->ru_utime,
900 sec, value);
901 printf_filtered ("utime, children: %s.%06d\n", plongest (sec), (int) value);
902 fbsd_core_fetch_timeval (gdbarch,
903 descdata + kp->ki_rusage_ch + kp->ru_stime,
904 sec, value);
905 printf_filtered ("stime, children: %s.%06d\n", plongest (sec), (int) value);
906 printf_filtered ("'nice' value: %d\n",
907 bfd_get_signed_8 (core_bfd, descdata + kp->ki_nice));
908 fbsd_core_fetch_timeval (gdbarch, descdata + kp->ki_start, sec, value);
909 printf_filtered ("Start time: %s.%06d\n", plongest (sec), (int) value);
910 printf_filtered ("Virtual memory size: %s kB\n",
911 pulongest (bfd_get (addr_bit, core_bfd,
912 descdata + kp->ki_size) / 1024));
913 printf_filtered ("Data size: %s pages\n",
914 pulongest (bfd_get (addr_bit, core_bfd,
915 descdata + kp->ki_dsize)));
916 printf_filtered ("Stack size: %s pages\n",
917 pulongest (bfd_get (addr_bit, core_bfd,
918 descdata + kp->ki_ssize)));
919 printf_filtered ("Text size: %s pages\n",
920 pulongest (bfd_get (addr_bit, core_bfd,
921 descdata + kp->ki_tsize)));
922 printf_filtered ("Resident set size: %s pages\n",
923 pulongest (bfd_get (addr_bit, core_bfd,
924 descdata + kp->ki_rssize)));
925 printf_filtered ("Maximum RSS: %s pages\n",
926 pulongest (bfd_get (long_bit, core_bfd,
927 descdata + kp->ki_rusage
928 + kp->ru_maxrss)));
929 fbsd_print_sigset ("Ignored Signals", descdata + kp->ki_sigignore);
930 fbsd_print_sigset ("Caught Signals", descdata + kp->ki_sigcatch);
931}
932
933/* Implement the "core_info_proc" gdbarch method. */
934
935static void
936fbsd_core_info_proc (struct gdbarch *gdbarch, const char *args,
937 enum info_proc_what what)
938{
939 bool do_cmdline = false;
940 bool do_cwd = false;
941 bool do_exe = false;
942 bool do_mappings = false;
943 bool do_status = false;
944 int pid;
945
946 switch (what)
947 {
948 case IP_MINIMAL:
949 do_cmdline = true;
950 do_cwd = true;
951 do_exe = true;
952 break;
953 case IP_MAPPINGS:
954 do_mappings = true;
955 break;
956 case IP_STATUS:
957 case IP_STAT:
958 do_status = true;
959 break;
960 case IP_CMDLINE:
961 do_cmdline = true;
962 break;
963 case IP_EXE:
964 do_exe = true;
965 break;
966 case IP_CWD:
967 do_cwd = true;
968 break;
969 case IP_ALL:
970 do_cmdline = true;
971 do_cwd = true;
972 do_exe = true;
973 do_mappings = true;
974 do_status = true;
975 break;
976 default:
977 return;
978 }
979
980 pid = bfd_core_file_pid (core_bfd);
981 if (pid != 0)
982 printf_filtered (_("process %d\n"), pid);
983
984 if (do_cmdline)
985 {
986 const char *cmdline;
987
988 cmdline = bfd_core_file_failing_command (core_bfd);
989 if (cmdline)
990 printf_filtered ("cmdline = '%s'\n", cmdline);
991 else
992 warning (_("Command line unavailable"));
993 }
994 if (do_cwd)
995 {
996 gdb::unique_xmalloc_ptr<char> cwd =
997 fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_CWD);
998 if (cwd)
999 printf_filtered ("cwd = '%s'\n", cwd.get ());
1000 else
1001 warning (_("unable to read current working directory"));
1002 }
1003 if (do_exe)
1004 {
1005 gdb::unique_xmalloc_ptr<char> exe =
1006 fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_TEXT);
1007 if (exe)
1008 printf_filtered ("exe = '%s'\n", exe.get ());
1009 else
1010 warning (_("unable to read executable path name"));
1011 }
1012 if (do_mappings)
1013 fbsd_core_info_proc_mappings (gdbarch);
1014 if (do_status)
1015 fbsd_core_info_proc_status (gdbarch);
1016}
1017
82372b2f
JB
1018/* Print descriptions of FreeBSD-specific AUXV entries to FILE. */
1019
1020static void
1021fbsd_print_auxv_entry (struct gdbarch *gdbarch, struct ui_file *file,
1022 CORE_ADDR type, CORE_ADDR val)
1023{
1024 const char *name;
1025 const char *description;
1026 enum auxv_format format;
1027
1028 switch (type)
1029 {
1030#define _TAGNAME(tag) #tag
1031#define TAGNAME(tag) _TAGNAME(AT_##tag)
1032#define TAG(tag, text, kind) \
1033 case AT_FREEBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
1034 TAG (EXECPATH, _("Executable path"), AUXV_FORMAT_STR);
1035 TAG (CANARY, _("Canary for SSP"), AUXV_FORMAT_HEX);
1036 TAG (CANARYLEN, ("Length of the SSP canary"), AUXV_FORMAT_DEC);
1037 TAG (OSRELDATE, _("OSRELDATE"), AUXV_FORMAT_DEC);
1038 TAG (NCPUS, _("Number of CPUs"), AUXV_FORMAT_DEC);
1039 TAG (PAGESIZES, _("Pagesizes"), AUXV_FORMAT_HEX);
1040 TAG (PAGESIZESLEN, _("Number of pagesizes"), AUXV_FORMAT_DEC);
1041 TAG (TIMEKEEP, _("Pointer to timehands"), AUXV_FORMAT_HEX);
1042 TAG (STACKPROT, _("Initial stack protection"), AUXV_FORMAT_HEX);
12c4bd7f
JB
1043 TAG (EHDRFLAGS, _("ELF header e_flags"), AUXV_FORMAT_HEX);
1044 TAG (HWCAP, _("Machine-dependent CPU capability hints"), AUXV_FORMAT_HEX);
82372b2f
JB
1045 default:
1046 default_print_auxv_entry (gdbarch, file, type, val);
1047 return;
1048 }
1049
1050 fprint_auxv_entry (file, name, description, format, type, val);
1051}
1052
762c974a
JB
1053/* Implement the "get_siginfo_type" gdbarch method. */
1054
1055static struct type *
1056fbsd_get_siginfo_type (struct gdbarch *gdbarch)
1057{
1058 struct fbsd_gdbarch_data *fbsd_gdbarch_data;
1059 struct type *int_type, *int32_type, *uint32_type, *long_type, *void_ptr_type;
1060 struct type *uid_type, *pid_type;
1061 struct type *sigval_type, *reason_type;
1062 struct type *siginfo_type;
1063 struct type *type;
1064
1065 fbsd_gdbarch_data = get_fbsd_gdbarch_data (gdbarch);
1066 if (fbsd_gdbarch_data->siginfo_type != NULL)
1067 return fbsd_gdbarch_data->siginfo_type;
1068
1069 int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
1070 0, "int");
1071 int32_type = arch_integer_type (gdbarch, 32, 0, "int32_t");
1072 uint32_type = arch_integer_type (gdbarch, 32, 1, "uint32_t");
1073 long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
1074 0, "long");
1075 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
1076
1077 /* union sigval */
1078 sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
1079 TYPE_NAME (sigval_type) = xstrdup ("sigval");
1080 append_composite_type_field (sigval_type, "sival_int", int_type);
1081 append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
1082
1083 /* __pid_t */
1084 pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
77b7c781 1085 TYPE_LENGTH (int32_type) * TARGET_CHAR_BIT, "__pid_t");
762c974a
JB
1086 TYPE_TARGET_TYPE (pid_type) = int32_type;
1087 TYPE_TARGET_STUB (pid_type) = 1;
1088
1089 /* __uid_t */
1090 uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
77b7c781
UW
1091 TYPE_LENGTH (uint32_type) * TARGET_CHAR_BIT,
1092 "__uid_t");
762c974a
JB
1093 TYPE_TARGET_TYPE (uid_type) = uint32_type;
1094 TYPE_TARGET_STUB (uid_type) = 1;
1095
1096 /* _reason */
1097 reason_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
1098
1099 /* _fault */
1100 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1101 append_composite_type_field (type, "si_trapno", int_type);
1102 append_composite_type_field (reason_type, "_fault", type);
1103
1104 /* _timer */
1105 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1106 append_composite_type_field (type, "si_timerid", int_type);
1107 append_composite_type_field (type, "si_overrun", int_type);
1108 append_composite_type_field (reason_type, "_timer", type);
1109
1110 /* _mesgq */
1111 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1112 append_composite_type_field (type, "si_mqd", int_type);
1113 append_composite_type_field (reason_type, "_mesgq", type);
1114
1115 /* _poll */
1116 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1117 append_composite_type_field (type, "si_band", long_type);
1118 append_composite_type_field (reason_type, "_poll", type);
1119
1120 /* __spare__ */
1121 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1122 append_composite_type_field (type, "__spare1__", long_type);
1123 append_composite_type_field (type, "__spare2__",
1124 init_vector_type (int_type, 7));
1125 append_composite_type_field (reason_type, "__spare__", type);
1126
1127 /* struct siginfo */
1128 siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1129 TYPE_NAME (siginfo_type) = xstrdup ("siginfo");
1130 append_composite_type_field (siginfo_type, "si_signo", int_type);
1131 append_composite_type_field (siginfo_type, "si_errno", int_type);
1132 append_composite_type_field (siginfo_type, "si_code", int_type);
1133 append_composite_type_field (siginfo_type, "si_pid", pid_type);
1134 append_composite_type_field (siginfo_type, "si_uid", uid_type);
1135 append_composite_type_field (siginfo_type, "si_status", int_type);
1136 append_composite_type_field (siginfo_type, "si_addr", void_ptr_type);
1137 append_composite_type_field (siginfo_type, "si_value", sigval_type);
1138 append_composite_type_field (siginfo_type, "_reason", reason_type);
1139
1140 fbsd_gdbarch_data->siginfo_type = siginfo_type;
1141
1142 return siginfo_type;
1143}
1144
e6cdd38e
JB
1145/* Implement the "get_syscall_number" gdbarch method. */
1146
1147static LONGEST
00431a78 1148fbsd_get_syscall_number (struct gdbarch *gdbarch, thread_info *thread)
e6cdd38e
JB
1149{
1150
1151 /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
1152 native targets fetch the system call number from the
1153 'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
1154 However, system call catching requires this function to be
1155 set. */
1156
1157 internal_error (__FILE__, __LINE__, _("fbsd_get_sycall_number called"));
1158}
1159
1736a7bd 1160/* To be called from GDB_OSABI_FREEBSD handlers. */
a904c024
AA
1161
1162void
1163fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1164{
79117428
JB
1165 set_gdbarch_core_pid_to_str (gdbarch, fbsd_core_pid_to_str);
1166 set_gdbarch_core_thread_name (gdbarch, fbsd_core_thread_name);
4b654465 1167 set_gdbarch_core_xfer_siginfo (gdbarch, fbsd_core_xfer_siginfo);
a904c024 1168 set_gdbarch_make_corefile_notes (gdbarch, fbsd_make_corefile_notes);
d2176225 1169 set_gdbarch_core_info_proc (gdbarch, fbsd_core_info_proc);
82372b2f 1170 set_gdbarch_print_auxv_entry (gdbarch, fbsd_print_auxv_entry);
762c974a 1171 set_gdbarch_get_siginfo_type (gdbarch, fbsd_get_siginfo_type);
e6cdd38e
JB
1172
1173 /* `catch syscall' */
1174 set_xml_syscall_file_name (gdbarch, "syscalls/freebsd.xml");
1175 set_gdbarch_get_syscall_number (gdbarch, fbsd_get_syscall_number);
a904c024 1176}
762c974a 1177
762c974a
JB
1178void
1179_initialize_fbsd_tdep (void)
1180{
1181 fbsd_gdbarch_data_handle =
1182 gdbarch_data_register_post_init (init_fbsd_gdbarch_data);
1183}