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/>. */
23 #include "solib-svr4.h"
24 #include "netbsd-tdep.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
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>. */
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
)
107 return GDB_SIGNAL_HUP
;
110 return GDB_SIGNAL_INT
;
113 return GDB_SIGNAL_QUIT
;
116 return GDB_SIGNAL_ILL
;
119 return GDB_SIGNAL_TRAP
;
122 return GDB_SIGNAL_ABRT
;
125 return GDB_SIGNAL_EMT
;
128 return GDB_SIGNAL_FPE
;
131 return GDB_SIGNAL_KILL
;
134 return GDB_SIGNAL_BUS
;
137 return GDB_SIGNAL_SEGV
;
140 return GDB_SIGNAL_SYS
;
143 return GDB_SIGNAL_PIPE
;
146 return GDB_SIGNAL_ALRM
;
149 return GDB_SIGNAL_TERM
;
152 return GDB_SIGNAL_URG
;
155 return GDB_SIGNAL_STOP
;
158 return GDB_SIGNAL_TSTP
;
161 return GDB_SIGNAL_CONT
;
164 return GDB_SIGNAL_CHLD
;
167 return GDB_SIGNAL_TTIN
;
170 return GDB_SIGNAL_TTOU
;
173 return GDB_SIGNAL_IO
;
176 return GDB_SIGNAL_XCPU
;
179 return GDB_SIGNAL_XFSZ
;
182 return GDB_SIGNAL_VTALRM
;
185 return GDB_SIGNAL_PROF
;
188 return GDB_SIGNAL_WINCH
;
191 return GDB_SIGNAL_INFO
;
194 return GDB_SIGNAL_USR1
;
197 return GDB_SIGNAL_USR2
;
200 return GDB_SIGNAL_PWR
;
202 /* SIGRTMIN and SIGRTMAX are not continuous in <gdb/signals.def>,
203 therefore we have to handle them here. */
205 return GDB_SIGNAL_REALTIME_33
;
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. */
224 nbsd_gdb_signal_to_target (struct gdbarch
*gdbarch
,
225 enum gdb_signal signal
)
238 case GDB_SIGNAL_QUIT
:
244 case GDB_SIGNAL_TRAP
:
247 case GDB_SIGNAL_ABRT
:
256 case GDB_SIGNAL_KILL
:
262 case GDB_SIGNAL_SEGV
:
268 case GDB_SIGNAL_PIPE
:
271 case GDB_SIGNAL_ALRM
:
274 case GDB_SIGNAL_TERM
:
280 case GDB_SIGNAL_TSTP
:
283 case GDB_SIGNAL_CONT
:
286 case GDB_SIGNAL_CHLD
:
289 case GDB_SIGNAL_TTIN
:
292 case GDB_SIGNAL_TTOU
:
298 case GDB_SIGNAL_XCPU
:
301 case GDB_SIGNAL_XFSZ
:
304 case GDB_SIGNAL_VTALRM
:
305 return NBSD_SIGVTALRM
;
307 case GDB_SIGNAL_PROF
:
310 case GDB_SIGNAL_WINCH
:
311 return NBSD_SIGWINCH
;
313 case GDB_SIGNAL_INFO
:
316 case GDB_SIGNAL_USR1
:
319 case GDB_SIGNAL_USR2
:
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
;
346 /* Shared library resolver handling. */
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 ());
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
);
377 /* Implement the "get_siginfo_type" gdbarch method. */
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
;
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;
401 type_allocator
alloc (gdbarch
);
402 type
*pid_type
= alloc
.new_type (TYPE_CODE_TYPEDEF
,
403 int32_type
->length () * char_bits
,
405 pid_type
->set_target_type (int32_type
);
408 type
*uid_type
= alloc
.new_type (TYPE_CODE_TYPEDEF
,
409 uint32_type
->length () * char_bits
,
411 uid_type
->set_target_type (uint32_type
);
414 type
*clock_type
= alloc
.new_type (TYPE_CODE_TYPEDEF
,
415 int_type
->length () * char_bits
,
417 clock_type
->set_target_type (int_type
);
420 type
*lwpid_type
= alloc
.new_type (TYPE_CODE_TYPEDEF
,
421 int32_type
->length () * char_bits
,
423 lwpid_type
->set_target_type (int32_type
);
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
);
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
);
438 type
*reason_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
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
);
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
);
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
);
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
);
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
);
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
);
493 append_composite_type_field (ksiginfo_type
, "_pad", int_type
);
494 append_composite_type_field (ksiginfo_type
, "_reason", reason_type
);
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
;
508 /* See netbsd-tdep.h. */
511 nbsd_info_proc_mappings_header (int addr_bit
)
513 gdb_printf (_("Mapped address spaces:\n\n"));
516 gdb_printf (" %18s %18s %10s %10s %9s %s\n",
519 " Size", " Offset", "Flags ", "File");
523 gdb_printf ("\t%10s %10s %10s %10s %9s %s\n",
526 " Size", " Offset", "Flags ", "File");
530 /* Helper function to generate mappings flags for a single VM map
531 entry in 'info proc mappings'. */
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' : '-';
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' : '-';
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
)
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
),
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
),
580 /* Implement the "get_syscall_number" gdbarch method. */
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
592 internal_error (_("nbsd_get_sycall_number called"));
595 /* See netbsd-tdep.h. */
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
);