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