gdb: microblaze: delete useless stubs
[deliverable/binutils-gdb.git] / gdb / fbsd-nat.c
CommitLineData
578c1c03
MK
1/* Native-dependent code for FreeBSD.
2
32d0add0 3 Copyright (C) 2002-2015 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{
260 struct fbsd_fork_child_info *info;
261
262 info = xcalloc (1, sizeof *info);
263
264 info->child = pid;
265 info->next = fbsd_pending_children;
266 fbsd_pending_children = info;
267}
268
269/* Check for a previously-recorded new child process event for PID.
270 If one is found, remove it from the list. */
271
272static int
273fbsd_is_child_pending (pid_t pid)
274{
275 struct fbsd_fork_child_info *info, *prev;
276
277 prev = NULL;
278 for (info = fbsd_pending_children; info; prev = info, info = info->next)
279 {
280 if (info->child == pid)
281 {
282 if (prev == NULL)
283 fbsd_pending_children = info->next;
284 else
285 prev->next = info->next;
286 xfree (info);
287 return 1;
288 }
289 }
290 return 0;
291}
292
293/* Fetch the external variant of the kernel's internal process
294 structure for the process PID into KP. */
295
296static void
297fbsd_fetch_kinfo_proc (pid_t pid, struct kinfo_proc *kp)
298{
299 size_t len;
300 int mib[4];
301
302 len = sizeof *kp;
303 mib[0] = CTL_KERN;
304 mib[1] = KERN_PROC;
305 mib[2] = KERN_PROC_PID;
306 mib[3] = pid;
307 if (sysctl (mib, 4, kp, &len, NULL, 0) == -1)
308 perror_with_name (("sysctl"));
309}
310#endif
311
312/* Wait for the child specified by PTID to do something. Return the
313 process ID of the child, or MINUS_ONE_PTID in case of error; store
314 the status in *OURSTATUS. */
315
316static ptid_t
317fbsd_wait (struct target_ops *ops,
318 ptid_t ptid, struct target_waitstatus *ourstatus,
319 int target_options)
320{
321 ptid_t wptid;
322
323 while (1)
324 {
325 wptid = super_wait (ops, ptid, ourstatus, target_options);
326 if (ourstatus->kind == TARGET_WAITKIND_STOPPED)
327 {
328 struct ptrace_lwpinfo pl;
329 pid_t pid;
330 int status;
331
332 pid = ptid_get_pid (wptid);
333 if (ptrace (PT_LWPINFO, pid, (caddr_t)&pl, sizeof pl) == -1)
334 perror_with_name (("ptrace"));
335
336#ifdef TDP_RFPPWAIT
337 if (pl.pl_flags & PL_FLAG_FORKED)
338 {
339 struct kinfo_proc kp;
340 pid_t child;
341
342 child = pl.pl_child_pid;
343 ourstatus->kind = TARGET_WAITKIND_FORKED;
344 ourstatus->value.related_pid = pid_to_ptid (child);
345
346 /* Make sure the other end of the fork is stopped too. */
347 if (!fbsd_is_child_pending (child))
348 {
349 pid = waitpid (child, &status, 0);
350 if (pid == -1)
351 perror_with_name (("waitpid"));
352
353 gdb_assert (pid == child);
354
355 if (ptrace (PT_LWPINFO, child, (caddr_t)&pl, sizeof pl) == -1)
356 perror_with_name (("ptrace"));
357
358 gdb_assert (pl.pl_flags & PL_FLAG_CHILD);
359 }
360
361 /* For vfork, the child process will have the P_PPWAIT
362 flag set. */
363 fbsd_fetch_kinfo_proc (child, &kp);
364 if (kp.ki_flag & P_PPWAIT)
365 ourstatus->kind = TARGET_WAITKIND_VFORKED;
366
367 return wptid;
368 }
369
370 if (pl.pl_flags & PL_FLAG_CHILD)
371 {
372 /* Remember that this child forked, but do not report it
373 until the parent reports its corresponding fork
374 event. */
375 fbsd_remember_child (ptid_get_pid (wptid));
376 continue;
377 }
378#endif
d2b41ca0
JB
379
380#ifdef PL_FLAG_EXEC
381 if (pl.pl_flags & PL_FLAG_EXEC)
382 {
383 ourstatus->kind = TARGET_WAITKIND_EXECD;
384 ourstatus->value.execd_pathname
385 = xstrdup (fbsd_pid_to_exec_file (NULL, pid));
386 return wptid;
387 }
388#endif
e58e05d6
JB
389 }
390 return wptid;
391 }
392}
393
394#ifdef TDP_RFPPWAIT
395/* Target hook for follow_fork. On entry and at return inferior_ptid is
396 the ptid of the followed inferior. */
397
398static int
399fbsd_follow_fork (struct target_ops *ops, int follow_child,
400 int detach_fork)
401{
402 if (!follow_child)
403 {
404 struct thread_info *tp = inferior_thread ();
405 pid_t child_pid = ptid_get_pid (tp->pending_follow.value.related_pid);
406
407 /* Breakpoints have already been detached from the child by
408 infrun.c. */
409
410 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
411 perror_with_name (("ptrace"));
412 }
413
414 return 0;
415}
416
417static int
418fbsd_insert_fork_catchpoint (struct target_ops *self, int pid)
419{
420 return 0;
421}
422
423static int
424fbsd_remove_fork_catchpoint (struct target_ops *self, int pid)
425{
426 return 0;
427}
428
429static int
430fbsd_insert_vfork_catchpoint (struct target_ops *self, int pid)
431{
432 return 0;
433}
434
435static int
436fbsd_remove_vfork_catchpoint (struct target_ops *self, int pid)
437{
438 return 0;
439}
440
441/* Enable fork tracing for a specific process.
442
443 To catch fork events, PT_FOLLOW_FORK is set on every traced process
444 to enable stops on returns from fork or vfork. Note that both the
445 parent and child will always stop, even if system call stops are
446 not enabled. */
447
448static void
449fbsd_enable_follow_fork (pid_t pid)
450{
451 if (ptrace (PT_FOLLOW_FORK, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
452 perror_with_name (("ptrace"));
453}
454
455/* Implement the "to_post_startup_inferior" target_ops method. */
456
457static void
458fbsd_post_startup_inferior (struct target_ops *self, ptid_t pid)
459{
460 fbsd_enable_follow_fork (ptid_get_pid (pid));
461}
462
463/* Implement the "to_post_attach" target_ops method. */
464
465static void
466fbsd_post_attach (struct target_ops *self, int pid)
467{
468 fbsd_enable_follow_fork (pid);
469}
470#endif
d2b41ca0
JB
471
472#ifdef PL_FLAG_EXEC
473/* If the FreeBSD kernel supports PL_FLAG_EXEC, then traced processes
474 will always stop after exec. */
475
476static int
477fbsd_insert_exec_catchpoint (struct target_ops *self, int pid)
478{
479 return 0;
480}
481
482static int
483fbsd_remove_exec_catchpoint (struct target_ops *self, int pid)
484{
485 return 0;
486}
487#endif
e58e05d6
JB
488#endif
489
8f60fe01
JB
490void
491fbsd_nat_add_target (struct target_ops *t)
492{
493 t->to_pid_to_exec_file = fbsd_pid_to_exec_file;
494 t->to_find_memory_regions = fbsd_find_memory_regions;
e58e05d6
JB
495#ifdef PT_LWPINFO
496 super_wait = t->to_wait;
497 t->to_wait = fbsd_wait;
498#ifdef TDP_RFPPWAIT
499 t->to_follow_fork = fbsd_follow_fork;
500 t->to_insert_fork_catchpoint = fbsd_insert_fork_catchpoint;
501 t->to_remove_fork_catchpoint = fbsd_remove_fork_catchpoint;
502 t->to_insert_vfork_catchpoint = fbsd_insert_vfork_catchpoint;
503 t->to_remove_vfork_catchpoint = fbsd_remove_vfork_catchpoint;
504 t->to_post_startup_inferior = fbsd_post_startup_inferior;
505 t->to_post_attach = fbsd_post_attach;
506#endif
d2b41ca0
JB
507#ifdef PL_FLAG_EXEC
508 t->to_insert_exec_catchpoint = fbsd_insert_exec_catchpoint;
509 t->to_remove_exec_catchpoint = fbsd_remove_exec_catchpoint;
510#endif
e58e05d6 511#endif
8f60fe01
JB
512 add_target (t);
513}
This page took 0.795056 seconds and 4 git commands to generate.