Automatic date update in version.in
[binutils-gdb.git] / gdb / netbsd-tdep.c
blob953b329674a48a102a6e951799720463fb74bfbe
1 /* Common target-dependent code for NetBSD systems.
3 Copyright (C) 2002-2024 Free Software Foundation, Inc.
5 Contributed by Wasabi Systems, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "auxv.h"
23 #include "solib-svr4.h"
24 #include "netbsd-tdep.h"
25 #include "gdbarch.h"
26 #include "objfiles.h"
27 #include "xml-syscall.h"
29 /* Flags in the 'kve_protection' field in struct kinfo_vmentry. These
30 match the KVME_PROT_* constants in <sys/sysctl.h>. */
32 #define KINFO_VME_PROT_READ 0x00000001
33 #define KINFO_VME_PROT_WRITE 0x00000002
34 #define KINFO_VME_PROT_EXEC 0x00000004
36 /* Flags in the 'kve_flags' field in struct kinfo_vmentry. These
37 match the KVME_FLAG_* constants in <sys/sysctl.h>. */
39 #define KINFO_VME_FLAG_COW 0x00000001
40 #define KINFO_VME_FLAG_NEEDS_COPY 0x00000002
41 #define KINFO_VME_FLAG_NOCOREDUMP 0x00000004
42 #define KINFO_VME_FLAG_PAGEABLE 0x00000008
43 #define KINFO_VME_FLAG_GROWS_UP 0x00000010
44 #define KINFO_VME_FLAG_GROWS_DOWN 0x00000020
46 int
47 nbsd_pc_in_sigtramp (CORE_ADDR pc, const char *func_name)
49 /* Check for libc-provided signal trampoline. All such trampolines
50 have function names which begin with "__sigtramp". */
52 return (func_name != NULL
53 && startswith (func_name, "__sigtramp"));
56 /* This enum is derived from NETBSD's <sys/signal.h>. */
58 enum
60 NBSD_SIGHUP = 1,
61 NBSD_SIGINT = 2,
62 NBSD_SIGQUIT = 3,
63 NBSD_SIGILL = 4,
64 NBSD_SIGTRAP = 5,
65 NBSD_SIGABRT = 6,
66 NBSD_SIGEMT = 7,
67 NBSD_SIGFPE = 8,
68 NBSD_SIGKILL = 9,
69 NBSD_SIGBUS = 10,
70 NBSD_SIGSEGV = 11,
71 NBSD_SIGSYS = 12,
72 NBSD_SIGPIPE = 13,
73 NBSD_SIGALRM = 14,
74 NBSD_SIGTERM = 15,
75 NBSD_SIGURG = 16,
76 NBSD_SIGSTOP = 17,
77 NBSD_SIGTSTP = 18,
78 NBSD_SIGCONT = 19,
79 NBSD_SIGCHLD = 20,
80 NBSD_SIGTTIN = 21,
81 NBSD_SIGTTOU = 22,
82 NBSD_SIGIO = 23,
83 NBSD_SIGXCPU = 24,
84 NBSD_SIGXFSZ = 25,
85 NBSD_SIGVTALRM = 26,
86 NBSD_SIGPROF = 27,
87 NBSD_SIGWINCH = 28,
88 NBSD_SIGINFO = 29,
89 NBSD_SIGUSR1 = 30,
90 NBSD_SIGUSR2 = 31,
91 NBSD_SIGPWR = 32,
92 NBSD_SIGRTMIN = 33,
93 NBSD_SIGRTMAX = 63,
96 /* Implement the "gdb_signal_from_target" gdbarch method. */
98 static enum gdb_signal
99 nbsd_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
101 switch (signal)
103 case 0:
104 return GDB_SIGNAL_0;
106 case NBSD_SIGHUP:
107 return GDB_SIGNAL_HUP;
109 case NBSD_SIGINT:
110 return GDB_SIGNAL_INT;
112 case NBSD_SIGQUIT:
113 return GDB_SIGNAL_QUIT;
115 case NBSD_SIGILL:
116 return GDB_SIGNAL_ILL;
118 case NBSD_SIGTRAP:
119 return GDB_SIGNAL_TRAP;
121 case NBSD_SIGABRT:
122 return GDB_SIGNAL_ABRT;
124 case NBSD_SIGEMT:
125 return GDB_SIGNAL_EMT;
127 case NBSD_SIGFPE:
128 return GDB_SIGNAL_FPE;
130 case NBSD_SIGKILL:
131 return GDB_SIGNAL_KILL;
133 case NBSD_SIGBUS:
134 return GDB_SIGNAL_BUS;
136 case NBSD_SIGSEGV:
137 return GDB_SIGNAL_SEGV;
139 case NBSD_SIGSYS:
140 return GDB_SIGNAL_SYS;
142 case NBSD_SIGPIPE:
143 return GDB_SIGNAL_PIPE;
145 case NBSD_SIGALRM:
146 return GDB_SIGNAL_ALRM;
148 case NBSD_SIGTERM:
149 return GDB_SIGNAL_TERM;
151 case NBSD_SIGURG:
152 return GDB_SIGNAL_URG;
154 case NBSD_SIGSTOP:
155 return GDB_SIGNAL_STOP;
157 case NBSD_SIGTSTP:
158 return GDB_SIGNAL_TSTP;
160 case NBSD_SIGCONT:
161 return GDB_SIGNAL_CONT;
163 case NBSD_SIGCHLD:
164 return GDB_SIGNAL_CHLD;
166 case NBSD_SIGTTIN:
167 return GDB_SIGNAL_TTIN;
169 case NBSD_SIGTTOU:
170 return GDB_SIGNAL_TTOU;
172 case NBSD_SIGIO:
173 return GDB_SIGNAL_IO;
175 case NBSD_SIGXCPU:
176 return GDB_SIGNAL_XCPU;
178 case NBSD_SIGXFSZ:
179 return GDB_SIGNAL_XFSZ;
181 case NBSD_SIGVTALRM:
182 return GDB_SIGNAL_VTALRM;
184 case NBSD_SIGPROF:
185 return GDB_SIGNAL_PROF;
187 case NBSD_SIGWINCH:
188 return GDB_SIGNAL_WINCH;
190 case NBSD_SIGINFO:
191 return GDB_SIGNAL_INFO;
193 case NBSD_SIGUSR1:
194 return GDB_SIGNAL_USR1;
196 case NBSD_SIGUSR2:
197 return GDB_SIGNAL_USR2;
199 case NBSD_SIGPWR:
200 return GDB_SIGNAL_PWR;
202 /* SIGRTMIN and SIGRTMAX are not continuous in <gdb/signals.def>,
203 therefore we have to handle them here. */
204 case NBSD_SIGRTMIN:
205 return GDB_SIGNAL_REALTIME_33;
207 case NBSD_SIGRTMAX:
208 return GDB_SIGNAL_REALTIME_63;
211 if (signal >= NBSD_SIGRTMIN + 1 && signal <= NBSD_SIGRTMAX - 1)
213 int offset = signal - NBSD_SIGRTMIN + 1;
215 return (enum gdb_signal) ((int) GDB_SIGNAL_REALTIME_34 + offset);
218 return GDB_SIGNAL_UNKNOWN;
221 /* Implement the "gdb_signal_to_target" gdbarch method. */
223 static int
224 nbsd_gdb_signal_to_target (struct gdbarch *gdbarch,
225 enum gdb_signal signal)
227 switch (signal)
229 case GDB_SIGNAL_0:
230 return 0;
232 case GDB_SIGNAL_HUP:
233 return NBSD_SIGHUP;
235 case GDB_SIGNAL_INT:
236 return NBSD_SIGINT;
238 case GDB_SIGNAL_QUIT:
239 return NBSD_SIGQUIT;
241 case GDB_SIGNAL_ILL:
242 return NBSD_SIGILL;
244 case GDB_SIGNAL_TRAP:
245 return NBSD_SIGTRAP;
247 case GDB_SIGNAL_ABRT:
248 return NBSD_SIGABRT;
250 case GDB_SIGNAL_EMT:
251 return NBSD_SIGEMT;
253 case GDB_SIGNAL_FPE:
254 return NBSD_SIGFPE;
256 case GDB_SIGNAL_KILL:
257 return NBSD_SIGKILL;
259 case GDB_SIGNAL_BUS:
260 return NBSD_SIGBUS;
262 case GDB_SIGNAL_SEGV:
263 return NBSD_SIGSEGV;
265 case GDB_SIGNAL_SYS:
266 return NBSD_SIGSYS;
268 case GDB_SIGNAL_PIPE:
269 return NBSD_SIGPIPE;
271 case GDB_SIGNAL_ALRM:
272 return NBSD_SIGALRM;
274 case GDB_SIGNAL_TERM:
275 return NBSD_SIGTERM;
277 case GDB_SIGNAL_URG:
278 return NBSD_SIGSTOP;
280 case GDB_SIGNAL_TSTP:
281 return NBSD_SIGTSTP;
283 case GDB_SIGNAL_CONT:
284 return NBSD_SIGCONT;
286 case GDB_SIGNAL_CHLD:
287 return NBSD_SIGCHLD;
289 case GDB_SIGNAL_TTIN:
290 return NBSD_SIGTTIN;
292 case GDB_SIGNAL_TTOU:
293 return NBSD_SIGTTOU;
295 case GDB_SIGNAL_IO:
296 return NBSD_SIGIO;
298 case GDB_SIGNAL_XCPU:
299 return NBSD_SIGXCPU;
301 case GDB_SIGNAL_XFSZ:
302 return NBSD_SIGXFSZ;
304 case GDB_SIGNAL_VTALRM:
305 return NBSD_SIGVTALRM;
307 case GDB_SIGNAL_PROF:
308 return NBSD_SIGPROF;
310 case GDB_SIGNAL_WINCH:
311 return NBSD_SIGWINCH;
313 case GDB_SIGNAL_INFO:
314 return NBSD_SIGINFO;
316 case GDB_SIGNAL_USR1:
317 return NBSD_SIGUSR1;
319 case GDB_SIGNAL_USR2:
320 return NBSD_SIGUSR2;
322 case GDB_SIGNAL_PWR:
323 return NBSD_SIGPWR;
325 /* GDB_SIGNAL_REALTIME_33 is not continuous in <gdb/signals.def>,
326 therefore we have to handle it here. */
327 case GDB_SIGNAL_REALTIME_33:
328 return NBSD_SIGRTMIN;
330 /* Same comment applies to _64. */
331 case GDB_SIGNAL_REALTIME_63:
332 return NBSD_SIGRTMAX;
335 if (signal >= GDB_SIGNAL_REALTIME_34
336 && signal <= GDB_SIGNAL_REALTIME_62)
338 int offset = signal - GDB_SIGNAL_REALTIME_32;
340 return NBSD_SIGRTMIN + 1 + offset;
343 return -1;
346 /* Shared library resolver handling. */
348 static CORE_ADDR
349 nbsd_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
351 struct bound_minimal_symbol msym;
353 msym = lookup_minimal_symbol ("_rtld_bind_start", NULL, NULL);
354 if (msym.minsym && msym.value_address () == pc)
355 return frame_unwind_caller_pc (get_current_frame ());
356 else
357 return find_solib_trampoline_target (get_current_frame (), pc);
360 struct nbsd_gdbarch_data
362 struct type *siginfo_type = nullptr;
365 static const registry<gdbarch>::key<nbsd_gdbarch_data>
366 nbsd_gdbarch_data_handle;
368 static struct nbsd_gdbarch_data *
369 get_nbsd_gdbarch_data (struct gdbarch *gdbarch)
371 struct nbsd_gdbarch_data *result = nbsd_gdbarch_data_handle.get (gdbarch);
372 if (result == nullptr)
373 result = nbsd_gdbarch_data_handle.emplace (gdbarch);
374 return result;
377 /* Implement the "get_siginfo_type" gdbarch method. */
379 static struct type *
380 nbsd_get_siginfo_type (struct gdbarch *gdbarch)
382 nbsd_gdbarch_data *nbsd_gdbarch_data = get_nbsd_gdbarch_data (gdbarch);
383 if (nbsd_gdbarch_data->siginfo_type != NULL)
384 return nbsd_gdbarch_data->siginfo_type;
386 type *char_type = builtin_type (gdbarch)->builtin_char;
387 type *int_type = builtin_type (gdbarch)->builtin_int;
388 type *long_type = builtin_type (gdbarch)->builtin_long;
390 type *void_ptr_type
391 = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
393 type *int32_type = builtin_type (gdbarch)->builtin_int32;
394 type *uint32_type = builtin_type (gdbarch)->builtin_uint32;
395 type *uint64_type = builtin_type (gdbarch)->builtin_uint64;
397 bool lp64 = void_ptr_type->length () == 8;
398 size_t char_bits = gdbarch_addressable_memory_unit_size (gdbarch) * 8;
400 /* pid_t */
401 type_allocator alloc (gdbarch);
402 type *pid_type = alloc.new_type (TYPE_CODE_TYPEDEF,
403 int32_type->length () * char_bits,
404 "pid_t");
405 pid_type->set_target_type (int32_type);
407 /* uid_t */
408 type *uid_type = alloc.new_type (TYPE_CODE_TYPEDEF,
409 uint32_type->length () * char_bits,
410 "uid_t");
411 uid_type->set_target_type (uint32_type);
413 /* clock_t */
414 type *clock_type = alloc.new_type (TYPE_CODE_TYPEDEF,
415 int_type->length () * char_bits,
416 "clock_t");
417 clock_type->set_target_type (int_type);
419 /* lwpid_t */
420 type *lwpid_type = alloc.new_type (TYPE_CODE_TYPEDEF,
421 int32_type->length () * char_bits,
422 "lwpid_t");
423 lwpid_type->set_target_type (int32_type);
425 /* union sigval */
426 type *sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
427 sigval_type->set_name (gdbarch_obstack_strdup (gdbarch, "sigval"));
428 append_composite_type_field (sigval_type, "sival_int", int_type);
429 append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
431 /* union _option */
432 type *option_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
433 option_type->set_name (gdbarch_obstack_strdup (gdbarch, "_option"));
434 append_composite_type_field (option_type, "_pe_other_pid", pid_type);
435 append_composite_type_field (option_type, "_pe_lwp", lwpid_type);
437 /* union _reason */
438 type *reason_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
440 /* _rt */
441 type *t = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
442 append_composite_type_field (t, "_pid", pid_type);
443 append_composite_type_field (t, "_uid", uid_type);
444 append_composite_type_field (t, "_value", sigval_type);
445 append_composite_type_field (reason_type, "_rt", t);
447 /* _child */
448 t = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
449 append_composite_type_field (t, "_pid", pid_type);
450 append_composite_type_field (t, "_uid", uid_type);
451 append_composite_type_field (t, "_status", int_type);
452 append_composite_type_field (t, "_utime", clock_type);
453 append_composite_type_field (t, "_stime", clock_type);
454 append_composite_type_field (reason_type, "_child", t);
456 /* _fault */
457 t = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
458 append_composite_type_field (t, "_addr", void_ptr_type);
459 append_composite_type_field (t, "_trap", int_type);
460 append_composite_type_field (t, "_trap2", int_type);
461 append_composite_type_field (t, "_trap3", int_type);
462 append_composite_type_field (reason_type, "_fault", t);
464 /* _poll */
465 t = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
466 append_composite_type_field (t, "_band", long_type);
467 append_composite_type_field (t, "_fd", int_type);
468 append_composite_type_field (reason_type, "_poll", t);
470 /* _syscall */
471 t = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
472 append_composite_type_field (t, "_sysnum", int_type);
473 append_composite_type_field (t, "_retval",
474 init_vector_type (int_type, 2));
475 append_composite_type_field (t, "_error", int_type);
476 append_composite_type_field (t, "_args",
477 init_vector_type (uint64_type, 8));
478 append_composite_type_field (reason_type, "_syscall", t);
480 /* _ptrace_state */
481 t = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
482 append_composite_type_field (t, "_pe_report_event", int_type);
483 append_composite_type_field (t, "_option", option_type);
484 append_composite_type_field (reason_type, "_ptrace_state", t);
486 /* struct _ksiginfo */
487 type *ksiginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
488 ksiginfo_type->set_name (gdbarch_obstack_strdup (gdbarch, "_ksiginfo"));
489 append_composite_type_field (ksiginfo_type, "_signo", int_type);
490 append_composite_type_field (ksiginfo_type, "_code", int_type);
491 append_composite_type_field (ksiginfo_type, "_errno", int_type);
492 if (lp64)
493 append_composite_type_field (ksiginfo_type, "_pad", int_type);
494 append_composite_type_field (ksiginfo_type, "_reason", reason_type);
496 /* union siginfo */
497 type *siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
498 siginfo_type->set_name (gdbarch_obstack_strdup (gdbarch, "siginfo"));
499 append_composite_type_field (siginfo_type, "si_pad",
500 init_vector_type (char_type, 128));
501 append_composite_type_field (siginfo_type, "_info", ksiginfo_type);
503 nbsd_gdbarch_data->siginfo_type = siginfo_type;
505 return siginfo_type;
508 /* See netbsd-tdep.h. */
510 void
511 nbsd_info_proc_mappings_header (int addr_bit)
513 gdb_printf (_("Mapped address spaces:\n\n"));
514 if (addr_bit == 64)
516 gdb_printf (" %18s %18s %10s %10s %9s %s\n",
517 "Start Addr",
518 " End Addr",
519 " Size", " Offset", "Flags ", "File");
521 else
523 gdb_printf ("\t%10s %10s %10s %10s %9s %s\n",
524 "Start Addr",
525 " End Addr",
526 " Size", " Offset", "Flags ", "File");
530 /* Helper function to generate mappings flags for a single VM map
531 entry in 'info proc mappings'. */
533 static const char *
534 nbsd_vm_map_entry_flags (int kve_flags, int kve_protection)
536 static char vm_flags[9];
538 vm_flags[0] = (kve_protection & KINFO_VME_PROT_READ) ? 'r' : '-';
539 vm_flags[1] = (kve_protection & KINFO_VME_PROT_WRITE) ? 'w' : '-';
540 vm_flags[2] = (kve_protection & KINFO_VME_PROT_EXEC) ? 'x' : '-';
541 vm_flags[3] = ' ';
542 vm_flags[4] = (kve_flags & KINFO_VME_FLAG_COW) ? 'C' : '-';
543 vm_flags[5] = (kve_flags & KINFO_VME_FLAG_NEEDS_COPY) ? 'N' : '-';
544 vm_flags[6] = (kve_flags & KINFO_VME_FLAG_PAGEABLE) ? 'P' : '-';
545 vm_flags[7] = (kve_flags & KINFO_VME_FLAG_GROWS_UP) ? 'U'
546 : (kve_flags & KINFO_VME_FLAG_GROWS_DOWN) ? 'D' : '-';
547 vm_flags[8] = '\0';
549 return vm_flags;
552 void
553 nbsd_info_proc_mappings_entry (int addr_bit, ULONGEST kve_start,
554 ULONGEST kve_end, ULONGEST kve_offset,
555 int kve_flags, int kve_protection,
556 const char *kve_path)
558 if (addr_bit == 64)
560 gdb_printf (" %18s %18s %10s %10s %9s %s\n",
561 hex_string (kve_start),
562 hex_string (kve_end),
563 hex_string (kve_end - kve_start),
564 hex_string (kve_offset),
565 nbsd_vm_map_entry_flags (kve_flags, kve_protection),
566 kve_path);
568 else
570 gdb_printf ("\t%10s %10s %10s %10s %9s %s\n",
571 hex_string (kve_start),
572 hex_string (kve_end),
573 hex_string (kve_end - kve_start),
574 hex_string (kve_offset),
575 nbsd_vm_map_entry_flags (kve_flags, kve_protection),
576 kve_path);
580 /* Implement the "get_syscall_number" gdbarch method. */
582 static LONGEST
583 nbsd_get_syscall_number (struct gdbarch *gdbarch, thread_info *thread)
586 /* NetBSD doesn't use gdbarch_get_syscall_number since NetBSD
587 native targets fetch the system call number from the
588 'si_sysnum' member of siginfo_t in nbsd_nat_target::wait.
589 However, system call catching requires this function to be
590 set. */
592 internal_error (_("nbsd_get_sycall_number called"));
595 /* See netbsd-tdep.h. */
597 void
598 nbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
600 set_gdbarch_gdb_signal_from_target (gdbarch, nbsd_gdb_signal_from_target);
601 set_gdbarch_gdb_signal_to_target (gdbarch, nbsd_gdb_signal_to_target);
602 set_gdbarch_skip_solib_resolver (gdbarch, nbsd_skip_solib_resolver);
603 set_gdbarch_auxv_parse (gdbarch, svr4_auxv_parse);
604 set_gdbarch_get_siginfo_type (gdbarch, nbsd_get_siginfo_type);
606 /* `catch syscall' */
607 set_xml_syscall_file_name (gdbarch, "syscalls/netbsd.xml");
608 set_gdbarch_get_syscall_number (gdbarch, nbsd_get_syscall_number);