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