Bug 444488 - Use glibc.pthread.stack_cache_size tunable
[valgrind.git] / coregrind / vgdb-invoker-freebsd.c
blobd9f3584f80ddd148230cf5d9e7ff8cb9cae4fc3a
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]);
115 PDEBUG(1, " to %p\n", (void *) memaddr);
118 /* Fill start and end extra bytes of buffer with existing memory data. */
120 buffer[0] = ptrace (PT_READ_I, inferior_pid,
121 (PTRACE_ARG3_TYPE) addr, 0);
123 if (count > 1) {
124 buffer[count - 1]
125 = ptrace (PT_READ_I, inferior_pid,
126 (PTRACE_ARG3_TYPE) (addr + (count - 1)
127 * sizeof (PTRACE_XFER_TYPE)),
131 /* Copy data to be written over corresponding part of buffer */
133 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
134 myaddr, len);
136 /* Write the entire buffer. */
138 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) {
139 errno = 0;
140 ptrace (PT_WRITE_I, inferior_pid,
141 (PTRACE_ARG3_TYPE) addr, buffer[i]);
142 if (errno)
143 return errno;
146 return 0;
149 static
150 char *status_image (int status)
152 static char result[256]; // large enough
153 int sz = 0;
154 #define APPEND(...) sz += snprintf (result+sz, 256 - sz - 1, __VA_ARGS__)
156 result[0] = 0;
158 if (WIFEXITED(status))
159 APPEND ("WIFEXITED %d ", WEXITSTATUS(status));
161 if (WIFSIGNALED(status)) {
162 APPEND ("WIFSIGNALED %d ", WTERMSIG(status));
163 if (WCOREDUMP(status)) APPEND ("WCOREDUMP ");
166 if (WIFSTOPPED(status))
167 APPEND ("WIFSTOPPED %d ", WSTOPSIG(status));
169 #ifdef WIFCONTINUED
170 if (WIFCONTINUED(status))
171 APPEND ("WIFCONTINUED ");
172 #endif
174 return result;
175 #undef APPEND
178 /* Wait till the process pid is reported as stopped with signal_expected.
179 If other signal(s) than signal_expected are received, waitstopped
180 will pass them to pid, waiting for signal_expected to stop pid.
181 Returns True when process is in stopped state with signal_expected.
182 Returns False if a problem was encountered while waiting for pid
183 to be stopped.
185 If pid is reported as being dead/exited, waitstopped will return False.
187 static
188 Bool waitstopped (pid_t pid, int signal_expected, const char *msg)
190 pid_t p;
191 int status = 0;
192 int signal_received;
193 int res;
195 while (1) {
196 DEBUG(1, "waitstopped %s before waitpid signal_expected %d\n",
197 msg, signal_expected);
198 p = waitpid(pid, &status, 0); /* PJF options was __WALL */
199 DEBUG(1, "after waitpid pid %d p %d status 0x%x %s\n", pid, p,
200 status, status_image (status));
201 if (p != pid) {
202 ERROR(errno, "%s waitpid pid %d in waitstopped %d status 0x%x %s\n",
203 msg, pid, p, status, status_image (status));
204 return False;
207 /* The process either exited or was terminated by a (fatal) signal. */
208 if (WIFEXITED(status) || WIFSIGNALED(status)) {
209 shutting_down = True;
210 return False;
213 assert (WIFSTOPPED(status));
214 signal_received = WSTOPSIG(status);
215 if (signal_received == signal_expected)
216 break;
218 /* pid received a signal which is not the signal we are waiting for.
219 If we have not (yet) changed the registers of the inferior
220 or we have (already) reset them, we can transmit the signal.
222 If we have already set the registers of the inferior, we cannot
223 transmit the signal, as this signal would arrive when the
224 gdbserver code runs. And valgrind only expects signals to
225 arrive in a small code portion around
226 client syscall logic, where signal are unmasked (see e.g.
227 m_syswrap/syscall-x86-linux.S ML_(do_syscall_for_client_WRK).
229 As ptrace is forcing a call to gdbserver by jumping
230 'out of this region', signals are not masked, but
231 will arrive outside of the allowed/expected code region.
232 So, if we have changed the registers of the inferior, we
233 rather queue the signal to transmit them when detaching,
234 after having restored the registers to the initial values. */
235 if (pid_of_save_regs) {
236 siginfo_t *newsiginfo;
237 struct ptrace_lwpinfo new_lwpinfo;
239 // realloc a bigger queue, and store new signal at the end.
240 // This is not very efficient but we assume not many sigs are queued.
241 if (signal_queue_sz >= 64) {
242 DEBUG(0, "too many queued signals while waiting for SIGSTOP\n");
243 return False;
245 signal_queue_sz++;
246 signal_queue = vrealloc(signal_queue,
247 sizeof(siginfo_t) * signal_queue_sz);
248 newsiginfo = signal_queue + (signal_queue_sz - 1);
250 res = ptrace (PT_LWPINFO, pid, (caddr_t)&new_lwpinfo, sizeof(new_lwpinfo));
251 *newsiginfo = new_lwpinfo.pl_siginfo;
252 if (res != 0) {
253 ERROR(errno, "PT_LWPINFO failed: signal lost !!!!\n");
254 signal_queue_sz--;
255 } else
256 DEBUG(1, "waitstopped PTRACE_CONT, queuing signal %d"
257 " si_signo %d si_pid %d\n",
258 signal_received, newsiginfo->si_signo, newsiginfo->si_pid);
259 res = ptrace (PT_CONTINUE, pid, (caddr_t)1, 0);
260 } else {
261 DEBUG(1, "waitstopped PT_CONTINUE with signal %d\n", signal_received);
262 res = ptrace (PT_CONTINUE, pid, (caddr_t)1, signal_received);
264 if (res != 0) {
265 ERROR(errno, "waitstopped PTRACE_CONT\n");
266 return False;
270 return True;
273 /* Stops the given pid, wait for the process to be stopped.
274 Returns True if successful, False otherwise.
275 msg is used in tracing and error reporting. */
276 static
277 Bool stop (pid_t pid, const char *msg)
279 long res;
281 DEBUG(1, "%s SIGSTOP pid %d\n", msg, pid);
282 res = kill (pid, SIGSTOP);
283 if (res != 0) {
284 ERROR(errno, "%s SIGSTOP pid %d %ld\n", msg, pid, res);
285 return False;
288 return waitstopped (pid, SIGSTOP, msg);
292 /* Attaches to given pid, wait for the process to be stopped.
293 Returns True if successful, False otherwise.
294 msg is used in tracing and error reporting. */
295 static
296 Bool attach (pid_t pid, const char *msg)
298 int res;
299 static Bool output_error = True;
300 static Bool initial_attach = True;
301 // For a ptrace_scope protected system, we do not want to output
302 // repetitively attach error. We will output once an error
303 // for the initial_attach. Once the 1st attach has succeeded, we
304 // again show all errors.
306 DEBUG(1, "%s PT_ATTACH pid %d\n", msg, pid);
307 res = ptrace (PT_ATTACH, pid, 0, 0);
308 if (res != 0) {
309 if (output_error || debuglevel > 0) {
310 ERROR(errno, "%s PT_ATTACH pid %d %ld\n", msg, pid, res);
311 if (initial_attach)
312 output_error = False;
314 return False;
317 initial_attach = False;
318 output_error = True;
319 return waitstopped(pid, SIGSTOP, msg);
322 static
323 void detach_from_all_threads (pid_t pid)
325 long res = ptrace (PT_DETACH, pid, NULL, 0);
327 if (res != 0) {
328 ERROR(errno, "PT_DETACH pid %d res %ld\n",
329 pid, res);
333 static struct reg reg_save;
335 /* Get the registers from pid into regs.
336 Returns True if all ok, otherwise False. */
337 static
338 Bool getregs (pid_t pid, struct reg *regs)
340 if (ptrace(PT_GETREGS, pid, (caddr_t)regs, 0) < 0) {
341 return False;
343 return True;
346 /* Set the registers of pid to regs.
347 Returns True if all ok, otherwise False. */
348 static
349 Bool setregs (pid_t pid, struct reg *regs)
351 if (ptrace(PT_SETREGS, pid, (caddr_t)regs, 0) < 0) {
352 return False;
354 return True;
357 /* Restore the registers to the saved value, then detaches from all threads */
358 static
359 void restore_and_detach (pid_t pid)
361 int res;
363 DEBUG(1, "restore_and_detach pid %d pid_of_save_regs %d\n",
364 pid, pid_of_save_regs);
366 if (pid_of_save_regs) {
367 /* In case the 'main pid' has been continued, we need to stop it
368 before resetting the registers. */
369 if (pid_of_save_regs_continued) {
370 pid_of_save_regs_continued = False;
371 if (!stop(pid_of_save_regs, "sigstop before reset regs"))
372 DEBUG(0, "Could not sigstop before reset");
375 DEBUG(1, "setregs restore registers pid %d\n", pid_of_save_regs);
376 if (!setregs(pid_of_save_regs, &reg_save)) {
377 ERROR(errno, "setregs restore registers pid %d after cont\n",
378 pid_of_save_regs);
381 /* Now, we transmit all the signals we have queued. */
382 if (signal_queue_sz > 0) {
383 int i;
384 for (i = 0; i < signal_queue_sz; i++) {
385 DEBUG(1, "PTRACE_CONT to transmit queued signal %d\n",
386 signal_queue[i].si_signo);
387 res = ptrace (PT_CONTINUE, pid_of_save_regs, (caddr_t)1,
388 signal_queue[i].si_signo);
389 if (res != 0)
390 ERROR(errno, "PT_CONTINUE with signal %d\n",
391 signal_queue[i].si_signo);
392 if (!stop(pid_of_save_regs, "sigstop after transmit sig"))
393 DEBUG(0, "Could not sigstop after transmit sig");
395 free (signal_queue);
396 signal_queue = NULL;
397 signal_queue_sz = 0;
399 pid_of_save_regs = 0;
400 } else {
401 DEBUG(1, "PTRACE_SETREGS restore registers: no pid\n");
403 if (signal_queue)
404 ERROR (0, "One or more signals queued were not delivered. "
405 "First signal: %d\n", signal_queue[0].si_signo);
406 detach_from_all_threads(pid);
409 Bool invoker_invoke_gdbserver (pid_t pid)
411 long res;
412 Bool stopped;
413 struct reg reg_mod;
414 Addr sp __attribute__((unused)); // Not used on all platforms.
416 /* A specific int value is passed to invoke_gdbserver, to check
417 everything goes according to the plan. */
418 const int check = 0x8BADF00D; // ate bad food.
420 const Addr bad_return = 0;
421 // A bad return address will be pushed on the stack.
422 // The function invoke_gdbserver cannot return. If ever it returns, a NULL
423 // address pushed on the stack should ensure this is detected.
425 /* Not yet attached. If problem, vgdb can abort,
426 no cleanup needed. */
428 DEBUG(1, "attach to 'main' pid %d\n", pid);
429 if (!attach(pid, "attach main pid")) {
430 ERROR(0, "error attach main pid %d\n", pid);
431 return False;
434 /* Now, we are attached. If problem, detach and return. */
436 DEBUG(1, "calling getregs\n");
438 if (!getregs(pid, &reg_mod)) {
439 detach_from_all_threads(pid);
440 return False;
442 reg_save = reg_mod;
444 DEBUG(1, "getregs call succeeded\n");
446 #if defined(VGA_x86)
447 sp = reg_mod.r_esp;
448 #elif defined(VGA_amd64)
449 sp = reg_mod.r_rsp;
450 if (shared32 != NULL) {
451 /* 64bit vgdb speaking with a 32bit executable.
452 To have system call restart properly, we need to sign extend rax.
453 For more info:
454 web search '[patch] Fix syscall restarts for amd64->i386 biarch'
455 e.g. http://sourceware.org/ml/gdb-patches/2009-11/msg00592.html */
456 *(long *)&reg_save.r_rax = *(int*)&reg_save.r_rax;
457 DEBUG(1, "Sign extending %8.8lx to %8.8lx\n",
458 reg_mod.r_rax, reg_save.r_rax);
460 #else
461 I_die_here : (sp) architecture missing in vgdb-invoker-freebsd.c
462 #endif
465 // the magic below is derived from spying what gdb sends to
466 // the (classical) gdbserver when invoking a C function.
467 if (shared32 != NULL) {
468 // vgdb speaking with a 32bit executable.
469 #if defined(VGA_x86) || defined(VGA_amd64)
470 const int regsize = 4;
471 int rw;
472 /* push check arg on the stack */
473 sp = sp - regsize;
474 DEBUG(1, "push check arg ptrace_write_memory\n");
475 assert(regsize == sizeof(check));
476 rw = ptrace_write_memory(pid, sp,
477 &check,
478 regsize);
479 if (rw != 0) {
480 ERROR(rw, "push check arg ptrace_write_memory\n");
481 detach_from_all_threads(pid);
482 return False;
485 sp = sp - regsize;
486 DEBUG(1, "push bad_return return address ptrace_write_memory\n");
487 // Note that for a 64 bits vgdb, only 4 bytes of NULL bad_return
488 // are written.
489 rw = ptrace_write_memory(pid, sp,
490 &bad_return,
491 regsize);
492 if (rw != 0) {
493 ERROR(rw, "push bad_return return address ptrace_write_memory\n");
494 detach_from_all_threads(pid);
495 return False;
497 #if defined(VGA_x86)
498 /* set ebp, esp, eip and orig_eax to invoke gdbserver */
499 // compiled in 32bits, speaking with a 32bits exe
500 reg_mod.r_ebp = sp; // bp set to sp
501 reg_mod.r_esp = sp;
502 reg_mod.r_eip = shared32->invoke_gdbserver;
503 #elif defined(VGA_amd64)
504 /* set ebp, esp, eip and orig_eax to invoke gdbserver */
505 // compiled in 64bits, speaking with a 32bits exe
506 reg_mod.r_rbp = sp; // bp set to sp
507 reg_mod.r_rsp = sp;
508 reg_mod.r_rip = shared32->invoke_gdbserver;
509 #else
510 I_die_here : not x86 or amd64 in x86/amd64 section/
511 #endif
513 #else
514 I_die_here : architecture missing in vgdb-invoker-freebsd.c
515 #endif
518 else if (shared64 != NULL) {
519 #if defined(VGA_x86)
520 assert(0); // cannot vgdb a 64 bits executable with a 32 bits exe
521 #elif defined(VGA_amd64)
522 // vgdb speaking with a 64 bit executable.
523 const int regsize = 8;
524 int rw;
526 /* give check arg in rdi */
527 reg_mod.r_rdi = check;
529 /* push return address on stack : return to breakaddr */
530 sp &= ~0xf; // keep the stack aligned on 16 bytes ...
531 //sp = sp - 128; // do not touch the amd64 redzone
532 sp = sp - regsize;
533 DEBUG(1, "push bad_return return address ptrace_write_memory\n");
534 rw = ptrace_write_memory(pid, sp,
535 &bad_return,
536 sizeof(bad_return));
537 if (rw != 0) {
538 ERROR(rw, "push bad_return return address ptrace_write_memory\n");
539 detach_from_all_threads(pid);
540 return False;
543 /* set rbp, rsp, rip and orig_rax to invoke gdbserver */
544 reg_mod.r_rbp = sp; // bp set to sp
545 reg_mod.r_rsp = sp;
546 reg_mod.r_rip = shared64->invoke_gdbserver;
548 #else
549 I_die_here: architecture missing in vgdb-invoker-freebsd.c
550 #endif
552 else {
553 assert(0);
556 DEBUG(1, "calling setregs\n");
558 if (!setregs(pid, &reg_mod)) {
559 detach_from_all_threads(pid);
560 return False;
563 DEBUG(1, "setregs succeeded\n");
565 /* Now that we have modified the registers, we set
566 pid_of_save_regs to indicate that restore_and_detach
567 must restore the registers in case of cleanup. */
568 pid_of_save_regs = pid;
569 pid_of_save_regs_continued = False;
572 /* We PTRACE_CONT-inue pid.
573 Either gdbserver will be invoked directly (if all
574 threads are interruptible) or gdbserver will be
575 called soon by the scheduler. In the first case,
576 pid will stop on the break inserted above when
577 gdbserver returns. In the 2nd case, the break will
578 be encountered directly. */
579 DEBUG(1, "PT_CONTINUE to invoke\n");
580 /* an address of 1 means continue without modifying IP
581 since we already set IP above there is no need to set it here */
582 res = ptrace (PT_CONTINUE, pid, (caddr_t)1, 0);
583 if (res != 0) {
584 ERROR(errno, "PT_CONTINUE\n");
585 restore_and_detach(pid);
586 return False;
588 pid_of_save_regs_continued = True;
589 /* Wait for SIGSTOP generated by m_gdbserver.c give_control_back_to_vgdb */
590 stopped = waitstopped (pid, SIGSTOP,
591 "waitpid status after PTRACE_CONT to invoke");
592 if (stopped) {
593 /* Here pid has properly stopped on the break. */
594 pid_of_save_regs_continued = False;
595 restore_and_detach(pid);
596 return True;
597 } else {
598 /* Whatever kind of problem happened. We shutdown. */
599 shutting_down = True;
600 return False;
604 void invoker_cleanup_restore_and_detach(void *v_pid)
606 DEBUG(1, "invoker_cleanup_restore_and_detach dying: %d\n", dying);
607 if (!dying)
608 restore_and_detach(*(int*)v_pid);
611 void invoker_restrictions_msg(void)
615 void invoker_valgrind_dying(void)
617 /* Avoid messing up with registers of valgrind when it is dying. */
618 pid_of_save_regs_continued = False;
619 dying = True;