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