Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / x86-bsd-nat.c
blob2bdd385e7f24e19198e15b2c7996d4a65522d592
1 /* Native-dependent code for X86 BSD's.
3 Copyright (C) 2003-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "inferior.h"
21 #include "gdbthread.h"
23 /* We include <signal.h> to make sure `struct fxsave64' is defined on
24 NetBSD, since NetBSD's <machine/reg.h> needs it. */
25 #include <signal.h>
26 #include <sys/types.h>
27 #include <sys/ptrace.h>
28 #include <machine/reg.h>
30 #include "x86-nat.h"
31 #include "x86-bsd-nat.h"
32 #include "inf-ptrace.h"
35 /* Support for debug registers. */
37 #ifdef HAVE_PT_GETDBREGS
39 static PTRACE_TYPE_RET
40 gdb_ptrace (PTRACE_TYPE_ARG1 request, ptid_t ptid, PTRACE_TYPE_ARG3 addr)
42 #ifdef __NetBSD__
43 /* Support for NetBSD threads: unlike other ptrace implementations in this
44 file, NetBSD requires that we pass both the pid and lwp. */
45 return ptrace (request, ptid.pid (), addr, ptid.lwp ());
46 #else
47 pid_t pid = get_ptrace_pid (ptid);
48 return ptrace (request, pid, addr, 0);
49 #endif
52 /* Helper macro to access debug register X. FreeBSD/amd64 and modern
53 versions of FreeBSD/i386 provide this macro in system headers. Define
54 a local version for systems that do not provide it. */
55 #ifndef DBREG_DRX
56 #ifdef __NetBSD__
57 #define DBREG_DRX(d, x) ((d)->dr[x])
58 #else
59 #define DBREG_DRX(d, x) ((&d->dr0)[x])
60 #endif
61 #endif
63 static unsigned long
64 x86bsd_dr_get (ptid_t ptid, int regnum)
66 struct dbreg dbregs;
68 if (gdb_ptrace (PT_GETDBREGS, ptid, (PTRACE_TYPE_ARG3) &dbregs) == -1)
69 perror_with_name (_("Couldn't read debug registers"));
71 return DBREG_DRX ((&dbregs), regnum);
74 static void
75 x86bsd_dr_set (ptid_t ptid, int regnum, unsigned long value)
77 struct dbreg dbregs;
79 if (gdb_ptrace (PT_GETDBREGS, ptid, (PTRACE_TYPE_ARG3) &dbregs) == -1)
80 perror_with_name (_("Couldn't get debug registers"));
82 /* For some mysterious reason, some of the reserved bits in the
83 debug control register get set. Mask these off, otherwise the
84 ptrace call below will fail. */
85 DBREG_DRX ((&dbregs), 7) &= ~(0xffffffff0000fc00);
87 DBREG_DRX ((&dbregs), regnum) = value;
89 for (thread_info *thread : current_inferior ()->non_exited_threads ())
91 if (gdb_ptrace (PT_SETDBREGS, thread->ptid,
92 (PTRACE_TYPE_ARG3) &dbregs) == -1)
93 perror_with_name (_("Couldn't write debug registers"));
97 static void
98 x86bsd_dr_set_control (unsigned long control)
100 x86bsd_dr_set (inferior_ptid, 7, control);
103 static void
104 x86bsd_dr_set_addr (int regnum, CORE_ADDR addr)
106 gdb_assert (regnum >= 0 && regnum <= 4);
108 x86bsd_dr_set (inferior_ptid, regnum, addr);
111 static CORE_ADDR
112 x86bsd_dr_get_addr (int regnum)
114 return x86bsd_dr_get (inferior_ptid, regnum);
117 static unsigned long
118 x86bsd_dr_get_status (void)
120 return x86bsd_dr_get (inferior_ptid, 6);
123 static unsigned long
124 x86bsd_dr_get_control (void)
126 return x86bsd_dr_get (inferior_ptid, 7);
129 #endif /* PT_GETDBREGS */
131 void _initialize_x86_bsd_nat ();
132 void
133 _initialize_x86_bsd_nat ()
135 #ifdef HAVE_PT_GETDBREGS
136 x86_dr_low.set_control = x86bsd_dr_set_control;
137 x86_dr_low.set_addr = x86bsd_dr_set_addr;
138 x86_dr_low.get_addr = x86bsd_dr_get_addr;
139 x86_dr_low.get_status = x86bsd_dr_get_status;
140 x86_dr_low.get_control = x86bsd_dr_get_control;
141 x86_set_debug_register_length (sizeof (void *));
142 #endif /* HAVE_PT_GETDBREGS */