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