Update copyright year range in all GDB files
[deliverable/binutils-gdb.git] / gdb / obsd-nat.c
CommitLineData
863e4da4
MK
1/* Native-dependent code for OpenBSD.
2
3666a048 3 Copyright (C) 2012-2021 Free Software Foundation, Inc.
863e4da4
MK
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 "defs.h"
21#include "gdbthread.h"
22#include "inferior.h"
23#include "target.h"
24
863e4da4
MK
25#include <sys/types.h>
26#include <sys/ptrace.h>
268a13a5 27#include "gdbsupport/gdb_wait.h"
863e4da4
MK
28
29#include "inf-child.h"
30#include "obsd-nat.h"
31
32/* OpenBSD 5.2 and later include rthreads which uses a thread model
ab4756af 33 that maps userland threads directly onto kernel threads in a 1:1
863e4da4
MK
34 fashion. */
35
36#ifdef PT_GET_THREAD_FIRST
37
89b085ac 38std::string
f6ac5f3d 39obsd_nat_target::pid_to_str (ptid_t ptid)
863e4da4 40{
e38504b3 41 if (ptid.lwp () != 0)
a068643d 42 return string_printf ("thread %ld", ptid.lwp ());
863e4da4
MK
43
44 return normal_pid_to_str (ptid);
45}
46
f6ac5f3d
PA
47void
48obsd_nat_target::update_thread_list ()
863e4da4 49{
e99b03dc 50 pid_t pid = inferior_ptid.pid ();
863e4da4
MK
51 struct ptrace_thread_state pts;
52
e8032dde
PA
53 prune_threads ();
54
ab4756af 55 if (ptrace (PT_GET_THREAD_FIRST, pid, (caddr_t)&pts, sizeof pts) == -1)
863e4da4
MK
56 perror_with_name (("ptrace"));
57
58 while (pts.pts_tid != -1)
59 {
fd79271b 60 ptid_t ptid = ptid_t (pid, pts.pts_tid, 0);
863e4da4 61
c7d64809 62 if (!in_thread_list (this, ptid))
863e4da4 63 {
e38504b3 64 if (inferior_ptid.lwp () == 0)
c7d64809 65 thread_change_ptid (this, inferior_ptid, ptid);
863e4da4 66 else
c7d64809 67 add_thread (this, ptid);
863e4da4
MK
68 }
69
ab4756af 70 if (ptrace (PT_GET_THREAD_NEXT, pid, (caddr_t)&pts, sizeof pts) == -1)
863e4da4
MK
71 perror_with_name (("ptrace"));
72 }
73}
74
f6ac5f3d
PA
75ptid_t
76obsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
b60cea74 77 target_wait_flags options)
863e4da4
MK
78{
79 pid_t pid;
80 int status, save_errno;
81
82 do
83 {
84 set_sigint_trap ();
85
86 do
87 {
e99b03dc 88 pid = waitpid (ptid.pid (), &status, 0);
863e4da4
MK
89 save_errno = errno;
90 }
91 while (pid == -1 && errno == EINTR);
92
93 clear_sigint_trap ();
94
95 if (pid == -1)
96 {
97 fprintf_unfiltered (gdb_stderr,
98 _("Child process unexpectedly missing: %s.\n"),
99 safe_strerror (save_errno));
100
101 /* Claim it exited with unknown signal. */
102 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
103 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
104 return inferior_ptid;
105 }
106
107 /* Ignore terminated detached child processes. */
e99b03dc 108 if (!WIFSTOPPED (status) && pid != inferior_ptid.pid ())
863e4da4
MK
109 pid = -1;
110 }
111 while (pid == -1);
112
f2907e49 113 ptid = ptid_t (pid);
863e4da4
MK
114
115 if (WIFSTOPPED (status))
116 {
117 ptrace_state_t pe;
118 pid_t fpid;
119
120 if (ptrace (PT_GET_PROCESS_STATE, pid, (caddr_t)&pe, sizeof pe) == -1)
121 perror_with_name (("ptrace"));
122
123 switch (pe.pe_report_event)
124 {
125 case PTRACE_FORK:
126 ourstatus->kind = TARGET_WAITKIND_FORKED;
f2907e49 127 ourstatus->value.related_pid = ptid_t (pe.pe_other_pid);
863e4da4
MK
128
129 /* Make sure the other end of the fork is stopped too. */
130 fpid = waitpid (pe.pe_other_pid, &status, 0);
131 if (fpid == -1)
132 perror_with_name (("waitpid"));
133
134 if (ptrace (PT_GET_PROCESS_STATE, fpid,
135 (caddr_t)&pe, sizeof pe) == -1)
136 perror_with_name (("ptrace"));
137
138 gdb_assert (pe.pe_report_event == PTRACE_FORK);
139 gdb_assert (pe.pe_other_pid == pid);
e99b03dc 140 if (fpid == inferior_ptid.pid ())
863e4da4 141 {
f2907e49
TT
142 ourstatus->value.related_pid = ptid_t (pe.pe_other_pid);
143 return ptid_t (fpid);
863e4da4
MK
144 }
145
f2907e49 146 return ptid_t (pid);
863e4da4
MK
147 }
148
fd79271b 149 ptid = ptid_t (pid, pe.pe_tid, 0);
c7d64809 150 if (!in_thread_list (this, ptid))
863e4da4 151 {
e38504b3 152 if (inferior_ptid.lwp () == 0)
c7d64809 153 thread_change_ptid (this, inferior_ptid, ptid);
863e4da4 154 else
c7d64809 155 add_thread (this, ptid);
863e4da4
MK
156 }
157 }
158
159 store_waitstatus (ourstatus, status);
160 return ptid;
161}
162
863e4da4 163#endif /* PT_GET_THREAD_FIRST */
7632c6ce
KR
164
165#ifdef PT_GET_PROCESS_STATE
166
167void
168obsd_nat_target::post_attach (int pid)
169{
170 ptrace_event_t pe;
171
172 /* Set the initial event mask. */
173 memset (&pe, 0, sizeof pe);
174 pe.pe_set_event |= PTRACE_FORK;
175 if (ptrace (PT_SET_EVENT_MASK, pid,
176 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
177 perror_with_name (("ptrace"));
178}
179
180void
181obsd_nat_target::post_startup_inferior (ptid_t pid)
182{
183 ptrace_event_t pe;
184
185 /* Set the initial event mask. */
186 memset (&pe, 0, sizeof pe);
187 pe.pe_set_event |= PTRACE_FORK;
188 if (ptrace (PT_SET_EVENT_MASK, pid.pid (),
189 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
190 perror_with_name (("ptrace"));
191}
192
193/* Target hook for follow_fork. On entry and at return inferior_ptid is
194 the ptid of the followed inferior. */
195
196bool
197obsd_nat_target::follow_fork (bool follow_child, bool detach_fork)
198{
199 if (!follow_child)
200 {
201 struct thread_info *tp = inferior_thread ();
202 pid_t child_pid = tp->pending_follow.value.related_pid.pid ();
203
204 /* Breakpoints have already been detached from the child by
205 infrun.c. */
206
207 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
208 perror_with_name (("ptrace"));
209 }
210
211 return false;
212}
213
214int
215obsd_nat_target::insert_fork_catchpoint (int pid)
216{
217 return 0;
218}
219
220int
221obsd_nat_target::remove_fork_catchpoint (int pid)
222{
223 return 0;
224}
225
226#endif /* PT_GET_PROCESS_STATE */
This page took 1.365745 seconds and 4 git commands to generate.