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