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