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