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