]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/obsd-nat.c
LoongArch: Fix relaxation overflow caused by ld -z separate-code
[thirdparty/binutils-gdb.git] / gdb / obsd-nat.c
1 /* Native-dependent code for OpenBSD.
2
3 Copyright (C) 2012-2024 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "gdbthread.h"
21 #include "inferior.h"
22 #include "target.h"
23
24 #include <sys/types.h>
25 #include <sys/ptrace.h>
26 #include "gdbsupport/gdb_wait.h"
27
28 #include "inf-ptrace.h"
29 #include "obsd-nat.h"
30
31 /* OpenBSD 5.2 and later include rthreads which uses a thread model
32 that maps userland threads directly onto kernel threads in a 1:1
33 fashion. */
34
35 std::string
36 obsd_nat_target::pid_to_str (ptid_t ptid)
37 {
38 if (ptid.lwp () != 0)
39 return string_printf ("thread %ld of process %d", ptid.lwp (), ptid.pid ());
40
41 return normal_pid_to_str (ptid);
42 }
43
44 void
45 obsd_nat_target::update_thread_list ()
46 {
47 pid_t pid = inferior_ptid.pid ();
48 struct ptrace_thread_state pts;
49
50 prune_threads ();
51
52 if (ptrace (PT_GET_THREAD_FIRST, pid, (caddr_t)&pts, sizeof pts) == -1)
53 perror_with_name (("ptrace"));
54
55 while (pts.pts_tid != -1)
56 {
57 ptid_t ptid = ptid_t (pid, pts.pts_tid, 0);
58
59 if (!in_thread_list (this, ptid))
60 {
61 if (inferior_ptid.lwp () == 0)
62 thread_change_ptid (this, inferior_ptid, ptid);
63 else
64 add_thread (this, ptid);
65 }
66
67 if (ptrace (PT_GET_THREAD_NEXT, pid, (caddr_t)&pts, sizeof pts) == -1)
68 perror_with_name (("ptrace"));
69 }
70 }
71
72 /* Enable additional event reporting on a new or existing process. */
73
74 static void
75 obsd_enable_proc_events (pid_t pid)
76 {
77 ptrace_event_t pe;
78
79 /* Set the initial event mask. */
80 memset (&pe, 0, sizeof pe);
81 pe.pe_set_event |= PTRACE_FORK;
82 if (ptrace (PT_SET_EVENT_MASK, pid,
83 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
84 perror_with_name (("ptrace"));
85 }
86
87 ptid_t
88 obsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
89 target_wait_flags options)
90 {
91 ptid_t wptid = inf_ptrace_target::wait (ptid, ourstatus, options);
92 if (ourstatus->kind () == TARGET_WAITKIND_STOPPED)
93 {
94 ptrace_state_t pe;
95
96 pid_t pid = wptid.pid ();
97 if (ptrace (PT_GET_PROCESS_STATE, pid, (caddr_t)&pe, sizeof pe) == -1)
98 perror_with_name (("ptrace"));
99
100 wptid = ptid_t (pid, pe.pe_tid, 0);
101
102 switch (pe.pe_report_event)
103 {
104 case PTRACE_FORK:
105 ourstatus->set_forked (ptid_t (pe.pe_other_pid));
106
107 /* Make sure the other end of the fork is stopped too. */
108 pid_t fpid = waitpid (pe.pe_other_pid, nullptr, 0);
109 if (fpid == -1)
110 perror_with_name (("waitpid"));
111
112 if (ptrace (PT_GET_PROCESS_STATE, fpid,
113 (caddr_t)&pe, sizeof pe) == -1)
114 perror_with_name (("ptrace"));
115
116 gdb_assert (pe.pe_report_event == PTRACE_FORK);
117 gdb_assert (pe.pe_other_pid == pid);
118 if (find_inferior_pid (this, fpid) != nullptr)
119 {
120 ourstatus->set_forked (ptid_t (pe.pe_other_pid));
121 wptid = ptid_t (fpid, pe.pe_tid, 0);
122 }
123
124 obsd_enable_proc_events (ourstatus->child_ptid ().pid ());
125 break;
126 }
127
128 /* Ensure the ptid is updated with an LWP id on the first stop
129 of a process. */
130 if (!in_thread_list (this, wptid))
131 {
132 if (in_thread_list (this, ptid_t (pid)))
133 thread_change_ptid (this, ptid_t (pid), wptid);
134 else
135 add_thread (this, wptid);
136 }
137 }
138 return wptid;
139 }
140
141 void
142 obsd_nat_target::post_attach (int pid)
143 {
144 obsd_enable_proc_events (pid);
145 }
146
147 /* Implement the virtual inf_ptrace_target::post_startup_inferior method. */
148
149 void
150 obsd_nat_target::post_startup_inferior (ptid_t pid)
151 {
152 obsd_enable_proc_events (pid.pid ());
153 }
154
155 /* Target hook for follow_fork. */
156
157 void
158 obsd_nat_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
159 target_waitkind fork_kind,
160 bool follow_child, bool detach_fork)
161 {
162 inf_ptrace_target::follow_fork (child_inf, child_ptid, fork_kind,
163 follow_child, detach_fork);
164
165 if (!follow_child && detach_fork)
166 {
167 /* Breakpoints have already been detached from the child by
168 infrun.c. */
169
170 if (ptrace (PT_DETACH, child_ptid.pid (), (PTRACE_TYPE_ARG3)1, 0) == -1)
171 perror_with_name (("ptrace"));
172 }
173 }
174
175 int
176 obsd_nat_target::insert_fork_catchpoint (int pid)
177 {
178 return 0;
179 }
180
181 int
182 obsd_nat_target::remove_fork_catchpoint (int pid)
183 {
184 return 0;
185 }