]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/fbsd-nat.c
GDB copyright headers update after running GDB's copyright.py script.
[thirdparty/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"
2020b7ab 25#include "gdbthread.h"
cea6e4f1 26#include "gdb_wait.h"
578c1c03 27#include <sys/types.h>
68b9939a 28#include <sys/procfs.h>
e58e05d6 29#include <sys/ptrace.h>
68b9939a 30#include <sys/sysctl.h>
25268153
JB
31#ifdef HAVE_KINFO_GETVMMAP
32#include <sys/user.h>
33#include <libutil.h>
34#endif
578c1c03
MK
35
36#include "elf-bfd.h"
37#include "fbsd-nat.h"
38
766062f6 39/* Return the name of a file that can be opened to get the symbols for
578c1c03
MK
40 the child process identified by PID. */
41
8f60fe01 42static char *
8dd27370 43fbsd_pid_to_exec_file (struct target_ops *self, int pid)
578c1c03 44{
b4ab256d
HZ
45 ssize_t len = PATH_MAX;
46 static char buf[PATH_MAX];
47 char name[PATH_MAX];
578c1c03 48
68b9939a
MK
49#ifdef KERN_PROC_PATHNAME
50 int mib[4];
578c1c03 51
68b9939a
MK
52 mib[0] = CTL_KERN;
53 mib[1] = KERN_PROC;
54 mib[2] = KERN_PROC_PATHNAME;
55 mib[3] = pid;
56 if (sysctl (mib, 4, buf, &len, NULL, 0) == 0)
578c1c03 57 return buf;
68b9939a 58#endif
578c1c03 59
b4ab256d
HZ
60 xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
61 len = readlink (name, buf, PATH_MAX - 1);
62 if (len != -1)
68b9939a 63 {
b4ab256d
HZ
64 buf[len] = '\0';
65 return buf;
68b9939a
MK
66 }
67
b4ab256d 68 return NULL;
578c1c03
MK
69}
70
25268153
JB
71#ifdef HAVE_KINFO_GETVMMAP
72/* Iterate over all the memory regions in the current inferior,
73 calling FUNC for each memory region. OBFD is passed as the last
74 argument to FUNC. */
75
8f60fe01 76static int
25268153
JB
77fbsd_find_memory_regions (struct target_ops *self,
78 find_memory_region_ftype func, void *obfd)
79{
80 pid_t pid = ptid_get_pid (inferior_ptid);
81 struct kinfo_vmentry *vmentl, *kve;
82 uint64_t size;
83 struct cleanup *cleanup;
84 int i, nitems;
85
86 vmentl = kinfo_getvmmap (pid, &nitems);
87 if (vmentl == NULL)
88 perror_with_name (_("Couldn't fetch VM map entries."));
89 cleanup = make_cleanup (free, vmentl);
90
91 for (i = 0; i < nitems; i++)
92 {
93 kve = &vmentl[i];
94
95 /* Skip unreadable segments and those where MAP_NOCORE has been set. */
96 if (!(kve->kve_protection & KVME_PROT_READ)
97 || kve->kve_flags & KVME_FLAG_NOCOREDUMP)
98 continue;
99
100 /* Skip segments with an invalid type. */
101 if (kve->kve_type != KVME_TYPE_DEFAULT
102 && kve->kve_type != KVME_TYPE_VNODE
103 && kve->kve_type != KVME_TYPE_SWAP
104 && kve->kve_type != KVME_TYPE_PHYS)
105 continue;
106
107 size = kve->kve_end - kve->kve_start;
108 if (info_verbose)
109 {
110 fprintf_filtered (gdb_stdout,
111 "Save segment, %ld bytes at %s (%c%c%c)\n",
112 (long) size,
113 paddress (target_gdbarch (), kve->kve_start),
114 kve->kve_protection & KVME_PROT_READ ? 'r' : '-',
115 kve->kve_protection & KVME_PROT_WRITE ? 'w' : '-',
116 kve->kve_protection & KVME_PROT_EXEC ? 'x' : '-');
117 }
118
119 /* Invoke the callback function to create the corefile segment.
120 Pass MODIFIED as true, we do not know the real modification state. */
121 func (kve->kve_start, size, kve->kve_protection & KVME_PROT_READ,
122 kve->kve_protection & KVME_PROT_WRITE,
123 kve->kve_protection & KVME_PROT_EXEC, 1, obfd);
124 }
125 do_cleanups (cleanup);
126 return 0;
127}
128#else
578c1c03
MK
129static int
130fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end,
131 char *protection)
132{
133 /* FreeBSD 5.1-RELEASE uses a 256-byte buffer. */
134 char buf[256];
135 int resident, privateresident;
136 unsigned long obj;
137 int ret = EOF;
138
139 /* As of FreeBSD 5.0-RELEASE, the layout is described in
140 /usr/src/sys/fs/procfs/procfs_map.c. Somewhere in 5.1-CURRENT a
141 new column was added to the procfs map. Therefore we can't use
142 fscanf since we need to support older releases too. */
143 if (fgets (buf, sizeof buf, mapfile) != NULL)
144 ret = sscanf (buf, "%lx %lx %d %d %lx %s", start, end,
145 &resident, &privateresident, &obj, protection);
146
147 return (ret != 0 && ret != EOF);
148}
149
150/* Iterate over all the memory regions in the current inferior,
151 calling FUNC for each memory region. OBFD is passed as the last
152 argument to FUNC. */
153
8f60fe01 154static int
2e73927c
TT
155fbsd_find_memory_regions (struct target_ops *self,
156 find_memory_region_ftype func, void *obfd)
578c1c03
MK
157{
158 pid_t pid = ptid_get_pid (inferior_ptid);
159 char *mapfilename;
160 FILE *mapfile;
161 unsigned long start, end, size;
162 char protection[4];
163 int read, write, exec;
7c8a8b04 164 struct cleanup *cleanup;
578c1c03
MK
165
166 mapfilename = xstrprintf ("/proc/%ld/map", (long) pid);
7c8a8b04 167 cleanup = make_cleanup (xfree, mapfilename);
578c1c03
MK
168 mapfile = fopen (mapfilename, "r");
169 if (mapfile == NULL)
8a3fe4f8 170 error (_("Couldn't open %s."), mapfilename);
7c8a8b04 171 make_cleanup_fclose (mapfile);
578c1c03
MK
172
173 if (info_verbose)
174 fprintf_filtered (gdb_stdout,
175 "Reading memory regions from %s\n", mapfilename);
176
177 /* Now iterate until end-of-file. */
178 while (fbsd_read_mapping (mapfile, &start, &end, &protection[0]))
179 {
180 size = end - start;
181
182 read = (strchr (protection, 'r') != 0);
183 write = (strchr (protection, 'w') != 0);
184 exec = (strchr (protection, 'x') != 0);
185
186 if (info_verbose)
187 {
188 fprintf_filtered (gdb_stdout,
5af949e3 189 "Save segment, %ld bytes at %s (%c%c%c)\n",
f5656ead 190 size, paddress (target_gdbarch (), start),
578c1c03
MK
191 read ? 'r' : '-',
192 write ? 'w' : '-',
193 exec ? 'x' : '-');
194 }
195
4f69f4c2
JK
196 /* Invoke the callback function to create the corefile segment.
197 Pass MODIFIED as true, we do not know the real modification state. */
198 func (start, size, read, write, exec, 1, obfd);
578c1c03
MK
199 }
200
7c8a8b04 201 do_cleanups (cleanup);
578c1c03
MK
202 return 0;
203}
25268153 204#endif
8f60fe01 205
e58e05d6
JB
206#ifdef PT_LWPINFO
207static ptid_t (*super_wait) (struct target_ops *,
208 ptid_t,
209 struct target_waitstatus *,
210 int);
211
212#ifdef TDP_RFPPWAIT
213/*
214 To catch fork events, PT_FOLLOW_FORK is set on every traced process
215 to enable stops on returns from fork or vfork. Note that both the
216 parent and child will always stop, even if system call stops are not
217 enabled.
218
219 After a fork, both the child and parent process will stop and report
220 an event. However, there is no guarantee of order. If the parent
221 reports its stop first, then fbsd_wait explicitly waits for the new
222 child before returning. If the child reports its stop first, then
223 the event is saved on a list and ignored until the parent's stop is
224 reported. fbsd_wait could have been changed to fetch the parent PID
225 of the new child and used that to wait for the parent explicitly.
226 However, if two threads in the parent fork at the same time, then
227 the wait on the parent might return the "wrong" fork event.
228
229 The initial version of PT_FOLLOW_FORK did not set PL_FLAG_CHILD for
230 the new child process. This flag could be inferred by treating any
231 events for an unknown pid as a new child.
232
233 In addition, the initial version of PT_FOLLOW_FORK did not report a
234 stop event for the parent process of a vfork until after the child
235 process executed a new program or exited. The kernel was changed to
236 defer the wait for exit or exec of the child until after posting the
237 stop event shortly after the change to introduce PL_FLAG_CHILD.
238 This could be worked around by reporting a vfork event when the
239 child event posted and ignoring the subsequent event from the
240 parent.
241
242 This implementation requires both of these fixes for simplicity's
243 sake. FreeBSD versions newer than 9.1 contain both fixes.
244*/
245
246struct fbsd_fork_child_info
247{
248 struct fbsd_fork_child_info *next;
249 pid_t child; /* Pid of new child. */
250};
251
252static struct fbsd_fork_child_info *fbsd_pending_children;
253
254/* Record a new child process event that is reported before the
255 corresponding fork event in the parent. */
256
257static void
258fbsd_remember_child (pid_t pid)
259{
8d749320 260 struct fbsd_fork_child_info *info = XCNEW (struct fbsd_fork_child_info);
e58e05d6
JB
261
262 info->child = pid;
263 info->next = fbsd_pending_children;
264 fbsd_pending_children = info;
265}
266
267/* Check for a previously-recorded new child process event for PID.
268 If one is found, remove it from the list. */
269
270static int
271fbsd_is_child_pending (pid_t pid)
272{
273 struct fbsd_fork_child_info *info, *prev;
274
275 prev = NULL;
276 for (info = fbsd_pending_children; info; prev = info, info = info->next)
277 {
278 if (info->child == pid)
279 {
280 if (prev == NULL)
281 fbsd_pending_children = info->next;
282 else
283 prev->next = info->next;
284 xfree (info);
285 return 1;
286 }
287 }
288 return 0;
289}
290
291/* Fetch the external variant of the kernel's internal process
292 structure for the process PID into KP. */
293
294static void
295fbsd_fetch_kinfo_proc (pid_t pid, struct kinfo_proc *kp)
296{
297 size_t len;
298 int mib[4];
299
300 len = sizeof *kp;
301 mib[0] = CTL_KERN;
302 mib[1] = KERN_PROC;
303 mib[2] = KERN_PROC_PID;
304 mib[3] = pid;
305 if (sysctl (mib, 4, kp, &len, NULL, 0) == -1)
306 perror_with_name (("sysctl"));
307}
308#endif
309
310/* Wait for the child specified by PTID to do something. Return the
311 process ID of the child, or MINUS_ONE_PTID in case of error; store
312 the status in *OURSTATUS. */
313
314static ptid_t
315fbsd_wait (struct target_ops *ops,
316 ptid_t ptid, struct target_waitstatus *ourstatus,
317 int target_options)
318{
319 ptid_t wptid;
320
321 while (1)
322 {
323 wptid = super_wait (ops, ptid, ourstatus, target_options);
324 if (ourstatus->kind == TARGET_WAITKIND_STOPPED)
325 {
326 struct ptrace_lwpinfo pl;
327 pid_t pid;
328 int status;
329
330 pid = ptid_get_pid (wptid);
331 if (ptrace (PT_LWPINFO, pid, (caddr_t)&pl, sizeof pl) == -1)
332 perror_with_name (("ptrace"));
333
334#ifdef TDP_RFPPWAIT
335 if (pl.pl_flags & PL_FLAG_FORKED)
336 {
337 struct kinfo_proc kp;
338 pid_t child;
339
340 child = pl.pl_child_pid;
341 ourstatus->kind = TARGET_WAITKIND_FORKED;
342 ourstatus->value.related_pid = pid_to_ptid (child);
343
344 /* Make sure the other end of the fork is stopped too. */
345 if (!fbsd_is_child_pending (child))
346 {
347 pid = waitpid (child, &status, 0);
348 if (pid == -1)
349 perror_with_name (("waitpid"));
350
351 gdb_assert (pid == child);
352
353 if (ptrace (PT_LWPINFO, child, (caddr_t)&pl, sizeof pl) == -1)
354 perror_with_name (("ptrace"));
355
356 gdb_assert (pl.pl_flags & PL_FLAG_CHILD);
357 }
358
359 /* For vfork, the child process will have the P_PPWAIT
360 flag set. */
361 fbsd_fetch_kinfo_proc (child, &kp);
362 if (kp.ki_flag & P_PPWAIT)
363 ourstatus->kind = TARGET_WAITKIND_VFORKED;
364
365 return wptid;
366 }
367
368 if (pl.pl_flags & PL_FLAG_CHILD)
369 {
370 /* Remember that this child forked, but do not report it
371 until the parent reports its corresponding fork
372 event. */
373 fbsd_remember_child (ptid_get_pid (wptid));
374 continue;
375 }
376#endif
d2b41ca0
JB
377
378#ifdef PL_FLAG_EXEC
379 if (pl.pl_flags & PL_FLAG_EXEC)
380 {
381 ourstatus->kind = TARGET_WAITKIND_EXECD;
382 ourstatus->value.execd_pathname
383 = xstrdup (fbsd_pid_to_exec_file (NULL, pid));
384 return wptid;
385 }
386#endif
e58e05d6
JB
387 }
388 return wptid;
389 }
390}
391
392#ifdef TDP_RFPPWAIT
393/* Target hook for follow_fork. On entry and at return inferior_ptid is
394 the ptid of the followed inferior. */
395
396static int
397fbsd_follow_fork (struct target_ops *ops, int follow_child,
398 int detach_fork)
399{
400 if (!follow_child)
401 {
402 struct thread_info *tp = inferior_thread ();
403 pid_t child_pid = ptid_get_pid (tp->pending_follow.value.related_pid);
404
405 /* Breakpoints have already been detached from the child by
406 infrun.c. */
407
408 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
409 perror_with_name (("ptrace"));
410 }
411
412 return 0;
413}
414
415static int
416fbsd_insert_fork_catchpoint (struct target_ops *self, int pid)
417{
418 return 0;
419}
420
421static int
422fbsd_remove_fork_catchpoint (struct target_ops *self, int pid)
423{
424 return 0;
425}
426
427static int
428fbsd_insert_vfork_catchpoint (struct target_ops *self, int pid)
429{
430 return 0;
431}
432
433static int
434fbsd_remove_vfork_catchpoint (struct target_ops *self, int pid)
435{
436 return 0;
437}
438
439/* Enable fork tracing for a specific process.
440
441 To catch fork events, PT_FOLLOW_FORK is set on every traced process
442 to enable stops on returns from fork or vfork. Note that both the
443 parent and child will always stop, even if system call stops are
444 not enabled. */
445
446static void
447fbsd_enable_follow_fork (pid_t pid)
448{
449 if (ptrace (PT_FOLLOW_FORK, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
450 perror_with_name (("ptrace"));
451}
452
453/* Implement the "to_post_startup_inferior" target_ops method. */
454
455static void
456fbsd_post_startup_inferior (struct target_ops *self, ptid_t pid)
457{
458 fbsd_enable_follow_fork (ptid_get_pid (pid));
459}
460
461/* Implement the "to_post_attach" target_ops method. */
462
463static void
464fbsd_post_attach (struct target_ops *self, int pid)
465{
466 fbsd_enable_follow_fork (pid);
467}
468#endif
d2b41ca0
JB
469
470#ifdef PL_FLAG_EXEC
471/* If the FreeBSD kernel supports PL_FLAG_EXEC, then traced processes
472 will always stop after exec. */
473
474static int
475fbsd_insert_exec_catchpoint (struct target_ops *self, int pid)
476{
477 return 0;
478}
479
480static int
481fbsd_remove_exec_catchpoint (struct target_ops *self, int pid)
482{
483 return 0;
484}
485#endif
e58e05d6
JB
486#endif
487
8f60fe01
JB
488void
489fbsd_nat_add_target (struct target_ops *t)
490{
491 t->to_pid_to_exec_file = fbsd_pid_to_exec_file;
492 t->to_find_memory_regions = fbsd_find_memory_regions;
e58e05d6
JB
493#ifdef PT_LWPINFO
494 super_wait = t->to_wait;
495 t->to_wait = fbsd_wait;
496#ifdef TDP_RFPPWAIT
497 t->to_follow_fork = fbsd_follow_fork;
498 t->to_insert_fork_catchpoint = fbsd_insert_fork_catchpoint;
499 t->to_remove_fork_catchpoint = fbsd_remove_fork_catchpoint;
500 t->to_insert_vfork_catchpoint = fbsd_insert_vfork_catchpoint;
501 t->to_remove_vfork_catchpoint = fbsd_remove_vfork_catchpoint;
502 t->to_post_startup_inferior = fbsd_post_startup_inferior;
503 t->to_post_attach = fbsd_post_attach;
504#endif
d2b41ca0
JB
505#ifdef PL_FLAG_EXEC
506 t->to_insert_exec_catchpoint = fbsd_insert_exec_catchpoint;
507 t->to_remove_exec_catchpoint = fbsd_remove_exec_catchpoint;
508#endif
e58e05d6 509#endif
8f60fe01
JB
510 add_target (t);
511}