blob: a9b79cd23e51a840a1f0f66b85725c2051f2f647 [file] [log] [blame]
philippe3c761f02013-12-01 14:56:28 +00001/*--------------------------------------------------------------------*/
2/*--- Implementation of vgdb invoker subsystem via ptrace() calls. ---*/
3/*--------------------------------------------------------------------*/
4
5/*
6 This file is part of Valgrind, a dynamic binary instrumentation
7 framework.
8
9 Copyright (C) 2011-2013 Philippe Waroquiers
10
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 02111-1307, USA.
25
26 The GNU General Public License is contained in the file COPYING.
27*/
28
29#include "config.h"
30
31#include "vgdb.h"
32#include "pub_core_threadstate.h"
33
34#include <alloca.h>
35#include <assert.h>
36#include <errno.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <sys/ptrace.h>
41#include <sys/time.h>
42#include <sys/user.h>
43#include <sys/wait.h>
44
philippec07369b2014-05-17 13:50:02 +000045#ifdef PTRACE_GETREGSET
46// TBD: better have a configure test instead ?
47#define HAVE_PTRACE_GETREGSET
48
49// A bi-arch build using PTRACE_GET/SETREGSET needs
50// some conversion code for register structures.
51// So, better do not use PTRACE_GET/SETREGSET
52// Rather we use PTRACE_GETREGS or PTRACE_PEEKUSER.
53
54// The only platform on which we must use PTRACE_GETREGSET is arm64.
55// The resulting vgdb cannot work in a bi-arch setup.
56// -1 means we will check that PTRACE_GETREGSET works.
57# if defined(VGA_arm64)
58#define USE_PTRACE_GETREGSET
59# endif
60#endif
61
62#include <sys/uio.h>
63#include <elf.h>
64
65#include <sys/procfs.h>
66
florian3629f4a2014-09-24 19:13:01 +000067// glibc versions prior to 2.5 do not define PTRACE_GETSIGINFO on
68// the platforms we support.
69#if !((__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 5))
florian3b4ef112014-07-27 12:24:46 +000070# ifndef PTRACE_GETSIGINFO
71# define PTRACE_GETSIGINFO 0x4202
72# endif
73#endif
74
philippe4769c3c2015-01-04 20:33:50 +000075// 32-bit or 64-bit wide, depending on primary architecture.
76typedef Addr CORE_ADDR;
77typedef Addr PTRACE_XFER_TYPE;
philippe3c761f02013-12-01 14:56:28 +000078typedef void* PTRACE_ARG3_TYPE;
philippe3c761f02013-12-01 14:56:28 +000079
philippe6654de82014-04-15 22:35:23 +000080// if > 0, pid for which registers have to be restored.
81// if == 0, means we have not yet called setregs (or have already
82// restored the registers).
83static int pid_of_save_regs = 0;
philippe3c761f02013-12-01 14:56:28 +000084/* True if we have continued pid_of_save_regs after PTRACE_ATTACH. */
85static Bool pid_of_save_regs_continued = False;
philippe6654de82014-04-15 22:35:23 +000086// When setregs has been called to change the registers of pid_of_save_regs,
87// vgdb cannot transmit the signals intercepted during ptrace.
88// So, we queue them, and will deliver them when detaching.
89// See function waitstopped for more info.
90static int signal_queue_sz = 0;
91static siginfo_t *signal_queue;
philippe3c761f02013-12-01 14:56:28 +000092
93/* True when loss of connection indicating that the Valgrind
94 process is dying. */
95static Bool dying = False;
96
97/* ptrace_(read|write)_memory are modified extracts of linux-low.c
98 from gdb 6.6. Copyrighted FSF */
99/* Copy LEN bytes from valgrind memory starting at MEMADDR
100 to vgdb memory starting at MYADDR. */
101static
102int ptrace_read_memory (pid_t inferior_pid, CORE_ADDR memaddr,
103 void *myaddr, size_t len)
104{
105 register int i;
106 /* Round starting address down to longword boundary. */
107 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
108 /* Round ending address up; get number of longwords that makes. */
109 register int count
110 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
111 / sizeof (PTRACE_XFER_TYPE);
112 /* Allocate buffer of that many longwords. */
113 register PTRACE_XFER_TYPE *buffer
114 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
115
116 /* Read all the longwords */
117 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) {
118 errno = 0;
119 buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid,
120 (PTRACE_ARG3_TYPE) addr, 0);
121 if (errno)
122 return errno;
123 }
124
125 /* Copy appropriate bytes out of the buffer. */
126 memcpy (myaddr,
127 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len);
128
129 return 0;
130}
131
132/* Copy LEN bytes of data from vgdb memory at MYADDR
133 to valgrind memory at MEMADDR.
134 On failure (cannot write the valgrind memory)
135 returns the value of errno. */
136__attribute__((unused)) /* not used on all platforms */
137static
138int ptrace_write_memory (pid_t inferior_pid, CORE_ADDR memaddr,
139 const void *myaddr, size_t len)
140{
141 register int i;
142 /* Round starting address down to longword boundary. */
143 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
144 /* Round ending address up; get number of longwords that makes. */
145 register int count
146 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
147 / sizeof (PTRACE_XFER_TYPE);
148 /* Allocate buffer of that many longwords. */
149 register PTRACE_XFER_TYPE *buffer
150 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
151
152 if (debuglevel >= 1) {
153 DEBUG (1, "Writing ");
154 for (i = 0; i < len; i++)
155 PDEBUG (1, "%02x", ((const unsigned char*)myaddr)[i]);
156 PDEBUG(1, " to %p\n", (void *) memaddr);
157 }
158
159 /* Fill start and end extra bytes of buffer with existing memory data. */
160
161 buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid,
162 (PTRACE_ARG3_TYPE) addr, 0);
163
164 if (count > 1) {
165 buffer[count - 1]
166 = ptrace (PTRACE_PEEKTEXT, inferior_pid,
167 (PTRACE_ARG3_TYPE) (addr + (count - 1)
168 * sizeof (PTRACE_XFER_TYPE)),
169 0);
170 }
171
172 /* Copy data to be written over corresponding part of buffer */
173
174 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
175 myaddr, len);
176
177 /* Write the entire buffer. */
178
179 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) {
180 errno = 0;
181 ptrace (PTRACE_POKETEXT, inferior_pid,
182 (PTRACE_ARG3_TYPE) addr, buffer[i]);
183 if (errno)
184 return errno;
185 }
186
187 return 0;
188}
189
190/* subset of VG_(threads) needed for vgdb ptrace.
191 This is initialized when process is attached. */
192typedef struct {
193 ThreadStatus status;
194 Int lwpid;
195}
196VgdbThreadState;
197static VgdbThreadState vgdb_threads[VG_N_THREADS];
198
199static const
200HChar* name_of_ThreadStatus ( ThreadStatus status )
201{
202 switch (status) {
203 case VgTs_Empty: return "VgTs_Empty";
204 case VgTs_Init: return "VgTs_Init";
205 case VgTs_Runnable: return "VgTs_Runnable";
206 case VgTs_WaitSys: return "VgTs_WaitSys";
207 case VgTs_Yielding: return "VgTs_Yielding";
208 case VgTs_Zombie: return "VgTs_Zombie";
209 default: return "VgTs_???";
210 }
211}
212
213static
214char *status_image (int status)
215{
florian7b7d5942014-12-19 20:29:22 +0000216 static char result[256]; // large enough
philippe3c761f02013-12-01 14:56:28 +0000217 int sz = 0;
218#define APPEND(...) sz += snprintf (result+sz, 256 - sz - 1, __VA_ARGS__)
219
220 result[0] = 0;
221
222 if (WIFEXITED(status))
223 APPEND ("WIFEXITED %d ", WEXITSTATUS(status));
224
225 if (WIFSIGNALED(status)) {
226 APPEND ("WIFSIGNALED %d ", WTERMSIG(status));
227 if (WCOREDUMP(status)) APPEND ("WCOREDUMP ");
228 }
229
230 if (WIFSTOPPED(status))
231 APPEND ("WIFSTOPPED %d ", WSTOPSIG(status));
232
233#ifdef WIFCONTINUED
234 if (WIFCONTINUED(status))
235 APPEND ("WIFCONTINUED ");
236#endif
237
238 return result;
239#undef APPEND
240}
241
242/* Wait till the process pid is reported as stopped with signal_expected.
243 If other signal(s) than signal_expected are received, waitstopped
244 will pass them to pid, waiting for signal_expected to stop pid.
245 Returns True when process is in stopped state with signal_expected.
246 Returns False if a problem was encountered while waiting for pid
247 to be stopped.
248
249 If pid is reported as being dead/exited, waitstopped will return False.
250*/
251static
252Bool waitstopped (pid_t pid, int signal_expected, const char *msg)
253{
254 pid_t p;
255 int status = 0;
256 int signal_received;
257 int res;
258
259 while (1) {
260 DEBUG(1, "waitstopped %s before waitpid signal_expected %d\n",
261 msg, signal_expected);
262 p = waitpid(pid, &status, __WALL);
263 DEBUG(1, "after waitpid pid %d p %d status 0x%x %s\n", pid, p,
264 status, status_image (status));
265 if (p != pid) {
266 ERROR(errno, "%s waitpid pid %d in waitstopped %d status 0x%x %s\n",
267 msg, pid, p, status, status_image (status));
268 return False;
269 }
270
271 if (WIFEXITED(status)) {
272 shutting_down = True;
273 return False;
274 }
275
276 assert (WIFSTOPPED(status));
277 signal_received = WSTOPSIG(status);
278 if (signal_received == signal_expected)
279 break;
280
281 /* pid received a signal which is not the signal we are waiting for.
philippe6654de82014-04-15 22:35:23 +0000282 If we have not (yet) changed the registers of the inferior
283 or we have (already) reset them, we can transmit the signal.
284
285 If we have already set the registers of the inferior, we cannot
286 transmit the signal, as this signal would arrive when the
287 gdbserver code runs. And valgrind only expects signals to
288 arrive in a small code portion around
289 client syscall logic, where signal are unmasked (see e.g.
290 m_syswrap/syscall-x86-linux.S ML_(do_syscall_for_client_WRK).
291
292 As ptrace is forcing a call to gdbserver by jumping
293 'out of this region', signals are not masked, but
294 will arrive outside of the allowed/expected code region.
295 So, if we have changed the registers of the inferior, we
296 rather queue the signal to transmit them when detaching,
297 after having restored the registers to the initial values. */
298 if (pid_of_save_regs) {
299 siginfo_t *newsiginfo;
300
301 // realloc a bigger queue, and store new signal at the end.
302 // This is not very efficient but we assume not many sigs are queued.
303 signal_queue_sz++;
philippe4769c3c2015-01-04 20:33:50 +0000304 signal_queue = vrealloc(signal_queue,
305 sizeof(siginfo_t) * signal_queue_sz);
philippe6654de82014-04-15 22:35:23 +0000306 newsiginfo = signal_queue + (signal_queue_sz - 1);
307
308 res = ptrace (PTRACE_GETSIGINFO, pid, NULL, newsiginfo);
309 if (res != 0) {
310 ERROR(errno, "PTRACE_GETSIGINFO failed: signal lost !!!!\n");
311 signal_queue_sz--;
312 } else
313 DEBUG(1, "waitstopped PTRACE_CONT, queuing signal %d"
314 " si_signo %d si_pid %d\n",
315 signal_received, newsiginfo->si_signo, newsiginfo->si_pid);
316 res = ptrace (PTRACE_CONT, pid, NULL, 0);
317 } else {
318 DEBUG(1, "waitstopped PTRACE_CONT with signal %d\n", signal_received);
319 res = ptrace (PTRACE_CONT, pid, NULL, signal_received);
320 }
philippe3c761f02013-12-01 14:56:28 +0000321 if (res != 0) {
322 ERROR(errno, "waitstopped PTRACE_CONT\n");
323 return False;
324 }
325 }
326
327 return True;
328}
329
330/* Stops the given pid, wait for the process to be stopped.
331 Returns True if succesful, False otherwise.
332 msg is used in tracing and error reporting. */
333static
334Bool stop (pid_t pid, const char *msg)
335{
336 long res;
337
338 DEBUG(1, "%s SIGSTOP pid %d\n", msg, pid);
339 res = kill (pid, SIGSTOP);
340 if (res != 0) {
341 ERROR(errno, "%s SIGSTOP pid %d %ld\n", msg, pid, res);
342 return False;
343 }
344
345 return waitstopped (pid, SIGSTOP, msg);
346
347}
348
349/* Attaches to given pid, wait for the process to be stopped.
350 Returns True if succesful, False otherwise.
351 msg is used in tracing and error reporting. */
352static
353Bool attach (pid_t pid, const char *msg)
354{
355 long res;
356 static Bool output_error = True;
357 static Bool initial_attach = True;
358 // For a ptrace_scope protected system, we do not want to output
359 // repetitively attach error. We will output once an error
360 // for the initial_attach. Once the 1st attach has succeeded, we
361 // again show all errors.
362
363 DEBUG(1, "%s PTRACE_ATTACH pid %d\n", msg, pid);
364 res = ptrace (PTRACE_ATTACH, pid, NULL, NULL);
365 if (res != 0) {
366 if (output_error || debuglevel > 0) {
367 ERROR(errno, "%s PTRACE_ATTACH pid %d %ld\n", msg, pid, res);
368 if (initial_attach)
369 output_error = False;
370 }
371 return False;
372 }
373
374 initial_attach = False;
375 output_error = True;
376 return waitstopped(pid, SIGSTOP, msg);
377}
378
379/* once we are attached to the pid, get the list of threads and stop
380 them all.
381 Returns True if all threads properly suspended, False otherwise. */
382static
383Bool acquire_and_suspend_threads (pid_t pid)
384{
385 int i;
386 int rw;
387 Bool pid_found = False;
388 Addr vgt;
389 int sz_tst;
390 int off_status;
391 int off_lwpid;
392 int nr_live_threads = 0;
393
394 if (shared32 != NULL) {
395 vgt = shared32->threads;
396 sz_tst = shared32->sizeof_ThreadState;
397 off_status = shared32->offset_status;
398 off_lwpid = shared32->offset_lwpid;
399 }
400 else if (shared64 != NULL) {
401 vgt = shared64->threads;
402 sz_tst = shared64->sizeof_ThreadState;
403 off_status = shared64->offset_status;
404 off_lwpid = shared64->offset_lwpid;
405 } else {
406 assert (0);
407 }
408
409 /* note: the entry 0 is unused */
410 for (i = 1; i < VG_N_THREADS; i++) {
411 vgt += sz_tst;
412 rw = ptrace_read_memory(pid, vgt+off_status,
413 &(vgdb_threads[i].status),
414 sizeof(ThreadStatus));
415 if (rw != 0) {
416 ERROR(rw, "status ptrace_read_memory\n");
417 return False;
418 }
419
420 rw = ptrace_read_memory(pid, vgt+off_lwpid,
421 &(vgdb_threads[i].lwpid),
422 sizeof(Int));
423 if (rw != 0) {
424 ERROR(rw, "lwpid ptrace_read_memory\n");
425 return False;
426 }
427
428 if (vgdb_threads[i].status != VgTs_Empty) {
429 DEBUG(1, "found tid %d status %s lwpid %d\n",
430 i, name_of_ThreadStatus(vgdb_threads[i].status),
431 vgdb_threads[i].lwpid);
432 nr_live_threads++;
433 if (vgdb_threads[i].lwpid <= 1) {
434 if (vgdb_threads[i].lwpid == 0
435 && vgdb_threads[i].status == VgTs_Init) {
436 DEBUG(1, "not set lwpid tid %d status %s lwpid %d\n",
437 i, name_of_ThreadStatus(vgdb_threads[i].status),
438 vgdb_threads[i].lwpid);
439 } else {
440 ERROR(1, "unexpected lwpid tid %d status %s lwpid %d\n",
441 i, name_of_ThreadStatus(vgdb_threads[i].status),
442 vgdb_threads[i].lwpid);
443 }
444 /* in case we have a VtTs_Init thread with lwpid not yet set,
445 we try again later. */
446 return False;
447 }
448 if (vgdb_threads[i].lwpid == pid) {
449 assert (!pid_found);
450 assert (i == 1);
451 pid_found = True;
452 } else {
453 if (!attach(vgdb_threads[i].lwpid, "attach_thread")) {
454 ERROR(0, "ERROR attach pid %d tid %d\n",
455 vgdb_threads[i].lwpid, i);
456 return False;
457 }
458 }
459 }
460 }
461 /* If we found no thread, it means the process is stopping, and
462 we better do not force anything to happen during that. */
463 if (nr_live_threads > 0)
464 return True;
465 else
466 return False;
467}
468
469static
470void detach_from_all_threads (pid_t pid)
471{
472 int i;
473 long res;
474 Bool pid_found = False;
475
476 /* detach from all the threads */
477 for (i = 1; i < VG_N_THREADS; i++) {
478 if (vgdb_threads[i].status != VgTs_Empty) {
479 if (vgdb_threads[i].status == VgTs_Init
480 && vgdb_threads[i].lwpid == 0) {
481 DEBUG(1, "skipping PTRACE_DETACH pid %d tid %d status %s\n",
482 vgdb_threads[i].lwpid, i,
483 name_of_ThreadStatus (vgdb_threads[i].status));
484 } else {
485 if (vgdb_threads[i].lwpid == pid) {
486 assert (!pid_found);
487 pid_found = True;
488 }
489 DEBUG(1, "PTRACE_DETACH pid %d tid %d status %s\n",
490 vgdb_threads[i].lwpid, i,
491 name_of_ThreadStatus (vgdb_threads[i].status));
492 res = ptrace (PTRACE_DETACH, vgdb_threads[i].lwpid, NULL, NULL);
493 if (res != 0) {
494 ERROR(errno, "PTRACE_DETACH pid %d tid %d status %s res %ld\n",
495 vgdb_threads[i].lwpid, i,
496 name_of_ThreadStatus (vgdb_threads[i].status),
497 res);
498 }
499 }
500 }
501 }
502
503 if (!pid_found && pid) {
504 /* No threads are live. Process is busy stopping.
505 We need to detach from pid explicitely. */
506 DEBUG(1, "no thread live => PTRACE_DETACH pid %d\n", pid);
507 res = ptrace (PTRACE_DETACH, pid, NULL, NULL);
508 if (res != 0)
509 ERROR(errno, "PTRACE_DETACH pid %d res %ld\n", pid, res);
510 }
511}
512
philippec07369b2014-05-17 13:50:02 +0000513# if defined(VGA_arm64)
mjw2a429f52014-07-18 20:45:37 +0000514/* arm64 is extra special, old glibc defined kernel user_pt_regs, but
515 newer glibc instead define user_regs_struct. */
516# ifdef HAVE_SYS_USER_REGS
517static struct user_regs_struct user_save;
518# else
philippec07369b2014-05-17 13:50:02 +0000519static struct user_pt_regs user_save;
mjw2a429f52014-07-18 20:45:37 +0000520# endif
philippec07369b2014-05-17 13:50:02 +0000521# else
philippe3c761f02013-12-01 14:56:28 +0000522static struct user user_save;
philippec07369b2014-05-17 13:50:02 +0000523# endif
philippe3c761f02013-12-01 14:56:28 +0000524// The below indicates if ptrace_getregs (and ptrace_setregs) can be used.
525// Note that some linux versions are defining PTRACE_GETREGS but using
526// it gives back EIO.
527// has_working_ptrace_getregs can take the following values:
528// -1 : PTRACE_GETREGS is defined
529// runtime check not yet done.
530// 0 : PTRACE_GETREGS runtime check has failed.
531// 1 : PTRACE_GETREGS defined and runtime check ok.
532#ifdef HAVE_PTRACE_GETREGS
533static int has_working_ptrace_getregs = -1;
534#endif
philippec07369b2014-05-17 13:50:02 +0000535// Similar but for PTRACE_GETREGSET
536#ifdef HAVE_PTRACE_GETREGSET
537static int has_working_ptrace_getregset = -1;
538#endif
philippe3c761f02013-12-01 14:56:28 +0000539
540/* Get the registers from pid into regs.
541 regs_bsz value gives the length of *regs.
542 Returns True if all ok, otherwise False. */
543static
544Bool getregs (pid_t pid, void *regs, long regs_bsz)
545{
546 DEBUG(1, "getregs regs_bsz %ld\n", regs_bsz);
philippec07369b2014-05-17 13:50:02 +0000547# ifdef HAVE_PTRACE_GETREGSET
548# ifndef USE_PTRACE_GETREGSET
549 if (has_working_ptrace_getregset)
550 DEBUG(1, "PTRACE_GETREGSET defined, not used (yet?) by vgdb\n");
551 has_working_ptrace_getregset = 0;
552# endif
553 if (has_working_ptrace_getregset) {
554 // Platforms having GETREGSET
555 long res;
556 elf_gregset_t elf_regs;
557 struct iovec iovec;
558
florianee0ee032014-09-02 14:21:25 +0000559 DEBUG(1, "getregs PTRACE_GETREGSET sizeof(elf_regs) %zu\n",
560 sizeof(elf_regs));
philippec07369b2014-05-17 13:50:02 +0000561 iovec.iov_base = regs;
562 iovec.iov_len = sizeof(elf_regs);
563
564 res = ptrace (PTRACE_GETREGSET, pid, NT_PRSTATUS, &iovec);
565 if (res == 0) {
566 if (has_working_ptrace_getregset == -1) {
567 // First call to PTRACE_GETREGSET succesful =>
568 has_working_ptrace_getregset = 1;
569 DEBUG(1, "detected a working PTRACE_GETREGSET\n");
570 }
571 assert (has_working_ptrace_getregset == 1);
572 return True;
573 }
574 else if (has_working_ptrace_getregset == 1) {
575 // We had a working call, but now it fails.
576 // This is unexpected.
577 ERROR(errno, "PTRACE_GETREGSET %ld\n", res);
578 return False;
579 } else {
580 // Check this is the first call:
581 assert (has_working_ptrace_getregset == -1);
582 if (errno == EIO) {
583 DEBUG(1, "detected a broken PTRACE_GETREGSET with EIO\n");
584 has_working_ptrace_getregset = 0;
585 // Fall over to the PTRACE_GETREGS or PTRACE_PEEKUSER case.
586 } else {
587 ERROR(errno, "broken PTRACE_GETREGSET unexpected errno %ld\n", res);
588 return False;
589 }
590 }
591 }
592# endif
593
philippe3c761f02013-12-01 14:56:28 +0000594# ifdef HAVE_PTRACE_GETREGS
595 if (has_working_ptrace_getregs) {
596 // Platforms having GETREGS
597 long res;
598 DEBUG(1, "getregs PTRACE_GETREGS\n");
599 res = ptrace (PTRACE_GETREGS, pid, NULL, regs);
600 if (res == 0) {
601 if (has_working_ptrace_getregs == -1) {
602 // First call to PTRACE_GETREGS succesful =>
603 has_working_ptrace_getregs = 1;
604 DEBUG(1, "detected a working PTRACE_GETREGS\n");
605 }
606 assert (has_working_ptrace_getregs == 1);
607 return True;
608 }
609 else if (has_working_ptrace_getregs == 1) {
610 // We had a working call, but now it fails.
611 // This is unexpected.
612 ERROR(errno, "PTRACE_GETREGS %ld\n", res);
613 return False;
614 } else {
615 // Check this is the first call:
616 assert (has_working_ptrace_getregs == -1);
617 if (errno == EIO) {
618 DEBUG(1, "detected a broken PTRACE_GETREGS with EIO\n");
619 has_working_ptrace_getregs = 0;
620 // Fall over to the PTRACE_PEEKUSER case.
621 } else {
622 ERROR(errno, "broken PTRACE_GETREGS unexpected errno %ld\n", res);
623 return False;
624 }
625 }
626 }
627# endif
628
629 // We assume PTRACE_PEEKUSER is defined everywhere.
630 {
631# ifdef PT_ENDREGS
632 long peek_bsz = PT_ENDREGS;
633 assert (peek_bsz <= regs_bsz);
634# else
635 long peek_bsz = regs_bsz-1;
636# endif
637 char *pregs = (char *) regs;
638 long offset;
639 errno = 0;
640 DEBUG(1, "getregs PTRACE_PEEKUSER(s) peek_bsz %ld\n", peek_bsz);
641 for (offset = 0; offset < peek_bsz; offset = offset + sizeof(long)) {
642 *(long *)(pregs+offset) = ptrace(PTRACE_PEEKUSER, pid, offset, NULL);
643 if (errno != 0) {
644 ERROR(errno, "PTRACE_PEEKUSER offset %ld\n", offset);
645 return False;
646 }
647 }
648 return True;
649 }
650
philippec07369b2014-05-17 13:50:02 +0000651 // If neither of PTRACE_GETREGSET PTRACE_GETREGS PTRACE_PEEKUSER have
652 // returned, then we are in serious trouble.
philippe3c761f02013-12-01 14:56:28 +0000653 assert (0);
654}
655
656/* Set the registers of pid to regs.
657 regs_bsz value gives the length of *regs.
658 Returns True if all ok, otherwise False. */
659static
660Bool setregs (pid_t pid, void *regs, long regs_bsz)
661{
662 DEBUG(1, "setregs regs_bsz %ld\n", regs_bsz);
philippec07369b2014-05-17 13:50:02 +0000663
664// Note : the below is checking for GETREGSET, not SETREGSET
665// as if one is defined and working, the other one should also work.
666# ifdef HAVE_PTRACE_GETREGSET
667 if (has_working_ptrace_getregset) {
668 // Platforms having SETREGSET
669 long res;
670 elf_gregset_t elf_regs;
671 struct iovec iovec;
672
673 // setregset can never be called before getregset has done a runtime check.
674 assert (has_working_ptrace_getregset == 1);
florianee0ee032014-09-02 14:21:25 +0000675 DEBUG(1, "setregs PTRACE_SETREGSET sizeof(elf_regs) %zu\n",
676 sizeof(elf_regs));
philippec07369b2014-05-17 13:50:02 +0000677 iovec.iov_base = regs;
678 iovec.iov_len = sizeof(elf_regs);
679 res = ptrace (PTRACE_SETREGSET, pid, NT_PRSTATUS, &iovec);
680 if (res != 0) {
681 ERROR(errno, "PTRACE_SETREGSET %ld\n", res);
682 return False;
683 }
684 return True;
685 }
686# endif
687
philippe3c761f02013-12-01 14:56:28 +0000688// Note : the below is checking for GETREGS, not SETREGS
689// as if one is defined and working, the other one should also work.
690# ifdef HAVE_PTRACE_GETREGS
691 if (has_working_ptrace_getregs) {
692 // Platforms having SETREGS
693 long res;
694 // setregs can never be called before getregs has done a runtime check.
695 assert (has_working_ptrace_getregs == 1);
696 DEBUG(1, "setregs PTRACE_SETREGS\n");
697 res = ptrace (PTRACE_SETREGS, pid, NULL, regs);
698 if (res != 0) {
699 ERROR(errno, "PTRACE_SETREGS %ld\n", res);
700 return False;
701 }
702 return True;
703 }
704# endif
705
706 {
707 char *pregs = (char *) regs;
708 long offset;
709 long res;
710# ifdef PT_ENDREGS
711 long peek_bsz = PT_ENDREGS;
712 assert (peek_bsz <= regs_bsz);
713# else
714 long peek_bsz = regs_bsz-1;
715# endif
716 errno = 0;
717 DEBUG(1, "setregs PTRACE_POKEUSER(s) %ld\n", peek_bsz);
718 for (offset = 0; offset < peek_bsz; offset = offset + sizeof(long)) {
719 res = ptrace(PTRACE_POKEUSER, pid, offset, *(long*)(pregs+offset));
720 if (errno != 0) {
721 ERROR(errno, "PTRACE_POKEUSER offset %ld res %ld\n", offset, res);
722 return False;
723 }
724 }
725 return True;
726 }
727
728 // If neither PTRACE_SETREGS not PTRACE_POKEUSER have returned,
729 // then we are in serious trouble.
730 assert (0);
731}
732
733/* Restore the registers to the saved value, then detaches from all threads */
734static
735void restore_and_detach (pid_t pid)
736{
philippe6654de82014-04-15 22:35:23 +0000737 int res;
738
739 DEBUG(1, "restore_and_detach pid %d pid_of_save_regs %d\n",
740 pid, pid_of_save_regs);
741
philippe3c761f02013-12-01 14:56:28 +0000742 if (pid_of_save_regs) {
743 /* In case the 'main pid' has been continued, we need to stop it
744 before resetting the registers. */
745 if (pid_of_save_regs_continued) {
746 pid_of_save_regs_continued = False;
747 if (!stop(pid_of_save_regs, "sigstop before reset regs"))
748 DEBUG(0, "Could not sigstop before reset");
749 }
750
751 DEBUG(1, "setregs restore registers pid %d\n", pid_of_save_regs);
752 if (!setregs(pid_of_save_regs, &user_save.regs, sizeof(user_save.regs))) {
753 ERROR(errno, "setregs restore registers pid %d after cont\n",
754 pid_of_save_regs);
755 }
philippe6654de82014-04-15 22:35:23 +0000756
757 /* Now, we transmit all the signals we have queued. */
758 if (signal_queue_sz > 0) {
759 int i;
760 for (i = 0; i < signal_queue_sz; i++) {
761 DEBUG(1, "PTRACE_CONT to transmit queued signal %d\n",
762 signal_queue[i].si_signo);
763 res = ptrace (PTRACE_CONT, pid_of_save_regs, NULL,
764 signal_queue[i].si_signo);
765 if (res != 0)
766 ERROR(errno, "PTRACE_CONT with signal %d\n",
767 signal_queue[i].si_signo);
768 if (!stop(pid_of_save_regs, "sigstop after transmit sig"))
769 DEBUG(0, "Could not sigstop after transmit sig");
770 }
771 free (signal_queue);
772 signal_queue = NULL;
773 signal_queue_sz = 0;
774 }
philippe3c761f02013-12-01 14:56:28 +0000775 pid_of_save_regs = 0;
776 } else {
777 DEBUG(1, "PTRACE_SETREGS restore registers: no pid\n");
778 }
philippe6654de82014-04-15 22:35:23 +0000779 if (signal_queue)
780 ERROR (0, "One or more signals queued were not delivered. "
carll7136e5f2014-05-21 19:06:59 +0000781 "First signal: %d\n", signal_queue[0].si_signo);
philippe3c761f02013-12-01 14:56:28 +0000782 detach_from_all_threads(pid);
783}
784
785Bool invoker_invoke_gdbserver (pid_t pid)
786{
787 long res;
788 Bool stopped;
philippec07369b2014-05-17 13:50:02 +0000789# if defined(VGA_arm64)
mjw2a429f52014-07-18 20:45:37 +0000790/* arm64 is extra special, old glibc defined kernel user_pt_regs, but
791 newer glibc instead define user_regs_struct. */
792# ifdef HAVE_SYS_USER_REGS
793 struct user_regs_struct user_mod;
794# else
philippec07369b2014-05-17 13:50:02 +0000795 struct user_pt_regs user_mod;
mjw2a429f52014-07-18 20:45:37 +0000796# endif
philippec07369b2014-05-17 13:50:02 +0000797# else
philippe3c761f02013-12-01 14:56:28 +0000798 struct user user_mod;
philippec07369b2014-05-17 13:50:02 +0000799# endif
philippe3c761f02013-12-01 14:56:28 +0000800 Addr sp;
801 /* A specific int value is passed to invoke_gdbserver, to check
802 everything goes according to the plan. */
803 const int check = 0x8BADF00D; // ate bad food.
804
805 const Addr bad_return = 0;
806 // A bad return address will be pushed on the stack.
807 // The function invoke_gdbserver cannot return. If ever it returns, a NULL
808 // address pushed on the stack should ensure this is detected.
809
810 /* Not yet attached. If problem, vgdb can abort,
811 no cleanup needed. */
812
813 DEBUG(1, "attach to 'main' pid %d\n", pid);
814 if (!attach(pid, "attach main pid")) {
815 ERROR(0, "error attach main pid %d\n", pid);
816 return False;
817 }
818
819 /* Now, we are attached. If problem, detach and return. */
820
821 if (!acquire_and_suspend_threads(pid)) {
822 detach_from_all_threads(pid);
823 /* if the pid does not exist anymore, we better stop */
824 if (kill(pid, 0) != 0)
825 XERROR (errno, "invoke_gdbserver: check for pid %d existence failed\n",
826 pid);
827 return False;
828 }
829
830 if (!getregs(pid, &user_mod.regs, sizeof(user_mod.regs))) {
831 detach_from_all_threads(pid);
832 return False;
833 }
834 user_save = user_mod;
835
836#if defined(VGA_x86)
837 sp = user_mod.regs.esp;
838#elif defined(VGA_amd64)
839 sp = user_mod.regs.rsp;
840 if (shared32 != NULL) {
841 /* 64bit vgdb speaking with a 32bit executable.
842 To have system call restart properly, we need to sign extend rax.
843 For more info:
844 web search '[patch] Fix syscall restarts for amd64->i386 biarch'
845 e.g. http://sourceware.org/ml/gdb-patches/2009-11/msg00592.html */
846 *(long *)&user_save.regs.rax = *(int*)&user_save.regs.rax;
847 DEBUG(1, "Sign extending %8.8lx to %8.8lx\n",
848 user_mod.regs.rax, user_save.regs.rax);
849 }
850#elif defined(VGA_arm)
851 sp = user_mod.regs.uregs[13];
philippec07369b2014-05-17 13:50:02 +0000852#elif defined(VGA_arm64)
853 sp = user_mod.sp;
philippe3c761f02013-12-01 14:56:28 +0000854#elif defined(VGA_ppc32)
855 sp = user_mod.regs.gpr[1];
carllcae0cc22014-08-07 23:17:29 +0000856#elif defined(VGA_ppc64be) || defined(VGA_ppc64le)
philippe3c761f02013-12-01 14:56:28 +0000857 sp = user_mod.regs.gpr[1];
858#elif defined(VGA_s390x)
859 sp = user_mod.regs.gprs[15];
860#elif defined(VGA_mips32)
861 long long *p = (long long *)user_mod.regs;
862 sp = p[29];
863#elif defined(VGA_mips64)
864 sp = user_mod.regs[29];
865#else
philippe4769c3c2015-01-04 20:33:50 +0000866 I_die_here : (sp) architecture missing in vgdb-invoker-ptrace.c
philippe3c761f02013-12-01 14:56:28 +0000867#endif
868
869
870 // the magic below is derived from spying what gdb sends to
871 // the (classical) gdbserver when invoking a C function.
872 if (shared32 != NULL) {
873 // vgdb speaking with a 32bit executable.
874#if defined(VGA_x86) || defined(VGA_amd64)
875 const int regsize = 4;
876 int rw;
877 /* push check arg on the stack */
878 sp = sp - regsize;
879 DEBUG(1, "push check arg ptrace_write_memory\n");
880 assert(regsize == sizeof(check));
881 rw = ptrace_write_memory(pid, sp,
882 &check,
883 regsize);
884 if (rw != 0) {
885 ERROR(rw, "push check arg ptrace_write_memory");
886 detach_from_all_threads(pid);
887 return False;
888 }
889
890 sp = sp - regsize;
891 DEBUG(1, "push bad_return return address ptrace_write_memory\n");
892 // Note that for a 64 bits vgdb, only 4 bytes of NULL bad_return
893 // are written.
894 rw = ptrace_write_memory(pid, sp,
895 &bad_return,
896 regsize);
897 if (rw != 0) {
898 ERROR(rw, "push bad_return return address ptrace_write_memory");
899 detach_from_all_threads(pid);
900 return False;
901 }
902#if defined(VGA_x86)
903 /* set ebp, esp, eip and orig_eax to invoke gdbserver */
904 // compiled in 32bits, speaking with a 32bits exe
905 user_mod.regs.ebp = sp; // bp set to sp
906 user_mod.regs.esp = sp;
907 user_mod.regs.eip = shared32->invoke_gdbserver;
908 user_mod.regs.orig_eax = -1L;
909#elif defined(VGA_amd64)
910 /* set ebp, esp, eip and orig_eax to invoke gdbserver */
911 // compiled in 64bits, speaking with a 32bits exe
912 user_mod.regs.rbp = sp; // bp set to sp
913 user_mod.regs.rsp = sp;
914 user_mod.regs.rip = shared32->invoke_gdbserver;
915 user_mod.regs.orig_rax = -1L;
916#else
917 I_die_here : not x86 or amd64 in x86/amd64 section/
918#endif
919
carllcae0cc22014-08-07 23:17:29 +0000920#elif defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_ppc64le)
philippe3c761f02013-12-01 14:56:28 +0000921 user_mod.regs.nip = shared32->invoke_gdbserver;
922 user_mod.regs.trap = -1L;
923 /* put check arg in register 3 */
924 user_mod.regs.gpr[3] = check;
925 /* put NULL return address in Link Register */
926 user_mod.regs.link = bad_return;
927
928#elif defined(VGA_arm)
929 /* put check arg in register 0 */
930 user_mod.regs.uregs[0] = check;
931 /* put NULL return address in Link Register */
932 user_mod.regs.uregs[14] = bad_return;
933 user_mod.regs.uregs[15] = shared32->invoke_gdbserver;
934
philippec07369b2014-05-17 13:50:02 +0000935#elif defined(VGA_arm64)
936 XERROR(0, "TBD arm64: vgdb a 32 bits executable with a 64 bits exe");
937
philippe3c761f02013-12-01 14:56:28 +0000938#elif defined(VGA_s390x)
939 XERROR(0, "(fn32) s390x has no 32bits implementation");
940#elif defined(VGA_mips32)
941 /* put check arg in register 4 */
942 p[4] = check;
943 /* put NULL return address in ra */
944 p[31] = bad_return;
945 p[34] = shared32->invoke_gdbserver;
946 p[25] = shared32->invoke_gdbserver;
947 /* make stack space for args */
948 p[29] = sp - 32;
949
950#elif defined(VGA_mips64)
951 assert(0); // cannot vgdb a 32 bits executable with a 64 bits exe
952#else
philippe4769c3c2015-01-04 20:33:50 +0000953 I_die_here : architecture missing in vgdb-invoker-ptrace.c
philippe3c761f02013-12-01 14:56:28 +0000954#endif
955 }
956
957 else if (shared64 != NULL) {
958#if defined(VGA_x86)
959 assert(0); // cannot vgdb a 64 bits executable with a 32 bits exe
960#elif defined(VGA_amd64)
961 // vgdb speaking with a 64 bit executable.
962 const int regsize = 8;
963 int rw;
964
965 /* give check arg in rdi */
966 user_mod.regs.rdi = check;
967
968 /* push return address on stack : return to breakaddr */
969 sp = sp - regsize;
970 DEBUG(1, "push bad_return return address ptrace_write_memory\n");
971 rw = ptrace_write_memory(pid, sp,
972 &bad_return,
973 sizeof(bad_return));
974 if (rw != 0) {
975 ERROR(rw, "push bad_return return address ptrace_write_memory");
976 detach_from_all_threads(pid);
977 return False;
978 }
979
980 /* set rbp, rsp, rip and orig_rax to invoke gdbserver */
981 user_mod.regs.rbp = sp; // bp set to sp
982 user_mod.regs.rsp = sp;
983 user_mod.regs.rip = shared64->invoke_gdbserver;
984 user_mod.regs.orig_rax = -1L;
985
986#elif defined(VGA_arm)
987 assert(0); // cannot vgdb a 64 bits executable with a 32 bits exe
philippec07369b2014-05-17 13:50:02 +0000988#elif defined(VGA_arm64)
989 user_mod.regs[0] = check;
990 user_mod.sp = sp;
991 user_mod.pc = shared64->invoke_gdbserver;
992 /* put NULL return address in Link Register */
993 user_mod.regs[30] = bad_return;
994
philippe3c761f02013-12-01 14:56:28 +0000995#elif defined(VGA_ppc32)
996 assert(0); // cannot vgdb a 64 bits executable with a 32 bits exe
carllcae0cc22014-08-07 23:17:29 +0000997#elif defined(VGA_ppc64be)
philippe4769c3c2015-01-04 20:33:50 +0000998 Addr func_addr;
999 Addr toc_addr;
philippe3c761f02013-12-01 14:56:28 +00001000 int rw;
1001 rw = ptrace_read_memory(pid, shared64->invoke_gdbserver,
1002 &func_addr,
philippe4769c3c2015-01-04 20:33:50 +00001003 sizeof(Addr));
philippe3c761f02013-12-01 14:56:28 +00001004 if (rw != 0) {
1005 ERROR(rw, "ppc64 read func_addr\n");
1006 detach_from_all_threads(pid);
1007 return False;
1008 }
1009 rw = ptrace_read_memory(pid, shared64->invoke_gdbserver+8,
1010 &toc_addr,
philippe4769c3c2015-01-04 20:33:50 +00001011 sizeof(Addr));
philippe3c761f02013-12-01 14:56:28 +00001012 if (rw != 0) {
1013 ERROR(rw, "ppc64 read toc_addr\n");
1014 detach_from_all_threads(pid);
1015 return False;
1016 }
1017 // We are not pushing anything on the stack, so it is not
1018 // very clear why the sp has to be decreased, but it seems
1019 // needed. The ppc64 ABI might give some lights on this ?
1020 user_mod.regs.gpr[1] = sp - 220;
1021 user_mod.regs.gpr[2] = toc_addr;
1022 user_mod.regs.nip = func_addr;
1023 user_mod.regs.trap = -1L;
1024 /* put check arg in register 3 */
1025 user_mod.regs.gpr[3] = check;
1026 /* put bad_return return address in Link Register */
1027 user_mod.regs.link = bad_return;
carll582d5822014-08-07 23:35:54 +00001028#elif defined(VGA_ppc64le)
1029 /* LE does not use the function pointer structure used in BE */
1030 user_mod.regs.nip = shared64->invoke_gdbserver;
1031 user_mod.regs.gpr[1] = sp - 512;
1032 user_mod.regs.gpr[12] = user_mod.regs.nip;
1033 user_mod.regs.trap = -1L;
1034 /* put check arg in register 3 */
1035 user_mod.regs.gpr[3] = check;
1036 /* put bad_return return address in Link Register */
1037 user_mod.regs.link = bad_return;
philippe3c761f02013-12-01 14:56:28 +00001038#elif defined(VGA_s390x)
1039 /* put check arg in register r2 */
1040 user_mod.regs.gprs[2] = check;
1041 /* bad_return Return address is in r14 */
1042 user_mod.regs.gprs[14] = bad_return;
1043 /* minimum stack frame */
1044 sp = sp - 160;
1045 user_mod.regs.gprs[15] = sp;
1046 /* set program counter */
1047 user_mod.regs.psw.addr = shared64->invoke_gdbserver;
1048#elif defined(VGA_mips32)
1049 assert(0); // cannot vgdb a 64 bits executable with a 32 bits exe
1050#elif defined(VGA_mips64)
1051 /* put check arg in register 4 */
1052 user_mod.regs[4] = check;
1053 /* put NULL return address in ra */
1054 user_mod.regs[31] = bad_return;
1055 user_mod.regs[34] = shared64->invoke_gdbserver;
1056 user_mod.regs[25] = shared64->invoke_gdbserver;
1057#else
philippe4769c3c2015-01-04 20:33:50 +00001058 I_die_here: architecture missing in vgdb-invoker-ptrace.c
philippe3c761f02013-12-01 14:56:28 +00001059#endif
1060 }
1061 else {
1062 assert(0);
1063 }
1064
1065 if (!setregs(pid, &user_mod.regs, sizeof(user_mod.regs))) {
1066 detach_from_all_threads(pid);
1067 return False;
1068 }
1069 /* Now that we have modified the registers, we set
1070 pid_of_save_regs to indicate that restore_and_detach
1071 must restore the registers in case of cleanup. */
1072 pid_of_save_regs = pid;
1073 pid_of_save_regs_continued = False;
1074
1075
1076 /* We PTRACE_CONT-inue pid.
1077 Either gdbserver will be invoked directly (if all
1078 threads are interruptible) or gdbserver will be
1079 called soon by the scheduler. In the first case,
1080 pid will stop on the break inserted above when
1081 gdbserver returns. In the 2nd case, the break will
1082 be encountered directly. */
1083 DEBUG(1, "PTRACE_CONT to invoke\n");
1084 res = ptrace (PTRACE_CONT, pid, NULL, NULL);
1085 if (res != 0) {
1086 ERROR(errno, "PTRACE_CONT\n");
1087 restore_and_detach(pid);
1088 return False;
1089 }
1090 pid_of_save_regs_continued = True;
1091 /* Wait for SIGSTOP generated by m_gdbserver.c give_control_back_to_vgdb */
1092 stopped = waitstopped (pid, SIGSTOP,
1093 "waitpid status after PTRACE_CONT to invoke");
1094 if (stopped) {
1095 /* Here pid has properly stopped on the break. */
1096 pid_of_save_regs_continued = False;
1097 restore_and_detach(pid);
1098 return True;
1099 } else {
1100 /* Whatever kind of problem happened. We shutdown. */
1101 shutting_down = True;
1102 return False;
1103 }
1104}
1105
1106void invoker_cleanup_restore_and_detach(void *v_pid)
1107{
1108 DEBUG(1, "invoker_cleanup_restore_and_detach dying: %d\n", dying);
1109 if (!dying)
1110 restore_and_detach(*(int*)v_pid);
1111}
1112
1113void invoker_restrictions_msg(void)
1114{
1115}
1116
1117void invoker_valgrind_dying(void)
1118{
1119 /* Avoid messing up with registers of valgrind when it is dying. */
1120 pid_of_save_regs_continued = False;
1121 dying = True;
1122}