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