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