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