[ARM] Fix endless recursion on calculating CPRC candidate
[deliverable/binutils-gdb.git] / gdb / fbsd-nat.c
CommitLineData
578c1c03
MK
1/* Native-dependent code for FreeBSD.
2
618f726f 3 Copyright (C) 2002-2016 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"
21#include "gdbcore.h"
22#include "inferior.h"
23#include "regcache.h"
24#include "regset.h"
6e9567fe 25#include "gdbcmd.h"
2020b7ab 26#include "gdbthread.h"
cea6e4f1 27#include "gdb_wait.h"
578c1c03 28#include <sys/types.h>
68b9939a 29#include <sys/procfs.h>
e58e05d6 30#include <sys/ptrace.h>
68b9939a 31#include <sys/sysctl.h>
25268153
JB
32#ifdef HAVE_KINFO_GETVMMAP
33#include <sys/user.h>
34#include <libutil.h>
35#endif
578c1c03
MK
36
37#include "elf-bfd.h"
38#include "fbsd-nat.h"
39
766062f6 40/* Return the name of a file that can be opened to get the symbols for
578c1c03
MK
41 the child process identified by PID. */
42
8f60fe01 43static char *
8dd27370 44fbsd_pid_to_exec_file (struct target_ops *self, int pid)
578c1c03 45{
f2feec98 46 ssize_t len;
b4ab256d
HZ
47 static char buf[PATH_MAX];
48 char name[PATH_MAX];
578c1c03 49
68b9939a 50#ifdef KERN_PROC_PATHNAME
f2feec98 51 size_t buflen;
68b9939a 52 int mib[4];
578c1c03 53
68b9939a
MK
54 mib[0] = CTL_KERN;
55 mib[1] = KERN_PROC;
56 mib[2] = KERN_PROC_PATHNAME;
57 mib[3] = pid;
f2feec98
JB
58 buflen = sizeof buf;
59 if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0)
578c1c03 60 return buf;
68b9939a 61#endif
578c1c03 62
b4ab256d
HZ
63 xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
64 len = readlink (name, buf, PATH_MAX - 1);
65 if (len != -1)
68b9939a 66 {
b4ab256d
HZ
67 buf[len] = '\0';
68 return buf;
68b9939a
MK
69 }
70
b4ab256d 71 return NULL;
578c1c03
MK
72}
73
25268153
JB
74#ifdef HAVE_KINFO_GETVMMAP
75/* Iterate over all the memory regions in the current inferior,
76 calling FUNC for each memory region. OBFD is passed as the last
77 argument to FUNC. */
78
8f60fe01 79static int
25268153
JB
80fbsd_find_memory_regions (struct target_ops *self,
81 find_memory_region_ftype func, void *obfd)
82{
83 pid_t pid = ptid_get_pid (inferior_ptid);
84 struct kinfo_vmentry *vmentl, *kve;
85 uint64_t size;
86 struct cleanup *cleanup;
87 int i, nitems;
88
89 vmentl = kinfo_getvmmap (pid, &nitems);
90 if (vmentl == NULL)
91 perror_with_name (_("Couldn't fetch VM map entries."));
92 cleanup = make_cleanup (free, vmentl);
93
94 for (i = 0; i < nitems; i++)
95 {
96 kve = &vmentl[i];
97
98 /* Skip unreadable segments and those where MAP_NOCORE has been set. */
99 if (!(kve->kve_protection & KVME_PROT_READ)
100 || kve->kve_flags & KVME_FLAG_NOCOREDUMP)
101 continue;
102
103 /* Skip segments with an invalid type. */
104 if (kve->kve_type != KVME_TYPE_DEFAULT
105 && kve->kve_type != KVME_TYPE_VNODE
106 && kve->kve_type != KVME_TYPE_SWAP
107 && kve->kve_type != KVME_TYPE_PHYS)
108 continue;
109
110 size = kve->kve_end - kve->kve_start;
111 if (info_verbose)
112 {
113 fprintf_filtered (gdb_stdout,
114 "Save segment, %ld bytes at %s (%c%c%c)\n",
115 (long) size,
116 paddress (target_gdbarch (), kve->kve_start),
117 kve->kve_protection & KVME_PROT_READ ? 'r' : '-',
118 kve->kve_protection & KVME_PROT_WRITE ? 'w' : '-',
119 kve->kve_protection & KVME_PROT_EXEC ? 'x' : '-');
120 }
121
122 /* Invoke the callback function to create the corefile segment.
123 Pass MODIFIED as true, we do not know the real modification state. */
124 func (kve->kve_start, size, kve->kve_protection & KVME_PROT_READ,
125 kve->kve_protection & KVME_PROT_WRITE,
126 kve->kve_protection & KVME_PROT_EXEC, 1, obfd);
127 }
128 do_cleanups (cleanup);
129 return 0;
130}
131#else
578c1c03
MK
132static int
133fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end,
134 char *protection)
135{
136 /* FreeBSD 5.1-RELEASE uses a 256-byte buffer. */
137 char buf[256];
138 int resident, privateresident;
139 unsigned long obj;
140 int ret = EOF;
141
142 /* As of FreeBSD 5.0-RELEASE, the layout is described in
143 /usr/src/sys/fs/procfs/procfs_map.c. Somewhere in 5.1-CURRENT a
144 new column was added to the procfs map. Therefore we can't use
145 fscanf since we need to support older releases too. */
146 if (fgets (buf, sizeof buf, mapfile) != NULL)
147 ret = sscanf (buf, "%lx %lx %d %d %lx %s", start, end,
148 &resident, &privateresident, &obj, protection);
149
150 return (ret != 0 && ret != EOF);
151}
152
153/* Iterate over all the memory regions in the current inferior,
154 calling FUNC for each memory region. OBFD is passed as the last
155 argument to FUNC. */
156
8f60fe01 157static int
2e73927c
TT
158fbsd_find_memory_regions (struct target_ops *self,
159 find_memory_region_ftype func, void *obfd)
578c1c03
MK
160{
161 pid_t pid = ptid_get_pid (inferior_ptid);
162 char *mapfilename;
163 FILE *mapfile;
164 unsigned long start, end, size;
165 char protection[4];
166 int read, write, exec;
7c8a8b04 167 struct cleanup *cleanup;
578c1c03
MK
168
169 mapfilename = xstrprintf ("/proc/%ld/map", (long) pid);
7c8a8b04 170 cleanup = make_cleanup (xfree, mapfilename);
578c1c03
MK
171 mapfile = fopen (mapfilename, "r");
172 if (mapfile == NULL)
8a3fe4f8 173 error (_("Couldn't open %s."), mapfilename);
7c8a8b04 174 make_cleanup_fclose (mapfile);
578c1c03
MK
175
176 if (info_verbose)
177 fprintf_filtered (gdb_stdout,
178 "Reading memory regions from %s\n", mapfilename);
179
180 /* Now iterate until end-of-file. */
181 while (fbsd_read_mapping (mapfile, &start, &end, &protection[0]))
182 {
183 size = end - start;
184
185 read = (strchr (protection, 'r') != 0);
186 write = (strchr (protection, 'w') != 0);
187 exec = (strchr (protection, 'x') != 0);
188
189 if (info_verbose)
190 {
191 fprintf_filtered (gdb_stdout,
5af949e3 192 "Save segment, %ld bytes at %s (%c%c%c)\n",
f5656ead 193 size, paddress (target_gdbarch (), start),
578c1c03
MK
194 read ? 'r' : '-',
195 write ? 'w' : '-',
196 exec ? 'x' : '-');
197 }
198
4f69f4c2
JK
199 /* Invoke the callback function to create the corefile segment.
200 Pass MODIFIED as true, we do not know the real modification state. */
201 func (start, size, read, write, exec, 1, obfd);
578c1c03
MK
202 }
203
7c8a8b04 204 do_cleanups (cleanup);
578c1c03
MK
205 return 0;
206}
25268153 207#endif
8f60fe01 208
7697fc9e
JB
209#ifdef KERN_PROC_AUXV
210static enum target_xfer_status (*super_xfer_partial) (struct target_ops *ops,
211 enum target_object object,
212 const char *annex,
213 gdb_byte *readbuf,
214 const gdb_byte *writebuf,
215 ULONGEST offset,
216 ULONGEST len,
217 ULONGEST *xfered_len);
218
219/* Implement the "to_xfer_partial target_ops" method. */
220
221static enum target_xfer_status
222fbsd_xfer_partial (struct target_ops *ops, enum target_object object,
223 const char *annex, gdb_byte *readbuf,
224 const gdb_byte *writebuf,
225 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
226{
227 pid_t pid = ptid_get_pid (inferior_ptid);
228
229 switch (object)
230 {
231 case TARGET_OBJECT_AUXV:
232 {
233 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
234 unsigned char *buf;
235 size_t buflen;
236 int mib[4];
237
238 if (writebuf != NULL)
239 return TARGET_XFER_E_IO;
240 mib[0] = CTL_KERN;
241 mib[1] = KERN_PROC;
242 mib[2] = KERN_PROC_AUXV;
243 mib[3] = pid;
244 if (offset == 0)
245 {
246 buf = readbuf;
247 buflen = len;
248 }
249 else
250 {
251 buflen = offset + len;
252 buf = XCNEWVEC (unsigned char, buflen);
253 cleanup = make_cleanup (xfree, buf);
254 }
255 if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0)
256 {
257 if (offset != 0)
258 {
259 if (buflen > offset)
260 {
261 buflen -= offset;
262 memcpy (readbuf, buf + offset, buflen);
263 }
264 else
265 buflen = 0;
266 }
267 do_cleanups (cleanup);
268 *xfered_len = buflen;
269 return (buflen == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
270 }
271 do_cleanups (cleanup);
272 return TARGET_XFER_E_IO;
273 }
274 default:
275 return super_xfer_partial (ops, object, annex, readbuf, writebuf, offset,
276 len, xfered_len);
277 }
278}
279#endif
280
e58e05d6 281#ifdef PT_LWPINFO
6e9567fe
JB
282static int debug_fbsd_lwp;
283
8607ea63
JB
284static void (*super_resume) (struct target_ops *,
285 ptid_t,
286 int,
287 enum gdb_signal);
e58e05d6
JB
288static ptid_t (*super_wait) (struct target_ops *,
289 ptid_t,
290 struct target_waitstatus *,
291 int);
292
6e9567fe
JB
293static void
294show_fbsd_lwp_debug (struct ui_file *file, int from_tty,
295 struct cmd_list_element *c, const char *value)
296{
297 fprintf_filtered (file, _("Debugging of FreeBSD lwp module is %s.\n"), value);
298}
299
300#if defined(TDP_RFPPWAIT) || defined(HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME)
301/* Fetch the external variant of the kernel's internal process
302 structure for the process PID into KP. */
303
304static void
305fbsd_fetch_kinfo_proc (pid_t pid, struct kinfo_proc *kp)
306{
307 size_t len;
308 int mib[4];
309
310 len = sizeof *kp;
311 mib[0] = CTL_KERN;
312 mib[1] = KERN_PROC;
313 mib[2] = KERN_PROC_PID;
314 mib[3] = pid;
315 if (sysctl (mib, 4, kp, &len, NULL, 0) == -1)
316 perror_with_name (("sysctl"));
317}
318#endif
319
320/*
321 FreeBSD's first thread support was via a "reentrant" version of libc
322 (libc_r) that first shipped in 2.2.7. This library multiplexed all
323 of the threads in a process onto a single kernel thread. This
324 library is supported via the bsd-uthread target.
325
326 FreeBSD 5.1 introduced two new threading libraries that made use of
327 multiple kernel threads. The first (libkse) scheduled M user
328 threads onto N (<= M) kernel threads (LWPs). The second (libthr)
329 bound each user thread to a dedicated kernel thread. libkse shipped
330 as the default threading library (libpthread).
331
332 FreeBSD 5.3 added a libthread_db to abstract the interface across
333 the various thread libraries (libc_r, libkse, and libthr).
334
335 FreeBSD 7.0 switched the default threading library from from libkse
336 to libpthread and removed libc_r.
337
338 FreeBSD 8.0 removed libkse and the in-kernel support for it. The
339 only threading library supported by 8.0 and later is libthr which
340 ties each user thread directly to an LWP. To simplify the
341 implementation, this target only supports LWP-backed threads using
342 ptrace directly rather than libthread_db.
343
344 FreeBSD 11.0 introduced LWP event reporting via PT_LWP_EVENTS.
345*/
346
347/* Return true if PTID is still active in the inferior. */
348
349static int
350fbsd_thread_alive (struct target_ops *ops, ptid_t ptid)
351{
352 if (ptid_lwp_p (ptid))
353 {
354 struct ptrace_lwpinfo pl;
355
356 if (ptrace (PT_LWPINFO, ptid_get_lwp (ptid), (caddr_t) &pl, sizeof pl)
357 == -1)
358 return 0;
359#ifdef PL_FLAG_EXITED
360 if (pl.pl_flags & PL_FLAG_EXITED)
361 return 0;
362#endif
363 }
364
365 return 1;
366}
367
368/* Convert PTID to a string. Returns the string in a static
369 buffer. */
370
371static char *
372fbsd_pid_to_str (struct target_ops *ops, ptid_t ptid)
373{
374 lwpid_t lwp;
375
376 lwp = ptid_get_lwp (ptid);
377 if (lwp != 0)
378 {
379 static char buf[64];
380 int pid = ptid_get_pid (ptid);
381
b2bae2f7 382 xsnprintf (buf, sizeof buf, "LWP %d of process %d", lwp, pid);
6e9567fe
JB
383 return buf;
384 }
385
386 return normal_pid_to_str (ptid);
387}
388
389#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME
390/* Return the name assigned to a thread by an application. Returns
391 the string in a static buffer. */
392
393static const char *
394fbsd_thread_name (struct target_ops *self, struct thread_info *thr)
395{
396 struct ptrace_lwpinfo pl;
397 struct kinfo_proc kp;
398 int pid = ptid_get_pid (thr->ptid);
399 long lwp = ptid_get_lwp (thr->ptid);
400 static char buf[sizeof pl.pl_tdname + 1];
401
402 /* Note that ptrace_lwpinfo returns the process command in pl_tdname
403 if a name has not been set explicitly. Return a NULL name in
404 that case. */
405 fbsd_fetch_kinfo_proc (pid, &kp);
406 if (ptrace (PT_LWPINFO, lwp, (caddr_t) &pl, sizeof pl) == -1)
407 perror_with_name (("ptrace"));
408 if (strcmp (kp.ki_comm, pl.pl_tdname) == 0)
409 return NULL;
410 xsnprintf (buf, sizeof buf, "%s", pl.pl_tdname);
411 return buf;
412}
413#endif
414
415#ifdef PT_LWP_EVENTS
416/* Enable LWP events for a specific process.
417
418 To catch LWP events, PT_LWP_EVENTS is set on every traced process.
419 This enables stops on the birth for new LWPs (excluding the "main" LWP)
420 and the death of LWPs (excluding the last LWP in a process). Note
421 that unlike fork events, the LWP that creates a new LWP does not
422 report an event. */
423
424static void
425fbsd_enable_lwp_events (pid_t pid)
426{
427 if (ptrace (PT_LWP_EVENTS, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
428 perror_with_name (("ptrace"));
429}
430#endif
431
432/* Add threads for any new LWPs in a process.
433
434 When LWP events are used, this function is only used to detect existing
435 threads when attaching to a process. On older systems, this function is
436 called to discover new threads each time the thread list is updated. */
437
438static void
439fbsd_add_threads (pid_t pid)
440{
441 struct cleanup *cleanup;
442 lwpid_t *lwps;
443 int i, nlwps;
444
445 gdb_assert (!in_thread_list (pid_to_ptid (pid)));
446 nlwps = ptrace (PT_GETNUMLWPS, pid, NULL, 0);
447 if (nlwps == -1)
448 perror_with_name (("ptrace"));
449
450 lwps = XCNEWVEC (lwpid_t, nlwps);
451 cleanup = make_cleanup (xfree, lwps);
452
453 nlwps = ptrace (PT_GETLWPLIST, pid, (caddr_t) lwps, nlwps);
454 if (nlwps == -1)
455 perror_with_name (("ptrace"));
456
457 for (i = 0; i < nlwps; i++)
458 {
459 ptid_t ptid = ptid_build (pid, lwps[i], 0);
460
461 if (!in_thread_list (ptid))
462 {
463#ifdef PT_LWP_EVENTS
464 struct ptrace_lwpinfo pl;
465
466 /* Don't add exited threads. Note that this is only called
467 when attaching to a multi-threaded process. */
468 if (ptrace (PT_LWPINFO, lwps[i], (caddr_t) &pl, sizeof pl) == -1)
469 perror_with_name (("ptrace"));
470 if (pl.pl_flags & PL_FLAG_EXITED)
471 continue;
472#endif
473 if (debug_fbsd_lwp)
474 fprintf_unfiltered (gdb_stdlog,
475 "FLWP: adding thread for LWP %u\n",
476 lwps[i]);
477 add_thread (ptid);
478 }
479 }
480 do_cleanups (cleanup);
481}
482
483/* Implement the "to_update_thread_list" target_ops method. */
484
485static void
486fbsd_update_thread_list (struct target_ops *ops)
487{
488#ifdef PT_LWP_EVENTS
489 /* With support for thread events, threads are added/deleted from the
490 list as events are reported, so just try deleting exited threads. */
491 delete_exited_threads ();
492#else
493 prune_threads ();
494
495 fbsd_add_threads (ptid_get_pid (inferior_ptid));
496#endif
497}
498
e58e05d6
JB
499#ifdef TDP_RFPPWAIT
500/*
501 To catch fork events, PT_FOLLOW_FORK is set on every traced process
502 to enable stops on returns from fork or vfork. Note that both the
503 parent and child will always stop, even if system call stops are not
504 enabled.
505
506 After a fork, both the child and parent process will stop and report
507 an event. However, there is no guarantee of order. If the parent
508 reports its stop first, then fbsd_wait explicitly waits for the new
509 child before returning. If the child reports its stop first, then
510 the event is saved on a list and ignored until the parent's stop is
511 reported. fbsd_wait could have been changed to fetch the parent PID
512 of the new child and used that to wait for the parent explicitly.
513 However, if two threads in the parent fork at the same time, then
514 the wait on the parent might return the "wrong" fork event.
515
516 The initial version of PT_FOLLOW_FORK did not set PL_FLAG_CHILD for
517 the new child process. This flag could be inferred by treating any
518 events for an unknown pid as a new child.
519
520 In addition, the initial version of PT_FOLLOW_FORK did not report a
521 stop event for the parent process of a vfork until after the child
522 process executed a new program or exited. The kernel was changed to
523 defer the wait for exit or exec of the child until after posting the
524 stop event shortly after the change to introduce PL_FLAG_CHILD.
525 This could be worked around by reporting a vfork event when the
526 child event posted and ignoring the subsequent event from the
527 parent.
528
529 This implementation requires both of these fixes for simplicity's
530 sake. FreeBSD versions newer than 9.1 contain both fixes.
531*/
532
2c5c2a33 533struct fbsd_fork_info
e58e05d6 534{
2c5c2a33
JB
535 struct fbsd_fork_info *next;
536 ptid_t ptid;
e58e05d6
JB
537};
538
2c5c2a33 539static struct fbsd_fork_info *fbsd_pending_children;
e58e05d6
JB
540
541/* Record a new child process event that is reported before the
542 corresponding fork event in the parent. */
543
544static void
6e9567fe 545fbsd_remember_child (ptid_t pid)
e58e05d6 546{
2c5c2a33 547 struct fbsd_fork_info *info = XCNEW (struct fbsd_fork_info);
e58e05d6 548
2c5c2a33 549 info->ptid = pid;
e58e05d6
JB
550 info->next = fbsd_pending_children;
551 fbsd_pending_children = info;
552}
553
554/* Check for a previously-recorded new child process event for PID.
6e9567fe 555 If one is found, remove it from the list and return the PTID. */
e58e05d6 556
6e9567fe 557static ptid_t
e58e05d6
JB
558fbsd_is_child_pending (pid_t pid)
559{
2c5c2a33 560 struct fbsd_fork_info *info, *prev;
6e9567fe 561 ptid_t ptid;
e58e05d6
JB
562
563 prev = NULL;
564 for (info = fbsd_pending_children; info; prev = info, info = info->next)
565 {
2c5c2a33 566 if (ptid_get_pid (info->ptid) == pid)
e58e05d6
JB
567 {
568 if (prev == NULL)
569 fbsd_pending_children = info->next;
570 else
571 prev->next = info->next;
2c5c2a33 572 ptid = info->ptid;
e58e05d6 573 xfree (info);
6e9567fe 574 return ptid;
e58e05d6
JB
575 }
576 }
6e9567fe 577 return null_ptid;
e58e05d6 578}
2c5c2a33
JB
579
580static struct fbsd_fork_info *fbsd_pending_vfork_done;
581
582/* Record a pending vfork done event. */
583
584static void
585fbsd_add_vfork_done (ptid_t pid)
586{
587 struct fbsd_fork_info *info = XCNEW (struct fbsd_fork_info);
588
589 info->ptid = pid;
590 info->next = fbsd_pending_vfork_done;
591 fbsd_pending_vfork_done = info;
592}
593
594/* Check for a pending vfork done event for a specific PID. */
595
596static int
597fbsd_is_vfork_done_pending (pid_t pid)
598{
599 struct fbsd_fork_info *info;
600
601 for (info = fbsd_pending_vfork_done; info != NULL; info = info->next)
602 {
603 if (ptid_get_pid (info->ptid) == pid)
604 return 1;
605 }
606 return 0;
607}
608
609/* Check for a pending vfork done event. If one is found, remove it
610 from the list and return the PTID. */
611
612static ptid
613fbsd_next_vfork_done (void)
614{
615 struct fbsd_fork_info *info;
616 ptid_t ptid;
617
618 if (fbsd_pending_vfork_done != NULL)
619 {
620 info = fbsd_pending_vfork_done;
621 fbsd_pending_vfork_done = info->next;
622 ptid = info->ptid;
623 xfree (info);
624 return ptid;
625 }
626 return null_ptid;
627}
e58e05d6
JB
628#endif
629
8607ea63
JB
630static int
631resume_one_thread_cb (struct thread_info *tp, void *data)
632{
633 ptid_t *ptid = (ptid_t *) data;
634 int request;
635
636 if (ptid_get_pid (tp->ptid) != ptid_get_pid (*ptid))
637 return 0;
638
639 if (ptid_get_lwp (tp->ptid) == ptid_get_lwp (*ptid))
640 request = PT_RESUME;
641 else
642 request = PT_SUSPEND;
643
644 if (ptrace (request, ptid_get_lwp (tp->ptid), NULL, 0) == -1)
645 perror_with_name (("ptrace"));
646 return 0;
647}
648
649static int
650resume_all_threads_cb (struct thread_info *tp, void *data)
651{
652 ptid_t *filter = (ptid_t *) data;
653
654 if (!ptid_match (tp->ptid, *filter))
655 return 0;
656
657 if (ptrace (PT_RESUME, ptid_get_lwp (tp->ptid), NULL, 0) == -1)
658 perror_with_name (("ptrace"));
659 return 0;
660}
661
662/* Implement the "to_resume" target_ops method. */
663
664static void
665fbsd_resume (struct target_ops *ops,
666 ptid_t ptid, int step, enum gdb_signal signo)
667{
2c5c2a33
JB
668#ifdef TDP_RFPPWAIT
669 pid_t pid;
670
671 /* Don't PT_CONTINUE a process which has a pending vfork done event. */
672 if (ptid_equal (minus_one_ptid, ptid))
673 pid = ptid_get_pid (inferior_ptid);
674 else
675 pid = ptid_get_pid (ptid);
676 if (fbsd_is_vfork_done_pending (pid))
677 return;
678#endif
8607ea63
JB
679
680 if (debug_fbsd_lwp)
681 fprintf_unfiltered (gdb_stdlog,
682 "FLWP: fbsd_resume for ptid (%d, %ld, %ld)\n",
683 ptid_get_pid (ptid), ptid_get_lwp (ptid),
684 ptid_get_tid (ptid));
685 if (ptid_lwp_p (ptid))
686 {
687 /* If ptid is a specific LWP, suspend all other LWPs in the process. */
688 iterate_over_threads (resume_one_thread_cb, &ptid);
689 }
690 else
691 {
692 /* If ptid is a wildcard, resume all matching threads (they won't run
693 until the process is continued however). */
694 iterate_over_threads (resume_all_threads_cb, &ptid);
695 ptid = inferior_ptid;
696 }
697 super_resume (ops, ptid, step, signo);
698}
699
e58e05d6
JB
700/* Wait for the child specified by PTID to do something. Return the
701 process ID of the child, or MINUS_ONE_PTID in case of error; store
702 the status in *OURSTATUS. */
703
704static ptid_t
705fbsd_wait (struct target_ops *ops,
706 ptid_t ptid, struct target_waitstatus *ourstatus,
707 int target_options)
708{
709 ptid_t wptid;
710
711 while (1)
712 {
2c5c2a33
JB
713 wptid = fbsd_next_vfork_done ();
714 if (!ptid_equal (wptid, null_ptid))
715 {
716 ourstatus->kind = TARGET_WAITKIND_VFORK_DONE;
717 return wptid;
718 }
e58e05d6
JB
719 wptid = super_wait (ops, ptid, ourstatus, target_options);
720 if (ourstatus->kind == TARGET_WAITKIND_STOPPED)
721 {
722 struct ptrace_lwpinfo pl;
723 pid_t pid;
724 int status;
725
726 pid = ptid_get_pid (wptid);
6e9567fe 727 if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof pl) == -1)
e58e05d6
JB
728 perror_with_name (("ptrace"));
729
6e9567fe
JB
730 wptid = ptid_build (pid, pl.pl_lwpid, 0);
731
732#ifdef PT_LWP_EVENTS
733 if (pl.pl_flags & PL_FLAG_EXITED)
734 {
735 /* If GDB attaches to a multi-threaded process, exiting
736 threads might be skipped during fbsd_post_attach that
737 have not yet reported their PL_FLAG_EXITED event.
738 Ignore EXITED events for an unknown LWP. */
739 if (in_thread_list (wptid))
740 {
741 if (debug_fbsd_lwp)
742 fprintf_unfiltered (gdb_stdlog,
743 "FLWP: deleting thread for LWP %u\n",
744 pl.pl_lwpid);
745 if (print_thread_events)
746 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str
747 (wptid));
748 delete_thread (wptid);
749 }
750 if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1)
751 perror_with_name (("ptrace"));
752 continue;
753 }
754#endif
755
756 /* Switch to an LWP PTID on the first stop in a new process.
757 This is done after handling PL_FLAG_EXITED to avoid
758 switching to an exited LWP. It is done before checking
759 PL_FLAG_BORN in case the first stop reported after
760 attaching to an existing process is a PL_FLAG_BORN
761 event. */
762 if (in_thread_list (pid_to_ptid (pid)))
763 {
764 if (debug_fbsd_lwp)
765 fprintf_unfiltered (gdb_stdlog,
766 "FLWP: using LWP %u for first thread\n",
767 pl.pl_lwpid);
768 thread_change_ptid (pid_to_ptid (pid), wptid);
769 }
770
771#ifdef PT_LWP_EVENTS
772 if (pl.pl_flags & PL_FLAG_BORN)
773 {
774 /* If GDB attaches to a multi-threaded process, newborn
775 threads might be added by fbsd_add_threads that have
776 not yet reported their PL_FLAG_BORN event. Ignore
777 BORN events for an already-known LWP. */
778 if (!in_thread_list (wptid))
779 {
780 if (debug_fbsd_lwp)
781 fprintf_unfiltered (gdb_stdlog,
782 "FLWP: adding thread for LWP %u\n",
783 pl.pl_lwpid);
784 add_thread (wptid);
785 }
786 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
787 return wptid;
788 }
789#endif
790
e58e05d6
JB
791#ifdef TDP_RFPPWAIT
792 if (pl.pl_flags & PL_FLAG_FORKED)
793 {
794 struct kinfo_proc kp;
6e9567fe 795 ptid_t child_ptid;
e58e05d6
JB
796 pid_t child;
797
798 child = pl.pl_child_pid;
799 ourstatus->kind = TARGET_WAITKIND_FORKED;
e58e05d6
JB
800
801 /* Make sure the other end of the fork is stopped too. */
6e9567fe
JB
802 child_ptid = fbsd_is_child_pending (child);
803 if (ptid_equal (child_ptid, null_ptid))
e58e05d6
JB
804 {
805 pid = waitpid (child, &status, 0);
806 if (pid == -1)
807 perror_with_name (("waitpid"));
808
809 gdb_assert (pid == child);
810
811 if (ptrace (PT_LWPINFO, child, (caddr_t)&pl, sizeof pl) == -1)
812 perror_with_name (("ptrace"));
813
814 gdb_assert (pl.pl_flags & PL_FLAG_CHILD);
6e9567fe 815 child_ptid = ptid_build (child, pl.pl_lwpid, 0);
e58e05d6
JB
816 }
817
818 /* For vfork, the child process will have the P_PPWAIT
819 flag set. */
820 fbsd_fetch_kinfo_proc (child, &kp);
821 if (kp.ki_flag & P_PPWAIT)
822 ourstatus->kind = TARGET_WAITKIND_VFORKED;
6e9567fe 823 ourstatus->value.related_pid = child_ptid;
e58e05d6
JB
824
825 return wptid;
826 }
827
828 if (pl.pl_flags & PL_FLAG_CHILD)
829 {
830 /* Remember that this child forked, but do not report it
831 until the parent reports its corresponding fork
832 event. */
6e9567fe 833 fbsd_remember_child (wptid);
e58e05d6
JB
834 continue;
835 }
836#endif
d2b41ca0
JB
837
838#ifdef PL_FLAG_EXEC
839 if (pl.pl_flags & PL_FLAG_EXEC)
840 {
841 ourstatus->kind = TARGET_WAITKIND_EXECD;
842 ourstatus->value.execd_pathname
843 = xstrdup (fbsd_pid_to_exec_file (NULL, pid));
844 return wptid;
845 }
846#endif
e6cdd38e
JB
847
848 /* Note that PL_FLAG_SCE is set for any event reported while
849 a thread is executing a system call in the kernel. In
850 particular, signals that interrupt a sleep in a system
851 call will report this flag as part of their event. Stops
852 explicitly for system call entry and exit always use
853 SIGTRAP, so only treat SIGTRAP events as system call
854 entry/exit events. */
855 if (pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX)
856 && ourstatus->value.sig == SIGTRAP)
857 {
858#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE
859 if (catch_syscall_enabled ())
860 {
861 if (catching_syscall_number (pl.pl_syscall_code))
862 {
863 if (pl.pl_flags & PL_FLAG_SCE)
864 ourstatus->kind = TARGET_WAITKIND_SYSCALL_ENTRY;
865 else
866 ourstatus->kind = TARGET_WAITKIND_SYSCALL_RETURN;
867 ourstatus->value.syscall_number = pl.pl_syscall_code;
868 return wptid;
869 }
870 }
871#endif
872 /* If the core isn't interested in this event, just
873 continue the process explicitly and wait for another
874 event. Note that PT_SYSCALL is "sticky" on FreeBSD
875 and once system call stops are enabled on a process
876 it stops for all system call entries and exits. */
877 if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1)
878 perror_with_name (("ptrace"));
879 continue;
880 }
e58e05d6
JB
881 }
882 return wptid;
883 }
884}
885
886#ifdef TDP_RFPPWAIT
887/* Target hook for follow_fork. On entry and at return inferior_ptid is
888 the ptid of the followed inferior. */
889
890static int
891fbsd_follow_fork (struct target_ops *ops, int follow_child,
892 int detach_fork)
893{
bb2a62e6 894 if (!follow_child && detach_fork)
e58e05d6
JB
895 {
896 struct thread_info *tp = inferior_thread ();
2c5c2a33 897 int has_vforked = tp->pending_follow.kind == TARGET_WAITKIND_VFORKED;
e58e05d6
JB
898 pid_t child_pid = ptid_get_pid (tp->pending_follow.value.related_pid);
899
900 /* Breakpoints have already been detached from the child by
901 infrun.c. */
902
903 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
904 perror_with_name (("ptrace"));
2c5c2a33
JB
905
906 if (has_vforked)
907 {
908 /* We can't insert breakpoints until the child process has
909 finished with the shared memory region. The parent
910 process doesn't wait for the child process to exit or
911 exec until after it has been resumed from the ptrace stop
912 to report the fork. Once it has been resumed it doesn't
913 stop again before returning to userland, so there is no
914 reliable way to wait on the parent.
915
916 We can't stay attached to the child to wait for an exec
917 or exit because it may invoke ptrace(PT_TRACE_ME)
918 (e.g. if the parent process is a debugger forking a new
919 child process).
920
921 In the end, the best we can do is to make sure it runs
922 for a little while. Hopefully it will be out of range of
923 any breakpoints we reinsert. Usually this is only the
924 single-step breakpoint at vfork's return point. */
925
926 usleep (10000);
927
928 /* Schedule a fake VFORK_DONE event to report on the next
929 wait. */
930 fbsd_add_vfork_done (inferior_ptid);
931 }
e58e05d6
JB
932 }
933
934 return 0;
935}
936
937static int
938fbsd_insert_fork_catchpoint (struct target_ops *self, int pid)
939{
940 return 0;
941}
942
943static int
944fbsd_remove_fork_catchpoint (struct target_ops *self, int pid)
945{
946 return 0;
947}
948
949static int
950fbsd_insert_vfork_catchpoint (struct target_ops *self, int pid)
951{
952 return 0;
953}
954
955static int
956fbsd_remove_vfork_catchpoint (struct target_ops *self, int pid)
957{
958 return 0;
959}
960
961/* Enable fork tracing for a specific process.
962
963 To catch fork events, PT_FOLLOW_FORK is set on every traced process
964 to enable stops on returns from fork or vfork. Note that both the
965 parent and child will always stop, even if system call stops are
966 not enabled. */
967
968static void
969fbsd_enable_follow_fork (pid_t pid)
970{
971 if (ptrace (PT_FOLLOW_FORK, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
972 perror_with_name (("ptrace"));
973}
6e9567fe 974#endif
e58e05d6
JB
975
976/* Implement the "to_post_startup_inferior" target_ops method. */
977
978static void
979fbsd_post_startup_inferior (struct target_ops *self, ptid_t pid)
980{
6e9567fe 981#ifdef TDP_RFPPWAIT
e58e05d6 982 fbsd_enable_follow_fork (ptid_get_pid (pid));
6e9567fe
JB
983#endif
984#ifdef PT_LWP_EVENTS
985 fbsd_enable_lwp_events (ptid_get_pid (pid));
986#endif
e58e05d6
JB
987}
988
989/* Implement the "to_post_attach" target_ops method. */
990
991static void
992fbsd_post_attach (struct target_ops *self, int pid)
993{
6e9567fe 994#ifdef TDP_RFPPWAIT
e58e05d6 995 fbsd_enable_follow_fork (pid);
e58e05d6 996#endif
6e9567fe
JB
997#ifdef PT_LWP_EVENTS
998 fbsd_enable_lwp_events (pid);
999#endif
1000 fbsd_add_threads (pid);
1001}
d2b41ca0
JB
1002
1003#ifdef PL_FLAG_EXEC
1004/* If the FreeBSD kernel supports PL_FLAG_EXEC, then traced processes
1005 will always stop after exec. */
1006
1007static int
1008fbsd_insert_exec_catchpoint (struct target_ops *self, int pid)
1009{
1010 return 0;
1011}
1012
1013static int
1014fbsd_remove_exec_catchpoint (struct target_ops *self, int pid)
1015{
1016 return 0;
1017}
1018#endif
e6cdd38e
JB
1019
1020#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE
1021static int
1022fbsd_set_syscall_catchpoint (struct target_ops *self, int pid, int needed,
1023 int any_count, int table_size, int *table)
1024{
1025
1026 /* Ignore the arguments. inf-ptrace.c will use PT_SYSCALL which
1027 will catch all system call entries and exits. The system calls
1028 are filtered by GDB rather than the kernel. */
1029 return 0;
1030}
1031#endif
e58e05d6
JB
1032#endif
1033
8f60fe01
JB
1034void
1035fbsd_nat_add_target (struct target_ops *t)
1036{
1037 t->to_pid_to_exec_file = fbsd_pid_to_exec_file;
1038 t->to_find_memory_regions = fbsd_find_memory_regions;
7697fc9e
JB
1039#ifdef KERN_PROC_AUXV
1040 super_xfer_partial = t->to_xfer_partial;
1041 t->to_xfer_partial = fbsd_xfer_partial;
1042#endif
e58e05d6 1043#ifdef PT_LWPINFO
6e9567fe
JB
1044 t->to_thread_alive = fbsd_thread_alive;
1045 t->to_pid_to_str = fbsd_pid_to_str;
1046#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME
1047 t->to_thread_name = fbsd_thread_name;
1048#endif
1049 t->to_update_thread_list = fbsd_update_thread_list;
1050 t->to_has_thread_control = tc_schedlock;
1051 super_resume = t->to_resume;
1052 t->to_resume = fbsd_resume;
e58e05d6
JB
1053 super_wait = t->to_wait;
1054 t->to_wait = fbsd_wait;
6e9567fe
JB
1055 t->to_post_startup_inferior = fbsd_post_startup_inferior;
1056 t->to_post_attach = fbsd_post_attach;
e58e05d6
JB
1057#ifdef TDP_RFPPWAIT
1058 t->to_follow_fork = fbsd_follow_fork;
1059 t->to_insert_fork_catchpoint = fbsd_insert_fork_catchpoint;
1060 t->to_remove_fork_catchpoint = fbsd_remove_fork_catchpoint;
1061 t->to_insert_vfork_catchpoint = fbsd_insert_vfork_catchpoint;
1062 t->to_remove_vfork_catchpoint = fbsd_remove_vfork_catchpoint;
e58e05d6 1063#endif
d2b41ca0
JB
1064#ifdef PL_FLAG_EXEC
1065 t->to_insert_exec_catchpoint = fbsd_insert_exec_catchpoint;
1066 t->to_remove_exec_catchpoint = fbsd_remove_exec_catchpoint;
1067#endif
e6cdd38e
JB
1068#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE
1069 t->to_set_syscall_catchpoint = fbsd_set_syscall_catchpoint;
1070#endif
e58e05d6 1071#endif
8f60fe01
JB
1072 add_target (t);
1073}
6e9567fe
JB
1074
1075/* Provide a prototype to silence -Wmissing-prototypes. */
1076extern initialize_file_ftype _initialize_fbsd_nat;
1077
1078void
1079_initialize_fbsd_nat (void)
1080{
1081#ifdef PT_LWPINFO
1082 add_setshow_boolean_cmd ("fbsd-lwp", class_maintenance,
1083 &debug_fbsd_lwp, _("\
1084Set debugging of FreeBSD lwp module."), _("\
1085Show debugging of FreeBSD lwp module."), _("\
1086Enables printf debugging output."),
1087 NULL,
1088 &show_fbsd_lwp_debug,
1089 &setdebuglist, &showdebuglist);
1090#endif
1091}
This page took 1.219992 seconds and 4 git commands to generate.