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