M68K Linux: Define regset structures.
[deliverable/binutils-gdb.git] / gdb / obsd-nat.c
CommitLineData
863e4da4
MK
1/* Native-dependent code for OpenBSD.
2
3 Copyright (C) 2012-2014 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
863e4da4
MK
25#include <sys/types.h>
26#include <sys/ptrace.h>
dc92ace0 27#include "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
38static char *
39obsd_pid_to_str (struct target_ops *ops, ptid_t ptid)
40{
41 if (ptid_get_lwp (ptid) != 0)
42 {
43 static char buf[64];
44
45 xsnprintf (buf, sizeof buf, "thread %ld", ptid_get_lwp (ptid));
46 return buf;
47 }
48
49 return normal_pid_to_str (ptid);
50}
51
52static void
53obsd_find_new_threads (struct target_ops *ops)
54{
55 pid_t pid = ptid_get_pid (inferior_ptid);
56 struct ptrace_thread_state pts;
57
ab4756af 58 if (ptrace (PT_GET_THREAD_FIRST, pid, (caddr_t)&pts, sizeof pts) == -1)
863e4da4
MK
59 perror_with_name (("ptrace"));
60
61 while (pts.pts_tid != -1)
62 {
63 ptid_t ptid = ptid_build (pid, pts.pts_tid, 0);
64
65 if (!in_thread_list (ptid))
66 {
67 if (ptid_get_lwp (inferior_ptid) == 0)
68 thread_change_ptid (inferior_ptid, ptid);
69 else
70 add_thread (ptid);
71 }
72
ab4756af 73 if (ptrace (PT_GET_THREAD_NEXT, pid, (caddr_t)&pts, sizeof pts) == -1)
863e4da4
MK
74 perror_with_name (("ptrace"));
75 }
76}
77
78static ptid_t
79obsd_wait (struct target_ops *ops,
80 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
81{
82 pid_t pid;
83 int status, save_errno;
84
85 do
86 {
87 set_sigint_trap ();
88
89 do
90 {
91 pid = waitpid (ptid_get_pid (ptid), &status, 0);
92 save_errno = errno;
93 }
94 while (pid == -1 && errno == EINTR);
95
96 clear_sigint_trap ();
97
98 if (pid == -1)
99 {
100 fprintf_unfiltered (gdb_stderr,
101 _("Child process unexpectedly missing: %s.\n"),
102 safe_strerror (save_errno));
103
104 /* Claim it exited with unknown signal. */
105 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
106 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
107 return inferior_ptid;
108 }
109
110 /* Ignore terminated detached child processes. */
111 if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid))
112 pid = -1;
113 }
114 while (pid == -1);
115
116 ptid = pid_to_ptid (pid);
117
118 if (WIFSTOPPED (status))
119 {
120 ptrace_state_t pe;
121 pid_t fpid;
122
123 if (ptrace (PT_GET_PROCESS_STATE, pid, (caddr_t)&pe, sizeof pe) == -1)
124 perror_with_name (("ptrace"));
125
126 switch (pe.pe_report_event)
127 {
128 case PTRACE_FORK:
129 ourstatus->kind = TARGET_WAITKIND_FORKED;
130 ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
131
132 /* Make sure the other end of the fork is stopped too. */
133 fpid = waitpid (pe.pe_other_pid, &status, 0);
134 if (fpid == -1)
135 perror_with_name (("waitpid"));
136
137 if (ptrace (PT_GET_PROCESS_STATE, fpid,
138 (caddr_t)&pe, sizeof pe) == -1)
139 perror_with_name (("ptrace"));
140
141 gdb_assert (pe.pe_report_event == PTRACE_FORK);
142 gdb_assert (pe.pe_other_pid == pid);
143 if (fpid == ptid_get_pid (inferior_ptid))
144 {
145 ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
146 return pid_to_ptid (fpid);
147 }
148
149 return pid_to_ptid (pid);
150 }
151
152 ptid = ptid_build (pid, pe.pe_tid, 0);
153 if (!in_thread_list (ptid))
154 {
155 if (ptid_get_lwp (inferior_ptid) == 0)
156 thread_change_ptid (inferior_ptid, ptid);
157 else
158 add_thread (ptid);
159 }
160 }
161
162 store_waitstatus (ourstatus, status);
163 return ptid;
164}
165
166void
167obsd_add_target (struct target_ops *t)
168{
169 /* Override some methods to support threads. */
170 t->to_pid_to_str = obsd_pid_to_str;
171 t->to_find_new_threads = obsd_find_new_threads;
172 t->to_wait = obsd_wait;
173 add_target (t);
174}
175
176#else
177
178void
179obsd_add_target (struct target_ops *t)
180{
181 add_target (t);
182}
183
184#endif /* PT_GET_THREAD_FIRST */
This page took 0.079903 seconds and 4 git commands to generate.