gdbsupport: make gdb_open_cloexec return scoped_fd
[binutils-gdb.git] / gdb / obsd-nat.c
1 /* Native-dependent code for OpenBSD.
2
3 Copyright (C) 2012-2021 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 "defs.h"
21 #include "gdbthread.h"
22 #include "inferior.h"
23 #include "target.h"
24
25 #include <sys/types.h>
26 #include <sys/ptrace.h>
27 #include "gdbsupport/gdb_wait.h"
28
29 #include "inf-ptrace.h"
30 #include "obsd-nat.h"
31
32 /* OpenBSD 5.2 and later include rthreads which uses a thread model
33 that maps userland threads directly onto kernel threads in a 1:1
34 fashion. */
35
36 std::string
37 obsd_nat_target::pid_to_str (ptid_t ptid)
38 {
39 if (ptid.lwp () != 0)
40 return string_printf ("thread %ld of process %d", ptid.lwp (), ptid.pid ());
41
42 return normal_pid_to_str (ptid);
43 }
44
45 void
46 obsd_nat_target::update_thread_list ()
47 {
48 pid_t pid = inferior_ptid.pid ();
49 struct ptrace_thread_state pts;
50
51 prune_threads ();
52
53 if (ptrace (PT_GET_THREAD_FIRST, pid, (caddr_t)&pts, sizeof pts) == -1)
54 perror_with_name (("ptrace"));
55
56 while (pts.pts_tid != -1)
57 {
58 ptid_t ptid = ptid_t (pid, pts.pts_tid, 0);
59
60 if (!in_thread_list (this, ptid))
61 {
62 if (inferior_ptid.lwp () == 0)
63 thread_change_ptid (this, inferior_ptid, ptid);
64 else
65 add_thread (this, ptid);
66 }
67
68 if (ptrace (PT_GET_THREAD_NEXT, pid, (caddr_t)&pts, sizeof pts) == -1)
69 perror_with_name (("ptrace"));
70 }
71 }
72
73 /* Enable additional event reporting on a new or existing process. */
74
75 static void
76 obsd_enable_proc_events (pid_t pid)
77 {
78 ptrace_event_t pe;
79
80 /* Set the initial event mask. */
81 memset (&pe, 0, sizeof pe);
82 pe.pe_set_event |= PTRACE_FORK;
83 if (ptrace (PT_SET_EVENT_MASK, pid,
84 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
85 perror_with_name (("ptrace"));
86 }
87
88 ptid_t
89 obsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
90 target_wait_flags options)
91 {
92 ptid_t wptid = inf_ptrace_target::wait (ptid, ourstatus, options);
93 if (ourstatus->kind == TARGET_WAITKIND_STOPPED)
94 {
95 ptrace_state_t pe;
96
97 pid_t pid = wptid.pid ();
98 if (ptrace (PT_GET_PROCESS_STATE, pid, (caddr_t)&pe, sizeof pe) == -1)
99 perror_with_name (("ptrace"));
100
101 wptid = ptid_t (pid, pe.pe_tid, 0);
102
103 switch (pe.pe_report_event)
104 {
105 case PTRACE_FORK:
106 ourstatus->kind = TARGET_WAITKIND_FORKED;
107 ourstatus->value.related_pid = ptid_t (pe.pe_other_pid);
108
109 /* Make sure the other end of the fork is stopped too. */
110 pid_t fpid = waitpid (pe.pe_other_pid, nullptr, 0);
111 if (fpid == -1)
112 perror_with_name (("waitpid"));
113
114 if (ptrace (PT_GET_PROCESS_STATE, fpid,
115 (caddr_t)&pe, sizeof pe) == -1)
116 perror_with_name (("ptrace"));
117
118 gdb_assert (pe.pe_report_event == PTRACE_FORK);
119 gdb_assert (pe.pe_other_pid == pid);
120 if (find_inferior_pid (this, fpid) != nullptr)
121 {
122 ourstatus->value.related_pid = ptid_t (pe.pe_other_pid);
123 wptid = ptid_t (fpid, pe.pe_tid, 0);
124 }
125
126 obsd_enable_proc_events (ourstatus->value.related_pid.pid ());
127 break;
128 }
129
130 /* Ensure the ptid is updated with an LWP id on the first stop
131 of a process. */
132 if (!in_thread_list (this, wptid))
133 {
134 if (in_thread_list (this, ptid_t (pid)))
135 thread_change_ptid (this, ptid_t (pid), wptid);
136 else
137 add_thread (this, wptid);
138 }
139 }
140 return wptid;
141 }
142
143 void
144 obsd_nat_target::post_attach (int pid)
145 {
146 obsd_enable_proc_events (pid);
147 }
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 }