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