Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / mips64-obsd-nat.c
blobced3ffb2ee28be2c755320133a59737b1d1f423c
1 /* Native-dependent code for OpenBSD/mips64.
3 Copyright (C) 2004-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 "regcache.h"
22 #include "target.h"
24 #include <sys/types.h>
25 #include <sys/ptrace.h>
26 #include <machine/reg.h>
28 #include "mips-tdep.h"
29 #include "inf-ptrace.h"
30 #include "obsd-nat.h"
32 /* Shorthand for some register numbers used below. */
33 #define MIPS_PC_REGNUM MIPS_EMBED_PC_REGNUM
34 #define MIPS_FP0_REGNUM MIPS_EMBED_FP0_REGNUM
35 #define MIPS_FSR_REGNUM MIPS_EMBED_FP0_REGNUM + 32
37 struct mips64_obsd_nat_target final : public obsd_nat_target
39 void fetch_registers (struct regcache *, int) override;
40 void store_registers (struct regcache *, int) override;
43 static mips64_obsd_nat_target the_mips64_obsd_nat_target;
45 /* Supply the general-purpose registers stored in GREGS to REGCACHE. */
47 static void
48 mips64obsd_supply_gregset (struct regcache *regcache, const void *gregs)
50 const char *regs = gregs;
51 int regnum;
53 for (regnum = MIPS_ZERO_REGNUM; regnum <= MIPS_PC_REGNUM; regnum++)
54 regcache->raw_supply (regnum, regs + regnum * 8);
56 for (regnum = MIPS_FP0_REGNUM; regnum <= MIPS_FSR_REGNUM; regnum++)
57 regcache->raw_supply (regnum, regs + (regnum + 2) * 8);
60 /* Collect the general-purpose registers from REGCACHE and store them
61 in GREGS. */
63 static void
64 mips64obsd_collect_gregset (const struct regcache *regcache,
65 void *gregs, int regnum)
67 char *regs = gregs;
68 int i;
70 for (i = MIPS_ZERO_REGNUM; i <= MIPS_PC_REGNUM; i++)
72 if (regnum == -1 || regnum == i)
73 regcache->raw_collect (i, regs + i * 8);
76 for (i = MIPS_FP0_REGNUM; i <= MIPS_FSR_REGNUM; i++)
78 if (regnum == -1 || regnum == i)
79 regcache->raw_collect (i, regs + (i + 2) * 8);
84 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
85 for all registers. */
87 void
88 mips64_obsd_nat_target::fetch_registers (struct regcache *regcache, int regnum)
90 struct reg regs;
91 pid_t pid = regcache->ptid ().pid ();
93 if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
94 perror_with_name (_("Couldn't get registers"));
96 mips64obsd_supply_gregset (regcache, &regs);
99 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
100 this for all registers. */
102 static void
103 mips64_obsd_nat_target::store_registers (struct regcache *regcache, int regnum)
105 struct reg regs;
106 pid_t pid = regcache->ptid ().pid ();
108 if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
109 perror_with_name (_("Couldn't get registers"));
111 mips64obsd_collect_gregset (regcache, &regs, regnum);
113 if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
114 perror_with_name (_("Couldn't write registers"));
117 void _initialize_mips64obsd_nat ();
118 void
119 _initialize_mips64obsd_nat ()
121 add_inf_child_target (&the_mips64_obsd_nat_target);