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