Fri Dec 1 22:22:40 2017 UTC ()
don't suspend other threads if we are resuming.


(christos)
diff -r1.3 -r1.4 src/external/gpl3/gdb/dist/gdb/nbsd-nat.c

cvs diff -r1.3 -r1.4 src/external/gpl3/gdb/dist/gdb/nbsd-nat.c (switch to unified diff)

--- src/external/gpl3/gdb/dist/gdb/nbsd-nat.c 2017/12/01 22:20:44 1.3
+++ src/external/gpl3/gdb/dist/gdb/nbsd-nat.c 2017/12/01 22:22:40 1.4
@@ -1,990 +1,997 @@ @@ -1,990 +1,997 @@
1/* Native-dependent code for NetBSD 1/* Native-dependent code for NetBSD
2 2
3 Copyright (C) 2002-2017 Free Software Foundation, Inc. 3 Copyright (C) 2002-2017 Free Software Foundation, Inc.
4 4
5 This file is part of GDB. 5 This file is part of GDB.
6 6
7 This program is free software; you can redistribute it and/or modify 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 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 9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version. 10 (at your option) any later version.
11 11
12 This program is distributed in the hope that it will be useful, 12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 15 GNU General Public License for more details.
16 16
17 You should have received a copy of the GNU General Public License 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/>. */ 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 19
20#include "defs.h" 20#include "defs.h"
21#include "gdbcore.h" 21#include "gdbcore.h"
22#include "inferior.h" 22#include "inferior.h"
23#include "regcache.h" 23#include "regcache.h"
24#include "regset.h" 24#include "regset.h"
25#include "gdbcmd.h" 25#include "gdbcmd.h"
26#include "gdbthread.h" 26#include "gdbthread.h"
27#include "gdb_wait.h" 27#include "gdb_wait.h"
28#include <sys/types.h> 28#include <sys/types.h>
29#include <sys/ptrace.h> 29#include <sys/ptrace.h>
30#include <sys/sysctl.h> 30#include <sys/sysctl.h>
31#ifdef HAVE_KINFO_GETVMMAP 31#ifdef HAVE_KINFO_GETVMMAP
32#include <util.h> 32#include <util.h>
33#endif 33#endif
34 34
35#include "elf-bfd.h" 35#include "elf-bfd.h"
36#include "nbsd-nat.h" 36#include "nbsd-nat.h"
37 37
38/* Return the name of a file that can be opened to get the symbols for 38/* Return the name of a file that can be opened to get the symbols for
39 the child process identified by PID. */ 39 the child process identified by PID. */
40 40
41static char * 41static char *
42nbsd_pid_to_exec_file (struct target_ops *self, int pid) 42nbsd_pid_to_exec_file (struct target_ops *self, int pid)
43{ 43{
44 ssize_t len; 44 ssize_t len;
45 static char buf[PATH_MAX]; 45 static char buf[PATH_MAX];
46 char name[PATH_MAX]; 46 char name[PATH_MAX];
47 47
48#ifdef KERN_PROC_PATHNAME 48#ifdef KERN_PROC_PATHNAME
49 size_t buflen; 49 size_t buflen;
50 int mib[4]; 50 int mib[4];
51 51
52 mib[0] = CTL_KERN; 52 mib[0] = CTL_KERN;
53 mib[1] = KERN_PROC; 53 mib[1] = KERN_PROC;
54 mib[2] = KERN_PROC_PATHNAME; 54 mib[2] = KERN_PROC_PATHNAME;
55 mib[3] = pid; 55 mib[3] = pid;
56 buflen = sizeof buf; 56 buflen = sizeof buf;
57 if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0) 57 if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0)
58 return buf; 58 return buf;
59#endif 59#endif
60 60
61 xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid); 61 xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
62 len = readlink (name, buf, PATH_MAX - 1); 62 len = readlink (name, buf, PATH_MAX - 1);
63 if (len != -1) 63 if (len != -1)
64 { 64 {
65 buf[len] = '\0'; 65 buf[len] = '\0';
66 return buf; 66 return buf;
67 } 67 }
68 68
69 return NULL; 69 return NULL;
70} 70}
71 71
72/* Iterate over all the memory regions in the current inferior, 72/* Iterate over all the memory regions in the current inferior,
73 calling FUNC for each memory region. OBFD is passed as the last 73 calling FUNC for each memory region. OBFD is passed as the last
74 argument to FUNC. */ 74 argument to FUNC. */
75 75
76static int 76static int
77nbsd_find_memory_regions (struct target_ops *self, 77nbsd_find_memory_regions (struct target_ops *self,
78 find_memory_region_ftype func, void *obfd) 78 find_memory_region_ftype func, void *obfd)
79{ 79{
80 pid_t pid = ptid_get_pid (inferior_ptid); 80 pid_t pid = ptid_get_pid (inferior_ptid);
81 struct kinfo_vmentry *vmentl, *kve; 81 struct kinfo_vmentry *vmentl, *kve;
82 uint64_t size; 82 uint64_t size;
83 struct cleanup *cleanup; 83 struct cleanup *cleanup;
84 int i; 84 int i;
85 size_t nitems; 85 size_t nitems;
86 86
87 vmentl = kinfo_getvmmap (pid, &nitems); 87 vmentl = kinfo_getvmmap (pid, &nitems);
88 if (vmentl == NULL) 88 if (vmentl == NULL)
89 perror_with_name (_("Couldn't fetch VM map entries.")); 89 perror_with_name (_("Couldn't fetch VM map entries."));
90 cleanup = make_cleanup (free, vmentl); 90 cleanup = make_cleanup (free, vmentl);
91 91
92 for (i = 0; i < nitems; i++) 92 for (i = 0; i < nitems; i++)
93 { 93 {
94 kve = &vmentl[i]; 94 kve = &vmentl[i];
95 95
96 /* Skip unreadable segments and those where MAP_NOCORE has been set. */ 96 /* Skip unreadable segments and those where MAP_NOCORE has been set. */
97 if (!(kve->kve_protection & KVME_PROT_READ) 97 if (!(kve->kve_protection & KVME_PROT_READ)
98 || kve->kve_flags & KVME_FLAG_NOCOREDUMP) 98 || kve->kve_flags & KVME_FLAG_NOCOREDUMP)
99 continue; 99 continue;
100 100
101 /* Skip segments with an invalid type. */ 101 /* Skip segments with an invalid type. */
102 switch (kve->kve_type) { 102 switch (kve->kve_type) {
103 case KVME_TYPE_VNODE: 103 case KVME_TYPE_VNODE:
104 case KVME_TYPE_ANON: 104 case KVME_TYPE_ANON:
105 case KVME_TYPE_SUBMAP: 105 case KVME_TYPE_SUBMAP:
106 case KVME_TYPE_OBJECT: 106 case KVME_TYPE_OBJECT:
107 break; 107 break;
108 default: 108 default:
109 continue; 109 continue;
110 } 110 }
111 111
112 size = kve->kve_end - kve->kve_start; 112 size = kve->kve_end - kve->kve_start;
113 if (info_verbose) 113 if (info_verbose)
114 { 114 {
115 fprintf_filtered (gdb_stdout,  115 fprintf_filtered (gdb_stdout,
116 "Save segment, %ld bytes at %s (%c%c%c)\n", 116 "Save segment, %ld bytes at %s (%c%c%c)\n",
117 (long) size, 117 (long) size,
118 paddress (target_gdbarch (), kve->kve_start), 118 paddress (target_gdbarch (), kve->kve_start),
119 kve->kve_protection & KVME_PROT_READ ? 'r' : '-', 119 kve->kve_protection & KVME_PROT_READ ? 'r' : '-',
120 kve->kve_protection & KVME_PROT_WRITE ? 'w' : '-', 120 kve->kve_protection & KVME_PROT_WRITE ? 'w' : '-',
121 kve->kve_protection & KVME_PROT_EXEC ? 'x' : '-'); 121 kve->kve_protection & KVME_PROT_EXEC ? 'x' : '-');
122 } 122 }
123 123
124 /* Invoke the callback function to create the corefile segment. 124 /* Invoke the callback function to create the corefile segment.
125 Pass MODIFIED as true, we do not know the real modification state. */ 125 Pass MODIFIED as true, we do not know the real modification state. */
126 func (kve->kve_start, size, kve->kve_protection & KVME_PROT_READ, 126 func (kve->kve_start, size, kve->kve_protection & KVME_PROT_READ,
127 kve->kve_protection & KVME_PROT_WRITE, 127 kve->kve_protection & KVME_PROT_WRITE,
128 kve->kve_protection & KVME_PROT_EXEC, 1, obfd); 128 kve->kve_protection & KVME_PROT_EXEC, 1, obfd);
129 } 129 }
130 do_cleanups (cleanup); 130 do_cleanups (cleanup);
131 return 0; 131 return 0;
132} 132}
133 133
134#ifdef KERN_PROC_AUXV 134#ifdef KERN_PROC_AUXV
135static enum target_xfer_status (*super_xfer_partial) (struct target_ops *ops, 135static enum target_xfer_status (*super_xfer_partial) (struct target_ops *ops,
136 enum target_object object, 136 enum target_object object,
137 const char *annex, 137 const char *annex,
138 gdb_byte *readbuf, 138 gdb_byte *readbuf,
139 const gdb_byte *writebuf, 139 const gdb_byte *writebuf,
140 ULONGEST offset, 140 ULONGEST offset,
141 ULONGEST len, 141 ULONGEST len,
142 ULONGEST *xfered_len); 142 ULONGEST *xfered_len);
143 143
144/* Implement the "to_xfer_partial target_ops" method. */ 144/* Implement the "to_xfer_partial target_ops" method. */
145 145
146static enum target_xfer_status 146static enum target_xfer_status
147nbsd_xfer_partial (struct target_ops *ops, enum target_object object, 147nbsd_xfer_partial (struct target_ops *ops, enum target_object object,
148 const char *annex, gdb_byte *readbuf, 148 const char *annex, gdb_byte *readbuf,
149 const gdb_byte *writebuf, 149 const gdb_byte *writebuf,
150 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) 150 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
151{ 151{
152 pid_t pid = ptid_get_pid (inferior_ptid); 152 pid_t pid = ptid_get_pid (inferior_ptid);
153 153
154 switch (object) 154 switch (object)
155 { 155 {
156 case TARGET_OBJECT_AUXV: 156 case TARGET_OBJECT_AUXV:
157 { 157 {
158 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL); 158 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
159 unsigned char *buf; 159 unsigned char *buf;
160 size_t buflen; 160 size_t buflen;
161 int mib[4]; 161 int mib[4];
162 162
163 if (writebuf != NULL) 163 if (writebuf != NULL)
164 return TARGET_XFER_E_IO; 164 return TARGET_XFER_E_IO;
165 mib[0] = CTL_KERN; 165 mib[0] = CTL_KERN;
166 mib[1] = KERN_PROC; 166 mib[1] = KERN_PROC;
167 mib[2] = KERN_PROC_AUXV; 167 mib[2] = KERN_PROC_AUXV;
168 mib[3] = pid; 168 mib[3] = pid;
169 if (offset == 0) 169 if (offset == 0)
170 { 170 {
171 buf = readbuf; 171 buf = readbuf;
172 buflen = len; 172 buflen = len;
173 } 173 }
174 else 174 else
175 { 175 {
176 buflen = offset + len; 176 buflen = offset + len;
177 buf = XCNEWVEC (unsigned char, buflen); 177 buf = XCNEWVEC (unsigned char, buflen);
178 cleanup = make_cleanup (xfree, buf); 178 cleanup = make_cleanup (xfree, buf);
179 } 179 }
180 if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0) 180 if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0)
181 { 181 {
182 if (offset != 0) 182 if (offset != 0)
183 { 183 {
184 if (buflen > offset) 184 if (buflen > offset)
185 { 185 {
186 buflen -= offset; 186 buflen -= offset;
187 memcpy (readbuf, buf + offset, buflen); 187 memcpy (readbuf, buf + offset, buflen);
188 } 188 }
189 else 189 else
190 buflen = 0; 190 buflen = 0;
191 } 191 }
192 do_cleanups (cleanup); 192 do_cleanups (cleanup);
193 *xfered_len = buflen; 193 *xfered_len = buflen;
194 return (buflen == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK; 194 return (buflen == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
195 } 195 }
196 do_cleanups (cleanup); 196 do_cleanups (cleanup);
197 return TARGET_XFER_E_IO; 197 return TARGET_XFER_E_IO;
198 } 198 }
199 default: 199 default:
200 return super_xfer_partial (ops, object, annex, readbuf, writebuf, offset, 200 return super_xfer_partial (ops, object, annex, readbuf, writebuf, offset,
201 len, xfered_len); 201 len, xfered_len);
202 } 202 }
203} 203}
204#endif 204#endif
205 205
206#ifdef PT_LWPINFO 206#ifdef PT_LWPINFO
207static int debug_nbsd_lwp; 207static int debug_nbsd_lwp;
208 208
209static void (*super_resume) (struct target_ops *, 209static void (*super_resume) (struct target_ops *,
210 ptid_t, 210 ptid_t,
211 int, 211 int,
212 enum gdb_signal); 212 enum gdb_signal);
213static ptid_t (*super_wait) (struct target_ops *, 213static ptid_t (*super_wait) (struct target_ops *,
214 ptid_t, 214 ptid_t,
215 struct target_waitstatus *, 215 struct target_waitstatus *,
216 int); 216 int);
217 217
218static void 218static void
219show_nbsd_lwp_debug (struct ui_file *file, int from_tty, 219show_nbsd_lwp_debug (struct ui_file *file, int from_tty,
220 struct cmd_list_element *c, const char *value) 220 struct cmd_list_element *c, const char *value)
221{ 221{
222 fprintf_filtered (file, _("Debugging of NetBSD lwp module is %s.\n"), value); 222 fprintf_filtered (file, _("Debugging of NetBSD lwp module is %s.\n"), value);
223} 223}
224 224
225#if defined(TDP_RFPPWAIT) || defined(HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME) 225#if defined(TDP_RFPPWAIT) || defined(HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME)
226/* Fetch the external variant of the kernel's internal process 226/* Fetch the external variant of the kernel's internal process
227 structure for the process PID into KP. */ 227 structure for the process PID into KP. */
228 228
229static void 229static void
230nbsd_fetch_kinfo_proc (pid_t pid, struct kinfo_proc *kp) 230nbsd_fetch_kinfo_proc (pid_t pid, struct kinfo_proc *kp)
231{ 231{
232 size_t len; 232 size_t len;
233 int mib[4]; 233 int mib[4];
234 234
235 len = sizeof *kp; 235 len = sizeof *kp;
236 mib[0] = CTL_KERN; 236 mib[0] = CTL_KERN;
237 mib[1] = KERN_PROC; 237 mib[1] = KERN_PROC;
238 mib[2] = KERN_PROC_PID; 238 mib[2] = KERN_PROC_PID;
239 mib[3] = pid; 239 mib[3] = pid;
240 if (sysctl (mib, 4, kp, &len, NULL, 0) == -1) 240 if (sysctl (mib, 4, kp, &len, NULL, 0) == -1)
241 perror_with_name (("sysctl")); 241 perror_with_name (("sysctl"));
242} 242}
243#endif 243#endif
244 244
245 245
246/* Return true if PTID is still active in the inferior. */ 246/* Return true if PTID is still active in the inferior. */
247 247
248static int 248static int
249nbsd_thread_alive (struct target_ops *ops, ptid_t ptid) 249nbsd_thread_alive (struct target_ops *ops, ptid_t ptid)
250{ 250{
251 if (ptid_lwp_p (ptid)) 251 if (ptid_lwp_p (ptid))
252 { 252 {
253 struct ptrace_lwpinfo pl; 253 struct ptrace_lwpinfo pl;
254 254
255 pl.pl_lwpid = ptid_get_lwp (ptid); 255 pl.pl_lwpid = ptid_get_lwp (ptid);
256 if (ptrace (PT_LWPINFO, ptid_get_pid (ptid), (caddr_t) &pl, sizeof pl) 256 if (ptrace (PT_LWPINFO, ptid_get_pid (ptid), (caddr_t) &pl, sizeof pl)
257 == -1) 257 == -1)
258 return 0; 258 return 0;
259#ifdef PL_FLAG_EXITED 259#ifdef PL_FLAG_EXITED
260 if (pl.pl_flags & PL_FLAG_EXITED) 260 if (pl.pl_flags & PL_FLAG_EXITED)
261 return 0; 261 return 0;
262#endif 262#endif
263 } 263 }
264 264
265 return 1; 265 return 1;
266} 266}
267 267
268/* Convert PTID to a string. Returns the string in a static 268/* Convert PTID to a string. Returns the string in a static
269 buffer. */ 269 buffer. */
270 270
271static const char * 271static const char *
272nbsd_pid_to_str (struct target_ops *ops, ptid_t ptid) 272nbsd_pid_to_str (struct target_ops *ops, ptid_t ptid)
273{ 273{
274 lwpid_t lwp; 274 lwpid_t lwp;
275 275
276 lwp = ptid_get_lwp (ptid); 276 lwp = ptid_get_lwp (ptid);
277 if (lwp != 0) 277 if (lwp != 0)
278 { 278 {
279 static char buf[64]; 279 static char buf[64];
280 int pid = ptid_get_pid (ptid); 280 int pid = ptid_get_pid (ptid);
281 281
282 xsnprintf (buf, sizeof buf, "LWP %d of process %d", lwp, pid); 282 xsnprintf (buf, sizeof buf, "LWP %d of process %d", lwp, pid);
283 return buf; 283 return buf;
284 } 284 }
285 285
286 return normal_pid_to_str (ptid); 286 return normal_pid_to_str (ptid);
287} 287}
288 288
289#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME 289#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME
290/* Return the name assigned to a thread by an application. Returns 290/* Return the name assigned to a thread by an application. Returns
291 the string in a static buffer. */ 291 the string in a static buffer. */
292 292
293static const char * 293static const char *
294nbsd_thread_name (struct target_ops *self, struct thread_info *thr) 294nbsd_thread_name (struct target_ops *self, struct thread_info *thr)
295{ 295{
296 struct ptrace_lwpinfo pl; 296 struct ptrace_lwpinfo pl;
297 struct kinfo_proc kp; 297 struct kinfo_proc kp;
298 pid_t pid = ptid_get_pid (thr->ptid); 298 pid_t pid = ptid_get_pid (thr->ptid);
299 lwpid_t lwp = ptid_get_lwp (thr->ptid); 299 lwpid_t lwp = ptid_get_lwp (thr->ptid);
300 static char buf[sizeof pl.pl_tdname + 1]; 300 static char buf[sizeof pl.pl_tdname + 1];
301 301
302 /* Note that ptrace_lwpinfo returns the process command in pl_tdname 302 /* Note that ptrace_lwpinfo returns the process command in pl_tdname
303 if a name has not been set explicitly. Return a NULL name in 303 if a name has not been set explicitly. Return a NULL name in
304 that case. */ 304 that case. */
305 nbsd_fetch_kinfo_proc (pid, &kp); 305 nbsd_fetch_kinfo_proc (pid, &kp);
306 pl.pl_lwpid = lwp; 306 pl.pl_lwpid = lwp;
307 if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof pl) == -1) 307 if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof pl) == -1)
308 perror_with_name (("ptrace")); 308 perror_with_name (("ptrace"));
309 if (strcmp (kp.ki_comm, pl.pl_tdname) == 0) 309 if (strcmp (kp.ki_comm, pl.pl_tdname) == 0)
310 return NULL; 310 return NULL;
311 xsnprintf (buf, sizeof buf, "%s", pl.pl_tdname); 311 xsnprintf (buf, sizeof buf, "%s", pl.pl_tdname);
312 return buf; 312 return buf;
313} 313}
314#endif 314#endif
315 315
316/* Enable additional event reporting on new processes. 316/* Enable additional event reporting on new processes.
317 317
318 To catch fork events, PTRACE_FORK is set on every traced process 318 To catch fork events, PTRACE_FORK is set on every traced process
319 to enable stops on returns from fork or vfork. Note that both the 319 to enable stops on returns from fork or vfork. Note that both the
320 parent and child will always stop, even if system call stops are 320 parent and child will always stop, even if system call stops are
321 not enabled. 321 not enabled.
322 322
323 To catch LWP events, PTRACE_EVENTS is set on every traced process. 323 To catch LWP events, PTRACE_EVENTS is set on every traced process.
324 This enables stops on the birth for new LWPs (excluding the "main" LWP) 324 This enables stops on the birth for new LWPs (excluding the "main" LWP)
325 and the death of LWPs (excluding the last LWP in a process). Note 325 and the death of LWPs (excluding the last LWP in a process). Note
326 that unlike fork events, the LWP that creates a new LWP does not 326 that unlike fork events, the LWP that creates a new LWP does not
327 report an event. */ 327 report an event. */
328 328
329static void 329static void
330nbsd_enable_proc_events (pid_t pid) 330nbsd_enable_proc_events (pid_t pid)
331{ 331{
332#ifdef PT_GET_EVENT_MASK 332#ifdef PT_GET_EVENT_MASK
333 int events; 333 int events;
334 334
335 if (ptrace (PT_GET_EVENT_MASK, pid, (PTRACE_TYPE_ARG3)&events, 335 if (ptrace (PT_GET_EVENT_MASK, pid, (PTRACE_TYPE_ARG3)&events,
336 sizeof (events)) == -1) 336 sizeof (events)) == -1)
337 perror_with_name (("ptrace")); 337 perror_with_name (("ptrace"));
338 events |= PTRACE_FORK; 338 events |= PTRACE_FORK;
339#ifdef PTRACE_LWP 339#ifdef PTRACE_LWP
340 events |= PTRACE_LWP; 340 events |= PTRACE_LWP;
341#endif 341#endif
342#ifdef notyet 342#ifdef notyet
343#ifdef PTRACE_VFORK 343#ifdef PTRACE_VFORK
344 events |= PTRACE_VFORK; 344 events |= PTRACE_VFORK;
345#endif 345#endif
346#endif 346#endif
347 if (ptrace (PT_SET_EVENT_MASK, pid, (PTRACE_TYPE_ARG3)&events, 347 if (ptrace (PT_SET_EVENT_MASK, pid, (PTRACE_TYPE_ARG3)&events,
348 sizeof (events)) == -1) 348 sizeof (events)) == -1)
349 perror_with_name (("ptrace")); 349 perror_with_name (("ptrace"));
350#else 350#else
351#ifdef TDP_RFPPWAIT 351#ifdef TDP_RFPPWAIT
352 if (ptrace (PT_FOLLOW_FORK, pid, (PTRACE_TYPE_ARG3)0, 1) == -1) 352 if (ptrace (PT_FOLLOW_FORK, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
353 perror_with_name (("ptrace")); 353 perror_with_name (("ptrace"));
354#endif 354#endif
355#ifdef PT_LWP_EVENTS 355#ifdef PT_LWP_EVENTS
356 if (ptrace (PT_LWP_EVENTS, pid, (PTRACE_TYPE_ARG3)0, 1) == -1) 356 if (ptrace (PT_LWP_EVENTS, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
357 perror_with_name (("ptrace")); 357 perror_with_name (("ptrace"));
358#endif 358#endif
359#endif 359#endif
360} 360}
361 361
362/* Add threads for any new LWPs in a process. 362/* Add threads for any new LWPs in a process.
363 363
364 When LWP events are used, this function is only used to detect existing 364 When LWP events are used, this function is only used to detect existing
365 threads when attaching to a process. On older systems, this function is 365 threads when attaching to a process. On older systems, this function is
366 called to discover new threads each time the thread list is updated. */ 366 called to discover new threads each time the thread list is updated. */
367 367
368static void 368static void
369nbsd_add_threads (pid_t pid) 369nbsd_add_threads (pid_t pid)
370{ 370{
371 int val; 371 int val;
372 struct ptrace_lwpinfo pl; 372 struct ptrace_lwpinfo pl;
373 373
374 gdb_assert (!in_thread_list (pid_to_ptid (pid))); 374 gdb_assert (!in_thread_list (pid_to_ptid (pid)));
375 pl.pl_lwpid = 0; 375 pl.pl_lwpid = 0;
376 while ((val = ptrace (PT_LWPINFO, pid, (void *)&pl, sizeof(pl))) != -1 376 while ((val = ptrace (PT_LWPINFO, pid, (void *)&pl, sizeof(pl))) != -1
377 && pl.pl_lwpid != 0) 377 && pl.pl_lwpid != 0)
378 { 378 {
379 ptid_t ptid = ptid_build (pid, pl.pl_lwpid, 0); 379 ptid_t ptid = ptid_build (pid, pl.pl_lwpid, 0);
380 if (!in_thread_list (ptid)) 380 if (!in_thread_list (ptid))
381 add_thread (ptid); 381 add_thread (ptid);
382 } 382 }
383} 383}
384 384
385/* Implement the "to_update_thread_list" target_ops method. */ 385/* Implement the "to_update_thread_list" target_ops method. */
386 386
387static void 387static void
388nbsd_update_thread_list (struct target_ops *ops) 388nbsd_update_thread_list (struct target_ops *ops)
389{ 389{
390#ifdef PT_LWP_EVENTS 390#ifdef PT_LWP_EVENTS
391 /* With support for thread events, threads are added/deleted from the 391 /* With support for thread events, threads are added/deleted from the
392 list as events are reported, so just try deleting exited threads. */ 392 list as events are reported, so just try deleting exited threads. */
393 delete_exited_threads (); 393 delete_exited_threads ();
394#else 394#else
395 prune_threads (); 395 prune_threads ();
396 396
397 nbsd_add_threads (ptid_get_pid (inferior_ptid)); 397 nbsd_add_threads (ptid_get_pid (inferior_ptid));
398#endif 398#endif
399} 399}
400 400
401#ifdef TDP_RFPPWAIT 401#ifdef TDP_RFPPWAIT
402/* 402/*
403 To catch fork events, PT_FOLLOW_FORK is set on every traced process 403 To catch fork events, PT_FOLLOW_FORK is set on every traced process
404 to enable stops on returns from fork or vfork. Note that both the 404 to enable stops on returns from fork or vfork. Note that both the
405 parent and child will always stop, even if system call stops are not 405 parent and child will always stop, even if system call stops are not
406 enabled. 406 enabled.
407 407
408 After a fork, both the child and parent process will stop and report 408 After a fork, both the child and parent process will stop and report
409 an event. However, there is no guarantee of order. If the parent 409 an event. However, there is no guarantee of order. If the parent
410 reports its stop first, then nbsd_wait explicitly waits for the new 410 reports its stop first, then nbsd_wait explicitly waits for the new
411 child before returning. If the child reports its stop first, then 411 child before returning. If the child reports its stop first, then
412 the event is saved on a list and ignored until the parent's stop is 412 the event is saved on a list and ignored until the parent's stop is
413 reported. nbsd_wait could have been changed to fetch the parent PID 413 reported. nbsd_wait could have been changed to fetch the parent PID
414 of the new child and used that to wait for the parent explicitly. 414 of the new child and used that to wait for the parent explicitly.
415 However, if two threads in the parent fork at the same time, then 415 However, if two threads in the parent fork at the same time, then
416 the wait on the parent might return the "wrong" fork event. 416 the wait on the parent might return the "wrong" fork event.
417 417
418 The initial version of PT_FOLLOW_FORK did not set PL_FLAG_CHILD for 418 The initial version of PT_FOLLOW_FORK did not set PL_FLAG_CHILD for
419 the new child process. This flag could be inferred by treating any 419 the new child process. This flag could be inferred by treating any
420 events for an unknown pid as a new child. 420 events for an unknown pid as a new child.
421 421
422 In addition, the initial version of PT_FOLLOW_FORK did not report a 422 In addition, the initial version of PT_FOLLOW_FORK did not report a
423 stop event for the parent process of a vfork until after the child 423 stop event for the parent process of a vfork until after the child
424 process executed a new program or exited. The kernel was changed to 424 process executed a new program or exited. The kernel was changed to
425 defer the wait for exit or exec of the child until after posting the 425 defer the wait for exit or exec of the child until after posting the
426 stop event shortly after the change to introduce PL_FLAG_CHILD. 426 stop event shortly after the change to introduce PL_FLAG_CHILD.
427 This could be worked around by reporting a vfork event when the 427 This could be worked around by reporting a vfork event when the
428 child event posted and ignoring the subsequent event from the 428 child event posted and ignoring the subsequent event from the
429 parent. 429 parent.
430 430
431 This implementation requires both of these fixes for simplicity's 431 This implementation requires both of these fixes for simplicity's
432 sake. FreeBSD versions newer than 9.1 contain both fixes. 432 sake. FreeBSD versions newer than 9.1 contain both fixes.
433*/ 433*/
434 434
435struct nbsd_fork_info 435struct nbsd_fork_info
436{ 436{
437 struct nbsd_fork_info *next; 437 struct nbsd_fork_info *next;
438 ptid_t ptid; 438 ptid_t ptid;
439}; 439};
440 440
441static struct nbsd_fork_info *nbsd_pending_children; 441static struct nbsd_fork_info *nbsd_pending_children;
442 442
443/* Record a new child process event that is reported before the 443/* Record a new child process event that is reported before the
444 corresponding fork event in the parent. */ 444 corresponding fork event in the parent. */
445 445
446static void 446static void
447nbsd_remember_child (ptid_t pid) 447nbsd_remember_child (ptid_t pid)
448{ 448{
449 struct nbsd_fork_info *info = XCNEW (struct nbsd_fork_info); 449 struct nbsd_fork_info *info = XCNEW (struct nbsd_fork_info);
450 450
451 info->ptid = pid; 451 info->ptid = pid;
452 info->next = nbsd_pending_children; 452 info->next = nbsd_pending_children;
453 nbsd_pending_children = info; 453 nbsd_pending_children = info;
454} 454}
455 455
456/* Check for a previously-recorded new child process event for PID. 456/* Check for a previously-recorded new child process event for PID.
457 If one is found, remove it from the list and return the PTID. */ 457 If one is found, remove it from the list and return the PTID. */
458 458
459static ptid_t 459static ptid_t
460nbsd_is_child_pending (pid_t pid) 460nbsd_is_child_pending (pid_t pid)
461{ 461{
462 struct nbsd_fork_info *info, *prev; 462 struct nbsd_fork_info *info, *prev;
463 ptid_t ptid; 463 ptid_t ptid;
464 464
465 prev = NULL; 465 prev = NULL;
466 for (info = nbsd_pending_children; info; prev = info, info = info->next) 466 for (info = nbsd_pending_children; info; prev = info, info = info->next)
467 { 467 {
468 if (ptid_get_pid (info->ptid) == pid) 468 if (ptid_get_pid (info->ptid) == pid)
469 { 469 {
470 if (prev == NULL) 470 if (prev == NULL)
471 nbsd_pending_children = info->next; 471 nbsd_pending_children = info->next;
472 else 472 else
473 prev->next = info->next; 473 prev->next = info->next;
474 ptid = info->ptid; 474 ptid = info->ptid;
475 xfree (info); 475 xfree (info);
476 return ptid; 476 return ptid;
477 } 477 }
478 } 478 }
479 return null_ptid; 479 return null_ptid;
480} 480}
481 481
482#ifndef PTRACE_VFORK 482#ifndef PTRACE_VFORK
483static struct nbsd_fork_info *nbsd_pending_vfork_done; 483static struct nbsd_fork_info *nbsd_pending_vfork_done;
484 484
485/* Record a pending vfork done event. */ 485/* Record a pending vfork done event. */
486 486
487static void 487static void
488nbsd_add_vfork_done (ptid_t pid) 488nbsd_add_vfork_done (ptid_t pid)
489{ 489{
490 struct nbsd_fork_info *info = XCNEW (struct nbsd_fork_info); 490 struct nbsd_fork_info *info = XCNEW (struct nbsd_fork_info);
491 491
492 info->ptid = pid; 492 info->ptid = pid;
493 info->next = nbsd_pending_vfork_done; 493 info->next = nbsd_pending_vfork_done;
494 nbsd_pending_vfork_done = info; 494 nbsd_pending_vfork_done = info;
495} 495}
496 496
497/* Check for a pending vfork done event for a specific PID. */ 497/* Check for a pending vfork done event for a specific PID. */
498 498
499static int 499static int
500nbsd_is_vfork_done_pending (pid_t pid) 500nbsd_is_vfork_done_pending (pid_t pid)
501{ 501{
502 struct nbsd_fork_info *info; 502 struct nbsd_fork_info *info;
503 503
504 for (info = nbsd_pending_vfork_done; info != NULL; info = info->next) 504 for (info = nbsd_pending_vfork_done; info != NULL; info = info->next)
505 { 505 {
506 if (ptid_get_pid (info->ptid) == pid) 506 if (ptid_get_pid (info->ptid) == pid)
507 return 1; 507 return 1;
508 } 508 }
509 return 0; 509 return 0;
510} 510}
511 511
512/* Check for a pending vfork done event. If one is found, remove it 512/* Check for a pending vfork done event. If one is found, remove it
513 from the list and return the PTID. */ 513 from the list and return the PTID. */
514 514
515static ptid_t 515static ptid_t
516nbsd_next_vfork_done (void) 516nbsd_next_vfork_done (void)
517{ 517{
518 struct nbsd_fork_info *info; 518 struct nbsd_fork_info *info;
519 ptid_t ptid; 519 ptid_t ptid;
520 520
521 if (nbsd_pending_vfork_done != NULL) 521 if (nbsd_pending_vfork_done != NULL)
522 { 522 {
523 info = nbsd_pending_vfork_done; 523 info = nbsd_pending_vfork_done;
524 nbsd_pending_vfork_done = info->next; 524 nbsd_pending_vfork_done = info->next;
525 ptid = info->ptid; 525 ptid = info->ptid;
526 xfree (info); 526 xfree (info);
527 return ptid; 527 return ptid;
528 } 528 }
529 return null_ptid; 529 return null_ptid;
530} 530}
531#endif 531#endif
532#endif 532#endif
533 533
534/* Implement the "to_resume" target_ops method. */ 534/* Implement the "to_resume" target_ops method. */
535 535
536static void 536static void
537nbsd_resume (struct target_ops *ops, 537nbsd_resume (struct target_ops *ops,
538 ptid_t ptid, int step, enum gdb_signal signo) 538 ptid_t ptid, int step, enum gdb_signal signo)
539{ 539{
540#if defined(TDP_RFPPWAIT) && !defined(PTRACE_VFORK) 540#if defined(TDP_RFPPWAIT) && !defined(PTRACE_VFORK)
541 pid_t pid; 541 pid_t pid;
542 542
543 /* Don't PT_CONTINUE a process which has a pending vfork done event. */ 543 /* Don't PT_CONTINUE a process which has a pending vfork done event. */
544 if (ptid_equal (minus_one_ptid, ptid)) 544 if (ptid_equal (minus_one_ptid, ptid))
545 pid = ptid_get_pid (inferior_ptid); 545 pid = ptid_get_pid (inferior_ptid);
546 else 546 else
547 pid = ptid_get_pid (ptid); 547 pid = ptid_get_pid (ptid);
548 if (nbsd_is_vfork_done_pending (pid)) 548 if (nbsd_is_vfork_done_pending (pid))
549 return; 549 return;
550#endif 550#endif
551 551
552 if (debug_nbsd_lwp) 552 if (debug_nbsd_lwp)
553 fprintf_unfiltered (gdb_stdlog, 553 fprintf_unfiltered (gdb_stdlog,
554 "NLWP: nbsd_resume for ptid (%d, %ld, %ld)\n", 554 "NLWP: nbsd_resume for ptid (%d, %ld, %ld)\n",
555 ptid_get_pid (ptid), ptid_get_lwp (ptid), 555 ptid_get_pid (ptid), ptid_get_lwp (ptid),
556 ptid_get_tid (ptid)); 556 ptid_get_tid (ptid));
557 if (ptid_lwp_p (ptid)) 557 if (ptid_lwp_p (ptid))
558 { 558 {
559 /* If ptid is a specific LWP, suspend all other LWPs in the process. */ 559 /* FreeBSD: If ptid is a specific LWP, suspend all other LWPs in the
 560 * process.
 561 */
 562 /* NetBSD, this function is about resuming so we only deal with
 563 * the thread we've been asked to work with
 564 */
560 struct thread_info *tp; 565 struct thread_info *tp;
561 int request; 566 int request;
562 567
563 ALL_NON_EXITED_THREADS (tp) 568 ALL_NON_EXITED_THREADS (tp)
564 { 569 {
565 if (ptid_get_pid (tp->ptid) != ptid_get_pid (ptid)) 570 if (ptid_get_pid (tp->ptid) != ptid_get_pid (ptid))
566 continue; 571 continue;
567 572
568 if (ptid_get_lwp (tp->ptid) == ptid_get_lwp (ptid)) 573 if (ptid_get_lwp (tp->ptid) == ptid_get_lwp (ptid))
569 request = PT_RESUME; 574 request = PT_RESUME;
 575#ifndef __NetBSD__
570 else 576 else
571 request = PT_SUSPEND; 577 request = PT_SUSPEND;
 578#endif
572 579
573 if (ptrace (request, ptid_get_pid (tp->ptid), NULL, 580 if (ptrace (request, ptid_get_pid (tp->ptid), NULL,
574 ptid_get_lwp (tp->ptid)) == -1) 581 ptid_get_lwp (tp->ptid)) == -1)
575 perror_with_name (("ptrace")); 582 perror_with_name (("ptrace"));
576 } 583 }
577 } 584 }
578 else 585 else
579 { 586 {
580 /* If ptid is a wildcard, resume all matching threads (they won't run 587 /* If ptid is a wildcard, resume all matching threads (they won't run
581 until the process is continued however). */ 588 until the process is continued however). */
582 struct thread_info *tp; 589 struct thread_info *tp;
583 590
584 ALL_NON_EXITED_THREADS (tp) 591 ALL_NON_EXITED_THREADS (tp)
585 { 592 {
586 if (!ptid_match (tp->ptid, ptid)) 593 if (!ptid_match (tp->ptid, ptid))
587 continue; 594 continue;
588 595
589 if (ptrace (PT_RESUME, ptid_get_pid (tp->ptid), NULL, 596 if (ptrace (PT_RESUME, ptid_get_pid (tp->ptid), NULL,
590 ptid_get_lwp (tp->ptid)) == -1) 597 ptid_get_lwp (tp->ptid)) == -1)
591 perror_with_name (("ptrace")); 598 perror_with_name (("ptrace"));
592 } 599 }
593 ptid = inferior_ptid; 600 ptid = inferior_ptid;
594 } 601 }
595 super_resume (ops, ptid, step, signo); 602 super_resume (ops, ptid, step, signo);
596} 603}
597 604
598/* Wait for the child specified by PTID to do something. Return the 605/* Wait for the child specified by PTID to do something. Return the
599 process ID of the child, or MINUS_ONE_PTID in case of error; store 606 process ID of the child, or MINUS_ONE_PTID in case of error; store
600 the status in *OURSTATUS. */ 607 the status in *OURSTATUS. */
601 608
602static ptid_t 609static ptid_t
603nbsd_wait (struct target_ops *ops, 610nbsd_wait (struct target_ops *ops,
604 ptid_t ptid, struct target_waitstatus *ourstatus, 611 ptid_t ptid, struct target_waitstatus *ourstatus,
605 int target_options) 612 int target_options)
606{ 613{
607 ptid_t wptid; 614 ptid_t wptid;
608 615
609 while (1) 616 while (1)
610 { 617 {
611#ifndef PTRACE_VFORK 618#ifndef PTRACE_VFORK
612 wptid = nbsd_next_vfork_done (); 619 wptid = nbsd_next_vfork_done ();
613 if (!ptid_equal (wptid, null_ptid)) 620 if (!ptid_equal (wptid, null_ptid))
614 { 621 {
615 ourstatus->kind = TARGET_WAITKIND_VFORK_DONE; 622 ourstatus->kind = TARGET_WAITKIND_VFORK_DONE;
616 return wptid; 623 return wptid;
617 } 624 }
618#endif 625#endif
619 wptid = super_wait (ops, ptid, ourstatus, target_options); 626 wptid = super_wait (ops, ptid, ourstatus, target_options);
620 if (ourstatus->kind == TARGET_WAITKIND_STOPPED) 627 if (ourstatus->kind == TARGET_WAITKIND_STOPPED)
621 { 628 {
622 struct ptrace_lwpinfo pl; 629 struct ptrace_lwpinfo pl;
623 pid_t pid; 630 pid_t pid;
624 int status; 631 int status;
625 632
626 pid = ptid_get_pid (wptid); 633 pid = ptid_get_pid (wptid);
627 pl.pl_lwpid = 0; 634 pl.pl_lwpid = 0;
628 if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof pl) == -1) 635 if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof pl) == -1)
629 perror_with_name (("ptrace")); 636 perror_with_name (("ptrace"));
630 637
631 wptid = ptid_build (pid, pl.pl_lwpid, 0); 638 wptid = ptid_build (pid, pl.pl_lwpid, 0);
632 639
633#ifdef PT_LWP_EVENTS 640#ifdef PT_LWP_EVENTS
634 if (pl.pl_flags & PL_FLAG_EXITED) 641 if (pl.pl_flags & PL_FLAG_EXITED)
635 { 642 {
636 /* If GDB attaches to a multi-threaded process, exiting 643 /* If GDB attaches to a multi-threaded process, exiting
637 threads might be skipped during nbsd_post_attach that 644 threads might be skipped during nbsd_post_attach that
638 have not yet reported their PL_FLAG_EXITED event. 645 have not yet reported their PL_FLAG_EXITED event.
639 Ignore EXITED events for an unknown LWP. */ 646 Ignore EXITED events for an unknown LWP. */
640 if (in_thread_list (wptid)) 647 if (in_thread_list (wptid))
641 { 648 {
642 if (debug_nbsd_lwp) 649 if (debug_nbsd_lwp)
643 fprintf_unfiltered (gdb_stdlog, 650 fprintf_unfiltered (gdb_stdlog,
644 "NLWP: deleting thread for LWP %u\n", 651 "NLWP: deleting thread for LWP %u\n",
645 pl.pl_lwpid); 652 pl.pl_lwpid);
646 if (print_thread_events) 653 if (print_thread_events)
647 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str 654 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str
648 (wptid)); 655 (wptid));
649 delete_thread (wptid); 656 delete_thread (wptid);
650 } 657 }
651 if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1) 658 if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1)
652 perror_with_name (("ptrace")); 659 perror_with_name (("ptrace"));
653 continue; 660 continue;
654 } 661 }
655#endif 662#endif
656 663
657 /* Switch to an LWP PTID on the first stop in a new process. 664 /* Switch to an LWP PTID on the first stop in a new process.
658 This is done after handling PL_FLAG_EXITED to avoid 665 This is done after handling PL_FLAG_EXITED to avoid
659 switching to an exited LWP. It is done before checking 666 switching to an exited LWP. It is done before checking
660 PL_FLAG_BORN in case the first stop reported after 667 PL_FLAG_BORN in case the first stop reported after
661 attaching to an existing process is a PL_FLAG_BORN 668 attaching to an existing process is a PL_FLAG_BORN
662 event. */ 669 event. */
663 if (in_thread_list (pid_to_ptid (pid))) 670 if (in_thread_list (pid_to_ptid (pid)))
664 { 671 {
665 if (debug_nbsd_lwp) 672 if (debug_nbsd_lwp)
666 fprintf_unfiltered (gdb_stdlog, 673 fprintf_unfiltered (gdb_stdlog,
667 "NLWP: using LWP %u for first thread\n", 674 "NLWP: using LWP %u for first thread\n",
668 pl.pl_lwpid); 675 pl.pl_lwpid);
669 thread_change_ptid (pid_to_ptid (pid), wptid); 676 thread_change_ptid (pid_to_ptid (pid), wptid);
670 } 677 }
671 678
672#ifdef PT_LWP_EVENTS 679#ifdef PT_LWP_EVENTS
673 if (pl.pl_flags & PL_FLAG_BORN) 680 if (pl.pl_flags & PL_FLAG_BORN)
674 { 681 {
675 /* If GDB attaches to a multi-threaded process, newborn 682 /* If GDB attaches to a multi-threaded process, newborn
676 threads might be added by nbsd_add_threads that have 683 threads might be added by nbsd_add_threads that have
677 not yet reported their PL_FLAG_BORN event. Ignore 684 not yet reported their PL_FLAG_BORN event. Ignore
678 BORN events for an already-known LWP. */ 685 BORN events for an already-known LWP. */
679 if (!in_thread_list (wptid)) 686 if (!in_thread_list (wptid))
680 { 687 {
681 if (debug_nbsd_lwp) 688 if (debug_nbsd_lwp)
682 fprintf_unfiltered (gdb_stdlog, 689 fprintf_unfiltered (gdb_stdlog,
683 "NLWP: adding thread for LWP %u\n", 690 "NLWP: adding thread for LWP %u\n",
684 pl.pl_lwpid); 691 pl.pl_lwpid);
685 add_thread (wptid); 692 add_thread (wptid);
686 } 693 }
687 ourstatus->kind = TARGET_WAITKIND_SPURIOUS; 694 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
688 return wptid; 695 return wptid;
689 } 696 }
690#endif 697#endif
691 698
692#ifdef TDP_RFPPWAIT 699#ifdef TDP_RFPPWAIT
693 if (pl.pl_flags & PL_FLAG_FORKED) 700 if (pl.pl_flags & PL_FLAG_FORKED)
694 { 701 {
695#ifndef PTRACE_VFORK 702#ifndef PTRACE_VFORK
696 struct kinfo_proc kp; 703 struct kinfo_proc kp;
697#endif 704#endif
698 ptid_t child_ptid; 705 ptid_t child_ptid;
699 pid_t child; 706 pid_t child;
700 707
701 child = pl.pl_child_pid; 708 child = pl.pl_child_pid;
702 ourstatus->kind = TARGET_WAITKIND_FORKED; 709 ourstatus->kind = TARGET_WAITKIND_FORKED;
703#ifdef PTRACE_VFORK 710#ifdef PTRACE_VFORK
704 if (pl.pl_flags & PL_FLAG_VFORKED) 711 if (pl.pl_flags & PL_FLAG_VFORKED)
705 ourstatus->kind = TARGET_WAITKIND_VFORKED; 712 ourstatus->kind = TARGET_WAITKIND_VFORKED;
706#endif 713#endif
707 714
708 /* Make sure the other end of the fork is stopped too. */ 715 /* Make sure the other end of the fork is stopped too. */
709 child_ptid = nbsd_is_child_pending (child); 716 child_ptid = nbsd_is_child_pending (child);
710 if (ptid_equal (child_ptid, null_ptid)) 717 if (ptid_equal (child_ptid, null_ptid))
711 { 718 {
712 pid = waitpid (child, &status, 0); 719 pid = waitpid (child, &status, 0);
713 if (pid == -1) 720 if (pid == -1)
714 perror_with_name (("waitpid")); 721 perror_with_name (("waitpid"));
715 722
716 gdb_assert (pid == child); 723 gdb_assert (pid == child);
717 pl.pl_lwpid = 0; 724 pl.pl_lwpid = 0;
718 if (ptrace (PT_LWPINFO, child, (caddr_t)&pl, sizeof pl) == -1) 725 if (ptrace (PT_LWPINFO, child, (caddr_t)&pl, sizeof pl) == -1)
719 perror_with_name (("ptrace")); 726 perror_with_name (("ptrace"));
720 727
721 gdb_assert (pl.pl_flags & PL_FLAG_CHILD); 728 gdb_assert (pl.pl_flags & PL_FLAG_CHILD);
722 child_ptid = ptid_build (child, pl.pl_lwpid, 0); 729 child_ptid = ptid_build (child, pl.pl_lwpid, 0);
723 } 730 }
724 731
725 /* Enable additional events on the child process. */ 732 /* Enable additional events on the child process. */
726 nbsd_enable_proc_events (ptid_get_pid (child_ptid)); 733 nbsd_enable_proc_events (ptid_get_pid (child_ptid));
727 734
728#ifndef PTRACE_VFORK 735#ifndef PTRACE_VFORK
729 /* For vfork, the child process will have the P_PPWAIT 736 /* For vfork, the child process will have the P_PPWAIT
730 flag set. */ 737 flag set. */
731 nbsd_fetch_kinfo_proc (child, &kp); 738 nbsd_fetch_kinfo_proc (child, &kp);
732 if (kp.ki_flag & P_PPWAIT) 739 if (kp.ki_flag & P_PPWAIT)
733 ourstatus->kind = TARGET_WAITKIND_VFORKED; 740 ourstatus->kind = TARGET_WAITKIND_VFORKED;
734#endif 741#endif
735 ourstatus->value.related_pid = child_ptid; 742 ourstatus->value.related_pid = child_ptid;
736 743
737 return wptid; 744 return wptid;
738 } 745 }
739 746
740 if (pl.pl_flags & PL_FLAG_CHILD) 747 if (pl.pl_flags & PL_FLAG_CHILD)
741 { 748 {
742 /* Remember that this child forked, but do not report it 749 /* Remember that this child forked, but do not report it
743 until the parent reports its corresponding fork 750 until the parent reports its corresponding fork
744 event. */ 751 event. */
745 nbsd_remember_child (wptid); 752 nbsd_remember_child (wptid);
746 continue; 753 continue;
747 } 754 }
748 755
749#ifdef PTRACE_VFORK 756#ifdef PTRACE_VFORK
750 if (pl.pl_flags & PL_FLAG_VFORK_DONE) 757 if (pl.pl_flags & PL_FLAG_VFORK_DONE)
751 { 758 {
752 ourstatus->kind = TARGET_WAITKIND_VFORK_DONE; 759 ourstatus->kind = TARGET_WAITKIND_VFORK_DONE;
753 return wptid; 760 return wptid;
754 } 761 }
755#endif 762#endif
756#endif 763#endif
757 764
758#ifdef PL_FLAG_EXEC 765#ifdef PL_FLAG_EXEC
759 if (pl.pl_flags & PL_FLAG_EXEC) 766 if (pl.pl_flags & PL_FLAG_EXEC)
760 { 767 {
761 ourstatus->kind = TARGET_WAITKIND_EXECD; 768 ourstatus->kind = TARGET_WAITKIND_EXECD;
762 ourstatus->value.execd_pathname 769 ourstatus->value.execd_pathname
763 = xstrdup (nbsd_pid_to_exec_file (NULL, pid)); 770 = xstrdup (nbsd_pid_to_exec_file (NULL, pid));
764 return wptid; 771 return wptid;
765 } 772 }
766#endif 773#endif
767 774
768 /* Note that PL_FLAG_SCE is set for any event reported while 775 /* Note that PL_FLAG_SCE is set for any event reported while
769 a thread is executing a system call in the kernel. In 776 a thread is executing a system call in the kernel. In
770 particular, signals that interrupt a sleep in a system 777 particular, signals that interrupt a sleep in a system
771 call will report this flag as part of their event. Stops 778 call will report this flag as part of their event. Stops
772 explicitly for system call entry and exit always use 779 explicitly for system call entry and exit always use
773 SIGTRAP, so only treat SIGTRAP events as system call 780 SIGTRAP, so only treat SIGTRAP events as system call
774 entry/exit events. */ 781 entry/exit events. */
775#ifdef PL_FLAG_SCE 782#ifdef PL_FLAG_SCE
776 if (pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX) 783 if (pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX)
777 && ourstatus->value.sig == SIGTRAP) 784 && ourstatus->value.sig == SIGTRAP)
778 { 785 {
779#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE 786#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE
780 if (catch_syscall_enabled ()) 787 if (catch_syscall_enabled ())
781 { 788 {
782 if (catching_syscall_number (pl.pl_syscall_code)) 789 if (catching_syscall_number (pl.pl_syscall_code))
783 { 790 {
784 if (pl.pl_flags & PL_FLAG_SCE) 791 if (pl.pl_flags & PL_FLAG_SCE)
785 ourstatus->kind = TARGET_WAITKIND_SYSCALL_ENTRY; 792 ourstatus->kind = TARGET_WAITKIND_SYSCALL_ENTRY;
786 else 793 else
787 ourstatus->kind = TARGET_WAITKIND_SYSCALL_RETURN; 794 ourstatus->kind = TARGET_WAITKIND_SYSCALL_RETURN;
788 ourstatus->value.syscall_number = pl.pl_syscall_code; 795 ourstatus->value.syscall_number = pl.pl_syscall_code;
789 return wptid; 796 return wptid;
790 } 797 }
791 } 798 }
792#endif 799#endif
793 /* If the core isn't interested in this event, just 800 /* If the core isn't interested in this event, just
794 continue the process explicitly and wait for another 801 continue the process explicitly and wait for another
795 event. Note that PT_SYSCALL is "sticky" on FreeBSD 802 event. Note that PT_SYSCALL is "sticky" on FreeBSD
796 and once system call stops are enabled on a process 803 and once system call stops are enabled on a process
797 it stops for all system call entries and exits. */ 804 it stops for all system call entries and exits. */
798 if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1) 805 if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1)
799 perror_with_name (("ptrace")); 806 perror_with_name (("ptrace"));
800 continue; 807 continue;
801 } 808 }
802#endif 809#endif
803 } 810 }
804 return wptid; 811 return wptid;
805 } 812 }
806} 813}
807 814
808#ifdef TDP_RFPPWAIT 815#ifdef TDP_RFPPWAIT
809/* Target hook for follow_fork. On entry and at return inferior_ptid is 816/* Target hook for follow_fork. On entry and at return inferior_ptid is
810 the ptid of the followed inferior. */ 817 the ptid of the followed inferior. */
811 818
812static int 819static int
813nbsd_follow_fork (struct target_ops *ops, int follow_child, 820nbsd_follow_fork (struct target_ops *ops, int follow_child,
814 int detach_fork) 821 int detach_fork)
815{ 822{
816 if (!follow_child && detach_fork) 823 if (!follow_child && detach_fork)
817 { 824 {
818 struct thread_info *tp = inferior_thread (); 825 struct thread_info *tp = inferior_thread ();
819 pid_t child_pid = ptid_get_pid (tp->pending_follow.value.related_pid); 826 pid_t child_pid = ptid_get_pid (tp->pending_follow.value.related_pid);
820 827
821 /* Breakpoints have already been detached from the child by 828 /* Breakpoints have already been detached from the child by
822 infrun.c. */ 829 infrun.c. */
823 830
824 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1) 831 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
825 perror_with_name (("ptrace")); 832 perror_with_name (("ptrace"));
826 833
827#ifndef PTRACE_VFORK 834#ifndef PTRACE_VFORK
828 if (tp->pending_follow.kind == TARGET_WAITKIND_VFORKED) 835 if (tp->pending_follow.kind == TARGET_WAITKIND_VFORKED)
829 { 836 {
830 /* We can't insert breakpoints until the child process has 837 /* We can't insert breakpoints until the child process has
831 finished with the shared memory region. The parent 838 finished with the shared memory region. The parent
832 process doesn't wait for the child process to exit or 839 process doesn't wait for the child process to exit or
833 exec until after it has been resumed from the ptrace stop 840 exec until after it has been resumed from the ptrace stop
834 to report the fork. Once it has been resumed it doesn't 841 to report the fork. Once it has been resumed it doesn't
835 stop again before returning to userland, so there is no 842 stop again before returning to userland, so there is no
836 reliable way to wait on the parent. 843 reliable way to wait on the parent.
837 844
838 We can't stay attached to the child to wait for an exec 845 We can't stay attached to the child to wait for an exec
839 or exit because it may invoke ptrace(PT_TRACE_ME) 846 or exit because it may invoke ptrace(PT_TRACE_ME)
840 (e.g. if the parent process is a debugger forking a new 847 (e.g. if the parent process is a debugger forking a new
841 child process). 848 child process).
842 849
843 In the end, the best we can do is to make sure it runs 850 In the end, the best we can do is to make sure it runs
844 for a little while. Hopefully it will be out of range of 851 for a little while. Hopefully it will be out of range of
845 any breakpoints we reinsert. Usually this is only the 852 any breakpoints we reinsert. Usually this is only the
846 single-step breakpoint at vfork's return point. */ 853 single-step breakpoint at vfork's return point. */
847 854
848 usleep (10000); 855 usleep (10000);
849 856
850 /* Schedule a fake VFORK_DONE event to report on the next 857 /* Schedule a fake VFORK_DONE event to report on the next
851 wait. */ 858 wait. */
852 nbsd_add_vfork_done (inferior_ptid); 859 nbsd_add_vfork_done (inferior_ptid);
853 } 860 }
854#endif 861#endif
855 } 862 }
856 863
857 return 0; 864 return 0;
858} 865}
859 866
860static int 867static int
861nbsd_insert_fork_catchpoint (struct target_ops *self, int pid) 868nbsd_insert_fork_catchpoint (struct target_ops *self, int pid)
862{ 869{
863 return 0; 870 return 0;
864} 871}
865 872
866static int 873static int
867nbsd_remove_fork_catchpoint (struct target_ops *self, int pid) 874nbsd_remove_fork_catchpoint (struct target_ops *self, int pid)
868{ 875{
869 return 0; 876 return 0;
870} 877}
871 878
872static int 879static int
873nbsd_insert_vfork_catchpoint (struct target_ops *self, int pid) 880nbsd_insert_vfork_catchpoint (struct target_ops *self, int pid)
874{ 881{
875 return 0; 882 return 0;
876} 883}
877 884
878static int 885static int
879nbsd_remove_vfork_catchpoint (struct target_ops *self, int pid) 886nbsd_remove_vfork_catchpoint (struct target_ops *self, int pid)
880{ 887{
881 return 0; 888 return 0;
882} 889}
883#endif 890#endif
884 891
885/* Implement the "to_post_startup_inferior" target_ops method. */ 892/* Implement the "to_post_startup_inferior" target_ops method. */
886 893
887static void 894static void
888nbsd_post_startup_inferior (struct target_ops *self, ptid_t pid) 895nbsd_post_startup_inferior (struct target_ops *self, ptid_t pid)
889{ 896{
890 nbsd_enable_proc_events (ptid_get_pid (pid)); 897 nbsd_enable_proc_events (ptid_get_pid (pid));
891} 898}
892 899
893/* Implement the "to_post_attach" target_ops method. */ 900/* Implement the "to_post_attach" target_ops method. */
894 901
895static void 902static void
896nbsd_post_attach (struct target_ops *self, int pid) 903nbsd_post_attach (struct target_ops *self, int pid)
897{ 904{
898 nbsd_enable_proc_events (pid); 905 nbsd_enable_proc_events (pid);
899 nbsd_add_threads (pid); 906 nbsd_add_threads (pid);
900} 907}
901 908
902#ifdef PL_FLAG_EXEC 909#ifdef PL_FLAG_EXEC
903/* If the FreeBSD kernel supports PL_FLAG_EXEC, then traced processes 910/* If the FreeBSD kernel supports PL_FLAG_EXEC, then traced processes
904 will always stop after exec. */ 911 will always stop after exec. */
905 912
906static int 913static int
907nbsd_insert_exec_catchpoint (struct target_ops *self, int pid) 914nbsd_insert_exec_catchpoint (struct target_ops *self, int pid)
908{ 915{
909 return 0; 916 return 0;
910} 917}
911 918
912static int 919static int
913nbsd_remove_exec_catchpoint (struct target_ops *self, int pid) 920nbsd_remove_exec_catchpoint (struct target_ops *self, int pid)
914{ 921{
915 return 0; 922 return 0;
916} 923}
917#endif 924#endif
918 925
919#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE 926#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE
920static int 927static int
921nbsd_set_syscall_catchpoint (struct target_ops *self, int pid, int needed, 928nbsd_set_syscall_catchpoint (struct target_ops *self, int pid, int needed,
922 int any_count, int table_size, int *table) 929 int any_count, int table_size, int *table)
923{ 930{
924 931
925 /* Ignore the arguments. inf-ptrace.c will use PT_SYSCALL which 932 /* Ignore the arguments. inf-ptrace.c will use PT_SYSCALL which
926 will catch all system call entries and exits. The system calls 933 will catch all system call entries and exits. The system calls
927 are filtered by GDB rather than the kernel. */ 934 are filtered by GDB rather than the kernel. */
928 return 0; 935 return 0;
929} 936}
930#endif 937#endif
931#endif 938#endif
932 939
933void 940void
934nbsd_nat_add_target (struct target_ops *t) 941nbsd_nat_add_target (struct target_ops *t)
935{ 942{
936 t->to_pid_to_exec_file = nbsd_pid_to_exec_file; 943 t->to_pid_to_exec_file = nbsd_pid_to_exec_file;
937 t->to_find_memory_regions = nbsd_find_memory_regions; 944 t->to_find_memory_regions = nbsd_find_memory_regions;
938#ifdef KERN_PROC_AUXV 945#ifdef KERN_PROC_AUXV
939 super_xfer_partial = t->to_xfer_partial; 946 super_xfer_partial = t->to_xfer_partial;
940 t->to_xfer_partial = nbsd_xfer_partial; 947 t->to_xfer_partial = nbsd_xfer_partial;
941#endif 948#endif
942#ifdef PT_LWPINFO 949#ifdef PT_LWPINFO
943 t->to_thread_alive = nbsd_thread_alive; 950 t->to_thread_alive = nbsd_thread_alive;
944 t->to_pid_to_str = nbsd_pid_to_str; 951 t->to_pid_to_str = nbsd_pid_to_str;
945#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME 952#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME
946 t->to_thread_name = nbsd_thread_name; 953 t->to_thread_name = nbsd_thread_name;
947#endif 954#endif
948 t->to_update_thread_list = nbsd_update_thread_list; 955 t->to_update_thread_list = nbsd_update_thread_list;
949 t->to_has_thread_control = tc_schedlock; 956 t->to_has_thread_control = tc_schedlock;
950 super_resume = t->to_resume; 957 super_resume = t->to_resume;
951 t->to_resume = nbsd_resume; 958 t->to_resume = nbsd_resume;
952 super_wait = t->to_wait; 959 super_wait = t->to_wait;
953 t->to_wait = nbsd_wait; 960 t->to_wait = nbsd_wait;
954 t->to_post_startup_inferior = nbsd_post_startup_inferior; 961 t->to_post_startup_inferior = nbsd_post_startup_inferior;
955 t->to_post_attach = nbsd_post_attach; 962 t->to_post_attach = nbsd_post_attach;
956#ifdef TDP_RFPPWAIT 963#ifdef TDP_RFPPWAIT
957 t->to_follow_fork = nbsd_follow_fork; 964 t->to_follow_fork = nbsd_follow_fork;
958 t->to_insert_fork_catchpoint = nbsd_insert_fork_catchpoint; 965 t->to_insert_fork_catchpoint = nbsd_insert_fork_catchpoint;
959 t->to_remove_fork_catchpoint = nbsd_remove_fork_catchpoint; 966 t->to_remove_fork_catchpoint = nbsd_remove_fork_catchpoint;
960 t->to_insert_vfork_catchpoint = nbsd_insert_vfork_catchpoint; 967 t->to_insert_vfork_catchpoint = nbsd_insert_vfork_catchpoint;
961 t->to_remove_vfork_catchpoint = nbsd_remove_vfork_catchpoint; 968 t->to_remove_vfork_catchpoint = nbsd_remove_vfork_catchpoint;
962#endif 969#endif
963#ifdef PL_FLAG_EXEC 970#ifdef PL_FLAG_EXEC
964 t->to_insert_exec_catchpoint = nbsd_insert_exec_catchpoint; 971 t->to_insert_exec_catchpoint = nbsd_insert_exec_catchpoint;
965 t->to_remove_exec_catchpoint = nbsd_remove_exec_catchpoint; 972 t->to_remove_exec_catchpoint = nbsd_remove_exec_catchpoint;
966#endif 973#endif
967#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE 974#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE
968 t->to_set_syscall_catchpoint = nbsd_set_syscall_catchpoint; 975 t->to_set_syscall_catchpoint = nbsd_set_syscall_catchpoint;
969#endif 976#endif
970#endif 977#endif
971 add_target (t); 978 add_target (t);
972} 979}
973 980
974/* Provide a prototype to silence -Wmissing-prototypes. */ 981/* Provide a prototype to silence -Wmissing-prototypes. */
975extern initialize_file_ftype _initialize_nbsd_nat; 982extern initialize_file_ftype _initialize_nbsd_nat;
976 983
977void 984void
978_initialize_nbsd_nat (void) 985_initialize_nbsd_nat (void)
979{ 986{
980#ifdef PT_LWPINFO 987#ifdef PT_LWPINFO
981 add_setshow_boolean_cmd ("nbsd-lwp", class_maintenance, 988 add_setshow_boolean_cmd ("nbsd-lwp", class_maintenance,
982 &debug_nbsd_lwp, _("\ 989 &debug_nbsd_lwp, _("\
983Set debugging of NetBSD lwp module."), _("\ 990Set debugging of NetBSD lwp module."), _("\
984Show debugging of NetBSD lwp module."), _("\ 991Show debugging of NetBSD lwp module."), _("\
985Enables printf debugging output."), 992Enables printf debugging output."),
986 NULL, 993 NULL,
987 &show_nbsd_lwp_debug, 994 &show_nbsd_lwp_debug,
988 &setdebuglist, &showdebuglist); 995 &setdebuglist, &showdebuglist);
989#endif 996#endif
990} 997}