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