Add support for LWP-based threads on FreeBSD.
[deliverable/binutils-gdb.git] / gdb / fbsd-nat.c
CommitLineData
578c1c03
MK
1/* Native-dependent code for FreeBSD.
2
618f726f 3 Copyright (C) 2002-2016 Free Software Foundation, Inc.
578c1c03
MK
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
578c1c03
MK
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
578c1c03
MK
19
20#include "defs.h"
21#include "gdbcore.h"
22#include "inferior.h"
23#include "regcache.h"
24#include "regset.h"
6e9567fe 25#include "gdbcmd.h"
2020b7ab 26#include "gdbthread.h"
cea6e4f1 27#include "gdb_wait.h"
578c1c03 28#include <sys/types.h>
68b9939a 29#include <sys/procfs.h>
e58e05d6 30#include <sys/ptrace.h>
68b9939a 31#include <sys/sysctl.h>
25268153
JB
32#ifdef HAVE_KINFO_GETVMMAP
33#include <sys/user.h>
34#include <libutil.h>
35#endif
578c1c03
MK
36
37#include "elf-bfd.h"
38#include "fbsd-nat.h"
39
766062f6 40/* Return the name of a file that can be opened to get the symbols for
578c1c03
MK
41 the child process identified by PID. */
42
8f60fe01 43static char *
8dd27370 44fbsd_pid_to_exec_file (struct target_ops *self, int pid)
578c1c03 45{
b4ab256d
HZ
46 ssize_t len = PATH_MAX;
47 static char buf[PATH_MAX];
48 char name[PATH_MAX];
578c1c03 49
68b9939a
MK
50#ifdef KERN_PROC_PATHNAME
51 int mib[4];
578c1c03 52
68b9939a
MK
53 mib[0] = CTL_KERN;
54 mib[1] = KERN_PROC;
55 mib[2] = KERN_PROC_PATHNAME;
56 mib[3] = pid;
57 if (sysctl (mib, 4, buf, &len, NULL, 0) == 0)
578c1c03 58 return buf;
68b9939a 59#endif
578c1c03 60
b4ab256d
HZ
61 xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
62 len = readlink (name, buf, PATH_MAX - 1);
63 if (len != -1)
68b9939a 64 {
b4ab256d
HZ
65 buf[len] = '\0';
66 return buf;
68b9939a
MK
67 }
68
b4ab256d 69 return NULL;
578c1c03
MK
70}
71
25268153
JB
72#ifdef HAVE_KINFO_GETVMMAP
73/* Iterate over all the memory regions in the current inferior,
74 calling FUNC for each memory region. OBFD is passed as the last
75 argument to FUNC. */
76
8f60fe01 77static int
25268153
JB
78fbsd_find_memory_regions (struct target_ops *self,
79 find_memory_region_ftype func, void *obfd)
80{
81 pid_t pid = ptid_get_pid (inferior_ptid);
82 struct kinfo_vmentry *vmentl, *kve;
83 uint64_t size;
84 struct cleanup *cleanup;
85 int i, nitems;
86
87 vmentl = kinfo_getvmmap (pid, &nitems);
88 if (vmentl == NULL)
89 perror_with_name (_("Couldn't fetch VM map entries."));
90 cleanup = make_cleanup (free, vmentl);
91
92 for (i = 0; i < nitems; i++)
93 {
94 kve = &vmentl[i];
95
96 /* Skip unreadable segments and those where MAP_NOCORE has been set. */
97 if (!(kve->kve_protection & KVME_PROT_READ)
98 || kve->kve_flags & KVME_FLAG_NOCOREDUMP)
99 continue;
100
101 /* Skip segments with an invalid type. */
102 if (kve->kve_type != KVME_TYPE_DEFAULT
103 && kve->kve_type != KVME_TYPE_VNODE
104 && kve->kve_type != KVME_TYPE_SWAP
105 && kve->kve_type != KVME_TYPE_PHYS)
106 continue;
107
108 size = kve->kve_end - kve->kve_start;
109 if (info_verbose)
110 {
111 fprintf_filtered (gdb_stdout,
112 "Save segment, %ld bytes at %s (%c%c%c)\n",
113 (long) size,
114 paddress (target_gdbarch (), kve->kve_start),
115 kve->kve_protection & KVME_PROT_READ ? 'r' : '-',
116 kve->kve_protection & KVME_PROT_WRITE ? 'w' : '-',
117 kve->kve_protection & KVME_PROT_EXEC ? 'x' : '-');
118 }
119
120 /* Invoke the callback function to create the corefile segment.
121 Pass MODIFIED as true, we do not know the real modification state. */
122 func (kve->kve_start, size, kve->kve_protection & KVME_PROT_READ,
123 kve->kve_protection & KVME_PROT_WRITE,
124 kve->kve_protection & KVME_PROT_EXEC, 1, obfd);
125 }
126 do_cleanups (cleanup);
127 return 0;
128}
129#else
578c1c03
MK
130static int
131fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end,
132 char *protection)
133{
134 /* FreeBSD 5.1-RELEASE uses a 256-byte buffer. */
135 char buf[256];
136 int resident, privateresident;
137 unsigned long obj;
138 int ret = EOF;
139
140 /* As of FreeBSD 5.0-RELEASE, the layout is described in
141 /usr/src/sys/fs/procfs/procfs_map.c. Somewhere in 5.1-CURRENT a
142 new column was added to the procfs map. Therefore we can't use
143 fscanf since we need to support older releases too. */
144 if (fgets (buf, sizeof buf, mapfile) != NULL)
145 ret = sscanf (buf, "%lx %lx %d %d %lx %s", start, end,
146 &resident, &privateresident, &obj, protection);
147
148 return (ret != 0 && ret != EOF);
149}
150
151/* Iterate over all the memory regions in the current inferior,
152 calling FUNC for each memory region. OBFD is passed as the last
153 argument to FUNC. */
154
8f60fe01 155static int
2e73927c
TT
156fbsd_find_memory_regions (struct target_ops *self,
157 find_memory_region_ftype func, void *obfd)
578c1c03
MK
158{
159 pid_t pid = ptid_get_pid (inferior_ptid);
160 char *mapfilename;
161 FILE *mapfile;
162 unsigned long start, end, size;
163 char protection[4];
164 int read, write, exec;
7c8a8b04 165 struct cleanup *cleanup;
578c1c03
MK
166
167 mapfilename = xstrprintf ("/proc/%ld/map", (long) pid);
7c8a8b04 168 cleanup = make_cleanup (xfree, mapfilename);
578c1c03
MK
169 mapfile = fopen (mapfilename, "r");
170 if (mapfile == NULL)
8a3fe4f8 171 error (_("Couldn't open %s."), mapfilename);
7c8a8b04 172 make_cleanup_fclose (mapfile);
578c1c03
MK
173
174 if (info_verbose)
175 fprintf_filtered (gdb_stdout,
176 "Reading memory regions from %s\n", mapfilename);
177
178 /* Now iterate until end-of-file. */
179 while (fbsd_read_mapping (mapfile, &start, &end, &protection[0]))
180 {
181 size = end - start;
182
183 read = (strchr (protection, 'r') != 0);
184 write = (strchr (protection, 'w') != 0);
185 exec = (strchr (protection, 'x') != 0);
186
187 if (info_verbose)
188 {
189 fprintf_filtered (gdb_stdout,
5af949e3 190 "Save segment, %ld bytes at %s (%c%c%c)\n",
f5656ead 191 size, paddress (target_gdbarch (), start),
578c1c03
MK
192 read ? 'r' : '-',
193 write ? 'w' : '-',
194 exec ? 'x' : '-');
195 }
196
4f69f4c2
JK
197 /* Invoke the callback function to create the corefile segment.
198 Pass MODIFIED as true, we do not know the real modification state. */
199 func (start, size, read, write, exec, 1, obfd);
578c1c03
MK
200 }
201
7c8a8b04 202 do_cleanups (cleanup);
578c1c03
MK
203 return 0;
204}
25268153 205#endif
8f60fe01 206
e58e05d6 207#ifdef PT_LWPINFO
6e9567fe
JB
208static int debug_fbsd_lwp;
209
e58e05d6
JB
210static ptid_t (*super_wait) (struct target_ops *,
211 ptid_t,
212 struct target_waitstatus *,
213 int);
214
6e9567fe
JB
215static void
216show_fbsd_lwp_debug (struct ui_file *file, int from_tty,
217 struct cmd_list_element *c, const char *value)
218{
219 fprintf_filtered (file, _("Debugging of FreeBSD lwp module is %s.\n"), value);
220}
221
222#if defined(TDP_RFPPWAIT) || defined(HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME)
223/* Fetch the external variant of the kernel's internal process
224 structure for the process PID into KP. */
225
226static void
227fbsd_fetch_kinfo_proc (pid_t pid, struct kinfo_proc *kp)
228{
229 size_t len;
230 int mib[4];
231
232 len = sizeof *kp;
233 mib[0] = CTL_KERN;
234 mib[1] = KERN_PROC;
235 mib[2] = KERN_PROC_PID;
236 mib[3] = pid;
237 if (sysctl (mib, 4, kp, &len, NULL, 0) == -1)
238 perror_with_name (("sysctl"));
239}
240#endif
241
242/*
243 FreeBSD's first thread support was via a "reentrant" version of libc
244 (libc_r) that first shipped in 2.2.7. This library multiplexed all
245 of the threads in a process onto a single kernel thread. This
246 library is supported via the bsd-uthread target.
247
248 FreeBSD 5.1 introduced two new threading libraries that made use of
249 multiple kernel threads. The first (libkse) scheduled M user
250 threads onto N (<= M) kernel threads (LWPs). The second (libthr)
251 bound each user thread to a dedicated kernel thread. libkse shipped
252 as the default threading library (libpthread).
253
254 FreeBSD 5.3 added a libthread_db to abstract the interface across
255 the various thread libraries (libc_r, libkse, and libthr).
256
257 FreeBSD 7.0 switched the default threading library from from libkse
258 to libpthread and removed libc_r.
259
260 FreeBSD 8.0 removed libkse and the in-kernel support for it. The
261 only threading library supported by 8.0 and later is libthr which
262 ties each user thread directly to an LWP. To simplify the
263 implementation, this target only supports LWP-backed threads using
264 ptrace directly rather than libthread_db.
265
266 FreeBSD 11.0 introduced LWP event reporting via PT_LWP_EVENTS.
267*/
268
269/* Return true if PTID is still active in the inferior. */
270
271static int
272fbsd_thread_alive (struct target_ops *ops, ptid_t ptid)
273{
274 if (ptid_lwp_p (ptid))
275 {
276 struct ptrace_lwpinfo pl;
277
278 if (ptrace (PT_LWPINFO, ptid_get_lwp (ptid), (caddr_t) &pl, sizeof pl)
279 == -1)
280 return 0;
281#ifdef PL_FLAG_EXITED
282 if (pl.pl_flags & PL_FLAG_EXITED)
283 return 0;
284#endif
285 }
286
287 return 1;
288}
289
290/* Convert PTID to a string. Returns the string in a static
291 buffer. */
292
293static char *
294fbsd_pid_to_str (struct target_ops *ops, ptid_t ptid)
295{
296 lwpid_t lwp;
297
298 lwp = ptid_get_lwp (ptid);
299 if (lwp != 0)
300 {
301 static char buf[64];
302 int pid = ptid_get_pid (ptid);
303
304 xsnprintf (buf, sizeof buf, "process %d, LWP %d", pid, lwp);
305 return buf;
306 }
307
308 return normal_pid_to_str (ptid);
309}
310
311#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME
312/* Return the name assigned to a thread by an application. Returns
313 the string in a static buffer. */
314
315static const char *
316fbsd_thread_name (struct target_ops *self, struct thread_info *thr)
317{
318 struct ptrace_lwpinfo pl;
319 struct kinfo_proc kp;
320 int pid = ptid_get_pid (thr->ptid);
321 long lwp = ptid_get_lwp (thr->ptid);
322 static char buf[sizeof pl.pl_tdname + 1];
323
324 /* Note that ptrace_lwpinfo returns the process command in pl_tdname
325 if a name has not been set explicitly. Return a NULL name in
326 that case. */
327 fbsd_fetch_kinfo_proc (pid, &kp);
328 if (ptrace (PT_LWPINFO, lwp, (caddr_t) &pl, sizeof pl) == -1)
329 perror_with_name (("ptrace"));
330 if (strcmp (kp.ki_comm, pl.pl_tdname) == 0)
331 return NULL;
332 xsnprintf (buf, sizeof buf, "%s", pl.pl_tdname);
333 return buf;
334}
335#endif
336
337#ifdef PT_LWP_EVENTS
338/* Enable LWP events for a specific process.
339
340 To catch LWP events, PT_LWP_EVENTS is set on every traced process.
341 This enables stops on the birth for new LWPs (excluding the "main" LWP)
342 and the death of LWPs (excluding the last LWP in a process). Note
343 that unlike fork events, the LWP that creates a new LWP does not
344 report an event. */
345
346static void
347fbsd_enable_lwp_events (pid_t pid)
348{
349 if (ptrace (PT_LWP_EVENTS, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
350 perror_with_name (("ptrace"));
351}
352#endif
353
354/* Add threads for any new LWPs in a process.
355
356 When LWP events are used, this function is only used to detect existing
357 threads when attaching to a process. On older systems, this function is
358 called to discover new threads each time the thread list is updated. */
359
360static void
361fbsd_add_threads (pid_t pid)
362{
363 struct cleanup *cleanup;
364 lwpid_t *lwps;
365 int i, nlwps;
366
367 gdb_assert (!in_thread_list (pid_to_ptid (pid)));
368 nlwps = ptrace (PT_GETNUMLWPS, pid, NULL, 0);
369 if (nlwps == -1)
370 perror_with_name (("ptrace"));
371
372 lwps = XCNEWVEC (lwpid_t, nlwps);
373 cleanup = make_cleanup (xfree, lwps);
374
375 nlwps = ptrace (PT_GETLWPLIST, pid, (caddr_t) lwps, nlwps);
376 if (nlwps == -1)
377 perror_with_name (("ptrace"));
378
379 for (i = 0; i < nlwps; i++)
380 {
381 ptid_t ptid = ptid_build (pid, lwps[i], 0);
382
383 if (!in_thread_list (ptid))
384 {
385#ifdef PT_LWP_EVENTS
386 struct ptrace_lwpinfo pl;
387
388 /* Don't add exited threads. Note that this is only called
389 when attaching to a multi-threaded process. */
390 if (ptrace (PT_LWPINFO, lwps[i], (caddr_t) &pl, sizeof pl) == -1)
391 perror_with_name (("ptrace"));
392 if (pl.pl_flags & PL_FLAG_EXITED)
393 continue;
394#endif
395 if (debug_fbsd_lwp)
396 fprintf_unfiltered (gdb_stdlog,
397 "FLWP: adding thread for LWP %u\n",
398 lwps[i]);
399 add_thread (ptid);
400 }
401 }
402 do_cleanups (cleanup);
403}
404
405/* Implement the "to_update_thread_list" target_ops method. */
406
407static void
408fbsd_update_thread_list (struct target_ops *ops)
409{
410#ifdef PT_LWP_EVENTS
411 /* With support for thread events, threads are added/deleted from the
412 list as events are reported, so just try deleting exited threads. */
413 delete_exited_threads ();
414#else
415 prune_threads ();
416
417 fbsd_add_threads (ptid_get_pid (inferior_ptid));
418#endif
419}
420
421static void (*super_resume) (struct target_ops *,
422 ptid_t,
423 int,
424 enum gdb_signal);
425
426static int
427resume_one_thread_cb (struct thread_info *tp, void *data)
428{
429 ptid_t *ptid = data;
430 int request;
431
432 if (ptid_get_pid (tp->ptid) != ptid_get_pid (*ptid))
433 return 0;
434
435 if (ptid_get_lwp (tp->ptid) == ptid_get_lwp (*ptid))
436 request = PT_RESUME;
437 else
438 request = PT_SUSPEND;
439
440 if (ptrace (request, ptid_get_lwp (tp->ptid), NULL, 0) == -1)
441 perror_with_name (("ptrace"));
442 return 0;
443}
444
445static int
446resume_all_threads_cb (struct thread_info *tp, void *data)
447{
448 ptid_t *filter = data;
449
450 if (!ptid_match (tp->ptid, *filter))
451 return 0;
452
453 if (ptrace (PT_RESUME, ptid_get_lwp (tp->ptid), NULL, 0) == -1)
454 perror_with_name (("ptrace"));
455 return 0;
456}
457
458/* Implement the "to_resume" target_ops method. */
459
460static void
461fbsd_resume (struct target_ops *ops,
462 ptid_t ptid, int step, enum gdb_signal signo)
463{
464
465 if (debug_fbsd_lwp)
466 fprintf_unfiltered (gdb_stdlog,
467 "FLWP: fbsd_resume for ptid (%d, %ld, %ld)\n",
468 ptid_get_pid (ptid), ptid_get_lwp (ptid),
469 ptid_get_tid (ptid));
470 if (ptid_lwp_p (ptid))
471 {
472 /* If ptid is a specific LWP, suspend all other LWPs in the process. */
473 iterate_over_threads (resume_one_thread_cb, &ptid);
474 }
475 else
476 {
477 /* If ptid is a wildcard, resume all matching threads (they won't run
478 until the process is continued however). */
479 iterate_over_threads (resume_all_threads_cb, &ptid);
480 ptid = inferior_ptid;
481 }
482 super_resume (ops, ptid, step, signo);
483}
484
e58e05d6
JB
485#ifdef TDP_RFPPWAIT
486/*
487 To catch fork events, PT_FOLLOW_FORK is set on every traced process
488 to enable stops on returns from fork or vfork. Note that both the
489 parent and child will always stop, even if system call stops are not
490 enabled.
491
492 After a fork, both the child and parent process will stop and report
493 an event. However, there is no guarantee of order. If the parent
494 reports its stop first, then fbsd_wait explicitly waits for the new
495 child before returning. If the child reports its stop first, then
496 the event is saved on a list and ignored until the parent's stop is
497 reported. fbsd_wait could have been changed to fetch the parent PID
498 of the new child and used that to wait for the parent explicitly.
499 However, if two threads in the parent fork at the same time, then
500 the wait on the parent might return the "wrong" fork event.
501
502 The initial version of PT_FOLLOW_FORK did not set PL_FLAG_CHILD for
503 the new child process. This flag could be inferred by treating any
504 events for an unknown pid as a new child.
505
506 In addition, the initial version of PT_FOLLOW_FORK did not report a
507 stop event for the parent process of a vfork until after the child
508 process executed a new program or exited. The kernel was changed to
509 defer the wait for exit or exec of the child until after posting the
510 stop event shortly after the change to introduce PL_FLAG_CHILD.
511 This could be worked around by reporting a vfork event when the
512 child event posted and ignoring the subsequent event from the
513 parent.
514
515 This implementation requires both of these fixes for simplicity's
516 sake. FreeBSD versions newer than 9.1 contain both fixes.
517*/
518
519struct fbsd_fork_child_info
520{
521 struct fbsd_fork_child_info *next;
6e9567fe 522 ptid_t child; /* Pid of new child. */
e58e05d6
JB
523};
524
525static struct fbsd_fork_child_info *fbsd_pending_children;
526
527/* Record a new child process event that is reported before the
528 corresponding fork event in the parent. */
529
530static void
6e9567fe 531fbsd_remember_child (ptid_t pid)
e58e05d6 532{
8d749320 533 struct fbsd_fork_child_info *info = XCNEW (struct fbsd_fork_child_info);
e58e05d6
JB
534
535 info->child = pid;
536 info->next = fbsd_pending_children;
537 fbsd_pending_children = info;
538}
539
540/* Check for a previously-recorded new child process event for PID.
6e9567fe 541 If one is found, remove it from the list and return the PTID. */
e58e05d6 542
6e9567fe 543static ptid_t
e58e05d6
JB
544fbsd_is_child_pending (pid_t pid)
545{
546 struct fbsd_fork_child_info *info, *prev;
6e9567fe 547 ptid_t ptid;
e58e05d6
JB
548
549 prev = NULL;
550 for (info = fbsd_pending_children; info; prev = info, info = info->next)
551 {
6e9567fe 552 if (ptid_get_pid (info->child) == pid)
e58e05d6
JB
553 {
554 if (prev == NULL)
555 fbsd_pending_children = info->next;
556 else
557 prev->next = info->next;
6e9567fe 558 ptid = info->child;
e58e05d6 559 xfree (info);
6e9567fe 560 return ptid;
e58e05d6
JB
561 }
562 }
6e9567fe 563 return null_ptid;
e58e05d6
JB
564}
565#endif
566
567/* Wait for the child specified by PTID to do something. Return the
568 process ID of the child, or MINUS_ONE_PTID in case of error; store
569 the status in *OURSTATUS. */
570
571static ptid_t
572fbsd_wait (struct target_ops *ops,
573 ptid_t ptid, struct target_waitstatus *ourstatus,
574 int target_options)
575{
576 ptid_t wptid;
577
578 while (1)
579 {
580 wptid = super_wait (ops, ptid, ourstatus, target_options);
581 if (ourstatus->kind == TARGET_WAITKIND_STOPPED)
582 {
583 struct ptrace_lwpinfo pl;
584 pid_t pid;
585 int status;
586
587 pid = ptid_get_pid (wptid);
6e9567fe 588 if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof pl) == -1)
e58e05d6
JB
589 perror_with_name (("ptrace"));
590
6e9567fe
JB
591 wptid = ptid_build (pid, pl.pl_lwpid, 0);
592
593#ifdef PT_LWP_EVENTS
594 if (pl.pl_flags & PL_FLAG_EXITED)
595 {
596 /* If GDB attaches to a multi-threaded process, exiting
597 threads might be skipped during fbsd_post_attach that
598 have not yet reported their PL_FLAG_EXITED event.
599 Ignore EXITED events for an unknown LWP. */
600 if (in_thread_list (wptid))
601 {
602 if (debug_fbsd_lwp)
603 fprintf_unfiltered (gdb_stdlog,
604 "FLWP: deleting thread for LWP %u\n",
605 pl.pl_lwpid);
606 if (print_thread_events)
607 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str
608 (wptid));
609 delete_thread (wptid);
610 }
611 if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1)
612 perror_with_name (("ptrace"));
613 continue;
614 }
615#endif
616
617 /* Switch to an LWP PTID on the first stop in a new process.
618 This is done after handling PL_FLAG_EXITED to avoid
619 switching to an exited LWP. It is done before checking
620 PL_FLAG_BORN in case the first stop reported after
621 attaching to an existing process is a PL_FLAG_BORN
622 event. */
623 if (in_thread_list (pid_to_ptid (pid)))
624 {
625 if (debug_fbsd_lwp)
626 fprintf_unfiltered (gdb_stdlog,
627 "FLWP: using LWP %u for first thread\n",
628 pl.pl_lwpid);
629 thread_change_ptid (pid_to_ptid (pid), wptid);
630 }
631
632#ifdef PT_LWP_EVENTS
633 if (pl.pl_flags & PL_FLAG_BORN)
634 {
635 /* If GDB attaches to a multi-threaded process, newborn
636 threads might be added by fbsd_add_threads that have
637 not yet reported their PL_FLAG_BORN event. Ignore
638 BORN events for an already-known LWP. */
639 if (!in_thread_list (wptid))
640 {
641 if (debug_fbsd_lwp)
642 fprintf_unfiltered (gdb_stdlog,
643 "FLWP: adding thread for LWP %u\n",
644 pl.pl_lwpid);
645 add_thread (wptid);
646 }
647 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
648 return wptid;
649 }
650#endif
651
e58e05d6
JB
652#ifdef TDP_RFPPWAIT
653 if (pl.pl_flags & PL_FLAG_FORKED)
654 {
655 struct kinfo_proc kp;
6e9567fe 656 ptid_t child_ptid;
e58e05d6
JB
657 pid_t child;
658
659 child = pl.pl_child_pid;
660 ourstatus->kind = TARGET_WAITKIND_FORKED;
e58e05d6
JB
661
662 /* Make sure the other end of the fork is stopped too. */
6e9567fe
JB
663 child_ptid = fbsd_is_child_pending (child);
664 if (ptid_equal (child_ptid, null_ptid))
e58e05d6
JB
665 {
666 pid = waitpid (child, &status, 0);
667 if (pid == -1)
668 perror_with_name (("waitpid"));
669
670 gdb_assert (pid == child);
671
672 if (ptrace (PT_LWPINFO, child, (caddr_t)&pl, sizeof pl) == -1)
673 perror_with_name (("ptrace"));
674
675 gdb_assert (pl.pl_flags & PL_FLAG_CHILD);
6e9567fe 676 child_ptid = ptid_build (child, pl.pl_lwpid, 0);
e58e05d6
JB
677 }
678
679 /* For vfork, the child process will have the P_PPWAIT
680 flag set. */
681 fbsd_fetch_kinfo_proc (child, &kp);
682 if (kp.ki_flag & P_PPWAIT)
683 ourstatus->kind = TARGET_WAITKIND_VFORKED;
6e9567fe 684 ourstatus->value.related_pid = child_ptid;
e58e05d6
JB
685
686 return wptid;
687 }
688
689 if (pl.pl_flags & PL_FLAG_CHILD)
690 {
691 /* Remember that this child forked, but do not report it
692 until the parent reports its corresponding fork
693 event. */
6e9567fe 694 fbsd_remember_child (wptid);
e58e05d6
JB
695 continue;
696 }
697#endif
d2b41ca0
JB
698
699#ifdef PL_FLAG_EXEC
700 if (pl.pl_flags & PL_FLAG_EXEC)
701 {
702 ourstatus->kind = TARGET_WAITKIND_EXECD;
703 ourstatus->value.execd_pathname
704 = xstrdup (fbsd_pid_to_exec_file (NULL, pid));
705 return wptid;
706 }
707#endif
e58e05d6
JB
708 }
709 return wptid;
710 }
711}
712
713#ifdef TDP_RFPPWAIT
714/* Target hook for follow_fork. On entry and at return inferior_ptid is
715 the ptid of the followed inferior. */
716
717static int
718fbsd_follow_fork (struct target_ops *ops, int follow_child,
719 int detach_fork)
720{
721 if (!follow_child)
722 {
723 struct thread_info *tp = inferior_thread ();
724 pid_t child_pid = ptid_get_pid (tp->pending_follow.value.related_pid);
725
726 /* Breakpoints have already been detached from the child by
727 infrun.c. */
728
729 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
730 perror_with_name (("ptrace"));
731 }
732
733 return 0;
734}
735
736static int
737fbsd_insert_fork_catchpoint (struct target_ops *self, int pid)
738{
739 return 0;
740}
741
742static int
743fbsd_remove_fork_catchpoint (struct target_ops *self, int pid)
744{
745 return 0;
746}
747
748static int
749fbsd_insert_vfork_catchpoint (struct target_ops *self, int pid)
750{
751 return 0;
752}
753
754static int
755fbsd_remove_vfork_catchpoint (struct target_ops *self, int pid)
756{
757 return 0;
758}
759
760/* Enable fork tracing for a specific process.
761
762 To catch fork events, PT_FOLLOW_FORK is set on every traced process
763 to enable stops on returns from fork or vfork. Note that both the
764 parent and child will always stop, even if system call stops are
765 not enabled. */
766
767static void
768fbsd_enable_follow_fork (pid_t pid)
769{
770 if (ptrace (PT_FOLLOW_FORK, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
771 perror_with_name (("ptrace"));
772}
6e9567fe 773#endif
e58e05d6
JB
774
775/* Implement the "to_post_startup_inferior" target_ops method. */
776
777static void
778fbsd_post_startup_inferior (struct target_ops *self, ptid_t pid)
779{
6e9567fe 780#ifdef TDP_RFPPWAIT
e58e05d6 781 fbsd_enable_follow_fork (ptid_get_pid (pid));
6e9567fe
JB
782#endif
783#ifdef PT_LWP_EVENTS
784 fbsd_enable_lwp_events (ptid_get_pid (pid));
785#endif
e58e05d6
JB
786}
787
788/* Implement the "to_post_attach" target_ops method. */
789
790static void
791fbsd_post_attach (struct target_ops *self, int pid)
792{
6e9567fe 793#ifdef TDP_RFPPWAIT
e58e05d6 794 fbsd_enable_follow_fork (pid);
e58e05d6 795#endif
6e9567fe
JB
796#ifdef PT_LWP_EVENTS
797 fbsd_enable_lwp_events (pid);
798#endif
799 fbsd_add_threads (pid);
800}
d2b41ca0
JB
801
802#ifdef PL_FLAG_EXEC
803/* If the FreeBSD kernel supports PL_FLAG_EXEC, then traced processes
804 will always stop after exec. */
805
806static int
807fbsd_insert_exec_catchpoint (struct target_ops *self, int pid)
808{
809 return 0;
810}
811
812static int
813fbsd_remove_exec_catchpoint (struct target_ops *self, int pid)
814{
815 return 0;
816}
817#endif
e58e05d6
JB
818#endif
819
8f60fe01
JB
820void
821fbsd_nat_add_target (struct target_ops *t)
822{
823 t->to_pid_to_exec_file = fbsd_pid_to_exec_file;
824 t->to_find_memory_regions = fbsd_find_memory_regions;
e58e05d6 825#ifdef PT_LWPINFO
6e9567fe
JB
826 t->to_thread_alive = fbsd_thread_alive;
827 t->to_pid_to_str = fbsd_pid_to_str;
828#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME
829 t->to_thread_name = fbsd_thread_name;
830#endif
831 t->to_update_thread_list = fbsd_update_thread_list;
832 t->to_has_thread_control = tc_schedlock;
833 super_resume = t->to_resume;
834 t->to_resume = fbsd_resume;
e58e05d6
JB
835 super_wait = t->to_wait;
836 t->to_wait = fbsd_wait;
6e9567fe
JB
837 t->to_post_startup_inferior = fbsd_post_startup_inferior;
838 t->to_post_attach = fbsd_post_attach;
e58e05d6
JB
839#ifdef TDP_RFPPWAIT
840 t->to_follow_fork = fbsd_follow_fork;
841 t->to_insert_fork_catchpoint = fbsd_insert_fork_catchpoint;
842 t->to_remove_fork_catchpoint = fbsd_remove_fork_catchpoint;
843 t->to_insert_vfork_catchpoint = fbsd_insert_vfork_catchpoint;
844 t->to_remove_vfork_catchpoint = fbsd_remove_vfork_catchpoint;
e58e05d6 845#endif
d2b41ca0
JB
846#ifdef PL_FLAG_EXEC
847 t->to_insert_exec_catchpoint = fbsd_insert_exec_catchpoint;
848 t->to_remove_exec_catchpoint = fbsd_remove_exec_catchpoint;
849#endif
e58e05d6 850#endif
8f60fe01
JB
851 add_target (t);
852}
6e9567fe
JB
853
854/* Provide a prototype to silence -Wmissing-prototypes. */
855extern initialize_file_ftype _initialize_fbsd_nat;
856
857void
858_initialize_fbsd_nat (void)
859{
860#ifdef PT_LWPINFO
861 add_setshow_boolean_cmd ("fbsd-lwp", class_maintenance,
862 &debug_fbsd_lwp, _("\
863Set debugging of FreeBSD lwp module."), _("\
864Show debugging of FreeBSD lwp module."), _("\
865Enables printf debugging output."),
866 NULL,
867 &show_fbsd_lwp_debug,
868 &setdebuglist, &showdebuglist);
869#endif
870}
This page took 0.99915 seconds and 4 git commands to generate.