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