windows-nat: Don't change current_event.dwThreadId in handle_output_debug_string()
[deliverable/binutils-gdb.git] / gdb / fbsd-nat.c
1 /* Native-dependent code for FreeBSD.
2
3 Copyright (C) 2002-2015 Free Software Foundation, Inc.
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
9 the Free Software Foundation; either version 3 of the License, or
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "gdbcore.h"
22 #include "inferior.h"
23 #include "regcache.h"
24 #include "regset.h"
25 #include "gdbthread.h"
26 #include <sys/types.h>
27 #include <sys/procfs.h>
28 #include <sys/sysctl.h>
29 #ifdef HAVE_KINFO_GETVMMAP
30 #include <sys/user.h>
31 #include <libutil.h>
32 #endif
33
34 #include "elf-bfd.h"
35 #include "fbsd-nat.h"
36
37 /* Return the name of a file that can be opened to get the symbols for
38 the child process identified by PID. */
39
40 char *
41 fbsd_pid_to_exec_file (struct target_ops *self, int pid)
42 {
43 ssize_t len = PATH_MAX;
44 static char buf[PATH_MAX];
45 char name[PATH_MAX];
46
47 #ifdef KERN_PROC_PATHNAME
48 int mib[4];
49
50 mib[0] = CTL_KERN;
51 mib[1] = KERN_PROC;
52 mib[2] = KERN_PROC_PATHNAME;
53 mib[3] = pid;
54 if (sysctl (mib, 4, buf, &len, NULL, 0) == 0)
55 return buf;
56 #endif
57
58 xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
59 len = readlink (name, buf, PATH_MAX - 1);
60 if (len != -1)
61 {
62 buf[len] = '\0';
63 return buf;
64 }
65
66 return NULL;
67 }
68
69 #ifdef HAVE_KINFO_GETVMMAP
70 /* Iterate over all the memory regions in the current inferior,
71 calling FUNC for each memory region. OBFD is passed as the last
72 argument to FUNC. */
73
74 int
75 fbsd_find_memory_regions (struct target_ops *self,
76 find_memory_region_ftype func, void *obfd)
77 {
78 pid_t pid = ptid_get_pid (inferior_ptid);
79 struct kinfo_vmentry *vmentl, *kve;
80 uint64_t size;
81 struct cleanup *cleanup;
82 int i, nitems;
83
84 vmentl = kinfo_getvmmap (pid, &nitems);
85 if (vmentl == NULL)
86 perror_with_name (_("Couldn't fetch VM map entries."));
87 cleanup = make_cleanup (free, vmentl);
88
89 for (i = 0; i < nitems; i++)
90 {
91 kve = &vmentl[i];
92
93 /* Skip unreadable segments and those where MAP_NOCORE has been set. */
94 if (!(kve->kve_protection & KVME_PROT_READ)
95 || kve->kve_flags & KVME_FLAG_NOCOREDUMP)
96 continue;
97
98 /* Skip segments with an invalid type. */
99 if (kve->kve_type != KVME_TYPE_DEFAULT
100 && kve->kve_type != KVME_TYPE_VNODE
101 && kve->kve_type != KVME_TYPE_SWAP
102 && kve->kve_type != KVME_TYPE_PHYS)
103 continue;
104
105 size = kve->kve_end - kve->kve_start;
106 if (info_verbose)
107 {
108 fprintf_filtered (gdb_stdout,
109 "Save segment, %ld bytes at %s (%c%c%c)\n",
110 (long) size,
111 paddress (target_gdbarch (), kve->kve_start),
112 kve->kve_protection & KVME_PROT_READ ? 'r' : '-',
113 kve->kve_protection & KVME_PROT_WRITE ? 'w' : '-',
114 kve->kve_protection & KVME_PROT_EXEC ? 'x' : '-');
115 }
116
117 /* Invoke the callback function to create the corefile segment.
118 Pass MODIFIED as true, we do not know the real modification state. */
119 func (kve->kve_start, size, kve->kve_protection & KVME_PROT_READ,
120 kve->kve_protection & KVME_PROT_WRITE,
121 kve->kve_protection & KVME_PROT_EXEC, 1, obfd);
122 }
123 do_cleanups (cleanup);
124 return 0;
125 }
126 #else
127 static int
128 fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end,
129 char *protection)
130 {
131 /* FreeBSD 5.1-RELEASE uses a 256-byte buffer. */
132 char buf[256];
133 int resident, privateresident;
134 unsigned long obj;
135 int ret = EOF;
136
137 /* As of FreeBSD 5.0-RELEASE, the layout is described in
138 /usr/src/sys/fs/procfs/procfs_map.c. Somewhere in 5.1-CURRENT a
139 new column was added to the procfs map. Therefore we can't use
140 fscanf since we need to support older releases too. */
141 if (fgets (buf, sizeof buf, mapfile) != NULL)
142 ret = sscanf (buf, "%lx %lx %d %d %lx %s", start, end,
143 &resident, &privateresident, &obj, protection);
144
145 return (ret != 0 && ret != EOF);
146 }
147
148 /* Iterate over all the memory regions in the current inferior,
149 calling FUNC for each memory region. OBFD is passed as the last
150 argument to FUNC. */
151
152 int
153 fbsd_find_memory_regions (struct target_ops *self,
154 find_memory_region_ftype func, void *obfd)
155 {
156 pid_t pid = ptid_get_pid (inferior_ptid);
157 char *mapfilename;
158 FILE *mapfile;
159 unsigned long start, end, size;
160 char protection[4];
161 int read, write, exec;
162 struct cleanup *cleanup;
163
164 mapfilename = xstrprintf ("/proc/%ld/map", (long) pid);
165 cleanup = make_cleanup (xfree, mapfilename);
166 mapfile = fopen (mapfilename, "r");
167 if (mapfile == NULL)
168 error (_("Couldn't open %s."), mapfilename);
169 make_cleanup_fclose (mapfile);
170
171 if (info_verbose)
172 fprintf_filtered (gdb_stdout,
173 "Reading memory regions from %s\n", mapfilename);
174
175 /* Now iterate until end-of-file. */
176 while (fbsd_read_mapping (mapfile, &start, &end, &protection[0]))
177 {
178 size = end - start;
179
180 read = (strchr (protection, 'r') != 0);
181 write = (strchr (protection, 'w') != 0);
182 exec = (strchr (protection, 'x') != 0);
183
184 if (info_verbose)
185 {
186 fprintf_filtered (gdb_stdout,
187 "Save segment, %ld bytes at %s (%c%c%c)\n",
188 size, paddress (target_gdbarch (), start),
189 read ? 'r' : '-',
190 write ? 'w' : '-',
191 exec ? 'x' : '-');
192 }
193
194 /* Invoke the callback function to create the corefile segment.
195 Pass MODIFIED as true, we do not know the real modification state. */
196 func (start, size, read, write, exec, 1, obfd);
197 }
198
199 do_cleanups (cleanup);
200 return 0;
201 }
202 #endif
This page took 0.034448 seconds and 4 git commands to generate.