Fix typo in FAQ.
[valgrind.git] / coregrind / vgdb-invoker-freebsd.c
blobdf34e0a1515280b0fb124a6b7b4a53f6fd4a3d19
1 /*--------------------------------------------------------------------*/
2 /*--- Implementation of vgdb invoker subsystem via ptrace() calls. ---*/
3 /*--------------------------------------------------------------------*/
5 /*
6 This file is part of Valgrind, a dynamic binary instrumentation
7 framework.
9 Copyright (C) 2011-2017 Philippe Waroquiers
10 Copyright (C) 2021-2022 Paul Floyd
11 pjfloyd@wanadoo.fr
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, see <http://www.gnu.org/licenses/>.
26 The GNU General Public License is contained in the file COPYING.
29 #include "config.h"
31 #include "vgdb.h"
32 #include "pub_core_threadstate.h"
34 #include <assert.h>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <signal.h>
40 #include <sys/ptrace.h>
41 #include <sys/time.h>
42 #include <sys/user.h>
43 #include <sys/wait.h>
44 #include <sys/uio.h>
45 #include <elf.h>
46 #include <sys/procfs.h>
49 * This file is largely a copy of vgdb-invoker-ptrace.c
51 * Sadly, though ptrace exists on most unix-like systems,
52 * no two versions are the same.
54 * The main two differences are that
55 * - FreeBSD ptrace works at the process level, not lwps
56 * so just attaching to the main pid stops everything
57 * (no need to read memory to get lwpids and stop all of the
58 * threads)
59 * - Reading registers is a lot simpler.
61 * Known limitations:
62 * gdbserver_tests/nlcontrolc has different behaviour
63 * becase attaching causes the select() syscall of main
64 * to be interrupted. This seems to be a "feature" of
65 * ptrace on FreeBSD so I doubt it can be fixed.
69 // 32-bit or 64-bit wide, depending on primary architecture.
70 typedef Addr CORE_ADDR;
71 typedef int PTRACE_XFER_TYPE;
72 typedef caddr_t PTRACE_ARG3_TYPE;
74 // if > 0, pid for which registers have to be restored.
75 // if == 0, means we have not yet called setregs (or have already
76 // restored the registers).
77 static int pid_of_save_regs = 0;
78 /* True if we have continued pid_of_save_regs after PT_ATTACH. */
79 static Bool pid_of_save_regs_continued = False;
80 // When setregs has been called to change the registers of pid_of_save_regs,
81 // vgdb cannot transmit the signals intercepted during ptrace.
82 // So, we queue them, and will deliver them when detaching.
83 // See function waitstopped for more info.
84 static int signal_queue_sz = 0;
85 static siginfo_t *signal_queue;
87 /* True when loss of connection indicating that the Valgrind
88 process is dying. */
89 static Bool dying = False;
91 /* Copy LEN bytes of data from vgdb memory at MYADDR
92 to valgrind memory at MEMADDR.
93 On failure (cannot write the valgrind memory)
94 returns the value of errno. */
95 __attribute__((unused)) /* not used on all platforms */
96 static
97 int ptrace_write_memory (pid_t inferior_pid, CORE_ADDR memaddr,
98 const void *myaddr, size_t len)
100 size_t i;
101 /* Round starting address down to longword boundary. */
102 CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
103 /* Round ending address up; get number of longwords that makes. */
104 size_t count
105 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
106 / sizeof (PTRACE_XFER_TYPE);
107 /* Allocate buffer of that many longwords. */
108 PTRACE_XFER_TYPE *buffer
109 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
111 if (debuglevel >= 1) {
112 DEBUG (1, "Writing ");
113 for (i = 0; i < len; i++) {
114 PDEBUG (1, "%02x", ((const unsigned char*)myaddr)[i]);
116 PDEBUG(1, " to %p\n", (void *) memaddr);
119 /* Fill start and end extra bytes of buffer with existing memory data. */
121 buffer[0] = ptrace (PT_READ_I, inferior_pid,
122 (PTRACE_ARG3_TYPE) addr, 0);
124 if (count > 1) {
125 buffer[count - 1]
126 = ptrace (PT_READ_I, inferior_pid,
127 (PTRACE_ARG3_TYPE) (addr + (count - 1)
128 * sizeof (PTRACE_XFER_TYPE)),
132 /* Copy data to be written over corresponding part of buffer */
134 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
135 myaddr, len);
137 /* Write the entire buffer. */
139 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) {
140 errno = 0;
141 ptrace (PT_WRITE_I, inferior_pid,
142 (PTRACE_ARG3_TYPE) addr, buffer[i]);
143 if (errno) {
144 return errno;
148 return 0;
151 static
152 char *status_image (int status)
154 static char result[256]; // large enough
155 int sz = 0;
156 #define APPEND(...) sz += snprintf (result+sz, 256 - sz - 1, __VA_ARGS__)
158 result[0] = 0;
160 if (WIFEXITED(status)) {
161 APPEND ("WIFEXITED %d ", WEXITSTATUS(status));
164 if (WIFSIGNALED(status)) {
165 APPEND ("WIFSIGNALED %d ", WTERMSIG(status));
166 if (WCOREDUMP(status)) {
167 APPEND ("WCOREDUMP ");
171 if (WIFSTOPPED(status))
172 APPEND ("WIFSTOPPED %d ", WSTOPSIG(status));
174 #ifdef WIFCONTINUED
175 if (WIFCONTINUED(status)) {
176 APPEND ("WIFCONTINUED ");
178 #endif
180 return result;
181 #undef APPEND
184 /* Wait till the process pid is reported as stopped with signal_expected.
185 If other signal(s) than signal_expected are received, waitstopped
186 will pass them to pid, waiting for signal_expected to stop pid.
187 Returns True when process is in stopped state with signal_expected.
188 Returns False if a problem was encountered while waiting for pid
189 to be stopped.
191 If pid is reported as being dead/exited, waitstopped will return False.
193 static
194 Bool waitstopped (pid_t pid, int signal_expected, const char *msg)
196 pid_t p;
197 int status = 0;
198 int signal_received;
199 int res;
201 while (1) {
202 DEBUG(1, "waitstopped %s before waitpid signal_expected %d\n",
203 msg, signal_expected);
204 p = waitpid(pid, &status, 0); /* PJF options was __WALL */
205 DEBUG(1, "after waitpid pid %d p %d status 0x%x %s\n", pid, p,
206 status, status_image (status));
207 if (p != pid) {
208 ERROR(errno, "%s waitpid pid %d in waitstopped %d status 0x%x %s\n",
209 msg, pid, p, status, status_image (status));
210 return False;
213 /* The process either exited or was terminated by a (fatal) signal. */
214 if (WIFEXITED(status) || WIFSIGNALED(status)) {
215 shutting_down = True;
216 return False;
219 assert (WIFSTOPPED(status));
220 signal_received = WSTOPSIG(status);
221 if (signal_received == signal_expected) {
222 break;
225 /* pid received a signal which is not the signal we are waiting for.
226 If we have not (yet) changed the registers of the inferior
227 or we have (already) reset them, we can transmit the signal.
229 If we have already set the registers of the inferior, we cannot
230 transmit the signal, as this signal would arrive when the
231 gdbserver code runs. And valgrind only expects signals to
232 arrive in a small code portion around
233 client syscall logic, where signal are unmasked (see e.g.
234 m_syswrap/syscall-x86-linux.S ML_(do_syscall_for_client_WRK).
236 As ptrace is forcing a call to gdbserver by jumping
237 'out of this region', signals are not masked, but
238 will arrive outside of the allowed/expected code region.
239 So, if we have changed the registers of the inferior, we
240 rather queue the signal to transmit them when detaching,
241 after having restored the registers to the initial values. */
242 if (pid_of_save_regs) {
243 siginfo_t *newsiginfo;
244 struct ptrace_lwpinfo new_lwpinfo;
246 // realloc a bigger queue, and store new signal at the end.
247 // This is not very efficient but we assume not many sigs are queued.
248 if (signal_queue_sz >= 64) {
249 DEBUG(0, "too many queued signals while waiting for SIGSTOP\n");
250 return False;
252 signal_queue_sz++;
253 signal_queue = vrealloc(signal_queue,
254 sizeof(siginfo_t) * signal_queue_sz);
255 newsiginfo = signal_queue + (signal_queue_sz - 1);
257 res = ptrace (PT_LWPINFO, pid, (caddr_t)&new_lwpinfo, sizeof(new_lwpinfo));
258 *newsiginfo = new_lwpinfo.pl_siginfo;
259 if (res != 0) {
260 ERROR(errno, "PT_LWPINFO failed: signal lost !!!!\n");
261 signal_queue_sz--;
262 } else {
263 DEBUG(1, "waitstopped PTRACE_CONT, queuing signal %d"
264 " si_signo %d si_pid %d\n",
265 signal_received, newsiginfo->si_signo, newsiginfo->si_pid);
267 res = ptrace (PT_CONTINUE, pid, (caddr_t)1, 0);
268 } else {
269 DEBUG(1, "waitstopped PT_CONTINUE with signal %d\n", signal_received);
270 res = ptrace (PT_CONTINUE, pid, (caddr_t)1, signal_received);
272 if (res != 0) {
273 ERROR(errno, "waitstopped PTRACE_CONT\n");
274 return False;
278 return True;
281 /* Stops the given pid, wait for the process to be stopped.
282 Returns True if successful, False otherwise.
283 msg is used in tracing and error reporting. */
284 static
285 Bool stop (pid_t pid, const char *msg)
287 long res;
289 DEBUG(1, "%s SIGSTOP pid %d\n", msg, pid);
290 res = kill (pid, SIGSTOP);
291 if (res != 0) {
292 ERROR(errno, "%s SIGSTOP pid %d %ld\n", msg, pid, res);
293 return False;
296 return waitstopped (pid, SIGSTOP, msg);
300 /* Attaches to given pid, wait for the process to be stopped.
301 Returns True if successful, False otherwise.
302 msg is used in tracing and error reporting. */
303 static
304 Bool attach (pid_t pid, const char *msg)
306 int res;
307 static Bool output_error = True;
308 static Bool initial_attach = True;
309 // For a ptrace_scope protected system, we do not want to output
310 // repetitively attach error. We will output once an error
311 // for the initial_attach. Once the 1st attach has succeeded, we
312 // again show all errors.
314 DEBUG(1, "%s PT_ATTACH pid %d\n", msg, pid);
315 res = ptrace (PT_ATTACH, pid, 0, 0);
316 if (res != 0) {
317 if (output_error || debuglevel > 0) {
318 ERROR(errno, "%s PT_ATTACH pid %d %d\n", msg, pid, res);
319 if (initial_attach) {
320 output_error = False;
323 return False;
326 initial_attach = False;
327 output_error = True;
328 return waitstopped(pid, SIGSTOP, msg);
331 static
332 void detach_from_all_threads (pid_t pid)
334 long res = ptrace (PT_DETACH, pid, NULL, 0);
336 if (res != 0) {
337 ERROR(errno, "PT_DETACH pid %d res %ld\n",
338 pid, res);
342 static struct reg reg_save;
344 /* Get the registers from pid into regs.
345 Returns True if all ok, otherwise False. */
346 static
347 Bool getregs (pid_t pid, struct reg *regs)
349 if (ptrace(PT_GETREGS, pid, (caddr_t)regs, 0) < 0) {
350 return False;
352 return True;
355 /* Set the registers of pid to regs.
356 Returns True if all ok, otherwise False. */
357 static
358 Bool setregs (pid_t pid, struct reg *regs)
360 if (ptrace(PT_SETREGS, pid, (caddr_t)regs, 0) < 0) {
361 return False;
363 return True;
366 /* Restore the registers to the saved value, then detaches from all threads */
367 static
368 void restore_and_detach (pid_t pid)
370 int res;
372 DEBUG(1, "restore_and_detach pid %d pid_of_save_regs %d\n",
373 pid, pid_of_save_regs);
375 if (pid_of_save_regs) {
376 /* In case the 'main pid' has been continued, we need to stop it
377 before resetting the registers. */
378 if (pid_of_save_regs_continued) {
379 pid_of_save_regs_continued = False;
380 if (!stop(pid_of_save_regs, "sigstop before reset regs")) {
381 DEBUG(0, "Could not sigstop before reset");
385 DEBUG(1, "setregs restore registers pid %d\n", pid_of_save_regs);
386 if (!setregs(pid_of_save_regs, &reg_save)) {
387 ERROR(errno, "setregs restore registers pid %d after cont\n",
388 pid_of_save_regs);
391 /* Now, we transmit all the signals we have queued. */
392 if (signal_queue_sz > 0) {
393 int i;
394 for (i = 0; i < signal_queue_sz; i++) {
395 DEBUG(1, "PTRACE_CONT to transmit queued signal %d\n",
396 signal_queue[i].si_signo);
397 res = ptrace (PT_CONTINUE, pid_of_save_regs, (caddr_t)1,
398 signal_queue[i].si_signo);
399 if (res != 0) {
400 ERROR(errno, "PT_CONTINUE with signal %d\n",
401 signal_queue[i].si_signo);
403 if (!stop(pid_of_save_regs, "sigstop after transmit sig")) {
404 DEBUG(0, "Could not sigstop after transmit sig");
407 free (signal_queue);
408 signal_queue = NULL;
409 signal_queue_sz = 0;
411 pid_of_save_regs = 0;
412 } else {
413 DEBUG(1, "PTRACE_SETREGS restore registers: no pid\n");
415 if (signal_queue) {
416 ERROR (0, "One or more signals queued were not delivered. "
417 "First signal: %d\n", signal_queue[0].si_signo);
419 detach_from_all_threads(pid);
422 Bool invoker_invoke_gdbserver (pid_t pid)
424 long res;
425 Bool stopped;
426 struct reg reg_mod;
427 Addr sp __attribute__((unused)); // Not used on all platforms.
429 /* A specific int value is passed to invoke_gdbserver, to check
430 everything goes according to the plan. */
431 const int check = 0x8BADF00D; // ate bad food.
433 const Addr bad_return = 0;
434 // A bad return address will be pushed on the stack.
435 // The function invoke_gdbserver cannot return. If ever it returns, a NULL
436 // address pushed on the stack should ensure this is detected.
438 /* Not yet attached. If problem, vgdb can abort,
439 no cleanup needed. */
441 DEBUG(1, "attach to 'main' pid %d\n", pid);
442 if (!attach(pid, "attach main pid")) {
443 ERROR(0, "error attach main pid %d\n", pid);
444 return False;
447 /* Now, we are attached. If problem, detach and return. */
449 DEBUG(1, "calling getregs\n");
451 if (!getregs(pid, &reg_mod)) {
452 detach_from_all_threads(pid);
453 return False;
455 reg_save = reg_mod;
457 DEBUG(1, "getregs call succeeded\n");
459 #if defined(VGA_x86)
460 sp = reg_mod.r_esp;
461 #elif defined(VGA_amd64)
462 sp = reg_mod.r_rsp;
463 if (shared32 != NULL) {
464 /* 64bit vgdb speaking with a 32bit executable.
465 To have system call restart properly, we need to sign extend rax.
466 For more info:
467 web search '[patch] Fix syscall restarts for amd64->i386 biarch'
468 e.g. http://sourceware.org/ml/gdb-patches/2009-11/msg00592.html */
469 *(long *)&reg_save.r_rax = *(int*)&reg_save.r_rax;
470 DEBUG(1, "Sign extending %8.8lx to %8.8lx\n",
471 reg_mod.r_rax, reg_save.r_rax);
473 #elif defined(VGA_arm64)
474 sp = reg_mod.sp;
475 #else
476 I_die_here : (sp) architecture missing in vgdb-invoker-freebsd.c
477 #endif
480 // the magic below is derived from spying what gdb sends to
481 // the (classical) gdbserver when invoking a C function.
482 if (shared32 != NULL) {
483 // vgdb speaking with a 32bit executable.
484 #if defined(VGA_x86) || defined(VGA_amd64)
485 const int regsize = 4;
486 int rw;
487 /* push check arg on the stack */
488 sp = sp - regsize;
489 DEBUG(1, "push check arg ptrace_write_memory\n");
490 assert(regsize == sizeof(check));
491 rw = ptrace_write_memory(pid, sp,
492 &check,
493 regsize);
494 if (rw != 0) {
495 ERROR(rw, "push check arg ptrace_write_memory\n");
496 detach_from_all_threads(pid);
497 return False;
500 sp = sp - regsize;
501 DEBUG(1, "push bad_return return address ptrace_write_memory\n");
502 // Note that for a 64 bits vgdb, only 4 bytes of NULL bad_return
503 // are written.
504 rw = ptrace_write_memory(pid, sp,
505 &bad_return,
506 regsize);
507 if (rw != 0) {
508 ERROR(rw, "push bad_return return address ptrace_write_memory\n");
509 detach_from_all_threads(pid);
510 return False;
512 #if defined(VGA_x86)
513 /* set ebp, esp, eip and orig_eax to invoke gdbserver */
514 // compiled in 32bits, speaking with a 32bits exe
515 reg_mod.r_ebp = sp; // bp set to sp
516 reg_mod.r_esp = sp;
517 reg_mod.r_eip = shared32->invoke_gdbserver;
518 #elif defined(VGA_amd64)
519 /* set ebp, esp, eip and orig_eax to invoke gdbserver */
520 // compiled in 64bits, speaking with a 32bits exe
521 reg_mod.r_rbp = sp; // bp set to sp
522 reg_mod.r_rsp = sp;
523 reg_mod.r_rip = shared32->invoke_gdbserver;
524 #endif
526 #elif defined(VGA_arm64)
527 XERROR(0, "TBD arm64: vgdb a 32 bits executable with a 64 bits exe\n");
528 #else
529 I_die_here : architecture missing in vgdb-invoker-freebsd.c
530 #endif
533 else if (shared64 != NULL) {
534 #if defined(VGA_x86)
535 assert(0); // cannot vgdb a 64 bits executable with a 32 bits exe
536 #elif defined(VGA_amd64)
537 // vgdb speaking with a 64 bit executable.
538 const int regsize = 8;
539 int rw;
541 /* give check arg in rdi */
542 reg_mod.r_rdi = check;
544 /* push return address on stack : return to breakaddr */
545 sp &= ~0xf; // keep the stack aligned on 16 bytes ...
546 //sp = sp - 128; // do not touch the amd64 redzone
547 sp = sp - regsize;
548 DEBUG(1, "push bad_return return address ptrace_write_memory\n");
549 rw = ptrace_write_memory(pid, sp,
550 &bad_return,
551 sizeof(bad_return));
552 if (rw != 0) {
553 ERROR(rw, "push bad_return return address ptrace_write_memory\n");
554 detach_from_all_threads(pid);
555 return False;
558 /* set rbp, rsp, rip and orig_rax to invoke gdbserver */
559 reg_mod.r_rbp = sp; // bp set to sp
560 reg_mod.r_rsp = sp;
561 reg_mod.r_rip = shared64->invoke_gdbserver;
562 #elif defined(VGA_arm64)
563 reg_mod.x[0] = check;
564 reg_mod.sp = sp;
565 reg_mod.elr = shared64->invoke_gdbserver;
566 /* put NULL return address in Link Register */
567 reg_mod.lr = bad_return;
569 #else
570 I_die_here: architecture missing in vgdb-invoker-freebsd.c
571 #endif
573 else {
574 assert(0);
577 DEBUG(1, "calling setregs\n");
579 if (!setregs(pid, &reg_mod)) {
580 detach_from_all_threads(pid);
581 return False;
584 DEBUG(1, "setregs succeeded\n");
586 /* Now that we have modified the registers, we set
587 pid_of_save_regs to indicate that restore_and_detach
588 must restore the registers in case of cleanup. */
589 pid_of_save_regs = pid;
590 pid_of_save_regs_continued = False;
593 /* We PTRACE_CONT-inue pid.
594 Either gdbserver will be invoked directly (if all
595 threads are interruptible) or gdbserver will be
596 called soon by the scheduler. In the first case,
597 pid will stop on the break inserted above when
598 gdbserver returns. In the 2nd case, the break will
599 be encountered directly. */
600 DEBUG(1, "PT_CONTINUE to invoke\n");
601 /* an address of 1 means continue without modifying IP
602 since we already set IP above there is no need to set it here */
603 res = ptrace (PT_CONTINUE, pid, (caddr_t)1, 0);
604 if (res != 0) {
605 ERROR(errno, "PT_CONTINUE\n");
606 restore_and_detach(pid);
607 return False;
609 pid_of_save_regs_continued = True;
610 /* Wait for SIGSTOP generated by m_gdbserver.c give_control_back_to_vgdb */
611 stopped = waitstopped (pid, SIGSTOP,
612 "waitpid status after PTRACE_CONT to invoke");
613 if (stopped) {
614 /* Here pid has properly stopped on the break. */
615 pid_of_save_regs_continued = False;
616 restore_and_detach(pid);
617 return True;
619 /* Whatever kind of problem happened. We shutdown. */
620 shutting_down = True;
621 return False;
624 void invoker_cleanup_restore_and_detach(void *v_pid)
626 DEBUG(1, "invoker_cleanup_restore_and_detach dying: %d\n", dying);
627 if (!dying) {
628 restore_and_detach(*(int*)v_pid);
632 void invoker_restrictions_msg(void)
636 void invoker_valgrind_dying(void)
638 /* Avoid messing up with registers of valgrind when it is dying. */
639 pid_of_save_regs_continued = False;
640 dying = True;