]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/fbsd-nat.c
Add a new debug knob for the FreeBSD native target.
[thirdparty/binutils-gdb.git] / gdb / fbsd-nat.c
1 /* Native-dependent code for FreeBSD.
2
3 Copyright (C) 2002-2018 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 "byte-vector.h"
22 #include "gdbcore.h"
23 #include "inferior.h"
24 #include "regcache.h"
25 #include "regset.h"
26 #include "gdbcmd.h"
27 #include "gdbthread.h"
28 #include "gdb_wait.h"
29 #include <sys/types.h>
30 #include <sys/procfs.h>
31 #include <sys/ptrace.h>
32 #include <sys/signal.h>
33 #include <sys/sysctl.h>
34 #include <sys/user.h>
35 #if defined(HAVE_KINFO_GETFILE) || defined(HAVE_KINFO_GETVMMAP)
36 #include <libutil.h>
37 #endif
38 #if !defined(HAVE_KINFO_GETVMMAP)
39 #include "filestuff.h"
40 #endif
41
42 #include "elf-bfd.h"
43 #include "fbsd-nat.h"
44 #include "fbsd-tdep.h"
45
46 #include <list>
47
48 /* Return the name of a file that can be opened to get the symbols for
49 the child process identified by PID. */
50
51 static char *
52 fbsd_pid_to_exec_file (struct target_ops *self, int pid)
53 {
54 ssize_t len;
55 static char buf[PATH_MAX];
56 char name[PATH_MAX];
57
58 #ifdef KERN_PROC_PATHNAME
59 size_t buflen;
60 int mib[4];
61
62 mib[0] = CTL_KERN;
63 mib[1] = KERN_PROC;
64 mib[2] = KERN_PROC_PATHNAME;
65 mib[3] = pid;
66 buflen = sizeof buf;
67 if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0)
68 /* The kern.proc.pathname.<pid> sysctl returns a length of zero
69 for processes without an associated executable such as kernel
70 processes. */
71 return buflen == 0 ? NULL : buf;
72 #endif
73
74 xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
75 len = readlink (name, buf, PATH_MAX - 1);
76 if (len != -1)
77 {
78 buf[len] = '\0';
79 return buf;
80 }
81
82 return NULL;
83 }
84
85 #ifdef HAVE_KINFO_GETVMMAP
86 /* Iterate over all the memory regions in the current inferior,
87 calling FUNC for each memory region. OBFD is passed as the last
88 argument to FUNC. */
89
90 static int
91 fbsd_find_memory_regions (struct target_ops *self,
92 find_memory_region_ftype func, void *obfd)
93 {
94 pid_t pid = ptid_get_pid (inferior_ptid);
95 struct kinfo_vmentry *kve;
96 uint64_t size;
97 int i, nitems;
98
99 gdb::unique_xmalloc_ptr<struct kinfo_vmentry>
100 vmentl (kinfo_getvmmap (pid, &nitems));
101 if (vmentl == NULL)
102 perror_with_name (_("Couldn't fetch VM map entries."));
103
104 for (i = 0, kve = vmentl.get (); i < nitems; i++, kve++)
105 {
106 /* Skip unreadable segments and those where MAP_NOCORE has been set. */
107 if (!(kve->kve_protection & KVME_PROT_READ)
108 || kve->kve_flags & KVME_FLAG_NOCOREDUMP)
109 continue;
110
111 /* Skip segments with an invalid type. */
112 if (kve->kve_type != KVME_TYPE_DEFAULT
113 && kve->kve_type != KVME_TYPE_VNODE
114 && kve->kve_type != KVME_TYPE_SWAP
115 && kve->kve_type != KVME_TYPE_PHYS)
116 continue;
117
118 size = kve->kve_end - kve->kve_start;
119 if (info_verbose)
120 {
121 fprintf_filtered (gdb_stdout,
122 "Save segment, %ld bytes at %s (%c%c%c)\n",
123 (long) size,
124 paddress (target_gdbarch (), kve->kve_start),
125 kve->kve_protection & KVME_PROT_READ ? 'r' : '-',
126 kve->kve_protection & KVME_PROT_WRITE ? 'w' : '-',
127 kve->kve_protection & KVME_PROT_EXEC ? 'x' : '-');
128 }
129
130 /* Invoke the callback function to create the corefile segment.
131 Pass MODIFIED as true, we do not know the real modification state. */
132 func (kve->kve_start, size, kve->kve_protection & KVME_PROT_READ,
133 kve->kve_protection & KVME_PROT_WRITE,
134 kve->kve_protection & KVME_PROT_EXEC, 1, obfd);
135 }
136 return 0;
137 }
138 #else
139 static int
140 fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end,
141 char *protection)
142 {
143 /* FreeBSD 5.1-RELEASE uses a 256-byte buffer. */
144 char buf[256];
145 int resident, privateresident;
146 unsigned long obj;
147 int ret = EOF;
148
149 /* As of FreeBSD 5.0-RELEASE, the layout is described in
150 /usr/src/sys/fs/procfs/procfs_map.c. Somewhere in 5.1-CURRENT a
151 new column was added to the procfs map. Therefore we can't use
152 fscanf since we need to support older releases too. */
153 if (fgets (buf, sizeof buf, mapfile) != NULL)
154 ret = sscanf (buf, "%lx %lx %d %d %lx %s", start, end,
155 &resident, &privateresident, &obj, protection);
156
157 return (ret != 0 && ret != EOF);
158 }
159
160 /* Iterate over all the memory regions in the current inferior,
161 calling FUNC for each memory region. OBFD is passed as the last
162 argument to FUNC. */
163
164 static int
165 fbsd_find_memory_regions (struct target_ops *self,
166 find_memory_region_ftype func, void *obfd)
167 {
168 pid_t pid = ptid_get_pid (inferior_ptid);
169 unsigned long start, end, size;
170 char protection[4];
171 int read, write, exec;
172
173 std::string mapfilename = string_printf ("/proc/%ld/map", (long) pid);
174 gdb_file_up mapfile (fopen (mapfilename.c_str (), "r"));
175 if (mapfile == NULL)
176 error (_("Couldn't open %s."), mapfilename.c_str ());
177
178 if (info_verbose)
179 fprintf_filtered (gdb_stdout,
180 "Reading memory regions from %s\n", mapfilename.c_str ());
181
182 /* Now iterate until end-of-file. */
183 while (fbsd_read_mapping (mapfile.get (), &start, &end, &protection[0]))
184 {
185 size = end - start;
186
187 read = (strchr (protection, 'r') != 0);
188 write = (strchr (protection, 'w') != 0);
189 exec = (strchr (protection, 'x') != 0);
190
191 if (info_verbose)
192 {
193 fprintf_filtered (gdb_stdout,
194 "Save segment, %ld bytes at %s (%c%c%c)\n",
195 size, paddress (target_gdbarch (), start),
196 read ? 'r' : '-',
197 write ? 'w' : '-',
198 exec ? 'x' : '-');
199 }
200
201 /* Invoke the callback function to create the corefile segment.
202 Pass MODIFIED as true, we do not know the real modification state. */
203 func (start, size, read, write, exec, 1, obfd);
204 }
205
206 return 0;
207 }
208 #endif
209
210 /* Fetch the command line for a running process. */
211
212 static gdb::unique_xmalloc_ptr<char>
213 fbsd_fetch_cmdline (pid_t pid)
214 {
215 size_t len;
216 int mib[4];
217
218 len = 0;
219 mib[0] = CTL_KERN;
220 mib[1] = KERN_PROC;
221 mib[2] = KERN_PROC_ARGS;
222 mib[3] = pid;
223 if (sysctl (mib, 4, NULL, &len, NULL, 0) == -1)
224 return nullptr;
225
226 if (len == 0)
227 return nullptr;
228
229 gdb::unique_xmalloc_ptr<char> cmdline ((char *) xmalloc (len));
230 if (sysctl (mib, 4, cmdline.get (), &len, NULL, 0) == -1)
231 return nullptr;
232
233 return cmdline;
234 }
235
236 /* Fetch the external variant of the kernel's internal process
237 structure for the process PID into KP. */
238
239 static bool
240 fbsd_fetch_kinfo_proc (pid_t pid, struct kinfo_proc *kp)
241 {
242 size_t len;
243 int mib[4];
244
245 len = sizeof *kp;
246 mib[0] = CTL_KERN;
247 mib[1] = KERN_PROC;
248 mib[2] = KERN_PROC_PID;
249 mib[3] = pid;
250 return (sysctl (mib, 4, kp, &len, NULL, 0) == 0);
251 }
252
253 /* Implement the "to_info_proc target_ops" method. */
254
255 static void
256 fbsd_info_proc (struct target_ops *ops, const char *args,
257 enum info_proc_what what)
258 {
259 #ifdef HAVE_KINFO_GETFILE
260 gdb::unique_xmalloc_ptr<struct kinfo_file> fdtbl;
261 int nfd = 0;
262 #endif
263 struct kinfo_proc kp;
264 char *tmp;
265 pid_t pid;
266 bool do_cmdline = false;
267 bool do_cwd = false;
268 bool do_exe = false;
269 #ifdef HAVE_KINFO_GETVMMAP
270 bool do_mappings = false;
271 #endif
272 bool do_status = false;
273
274 switch (what)
275 {
276 case IP_MINIMAL:
277 do_cmdline = true;
278 do_cwd = true;
279 do_exe = true;
280 break;
281 #ifdef HAVE_KINFO_GETVMMAP
282 case IP_MAPPINGS:
283 do_mappings = true;
284 break;
285 #endif
286 case IP_STATUS:
287 case IP_STAT:
288 do_status = true;
289 break;
290 case IP_CMDLINE:
291 do_cmdline = true;
292 break;
293 case IP_EXE:
294 do_exe = true;
295 break;
296 case IP_CWD:
297 do_cwd = true;
298 break;
299 case IP_ALL:
300 do_cmdline = true;
301 do_cwd = true;
302 do_exe = true;
303 #ifdef HAVE_KINFO_GETVMMAP
304 do_mappings = true;
305 #endif
306 do_status = true;
307 break;
308 default:
309 error (_("Not supported on this target."));
310 }
311
312 gdb_argv built_argv (args);
313 if (built_argv.count () == 0)
314 {
315 pid = ptid_get_pid (inferior_ptid);
316 if (pid == 0)
317 error (_("No current process: you must name one."));
318 }
319 else if (built_argv.count () == 1 && isdigit (built_argv[0][0]))
320 pid = strtol (built_argv[0], NULL, 10);
321 else
322 error (_("Invalid arguments."));
323
324 printf_filtered (_("process %d\n"), pid);
325 #ifdef HAVE_KINFO_GETFILE
326 if (do_cwd || do_exe)
327 fdtbl.reset (kinfo_getfile (pid, &nfd));
328 #endif
329
330 if (do_cmdline)
331 {
332 gdb::unique_xmalloc_ptr<char> cmdline = fbsd_fetch_cmdline (pid);
333 if (cmdline != nullptr)
334 printf_filtered ("cmdline = '%s'\n", cmdline.get ());
335 else
336 warning (_("unable to fetch command line"));
337 }
338 if (do_cwd)
339 {
340 const char *cwd = NULL;
341 #ifdef HAVE_KINFO_GETFILE
342 struct kinfo_file *kf = fdtbl.get ();
343 for (int i = 0; i < nfd; i++, kf++)
344 {
345 if (kf->kf_type == KF_TYPE_VNODE && kf->kf_fd == KF_FD_TYPE_CWD)
346 {
347 cwd = kf->kf_path;
348 break;
349 }
350 }
351 #endif
352 if (cwd != NULL)
353 printf_filtered ("cwd = '%s'\n", cwd);
354 else
355 warning (_("unable to fetch current working directory"));
356 }
357 if (do_exe)
358 {
359 const char *exe = NULL;
360 #ifdef HAVE_KINFO_GETFILE
361 struct kinfo_file *kf = fdtbl.get ();
362 for (int i = 0; i < nfd; i++, kf++)
363 {
364 if (kf->kf_type == KF_TYPE_VNODE && kf->kf_fd == KF_FD_TYPE_TEXT)
365 {
366 exe = kf->kf_path;
367 break;
368 }
369 }
370 #endif
371 if (exe == NULL)
372 exe = fbsd_pid_to_exec_file (ops, pid);
373 if (exe != NULL)
374 printf_filtered ("exe = '%s'\n", exe);
375 else
376 warning (_("unable to fetch executable path name"));
377 }
378 #ifdef HAVE_KINFO_GETVMMAP
379 if (do_mappings)
380 {
381 int nvment;
382 gdb::unique_xmalloc_ptr<struct kinfo_vmentry>
383 vmentl (kinfo_getvmmap (pid, &nvment));
384
385 if (vmentl != nullptr)
386 {
387 printf_filtered (_("Mapped address spaces:\n\n"));
388 #ifdef __LP64__
389 printf_filtered (" %18s %18s %10s %10s %9s %s\n",
390 "Start Addr",
391 " End Addr",
392 " Size", " Offset", "Flags ", "File");
393 #else
394 printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
395 "Start Addr",
396 " End Addr",
397 " Size", " Offset", "Flags ", "File");
398 #endif
399
400 struct kinfo_vmentry *kve = vmentl.get ();
401 for (int i = 0; i < nvment; i++, kve++)
402 {
403 ULONGEST start, end;
404
405 start = kve->kve_start;
406 end = kve->kve_end;
407 #ifdef __LP64__
408 printf_filtered (" %18s %18s %10s %10s %9s %s\n",
409 hex_string (start),
410 hex_string (end),
411 hex_string (end - start),
412 hex_string (kve->kve_offset),
413 fbsd_vm_map_entry_flags (kve->kve_flags,
414 kve->kve_protection),
415 kve->kve_path);
416 #else
417 printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
418 hex_string (start),
419 hex_string (end),
420 hex_string (end - start),
421 hex_string (kve->kve_offset),
422 fbsd_vm_map_entry_flags (kve->kve_flags,
423 kve->kve_protection),
424 kve->kve_path);
425 #endif
426 }
427 }
428 else
429 warning (_("unable to fetch virtual memory map"));
430 }
431 #endif
432 if (do_status)
433 {
434 if (!fbsd_fetch_kinfo_proc (pid, &kp))
435 warning (_("Failed to fetch process information"));
436 else
437 {
438 const char *state;
439 int pgtok;
440
441 printf_filtered ("Name: %s\n", kp.ki_comm);
442 switch (kp.ki_stat)
443 {
444 case SIDL:
445 state = "I (idle)";
446 break;
447 case SRUN:
448 state = "R (running)";
449 break;
450 case SSTOP:
451 state = "T (stopped)";
452 break;
453 case SZOMB:
454 state = "Z (zombie)";
455 break;
456 case SSLEEP:
457 state = "S (sleeping)";
458 break;
459 case SWAIT:
460 state = "W (interrupt wait)";
461 break;
462 case SLOCK:
463 state = "L (blocked on lock)";
464 break;
465 default:
466 state = "? (unknown)";
467 break;
468 }
469 printf_filtered ("State: %s\n", state);
470 printf_filtered ("Parent process: %d\n", kp.ki_ppid);
471 printf_filtered ("Process group: %d\n", kp.ki_pgid);
472 printf_filtered ("Session id: %d\n", kp.ki_sid);
473 printf_filtered ("TTY: %ju\n", (uintmax_t) kp.ki_tdev);
474 printf_filtered ("TTY owner process group: %d\n", kp.ki_tpgid);
475 printf_filtered ("User IDs (real, effective, saved): %d %d %d\n",
476 kp.ki_ruid, kp.ki_uid, kp.ki_svuid);
477 printf_filtered ("Group IDs (real, effective, saved): %d %d %d\n",
478 kp.ki_rgid, kp.ki_groups[0], kp.ki_svgid);
479 printf_filtered ("Groups: ");
480 for (int i = 0; i < kp.ki_ngroups; i++)
481 printf_filtered ("%d ", kp.ki_groups[i]);
482 printf_filtered ("\n");
483 printf_filtered ("Minor faults (no memory page): %ld\n",
484 kp.ki_rusage.ru_minflt);
485 printf_filtered ("Minor faults, children: %ld\n",
486 kp.ki_rusage_ch.ru_minflt);
487 printf_filtered ("Major faults (memory page faults): %ld\n",
488 kp.ki_rusage.ru_majflt);
489 printf_filtered ("Major faults, children: %ld\n",
490 kp.ki_rusage_ch.ru_majflt);
491 printf_filtered ("utime: %jd.%06ld\n",
492 (intmax_t) kp.ki_rusage.ru_utime.tv_sec,
493 kp.ki_rusage.ru_utime.tv_usec);
494 printf_filtered ("stime: %jd.%06ld\n",
495 (intmax_t) kp.ki_rusage.ru_stime.tv_sec,
496 kp.ki_rusage.ru_stime.tv_usec);
497 printf_filtered ("utime, children: %jd.%06ld\n",
498 (intmax_t) kp.ki_rusage_ch.ru_utime.tv_sec,
499 kp.ki_rusage_ch.ru_utime.tv_usec);
500 printf_filtered ("stime, children: %jd.%06ld\n",
501 (intmax_t) kp.ki_rusage_ch.ru_stime.tv_sec,
502 kp.ki_rusage_ch.ru_stime.tv_usec);
503 printf_filtered ("'nice' value: %d\n", kp.ki_nice);
504 printf_filtered ("Start time: %jd.%06ld\n", kp.ki_start.tv_sec,
505 kp.ki_start.tv_usec);
506 pgtok = getpagesize () / 1024;
507 printf_filtered ("Virtual memory size: %ju kB\n",
508 (uintmax_t) kp.ki_size / 1024);
509 printf_filtered ("Data size: %ju kB\n",
510 (uintmax_t) kp.ki_dsize * pgtok);
511 printf_filtered ("Stack size: %ju kB\n",
512 (uintmax_t) kp.ki_ssize * pgtok);
513 printf_filtered ("Text size: %ju kB\n",
514 (uintmax_t) kp.ki_tsize * pgtok);
515 printf_filtered ("Resident set size: %ju kB\n",
516 (uintmax_t) kp.ki_rssize * pgtok);
517 printf_filtered ("Maximum RSS: %ju kB\n",
518 (uintmax_t) kp.ki_rusage.ru_maxrss);
519 printf_filtered ("Pending Signals: ");
520 for (int i = 0; i < _SIG_WORDS; i++)
521 printf_filtered ("%08x ", kp.ki_siglist.__bits[i]);
522 printf_filtered ("\n");
523 printf_filtered ("Ignored Signals: ");
524 for (int i = 0; i < _SIG_WORDS; i++)
525 printf_filtered ("%08x ", kp.ki_sigignore.__bits[i]);
526 printf_filtered ("\n");
527 printf_filtered ("Caught Signals: ");
528 for (int i = 0; i < _SIG_WORDS; i++)
529 printf_filtered ("%08x ", kp.ki_sigcatch.__bits[i]);
530 printf_filtered ("\n");
531 }
532 }
533 }
534
535 #ifdef KERN_PROC_AUXV
536 static enum target_xfer_status (*super_xfer_partial) (struct target_ops *ops,
537 enum target_object object,
538 const char *annex,
539 gdb_byte *readbuf,
540 const gdb_byte *writebuf,
541 ULONGEST offset,
542 ULONGEST len,
543 ULONGEST *xfered_len);
544
545 #ifdef PT_LWPINFO
546 /* Return the size of siginfo for the current inferior. */
547
548 #ifdef __LP64__
549 union sigval32 {
550 int sival_int;
551 uint32_t sival_ptr;
552 };
553
554 /* This structure matches the naming and layout of `siginfo_t' in
555 <sys/signal.h>. In particular, the `si_foo' macros defined in that
556 header can be used with both types to copy fields in the `_reason'
557 union. */
558
559 struct siginfo32
560 {
561 int si_signo;
562 int si_errno;
563 int si_code;
564 __pid_t si_pid;
565 __uid_t si_uid;
566 int si_status;
567 uint32_t si_addr;
568 union sigval32 si_value;
569 union
570 {
571 struct
572 {
573 int _trapno;
574 } _fault;
575 struct
576 {
577 int _timerid;
578 int _overrun;
579 } _timer;
580 struct
581 {
582 int _mqd;
583 } _mesgq;
584 struct
585 {
586 int32_t _band;
587 } _poll;
588 struct
589 {
590 int32_t __spare1__;
591 int __spare2__[7];
592 } __spare__;
593 } _reason;
594 };
595 #endif
596
597 static size_t
598 fbsd_siginfo_size ()
599 {
600 #ifdef __LP64__
601 struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
602
603 /* Is the inferior 32-bit? If so, use the 32-bit siginfo size. */
604 if (gdbarch_long_bit (gdbarch) == 32)
605 return sizeof (struct siginfo32);
606 #endif
607 return sizeof (siginfo_t);
608 }
609
610 /* Convert a native 64-bit siginfo object to a 32-bit object. Note
611 that FreeBSD doesn't support writing to $_siginfo, so this only
612 needs to convert one way. */
613
614 static void
615 fbsd_convert_siginfo (siginfo_t *si)
616 {
617 #ifdef __LP64__
618 struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
619
620 /* Is the inferior 32-bit? If not, nothing to do. */
621 if (gdbarch_long_bit (gdbarch) != 32)
622 return;
623
624 struct siginfo32 si32;
625
626 si32.si_signo = si->si_signo;
627 si32.si_errno = si->si_errno;
628 si32.si_code = si->si_code;
629 si32.si_pid = si->si_pid;
630 si32.si_uid = si->si_uid;
631 si32.si_status = si->si_status;
632 si32.si_addr = (uintptr_t) si->si_addr;
633
634 /* If sival_ptr is being used instead of sival_int on a big-endian
635 platform, then sival_int will be zero since it holds the upper
636 32-bits of the pointer value. */
637 #if _BYTE_ORDER == _BIG_ENDIAN
638 if (si->si_value.sival_int == 0)
639 si32.si_value.sival_ptr = (uintptr_t) si->si_value.sival_ptr;
640 else
641 si32.si_value.sival_int = si->si_value.sival_int;
642 #else
643 si32.si_value.sival_int = si->si_value.sival_int;
644 #endif
645
646 /* Always copy the spare fields and then possibly overwrite them for
647 signal-specific or code-specific fields. */
648 si32._reason.__spare__.__spare1__ = si->_reason.__spare__.__spare1__;
649 for (int i = 0; i < 7; i++)
650 si32._reason.__spare__.__spare2__[i] = si->_reason.__spare__.__spare2__[i];
651 switch (si->si_signo) {
652 case SIGILL:
653 case SIGFPE:
654 case SIGSEGV:
655 case SIGBUS:
656 si32.si_trapno = si->si_trapno;
657 break;
658 }
659 switch (si->si_code) {
660 case SI_TIMER:
661 si32.si_timerid = si->si_timerid;
662 si32.si_overrun = si->si_overrun;
663 break;
664 case SI_MESGQ:
665 si32.si_mqd = si->si_mqd;
666 break;
667 }
668
669 memcpy(si, &si32, sizeof (si32));
670 #endif
671 }
672 #endif
673
674 /* Implement the "to_xfer_partial target_ops" method. */
675
676 static enum target_xfer_status
677 fbsd_xfer_partial (struct target_ops *ops, enum target_object object,
678 const char *annex, gdb_byte *readbuf,
679 const gdb_byte *writebuf,
680 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
681 {
682 pid_t pid = ptid_get_pid (inferior_ptid);
683
684 switch (object)
685 {
686 #ifdef PT_LWPINFO
687 case TARGET_OBJECT_SIGNAL_INFO:
688 {
689 struct ptrace_lwpinfo pl;
690 size_t siginfo_size;
691
692 /* FreeBSD doesn't support writing to $_siginfo. */
693 if (writebuf != NULL)
694 return TARGET_XFER_E_IO;
695
696 if (inferior_ptid.lwp_p ())
697 pid = inferior_ptid.lwp ();
698
699 siginfo_size = fbsd_siginfo_size ();
700 if (offset > siginfo_size)
701 return TARGET_XFER_E_IO;
702
703 if (ptrace (PT_LWPINFO, pid, (PTRACE_TYPE_ARG3) &pl, sizeof (pl)) == -1)
704 return TARGET_XFER_E_IO;
705
706 if (!(pl.pl_flags & PL_FLAG_SI))
707 return TARGET_XFER_E_IO;
708
709 fbsd_convert_siginfo (&pl.pl_siginfo);
710 if (offset + len > siginfo_size)
711 len = siginfo_size - offset;
712
713 memcpy (readbuf, ((gdb_byte *) &pl.pl_siginfo) + offset, len);
714 *xfered_len = len;
715 return TARGET_XFER_OK;
716 }
717 #endif
718 case TARGET_OBJECT_AUXV:
719 {
720 gdb::byte_vector buf_storage;
721 gdb_byte *buf;
722 size_t buflen;
723 int mib[4];
724
725 if (writebuf != NULL)
726 return TARGET_XFER_E_IO;
727 mib[0] = CTL_KERN;
728 mib[1] = KERN_PROC;
729 mib[2] = KERN_PROC_AUXV;
730 mib[3] = pid;
731 if (offset == 0)
732 {
733 buf = readbuf;
734 buflen = len;
735 }
736 else
737 {
738 buflen = offset + len;
739 buf_storage.resize (buflen);
740 buf = buf_storage.data ();
741 }
742 if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0)
743 {
744 if (offset != 0)
745 {
746 if (buflen > offset)
747 {
748 buflen -= offset;
749 memcpy (readbuf, buf + offset, buflen);
750 }
751 else
752 buflen = 0;
753 }
754 *xfered_len = buflen;
755 return (buflen == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
756 }
757 return TARGET_XFER_E_IO;
758 }
759 default:
760 return super_xfer_partial (ops, object, annex, readbuf, writebuf, offset,
761 len, xfered_len);
762 }
763 }
764 #endif
765
766 #ifdef PT_LWPINFO
767 static int debug_fbsd_lwp;
768 static int debug_fbsd_nat;
769
770 static void (*super_resume) (struct target_ops *,
771 ptid_t,
772 int,
773 enum gdb_signal);
774 static ptid_t (*super_wait) (struct target_ops *,
775 ptid_t,
776 struct target_waitstatus *,
777 int);
778
779 static void
780 show_fbsd_lwp_debug (struct ui_file *file, int from_tty,
781 struct cmd_list_element *c, const char *value)
782 {
783 fprintf_filtered (file, _("Debugging of FreeBSD lwp module is %s.\n"), value);
784 }
785
786 static void
787 show_fbsd_nat_debug (struct ui_file *file, int from_tty,
788 struct cmd_list_element *c, const char *value)
789 {
790 fprintf_filtered (file, _("Debugging of FreeBSD native target is %s.\n"),
791 value);
792 }
793
794 /*
795 FreeBSD's first thread support was via a "reentrant" version of libc
796 (libc_r) that first shipped in 2.2.7. This library multiplexed all
797 of the threads in a process onto a single kernel thread. This
798 library was supported via the bsd-uthread target.
799
800 FreeBSD 5.1 introduced two new threading libraries that made use of
801 multiple kernel threads. The first (libkse) scheduled M user
802 threads onto N (<= M) kernel threads (LWPs). The second (libthr)
803 bound each user thread to a dedicated kernel thread. libkse shipped
804 as the default threading library (libpthread).
805
806 FreeBSD 5.3 added a libthread_db to abstract the interface across
807 the various thread libraries (libc_r, libkse, and libthr).
808
809 FreeBSD 7.0 switched the default threading library from from libkse
810 to libpthread and removed libc_r.
811
812 FreeBSD 8.0 removed libkse and the in-kernel support for it. The
813 only threading library supported by 8.0 and later is libthr which
814 ties each user thread directly to an LWP. To simplify the
815 implementation, this target only supports LWP-backed threads using
816 ptrace directly rather than libthread_db.
817
818 FreeBSD 11.0 introduced LWP event reporting via PT_LWP_EVENTS.
819 */
820
821 /* Return true if PTID is still active in the inferior. */
822
823 static int
824 fbsd_thread_alive (struct target_ops *ops, ptid_t ptid)
825 {
826 if (ptid_lwp_p (ptid))
827 {
828 struct ptrace_lwpinfo pl;
829
830 if (ptrace (PT_LWPINFO, ptid_get_lwp (ptid), (caddr_t) &pl, sizeof pl)
831 == -1)
832 return 0;
833 #ifdef PL_FLAG_EXITED
834 if (pl.pl_flags & PL_FLAG_EXITED)
835 return 0;
836 #endif
837 }
838
839 return 1;
840 }
841
842 /* Convert PTID to a string. Returns the string in a static
843 buffer. */
844
845 static const char *
846 fbsd_pid_to_str (struct target_ops *ops, ptid_t ptid)
847 {
848 lwpid_t lwp;
849
850 lwp = ptid_get_lwp (ptid);
851 if (lwp != 0)
852 {
853 static char buf[64];
854 int pid = ptid_get_pid (ptid);
855
856 xsnprintf (buf, sizeof buf, "LWP %d of process %d", lwp, pid);
857 return buf;
858 }
859
860 return normal_pid_to_str (ptid);
861 }
862
863 #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME
864 /* Return the name assigned to a thread by an application. Returns
865 the string in a static buffer. */
866
867 static const char *
868 fbsd_thread_name (struct target_ops *self, struct thread_info *thr)
869 {
870 struct ptrace_lwpinfo pl;
871 struct kinfo_proc kp;
872 int pid = ptid_get_pid (thr->ptid);
873 long lwp = ptid_get_lwp (thr->ptid);
874 static char buf[sizeof pl.pl_tdname + 1];
875
876 /* Note that ptrace_lwpinfo returns the process command in pl_tdname
877 if a name has not been set explicitly. Return a NULL name in
878 that case. */
879 if (!fbsd_fetch_kinfo_proc (pid, &kp))
880 perror_with_name (_("Failed to fetch process information"));
881 if (ptrace (PT_LWPINFO, lwp, (caddr_t) &pl, sizeof pl) == -1)
882 perror_with_name (("ptrace"));
883 if (strcmp (kp.ki_comm, pl.pl_tdname) == 0)
884 return NULL;
885 xsnprintf (buf, sizeof buf, "%s", pl.pl_tdname);
886 return buf;
887 }
888 #endif
889
890 /* Enable additional event reporting on new processes.
891
892 To catch fork events, PTRACE_FORK is set on every traced process
893 to enable stops on returns from fork or vfork. Note that both the
894 parent and child will always stop, even if system call stops are
895 not enabled.
896
897 To catch LWP events, PTRACE_EVENTS is set on every traced process.
898 This enables stops on the birth for new LWPs (excluding the "main" LWP)
899 and the death of LWPs (excluding the last LWP in a process). Note
900 that unlike fork events, the LWP that creates a new LWP does not
901 report an event. */
902
903 static void
904 fbsd_enable_proc_events (pid_t pid)
905 {
906 #ifdef PT_GET_EVENT_MASK
907 int events;
908
909 if (ptrace (PT_GET_EVENT_MASK, pid, (PTRACE_TYPE_ARG3)&events,
910 sizeof (events)) == -1)
911 perror_with_name (("ptrace"));
912 events |= PTRACE_FORK | PTRACE_LWP;
913 #ifdef PTRACE_VFORK
914 events |= PTRACE_VFORK;
915 #endif
916 if (ptrace (PT_SET_EVENT_MASK, pid, (PTRACE_TYPE_ARG3)&events,
917 sizeof (events)) == -1)
918 perror_with_name (("ptrace"));
919 #else
920 #ifdef TDP_RFPPWAIT
921 if (ptrace (PT_FOLLOW_FORK, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
922 perror_with_name (("ptrace"));
923 #endif
924 #ifdef PT_LWP_EVENTS
925 if (ptrace (PT_LWP_EVENTS, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
926 perror_with_name (("ptrace"));
927 #endif
928 #endif
929 }
930
931 /* Add threads for any new LWPs in a process.
932
933 When LWP events are used, this function is only used to detect existing
934 threads when attaching to a process. On older systems, this function is
935 called to discover new threads each time the thread list is updated. */
936
937 static void
938 fbsd_add_threads (pid_t pid)
939 {
940 int i, nlwps;
941
942 gdb_assert (!in_thread_list (pid_to_ptid (pid)));
943 nlwps = ptrace (PT_GETNUMLWPS, pid, NULL, 0);
944 if (nlwps == -1)
945 perror_with_name (("ptrace"));
946
947 gdb::unique_xmalloc_ptr<lwpid_t[]> lwps (XCNEWVEC (lwpid_t, nlwps));
948
949 nlwps = ptrace (PT_GETLWPLIST, pid, (caddr_t) lwps.get (), nlwps);
950 if (nlwps == -1)
951 perror_with_name (("ptrace"));
952
953 for (i = 0; i < nlwps; i++)
954 {
955 ptid_t ptid = ptid_build (pid, lwps[i], 0);
956
957 if (!in_thread_list (ptid))
958 {
959 #ifdef PT_LWP_EVENTS
960 struct ptrace_lwpinfo pl;
961
962 /* Don't add exited threads. Note that this is only called
963 when attaching to a multi-threaded process. */
964 if (ptrace (PT_LWPINFO, lwps[i], (caddr_t) &pl, sizeof pl) == -1)
965 perror_with_name (("ptrace"));
966 if (pl.pl_flags & PL_FLAG_EXITED)
967 continue;
968 #endif
969 if (debug_fbsd_lwp)
970 fprintf_unfiltered (gdb_stdlog,
971 "FLWP: adding thread for LWP %u\n",
972 lwps[i]);
973 add_thread (ptid);
974 }
975 }
976 }
977
978 /* Implement the "to_update_thread_list" target_ops method. */
979
980 static void
981 fbsd_update_thread_list (struct target_ops *ops)
982 {
983 #ifdef PT_LWP_EVENTS
984 /* With support for thread events, threads are added/deleted from the
985 list as events are reported, so just try deleting exited threads. */
986 delete_exited_threads ();
987 #else
988 prune_threads ();
989
990 fbsd_add_threads (ptid_get_pid (inferior_ptid));
991 #endif
992 }
993
994 #ifdef TDP_RFPPWAIT
995 /*
996 To catch fork events, PT_FOLLOW_FORK is set on every traced process
997 to enable stops on returns from fork or vfork. Note that both the
998 parent and child will always stop, even if system call stops are not
999 enabled.
1000
1001 After a fork, both the child and parent process will stop and report
1002 an event. However, there is no guarantee of order. If the parent
1003 reports its stop first, then fbsd_wait explicitly waits for the new
1004 child before returning. If the child reports its stop first, then
1005 the event is saved on a list and ignored until the parent's stop is
1006 reported. fbsd_wait could have been changed to fetch the parent PID
1007 of the new child and used that to wait for the parent explicitly.
1008 However, if two threads in the parent fork at the same time, then
1009 the wait on the parent might return the "wrong" fork event.
1010
1011 The initial version of PT_FOLLOW_FORK did not set PL_FLAG_CHILD for
1012 the new child process. This flag could be inferred by treating any
1013 events for an unknown pid as a new child.
1014
1015 In addition, the initial version of PT_FOLLOW_FORK did not report a
1016 stop event for the parent process of a vfork until after the child
1017 process executed a new program or exited. The kernel was changed to
1018 defer the wait for exit or exec of the child until after posting the
1019 stop event shortly after the change to introduce PL_FLAG_CHILD.
1020 This could be worked around by reporting a vfork event when the
1021 child event posted and ignoring the subsequent event from the
1022 parent.
1023
1024 This implementation requires both of these fixes for simplicity's
1025 sake. FreeBSD versions newer than 9.1 contain both fixes.
1026 */
1027
1028 static std::list<ptid_t> fbsd_pending_children;
1029
1030 /* Record a new child process event that is reported before the
1031 corresponding fork event in the parent. */
1032
1033 static void
1034 fbsd_remember_child (ptid_t pid)
1035 {
1036 fbsd_pending_children.push_front (pid);
1037 }
1038
1039 /* Check for a previously-recorded new child process event for PID.
1040 If one is found, remove it from the list and return the PTID. */
1041
1042 static ptid_t
1043 fbsd_is_child_pending (pid_t pid)
1044 {
1045 for (auto it = fbsd_pending_children.begin ();
1046 it != fbsd_pending_children.end (); it++)
1047 if (it->pid () == pid)
1048 {
1049 ptid_t ptid = *it;
1050 fbsd_pending_children.erase (it);
1051 return ptid;
1052 }
1053 return null_ptid;
1054 }
1055
1056 #ifndef PTRACE_VFORK
1057 static std::forward_list<ptid_t> fbsd_pending_vfork_done;
1058
1059 /* Record a pending vfork done event. */
1060
1061 static void
1062 fbsd_add_vfork_done (ptid_t pid)
1063 {
1064 fbsd_pending_vfork_done.push_front (pid);
1065 }
1066
1067 /* Check for a pending vfork done event for a specific PID. */
1068
1069 static int
1070 fbsd_is_vfork_done_pending (pid_t pid)
1071 {
1072 for (auto it = fbsd_pending_vfork_done.begin ();
1073 it != fbsd_pending_vfork_done.end (); it++)
1074 if (it->pid () == pid)
1075 return 1;
1076 return 0;
1077 }
1078
1079 /* Check for a pending vfork done event. If one is found, remove it
1080 from the list and return the PTID. */
1081
1082 static ptid_t
1083 fbsd_next_vfork_done (void)
1084 {
1085 if (!fbsd_pending_vfork_done.empty ())
1086 {
1087 ptid_t ptid = fbsd_pending_vfork_done.front ();
1088 fbsd_pending_vfork_done.pop_front ();
1089 return ptid;
1090 }
1091 return null_ptid;
1092 }
1093 #endif
1094 #endif
1095
1096 /* Implement the "to_resume" target_ops method. */
1097
1098 static void
1099 fbsd_resume (struct target_ops *ops,
1100 ptid_t ptid, int step, enum gdb_signal signo)
1101 {
1102 #if defined(TDP_RFPPWAIT) && !defined(PTRACE_VFORK)
1103 pid_t pid;
1104
1105 /* Don't PT_CONTINUE a process which has a pending vfork done event. */
1106 if (ptid_equal (minus_one_ptid, ptid))
1107 pid = ptid_get_pid (inferior_ptid);
1108 else
1109 pid = ptid_get_pid (ptid);
1110 if (fbsd_is_vfork_done_pending (pid))
1111 return;
1112 #endif
1113
1114 if (debug_fbsd_lwp)
1115 fprintf_unfiltered (gdb_stdlog,
1116 "FLWP: fbsd_resume for ptid (%d, %ld, %ld)\n",
1117 ptid_get_pid (ptid), ptid_get_lwp (ptid),
1118 ptid_get_tid (ptid));
1119 if (ptid_lwp_p (ptid))
1120 {
1121 /* If ptid is a specific LWP, suspend all other LWPs in the process. */
1122 struct thread_info *tp;
1123 int request;
1124
1125 ALL_NON_EXITED_THREADS (tp)
1126 {
1127 if (ptid_get_pid (tp->ptid) != ptid_get_pid (ptid))
1128 continue;
1129
1130 if (ptid_get_lwp (tp->ptid) == ptid_get_lwp (ptid))
1131 request = PT_RESUME;
1132 else
1133 request = PT_SUSPEND;
1134
1135 if (ptrace (request, ptid_get_lwp (tp->ptid), NULL, 0) == -1)
1136 perror_with_name (("ptrace"));
1137 }
1138 }
1139 else
1140 {
1141 /* If ptid is a wildcard, resume all matching threads (they won't run
1142 until the process is continued however). */
1143 struct thread_info *tp;
1144
1145 ALL_NON_EXITED_THREADS (tp)
1146 {
1147 if (!ptid_match (tp->ptid, ptid))
1148 continue;
1149
1150 if (ptrace (PT_RESUME, ptid_get_lwp (tp->ptid), NULL, 0) == -1)
1151 perror_with_name (("ptrace"));
1152 }
1153 ptid = inferior_ptid;
1154 }
1155
1156 #if __FreeBSD_version < 1200052
1157 /* When multiple threads within a process wish to report STOPPED
1158 events from wait(), the kernel picks one thread event as the
1159 thread event to report. The chosen thread event is retrieved via
1160 PT_LWPINFO by passing the process ID as the request pid. If
1161 multiple events are pending, then the subsequent wait() after
1162 resuming a process will report another STOPPED event after
1163 resuming the process to handle the next thread event and so on.
1164
1165 A single thread event is cleared as a side effect of resuming the
1166 process with PT_CONTINUE, PT_STEP, etc. In older kernels,
1167 however, the request pid was used to select which thread's event
1168 was cleared rather than always clearing the event that was just
1169 reported. To avoid clearing the event of the wrong LWP, always
1170 pass the process ID instead of an LWP ID to PT_CONTINUE or
1171 PT_SYSCALL.
1172
1173 In the case of stepping, the process ID cannot be used with
1174 PT_STEP since it would step the thread that reported an event
1175 which may not be the thread indicated by PTID. For stepping, use
1176 PT_SETSTEP to enable stepping on the desired thread before
1177 resuming the process via PT_CONTINUE instead of using
1178 PT_STEP. */
1179 if (step)
1180 {
1181 if (ptrace (PT_SETSTEP, get_ptrace_pid (ptid), NULL, 0) == -1)
1182 perror_with_name (("ptrace"));
1183 step = 0;
1184 }
1185 ptid = ptid_t (ptid.pid ());
1186 #endif
1187 super_resume (ops, ptid, step, signo);
1188 }
1189
1190 /* Wait for the child specified by PTID to do something. Return the
1191 process ID of the child, or MINUS_ONE_PTID in case of error; store
1192 the status in *OURSTATUS. */
1193
1194 static ptid_t
1195 fbsd_wait (struct target_ops *ops,
1196 ptid_t ptid, struct target_waitstatus *ourstatus,
1197 int target_options)
1198 {
1199 ptid_t wptid;
1200
1201 while (1)
1202 {
1203 #ifndef PTRACE_VFORK
1204 wptid = fbsd_next_vfork_done ();
1205 if (!ptid_equal (wptid, null_ptid))
1206 {
1207 ourstatus->kind = TARGET_WAITKIND_VFORK_DONE;
1208 return wptid;
1209 }
1210 #endif
1211 wptid = super_wait (ops, ptid, ourstatus, target_options);
1212 if (ourstatus->kind == TARGET_WAITKIND_STOPPED)
1213 {
1214 struct ptrace_lwpinfo pl;
1215 pid_t pid;
1216 int status;
1217
1218 pid = ptid_get_pid (wptid);
1219 if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof pl) == -1)
1220 perror_with_name (("ptrace"));
1221
1222 wptid = ptid_build (pid, pl.pl_lwpid, 0);
1223
1224 if (debug_fbsd_nat)
1225 {
1226 fprintf_unfiltered (gdb_stdlog,
1227 "FNAT: stop for LWP %u event %d flags %#x\n",
1228 pl.pl_lwpid, pl.pl_event, pl.pl_flags);
1229 if (pl.pl_flags & PL_FLAG_SI)
1230 fprintf_unfiltered (gdb_stdlog,
1231 "FNAT: si_signo %u si_code %u\n",
1232 pl.pl_siginfo.si_signo,
1233 pl.pl_siginfo.si_code);
1234 }
1235
1236 #ifdef PT_LWP_EVENTS
1237 if (pl.pl_flags & PL_FLAG_EXITED)
1238 {
1239 /* If GDB attaches to a multi-threaded process, exiting
1240 threads might be skipped during fbsd_post_attach that
1241 have not yet reported their PL_FLAG_EXITED event.
1242 Ignore EXITED events for an unknown LWP. */
1243 if (in_thread_list (wptid))
1244 {
1245 if (debug_fbsd_lwp)
1246 fprintf_unfiltered (gdb_stdlog,
1247 "FLWP: deleting thread for LWP %u\n",
1248 pl.pl_lwpid);
1249 if (print_thread_events)
1250 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str
1251 (wptid));
1252 delete_thread (wptid);
1253 }
1254 if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1)
1255 perror_with_name (("ptrace"));
1256 continue;
1257 }
1258 #endif
1259
1260 /* Switch to an LWP PTID on the first stop in a new process.
1261 This is done after handling PL_FLAG_EXITED to avoid
1262 switching to an exited LWP. It is done before checking
1263 PL_FLAG_BORN in case the first stop reported after
1264 attaching to an existing process is a PL_FLAG_BORN
1265 event. */
1266 if (in_thread_list (pid_to_ptid (pid)))
1267 {
1268 if (debug_fbsd_lwp)
1269 fprintf_unfiltered (gdb_stdlog,
1270 "FLWP: using LWP %u for first thread\n",
1271 pl.pl_lwpid);
1272 thread_change_ptid (pid_to_ptid (pid), wptid);
1273 }
1274
1275 #ifdef PT_LWP_EVENTS
1276 if (pl.pl_flags & PL_FLAG_BORN)
1277 {
1278 /* If GDB attaches to a multi-threaded process, newborn
1279 threads might be added by fbsd_add_threads that have
1280 not yet reported their PL_FLAG_BORN event. Ignore
1281 BORN events for an already-known LWP. */
1282 if (!in_thread_list (wptid))
1283 {
1284 if (debug_fbsd_lwp)
1285 fprintf_unfiltered (gdb_stdlog,
1286 "FLWP: adding thread for LWP %u\n",
1287 pl.pl_lwpid);
1288 add_thread (wptid);
1289 }
1290 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
1291 return wptid;
1292 }
1293 #endif
1294
1295 #ifdef TDP_RFPPWAIT
1296 if (pl.pl_flags & PL_FLAG_FORKED)
1297 {
1298 #ifndef PTRACE_VFORK
1299 struct kinfo_proc kp;
1300 #endif
1301 ptid_t child_ptid;
1302 pid_t child;
1303
1304 child = pl.pl_child_pid;
1305 ourstatus->kind = TARGET_WAITKIND_FORKED;
1306 #ifdef PTRACE_VFORK
1307 if (pl.pl_flags & PL_FLAG_VFORKED)
1308 ourstatus->kind = TARGET_WAITKIND_VFORKED;
1309 #endif
1310
1311 /* Make sure the other end of the fork is stopped too. */
1312 child_ptid = fbsd_is_child_pending (child);
1313 if (ptid_equal (child_ptid, null_ptid))
1314 {
1315 pid = waitpid (child, &status, 0);
1316 if (pid == -1)
1317 perror_with_name (("waitpid"));
1318
1319 gdb_assert (pid == child);
1320
1321 if (ptrace (PT_LWPINFO, child, (caddr_t)&pl, sizeof pl) == -1)
1322 perror_with_name (("ptrace"));
1323
1324 gdb_assert (pl.pl_flags & PL_FLAG_CHILD);
1325 child_ptid = ptid_build (child, pl.pl_lwpid, 0);
1326 }
1327
1328 /* Enable additional events on the child process. */
1329 fbsd_enable_proc_events (ptid_get_pid (child_ptid));
1330
1331 #ifndef PTRACE_VFORK
1332 /* For vfork, the child process will have the P_PPWAIT
1333 flag set. */
1334 if (fbsd_fetch_kinfo_proc (child, &kp))
1335 {
1336 if (kp.ki_flag & P_PPWAIT)
1337 ourstatus->kind = TARGET_WAITKIND_VFORKED;
1338 }
1339 else
1340 warning (_("Failed to fetch process information"));
1341 #endif
1342 ourstatus->value.related_pid = child_ptid;
1343
1344 return wptid;
1345 }
1346
1347 if (pl.pl_flags & PL_FLAG_CHILD)
1348 {
1349 /* Remember that this child forked, but do not report it
1350 until the parent reports its corresponding fork
1351 event. */
1352 fbsd_remember_child (wptid);
1353 continue;
1354 }
1355
1356 #ifdef PTRACE_VFORK
1357 if (pl.pl_flags & PL_FLAG_VFORK_DONE)
1358 {
1359 ourstatus->kind = TARGET_WAITKIND_VFORK_DONE;
1360 return wptid;
1361 }
1362 #endif
1363 #endif
1364
1365 #ifdef PL_FLAG_EXEC
1366 if (pl.pl_flags & PL_FLAG_EXEC)
1367 {
1368 ourstatus->kind = TARGET_WAITKIND_EXECD;
1369 ourstatus->value.execd_pathname
1370 = xstrdup (fbsd_pid_to_exec_file (NULL, pid));
1371 return wptid;
1372 }
1373 #endif
1374
1375 /* Note that PL_FLAG_SCE is set for any event reported while
1376 a thread is executing a system call in the kernel. In
1377 particular, signals that interrupt a sleep in a system
1378 call will report this flag as part of their event. Stops
1379 explicitly for system call entry and exit always use
1380 SIGTRAP, so only treat SIGTRAP events as system call
1381 entry/exit events. */
1382 if (pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX)
1383 && ourstatus->value.sig == SIGTRAP)
1384 {
1385 #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE
1386 if (catch_syscall_enabled ())
1387 {
1388 if (catching_syscall_number (pl.pl_syscall_code))
1389 {
1390 if (pl.pl_flags & PL_FLAG_SCE)
1391 ourstatus->kind = TARGET_WAITKIND_SYSCALL_ENTRY;
1392 else
1393 ourstatus->kind = TARGET_WAITKIND_SYSCALL_RETURN;
1394 ourstatus->value.syscall_number = pl.pl_syscall_code;
1395 return wptid;
1396 }
1397 }
1398 #endif
1399 /* If the core isn't interested in this event, just
1400 continue the process explicitly and wait for another
1401 event. Note that PT_SYSCALL is "sticky" on FreeBSD
1402 and once system call stops are enabled on a process
1403 it stops for all system call entries and exits. */
1404 if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1)
1405 perror_with_name (("ptrace"));
1406 continue;
1407 }
1408 }
1409 return wptid;
1410 }
1411 }
1412
1413 #ifdef TDP_RFPPWAIT
1414 /* Target hook for follow_fork. On entry and at return inferior_ptid is
1415 the ptid of the followed inferior. */
1416
1417 static int
1418 fbsd_follow_fork (struct target_ops *ops, int follow_child,
1419 int detach_fork)
1420 {
1421 if (!follow_child && detach_fork)
1422 {
1423 struct thread_info *tp = inferior_thread ();
1424 pid_t child_pid = ptid_get_pid (tp->pending_follow.value.related_pid);
1425
1426 /* Breakpoints have already been detached from the child by
1427 infrun.c. */
1428
1429 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
1430 perror_with_name (("ptrace"));
1431
1432 #ifndef PTRACE_VFORK
1433 if (tp->pending_follow.kind == TARGET_WAITKIND_VFORKED)
1434 {
1435 /* We can't insert breakpoints until the child process has
1436 finished with the shared memory region. The parent
1437 process doesn't wait for the child process to exit or
1438 exec until after it has been resumed from the ptrace stop
1439 to report the fork. Once it has been resumed it doesn't
1440 stop again before returning to userland, so there is no
1441 reliable way to wait on the parent.
1442
1443 We can't stay attached to the child to wait for an exec
1444 or exit because it may invoke ptrace(PT_TRACE_ME)
1445 (e.g. if the parent process is a debugger forking a new
1446 child process).
1447
1448 In the end, the best we can do is to make sure it runs
1449 for a little while. Hopefully it will be out of range of
1450 any breakpoints we reinsert. Usually this is only the
1451 single-step breakpoint at vfork's return point. */
1452
1453 usleep (10000);
1454
1455 /* Schedule a fake VFORK_DONE event to report on the next
1456 wait. */
1457 fbsd_add_vfork_done (inferior_ptid);
1458 }
1459 #endif
1460 }
1461
1462 return 0;
1463 }
1464
1465 static int
1466 fbsd_insert_fork_catchpoint (struct target_ops *self, int pid)
1467 {
1468 return 0;
1469 }
1470
1471 static int
1472 fbsd_remove_fork_catchpoint (struct target_ops *self, int pid)
1473 {
1474 return 0;
1475 }
1476
1477 static int
1478 fbsd_insert_vfork_catchpoint (struct target_ops *self, int pid)
1479 {
1480 return 0;
1481 }
1482
1483 static int
1484 fbsd_remove_vfork_catchpoint (struct target_ops *self, int pid)
1485 {
1486 return 0;
1487 }
1488 #endif
1489
1490 /* Implement the "to_post_startup_inferior" target_ops method. */
1491
1492 static void
1493 fbsd_post_startup_inferior (struct target_ops *self, ptid_t pid)
1494 {
1495 fbsd_enable_proc_events (ptid_get_pid (pid));
1496 }
1497
1498 /* Implement the "to_post_attach" target_ops method. */
1499
1500 static void
1501 fbsd_post_attach (struct target_ops *self, int pid)
1502 {
1503 fbsd_enable_proc_events (pid);
1504 fbsd_add_threads (pid);
1505 }
1506
1507 #ifdef PL_FLAG_EXEC
1508 /* If the FreeBSD kernel supports PL_FLAG_EXEC, then traced processes
1509 will always stop after exec. */
1510
1511 static int
1512 fbsd_insert_exec_catchpoint (struct target_ops *self, int pid)
1513 {
1514 return 0;
1515 }
1516
1517 static int
1518 fbsd_remove_exec_catchpoint (struct target_ops *self, int pid)
1519 {
1520 return 0;
1521 }
1522 #endif
1523
1524 #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE
1525 static int
1526 fbsd_set_syscall_catchpoint (struct target_ops *self, int pid, bool needed,
1527 int any_count,
1528 gdb::array_view<const int> syscall_counts)
1529 {
1530
1531 /* Ignore the arguments. inf-ptrace.c will use PT_SYSCALL which
1532 will catch all system call entries and exits. The system calls
1533 are filtered by GDB rather than the kernel. */
1534 return 0;
1535 }
1536 #endif
1537 #endif
1538
1539 void
1540 fbsd_nat_add_target (struct target_ops *t)
1541 {
1542 t->to_pid_to_exec_file = fbsd_pid_to_exec_file;
1543 t->to_find_memory_regions = fbsd_find_memory_regions;
1544 t->to_info_proc = fbsd_info_proc;
1545 #ifdef KERN_PROC_AUXV
1546 super_xfer_partial = t->to_xfer_partial;
1547 t->to_xfer_partial = fbsd_xfer_partial;
1548 #endif
1549 #ifdef PT_LWPINFO
1550 t->to_thread_alive = fbsd_thread_alive;
1551 t->to_pid_to_str = fbsd_pid_to_str;
1552 #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME
1553 t->to_thread_name = fbsd_thread_name;
1554 #endif
1555 t->to_update_thread_list = fbsd_update_thread_list;
1556 t->to_has_thread_control = tc_schedlock;
1557 super_resume = t->to_resume;
1558 t->to_resume = fbsd_resume;
1559 super_wait = t->to_wait;
1560 t->to_wait = fbsd_wait;
1561 t->to_post_startup_inferior = fbsd_post_startup_inferior;
1562 t->to_post_attach = fbsd_post_attach;
1563 #ifdef TDP_RFPPWAIT
1564 t->to_follow_fork = fbsd_follow_fork;
1565 t->to_insert_fork_catchpoint = fbsd_insert_fork_catchpoint;
1566 t->to_remove_fork_catchpoint = fbsd_remove_fork_catchpoint;
1567 t->to_insert_vfork_catchpoint = fbsd_insert_vfork_catchpoint;
1568 t->to_remove_vfork_catchpoint = fbsd_remove_vfork_catchpoint;
1569 #endif
1570 #ifdef PL_FLAG_EXEC
1571 t->to_insert_exec_catchpoint = fbsd_insert_exec_catchpoint;
1572 t->to_remove_exec_catchpoint = fbsd_remove_exec_catchpoint;
1573 #endif
1574 #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE
1575 t->to_set_syscall_catchpoint = fbsd_set_syscall_catchpoint;
1576 #endif
1577 #endif
1578 add_target (t);
1579 }
1580
1581 void
1582 _initialize_fbsd_nat (void)
1583 {
1584 #ifdef PT_LWPINFO
1585 add_setshow_boolean_cmd ("fbsd-lwp", class_maintenance,
1586 &debug_fbsd_lwp, _("\
1587 Set debugging of FreeBSD lwp module."), _("\
1588 Show debugging of FreeBSD lwp module."), _("\
1589 Enables printf debugging output."),
1590 NULL,
1591 &show_fbsd_lwp_debug,
1592 &setdebuglist, &showdebuglist);
1593 add_setshow_boolean_cmd ("fbsd-nat", class_maintenance,
1594 &debug_fbsd_nat, _("\
1595 Set debugging of FreeBSD native target."), _("\
1596 Show debugging of FreeBSD native target."), _("\
1597 Enables printf debugging output."),
1598 NULL,
1599 &show_fbsd_nat_debug,
1600 &setdebuglist, &showdebuglist);
1601 #endif
1602 }