]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/fbsd-nat.c
Reword the string description of native FreeBSD ptids.
[thirdparty/binutils-gdb.git] / gdb / fbsd-nat.c
1 /* Native-dependent code for FreeBSD.
2
3 Copyright (C) 2002-2016 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 PT_LWPINFO
210 static int debug_fbsd_lwp;
211
212 static ptid_t (*super_wait) (struct target_ops *,
213 ptid_t,
214 struct target_waitstatus *,
215 int);
216
217 static void
218 show_fbsd_lwp_debug (struct ui_file *file, int from_tty,
219 struct cmd_list_element *c, const char *value)
220 {
221 fprintf_filtered (file, _("Debugging of FreeBSD lwp module is %s.\n"), value);
222 }
223
224 #if defined(TDP_RFPPWAIT) || defined(HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME)
225 /* Fetch the external variant of the kernel's internal process
226 structure for the process PID into KP. */
227
228 static void
229 fbsd_fetch_kinfo_proc (pid_t pid, struct kinfo_proc *kp)
230 {
231 size_t len;
232 int mib[4];
233
234 len = sizeof *kp;
235 mib[0] = CTL_KERN;
236 mib[1] = KERN_PROC;
237 mib[2] = KERN_PROC_PID;
238 mib[3] = pid;
239 if (sysctl (mib, 4, kp, &len, NULL, 0) == -1)
240 perror_with_name (("sysctl"));
241 }
242 #endif
243
244 /*
245 FreeBSD's first thread support was via a "reentrant" version of libc
246 (libc_r) that first shipped in 2.2.7. This library multiplexed all
247 of the threads in a process onto a single kernel thread. This
248 library is supported via the bsd-uthread target.
249
250 FreeBSD 5.1 introduced two new threading libraries that made use of
251 multiple kernel threads. The first (libkse) scheduled M user
252 threads onto N (<= M) kernel threads (LWPs). The second (libthr)
253 bound each user thread to a dedicated kernel thread. libkse shipped
254 as the default threading library (libpthread).
255
256 FreeBSD 5.3 added a libthread_db to abstract the interface across
257 the various thread libraries (libc_r, libkse, and libthr).
258
259 FreeBSD 7.0 switched the default threading library from from libkse
260 to libpthread and removed libc_r.
261
262 FreeBSD 8.0 removed libkse and the in-kernel support for it. The
263 only threading library supported by 8.0 and later is libthr which
264 ties each user thread directly to an LWP. To simplify the
265 implementation, this target only supports LWP-backed threads using
266 ptrace directly rather than libthread_db.
267
268 FreeBSD 11.0 introduced LWP event reporting via PT_LWP_EVENTS.
269 */
270
271 /* Return true if PTID is still active in the inferior. */
272
273 static int
274 fbsd_thread_alive (struct target_ops *ops, ptid_t ptid)
275 {
276 if (ptid_lwp_p (ptid))
277 {
278 struct ptrace_lwpinfo pl;
279
280 if (ptrace (PT_LWPINFO, ptid_get_lwp (ptid), (caddr_t) &pl, sizeof pl)
281 == -1)
282 return 0;
283 #ifdef PL_FLAG_EXITED
284 if (pl.pl_flags & PL_FLAG_EXITED)
285 return 0;
286 #endif
287 }
288
289 return 1;
290 }
291
292 /* Convert PTID to a string. Returns the string in a static
293 buffer. */
294
295 static char *
296 fbsd_pid_to_str (struct target_ops *ops, ptid_t ptid)
297 {
298 lwpid_t lwp;
299
300 lwp = ptid_get_lwp (ptid);
301 if (lwp != 0)
302 {
303 static char buf[64];
304 int pid = ptid_get_pid (ptid);
305
306 xsnprintf (buf, sizeof buf, "LWP %d of process %d", lwp, pid);
307 return buf;
308 }
309
310 return normal_pid_to_str (ptid);
311 }
312
313 #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME
314 /* Return the name assigned to a thread by an application. Returns
315 the string in a static buffer. */
316
317 static const char *
318 fbsd_thread_name (struct target_ops *self, struct thread_info *thr)
319 {
320 struct ptrace_lwpinfo pl;
321 struct kinfo_proc kp;
322 int pid = ptid_get_pid (thr->ptid);
323 long lwp = ptid_get_lwp (thr->ptid);
324 static char buf[sizeof pl.pl_tdname + 1];
325
326 /* Note that ptrace_lwpinfo returns the process command in pl_tdname
327 if a name has not been set explicitly. Return a NULL name in
328 that case. */
329 fbsd_fetch_kinfo_proc (pid, &kp);
330 if (ptrace (PT_LWPINFO, lwp, (caddr_t) &pl, sizeof pl) == -1)
331 perror_with_name (("ptrace"));
332 if (strcmp (kp.ki_comm, pl.pl_tdname) == 0)
333 return NULL;
334 xsnprintf (buf, sizeof buf, "%s", pl.pl_tdname);
335 return buf;
336 }
337 #endif
338
339 #ifdef PT_LWP_EVENTS
340 /* Enable LWP events for a specific process.
341
342 To catch LWP events, PT_LWP_EVENTS is set on every traced process.
343 This enables stops on the birth for new LWPs (excluding the "main" LWP)
344 and the death of LWPs (excluding the last LWP in a process). Note
345 that unlike fork events, the LWP that creates a new LWP does not
346 report an event. */
347
348 static void
349 fbsd_enable_lwp_events (pid_t pid)
350 {
351 if (ptrace (PT_LWP_EVENTS, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
352 perror_with_name (("ptrace"));
353 }
354 #endif
355
356 /* Add threads for any new LWPs in a process.
357
358 When LWP events are used, this function is only used to detect existing
359 threads when attaching to a process. On older systems, this function is
360 called to discover new threads each time the thread list is updated. */
361
362 static void
363 fbsd_add_threads (pid_t pid)
364 {
365 struct cleanup *cleanup;
366 lwpid_t *lwps;
367 int i, nlwps;
368
369 gdb_assert (!in_thread_list (pid_to_ptid (pid)));
370 nlwps = ptrace (PT_GETNUMLWPS, pid, NULL, 0);
371 if (nlwps == -1)
372 perror_with_name (("ptrace"));
373
374 lwps = XCNEWVEC (lwpid_t, nlwps);
375 cleanup = make_cleanup (xfree, lwps);
376
377 nlwps = ptrace (PT_GETLWPLIST, pid, (caddr_t) lwps, nlwps);
378 if (nlwps == -1)
379 perror_with_name (("ptrace"));
380
381 for (i = 0; i < nlwps; i++)
382 {
383 ptid_t ptid = ptid_build (pid, lwps[i], 0);
384
385 if (!in_thread_list (ptid))
386 {
387 #ifdef PT_LWP_EVENTS
388 struct ptrace_lwpinfo pl;
389
390 /* Don't add exited threads. Note that this is only called
391 when attaching to a multi-threaded process. */
392 if (ptrace (PT_LWPINFO, lwps[i], (caddr_t) &pl, sizeof pl) == -1)
393 perror_with_name (("ptrace"));
394 if (pl.pl_flags & PL_FLAG_EXITED)
395 continue;
396 #endif
397 if (debug_fbsd_lwp)
398 fprintf_unfiltered (gdb_stdlog,
399 "FLWP: adding thread for LWP %u\n",
400 lwps[i]);
401 add_thread (ptid);
402 }
403 }
404 do_cleanups (cleanup);
405 }
406
407 /* Implement the "to_update_thread_list" target_ops method. */
408
409 static void
410 fbsd_update_thread_list (struct target_ops *ops)
411 {
412 #ifdef PT_LWP_EVENTS
413 /* With support for thread events, threads are added/deleted from the
414 list as events are reported, so just try deleting exited threads. */
415 delete_exited_threads ();
416 #else
417 prune_threads ();
418
419 fbsd_add_threads (ptid_get_pid (inferior_ptid));
420 #endif
421 }
422
423 static void (*super_resume) (struct target_ops *,
424 ptid_t,
425 int,
426 enum gdb_signal);
427
428 static int
429 resume_one_thread_cb (struct thread_info *tp, void *data)
430 {
431 ptid_t *ptid = data;
432 int request;
433
434 if (ptid_get_pid (tp->ptid) != ptid_get_pid (*ptid))
435 return 0;
436
437 if (ptid_get_lwp (tp->ptid) == ptid_get_lwp (*ptid))
438 request = PT_RESUME;
439 else
440 request = PT_SUSPEND;
441
442 if (ptrace (request, ptid_get_lwp (tp->ptid), NULL, 0) == -1)
443 perror_with_name (("ptrace"));
444 return 0;
445 }
446
447 static int
448 resume_all_threads_cb (struct thread_info *tp, void *data)
449 {
450 ptid_t *filter = data;
451
452 if (!ptid_match (tp->ptid, *filter))
453 return 0;
454
455 if (ptrace (PT_RESUME, ptid_get_lwp (tp->ptid), NULL, 0) == -1)
456 perror_with_name (("ptrace"));
457 return 0;
458 }
459
460 /* Implement the "to_resume" target_ops method. */
461
462 static void
463 fbsd_resume (struct target_ops *ops,
464 ptid_t ptid, int step, enum gdb_signal signo)
465 {
466
467 if (debug_fbsd_lwp)
468 fprintf_unfiltered (gdb_stdlog,
469 "FLWP: fbsd_resume for ptid (%d, %ld, %ld)\n",
470 ptid_get_pid (ptid), ptid_get_lwp (ptid),
471 ptid_get_tid (ptid));
472 if (ptid_lwp_p (ptid))
473 {
474 /* If ptid is a specific LWP, suspend all other LWPs in the process. */
475 iterate_over_threads (resume_one_thread_cb, &ptid);
476 }
477 else
478 {
479 /* If ptid is a wildcard, resume all matching threads (they won't run
480 until the process is continued however). */
481 iterate_over_threads (resume_all_threads_cb, &ptid);
482 ptid = inferior_ptid;
483 }
484 super_resume (ops, ptid, step, signo);
485 }
486
487 #ifdef TDP_RFPPWAIT
488 /*
489 To catch fork events, PT_FOLLOW_FORK is set on every traced process
490 to enable stops on returns from fork or vfork. Note that both the
491 parent and child will always stop, even if system call stops are not
492 enabled.
493
494 After a fork, both the child and parent process will stop and report
495 an event. However, there is no guarantee of order. If the parent
496 reports its stop first, then fbsd_wait explicitly waits for the new
497 child before returning. If the child reports its stop first, then
498 the event is saved on a list and ignored until the parent's stop is
499 reported. fbsd_wait could have been changed to fetch the parent PID
500 of the new child and used that to wait for the parent explicitly.
501 However, if two threads in the parent fork at the same time, then
502 the wait on the parent might return the "wrong" fork event.
503
504 The initial version of PT_FOLLOW_FORK did not set PL_FLAG_CHILD for
505 the new child process. This flag could be inferred by treating any
506 events for an unknown pid as a new child.
507
508 In addition, the initial version of PT_FOLLOW_FORK did not report a
509 stop event for the parent process of a vfork until after the child
510 process executed a new program or exited. The kernel was changed to
511 defer the wait for exit or exec of the child until after posting the
512 stop event shortly after the change to introduce PL_FLAG_CHILD.
513 This could be worked around by reporting a vfork event when the
514 child event posted and ignoring the subsequent event from the
515 parent.
516
517 This implementation requires both of these fixes for simplicity's
518 sake. FreeBSD versions newer than 9.1 contain both fixes.
519 */
520
521 struct fbsd_fork_child_info
522 {
523 struct fbsd_fork_child_info *next;
524 ptid_t child; /* Pid of new child. */
525 };
526
527 static struct fbsd_fork_child_info *fbsd_pending_children;
528
529 /* Record a new child process event that is reported before the
530 corresponding fork event in the parent. */
531
532 static void
533 fbsd_remember_child (ptid_t pid)
534 {
535 struct fbsd_fork_child_info *info = XCNEW (struct fbsd_fork_child_info);
536
537 info->child = pid;
538 info->next = fbsd_pending_children;
539 fbsd_pending_children = info;
540 }
541
542 /* Check for a previously-recorded new child process event for PID.
543 If one is found, remove it from the list and return the PTID. */
544
545 static ptid_t
546 fbsd_is_child_pending (pid_t pid)
547 {
548 struct fbsd_fork_child_info *info, *prev;
549 ptid_t ptid;
550
551 prev = NULL;
552 for (info = fbsd_pending_children; info; prev = info, info = info->next)
553 {
554 if (ptid_get_pid (info->child) == pid)
555 {
556 if (prev == NULL)
557 fbsd_pending_children = info->next;
558 else
559 prev->next = info->next;
560 ptid = info->child;
561 xfree (info);
562 return ptid;
563 }
564 }
565 return null_ptid;
566 }
567 #endif
568
569 /* Wait for the child specified by PTID to do something. Return the
570 process ID of the child, or MINUS_ONE_PTID in case of error; store
571 the status in *OURSTATUS. */
572
573 static ptid_t
574 fbsd_wait (struct target_ops *ops,
575 ptid_t ptid, struct target_waitstatus *ourstatus,
576 int target_options)
577 {
578 ptid_t wptid;
579
580 while (1)
581 {
582 wptid = super_wait (ops, ptid, ourstatus, target_options);
583 if (ourstatus->kind == TARGET_WAITKIND_STOPPED)
584 {
585 struct ptrace_lwpinfo pl;
586 pid_t pid;
587 int status;
588
589 pid = ptid_get_pid (wptid);
590 if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof pl) == -1)
591 perror_with_name (("ptrace"));
592
593 wptid = ptid_build (pid, pl.pl_lwpid, 0);
594
595 #ifdef PT_LWP_EVENTS
596 if (pl.pl_flags & PL_FLAG_EXITED)
597 {
598 /* If GDB attaches to a multi-threaded process, exiting
599 threads might be skipped during fbsd_post_attach that
600 have not yet reported their PL_FLAG_EXITED event.
601 Ignore EXITED events for an unknown LWP. */
602 if (in_thread_list (wptid))
603 {
604 if (debug_fbsd_lwp)
605 fprintf_unfiltered (gdb_stdlog,
606 "FLWP: deleting thread for LWP %u\n",
607 pl.pl_lwpid);
608 if (print_thread_events)
609 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str
610 (wptid));
611 delete_thread (wptid);
612 }
613 if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1)
614 perror_with_name (("ptrace"));
615 continue;
616 }
617 #endif
618
619 /* Switch to an LWP PTID on the first stop in a new process.
620 This is done after handling PL_FLAG_EXITED to avoid
621 switching to an exited LWP. It is done before checking
622 PL_FLAG_BORN in case the first stop reported after
623 attaching to an existing process is a PL_FLAG_BORN
624 event. */
625 if (in_thread_list (pid_to_ptid (pid)))
626 {
627 if (debug_fbsd_lwp)
628 fprintf_unfiltered (gdb_stdlog,
629 "FLWP: using LWP %u for first thread\n",
630 pl.pl_lwpid);
631 thread_change_ptid (pid_to_ptid (pid), wptid);
632 }
633
634 #ifdef PT_LWP_EVENTS
635 if (pl.pl_flags & PL_FLAG_BORN)
636 {
637 /* If GDB attaches to a multi-threaded process, newborn
638 threads might be added by fbsd_add_threads that have
639 not yet reported their PL_FLAG_BORN event. Ignore
640 BORN events for an already-known LWP. */
641 if (!in_thread_list (wptid))
642 {
643 if (debug_fbsd_lwp)
644 fprintf_unfiltered (gdb_stdlog,
645 "FLWP: adding thread for LWP %u\n",
646 pl.pl_lwpid);
647 add_thread (wptid);
648 }
649 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
650 return wptid;
651 }
652 #endif
653
654 #ifdef TDP_RFPPWAIT
655 if (pl.pl_flags & PL_FLAG_FORKED)
656 {
657 struct kinfo_proc kp;
658 ptid_t child_ptid;
659 pid_t child;
660
661 child = pl.pl_child_pid;
662 ourstatus->kind = TARGET_WAITKIND_FORKED;
663
664 /* Make sure the other end of the fork is stopped too. */
665 child_ptid = fbsd_is_child_pending (child);
666 if (ptid_equal (child_ptid, null_ptid))
667 {
668 pid = waitpid (child, &status, 0);
669 if (pid == -1)
670 perror_with_name (("waitpid"));
671
672 gdb_assert (pid == child);
673
674 if (ptrace (PT_LWPINFO, child, (caddr_t)&pl, sizeof pl) == -1)
675 perror_with_name (("ptrace"));
676
677 gdb_assert (pl.pl_flags & PL_FLAG_CHILD);
678 child_ptid = ptid_build (child, pl.pl_lwpid, 0);
679 }
680
681 /* For vfork, the child process will have the P_PPWAIT
682 flag set. */
683 fbsd_fetch_kinfo_proc (child, &kp);
684 if (kp.ki_flag & P_PPWAIT)
685 ourstatus->kind = TARGET_WAITKIND_VFORKED;
686 ourstatus->value.related_pid = child_ptid;
687
688 return wptid;
689 }
690
691 if (pl.pl_flags & PL_FLAG_CHILD)
692 {
693 /* Remember that this child forked, but do not report it
694 until the parent reports its corresponding fork
695 event. */
696 fbsd_remember_child (wptid);
697 continue;
698 }
699 #endif
700
701 #ifdef PL_FLAG_EXEC
702 if (pl.pl_flags & PL_FLAG_EXEC)
703 {
704 ourstatus->kind = TARGET_WAITKIND_EXECD;
705 ourstatus->value.execd_pathname
706 = xstrdup (fbsd_pid_to_exec_file (NULL, pid));
707 return wptid;
708 }
709 #endif
710 }
711 return wptid;
712 }
713 }
714
715 #ifdef TDP_RFPPWAIT
716 /* Target hook for follow_fork. On entry and at return inferior_ptid is
717 the ptid of the followed inferior. */
718
719 static int
720 fbsd_follow_fork (struct target_ops *ops, int follow_child,
721 int detach_fork)
722 {
723 if (!follow_child)
724 {
725 struct thread_info *tp = inferior_thread ();
726 pid_t child_pid = ptid_get_pid (tp->pending_follow.value.related_pid);
727
728 /* Breakpoints have already been detached from the child by
729 infrun.c. */
730
731 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
732 perror_with_name (("ptrace"));
733 }
734
735 return 0;
736 }
737
738 static int
739 fbsd_insert_fork_catchpoint (struct target_ops *self, int pid)
740 {
741 return 0;
742 }
743
744 static int
745 fbsd_remove_fork_catchpoint (struct target_ops *self, int pid)
746 {
747 return 0;
748 }
749
750 static int
751 fbsd_insert_vfork_catchpoint (struct target_ops *self, int pid)
752 {
753 return 0;
754 }
755
756 static int
757 fbsd_remove_vfork_catchpoint (struct target_ops *self, int pid)
758 {
759 return 0;
760 }
761
762 /* Enable fork tracing for a specific process.
763
764 To catch fork events, PT_FOLLOW_FORK is set on every traced process
765 to enable stops on returns from fork or vfork. Note that both the
766 parent and child will always stop, even if system call stops are
767 not enabled. */
768
769 static void
770 fbsd_enable_follow_fork (pid_t pid)
771 {
772 if (ptrace (PT_FOLLOW_FORK, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
773 perror_with_name (("ptrace"));
774 }
775 #endif
776
777 /* Implement the "to_post_startup_inferior" target_ops method. */
778
779 static void
780 fbsd_post_startup_inferior (struct target_ops *self, ptid_t pid)
781 {
782 #ifdef TDP_RFPPWAIT
783 fbsd_enable_follow_fork (ptid_get_pid (pid));
784 #endif
785 #ifdef PT_LWP_EVENTS
786 fbsd_enable_lwp_events (ptid_get_pid (pid));
787 #endif
788 }
789
790 /* Implement the "to_post_attach" target_ops method. */
791
792 static void
793 fbsd_post_attach (struct target_ops *self, int pid)
794 {
795 #ifdef TDP_RFPPWAIT
796 fbsd_enable_follow_fork (pid);
797 #endif
798 #ifdef PT_LWP_EVENTS
799 fbsd_enable_lwp_events (pid);
800 #endif
801 fbsd_add_threads (pid);
802 }
803
804 #ifdef PL_FLAG_EXEC
805 /* If the FreeBSD kernel supports PL_FLAG_EXEC, then traced processes
806 will always stop after exec. */
807
808 static int
809 fbsd_insert_exec_catchpoint (struct target_ops *self, int pid)
810 {
811 return 0;
812 }
813
814 static int
815 fbsd_remove_exec_catchpoint (struct target_ops *self, int pid)
816 {
817 return 0;
818 }
819 #endif
820 #endif
821
822 void
823 fbsd_nat_add_target (struct target_ops *t)
824 {
825 t->to_pid_to_exec_file = fbsd_pid_to_exec_file;
826 t->to_find_memory_regions = fbsd_find_memory_regions;
827 #ifdef PT_LWPINFO
828 t->to_thread_alive = fbsd_thread_alive;
829 t->to_pid_to_str = fbsd_pid_to_str;
830 #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME
831 t->to_thread_name = fbsd_thread_name;
832 #endif
833 t->to_update_thread_list = fbsd_update_thread_list;
834 t->to_has_thread_control = tc_schedlock;
835 super_resume = t->to_resume;
836 t->to_resume = fbsd_resume;
837 super_wait = t->to_wait;
838 t->to_wait = fbsd_wait;
839 t->to_post_startup_inferior = fbsd_post_startup_inferior;
840 t->to_post_attach = fbsd_post_attach;
841 #ifdef TDP_RFPPWAIT
842 t->to_follow_fork = fbsd_follow_fork;
843 t->to_insert_fork_catchpoint = fbsd_insert_fork_catchpoint;
844 t->to_remove_fork_catchpoint = fbsd_remove_fork_catchpoint;
845 t->to_insert_vfork_catchpoint = fbsd_insert_vfork_catchpoint;
846 t->to_remove_vfork_catchpoint = fbsd_remove_vfork_catchpoint;
847 #endif
848 #ifdef PL_FLAG_EXEC
849 t->to_insert_exec_catchpoint = fbsd_insert_exec_catchpoint;
850 t->to_remove_exec_catchpoint = fbsd_remove_exec_catchpoint;
851 #endif
852 #endif
853 add_target (t);
854 }
855
856 /* Provide a prototype to silence -Wmissing-prototypes. */
857 extern initialize_file_ftype _initialize_fbsd_nat;
858
859 void
860 _initialize_fbsd_nat (void)
861 {
862 #ifdef PT_LWPINFO
863 add_setshow_boolean_cmd ("fbsd-lwp", class_maintenance,
864 &debug_fbsd_lwp, _("\
865 Set debugging of FreeBSD lwp module."), _("\
866 Show debugging of FreeBSD lwp module."), _("\
867 Enables printf debugging output."),
868 NULL,
869 &show_fbsd_lwp_debug,
870 &setdebuglist, &showdebuglist);
871 #endif
872 }