xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/netbsd-tdep.c (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
1*6881a400Schristos /* Common target-dependent code for NetBSD systems.
2*6881a400Schristos 
3*6881a400Schristos    Copyright (C) 2002-2023 Free Software Foundation, Inc.
4*6881a400Schristos 
5*6881a400Schristos    Contributed by Wasabi Systems, Inc.
6*6881a400Schristos 
7*6881a400Schristos    This file is part of GDB.
8*6881a400Schristos 
9*6881a400Schristos    This program is free software; you can redistribute it and/or modify
10*6881a400Schristos    it under the terms of the GNU General Public License as published by
11*6881a400Schristos    the Free Software Foundation; either version 3 of the License, or
12*6881a400Schristos    (at your option) any later version.
13*6881a400Schristos 
14*6881a400Schristos    This program is distributed in the hope that it will be useful,
15*6881a400Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
16*6881a400Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*6881a400Schristos    GNU General Public License for more details.
18*6881a400Schristos 
19*6881a400Schristos    You should have received a copy of the GNU General Public License
20*6881a400Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21*6881a400Schristos 
22*6881a400Schristos #include "defs.h"
23*6881a400Schristos #include "auxv.h"
24*6881a400Schristos #include "solib-svr4.h"
25*6881a400Schristos #include "netbsd-tdep.h"
26*6881a400Schristos #include "gdbarch.h"
27*6881a400Schristos #include "objfiles.h"
28*6881a400Schristos #include "xml-syscall.h"
29*6881a400Schristos 
30*6881a400Schristos #include "elf/common.h"
31*6881a400Schristos 
32*6881a400Schristos /* Flags in the 'kve_protection' field in struct kinfo_vmentry.  These
33*6881a400Schristos    match the KVME_PROT_* constants in <sys/sysctl.h>.  */
34*6881a400Schristos 
35*6881a400Schristos #define	KINFO_VME_PROT_READ	0x00000001
36*6881a400Schristos #define	KINFO_VME_PROT_WRITE	0x00000002
37*6881a400Schristos #define	KINFO_VME_PROT_EXEC	0x00000004
38*6881a400Schristos 
39*6881a400Schristos /* Flags in the 'kve_flags' field in struct kinfo_vmentry.  These
40*6881a400Schristos    match the KVME_FLAG_* constants in <sys/sysctl.h>.  */
41*6881a400Schristos 
42*6881a400Schristos #define	KINFO_VME_FLAG_COW		0x00000001
43*6881a400Schristos #define	KINFO_VME_FLAG_NEEDS_COPY	0x00000002
44*6881a400Schristos #define	KINFO_VME_FLAG_NOCOREDUMP	0x00000004
45*6881a400Schristos #define	KINFO_VME_FLAG_PAGEABLE		0x00000008
46*6881a400Schristos #define	KINFO_VME_FLAG_GROWS_UP		0x00000010
47*6881a400Schristos #define	KINFO_VME_FLAG_GROWS_DOWN	0x00000020
48*6881a400Schristos 
49*6881a400Schristos int
50*6881a400Schristos nbsd_pc_in_sigtramp (CORE_ADDR pc, const char *func_name)
51*6881a400Schristos {
52*6881a400Schristos   /* Check for libc-provided signal trampoline.  All such trampolines
53*6881a400Schristos      have function names which begin with "__sigtramp".  */
54*6881a400Schristos 
55*6881a400Schristos   return (func_name != NULL
56*6881a400Schristos 	  && startswith (func_name, "__sigtramp"));
57*6881a400Schristos }
58*6881a400Schristos 
59*6881a400Schristos /* This enum is derived from NETBSD's <sys/signal.h>.  */
60*6881a400Schristos 
61*6881a400Schristos enum
62*6881a400Schristos   {
63*6881a400Schristos    NBSD_SIGHUP = 1,
64*6881a400Schristos    NBSD_SIGINT = 2,
65*6881a400Schristos    NBSD_SIGQUIT = 3,
66*6881a400Schristos    NBSD_SIGILL = 4,
67*6881a400Schristos    NBSD_SIGTRAP = 5,
68*6881a400Schristos    NBSD_SIGABRT = 6,
69*6881a400Schristos    NBSD_SIGEMT = 7,
70*6881a400Schristos    NBSD_SIGFPE = 8,
71*6881a400Schristos    NBSD_SIGKILL = 9,
72*6881a400Schristos    NBSD_SIGBUS = 10,
73*6881a400Schristos    NBSD_SIGSEGV = 11,
74*6881a400Schristos    NBSD_SIGSYS = 12,
75*6881a400Schristos    NBSD_SIGPIPE = 13,
76*6881a400Schristos    NBSD_SIGALRM = 14,
77*6881a400Schristos    NBSD_SIGTERM = 15,
78*6881a400Schristos    NBSD_SIGURG = 16,
79*6881a400Schristos    NBSD_SIGSTOP = 17,
80*6881a400Schristos    NBSD_SIGTSTP = 18,
81*6881a400Schristos    NBSD_SIGCONT = 19,
82*6881a400Schristos    NBSD_SIGCHLD = 20,
83*6881a400Schristos    NBSD_SIGTTIN = 21,
84*6881a400Schristos    NBSD_SIGTTOU = 22,
85*6881a400Schristos    NBSD_SIGIO = 23,
86*6881a400Schristos    NBSD_SIGXCPU = 24,
87*6881a400Schristos    NBSD_SIGXFSZ = 25,
88*6881a400Schristos    NBSD_SIGVTALRM = 26,
89*6881a400Schristos    NBSD_SIGPROF = 27,
90*6881a400Schristos    NBSD_SIGWINCH = 28,
91*6881a400Schristos    NBSD_SIGINFO = 29,
92*6881a400Schristos    NBSD_SIGUSR1 = 30,
93*6881a400Schristos    NBSD_SIGUSR2 = 31,
94*6881a400Schristos    NBSD_SIGPWR = 32,
95*6881a400Schristos    NBSD_SIGRTMIN = 33,
96*6881a400Schristos    NBSD_SIGRTMAX = 63,
97*6881a400Schristos   };
98*6881a400Schristos 
99*6881a400Schristos /* Implement the "gdb_signal_from_target" gdbarch method.  */
100*6881a400Schristos 
101*6881a400Schristos static enum gdb_signal
102*6881a400Schristos nbsd_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
103*6881a400Schristos {
104*6881a400Schristos   switch (signal)
105*6881a400Schristos     {
106*6881a400Schristos     case 0:
107*6881a400Schristos       return GDB_SIGNAL_0;
108*6881a400Schristos 
109*6881a400Schristos     case NBSD_SIGHUP:
110*6881a400Schristos       return GDB_SIGNAL_HUP;
111*6881a400Schristos 
112*6881a400Schristos     case NBSD_SIGINT:
113*6881a400Schristos       return GDB_SIGNAL_INT;
114*6881a400Schristos 
115*6881a400Schristos     case NBSD_SIGQUIT:
116*6881a400Schristos       return GDB_SIGNAL_QUIT;
117*6881a400Schristos 
118*6881a400Schristos     case NBSD_SIGILL:
119*6881a400Schristos       return GDB_SIGNAL_ILL;
120*6881a400Schristos 
121*6881a400Schristos     case NBSD_SIGTRAP:
122*6881a400Schristos       return GDB_SIGNAL_TRAP;
123*6881a400Schristos 
124*6881a400Schristos     case NBSD_SIGABRT:
125*6881a400Schristos       return GDB_SIGNAL_ABRT;
126*6881a400Schristos 
127*6881a400Schristos     case NBSD_SIGEMT:
128*6881a400Schristos       return GDB_SIGNAL_EMT;
129*6881a400Schristos 
130*6881a400Schristos     case NBSD_SIGFPE:
131*6881a400Schristos       return GDB_SIGNAL_FPE;
132*6881a400Schristos 
133*6881a400Schristos     case NBSD_SIGKILL:
134*6881a400Schristos       return GDB_SIGNAL_KILL;
135*6881a400Schristos 
136*6881a400Schristos     case NBSD_SIGBUS:
137*6881a400Schristos       return GDB_SIGNAL_BUS;
138*6881a400Schristos 
139*6881a400Schristos     case NBSD_SIGSEGV:
140*6881a400Schristos       return GDB_SIGNAL_SEGV;
141*6881a400Schristos 
142*6881a400Schristos     case NBSD_SIGSYS:
143*6881a400Schristos       return GDB_SIGNAL_SYS;
144*6881a400Schristos 
145*6881a400Schristos     case NBSD_SIGPIPE:
146*6881a400Schristos       return GDB_SIGNAL_PIPE;
147*6881a400Schristos 
148*6881a400Schristos     case NBSD_SIGALRM:
149*6881a400Schristos       return GDB_SIGNAL_ALRM;
150*6881a400Schristos 
151*6881a400Schristos     case NBSD_SIGTERM:
152*6881a400Schristos       return GDB_SIGNAL_TERM;
153*6881a400Schristos 
154*6881a400Schristos     case NBSD_SIGURG:
155*6881a400Schristos       return GDB_SIGNAL_URG;
156*6881a400Schristos 
157*6881a400Schristos     case NBSD_SIGSTOP:
158*6881a400Schristos       return GDB_SIGNAL_STOP;
159*6881a400Schristos 
160*6881a400Schristos     case NBSD_SIGTSTP:
161*6881a400Schristos       return GDB_SIGNAL_TSTP;
162*6881a400Schristos 
163*6881a400Schristos     case NBSD_SIGCONT:
164*6881a400Schristos       return GDB_SIGNAL_CONT;
165*6881a400Schristos 
166*6881a400Schristos     case NBSD_SIGCHLD:
167*6881a400Schristos       return GDB_SIGNAL_CHLD;
168*6881a400Schristos 
169*6881a400Schristos     case NBSD_SIGTTIN:
170*6881a400Schristos       return GDB_SIGNAL_TTIN;
171*6881a400Schristos 
172*6881a400Schristos     case NBSD_SIGTTOU:
173*6881a400Schristos       return GDB_SIGNAL_TTOU;
174*6881a400Schristos 
175*6881a400Schristos     case NBSD_SIGIO:
176*6881a400Schristos       return GDB_SIGNAL_IO;
177*6881a400Schristos 
178*6881a400Schristos     case NBSD_SIGXCPU:
179*6881a400Schristos       return GDB_SIGNAL_XCPU;
180*6881a400Schristos 
181*6881a400Schristos     case NBSD_SIGXFSZ:
182*6881a400Schristos       return GDB_SIGNAL_XFSZ;
183*6881a400Schristos 
184*6881a400Schristos     case NBSD_SIGVTALRM:
185*6881a400Schristos       return GDB_SIGNAL_VTALRM;
186*6881a400Schristos 
187*6881a400Schristos     case NBSD_SIGPROF:
188*6881a400Schristos       return GDB_SIGNAL_PROF;
189*6881a400Schristos 
190*6881a400Schristos     case NBSD_SIGWINCH:
191*6881a400Schristos       return GDB_SIGNAL_WINCH;
192*6881a400Schristos 
193*6881a400Schristos     case NBSD_SIGINFO:
194*6881a400Schristos       return GDB_SIGNAL_INFO;
195*6881a400Schristos 
196*6881a400Schristos     case NBSD_SIGUSR1:
197*6881a400Schristos       return GDB_SIGNAL_USR1;
198*6881a400Schristos 
199*6881a400Schristos     case NBSD_SIGUSR2:
200*6881a400Schristos       return GDB_SIGNAL_USR2;
201*6881a400Schristos 
202*6881a400Schristos     case NBSD_SIGPWR:
203*6881a400Schristos       return GDB_SIGNAL_PWR;
204*6881a400Schristos 
205*6881a400Schristos     /* SIGRTMIN and SIGRTMAX are not continuous in <gdb/signals.def>,
206*6881a400Schristos        therefore we have to handle them here.  */
207*6881a400Schristos     case NBSD_SIGRTMIN:
208*6881a400Schristos       return GDB_SIGNAL_REALTIME_33;
209*6881a400Schristos 
210*6881a400Schristos     case NBSD_SIGRTMAX:
211*6881a400Schristos       return GDB_SIGNAL_REALTIME_63;
212*6881a400Schristos     }
213*6881a400Schristos 
214*6881a400Schristos   if (signal >= NBSD_SIGRTMIN + 1 && signal <= NBSD_SIGRTMAX - 1)
215*6881a400Schristos     {
216*6881a400Schristos       int offset = signal - NBSD_SIGRTMIN + 1;
217*6881a400Schristos 
218*6881a400Schristos       return (enum gdb_signal) ((int) GDB_SIGNAL_REALTIME_34 + offset);
219*6881a400Schristos     }
220*6881a400Schristos 
221*6881a400Schristos   return GDB_SIGNAL_UNKNOWN;
222*6881a400Schristos }
223*6881a400Schristos 
224*6881a400Schristos /* Implement the "gdb_signal_to_target" gdbarch method.  */
225*6881a400Schristos 
226*6881a400Schristos static int
227*6881a400Schristos nbsd_gdb_signal_to_target (struct gdbarch *gdbarch,
228*6881a400Schristos 		enum gdb_signal signal)
229*6881a400Schristos {
230*6881a400Schristos   switch (signal)
231*6881a400Schristos     {
232*6881a400Schristos     case GDB_SIGNAL_0:
233*6881a400Schristos       return 0;
234*6881a400Schristos 
235*6881a400Schristos     case GDB_SIGNAL_HUP:
236*6881a400Schristos       return NBSD_SIGHUP;
237*6881a400Schristos 
238*6881a400Schristos     case GDB_SIGNAL_INT:
239*6881a400Schristos       return NBSD_SIGINT;
240*6881a400Schristos 
241*6881a400Schristos     case GDB_SIGNAL_QUIT:
242*6881a400Schristos       return NBSD_SIGQUIT;
243*6881a400Schristos 
244*6881a400Schristos     case GDB_SIGNAL_ILL:
245*6881a400Schristos       return NBSD_SIGILL;
246*6881a400Schristos 
247*6881a400Schristos     case GDB_SIGNAL_TRAP:
248*6881a400Schristos       return NBSD_SIGTRAP;
249*6881a400Schristos 
250*6881a400Schristos     case GDB_SIGNAL_ABRT:
251*6881a400Schristos       return NBSD_SIGABRT;
252*6881a400Schristos 
253*6881a400Schristos     case GDB_SIGNAL_EMT:
254*6881a400Schristos       return NBSD_SIGEMT;
255*6881a400Schristos 
256*6881a400Schristos     case GDB_SIGNAL_FPE:
257*6881a400Schristos       return NBSD_SIGFPE;
258*6881a400Schristos 
259*6881a400Schristos     case GDB_SIGNAL_KILL:
260*6881a400Schristos       return NBSD_SIGKILL;
261*6881a400Schristos 
262*6881a400Schristos     case GDB_SIGNAL_BUS:
263*6881a400Schristos       return NBSD_SIGBUS;
264*6881a400Schristos 
265*6881a400Schristos     case GDB_SIGNAL_SEGV:
266*6881a400Schristos       return NBSD_SIGSEGV;
267*6881a400Schristos 
268*6881a400Schristos     case GDB_SIGNAL_SYS:
269*6881a400Schristos       return NBSD_SIGSYS;
270*6881a400Schristos 
271*6881a400Schristos     case GDB_SIGNAL_PIPE:
272*6881a400Schristos       return NBSD_SIGPIPE;
273*6881a400Schristos 
274*6881a400Schristos     case GDB_SIGNAL_ALRM:
275*6881a400Schristos       return NBSD_SIGALRM;
276*6881a400Schristos 
277*6881a400Schristos     case GDB_SIGNAL_TERM:
278*6881a400Schristos       return NBSD_SIGTERM;
279*6881a400Schristos 
280*6881a400Schristos     case GDB_SIGNAL_URG:
281*6881a400Schristos       return NBSD_SIGSTOP;
282*6881a400Schristos 
283*6881a400Schristos     case GDB_SIGNAL_TSTP:
284*6881a400Schristos       return NBSD_SIGTSTP;
285*6881a400Schristos 
286*6881a400Schristos     case GDB_SIGNAL_CONT:
287*6881a400Schristos       return NBSD_SIGCONT;
288*6881a400Schristos 
289*6881a400Schristos     case GDB_SIGNAL_CHLD:
290*6881a400Schristos       return NBSD_SIGCHLD;
291*6881a400Schristos 
292*6881a400Schristos     case GDB_SIGNAL_TTIN:
293*6881a400Schristos       return NBSD_SIGTTIN;
294*6881a400Schristos 
295*6881a400Schristos     case GDB_SIGNAL_TTOU:
296*6881a400Schristos       return NBSD_SIGTTOU;
297*6881a400Schristos 
298*6881a400Schristos     case GDB_SIGNAL_IO:
299*6881a400Schristos       return NBSD_SIGIO;
300*6881a400Schristos 
301*6881a400Schristos     case GDB_SIGNAL_XCPU:
302*6881a400Schristos       return NBSD_SIGXCPU;
303*6881a400Schristos 
304*6881a400Schristos     case GDB_SIGNAL_XFSZ:
305*6881a400Schristos       return NBSD_SIGXFSZ;
306*6881a400Schristos 
307*6881a400Schristos     case GDB_SIGNAL_VTALRM:
308*6881a400Schristos       return NBSD_SIGVTALRM;
309*6881a400Schristos 
310*6881a400Schristos     case GDB_SIGNAL_PROF:
311*6881a400Schristos       return NBSD_SIGPROF;
312*6881a400Schristos 
313*6881a400Schristos     case GDB_SIGNAL_WINCH:
314*6881a400Schristos       return NBSD_SIGWINCH;
315*6881a400Schristos 
316*6881a400Schristos     case GDB_SIGNAL_INFO:
317*6881a400Schristos       return NBSD_SIGINFO;
318*6881a400Schristos 
319*6881a400Schristos     case GDB_SIGNAL_USR1:
320*6881a400Schristos       return NBSD_SIGUSR1;
321*6881a400Schristos 
322*6881a400Schristos     case GDB_SIGNAL_USR2:
323*6881a400Schristos       return NBSD_SIGUSR2;
324*6881a400Schristos 
325*6881a400Schristos     case GDB_SIGNAL_PWR:
326*6881a400Schristos       return NBSD_SIGPWR;
327*6881a400Schristos 
328*6881a400Schristos     /* GDB_SIGNAL_REALTIME_33 is not continuous in <gdb/signals.def>,
329*6881a400Schristos        therefore we have to handle it here.  */
330*6881a400Schristos     case GDB_SIGNAL_REALTIME_33:
331*6881a400Schristos       return NBSD_SIGRTMIN;
332*6881a400Schristos 
333*6881a400Schristos     /* Same comment applies to _64.  */
334*6881a400Schristos     case GDB_SIGNAL_REALTIME_63:
335*6881a400Schristos       return NBSD_SIGRTMAX;
336*6881a400Schristos     }
337*6881a400Schristos 
338*6881a400Schristos   if (signal >= GDB_SIGNAL_REALTIME_34
339*6881a400Schristos       && signal <= GDB_SIGNAL_REALTIME_62)
340*6881a400Schristos     {
341*6881a400Schristos       int offset = signal - GDB_SIGNAL_REALTIME_32;
342*6881a400Schristos 
343*6881a400Schristos       return NBSD_SIGRTMIN + 1 + offset;
344*6881a400Schristos     }
345*6881a400Schristos 
346*6881a400Schristos   return -1;
347*6881a400Schristos }
348*6881a400Schristos 
349*6881a400Schristos /* Shared library resolver handling.  */
350*6881a400Schristos 
351*6881a400Schristos static CORE_ADDR
352*6881a400Schristos nbsd_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
353*6881a400Schristos {
354*6881a400Schristos   struct bound_minimal_symbol msym;
355*6881a400Schristos 
356*6881a400Schristos   msym = lookup_minimal_symbol ("_rtld_bind_start", NULL, NULL);
357*6881a400Schristos   if (msym.minsym && msym.value_address () == pc)
358*6881a400Schristos     return frame_unwind_caller_pc (get_current_frame ());
359*6881a400Schristos   else
360*6881a400Schristos     return find_solib_trampoline_target (get_current_frame (), pc);
361*6881a400Schristos }
362*6881a400Schristos 
363*6881a400Schristos struct nbsd_gdbarch_data
364*6881a400Schristos {
365*6881a400Schristos   struct type *siginfo_type = nullptr;
366*6881a400Schristos };
367*6881a400Schristos 
368*6881a400Schristos static const registry<gdbarch>::key<nbsd_gdbarch_data>
369*6881a400Schristos      nbsd_gdbarch_data_handle;
370*6881a400Schristos 
371*6881a400Schristos static struct nbsd_gdbarch_data *
372*6881a400Schristos get_nbsd_gdbarch_data (struct gdbarch *gdbarch)
373*6881a400Schristos {
374*6881a400Schristos   struct nbsd_gdbarch_data *result = nbsd_gdbarch_data_handle.get (gdbarch);
375*6881a400Schristos   if (result == nullptr)
376*6881a400Schristos     result = nbsd_gdbarch_data_handle.emplace (gdbarch);
377*6881a400Schristos   return result;
378*6881a400Schristos }
379*6881a400Schristos 
380*6881a400Schristos /* Print descriptions of NetBSD-specific AUXV entries to FILE.  */
381*6881a400Schristos 
382*6881a400Schristos static void
383*6881a400Schristos nbsd_print_auxv_entry (struct gdbarch *gdbarch, struct ui_file *file,
384*6881a400Schristos 		       CORE_ADDR type, CORE_ADDR val)
385*6881a400Schristos {
386*6881a400Schristos   const char *name = "???";
387*6881a400Schristos   const char *description = "";
388*6881a400Schristos   enum auxv_format format = AUXV_FORMAT_HEX;
389*6881a400Schristos 
390*6881a400Schristos   switch (type)
391*6881a400Schristos     {
392*6881a400Schristos     default:
393*6881a400Schristos       default_print_auxv_entry (gdbarch, file, type, val);
394*6881a400Schristos       return;
395*6881a400Schristos #define _TAGNAME(tag) #tag
396*6881a400Schristos #define TAGNAME(tag) _TAGNAME(AT_##tag)
397*6881a400Schristos #define TAG(tag, text, kind) \
398*6881a400Schristos       case AT_NETBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
399*6881a400Schristos       TAG (STACKBASE, _("Base address of main thread"), AUXV_FORMAT_HEX);
400*6881a400Schristos     }
401*6881a400Schristos 
402*6881a400Schristos   fprint_auxv_entry (file, name, description, format, type, val);
403*6881a400Schristos }
404*6881a400Schristos 
405*6881a400Schristos /* Implement the "get_siginfo_type" gdbarch method.  */
406*6881a400Schristos 
407*6881a400Schristos static struct type *
408*6881a400Schristos nbsd_get_siginfo_type (struct gdbarch *gdbarch)
409*6881a400Schristos {
410*6881a400Schristos   nbsd_gdbarch_data *nbsd_gdbarch_data = get_nbsd_gdbarch_data (gdbarch);
411*6881a400Schristos   if (nbsd_gdbarch_data->siginfo_type != NULL)
412*6881a400Schristos     return nbsd_gdbarch_data->siginfo_type;
413*6881a400Schristos 
414*6881a400Schristos   type *char_type = builtin_type (gdbarch)->builtin_char;
415*6881a400Schristos   type *int_type = builtin_type (gdbarch)->builtin_int;
416*6881a400Schristos   type *long_type = builtin_type (gdbarch)->builtin_long;
417*6881a400Schristos 
418*6881a400Schristos   type *void_ptr_type
419*6881a400Schristos     = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
420*6881a400Schristos 
421*6881a400Schristos   type *int32_type = builtin_type (gdbarch)->builtin_int32;
422*6881a400Schristos   type *uint32_type = builtin_type (gdbarch)->builtin_uint32;
423*6881a400Schristos   type *uint64_type = builtin_type (gdbarch)->builtin_uint64;
424*6881a400Schristos 
425*6881a400Schristos   bool lp64 = void_ptr_type->length () == 8;
426*6881a400Schristos   size_t char_bits = gdbarch_addressable_memory_unit_size (gdbarch) * 8;
427*6881a400Schristos 
428*6881a400Schristos   /* pid_t */
429*6881a400Schristos   type *pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
430*6881a400Schristos 			      int32_type->length () * char_bits, "pid_t");
431*6881a400Schristos   pid_type->set_target_type (int32_type);
432*6881a400Schristos 
433*6881a400Schristos   /* uid_t */
434*6881a400Schristos   type *uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
435*6881a400Schristos 			      uint32_type->length () * char_bits, "uid_t");
436*6881a400Schristos   uid_type->set_target_type (uint32_type);
437*6881a400Schristos 
438*6881a400Schristos   /* clock_t */
439*6881a400Schristos   type *clock_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
440*6881a400Schristos 				int_type->length () * char_bits, "clock_t");
441*6881a400Schristos   clock_type->set_target_type (int_type);
442*6881a400Schristos 
443*6881a400Schristos   /* lwpid_t */
444*6881a400Schristos   type *lwpid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
445*6881a400Schristos 				int32_type->length () * char_bits,
446*6881a400Schristos 				"lwpid_t");
447*6881a400Schristos   lwpid_type->set_target_type (int32_type);
448*6881a400Schristos 
449*6881a400Schristos   /* union sigval */
450*6881a400Schristos   type *sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
451*6881a400Schristos   sigval_type->set_name (gdbarch_obstack_strdup (gdbarch, "sigval"));
452*6881a400Schristos   append_composite_type_field (sigval_type, "sival_int", int_type);
453*6881a400Schristos   append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
454*6881a400Schristos 
455*6881a400Schristos   /* union _option */
456*6881a400Schristos   type *option_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
457*6881a400Schristos   option_type->set_name (gdbarch_obstack_strdup (gdbarch, "_option"));
458*6881a400Schristos   append_composite_type_field (option_type, "_pe_other_pid", pid_type);
459*6881a400Schristos   append_composite_type_field (option_type, "_pe_lwp", lwpid_type);
460*6881a400Schristos 
461*6881a400Schristos   /* union _reason */
462*6881a400Schristos   type *reason_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
463*6881a400Schristos 
464*6881a400Schristos   /* _rt */
465*6881a400Schristos   type *t = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
466*6881a400Schristos   append_composite_type_field (t, "_pid", pid_type);
467*6881a400Schristos   append_composite_type_field (t, "_uid", uid_type);
468*6881a400Schristos   append_composite_type_field (t, "_value", sigval_type);
469*6881a400Schristos   append_composite_type_field (reason_type, "_rt", t);
470*6881a400Schristos 
471*6881a400Schristos   /* _child */
472*6881a400Schristos   t = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
473*6881a400Schristos   append_composite_type_field (t, "_pid", pid_type);
474*6881a400Schristos   append_composite_type_field (t, "_uid", uid_type);
475*6881a400Schristos   append_composite_type_field (t, "_status", int_type);
476*6881a400Schristos   append_composite_type_field (t, "_utime", clock_type);
477*6881a400Schristos   append_composite_type_field (t, "_stime", clock_type);
478*6881a400Schristos   append_composite_type_field (reason_type, "_child", t);
479*6881a400Schristos 
480*6881a400Schristos   /* _fault */
481*6881a400Schristos   t = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
482*6881a400Schristos   append_composite_type_field (t, "_addr", void_ptr_type);
483*6881a400Schristos   append_composite_type_field (t, "_trap", int_type);
484*6881a400Schristos   append_composite_type_field (t, "_trap2", int_type);
485*6881a400Schristos   append_composite_type_field (t, "_trap3", int_type);
486*6881a400Schristos   append_composite_type_field (reason_type, "_fault", t);
487*6881a400Schristos 
488*6881a400Schristos   /* _poll */
489*6881a400Schristos   t = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
490*6881a400Schristos   append_composite_type_field (t, "_band", long_type);
491*6881a400Schristos   append_composite_type_field (t, "_fd", int_type);
492*6881a400Schristos   append_composite_type_field (reason_type, "_poll", t);
493*6881a400Schristos 
494*6881a400Schristos   /* _syscall */
495*6881a400Schristos   t = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
496*6881a400Schristos   append_composite_type_field (t, "_sysnum", int_type);
497*6881a400Schristos   append_composite_type_field (t, "_retval",
498*6881a400Schristos 			       init_vector_type (int_type, 2));
499*6881a400Schristos   append_composite_type_field (t, "_error", int_type);
500*6881a400Schristos   append_composite_type_field (t, "_args",
501*6881a400Schristos 			       init_vector_type (uint64_type, 8));
502*6881a400Schristos   append_composite_type_field (reason_type, "_syscall", t);
503*6881a400Schristos 
504*6881a400Schristos   /* _ptrace_state */
505*6881a400Schristos   t = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
506*6881a400Schristos   append_composite_type_field (t, "_pe_report_event", int_type);
507*6881a400Schristos   append_composite_type_field (t, "_option", option_type);
508*6881a400Schristos   append_composite_type_field (reason_type, "_ptrace_state", t);
509*6881a400Schristos 
510*6881a400Schristos   /* struct _ksiginfo */
511*6881a400Schristos   type *ksiginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
512*6881a400Schristos   ksiginfo_type->set_name (gdbarch_obstack_strdup (gdbarch, "_ksiginfo"));
513*6881a400Schristos   append_composite_type_field (ksiginfo_type, "_signo", int_type);
514*6881a400Schristos   append_composite_type_field (ksiginfo_type, "_code", int_type);
515*6881a400Schristos   append_composite_type_field (ksiginfo_type, "_errno", int_type);
516*6881a400Schristos   if (lp64)
517*6881a400Schristos     append_composite_type_field (ksiginfo_type, "_pad", int_type);
518*6881a400Schristos   append_composite_type_field (ksiginfo_type, "_reason", reason_type);
519*6881a400Schristos 
520*6881a400Schristos   /* union siginfo */
521*6881a400Schristos   type *siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
522*6881a400Schristos   siginfo_type->set_name (gdbarch_obstack_strdup (gdbarch, "siginfo"));
523*6881a400Schristos   append_composite_type_field (siginfo_type, "si_pad",
524*6881a400Schristos 			       init_vector_type (char_type, 128));
525*6881a400Schristos   append_composite_type_field (siginfo_type, "_info", ksiginfo_type);
526*6881a400Schristos 
527*6881a400Schristos   nbsd_gdbarch_data->siginfo_type = siginfo_type;
528*6881a400Schristos 
529*6881a400Schristos   return siginfo_type;
530*6881a400Schristos }
531*6881a400Schristos 
532*6881a400Schristos /* See netbsd-tdep.h.  */
533*6881a400Schristos 
534*6881a400Schristos void
535*6881a400Schristos nbsd_info_proc_mappings_header (int addr_bit)
536*6881a400Schristos {
537*6881a400Schristos   gdb_printf (_("Mapped address spaces:\n\n"));
538*6881a400Schristos   if (addr_bit == 64)
539*6881a400Schristos     {
540*6881a400Schristos       gdb_printf ("  %18s %18s %10s %10s %9s %s\n",
541*6881a400Schristos 		  "Start Addr",
542*6881a400Schristos 		  "  End Addr",
543*6881a400Schristos 		  "      Size", "    Offset", "Flags  ", "File");
544*6881a400Schristos     }
545*6881a400Schristos   else
546*6881a400Schristos     {
547*6881a400Schristos       gdb_printf ("\t%10s %10s %10s %10s %9s %s\n",
548*6881a400Schristos 		  "Start Addr",
549*6881a400Schristos 		  "  End Addr",
550*6881a400Schristos 		  "      Size", "    Offset", "Flags  ", "File");
551*6881a400Schristos     }
552*6881a400Schristos }
553*6881a400Schristos 
554*6881a400Schristos /* Helper function to generate mappings flags for a single VM map
555*6881a400Schristos    entry in 'info proc mappings'.  */
556*6881a400Schristos 
557*6881a400Schristos static const char *
558*6881a400Schristos nbsd_vm_map_entry_flags (int kve_flags, int kve_protection)
559*6881a400Schristos {
560*6881a400Schristos   static char vm_flags[9];
561*6881a400Schristos 
562*6881a400Schristos   vm_flags[0] = (kve_protection & KINFO_VME_PROT_READ) ? 'r' : '-';
563*6881a400Schristos   vm_flags[1] = (kve_protection & KINFO_VME_PROT_WRITE) ? 'w' : '-';
564*6881a400Schristos   vm_flags[2] = (kve_protection & KINFO_VME_PROT_EXEC) ? 'x' : '-';
565*6881a400Schristos   vm_flags[3] = ' ';
566*6881a400Schristos   vm_flags[4] = (kve_flags & KINFO_VME_FLAG_COW) ? 'C' : '-';
567*6881a400Schristos   vm_flags[5] = (kve_flags & KINFO_VME_FLAG_NEEDS_COPY) ? 'N' : '-';
568*6881a400Schristos   vm_flags[6] = (kve_flags & KINFO_VME_FLAG_PAGEABLE) ? 'P' : '-';
569*6881a400Schristos   vm_flags[7] = (kve_flags & KINFO_VME_FLAG_GROWS_UP) ? 'U'
570*6881a400Schristos     : (kve_flags & KINFO_VME_FLAG_GROWS_DOWN) ? 'D' : '-';
571*6881a400Schristos   vm_flags[8] = '\0';
572*6881a400Schristos 
573*6881a400Schristos   return vm_flags;
574*6881a400Schristos }
575*6881a400Schristos 
576*6881a400Schristos void
577*6881a400Schristos nbsd_info_proc_mappings_entry (int addr_bit, ULONGEST kve_start,
578*6881a400Schristos 			       ULONGEST kve_end, ULONGEST kve_offset,
579*6881a400Schristos 			       int kve_flags, int kve_protection,
580*6881a400Schristos 			       const char *kve_path)
581*6881a400Schristos {
582*6881a400Schristos   if (addr_bit == 64)
583*6881a400Schristos     {
584*6881a400Schristos       gdb_printf ("  %18s %18s %10s %10s %9s %s\n",
585*6881a400Schristos 		  hex_string (kve_start),
586*6881a400Schristos 		  hex_string (kve_end),
587*6881a400Schristos 		  hex_string (kve_end - kve_start),
588*6881a400Schristos 		  hex_string (kve_offset),
589*6881a400Schristos 		  nbsd_vm_map_entry_flags (kve_flags, kve_protection),
590*6881a400Schristos 		  kve_path);
591*6881a400Schristos     }
592*6881a400Schristos   else
593*6881a400Schristos     {
594*6881a400Schristos       gdb_printf ("\t%10s %10s %10s %10s %9s %s\n",
595*6881a400Schristos 		  hex_string (kve_start),
596*6881a400Schristos 		  hex_string (kve_end),
597*6881a400Schristos 		  hex_string (kve_end - kve_start),
598*6881a400Schristos 		  hex_string (kve_offset),
599*6881a400Schristos 		  nbsd_vm_map_entry_flags (kve_flags, kve_protection),
600*6881a400Schristos 		  kve_path);
601*6881a400Schristos     }
602*6881a400Schristos }
603*6881a400Schristos 
604*6881a400Schristos /* Implement the "get_syscall_number" gdbarch method.  */
605*6881a400Schristos 
606*6881a400Schristos static LONGEST
607*6881a400Schristos nbsd_get_syscall_number (struct gdbarch *gdbarch, thread_info *thread)
608*6881a400Schristos {
609*6881a400Schristos 
610*6881a400Schristos   /* NetBSD doesn't use gdbarch_get_syscall_number since NetBSD
611*6881a400Schristos      native targets fetch the system call number from the
612*6881a400Schristos      'si_sysnum' member of siginfo_t in nbsd_nat_target::wait.
613*6881a400Schristos      However, system call catching requires this function to be
614*6881a400Schristos      set.  */
615*6881a400Schristos 
616*6881a400Schristos   internal_error (_("nbsd_get_sycall_number called"));
617*6881a400Schristos }
618*6881a400Schristos 
619*6881a400Schristos /* See netbsd-tdep.h.  */
620*6881a400Schristos 
621*6881a400Schristos void
622*6881a400Schristos nbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
623*6881a400Schristos {
624*6881a400Schristos   set_gdbarch_gdb_signal_from_target (gdbarch, nbsd_gdb_signal_from_target);
625*6881a400Schristos   set_gdbarch_gdb_signal_to_target (gdbarch, nbsd_gdb_signal_to_target);
626*6881a400Schristos   set_gdbarch_skip_solib_resolver (gdbarch, nbsd_skip_solib_resolver);
627*6881a400Schristos   set_gdbarch_auxv_parse (gdbarch, svr4_auxv_parse);
628*6881a400Schristos   set_gdbarch_print_auxv_entry (gdbarch, nbsd_print_auxv_entry);
629*6881a400Schristos   set_gdbarch_get_siginfo_type (gdbarch, nbsd_get_siginfo_type);
630*6881a400Schristos 
631*6881a400Schristos   /* `catch syscall' */
632*6881a400Schristos   set_xml_syscall_file_name (gdbarch, "syscalls/netbsd.xml");
633*6881a400Schristos   set_gdbarch_get_syscall_number (gdbarch, nbsd_get_syscall_number);
634*6881a400Schristos }
635