1 /* Native-dependent code for OpenBSD.
3 Copyright (C) 2012-2018 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/>. */
21 #include "gdbthread.h"
25 #include <sys/types.h>
26 #include <sys/ptrace.h>
29 #include "inf-child.h"
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
36 #ifdef PT_GET_THREAD_FIRST
39 obsd_nat_target::pid_to_str (ptid_t ptid)
45 xsnprintf (buf, sizeof buf, "thread %ld", ptid.lwp ());
49 return normal_pid_to_str (ptid);
53 obsd_nat_target::update_thread_list ()
55 pid_t pid = inferior_ptid.pid ();
56 struct ptrace_thread_state pts;
60 if (ptrace (PT_GET_THREAD_FIRST, pid, (caddr_t)&pts, sizeof pts) == -1)
61 perror_with_name (("ptrace"));
63 while (pts.pts_tid != -1)
65 ptid_t ptid = ptid_t (pid, pts.pts_tid, 0);
67 if (!in_thread_list (ptid))
69 if (inferior_ptid.lwp () == 0)
70 thread_change_ptid (inferior_ptid, ptid);
75 if (ptrace (PT_GET_THREAD_NEXT, pid, (caddr_t)&pts, sizeof pts) == -1)
76 perror_with_name (("ptrace"));
81 obsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
85 int status, save_errno;
93 pid = waitpid (ptid.pid (), &status, 0);
96 while (pid == -1 && errno == EINTR);
102 fprintf_unfiltered (gdb_stderr,
103 _("Child process unexpectedly missing: %s.\n"),
104 safe_strerror (save_errno));
106 /* Claim it exited with unknown signal. */
107 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
108 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
109 return inferior_ptid;
112 /* Ignore terminated detached child processes. */
113 if (!WIFSTOPPED (status) && pid != inferior_ptid.pid ())
120 if (WIFSTOPPED (status))
125 if (ptrace (PT_GET_PROCESS_STATE, pid, (caddr_t)&pe, sizeof pe) == -1)
126 perror_with_name (("ptrace"));
128 switch (pe.pe_report_event)
131 ourstatus->kind = TARGET_WAITKIND_FORKED;
132 ourstatus->value.related_pid = ptid_t (pe.pe_other_pid);
134 /* Make sure the other end of the fork is stopped too. */
135 fpid = waitpid (pe.pe_other_pid, &status, 0);
137 perror_with_name (("waitpid"));
139 if (ptrace (PT_GET_PROCESS_STATE, fpid,
140 (caddr_t)&pe, sizeof pe) == -1)
141 perror_with_name (("ptrace"));
143 gdb_assert (pe.pe_report_event == PTRACE_FORK);
144 gdb_assert (pe.pe_other_pid == pid);
145 if (fpid == inferior_ptid.pid ())
147 ourstatus->value.related_pid = ptid_t (pe.pe_other_pid);
148 return ptid_t (fpid);
154 ptid = ptid_t (pid, pe.pe_tid, 0);
155 if (!in_thread_list (ptid))
157 if (inferior_ptid.lwp () == 0)
158 thread_change_ptid (inferior_ptid, ptid);
164 store_waitstatus (ourstatus, status);
168 #endif /* PT_GET_THREAD_FIRST */