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