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