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 bound_minimal_symbol msym
352 = lookup_minimal_symbol (current_program_space
, "_rtld_bind_start");
353 if (msym
.minsym
&& msym
.value_address () == pc
)
354 return frame_unwind_caller_pc (get_current_frame ());
356 return find_solib_trampoline_target (get_current_frame (), pc
);
359 struct nbsd_gdbarch_data
361 struct type
*siginfo_type
= nullptr;
364 static const registry
<gdbarch
>::key
<nbsd_gdbarch_data
>
365 nbsd_gdbarch_data_handle
;
367 static struct nbsd_gdbarch_data
*
368 get_nbsd_gdbarch_data (struct gdbarch
*gdbarch
)
370 struct nbsd_gdbarch_data
*result
= nbsd_gdbarch_data_handle
.get (gdbarch
);
371 if (result
== nullptr)
372 result
= nbsd_gdbarch_data_handle
.emplace (gdbarch
);
376 /* Implement the "get_siginfo_type" gdbarch method. */
379 nbsd_get_siginfo_type (struct gdbarch
*gdbarch
)
381 nbsd_gdbarch_data
*nbsd_gdbarch_data
= get_nbsd_gdbarch_data (gdbarch
);
382 if (nbsd_gdbarch_data
->siginfo_type
!= NULL
)
383 return nbsd_gdbarch_data
->siginfo_type
;
385 type
*char_type
= builtin_type (gdbarch
)->builtin_char
;
386 type
*int_type
= builtin_type (gdbarch
)->builtin_int
;
387 type
*long_type
= builtin_type (gdbarch
)->builtin_long
;
390 = lookup_pointer_type (builtin_type (gdbarch
)->builtin_void
);
392 type
*int32_type
= builtin_type (gdbarch
)->builtin_int32
;
393 type
*uint32_type
= builtin_type (gdbarch
)->builtin_uint32
;
394 type
*uint64_type
= builtin_type (gdbarch
)->builtin_uint64
;
396 bool lp64
= void_ptr_type
->length () == 8;
397 size_t char_bits
= gdbarch_addressable_memory_unit_size (gdbarch
) * 8;
400 type_allocator
alloc (gdbarch
);
401 type
*pid_type
= alloc
.new_type (TYPE_CODE_TYPEDEF
,
402 int32_type
->length () * char_bits
,
404 pid_type
->set_target_type (int32_type
);
407 type
*uid_type
= alloc
.new_type (TYPE_CODE_TYPEDEF
,
408 uint32_type
->length () * char_bits
,
410 uid_type
->set_target_type (uint32_type
);
413 type
*clock_type
= alloc
.new_type (TYPE_CODE_TYPEDEF
,
414 int_type
->length () * char_bits
,
416 clock_type
->set_target_type (int_type
);
419 type
*lwpid_type
= alloc
.new_type (TYPE_CODE_TYPEDEF
,
420 int32_type
->length () * char_bits
,
422 lwpid_type
->set_target_type (int32_type
);
425 type
*sigval_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
426 sigval_type
->set_name (gdbarch_obstack_strdup (gdbarch
, "sigval"));
427 append_composite_type_field (sigval_type
, "sival_int", int_type
);
428 append_composite_type_field (sigval_type
, "sival_ptr", void_ptr_type
);
431 type
*option_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
432 option_type
->set_name (gdbarch_obstack_strdup (gdbarch
, "_option"));
433 append_composite_type_field (option_type
, "_pe_other_pid", pid_type
);
434 append_composite_type_field (option_type
, "_pe_lwp", lwpid_type
);
437 type
*reason_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
440 type
*t
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
441 append_composite_type_field (t
, "_pid", pid_type
);
442 append_composite_type_field (t
, "_uid", uid_type
);
443 append_composite_type_field (t
, "_value", sigval_type
);
444 append_composite_type_field (reason_type
, "_rt", t
);
447 t
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
448 append_composite_type_field (t
, "_pid", pid_type
);
449 append_composite_type_field (t
, "_uid", uid_type
);
450 append_composite_type_field (t
, "_status", int_type
);
451 append_composite_type_field (t
, "_utime", clock_type
);
452 append_composite_type_field (t
, "_stime", clock_type
);
453 append_composite_type_field (reason_type
, "_child", t
);
456 t
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
457 append_composite_type_field (t
, "_addr", void_ptr_type
);
458 append_composite_type_field (t
, "_trap", int_type
);
459 append_composite_type_field (t
, "_trap2", int_type
);
460 append_composite_type_field (t
, "_trap3", int_type
);
461 append_composite_type_field (reason_type
, "_fault", t
);
464 t
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
465 append_composite_type_field (t
, "_band", long_type
);
466 append_composite_type_field (t
, "_fd", int_type
);
467 append_composite_type_field (reason_type
, "_poll", t
);
470 t
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
471 append_composite_type_field (t
, "_sysnum", int_type
);
472 append_composite_type_field (t
, "_retval",
473 init_vector_type (int_type
, 2));
474 append_composite_type_field (t
, "_error", int_type
);
475 append_composite_type_field (t
, "_args",
476 init_vector_type (uint64_type
, 8));
477 append_composite_type_field (reason_type
, "_syscall", t
);
480 t
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
481 append_composite_type_field (t
, "_pe_report_event", int_type
);
482 append_composite_type_field (t
, "_option", option_type
);
483 append_composite_type_field (reason_type
, "_ptrace_state", t
);
485 /* struct _ksiginfo */
486 type
*ksiginfo_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
487 ksiginfo_type
->set_name (gdbarch_obstack_strdup (gdbarch
, "_ksiginfo"));
488 append_composite_type_field (ksiginfo_type
, "_signo", int_type
);
489 append_composite_type_field (ksiginfo_type
, "_code", int_type
);
490 append_composite_type_field (ksiginfo_type
, "_errno", int_type
);
492 append_composite_type_field (ksiginfo_type
, "_pad", int_type
);
493 append_composite_type_field (ksiginfo_type
, "_reason", reason_type
);
496 type
*siginfo_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
497 siginfo_type
->set_name (gdbarch_obstack_strdup (gdbarch
, "siginfo"));
498 append_composite_type_field (siginfo_type
, "si_pad",
499 init_vector_type (char_type
, 128));
500 append_composite_type_field (siginfo_type
, "_info", ksiginfo_type
);
502 nbsd_gdbarch_data
->siginfo_type
= siginfo_type
;
507 /* See netbsd-tdep.h. */
510 nbsd_info_proc_mappings_header (int addr_bit
)
512 gdb_printf (_("Mapped address spaces:\n\n"));
515 gdb_printf (" %18s %18s %10s %10s %9s %s\n",
518 " Size", " Offset", "Flags ", "File");
522 gdb_printf ("\t%10s %10s %10s %10s %9s %s\n",
525 " Size", " Offset", "Flags ", "File");
529 /* Helper function to generate mappings flags for a single VM map
530 entry in 'info proc mappings'. */
533 nbsd_vm_map_entry_flags (int kve_flags
, int kve_protection
)
535 static char vm_flags
[9];
537 vm_flags
[0] = (kve_protection
& KINFO_VME_PROT_READ
) ? 'r' : '-';
538 vm_flags
[1] = (kve_protection
& KINFO_VME_PROT_WRITE
) ? 'w' : '-';
539 vm_flags
[2] = (kve_protection
& KINFO_VME_PROT_EXEC
) ? 'x' : '-';
541 vm_flags
[4] = (kve_flags
& KINFO_VME_FLAG_COW
) ? 'C' : '-';
542 vm_flags
[5] = (kve_flags
& KINFO_VME_FLAG_NEEDS_COPY
) ? 'N' : '-';
543 vm_flags
[6] = (kve_flags
& KINFO_VME_FLAG_PAGEABLE
) ? 'P' : '-';
544 vm_flags
[7] = (kve_flags
& KINFO_VME_FLAG_GROWS_UP
) ? 'U'
545 : (kve_flags
& KINFO_VME_FLAG_GROWS_DOWN
) ? 'D' : '-';
552 nbsd_info_proc_mappings_entry (int addr_bit
, ULONGEST kve_start
,
553 ULONGEST kve_end
, ULONGEST kve_offset
,
554 int kve_flags
, int kve_protection
,
555 const char *kve_path
)
559 gdb_printf (" %18s %18s %10s %10s %9s %s\n",
560 hex_string (kve_start
),
561 hex_string (kve_end
),
562 hex_string (kve_end
- kve_start
),
563 hex_string (kve_offset
),
564 nbsd_vm_map_entry_flags (kve_flags
, kve_protection
),
569 gdb_printf ("\t%10s %10s %10s %10s %9s %s\n",
570 hex_string (kve_start
),
571 hex_string (kve_end
),
572 hex_string (kve_end
- kve_start
),
573 hex_string (kve_offset
),
574 nbsd_vm_map_entry_flags (kve_flags
, kve_protection
),
579 /* Implement the "get_syscall_number" gdbarch method. */
582 nbsd_get_syscall_number (struct gdbarch
*gdbarch
, thread_info
*thread
)
585 /* NetBSD doesn't use gdbarch_get_syscall_number since NetBSD
586 native targets fetch the system call number from the
587 'si_sysnum' member of siginfo_t in nbsd_nat_target::wait.
588 However, system call catching requires this function to be
591 internal_error (_("nbsd_get_sycall_number called"));
594 /* See netbsd-tdep.h. */
597 nbsd_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
599 set_gdbarch_gdb_signal_from_target (gdbarch
, nbsd_gdb_signal_from_target
);
600 set_gdbarch_gdb_signal_to_target (gdbarch
, nbsd_gdb_signal_to_target
);
601 set_gdbarch_skip_solib_resolver (gdbarch
, nbsd_skip_solib_resolver
);
602 set_gdbarch_auxv_parse (gdbarch
, svr4_auxv_parse
);
603 set_gdbarch_get_siginfo_type (gdbarch
, nbsd_get_siginfo_type
);
605 /* `catch syscall' */
606 set_xml_syscall_file_name (gdbarch
, "syscalls/netbsd.xml");
607 set_gdbarch_get_syscall_number (gdbarch
, nbsd_get_syscall_number
);