sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1 | /*--------------------------------------------------------------------*/ |
| 2 | /*--- Relay between gdb and gdbserver embedded in valgrind vgdb.c ---*/ |
| 3 | /*--------------------------------------------------------------------*/ |
| 4 | |
| 5 | /* |
| 6 | This file is part of Valgrind, a dynamic binary instrumentation |
| 7 | framework. |
| 8 | |
sewardj | 03f8d3f | 2012-08-05 15:46:46 +0000 | [diff] [blame] | 9 | Copyright (C) 2011-2012 Philippe Waroquiers |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 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 | */ |
sewardj | 0ba37c9 | 2011-07-12 11:46:24 +0000 | [diff] [blame] | 28 | |
sewardj | cbe3850 | 2011-07-12 11:59:11 +0000 | [diff] [blame] | 29 | #include "pub_core_basics.h" |
| 30 | #include "pub_core_vki.h" |
| 31 | #include "pub_core_libcsetjmp.h" |
| 32 | #include "pub_core_threadstate.h" |
| 33 | #include "pub_core_gdbserver.h" |
florian | b985e2d | 2011-09-29 03:03:45 +0000 | [diff] [blame] | 34 | #include "config.h" |
sewardj | cbe3850 | 2011-07-12 11:59:11 +0000 | [diff] [blame] | 35 | |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 36 | #include <limits.h> |
sewardj | 56501b6 | 2011-07-12 17:28:39 +0000 | [diff] [blame] | 37 | #include <unistd.h> |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 38 | #include <string.h> |
| 39 | #include <poll.h> |
| 40 | #include <pthread.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <stdio.h> |
| 43 | #include <fcntl.h> |
| 44 | #include <dirent.h> |
| 45 | #include <sys/stat.h> |
| 46 | #include <sys/time.h> |
| 47 | #include <errno.h> |
sewardj | e7f8f3f | 2011-07-12 17:41:50 +0000 | [diff] [blame] | 48 | #include <signal.h> |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 49 | #include <sys/types.h> |
| 50 | #include <sys/socket.h> |
| 51 | #include <netinet/in.h> |
| 52 | #include <arpa/inet.h> |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 53 | #include <sys/mman.h> |
| 54 | #include <sys/ptrace.h> |
| 55 | #include <sys/wait.h> |
sewardj | cbe3850 | 2011-07-12 11:59:11 +0000 | [diff] [blame] | 56 | #include <assert.h> |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 57 | /* vgdb has two usages: |
| 58 | 1. relay application between gdb and the gdbserver embedded in valgrind. |
| 59 | 2. standalone to send monitor commands to a running valgrind-ified process |
| 60 | |
| 61 | It is made of a main program which reads arguments. If no |
| 62 | arguments are given or only --pid and --vgdb-prefix, then usage 1 is |
| 63 | assumed. |
| 64 | |
| 65 | As relay application, vgdb reads bytes from gdb on stdin and |
| 66 | writes these bytes to valgrind. Bytes read from valgrind are |
| 67 | written to gdb on stdout. Read/Write from/to valgrind is done |
| 68 | using FIFOs. There is one thread reading from stdin, writing to |
| 69 | valgrind on a FIFO. There is one thread reading from valgrind on a |
| 70 | FIFO, writing to gdb on stdout |
| 71 | |
| 72 | As a standalone utility, vgdb builds command packets to write to valgrind, |
| 73 | sends it and reads the reply. The same two threads are used to write/read. |
| 74 | Once all the commands are sent and their replies received, vgdb will exit. |
| 75 | |
| 76 | */ |
| 77 | |
| 78 | /* define PTRACEINVOKER to compile the ptrace related code |
| 79 | which ensures a valgrind process blocked in a system call |
| 80 | can be "waken up". PTRACEINVOKER implies some architecture |
| 81 | specific code and/or some OS specific code. */ |
| 82 | #if defined(VGA_arm) || defined(VGA_x86) || defined(VGA_amd64) \ |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame] | 83 | || defined(VGA_ppc32) || defined(VGA_ppc64) || defined(VGA_s390x) \ |
| 84 | || defined(VGP_mips32_linux) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 85 | #define PTRACEINVOKER |
| 86 | #else |
| 87 | I_die_here : (PTRACEINVOKER) architecture missing in vgdb.c |
| 88 | #endif |
| 89 | |
| 90 | /* Some darwin specific stuff is needed as ptrace is not |
| 91 | fully supported on MacOS. Till we find someone courageous |
| 92 | having access to Darwin, there is no PTRACEINVOKER. */ |
| 93 | #if defined(VGO_darwin) |
| 94 | #undef PTRACEINVOKER |
| 95 | #endif |
| 96 | |
philippe | 5d5dd8e | 2012-08-05 00:08:25 +0000 | [diff] [blame] | 97 | #if defined(VGPV_arm_linux_android) || defined(VGPV_x86_linux_android) |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 98 | #undef PTRACEINVOKER |
| 99 | #endif |
| 100 | |
| 101 | #if defined(PTRACEINVOKER) |
| 102 | #include <sys/user.h> |
| 103 | #if defined(VGO_linux) |
| 104 | # include <sys/prctl.h> |
| 105 | # include <linux/ptrace.h> |
| 106 | #endif |
| 107 | #endif |
| 108 | |
| 109 | |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 110 | // Outputs information for the user about ptrace_scope protection |
| 111 | // or ptrace not working. |
| 112 | static void ptrace_restrictions_msg(void); |
| 113 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 114 | static int debuglevel; |
| 115 | static struct timeval dbgtv; |
| 116 | /* if level <= debuglevel, print timestamp, then print provided by debug info */ |
| 117 | #define DEBUG(level, ...) (level <= debuglevel ? \ |
| 118 | gettimeofday(&dbgtv, NULL), \ |
| 119 | fprintf(stderr, "%ld.%6.6ld ", \ |
| 120 | (long int)dbgtv.tv_sec, \ |
| 121 | (long int)dbgtv.tv_usec), \ |
| 122 | fprintf(stderr, __VA_ARGS__),fflush(stderr) \ |
| 123 | : 0) |
| 124 | |
| 125 | /* same as DEBUG but does not print time stamp info */ |
| 126 | #define PDEBUG(level, ...) (level <= debuglevel ? \ |
| 127 | fprintf(stderr, __VA_ARGS__),fflush(stderr) \ |
| 128 | : 0) |
| 129 | |
| 130 | /* if errno != 0, |
| 131 | report the errno and fprintf the ... varargs on stderr. */ |
| 132 | #define ERROR(errno, ...) ((errno == 0 ? 0 : perror("syscall failed")), \ |
| 133 | fprintf(stderr, __VA_ARGS__), \ |
| 134 | fflush(stderr)) |
| 135 | /* same as ERROR, but also exits with status 1 */ |
| 136 | #define XERROR(errno, ...) ((errno == 0 ? 0 : perror("syscall failed")), \ |
| 137 | fprintf(stderr, __VA_ARGS__), \ |
| 138 | fflush(stderr), \ |
| 139 | exit(1)) |
| 140 | |
florian | b985e2d | 2011-09-29 03:03:45 +0000 | [diff] [blame] | 141 | static char *vgdb_prefix = NULL; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 142 | |
| 143 | /* Will be set to True when any condition indicating we have to shutdown |
| 144 | is encountered. */ |
| 145 | static Bool shutting_down = False; |
| 146 | |
| 147 | static VgdbShared32 *shared32; |
| 148 | static VgdbShared64 *shared64; |
| 149 | #define VS_written_by_vgdb (shared32 != NULL ? \ |
| 150 | shared32->written_by_vgdb \ |
| 151 | : shared64->written_by_vgdb) |
| 152 | #define VS_seen_by_valgrind (shared32 != NULL ? \ |
| 153 | shared32->seen_by_valgrind \ |
| 154 | : shared64->seen_by_valgrind) |
| 155 | |
| 156 | #define VS_vgdb_pid (shared32 != NULL ? shared32->vgdb_pid : shared64->vgdb_pid) |
| 157 | |
| 158 | /* Calls malloc (size). Exits if memory can't be allocated. */ |
| 159 | static |
| 160 | void *vmalloc(size_t size) |
| 161 | { |
| 162 | void * mem = malloc(size); |
| 163 | if (mem == NULL) |
| 164 | XERROR (errno, "can't allocate memory\n"); |
| 165 | return mem; |
| 166 | } |
| 167 | |
| 168 | /* Calls realloc (size). Exits if memory can't be allocated. */ |
| 169 | static |
| 170 | void *vrealloc(void *ptr,size_t size) |
| 171 | { |
| 172 | void * mem = realloc(ptr, size); |
| 173 | if (mem == NULL) |
| 174 | XERROR (errno, "can't reallocate memory\n"); |
| 175 | return mem; |
| 176 | } |
| 177 | |
florian | b985e2d | 2011-09-29 03:03:45 +0000 | [diff] [blame] | 178 | /* Return the name of a directory for temporary files. */ |
| 179 | static |
| 180 | const char *vgdb_tmpdir(void) |
| 181 | { |
| 182 | const char *tmpdir; |
| 183 | |
| 184 | tmpdir = getenv("TMPDIR"); |
philippe | ae52a93 | 2012-01-13 21:36:46 +0000 | [diff] [blame] | 185 | if (tmpdir == NULL || *tmpdir == '\0') |
| 186 | tmpdir = VG_TMPDIR; |
| 187 | if (tmpdir == NULL || *tmpdir == '\0') |
| 188 | tmpdir = "/tmp"; /* fallback */ |
florian | b985e2d | 2011-09-29 03:03:45 +0000 | [diff] [blame] | 189 | |
| 190 | return tmpdir; |
| 191 | } |
| 192 | |
philippe | ae52a93 | 2012-01-13 21:36:46 +0000 | [diff] [blame] | 193 | /* Return the default path prefix for the named pipes (FIFOs) used by vgdb/gdb |
florian | b985e2d | 2011-09-29 03:03:45 +0000 | [diff] [blame] | 194 | to communicate with valgrind */ |
| 195 | static |
| 196 | char *vgdb_prefix_default(void) |
| 197 | { |
philippe | ae52a93 | 2012-01-13 21:36:46 +0000 | [diff] [blame] | 198 | static HChar *prefix; |
florian | b985e2d | 2011-09-29 03:03:45 +0000 | [diff] [blame] | 199 | |
philippe | ae52a93 | 2012-01-13 21:36:46 +0000 | [diff] [blame] | 200 | if (prefix == NULL) { |
| 201 | const char *tmpdir = vgdb_tmpdir(); |
| 202 | prefix = vmalloc(strlen(tmpdir) + strlen("/vgdb-pipe") + 1); |
| 203 | strcpy(prefix, tmpdir); |
| 204 | strcat(prefix, "/vgdb-pipe"); |
| 205 | } |
florian | b985e2d | 2011-09-29 03:03:45 +0000 | [diff] [blame] | 206 | return prefix; |
| 207 | } |
| 208 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 209 | /* add nrw to the written_by_vgdb field of shared32 or shared64 */ |
| 210 | static |
| 211 | void add_written(int nrw) |
| 212 | { |
| 213 | if (shared32 != NULL) |
| 214 | shared32->written_by_vgdb += nrw; |
| 215 | else if (shared64 != NULL) |
| 216 | shared64->written_by_vgdb += nrw; |
| 217 | else |
| 218 | assert(0); |
| 219 | } |
| 220 | |
| 221 | static int shared_mem_fd = -1; |
| 222 | static |
| 223 | void map_vgdbshared (char* shared_mem) |
| 224 | { |
| 225 | struct stat fdstat; |
| 226 | void **s; |
| 227 | shared_mem_fd = open(shared_mem, O_RDWR); |
| 228 | /* shared_mem_fd will not be closed till vgdb exits. */ |
| 229 | |
| 230 | if (shared_mem_fd == -1) |
| 231 | XERROR (errno, "error opening %s shared memory file\n", shared_mem); |
| 232 | |
| 233 | if (fstat(shared_mem_fd, &fdstat) != 0) |
| 234 | XERROR (errno, "fstat"); |
| 235 | |
| 236 | if (fdstat.st_size == sizeof(VgdbShared64)) |
| 237 | s = (void*) &shared64; |
| 238 | else if (fdstat.st_size == sizeof(VgdbShared32)) |
| 239 | s = (void*) &shared32; |
| 240 | else |
| 241 | #if VEX_HOST_WORDSIZE == 8 |
| 242 | XERROR (0, |
| 243 | "error size shared memory file %s.\n" |
| 244 | "expecting size %d (64bits) or %d (32bits) got %ld.\n", |
| 245 | shared_mem, |
| 246 | (int) sizeof(VgdbShared64), (int) sizeof(VgdbShared32), |
| 247 | (long int)fdstat.st_size); |
| 248 | #elif VEX_HOST_WORDSIZE == 4 |
| 249 | XERROR (0, |
| 250 | "error size shared memory file %s.\n" |
| 251 | "expecting size %d (32bits) got %ld.\n", |
| 252 | shared_mem, |
| 253 | (int) sizeof(VgdbShared32), |
| 254 | fdstat.st_size); |
| 255 | #else |
| 256 | # error "unexpected wordsize" |
| 257 | #endif |
| 258 | |
| 259 | #if VEX_HOST_WORDSIZE == 4 |
| 260 | if (shared64 != NULL) |
| 261 | XERROR (0, "cannot use 32 bits vgdb with a 64bits valgrind process\n"); |
| 262 | /* But we can use a 64 bits vgdb with a 32 bits valgrind */ |
| 263 | #endif |
| 264 | |
| 265 | *s = (void*) mmap (NULL, fdstat.st_size, |
| 266 | PROT_READ|PROT_WRITE, MAP_SHARED, |
| 267 | shared_mem_fd, 0); |
| 268 | |
| 269 | if (*s == (void *) -1) |
| 270 | XERROR (errno, "error mmap shared memory file %s\n", shared_mem); |
| 271 | |
| 272 | } |
| 273 | |
| 274 | #if VEX_HOST_WORDSIZE == 8 |
| 275 | typedef Addr64 CORE_ADDR; |
| 276 | typedef Addr64 PTRACE_XFER_TYPE; |
| 277 | typedef void* PTRACE_ARG3_TYPE; |
| 278 | #elif VEX_HOST_WORDSIZE == 4 |
| 279 | typedef Addr32 CORE_ADDR; |
| 280 | typedef Addr32 PTRACE_XFER_TYPE; |
| 281 | typedef void* PTRACE_ARG3_TYPE; |
| 282 | #else |
| 283 | # error "unexpected wordsize" |
| 284 | #endif |
| 285 | |
| 286 | static Bool pid_of_save_regs_continued = False; |
| 287 | // True if we have continued pid_of_save_regs after PTRACE_ATTACH |
| 288 | |
| 289 | static Bool dying = False; |
| 290 | // Set to True when loss of connection indicating that the Valgrind |
| 291 | // process is dying. |
| 292 | |
| 293 | /* To be called when connection with valgrind is lost. In case we |
| 294 | have lost the connection, it means that Valgrind has closed the |
| 295 | connection and is busy exiting. We can't and don't have to stop it in |
| 296 | this case. */ |
| 297 | static |
| 298 | void valgrind_dying(void) |
| 299 | { |
| 300 | pid_of_save_regs_continued = False; |
| 301 | dying = True; |
| 302 | } |
| 303 | |
| 304 | |
| 305 | #ifdef PTRACEINVOKER |
| 306 | /* ptrace_(read|write)_memory are modified extracts of linux-low.c |
| 307 | from gdb 6.6. Copyrighted FSF */ |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 308 | /* Copy LEN bytes from valgrind memory starting at MEMADDR |
| 309 | to vgdb memory starting at MYADDR. */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 310 | |
| 311 | static |
| 312 | int ptrace_read_memory (pid_t inferior_pid, CORE_ADDR memaddr, |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 313 | void *myaddr, int len) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 314 | { |
| 315 | register int i; |
| 316 | /* Round starting address down to longword boundary. */ |
| 317 | register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE); |
| 318 | /* Round ending address up; get number of longwords that makes. */ |
| 319 | register int count |
| 320 | = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) |
| 321 | / sizeof (PTRACE_XFER_TYPE); |
| 322 | /* Allocate buffer of that many longwords. */ |
| 323 | register PTRACE_XFER_TYPE *buffer |
| 324 | = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE)); |
| 325 | |
| 326 | /* Read all the longwords */ |
| 327 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) { |
| 328 | errno = 0; |
| 329 | buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, |
| 330 | (PTRACE_ARG3_TYPE) addr, 0); |
| 331 | if (errno) |
| 332 | return errno; |
| 333 | } |
| 334 | |
| 335 | /* Copy appropriate bytes out of the buffer. */ |
| 336 | memcpy (myaddr, |
| 337 | (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len); |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 342 | /* Copy LEN bytes of data from vgdb memory at MYADDR |
| 343 | to valgrind memory at MEMADDR. |
| 344 | On failure (cannot write the valgrind memory) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 345 | returns the value of errno. */ |
philippe | 0e3e768 | 2012-06-10 12:26:26 +0000 | [diff] [blame] | 346 | __attribute__((unused)) /* not used on all platforms */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 347 | static |
| 348 | int ptrace_write_memory (pid_t inferior_pid, CORE_ADDR memaddr, |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 349 | const void *myaddr, int len) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 350 | { |
| 351 | register int i; |
| 352 | /* Round starting address down to longword boundary. */ |
| 353 | register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE); |
| 354 | /* Round ending address up; get number of longwords that makes. */ |
| 355 | register int count |
| 356 | = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) |
| 357 | / sizeof (PTRACE_XFER_TYPE); |
| 358 | /* Allocate buffer of that many longwords. */ |
| 359 | register PTRACE_XFER_TYPE *buffer |
| 360 | = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE)); |
| 361 | |
| 362 | if (debuglevel >= 1) { |
| 363 | DEBUG (1, "Writing "); |
| 364 | for (i = 0; i < len; i++) |
florian | 3e79863 | 2012-11-24 19:41:54 +0000 | [diff] [blame] | 365 | PDEBUG (1, "%02x", ((const unsigned char*)myaddr)[i]); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 366 | PDEBUG(1, " to %p\n", (void *) memaddr); |
| 367 | } |
| 368 | |
| 369 | /* Fill start and end extra bytes of buffer with existing memory data. */ |
| 370 | |
| 371 | buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid, |
| 372 | (PTRACE_ARG3_TYPE) addr, 0); |
| 373 | |
| 374 | if (count > 1) { |
| 375 | buffer[count - 1] |
| 376 | = ptrace (PTRACE_PEEKTEXT, inferior_pid, |
| 377 | (PTRACE_ARG3_TYPE) (addr + (count - 1) |
| 378 | * sizeof (PTRACE_XFER_TYPE)), |
| 379 | 0); |
| 380 | } |
| 381 | |
| 382 | /* Copy data to be written over corresponding part of buffer */ |
| 383 | |
| 384 | memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), |
| 385 | myaddr, len); |
| 386 | |
| 387 | /* Write the entire buffer. */ |
| 388 | |
| 389 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) { |
| 390 | errno = 0; |
| 391 | ptrace (PTRACE_POKETEXT, inferior_pid, |
| 392 | (PTRACE_ARG3_TYPE) addr, buffer[i]); |
| 393 | if (errno) |
| 394 | return errno; |
| 395 | } |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | /* subset of VG_(threads) needed for vgdb ptrace. |
| 401 | This is initialized when process is attached. */ |
| 402 | typedef struct { |
| 403 | ThreadStatus status; |
| 404 | Int lwpid; |
| 405 | } |
| 406 | VgdbThreadState; |
| 407 | static VgdbThreadState vgdb_threads[VG_N_THREADS]; |
| 408 | |
| 409 | static const |
| 410 | HChar* name_of_ThreadStatus ( ThreadStatus status ) |
| 411 | { |
| 412 | switch (status) { |
| 413 | case VgTs_Empty: return "VgTs_Empty"; |
| 414 | case VgTs_Init: return "VgTs_Init"; |
| 415 | case VgTs_Runnable: return "VgTs_Runnable"; |
| 416 | case VgTs_WaitSys: return "VgTs_WaitSys"; |
| 417 | case VgTs_Yielding: return "VgTs_Yielding"; |
| 418 | case VgTs_Zombie: return "VgTs_Zombie"; |
| 419 | default: return "VgTs_???"; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | static |
| 424 | char *status_image (int status) |
| 425 | { |
| 426 | static char result[256]; |
| 427 | int sz = 0; |
| 428 | #define APPEND(...) sz += snprintf (result+sz, 256 - sz - 1, __VA_ARGS__) |
| 429 | |
| 430 | result[0] = 0; |
| 431 | |
| 432 | if (WIFEXITED(status)) |
| 433 | APPEND ("WIFEXITED %d ", WEXITSTATUS(status)); |
| 434 | |
| 435 | if (WIFSIGNALED(status)) { |
| 436 | APPEND ("WIFSIGNALED %d ", WTERMSIG(status)); |
| 437 | if (WCOREDUMP(status)) APPEND ("WCOREDUMP "); |
| 438 | } |
| 439 | |
| 440 | if (WIFSTOPPED(status)) |
| 441 | APPEND ("WIFSTOPPED %d ", WSTOPSIG(status)); |
| 442 | |
philippe | 8fb1b13 | 2012-04-13 17:27:40 +0000 | [diff] [blame] | 443 | #ifdef WIFCONTINUED |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 444 | if (WIFCONTINUED(status)) |
| 445 | APPEND ("WIFCONTINUED "); |
philippe | 8fb1b13 | 2012-04-13 17:27:40 +0000 | [diff] [blame] | 446 | #endif |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 447 | |
| 448 | return result; |
| 449 | #undef APPEND |
| 450 | } |
| 451 | |
| 452 | /* Wait till the process pid is reported as stopped with signal_expected. |
| 453 | If other signal(s) than signal_expected are received, waitstopped |
| 454 | will pass them to pid, waiting for signal_expected to stop pid. |
| 455 | Returns True when process is in stopped state with signal_expected. |
| 456 | Returns False if a problem was encountered while waiting for pid |
| 457 | to be stopped. |
| 458 | |
| 459 | If pid is reported as being dead/exited, waitstopped will return False. |
| 460 | */ |
| 461 | static |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 462 | Bool waitstopped (int pid, int signal_expected, const char *msg) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 463 | { |
| 464 | pid_t p; |
| 465 | int status = 0; |
| 466 | int signal_received; |
| 467 | int res; |
| 468 | |
| 469 | while (1) { |
| 470 | DEBUG(1, "waitstopped %s before waitpid signal_expected %d\n", |
| 471 | msg, signal_expected); |
| 472 | p = waitpid(pid, &status, __WALL); |
| 473 | DEBUG(1, "after waitpid pid %d p %d status 0x%x %s\n", pid, p, |
| 474 | status, status_image (status)); |
| 475 | if (p != pid) { |
| 476 | ERROR(errno, "%s waitpid pid %d in waitstopped %d status 0x%x %s\n", |
| 477 | msg, pid, p, status, status_image (status)); |
| 478 | return False; |
| 479 | } |
| 480 | |
| 481 | if (WIFEXITED(status)) { |
| 482 | shutting_down = True; |
| 483 | return False; |
| 484 | } |
| 485 | |
| 486 | assert (WIFSTOPPED(status)); |
| 487 | signal_received = WSTOPSIG(status); |
| 488 | if (signal_received == signal_expected) |
| 489 | break; |
| 490 | |
| 491 | /* pid received a signal which is not the signal we are waiting for. |
| 492 | We continue pid, transmitting this signal. */ |
| 493 | DEBUG(1, "waitstopped PTRACE_CONT with signal %d\n", signal_received); |
| 494 | res = ptrace (PTRACE_CONT, pid, NULL, signal_received); |
| 495 | if (res != 0) { |
| 496 | ERROR(errno, "waitstopped PTRACE_CONT\n"); |
| 497 | return False; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | return True; |
| 502 | } |
| 503 | |
| 504 | /* Stops the given pid, wait for the process to be stopped. |
| 505 | Returns True if succesful, False otherwise. |
| 506 | msg is used in tracing and error reporting. */ |
| 507 | static |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 508 | Bool stop (int pid, const char *msg) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 509 | { |
| 510 | long res; |
| 511 | |
| 512 | DEBUG(1, "%s SIGSTOP pid %d\n", msg, pid); |
| 513 | res = kill (pid, SIGSTOP); |
| 514 | if (res != 0) { |
| 515 | ERROR(errno, "%s SIGSTOP pid %d %ld\n", msg, pid, res); |
| 516 | return False; |
| 517 | } |
| 518 | |
| 519 | return waitstopped (pid, SIGSTOP, msg); |
| 520 | |
| 521 | } |
| 522 | |
| 523 | /* Attaches to given pid, wait for the process to be stopped. |
| 524 | Returns True if succesful, False otherwise. |
| 525 | msg is used in tracing and error reporting. */ |
| 526 | static |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 527 | Bool attach (int pid, const char *msg) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 528 | { |
| 529 | long res; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 530 | static Bool output_error = True; |
| 531 | static Bool initial_attach = True; |
| 532 | // For a ptrace_scope protected system, we do not want to output |
| 533 | // repetitively attach error. We will output once an error |
| 534 | // for the initial_attach. Once the 1st attach has succeeded, we |
| 535 | // again show all errors. |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 536 | |
| 537 | DEBUG(1, "%s PTRACE_ATTACH pid %d\n", msg, pid); |
| 538 | res = ptrace (PTRACE_ATTACH, pid, NULL, NULL); |
| 539 | if (res != 0) { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 540 | if (output_error || debuglevel > 0) { |
| 541 | ERROR(errno, "%s PTRACE_ATTACH pid %d %ld\n", msg, pid, res); |
| 542 | if (initial_attach) |
| 543 | output_error = False; |
| 544 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 545 | return False; |
| 546 | } |
| 547 | |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 548 | initial_attach = False; |
| 549 | output_error = True; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 550 | return waitstopped(pid, SIGSTOP, msg); |
| 551 | } |
| 552 | |
| 553 | /* once we are attached to the pid, get the list of threads and stop |
| 554 | them all. |
| 555 | Returns True if all threads properly suspended, False otherwise. */ |
| 556 | static |
| 557 | Bool acquire_and_suspend_threads(int pid) |
| 558 | { |
| 559 | int i; |
| 560 | int rw; |
| 561 | Bool pid_found = False; |
| 562 | Addr vgt; |
| 563 | int sz_tst; |
| 564 | int off_status; |
| 565 | int off_lwpid; |
| 566 | int nr_live_threads = 0; |
| 567 | |
| 568 | if (shared32 != NULL) { |
| 569 | vgt = shared32->threads; |
| 570 | sz_tst = shared32->sizeof_ThreadState; |
| 571 | off_status = shared32->offset_status; |
| 572 | off_lwpid = shared32->offset_lwpid; |
| 573 | } |
| 574 | else if (shared64 != NULL) { |
| 575 | vgt = shared64->threads; |
| 576 | sz_tst = shared64->sizeof_ThreadState; |
| 577 | off_status = shared64->offset_status; |
| 578 | off_lwpid = shared64->offset_lwpid; |
| 579 | } else { |
| 580 | assert (0); |
| 581 | } |
| 582 | |
| 583 | /* note: the entry 0 is unused */ |
| 584 | for (i = 1; i < VG_N_THREADS; i++) { |
| 585 | vgt += sz_tst; |
| 586 | rw = ptrace_read_memory(pid, vgt+off_status, |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 587 | &(vgdb_threads[i].status), |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 588 | sizeof(ThreadStatus)); |
| 589 | if (rw != 0) { |
| 590 | ERROR(rw, "status ptrace_read_memory\n"); |
| 591 | return False; |
| 592 | } |
| 593 | |
| 594 | rw = ptrace_read_memory(pid, vgt+off_lwpid, |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 595 | &(vgdb_threads[i].lwpid), |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 596 | sizeof(Int)); |
| 597 | if (rw != 0) { |
| 598 | ERROR(rw, "lwpid ptrace_read_memory\n"); |
| 599 | return False; |
| 600 | } |
| 601 | |
| 602 | if (vgdb_threads[i].status != VgTs_Empty) { |
| 603 | DEBUG(1, "found tid %d status %s lwpid %d\n", |
| 604 | i, name_of_ThreadStatus(vgdb_threads[i].status), |
| 605 | vgdb_threads[i].lwpid); |
| 606 | nr_live_threads++; |
| 607 | if (vgdb_threads[i].lwpid <= 1) { |
| 608 | if (vgdb_threads[i].lwpid == 0 |
| 609 | && vgdb_threads[i].status == VgTs_Init) { |
| 610 | DEBUG(1, "not set lwpid tid %d status %s lwpid %d\n", |
| 611 | i, name_of_ThreadStatus(vgdb_threads[i].status), |
| 612 | vgdb_threads[i].lwpid); |
| 613 | } else { |
| 614 | ERROR(1, "unexpected lwpid tid %d status %s lwpid %d\n", |
| 615 | i, name_of_ThreadStatus(vgdb_threads[i].status), |
| 616 | vgdb_threads[i].lwpid); |
| 617 | } |
| 618 | /* in case we have a VtTs_Init thread with lwpid not yet set, |
| 619 | we try again later. */ |
| 620 | return False; |
| 621 | } |
| 622 | if (vgdb_threads[i].lwpid == pid) { |
| 623 | assert (!pid_found); |
| 624 | assert (i == 1); |
| 625 | pid_found = True; |
| 626 | } else { |
| 627 | if (!attach(vgdb_threads[i].lwpid, "attach_thread")) { |
| 628 | ERROR(0, "ERROR attach pid %d tid %d\n", |
| 629 | vgdb_threads[i].lwpid, i); |
| 630 | return False; |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | /* If we found no thread, it means the process is stopping, and |
| 636 | we better do not force anything to happen during that. */ |
| 637 | if (nr_live_threads > 0) |
| 638 | return True; |
| 639 | else |
| 640 | return False; |
| 641 | } |
| 642 | |
| 643 | static |
| 644 | void detach_from_all_threads(int pid) |
| 645 | { |
| 646 | int i; |
| 647 | long res; |
| 648 | Bool pid_found = False; |
| 649 | |
| 650 | /* detach from all the threads */ |
| 651 | for (i = 1; i < VG_N_THREADS; i++) { |
| 652 | if (vgdb_threads[i].status != VgTs_Empty) { |
| 653 | if (vgdb_threads[i].status == VgTs_Init |
| 654 | && vgdb_threads[i].lwpid == 0) { |
| 655 | DEBUG(1, "skipping PTRACE_DETACH pid %d tid %d status %s\n", |
| 656 | vgdb_threads[i].lwpid, i, |
| 657 | name_of_ThreadStatus (vgdb_threads[i].status)); |
| 658 | } else { |
| 659 | if (vgdb_threads[i].lwpid == pid) { |
| 660 | assert (!pid_found); |
| 661 | pid_found = True; |
| 662 | } |
| 663 | DEBUG(1, "PTRACE_DETACH pid %d tid %d status %s\n", |
| 664 | vgdb_threads[i].lwpid, i, |
| 665 | name_of_ThreadStatus (vgdb_threads[i].status)); |
| 666 | res = ptrace (PTRACE_DETACH, vgdb_threads[i].lwpid, NULL, NULL); |
| 667 | if (res != 0) { |
| 668 | ERROR(errno, "PTRACE_DETACH pid %d tid %d status %s res %ld\n", |
| 669 | vgdb_threads[i].lwpid, i, |
| 670 | name_of_ThreadStatus (vgdb_threads[i].status), |
| 671 | res); |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | if (!pid_found && pid) { |
| 678 | /* No threads are live. Process is busy stopping. |
| 679 | We need to detach from pid explicitely. */ |
| 680 | DEBUG(1, "no thread live => PTRACE_DETACH pid %d\n", pid); |
| 681 | res = ptrace (PTRACE_DETACH, pid, NULL, NULL); |
| 682 | if (res != 0) |
| 683 | ERROR(errno, "PTRACE_DETACH pid %d res %ld\n", pid, res); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | // if > 0, pid for which registers have to be restored. |
| 688 | static int pid_of_save_regs = 0; |
| 689 | static struct user user_save; |
| 690 | |
sewardj | eefeeb7 | 2011-05-10 11:01:07 +0000 | [diff] [blame] | 691 | // The below indicates if ptrace_getregs (and ptrace_setregs) can be used. |
| 692 | // Note that some linux versions are defining PTRACE_GETREGS but using |
| 693 | // it gives back EIO. |
| 694 | // has_working_ptrace_getregs can take the following values: |
| 695 | // -1 : PTRACE_GETREGS is defined |
| 696 | // runtime check not yet done. |
| 697 | // 0 : PTRACE_GETREGS runtime check has failed. |
| 698 | // 1 : PTRACE_GETREGS defined and runtime check ok. |
| 699 | #ifdef PTRACE_GETREGS |
| 700 | static int has_working_ptrace_getregs = -1; |
| 701 | #endif |
| 702 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 703 | /* Get the registers from pid into regs. |
sewardj | eefeeb7 | 2011-05-10 11:01:07 +0000 | [diff] [blame] | 704 | regs_bsz value gives the length of *regs. |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 705 | Returns True if all ok, otherwise False. */ |
| 706 | static |
sewardj | eefeeb7 | 2011-05-10 11:01:07 +0000 | [diff] [blame] | 707 | Bool getregs (int pid, void *regs, long regs_bsz) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 708 | { |
sewardj | eefeeb7 | 2011-05-10 11:01:07 +0000 | [diff] [blame] | 709 | DEBUG(1, "getregs regs_bsz %ld\n", regs_bsz); |
| 710 | # ifdef PTRACE_GETREGS |
| 711 | if (has_working_ptrace_getregs) { |
| 712 | // Platforms having GETREGS |
| 713 | long res; |
| 714 | DEBUG(1, "getregs PTRACE_GETREGS\n"); |
| 715 | res = ptrace (PTRACE_GETREGS, pid, NULL, regs); |
| 716 | if (res == 0) { |
| 717 | if (has_working_ptrace_getregs == -1) { |
| 718 | // First call to PTRACE_GETREGS succesful => |
| 719 | has_working_ptrace_getregs = 1; |
| 720 | DEBUG(1, "detected a working PTRACE_GETREGS\n"); |
| 721 | } |
| 722 | assert (has_working_ptrace_getregs == 1); |
| 723 | return True; |
| 724 | } |
| 725 | else if (has_working_ptrace_getregs == 1) { |
| 726 | // We had a working call, but now it fails. |
| 727 | // This is unexpected. |
| 728 | ERROR(errno, "PTRACE_GETREGS %ld\n", res); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 729 | return False; |
sewardj | eefeeb7 | 2011-05-10 11:01:07 +0000 | [diff] [blame] | 730 | } else { |
| 731 | // Check this is the first call: |
| 732 | assert (has_working_ptrace_getregs == -1); |
| 733 | if (errno == EIO) { |
| 734 | DEBUG(1, "detected a broken PTRACE_GETREGS with EIO\n"); |
| 735 | has_working_ptrace_getregs = 0; |
| 736 | // Fall over to the PTRACE_PEEKUSER case. |
| 737 | } else { |
| 738 | ERROR(errno, "broken PTRACE_GETREGS unexpected errno %ld\n", res); |
| 739 | return False; |
| 740 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 741 | } |
| 742 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 743 | # endif |
sewardj | eefeeb7 | 2011-05-10 11:01:07 +0000 | [diff] [blame] | 744 | |
| 745 | // We assume PTRACE_PEEKUSER is defined everywhere. |
| 746 | { |
| 747 | # ifdef PT_ENDREGS |
| 748 | long peek_bsz = PT_ENDREGS; |
| 749 | assert (peek_bsz <= regs_bsz); |
| 750 | # else |
| 751 | long peek_bsz = regs_bsz-1; |
| 752 | # endif |
| 753 | char *pregs = (char *) regs; |
| 754 | long offset; |
| 755 | errno = 0; |
| 756 | DEBUG(1, "getregs PTRACE_PEEKUSER(s) peek_bsz %ld\n", peek_bsz); |
| 757 | for (offset = 0; offset < peek_bsz; offset = offset + sizeof(long)) { |
| 758 | *(long *)(pregs+offset) = ptrace(PTRACE_PEEKUSER, pid, offset, NULL); |
| 759 | if (errno != 0) { |
| 760 | ERROR(errno, "PTRACE_PEEKUSER offset %ld\n", offset); |
| 761 | return False; |
| 762 | } |
| 763 | } |
| 764 | return True; |
| 765 | } |
| 766 | |
| 767 | // If neither PTRACE_GETREGS not PTRACE_PEEKUSER have returned, |
| 768 | // then we are in serious trouble. |
| 769 | assert (0); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | /* Set the registers of pid to regs. |
sewardj | eefeeb7 | 2011-05-10 11:01:07 +0000 | [diff] [blame] | 773 | regs_bsz value gives the length of *regs. |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 774 | Returns True if all ok, otherwise False. */ |
| 775 | static |
sewardj | eefeeb7 | 2011-05-10 11:01:07 +0000 | [diff] [blame] | 776 | Bool setregs (int pid, void *regs, long regs_bsz) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 777 | { |
sewardj | eefeeb7 | 2011-05-10 11:01:07 +0000 | [diff] [blame] | 778 | DEBUG(1, "setregs regs_bsz %ld\n", regs_bsz); |
| 779 | // Note : the below is checking for GETREGS, not SETREGS |
| 780 | // as if one is defined and working, the other one should also work. |
| 781 | # ifdef PTRACE_GETREGS |
| 782 | if (has_working_ptrace_getregs) { |
| 783 | // Platforms having SETREGS |
| 784 | long res; |
| 785 | // setregs can never be called before getregs has done a runtime check. |
| 786 | assert (has_working_ptrace_getregs == 1); |
| 787 | DEBUG(1, "setregs PTRACE_SETREGS\n"); |
| 788 | res = ptrace (PTRACE_SETREGS, pid, NULL, regs); |
| 789 | if (res != 0) { |
| 790 | ERROR(errno, "PTRACE_SETREGS %ld\n", res); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 791 | return False; |
| 792 | } |
sewardj | eefeeb7 | 2011-05-10 11:01:07 +0000 | [diff] [blame] | 793 | return True; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 794 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 795 | # endif |
sewardj | eefeeb7 | 2011-05-10 11:01:07 +0000 | [diff] [blame] | 796 | |
| 797 | { |
| 798 | char *pregs = (char *) regs; |
| 799 | long offset; |
| 800 | long res; |
| 801 | # ifdef PT_ENDREGS |
| 802 | long peek_bsz = PT_ENDREGS; |
| 803 | assert (peek_bsz <= regs_bsz); |
| 804 | # else |
| 805 | long peek_bsz = regs_bsz-1; |
| 806 | # endif |
| 807 | errno = 0; |
| 808 | DEBUG(1, "setregs PTRACE_POKEUSER(s) %ld\n", peek_bsz); |
| 809 | for (offset = 0; offset < peek_bsz; offset = offset + sizeof(long)) { |
| 810 | res = ptrace(PTRACE_POKEUSER, pid, offset, *(long*)(pregs+offset)); |
| 811 | if (errno != 0) { |
| 812 | ERROR(errno, "PTRACE_POKEUSER offset %ld res %ld\n", offset, res); |
| 813 | return False; |
| 814 | } |
| 815 | } |
| 816 | return True; |
| 817 | } |
| 818 | |
| 819 | // If neither PTRACE_SETREGS not PTRACE_POKEUSER have returned, |
| 820 | // then we are in serious trouble. |
| 821 | assert (0); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /* Restore the registers to the saved value, then detaches from all threads */ |
| 825 | static |
| 826 | void restore_and_detach(int pid) |
| 827 | { |
| 828 | if (pid_of_save_regs) { |
| 829 | /* In case the 'main pid' has been continued, we need to stop it |
| 830 | before resetting the registers. */ |
| 831 | if (pid_of_save_regs_continued) { |
| 832 | pid_of_save_regs_continued = False; |
| 833 | if (!stop(pid_of_save_regs, "sigstop before reset regs")) |
| 834 | DEBUG(0, "Could not sigstop before reset"); |
| 835 | } |
| 836 | |
| 837 | DEBUG(1, "setregs restore registers pid %d\n", pid_of_save_regs); |
sewardj | eefeeb7 | 2011-05-10 11:01:07 +0000 | [diff] [blame] | 838 | if (!setregs(pid_of_save_regs, &user_save.regs, sizeof(user_save.regs))) { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 839 | ERROR(errno, "setregs restore registers pid %d after cont\n", |
| 840 | pid_of_save_regs); |
| 841 | } |
| 842 | pid_of_save_regs = 0; |
| 843 | } else { |
| 844 | DEBUG(1, "PTRACE_SETREGS restore registers: no pid\n"); |
| 845 | } |
| 846 | detach_from_all_threads(pid); |
| 847 | } |
| 848 | |
| 849 | /* Ensures that the gdbserver code is invoked by pid. |
| 850 | If an error occurs, resets to the valgrind process |
| 851 | to the state it has before being ptrace-d. |
| 852 | Returns True if invoke successful, False otherwise. |
| 853 | */ |
| 854 | static |
| 855 | Bool invoke_gdbserver (int pid) |
| 856 | { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 857 | static Bool ptrace_restrictions_msg_given = False; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 858 | long res; |
| 859 | Bool stopped; |
| 860 | struct user user_mod; |
| 861 | Addr sp; |
| 862 | /* A specific int value is passed to invoke_gdbserver, to check |
| 863 | everything goes according to the plan. */ |
| 864 | const int check = 0x8BADF00D; // ate bad food. |
| 865 | |
| 866 | const Addr bad_return = 0; |
| 867 | // A bad return address will be pushed on the stack. |
| 868 | // The function invoke_gdbserver cannot return. If ever it returns, a NULL |
| 869 | // address pushed on the stack should ensure this is detected. |
| 870 | |
| 871 | /* Not yet attached. If problem, vgdb can abort, |
| 872 | no cleanup needed. |
| 873 | |
| 874 | On Ubuntu>= 10.10, a /proc setting can disable ptrace. |
| 875 | So, Valgrind has to SET_PTRACER this vgdb. Once this |
| 876 | is done, this vgdb can ptrace the valgrind process. */ |
| 877 | |
| 878 | DEBUG(1, "attach to 'main' pid %d\n", pid); |
| 879 | if (!attach(pid, "attach main pid")) { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 880 | if (!ptrace_restrictions_msg_given) { |
| 881 | ptrace_restrictions_msg_given = True; |
| 882 | ERROR(0, "error attach main pid %d\n", pid); |
| 883 | ptrace_restrictions_msg(); |
| 884 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 885 | return False; |
| 886 | } |
| 887 | |
| 888 | /* Now, we are attached. If problem, detach and return. */ |
| 889 | |
| 890 | if (!acquire_and_suspend_threads(pid)) { |
| 891 | detach_from_all_threads(pid); |
| 892 | /* if the pid does not exist anymore, we better stop */ |
| 893 | if (kill(pid, 0) != 0) |
| 894 | XERROR (errno, "invoke_gdbserver: check for pid %d existence failed\n", |
| 895 | pid); |
| 896 | return False; |
| 897 | } |
| 898 | |
sewardj | eefeeb7 | 2011-05-10 11:01:07 +0000 | [diff] [blame] | 899 | if (!getregs(pid, &user_mod.regs, sizeof(user_mod.regs))) { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 900 | detach_from_all_threads(pid); |
| 901 | return False; |
| 902 | } |
| 903 | user_save = user_mod; |
| 904 | |
| 905 | #if defined(VGA_x86) |
| 906 | sp = user_mod.regs.esp; |
| 907 | #elif defined(VGA_amd64) |
| 908 | sp = user_mod.regs.rsp; |
| 909 | if (shared32 != NULL) { |
| 910 | /* 64bit vgdb speaking with a 32bit executable. |
| 911 | To have system call restart properly, we need to sign extend rax. |
| 912 | For more info: |
| 913 | web search '[patch] Fix syscall restarts for amd64->i386 biarch' |
| 914 | e.g. http://sourceware.org/ml/gdb-patches/2009-11/msg00592.html */ |
| 915 | *(long *)&user_save.regs.rax = *(int*)&user_save.regs.rax; |
| 916 | DEBUG(1, "Sign extending %8.8lx to %8.8lx\n", |
| 917 | user_mod.regs.rax, user_save.regs.rax); |
| 918 | } |
| 919 | #elif defined(VGA_arm) |
| 920 | sp = user_mod.regs.uregs[13]; |
| 921 | #elif defined(VGA_ppc32) |
| 922 | sp = user_mod.regs.gpr[1]; |
| 923 | #elif defined(VGA_ppc64) |
| 924 | sp = user_mod.regs.gpr[1]; |
| 925 | #elif defined(VGA_s390x) |
| 926 | sp = user_mod.regs.gprs[15]; |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame] | 927 | #elif defined(VGA_mips32) |
philippe | 6dda089 | 2012-08-18 11:06:04 +0000 | [diff] [blame] | 928 | sp = user_mod.regs[29*2]; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 929 | #else |
| 930 | I_die_here : (sp) architecture missing in vgdb.c |
| 931 | #endif |
| 932 | |
| 933 | |
| 934 | // the magic below is derived from spying what gdb sends to |
| 935 | // the (classical) gdbserver when invoking a C function. |
| 936 | if (shared32 != NULL) { |
| 937 | // vgdb speaking with a 32bit executable. |
| 938 | #if defined(VGA_x86) || defined(VGA_amd64) |
| 939 | const int regsize = 4; |
| 940 | int rw; |
| 941 | /* push check arg on the stack */ |
| 942 | sp = sp - regsize; |
| 943 | DEBUG(1, "push check arg ptrace_write_memory\n"); |
| 944 | assert(regsize == sizeof(check)); |
| 945 | rw = ptrace_write_memory(pid, sp, |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 946 | &check, |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 947 | regsize); |
| 948 | if (rw != 0) { |
| 949 | ERROR(rw, "push check arg ptrace_write_memory"); |
| 950 | detach_from_all_threads(pid); |
| 951 | return False; |
| 952 | } |
| 953 | |
| 954 | sp = sp - regsize; |
| 955 | DEBUG(1, "push bad_return return address ptrace_write_memory\n"); |
| 956 | // Note that for a 64 bits vgdb, only 4 bytes of NULL bad_return |
| 957 | // are written. |
| 958 | rw = ptrace_write_memory(pid, sp, |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 959 | &bad_return, |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 960 | regsize); |
| 961 | if (rw != 0) { |
| 962 | ERROR(rw, "push bad_return return address ptrace_write_memory"); |
| 963 | detach_from_all_threads(pid); |
| 964 | return False; |
| 965 | } |
| 966 | #if defined(VGA_x86) |
| 967 | /* set ebp, esp, eip and orig_eax to invoke gdbserver */ |
| 968 | // compiled in 32bits, speaking with a 32bits exe |
| 969 | user_mod.regs.ebp = sp; // bp set to sp |
| 970 | user_mod.regs.esp = sp; |
| 971 | user_mod.regs.eip = shared32->invoke_gdbserver; |
| 972 | user_mod.regs.orig_eax = -1L; |
| 973 | #elif defined(VGA_amd64) |
| 974 | /* set ebp, esp, eip and orig_eax to invoke gdbserver */ |
| 975 | // compiled in 64bits, speaking with a 32bits exe |
| 976 | user_mod.regs.rbp = sp; // bp set to sp |
| 977 | user_mod.regs.rsp = sp; |
| 978 | user_mod.regs.rip = shared32->invoke_gdbserver; |
| 979 | user_mod.regs.orig_rax = -1L; |
| 980 | #else |
| 981 | I_die_here : not x86 or amd64 in x86/amd64 section/ |
| 982 | #endif |
| 983 | |
| 984 | #elif defined(VGA_ppc32) || defined(VGA_ppc64) |
| 985 | user_mod.regs.nip = shared32->invoke_gdbserver; |
| 986 | user_mod.regs.trap = -1L; |
| 987 | /* put check arg in register 3 */ |
| 988 | user_mod.regs.gpr[3] = check; |
| 989 | /* put NULL return address in Link Register */ |
| 990 | user_mod.regs.link = bad_return; |
| 991 | |
| 992 | #elif defined(VGA_arm) |
| 993 | /* put check arg in register 0 */ |
| 994 | user_mod.regs.uregs[0] = check; |
| 995 | /* put NULL return address in Link Register */ |
| 996 | user_mod.regs.uregs[14] = bad_return; |
| 997 | user_mod.regs.uregs[15] = shared32->invoke_gdbserver; |
| 998 | |
| 999 | #elif defined(VGA_s390x) |
| 1000 | XERROR(0, "(fn32) s390x has no 32bits implementation"); |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame] | 1001 | #elif defined(VGA_mips32) |
philippe | 6dda089 | 2012-08-18 11:06:04 +0000 | [diff] [blame] | 1002 | /* put check arg in register 4 */ |
| 1003 | user_mod.regs[4*2] = check; |
| 1004 | user_mod.regs[4*2+1] = 0xffffffff; // sign extend $a0 |
| 1005 | /* This sign extension is needed when vgdb 32 bits runs |
| 1006 | on a 64 bits OS. */ |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame] | 1007 | /* put NULL return address in ra */ |
philippe | 6dda089 | 2012-08-18 11:06:04 +0000 | [diff] [blame] | 1008 | user_mod.regs[31*2] = bad_return; |
| 1009 | user_mod.regs[31*2+1] = 0; |
| 1010 | user_mod.regs[34*2] = shared32->invoke_gdbserver; |
| 1011 | user_mod.regs[34*2+1] = 0; |
| 1012 | user_mod.regs[25*2] = shared32->invoke_gdbserver; |
| 1013 | user_mod.regs[25*2+1] = 0; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1014 | #else |
| 1015 | I_die_here : architecture missing in vgdb.c |
| 1016 | #endif |
| 1017 | } |
| 1018 | |
| 1019 | else if (shared64 != NULL) { |
| 1020 | #if defined(VGA_x86) |
| 1021 | assert(0); // cannot vgdb a 64 bits executable with a 32 bits exe |
| 1022 | #elif defined(VGA_amd64) |
| 1023 | // vgdb speaking with a 64 bit executable. |
| 1024 | const int regsize = 8; |
| 1025 | int rw; |
| 1026 | |
| 1027 | /* give check arg in rdi */ |
| 1028 | user_mod.regs.rdi = check; |
| 1029 | |
| 1030 | /* push return address on stack : return to breakaddr */ |
| 1031 | sp = sp - regsize; |
| 1032 | DEBUG(1, "push bad_return return address ptrace_write_memory\n"); |
| 1033 | rw = ptrace_write_memory(pid, sp, |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 1034 | &bad_return, |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1035 | sizeof(bad_return)); |
| 1036 | if (rw != 0) { |
| 1037 | ERROR(rw, "push bad_return return address ptrace_write_memory"); |
| 1038 | detach_from_all_threads(pid); |
| 1039 | return False; |
| 1040 | } |
| 1041 | |
| 1042 | /* set rbp, rsp, rip and orig_rax to invoke gdbserver */ |
| 1043 | user_mod.regs.rbp = sp; // bp set to sp |
| 1044 | user_mod.regs.rsp = sp; |
| 1045 | user_mod.regs.rip = shared64->invoke_gdbserver; |
| 1046 | user_mod.regs.orig_rax = -1L; |
| 1047 | |
| 1048 | #elif defined(VGA_arm) |
| 1049 | assert(0); // cannot vgdb a 64 bits executable with a 32 bits exe |
| 1050 | #elif defined(VGA_ppc32) |
| 1051 | assert(0); // cannot vgdb a 64 bits executable with a 32 bits exe |
| 1052 | #elif defined(VGA_ppc64) |
| 1053 | Addr64 func_addr; |
| 1054 | Addr64 toc_addr; |
| 1055 | int rw; |
| 1056 | rw = ptrace_read_memory(pid, shared64->invoke_gdbserver, |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 1057 | &func_addr, |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1058 | sizeof(Addr64)); |
| 1059 | if (rw != 0) { |
| 1060 | ERROR(rw, "ppc64 read func_addr\n"); |
| 1061 | detach_from_all_threads(pid); |
| 1062 | return False; |
| 1063 | } |
| 1064 | rw = ptrace_read_memory(pid, shared64->invoke_gdbserver+8, |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 1065 | &toc_addr, |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1066 | sizeof(Addr64)); |
| 1067 | if (rw != 0) { |
| 1068 | ERROR(rw, "ppc64 read toc_addr\n"); |
| 1069 | detach_from_all_threads(pid); |
| 1070 | return False; |
| 1071 | } |
| 1072 | // We are not pushing anything on the stack, so it is not |
| 1073 | // very clear why the sp has to be decreased, but it seems |
| 1074 | // needed. The ppc64 ABI might give some lights on this ? |
| 1075 | user_mod.regs.gpr[1] = sp - 220; |
| 1076 | user_mod.regs.gpr[2] = toc_addr; |
| 1077 | user_mod.regs.nip = func_addr; |
| 1078 | user_mod.regs.trap = -1L; |
| 1079 | /* put check arg in register 3 */ |
| 1080 | user_mod.regs.gpr[3] = check; |
| 1081 | /* put bad_return return address in Link Register */ |
| 1082 | user_mod.regs.link = bad_return; |
| 1083 | #elif defined(VGA_s390x) |
| 1084 | /* put check arg in register r2 */ |
| 1085 | user_mod.regs.gprs[2] = check; |
| 1086 | /* bad_return Return address is in r14 */ |
| 1087 | user_mod.regs.gprs[14] = bad_return; |
| 1088 | /* minimum stack frame */ |
| 1089 | sp = sp - 160; |
| 1090 | user_mod.regs.gprs[15] = sp; |
| 1091 | /* set program counter */ |
| 1092 | user_mod.regs.psw.addr = shared64->invoke_gdbserver; |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame] | 1093 | #elif defined(VGA_mips32) |
| 1094 | assert(0); // cannot vgdb a 64 bits executable with a 32 bits exe |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1095 | #else |
| 1096 | I_die_here: architecture missing in vgdb.c |
| 1097 | #endif |
| 1098 | } |
| 1099 | else { |
| 1100 | assert(0); |
| 1101 | } |
| 1102 | |
sewardj | eefeeb7 | 2011-05-10 11:01:07 +0000 | [diff] [blame] | 1103 | if (!setregs(pid, &user_mod.regs, sizeof(user_mod.regs))) { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1104 | detach_from_all_threads(pid); |
| 1105 | return False; |
| 1106 | } |
| 1107 | /* Now that we have modified the registers, we set |
| 1108 | pid_of_save_regs to indicate that restore_and_detach |
| 1109 | must restore the registers in case of cleanup. */ |
| 1110 | pid_of_save_regs = pid; |
| 1111 | pid_of_save_regs_continued = False; |
| 1112 | |
| 1113 | |
| 1114 | /* We PTRACE_CONT-inue pid. |
| 1115 | Either gdbserver will be invoked directly (if all |
| 1116 | threads are interruptible) or gdbserver will be |
| 1117 | called soon by the scheduler. In the first case, |
| 1118 | pid will stop on the break inserted above when |
| 1119 | gdbserver returns. In the 2nd case, the break will |
| 1120 | be encountered directly. */ |
| 1121 | DEBUG(1, "PTRACE_CONT to invoke\n"); |
| 1122 | res = ptrace (PTRACE_CONT, pid, NULL, NULL); |
| 1123 | if (res != 0) { |
| 1124 | ERROR(errno, "PTRACE_CONT\n"); |
| 1125 | restore_and_detach(pid); |
| 1126 | return False; |
| 1127 | } |
| 1128 | pid_of_save_regs_continued = True; |
sewardj | b2572b5 | 2011-06-26 09:36:38 +0000 | [diff] [blame] | 1129 | /* Wait for SIGSTOP generated by m_gdbserver.c give_control_back_to_vgdb */ |
| 1130 | stopped = waitstopped (pid, SIGSTOP, |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1131 | "waitpid status after PTRACE_CONT to invoke"); |
| 1132 | if (stopped) { |
| 1133 | /* Here pid has properly stopped on the break. */ |
| 1134 | pid_of_save_regs_continued = False; |
| 1135 | restore_and_detach(pid); |
| 1136 | return True; |
| 1137 | } else { |
| 1138 | /* Whatever kind of problem happened. We shutdown */ |
| 1139 | shutting_down = True; |
| 1140 | return False; |
| 1141 | } |
| 1142 | } |
| 1143 | #endif |
| 1144 | |
| 1145 | static |
| 1146 | void cleanup_restore_and_detach(void *v_pid) |
| 1147 | { |
| 1148 | DEBUG(1, "cleanup_restore_and_detach dying: %d\n", dying); |
| 1149 | #ifdef PTRACEINVOKER |
| 1150 | if (!dying) |
| 1151 | restore_and_detach(*(int*)v_pid); |
| 1152 | #endif |
| 1153 | } |
| 1154 | |
| 1155 | /* This function loops till shutting_down becomes true. In this loop, |
| 1156 | it verifies if valgrind process is reading the characters written |
| 1157 | by vgdb. The verification is done every max_invoke_ms ms. If |
| 1158 | valgrind is not reading characters, it will use invoke_gdbserver |
| 1159 | (if PTRACE_INVOKER is defined) to ensure that the gdbserver code is |
| 1160 | called soon by valgrind. */ |
| 1161 | static int max_invoke_ms = 100; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1162 | #define NEVER 99999999 |
| 1163 | static int cmd_time_out = NEVER; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1164 | static |
| 1165 | void *invoke_gdbserver_in_valgrind(void *v_pid) |
| 1166 | { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1167 | struct timeval cmd_max_end_time; |
| 1168 | Bool cmd_started = False; |
| 1169 | struct timeval invoke_time; |
| 1170 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1171 | int pid = *(int *)v_pid; |
| 1172 | int written_by_vgdb_before_sleep; |
| 1173 | int seen_by_valgrind_before_sleep; |
| 1174 | |
| 1175 | int invoked_written = -1; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1176 | unsigned int usecs; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1177 | |
| 1178 | pthread_cleanup_push(cleanup_restore_and_detach, v_pid); |
| 1179 | |
| 1180 | while (!shutting_down) { |
| 1181 | written_by_vgdb_before_sleep = VS_written_by_vgdb; |
| 1182 | seen_by_valgrind_before_sleep = VS_seen_by_valgrind; |
| 1183 | DEBUG(3, |
| 1184 | "written_by_vgdb_before_sleep %d " |
| 1185 | "seen_by_valgrind_before_sleep %d\n", |
| 1186 | written_by_vgdb_before_sleep, |
| 1187 | seen_by_valgrind_before_sleep); |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1188 | if (cmd_time_out != NEVER |
| 1189 | && !cmd_started |
| 1190 | && written_by_vgdb_before_sleep > seen_by_valgrind_before_sleep) { |
| 1191 | /* A command was started. Record the time at which it was started. */ |
| 1192 | DEBUG(1, "IO for command started\n"); |
| 1193 | gettimeofday(&cmd_max_end_time, NULL); |
| 1194 | cmd_max_end_time.tv_sec += cmd_time_out; |
| 1195 | cmd_started = True; |
| 1196 | } |
| 1197 | if (max_invoke_ms > 0) { |
| 1198 | usecs = 1000 * max_invoke_ms; |
| 1199 | gettimeofday(&invoke_time, NULL); |
| 1200 | invoke_time.tv_sec += max_invoke_ms / 1000; |
| 1201 | invoke_time.tv_usec += 1000 * (max_invoke_ms % 1000); |
| 1202 | invoke_time.tv_sec += invoke_time.tv_usec / (1000 * 1000); |
| 1203 | invoke_time.tv_usec = invoke_time.tv_usec % (1000 * 1000); |
| 1204 | } else { |
| 1205 | usecs = 0; |
| 1206 | } |
| 1207 | if (cmd_started) { |
| 1208 | // 0 usecs here means the thread just has to check gdbserver eats |
| 1209 | // the characters in <= cmd_time_out seconds. |
| 1210 | // We will just wait by 1 second max at a time. |
| 1211 | if (usecs == 0 || usecs > 1000 * 1000) |
| 1212 | usecs = 1000 * 1000; |
| 1213 | } |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1214 | usleep(usecs); |
| 1215 | |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1216 | /* If nothing happened during our sleep, let's try to wake up valgrind |
| 1217 | or check for cmd time out. */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1218 | if (written_by_vgdb_before_sleep == VS_written_by_vgdb |
| 1219 | && seen_by_valgrind_before_sleep == VS_seen_by_valgrind |
| 1220 | && VS_written_by_vgdb > VS_seen_by_valgrind) { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1221 | struct timeval now; |
| 1222 | gettimeofday(&now, NULL); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1223 | DEBUG(2, |
| 1224 | "after sleep " |
| 1225 | "written_by_vgdb %d " |
| 1226 | "seen_by_valgrind %d " |
| 1227 | "invoked_written %d\n", |
| 1228 | VS_written_by_vgdb, |
| 1229 | VS_seen_by_valgrind, |
| 1230 | invoked_written); |
| 1231 | /* if the pid does not exist anymore, we better stop */ |
| 1232 | if (kill(pid, 0) != 0) |
| 1233 | XERROR (errno, |
| 1234 | "invoke_gdbserver_in_valgrind: " |
| 1235 | "check for pid %d existence failed\n", pid); |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1236 | if (cmd_started) { |
| 1237 | if (timercmp (&now, &cmd_max_end_time, >)) |
| 1238 | XERROR (0, |
| 1239 | "pid %d did not handle a command in %d seconds\n", |
| 1240 | pid, cmd_time_out); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1241 | } |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1242 | if (max_invoke_ms > 0 && timercmp (&now, &invoke_time, >=)) { |
| 1243 | #if defined(PTRACEINVOKER) |
| 1244 | /* only need to wake up if the nr written has changed since |
| 1245 | last invoke. */ |
| 1246 | if (invoked_written != written_by_vgdb_before_sleep) { |
| 1247 | if (invoke_gdbserver(pid)) { |
| 1248 | /* If invoke succesful, no need to invoke again |
| 1249 | for the same value of written_by_vgdb_before_sleep. */ |
| 1250 | invoked_written = written_by_vgdb_before_sleep; |
| 1251 | } |
| 1252 | } |
| 1253 | #else |
| 1254 | DEBUG(2, "invoke_gdbserver via ptrace not (yet) implemented\n"); |
| 1255 | #endif |
| 1256 | } |
| 1257 | } else { |
| 1258 | // Something happened => restart timer check. |
| 1259 | if (cmd_time_out != NEVER) { |
| 1260 | DEBUG(2, "some IO was done => restart command\n"); |
| 1261 | cmd_started = False; |
| 1262 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1263 | } |
| 1264 | } |
| 1265 | pthread_cleanup_pop(0); |
| 1266 | return NULL; |
| 1267 | } |
| 1268 | |
| 1269 | static |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 1270 | int open_fifo (const char* name, int flags, const char* desc) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1271 | { |
| 1272 | int fd; |
| 1273 | DEBUG(1, "opening %s %s\n", name, desc); |
| 1274 | fd = open(name, flags); |
| 1275 | if (fd == -1) |
| 1276 | XERROR (errno, "error opening %s %s\n", name, desc); |
| 1277 | |
| 1278 | DEBUG(1, "opened %s %s fd %d\n", name, desc, fd); |
| 1279 | return fd; |
| 1280 | } |
| 1281 | |
| 1282 | /* acquire a lock on the first byte of the given fd. If not successful, |
| 1283 | exits with error. |
| 1284 | This allows to avoid having two vgdb speaking with the same Valgrind |
| 1285 | gdbserver as this causes serious headaches to the protocol. */ |
| 1286 | static |
| 1287 | void acquire_lock (int fd, int valgrind_pid) |
| 1288 | { |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1289 | struct flock fl; |
| 1290 | fl.l_type = F_WRLCK; |
| 1291 | fl.l_whence = SEEK_SET; |
| 1292 | fl.l_start = 0; |
| 1293 | fl.l_len = 1; |
| 1294 | if (fcntl(fd, F_SETLK, &fl) < 0) { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1295 | if (errno == EAGAIN || errno == EACCES) { |
| 1296 | XERROR(errno, |
| 1297 | "Cannot acquire lock.\n" |
| 1298 | "Probably vgdb pid %d already speaks with Valgrind pid %d\n", |
| 1299 | VS_vgdb_pid, |
| 1300 | valgrind_pid); |
| 1301 | } else { |
| 1302 | XERROR(errno, "cannot acquire lock.\n"); |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | /* Here, we have the lock. It will be released when fd will be closed. */ |
| 1307 | /* We indicate our pid to Valgrind gdbserver */ |
| 1308 | if (shared32 != NULL) |
| 1309 | shared32->vgdb_pid = getpid(); |
| 1310 | else if (shared64 != NULL) |
| 1311 | shared64->vgdb_pid = getpid(); |
| 1312 | else |
| 1313 | assert(0); |
| 1314 | } |
| 1315 | |
| 1316 | #define PBUFSIZ 16384 /* keep in sync with server.h */ |
| 1317 | |
| 1318 | /* read some characters from fd. |
| 1319 | Returns the nr of characters read, -1 if error. |
| 1320 | desc is a string used in tracing */ |
| 1321 | static |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 1322 | int read_buf (int fd, char* buf, const char* desc) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1323 | { |
| 1324 | int nrread; |
| 1325 | DEBUG(2, "reading %s\n", desc); |
| 1326 | nrread = read(fd, buf, PBUFSIZ); |
| 1327 | if (nrread == -1) { |
| 1328 | ERROR (errno, "error reading %s\n", desc); |
| 1329 | return -1; |
| 1330 | } |
| 1331 | buf[nrread] = '\0'; |
| 1332 | DEBUG(2, "read %s %s\n", desc, buf); |
| 1333 | return nrread; |
| 1334 | } |
| 1335 | |
| 1336 | /* write size bytes from buf to fd. |
| 1337 | desc is a description of the action for which the write is done. |
| 1338 | If notify, then add size to the shared cntr indicating to the |
| 1339 | valgrind process that there is new data. |
| 1340 | Returns True if write is ok, False if there was a problem. */ |
| 1341 | static |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 1342 | Bool write_buf(int fd, char* buf, int size, const char* desc, Bool notify) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1343 | { |
| 1344 | int nrwritten; |
| 1345 | int nrw; |
philippe | 66bc6e5 | 2012-10-31 15:07:39 +0000 | [diff] [blame] | 1346 | DEBUG(2, "writing %s len %d %.*s notify: %d\n", desc, size, |
| 1347 | size, buf, notify); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1348 | nrwritten = 0; |
| 1349 | while (nrwritten < size) { |
| 1350 | nrw = write (fd, buf+nrwritten, size - nrwritten); |
| 1351 | if (nrw == -1) { |
| 1352 | ERROR(errno, "error write %s\n", desc); |
| 1353 | return False; |
| 1354 | } |
| 1355 | nrwritten = nrwritten + nrw; |
| 1356 | if (notify) |
| 1357 | add_written(nrw); |
| 1358 | } |
| 1359 | return True; |
| 1360 | } |
| 1361 | |
| 1362 | typedef enum { |
| 1363 | FROM_GDB, |
| 1364 | TO_GDB, |
| 1365 | FROM_PID, |
| 1366 | TO_PID } ConnectionKind; |
| 1367 | static const int NumConnectionKind = TO_PID+1; |
| 1368 | static |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 1369 | const char *ppConnectionKind (ConnectionKind con) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1370 | { |
| 1371 | switch (con) { |
| 1372 | case FROM_GDB: return "FROM_GDB"; |
| 1373 | case TO_GDB: return "TO_GDB"; |
| 1374 | case FROM_PID: return "FROM_PID"; |
| 1375 | case TO_PID: return "TO_PID"; |
| 1376 | default: return "invalid connection kind"; |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | static char *shared_mem; |
| 1381 | |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1382 | static int from_gdb = 0; /* stdin by default, changed if --port is given. */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1383 | static char *from_gdb_to_pid; /* fifo name to write gdb command to pid */ |
| 1384 | /* Returns True in case read/write operations were done properly. |
| 1385 | Returns False in case of error. |
| 1386 | to_pid is the file descriptor to write to the process pid. */ |
| 1387 | static |
| 1388 | Bool read_from_gdb_write_to_pid(int to_pid) |
| 1389 | { |
philippe | 1d76a80 | 2011-12-26 21:21:37 +0000 | [diff] [blame] | 1390 | char buf[PBUFSIZ+1]; // +1 for trailing \0 |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1391 | int nrread; |
| 1392 | |
| 1393 | nrread = read_buf(from_gdb, buf, "from gdb on stdin"); |
| 1394 | if (nrread <= 0) { |
| 1395 | if (nrread == 0) |
| 1396 | DEBUG(1, "read 0 bytes from gdb => assume exit\n"); |
| 1397 | else |
| 1398 | DEBUG(1, "error reading bytes from gdb\n"); |
| 1399 | close (from_gdb); |
| 1400 | shutting_down = True; |
| 1401 | return False; |
| 1402 | } |
| 1403 | return write_buf(to_pid, buf, nrread, "to_pid", /* notify */ True); |
| 1404 | } |
| 1405 | |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1406 | static int to_gdb = 1; /* stdout by default, changed if --port is given. */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1407 | static char *to_gdb_from_pid; /* fifo name to read pid replies */ |
| 1408 | /* Returns True in case read/write operations were done properly. |
| 1409 | Returns False in case of error. |
| 1410 | from_pid is the file descriptor to read data from the process pid. */ |
| 1411 | static |
| 1412 | Bool read_from_pid_write_to_gdb(int from_pid) |
| 1413 | { |
philippe | 1d76a80 | 2011-12-26 21:21:37 +0000 | [diff] [blame] | 1414 | char buf[PBUFSIZ+1]; // +1 for trailing \0 |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1415 | int nrread; |
| 1416 | |
| 1417 | nrread = read_buf(from_pid, buf, "from pid"); |
| 1418 | if (nrread <= 0) { |
| 1419 | if (nrread == 0) |
| 1420 | DEBUG(1, "read 0 bytes from pid => assume exit\n"); |
| 1421 | else |
| 1422 | DEBUG(1, "error reading bytes from pid\n"); |
| 1423 | close (from_pid); |
| 1424 | shutting_down = True; |
| 1425 | return False; |
| 1426 | } |
| 1427 | return write_buf(to_gdb, buf, nrread, "to_gdb", /* notify */ False); |
| 1428 | } |
| 1429 | |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1430 | static |
| 1431 | void wait_for_gdb_connect (int in_port) |
| 1432 | { |
| 1433 | struct sockaddr_in addr; |
| 1434 | |
| 1435 | int listen_gdb = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 1436 | int gdb_connect; |
| 1437 | |
| 1438 | if (-1 == listen_gdb) { |
| 1439 | XERROR(errno, "cannot create socket"); |
| 1440 | } |
| 1441 | |
| 1442 | memset(&addr, 0, sizeof(addr)); |
| 1443 | |
| 1444 | addr.sin_family = AF_INET; |
| 1445 | addr.sin_port = htons((unsigned short int)in_port); |
| 1446 | addr.sin_addr.s_addr = INADDR_ANY; |
| 1447 | |
| 1448 | if (-1 == bind(listen_gdb,(struct sockaddr *)&addr, sizeof(addr))) { |
| 1449 | XERROR(errno, "bind failed"); |
| 1450 | } |
| 1451 | fprintf(stderr, "listening on port %d ...", in_port); |
| 1452 | fflush(stderr); |
| 1453 | if (-1 == listen(listen_gdb, 1)) { |
| 1454 | XERROR(errno, "error listen failed"); |
| 1455 | } |
| 1456 | |
| 1457 | gdb_connect = accept(listen_gdb, NULL, NULL); |
| 1458 | if (gdb_connect < 0) { |
| 1459 | XERROR(errno, "accept failed"); |
| 1460 | } |
| 1461 | fprintf(stderr, "connected.\n"); |
| 1462 | fflush(stderr); |
| 1463 | close(listen_gdb); |
| 1464 | from_gdb = gdb_connect; |
| 1465 | to_gdb = gdb_connect; |
| 1466 | } |
| 1467 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1468 | /* prepares the FIFOs filenames, map the shared memory. */ |
| 1469 | static |
| 1470 | void prepare_fifos_and_shared_mem(int pid) |
| 1471 | { |
florian | ab8630f | 2011-09-29 21:20:49 +0000 | [diff] [blame] | 1472 | const HChar *user, *host; |
| 1473 | unsigned len; |
| 1474 | |
| 1475 | user = getenv("LOGNAME"); |
| 1476 | if (user == NULL) user = getenv("USER"); |
| 1477 | if (user == NULL) user = "???"; |
| 1478 | |
| 1479 | host = getenv("HOST"); |
| 1480 | if (host == NULL) host = getenv("HOSTNAME"); |
| 1481 | if (host == NULL) host = "???"; |
| 1482 | |
| 1483 | len = strlen(vgdb_prefix) + strlen(user) + strlen(host) + 40; |
| 1484 | from_gdb_to_pid = vmalloc (len); |
| 1485 | to_gdb_from_pid = vmalloc (len); |
| 1486 | shared_mem = vmalloc (len); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1487 | /* below 3 lines must match the equivalent in remote-utils.c */ |
florian | ab8630f | 2011-09-29 21:20:49 +0000 | [diff] [blame] | 1488 | sprintf(from_gdb_to_pid, "%s-from-vgdb-to-%d-by-%s-on-%s", vgdb_prefix, |
| 1489 | pid, user, host); |
| 1490 | sprintf(to_gdb_from_pid, "%s-to-vgdb-from-%d-by-%s-on-%s", vgdb_prefix, |
| 1491 | pid, user, host); |
| 1492 | sprintf(shared_mem, "%s-shared-mem-vgdb-%d-by-%s-on-%s", vgdb_prefix, |
| 1493 | pid, user, host); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1494 | DEBUG (1, "vgdb: using %s %s %s\n", |
| 1495 | from_gdb_to_pid, to_gdb_from_pid, shared_mem); |
| 1496 | |
| 1497 | map_vgdbshared(shared_mem); |
| 1498 | } |
| 1499 | |
| 1500 | /* Convert hex digit A to a number. */ |
| 1501 | |
| 1502 | static int |
| 1503 | fromhex (int a) |
| 1504 | { |
| 1505 | if (a >= '0' && a <= '9') |
| 1506 | return a - '0'; |
| 1507 | else if (a >= 'a' && a <= 'f') |
| 1508 | return a - 'a' + 10; |
| 1509 | else |
| 1510 | XERROR(0, "Reply contains invalid hex digit %c\n", a); |
| 1511 | return 0; |
| 1512 | } |
| 1513 | |
| 1514 | /* Returns next char from fd. -1 if error, -2 if EOF. |
| 1515 | NB: must always call it with the same fd */ |
| 1516 | static int |
| 1517 | readchar (int fd) |
| 1518 | { |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 1519 | static char buf[PBUFSIZ+1]; // +1 for trailing \0 |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1520 | static int bufcnt = 0; |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 1521 | static unsigned char *bufp; |
| 1522 | // unsigned bufp to e.g. avoid having 255 converted to int -1 |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1523 | |
| 1524 | if (bufcnt-- > 0) |
| 1525 | return *bufp++; |
| 1526 | |
philippe | 1d76a80 | 2011-12-26 21:21:37 +0000 | [diff] [blame] | 1527 | bufcnt = read_buf (fd, buf, "static buf readchar"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1528 | |
| 1529 | if (bufcnt <= 0) { |
| 1530 | if (bufcnt == 0) { |
| 1531 | fprintf (stderr, "readchar: Got EOF\n"); |
| 1532 | return -2; |
| 1533 | } else { |
| 1534 | ERROR (errno, "readchar\n"); |
| 1535 | return -1; |
| 1536 | } |
| 1537 | } |
| 1538 | |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 1539 | bufp = (unsigned char *)buf; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1540 | bufcnt--; |
| 1541 | return *bufp++; |
| 1542 | } |
| 1543 | |
| 1544 | /* Read a packet from fromfd, with error checking, |
| 1545 | and store it in BUF. |
| 1546 | Returns length of packet, or -1 if error or -2 if EOF. |
| 1547 | Writes ack on ackfd */ |
| 1548 | |
| 1549 | static int |
| 1550 | getpkt (char *buf, int fromfd, int ackfd) |
| 1551 | { |
| 1552 | char *bp; |
| 1553 | unsigned char csum, c1, c2; |
| 1554 | int c; |
| 1555 | |
| 1556 | while (1) { |
| 1557 | csum = 0; |
| 1558 | |
| 1559 | while (1) { |
| 1560 | c = readchar (fromfd); |
| 1561 | if (c == '$') |
| 1562 | break; |
| 1563 | DEBUG(2, "[getpkt: discarding char '%c']\n", c); |
| 1564 | if (c < 0) |
| 1565 | return c; |
| 1566 | } |
| 1567 | |
| 1568 | bp = buf; |
| 1569 | while (1) { |
| 1570 | c = readchar (fromfd); |
| 1571 | if (c < 0) |
| 1572 | return c; |
| 1573 | if (c == '#') |
| 1574 | break; |
| 1575 | if (c == '*') { |
| 1576 | int repeat; |
| 1577 | int r; |
| 1578 | int prev; |
| 1579 | prev = *(bp-1); |
| 1580 | csum += c; |
| 1581 | repeat = readchar (fromfd); |
| 1582 | csum += repeat; |
| 1583 | for (r = 0; r < repeat - 29; r ++) |
| 1584 | *bp++ = prev; |
| 1585 | } else { |
| 1586 | *bp++ = c; |
| 1587 | csum += c; |
| 1588 | } |
| 1589 | } |
| 1590 | *bp = 0; |
| 1591 | |
| 1592 | c1 = fromhex (readchar (fromfd)); |
| 1593 | c2 = fromhex (readchar (fromfd)); |
| 1594 | |
| 1595 | if (csum == (c1 << 4) + c2) |
| 1596 | break; |
| 1597 | |
| 1598 | fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n", |
| 1599 | (c1 << 4) + c2, csum, buf); |
| 1600 | if (write (ackfd, "-", 1) != 1) |
| 1601 | ERROR(0, "error when writing - (nack)\n"); |
| 1602 | else |
| 1603 | add_written(1); |
| 1604 | } |
| 1605 | |
| 1606 | DEBUG(2, "getpkt (\"%s\"); [sending ack] \n", buf); |
| 1607 | if (write (ackfd, "+", 1) != 1) |
| 1608 | ERROR(0, "error when writing + (ack)\n"); |
| 1609 | else |
| 1610 | add_written(1); |
| 1611 | return bp - buf; |
| 1612 | } |
| 1613 | |
| 1614 | static int sigint = 0; |
| 1615 | static int sigterm = 0; |
| 1616 | static int sigpipe = 0; |
| 1617 | static int sighup = 0; |
| 1618 | static int sigusr1 = 0; |
| 1619 | static int sigalrm = 0; |
| 1620 | static int sigusr1_fd = -1; |
| 1621 | static pthread_t invoke_gdbserver_in_valgrind_thread; |
| 1622 | |
| 1623 | static |
| 1624 | void received_signal (int signum) |
| 1625 | { |
| 1626 | if (signum == SIGINT) |
| 1627 | sigint++; |
| 1628 | else if (signum == SIGUSR1) { |
| 1629 | sigusr1++; |
| 1630 | if (sigusr1_fd >= 0) { |
| 1631 | char control_c = '\003'; |
| 1632 | write_buf(sigusr1_fd, &control_c, 1, |
| 1633 | "write \\003 on SIGUSR1", /* notify */ True); |
| 1634 | } |
| 1635 | } |
| 1636 | else if (signum == SIGTERM) { |
| 1637 | shutting_down = True; |
| 1638 | sigterm++; |
| 1639 | } else if (signum == SIGHUP) { |
| 1640 | shutting_down = True; |
| 1641 | sighup++; |
| 1642 | } else if (signum == SIGPIPE) { |
| 1643 | sigpipe++; |
| 1644 | } else if (signum == SIGALRM) { |
| 1645 | sigalrm++; |
philippe | 5d5dd8e | 2012-08-05 00:08:25 +0000 | [diff] [blame] | 1646 | #if defined(VGPV_arm_linux_android) || defined(VGPV_x86_linux_android) |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1647 | /* Android has no pthread_cancel. As it also does not have |
| 1648 | PTRACE_INVOKER, there is no need for cleanup action. |
| 1649 | So, we just do nothing. */ |
| 1650 | DEBUG(1, "sigalrm received, no action on android\n"); |
| 1651 | #else |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1652 | /* Note: we cannot directly invoke restore_and_detach : this must |
| 1653 | be done by the thread that has attached. |
| 1654 | We have in this thread pushed a cleanup handler that will |
| 1655 | cleanup what is needed. */ |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1656 | DEBUG(1, "pthread_cancel invoke_gdbserver_in_valgrind_thread\n"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1657 | pthread_cancel(invoke_gdbserver_in_valgrind_thread); |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1658 | #endif |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1659 | } else { |
| 1660 | ERROR(0, "unexpected signal %d\n", signum); |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | /* install the signal handlers allowing e.g. vgdb to cleanup in |
| 1665 | case of termination. */ |
| 1666 | static |
| 1667 | void install_handlers(void) |
| 1668 | { |
| 1669 | struct sigaction action, oldaction; |
| 1670 | |
| 1671 | action.sa_handler = received_signal; |
| 1672 | sigemptyset (&action.sa_mask); |
| 1673 | action.sa_flags = 0; |
| 1674 | |
| 1675 | /* SIGINT: when user types C-c in gdb, this sends |
| 1676 | a SIGINT to vgdb + causes a character to be sent to remote gdbserver. |
| 1677 | The later is enough to wakeup the valgrind process. */ |
| 1678 | if (sigaction (SIGINT, &action, &oldaction) != 0) |
| 1679 | XERROR (errno, "vgdb error sigaction SIGINT\n"); |
| 1680 | /* We might do something more intelligent than just |
| 1681 | reporting this SIGINT E.g. behave similarly to the gdb: two |
| 1682 | control-C without feedback from the debugged process would |
| 1683 | mean to stop debugging it. */ |
| 1684 | |
| 1685 | /* SIGUSR1: this is used to facilitate automatic testing. When |
| 1686 | vgdb receives this signal, it will simulate the user typing C-c. */ |
| 1687 | if (sigaction (SIGUSR1, &action, &oldaction) != 0) |
| 1688 | XERROR (errno, "vgdb error sigaction SIGUSR1\n"); |
| 1689 | |
| 1690 | |
| 1691 | /* SIGTERM: can receive this signal (e.g. from gdb) to terminate vgdb |
| 1692 | when detaching or similar. A clean shutdown will be done as both |
| 1693 | the read and write side will detect an end of file. */ |
| 1694 | if (sigaction (SIGTERM, &action, &oldaction) != 0) |
| 1695 | XERROR (errno, "vgdb error sigaction SIGTERM\n"); |
| 1696 | |
| 1697 | /* SIGPIPE: can receive this signal when gdb detaches or kill the |
| 1698 | process debugged: gdb will close its pipes to vgdb. vgdb |
| 1699 | must resist to this signal to allow a clean shutdown. */ |
| 1700 | if (sigaction (SIGPIPE, &action, &oldaction) != 0) |
| 1701 | XERROR (errno, "vgdb error sigaction SIGPIPE\n"); |
| 1702 | |
| 1703 | /* SIGALRM: in case invoke thread is blocked, alarm is used |
| 1704 | to cleanup. */ |
| 1705 | if (sigaction (SIGALRM, &action, &oldaction) != 0) |
| 1706 | XERROR (errno, "vgdb error sigaction SIGALRM\n"); |
| 1707 | } |
| 1708 | |
| 1709 | /* close the FIFOs provided connections, terminate the invoker thread. */ |
| 1710 | static |
| 1711 | void close_connection(int to_pid, int from_pid) |
| 1712 | { |
| 1713 | DEBUG(1, "nr received signals: sigint %d sigterm %d sighup %d sigpipe %d\n", |
| 1714 | sigint, sigterm, sighup, sigpipe); |
| 1715 | /* Note that we do not forward sigterm to the valgrind process: |
| 1716 | a sigterm signal is (probably) received from gdb if the user wants to |
| 1717 | kill the debugged process. The kill instruction has been given to |
| 1718 | the valgrind process, which should execute a clean exit. */ |
| 1719 | |
| 1720 | /* We first close the connection to pid. The pid will then |
| 1721 | terminates its gdbserver work. We keep the from pid |
| 1722 | fifo opened till the invoker thread is finished. |
| 1723 | This allows the gdbserver to finish sending its last reply. */ |
| 1724 | if (close(to_pid) != 0) |
| 1725 | ERROR(errno, "close to_pid\n"); |
| 1726 | |
| 1727 | /* if there is a task that was busy trying to wake up valgrind |
| 1728 | process, we wait for it to be terminated otherwise threads |
| 1729 | in the valgrind process can stay stopped if vgdb main |
| 1730 | exits before the invoke thread had time to detach from |
| 1731 | all valgrind threads. */ |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1732 | if (max_invoke_ms > 0 || cmd_time_out != NEVER) { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1733 | int join; |
| 1734 | |
| 1735 | /* It is surprisingly complex to properly shutdown or exit the |
| 1736 | valgrind process in which gdbserver has been invoked through |
| 1737 | ptrace. In the normal case (gdb detaches from the process, |
| 1738 | or process is continued), the valgrind process will reach the |
| 1739 | breakpoint place. Using ptrace, vgdb will ensure the |
| 1740 | previous activity of the process is resumed (e.g. restart a |
| 1741 | blocking system call). The special case is when gdb asks the |
| 1742 | valgrind process to exit (using either the "kill" command or |
| 1743 | "monitor exit"). In such a case, the valgrind process will |
| 1744 | call exit. But a ptraced process will be blocked in exit, |
| 1745 | waiting for the ptracing process to detach or die. vgdb |
| 1746 | cannot detach unconditionally as otherwise, in the normal |
sewardj | b2572b5 | 2011-06-26 09:36:38 +0000 | [diff] [blame] | 1747 | case, the valgrind process would stop abnormally with SIGSTOP |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1748 | (as vgdb would not be there to catch it). vgdb can also not |
| 1749 | die unconditionally otherwise again, similar problem. So, we |
| 1750 | assume that most of the time, we arrive here in the normal |
| 1751 | case, and so, the breakpoint has been encountered by the |
| 1752 | valgrind process, so the invoker thread will exit and the |
| 1753 | join will succeed. For the "kill" case, we cause an alarm |
| 1754 | signal to be sent after a few seconds. This means that in the |
| 1755 | normal case, the gdbserver code in valgrind process must have |
| 1756 | returned the control in less than the alarm nr of seconds, |
sewardj | b2572b5 | 2011-06-26 09:36:38 +0000 | [diff] [blame] | 1757 | otherwise, valgrind will stop abnormally with SIGSTOP. */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1758 | (void) alarm (3); |
| 1759 | |
| 1760 | DEBUG(1, "joining with invoke_gdbserver_in_valgrind_thread\n"); |
| 1761 | join = pthread_join(invoke_gdbserver_in_valgrind_thread, NULL); |
| 1762 | if (join != 0) |
| 1763 | XERROR |
| 1764 | (join, |
| 1765 | "vgdb error pthread_join invoke_gdbserver_in_valgrind_thread\n"); |
| 1766 | } |
| 1767 | if (close(from_pid) != 0) |
| 1768 | ERROR(errno, "close from_pid\n"); |
| 1769 | } |
| 1770 | |
| 1771 | /* Relay data between gdb and Valgrind gdbserver, till EOF or an |
| 1772 | error is encountered. */ |
| 1773 | static |
| 1774 | void gdb_relay (int pid) |
| 1775 | { |
| 1776 | int from_pid = -1; /* fd to read from pid */ |
| 1777 | int to_pid = -1; /* fd to write to pid */ |
| 1778 | |
| 1779 | int shutdown_loop = 0; |
| 1780 | fprintf (stderr, "relaying data between gdb and process %d\n", pid); |
| 1781 | fflush (stderr); |
| 1782 | |
| 1783 | if (max_invoke_ms > 0) |
| 1784 | pthread_create(&invoke_gdbserver_in_valgrind_thread, NULL, |
| 1785 | invoke_gdbserver_in_valgrind, (void *) &pid); |
| 1786 | to_pid = open_fifo(from_gdb_to_pid, O_WRONLY, "write to pid"); |
| 1787 | acquire_lock (shared_mem_fd, pid); |
| 1788 | |
| 1789 | from_pid = open_fifo (to_gdb_from_pid, O_RDONLY|O_NONBLOCK, |
| 1790 | "read mode from pid"); |
| 1791 | |
| 1792 | sigusr1_fd = to_pid; /* allow simulating user typing control-c */ |
| 1793 | |
| 1794 | while (1) { |
| 1795 | ConnectionKind ck; |
| 1796 | int ret; |
| 1797 | struct pollfd pollfds[NumConnectionKind]; |
| 1798 | |
| 1799 | /* watch data written by gdb, watch POLLERR on both gdb fd */ |
| 1800 | pollfds[FROM_GDB].fd = from_gdb; |
| 1801 | pollfds[FROM_GDB].events = POLLIN; |
| 1802 | pollfds[FROM_GDB].revents = 0; |
| 1803 | pollfds[TO_GDB].fd = to_gdb; |
| 1804 | pollfds[TO_GDB].events = 0; |
| 1805 | pollfds[TO_GDB].revents = 0; |
| 1806 | |
| 1807 | /* watch data written by pid, watch POLLERR on both pid fd */ |
| 1808 | pollfds[FROM_PID].fd = from_pid; |
| 1809 | pollfds[FROM_PID].events = POLLIN; |
| 1810 | pollfds[FROM_PID].revents = 0; |
| 1811 | pollfds[TO_PID].fd = to_pid; |
| 1812 | pollfds[TO_PID].events = 0; |
| 1813 | pollfds[TO_PID].revents = 0; |
| 1814 | |
| 1815 | ret = poll(pollfds, |
| 1816 | NumConnectionKind, |
| 1817 | (shutting_down ? |
| 1818 | 1 /* one second */ |
| 1819 | : -1 /* infinite */)); |
| 1820 | DEBUG(2, "poll ret %d errno %d\n", ret, errno); |
| 1821 | |
| 1822 | /* check for unexpected error */ |
| 1823 | if (ret <= 0 && errno != EINTR) { |
| 1824 | ERROR (errno, "unexpected poll ret %d\n", ret); |
| 1825 | shutting_down = True; |
| 1826 | break; |
| 1827 | } |
| 1828 | |
| 1829 | /* check for data to read */ |
| 1830 | for (ck = 0; ck < NumConnectionKind; ck ++) { |
| 1831 | if (pollfds[ck].revents & POLLIN) { |
| 1832 | switch (ck) { |
| 1833 | case FROM_GDB: |
| 1834 | if (!read_from_gdb_write_to_pid(to_pid)) |
| 1835 | shutting_down = True; |
| 1836 | break; |
philippe | 1d76a80 | 2011-12-26 21:21:37 +0000 | [diff] [blame] | 1837 | case FROM_PID: |
| 1838 | if (!read_from_pid_write_to_gdb(from_pid)) |
| 1839 | shutting_down = True; |
| 1840 | break; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1841 | default: XERROR(0, "unexpected POLLIN on %s\n", |
| 1842 | ppConnectionKind(ck)); |
| 1843 | } |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | /* check for an fd being in error condition */ |
| 1848 | for (ck = 0; ck < NumConnectionKind; ck ++) { |
| 1849 | if (pollfds[ck].revents & POLLERR) { |
| 1850 | DEBUG(1, "connection %s fd %d POLLERR error condition\n", |
| 1851 | ppConnectionKind(ck), pollfds[ck].fd); |
| 1852 | valgrind_dying(); |
| 1853 | shutting_down = True; |
| 1854 | } |
| 1855 | if (pollfds[ck].revents & POLLHUP) { |
| 1856 | DEBUG(1, "connection %s fd %d POLLHUP error condition\n", |
| 1857 | ppConnectionKind(ck), pollfds[ck].fd); |
| 1858 | valgrind_dying(); |
| 1859 | shutting_down = True; |
| 1860 | } |
| 1861 | if (pollfds[ck].revents & POLLNVAL) { |
| 1862 | DEBUG(1, "connection %s fd %d POLLNVAL error condition\n", |
| 1863 | ppConnectionKind(ck), pollfds[ck].fd); |
| 1864 | valgrind_dying(); |
| 1865 | shutting_down = True; |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | if (shutting_down) { |
| 1870 | /* we let some time to the final packets to be transferred */ |
| 1871 | shutdown_loop++; |
| 1872 | if (shutdown_loop > 3) |
| 1873 | break; |
| 1874 | } |
| 1875 | } |
| 1876 | close_connection(to_pid, from_pid); |
| 1877 | } |
| 1878 | |
| 1879 | static int packet_len_for_command(char *cmd) |
| 1880 | { |
| 1881 | /* cmd will be send as a packet $qRcmd,xxxx....................xx#cc */ |
| 1882 | return 7+ 2*strlen(cmd) +3 + 1; |
| 1883 | } |
| 1884 | |
| 1885 | /* hyper-minimal protocol implementation that |
| 1886 | sends the provided commands (using qRcmd packets) |
| 1887 | and read and display their replies. */ |
| 1888 | static |
| 1889 | void standalone_send_commands(int pid, |
| 1890 | int last_command, |
| 1891 | char *commands[] ) |
| 1892 | { |
| 1893 | int from_pid = -1; /* fd to read from pid */ |
| 1894 | int to_pid = -1; /* fd to write to pid */ |
| 1895 | |
| 1896 | int i; |
| 1897 | int hi; |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 1898 | char hex[3]; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1899 | unsigned char cksum; |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 1900 | char *hexcommand; |
| 1901 | char buf[PBUFSIZ+1]; // +1 for trailing \0 |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1902 | int buflen; |
| 1903 | int nc; |
| 1904 | |
| 1905 | |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1906 | if (max_invoke_ms > 0 || cmd_time_out != NEVER) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1907 | pthread_create(&invoke_gdbserver_in_valgrind_thread, NULL, |
| 1908 | invoke_gdbserver_in_valgrind, (void *) &pid); |
| 1909 | |
| 1910 | to_pid = open_fifo(from_gdb_to_pid, O_WRONLY, "write to pid"); |
| 1911 | acquire_lock (shared_mem_fd, pid); |
| 1912 | |
| 1913 | /* first send a C-c \003 to pid, so that it wakes up the process |
| 1914 | After that, we can open the fifo from the pid in read mode |
| 1915 | We then start to wait for packets (normally first a resume reply) |
| 1916 | At that point, we send our command and expect replies */ |
| 1917 | buf[0] = '\003'; |
| 1918 | write_buf(to_pid, buf, 1, "write \\003 to wake up", /* notify */ True); |
| 1919 | from_pid = open_fifo(to_gdb_from_pid, O_RDONLY, |
| 1920 | "read cmd result from pid"); |
| 1921 | |
| 1922 | for (nc = 0; nc <= last_command; nc++) { |
| 1923 | fprintf (stderr, "sending command %s to pid %d\n", commands[nc], pid); |
| 1924 | fflush (stderr); |
| 1925 | |
| 1926 | /* prepare hexcommand $qRcmd,xxxx....................xx#cc */ |
| 1927 | hexcommand = vmalloc (packet_len_for_command(commands[nc])); |
| 1928 | hexcommand[0] = 0; |
| 1929 | strcat (hexcommand, "$qRcmd,"); |
| 1930 | for (i = 0; i < strlen(commands[nc]); i++) { |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 1931 | sprintf(hex, "%02x", (unsigned char) commands[nc][i]); |
| 1932 | // Need to use unsigned char, to avoid sign extension. |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1933 | strcat (hexcommand, hex); |
| 1934 | } |
| 1935 | /* checksum (but without the $) */ |
| 1936 | cksum = 0; |
| 1937 | for (hi = 1; hi < strlen(hexcommand); hi++) |
| 1938 | cksum+=hexcommand[hi]; |
| 1939 | strcat(hexcommand, "#"); |
| 1940 | sprintf(hex, "%02x", cksum); |
| 1941 | strcat(hexcommand, hex); |
| 1942 | write_buf(to_pid, hexcommand, strlen(hexcommand), |
| 1943 | "writing hex command to pid", /* notify */ True); |
| 1944 | |
| 1945 | /* we exit of the below loop explicitely when the command has |
| 1946 | been handled or because a signal handler will set |
| 1947 | shutting_down. */ |
| 1948 | while (!shutting_down) { |
| 1949 | buflen = getpkt(buf, from_pid, to_pid); |
| 1950 | if (buflen < 0) { |
| 1951 | ERROR (0, "error reading packet\n"); |
| 1952 | if (buflen == -2) |
| 1953 | valgrind_dying(); |
| 1954 | break; |
| 1955 | } |
| 1956 | if (strlen(buf) == 0) { |
| 1957 | DEBUG(0, "empty packet rcvd (packet qRcmd not recognised?)\n"); |
| 1958 | break; |
| 1959 | } |
| 1960 | if (strcmp(buf, "OK") == 0) { |
| 1961 | DEBUG(1, "OK packet rcvd\n"); |
| 1962 | break; |
| 1963 | } |
| 1964 | if (buf[0] == 'E') { |
| 1965 | DEBUG(0, |
| 1966 | "E NN error packet rcvd: %s (unknown monitor command?)\n", |
| 1967 | buf); |
| 1968 | break; |
| 1969 | } |
| 1970 | if (buf[0] == 'W') { |
| 1971 | DEBUG(0, "W stopped packet rcvd: %s\n", buf); |
| 1972 | break; |
| 1973 | } |
| 1974 | if (buf[0] == 'T') { |
| 1975 | DEBUG(1, "T resume reply packet received: %s\n", buf); |
| 1976 | continue; |
| 1977 | } |
| 1978 | |
| 1979 | /* must be here an O packet with hex encoded string reply |
| 1980 | => decode and print it */ |
| 1981 | if (buf[0] != 'O') { |
| 1982 | DEBUG(0, "expecting O packet, received: %s\n", buf); |
| 1983 | continue; |
| 1984 | } |
| 1985 | { |
| 1986 | char buf_print[buflen/2 + 1]; |
| 1987 | for (i = 1; i < buflen; i = i + 2) |
| 1988 | buf_print[i/2] = (fromhex(*(buf+i)) << 4) |
| 1989 | + fromhex(*(buf+i+1)); |
| 1990 | buf_print[buflen/2] = 0; |
| 1991 | printf("%s", buf_print); |
| 1992 | fflush(stdout); |
| 1993 | } |
| 1994 | } |
| 1995 | free (hexcommand); |
| 1996 | } |
| 1997 | shutting_down = True; |
| 1998 | |
| 1999 | close_connection(to_pid, from_pid); |
| 2000 | } |
| 2001 | |
| 2002 | /* report to user the existence of a vgdb-able valgrind process |
| 2003 | with given pid */ |
| 2004 | static |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2005 | void report_pid (int pid, Bool on_stdout) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2006 | { |
| 2007 | char cmdline_file[100]; |
| 2008 | char cmdline[1000]; |
| 2009 | int fd; |
| 2010 | int i, sz; |
| 2011 | |
| 2012 | sprintf(cmdline_file, "/proc/%d/cmdline", pid); |
| 2013 | fd = open (cmdline_file, O_RDONLY); |
| 2014 | if (fd == -1) { |
| 2015 | DEBUG(1, "error opening cmdline file %s %s\n", |
| 2016 | cmdline_file, strerror(errno)); |
| 2017 | sprintf(cmdline, "(could not obtain process command line)"); |
| 2018 | } else { |
| 2019 | sz = read(fd, cmdline, 1000); |
| 2020 | for (i = 0; i < sz; i++) |
| 2021 | if (cmdline[i] == 0) |
| 2022 | cmdline[i] = ' '; |
| 2023 | cmdline[sz] = 0; |
sewardj | d8c12f1 | 2011-05-11 22:54:57 +0000 | [diff] [blame] | 2024 | close (fd); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2025 | } |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2026 | fprintf((on_stdout ? stdout : stderr), "use --pid=%d for %s\n", pid, cmdline); |
| 2027 | fflush((on_stdout ? stdout : stderr)); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2030 | /* Possibly produces additional usage information documenting the |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2031 | ptrace restrictions. */ |
| 2032 | static |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2033 | void ptrace_restrictions_msg(void) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2034 | { |
| 2035 | # ifdef PR_SET_PTRACER |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 2036 | const char *ptrace_scope_setting_file = "/proc/sys/kernel/yama/ptrace_scope"; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2037 | int fd = -1; |
| 2038 | char ptrace_scope = 'X'; |
| 2039 | fd = open (ptrace_scope_setting_file, O_RDONLY, 0); |
| 2040 | if (fd >= 0 && (read (fd, &ptrace_scope, 1) == 1) && (ptrace_scope != '0')) { |
| 2041 | fprintf (stderr, |
| 2042 | "Note: your kernel restricts ptrace invoker using %s\n" |
| 2043 | "vgdb will only be able to attach to a Valgrind process\n" |
| 2044 | "blocked in a system call *after* an initial successful attach\n", |
| 2045 | ptrace_scope_setting_file); |
| 2046 | } else if (ptrace_scope == 'X') { |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2047 | DEBUG (1, |
| 2048 | "PR_SET_PTRACER defined" |
| 2049 | " but could not determine ptrace scope from %s\n", |
| 2050 | ptrace_scope_setting_file); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2051 | } |
| 2052 | if (fd >= 0) |
| 2053 | close (fd); |
| 2054 | # endif |
| 2055 | |
| 2056 | # ifndef PTRACEINVOKER |
| 2057 | fprintf(stderr, |
| 2058 | "Note: ptrace invoker not implemented\n" |
| 2059 | "For more info: read user manual section" |
| 2060 | " 'Limitations of the Valgrind gdbserver'\n"); |
| 2061 | # endif |
| 2062 | } |
| 2063 | |
| 2064 | static |
| 2065 | void usage(void) |
| 2066 | { |
| 2067 | fprintf(stderr, |
| 2068 | "Usage: vgdb [OPTION]... [[-c] COMMAND]...\n" |
| 2069 | "vgdb (valgrind gdb) has two usages\n" |
| 2070 | " 1. standalone to send monitor commands to a Valgrind gdbserver.\n" |
| 2071 | " The OPTION(s) must be followed by the command to send\n" |
| 2072 | " To send more than one command, separate the commands with -c\n" |
| 2073 | " 2. relay application between gdb and a Valgrind gdbserver.\n" |
| 2074 | " Only OPTION(s) can be given.\n" |
| 2075 | "\n" |
| 2076 | " OPTIONS are [--pid=<number>] [--vgdb-prefix=<prefix>]\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2077 | " [--wait=<number>] [--max-invoke-ms=<number>]\n" |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 2078 | " [--port=<portnr>\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2079 | " [--cmd-time-out=<number>] [-l] [-D] [-d]\n" |
| 2080 | " \n" |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2081 | " --pid arg must be given if multiple Valgrind gdbservers are found.\n" |
| 2082 | " --vgdb-prefix arg must be given to both Valgrind and vgdb utility\n" |
| 2083 | " if you want to change the default prefix for the FIFOs communication\n" |
| 2084 | " between the Valgrind gdbserver and vgdb.\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2085 | " --wait (default 0) tells vgdb to check during the specified number\n" |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2086 | " of seconds if a Valgrind gdbserver can be found.\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2087 | " --max-invoke-ms (default 100) gives the nr of milli-seconds after which vgdb\n" |
| 2088 | " will force the invocation of the Valgrind gdbserver (if the Valgrind\n" |
| 2089 | " process is blocked in a system call).\n" |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 2090 | " --port instructs vgdb to listen for gdb on the specified port nr.\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2091 | " --cmd-time-out (default 99999999) tells vgdb to exit if the found Valgrind\n" |
| 2092 | " gdbserver has not processed a command after number seconds\n" |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2093 | " -l arg tells to show the list of running Valgrind gdbserver and then exit.\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2094 | " -D arg tells to show shared mem status and then exit.\n" |
| 2095 | " -d arg tells to show debug info. Multiple -d args for more debug info\n" |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2096 | "\n" |
| 2097 | " -h --help shows this message\n" |
| 2098 | " To get help from the Valgrind gdbserver, use vgdb help\n" |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2099 | "\n" |
| 2100 | ); |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2101 | ptrace_restrictions_msg(); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2104 | /* If show_list, outputs on stdout the list of Valgrind processes with gdbserver activated. |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2105 | and then exits. |
| 2106 | |
| 2107 | else if arg_pid == -1, waits maximum check_trials seconds to discover |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2108 | a valgrind pid appearing. |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2109 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2110 | Otherwise verify arg_pid is valid and corresponds to a Valgrind process |
| 2111 | with gdbserver activated. |
| 2112 | |
| 2113 | Returns the pid to work with |
| 2114 | or exits in case of error (e.g. no pid found corresponding to arg_pid */ |
| 2115 | |
| 2116 | static |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2117 | int search_arg_pid(int arg_pid, int check_trials, Bool show_list) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2118 | { |
| 2119 | int i; |
| 2120 | int pid = -1; |
| 2121 | |
| 2122 | if (arg_pid == 0 || arg_pid < -1) { |
| 2123 | fprintf (stderr, "vgdb error: invalid pid %d given\n", arg_pid); |
| 2124 | exit (1); |
| 2125 | } else { |
| 2126 | /* search for a matching named fifo. |
| 2127 | If we have been given a pid, we will check that the matching FIFO is |
| 2128 | there (or wait the nr of check_trials for this to appear). |
| 2129 | If no pid has been given, then if we find only one FIFO, |
| 2130 | we will use this to build the pid to use. |
| 2131 | If we find multiple processes with valid FIFO, we report them and will |
| 2132 | exit with an error. */ |
| 2133 | DIR *vgdb_dir; |
| 2134 | char *vgdb_dir_name = vmalloc (strlen (vgdb_prefix) + 3); |
| 2135 | struct dirent *f; |
| 2136 | int is; |
| 2137 | int nr_valid_pid = 0; |
| 2138 | const char *suffix = "-from-vgdb-to-"; /* followed by pid */ |
| 2139 | char *vgdb_format = vmalloc (strlen(vgdb_prefix) + strlen(suffix) + 1); |
| 2140 | |
| 2141 | strcpy (vgdb_format, vgdb_prefix); |
| 2142 | strcat (vgdb_format, suffix); |
| 2143 | |
| 2144 | strcpy (vgdb_dir_name, vgdb_prefix); |
| 2145 | |
| 2146 | for (is = strlen(vgdb_prefix) - 1; is >= 0; is--) |
| 2147 | if (vgdb_dir_name[is] == '/') { |
| 2148 | vgdb_dir_name[is+1] = '\0'; |
| 2149 | break; |
| 2150 | } |
| 2151 | if (strlen(vgdb_dir_name) == 0) |
| 2152 | strcpy (vgdb_dir_name, "./"); |
| 2153 | |
| 2154 | DEBUG(1, "searching pid in directory %s format %s\n", |
| 2155 | vgdb_dir_name, vgdb_format); |
| 2156 | |
| 2157 | /* try to find FIFOs with valid pid. |
| 2158 | On exit of the loop, pid is set to: |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2159 | the last pid found if show_list (or -1 if no process was listed) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2160 | -1 if no FIFOs matching a running process is found |
| 2161 | -2 if multiple FIFOs of running processes are found |
| 2162 | otherwise it is set to the (only) pid found that can be debugged |
| 2163 | */ |
| 2164 | for (i = 0; i < check_trials; i++) { |
| 2165 | DEBUG(1, "check_trial %d \n", i); |
| 2166 | if (i > 0) |
| 2167 | /* wait one second before checking again */ |
| 2168 | sleep(1); |
| 2169 | |
| 2170 | vgdb_dir = opendir (vgdb_dir_name); |
| 2171 | if (vgdb_dir == NULL) |
| 2172 | XERROR (errno, |
| 2173 | "vgdb error: opening directory %s searching vgdb fifo\n", |
| 2174 | vgdb_dir_name); |
| 2175 | |
| 2176 | errno = 0; /* avoid complain if vgdb_dir is empty */ |
| 2177 | while ((f = readdir (vgdb_dir))) { |
| 2178 | struct stat st; |
philippe | bb4827b | 2012-04-06 11:51:04 +0000 | [diff] [blame] | 2179 | char pathname[strlen(vgdb_dir_name) + strlen(f->d_name) + 1]; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2180 | char *wrongpid; |
| 2181 | int newpid; |
| 2182 | |
| 2183 | strcpy (pathname, vgdb_dir_name); |
| 2184 | strcat (pathname, f->d_name); |
philippe | bb4827b | 2012-04-06 11:51:04 +0000 | [diff] [blame] | 2185 | DEBUG(3, "checking pathname is FIFO %s\n", pathname); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2186 | if (stat (pathname, &st) != 0) { |
| 2187 | if (debuglevel >= 3) |
| 2188 | ERROR (errno, "vgdb error: stat %s searching vgdb fifo\n", |
| 2189 | pathname); |
| 2190 | } else if (S_ISFIFO (st.st_mode)) { |
philippe | bb4827b | 2012-04-06 11:51:04 +0000 | [diff] [blame] | 2191 | DEBUG(3, "trying FIFO %s\n", pathname); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2192 | if (strncmp (pathname, vgdb_format, |
| 2193 | strlen (vgdb_format)) == 0) { |
| 2194 | newpid = strtol(pathname + strlen (vgdb_format), |
| 2195 | &wrongpid, 10); |
florian | ab8630f | 2011-09-29 21:20:49 +0000 | [diff] [blame] | 2196 | if (*wrongpid == '-' && newpid > 0 |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2197 | && kill (newpid, 0) == 0) { |
| 2198 | nr_valid_pid++; |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2199 | if (show_list) { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2200 | report_pid (newpid, /*on_stdout*/ True); |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2201 | pid = newpid; |
| 2202 | } else if (arg_pid != -1) { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2203 | if (arg_pid == newpid) { |
| 2204 | pid = newpid; |
| 2205 | } |
| 2206 | } else if (nr_valid_pid > 1) { |
| 2207 | if (nr_valid_pid == 2) { |
| 2208 | fprintf |
| 2209 | (stderr, |
| 2210 | "no --pid= arg given" |
| 2211 | " and multiple valgrind pids found:\n"); |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2212 | report_pid (pid, /*on_stdout*/ False); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2213 | } |
| 2214 | pid = -2; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2215 | report_pid (newpid, /*on_stdout*/ False); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2216 | } else { |
| 2217 | pid = newpid; |
| 2218 | } |
| 2219 | } |
| 2220 | } |
| 2221 | } |
| 2222 | errno = 0; /* avoid complain if at the end of vgdb_dir */ |
| 2223 | } |
| 2224 | if (f == NULL && errno != 0) |
| 2225 | XERROR (errno, "vgdb error: reading directory %s for vgdb fifo\n", |
| 2226 | vgdb_dir_name); |
| 2227 | |
| 2228 | closedir (vgdb_dir); |
| 2229 | if (pid != -1) |
| 2230 | break; |
| 2231 | } |
| 2232 | |
| 2233 | free (vgdb_dir_name); |
| 2234 | free (vgdb_format); |
| 2235 | } |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2236 | |
| 2237 | if (show_list) { |
| 2238 | exit (1); |
| 2239 | } else if (pid == -1) { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2240 | if (arg_pid == -1) |
| 2241 | fprintf (stderr, "vgdb error: no FIFO found and no pid given\n"); |
| 2242 | else |
| 2243 | fprintf (stderr, "vgdb error: no FIFO found matching pid %d\n", |
| 2244 | arg_pid); |
| 2245 | exit (1); |
| 2246 | } |
| 2247 | else if (pid == -2) { |
| 2248 | /* no arg_pid given, multiple FIFOs found */ |
| 2249 | exit (1); |
| 2250 | } |
| 2251 | else { |
| 2252 | return pid; |
| 2253 | } |
| 2254 | } |
| 2255 | |
| 2256 | /* return true if the numeric value of an option of the |
| 2257 | form --xxxxxxxxx=<number> could properly be extracted |
| 2258 | from arg. If True is returned, *value contains the |
| 2259 | extracted value.*/ |
| 2260 | static |
| 2261 | Bool numeric_val(char* arg, int *value) |
| 2262 | { |
| 2263 | const char *eq_pos = strchr(arg, '='); |
| 2264 | char *wrong; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2265 | long long int long_value; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2266 | |
| 2267 | if (eq_pos == NULL) |
| 2268 | return False; |
| 2269 | |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2270 | long_value = strtoll(eq_pos+1, &wrong, 10); |
| 2271 | if (long_value < 0 || long_value > INT_MAX) |
| 2272 | return False; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2273 | if (*wrong) |
| 2274 | return False; |
| 2275 | |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2276 | *value = (int) long_value; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2277 | return True; |
| 2278 | } |
| 2279 | |
| 2280 | /* true if arg matches the provided option */ |
| 2281 | static |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 2282 | Bool is_opt(char* arg, const char *option) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2283 | { |
| 2284 | int option_len = strlen(option); |
| 2285 | if (option[option_len-1] == '=') |
| 2286 | return (0 == strncmp(option, arg, option_len)); |
| 2287 | else |
| 2288 | return (0 == strcmp(option, arg)); |
| 2289 | } |
| 2290 | |
| 2291 | /* Parse command lines options. If error(s), exits. |
| 2292 | Otherwise returns the options in *p_... args. |
| 2293 | commands must be big enough for the commands extracted from argv. |
| 2294 | On return, *p_last_command gives the position in commands where |
| 2295 | the last command has been allocated (using vmalloc). */ |
| 2296 | static |
| 2297 | void parse_options(int argc, char** argv, |
| 2298 | Bool *p_show_shared_mem, |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2299 | Bool *p_show_list, |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2300 | int *p_arg_pid, |
| 2301 | int *p_check_trials, |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 2302 | int *p_port, |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2303 | int *p_last_command, |
| 2304 | char *commands[]) |
| 2305 | { |
| 2306 | Bool show_shared_mem = False; |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2307 | Bool show_list = False; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2308 | int arg_pid = -1; |
| 2309 | int check_trials = 1; |
| 2310 | int last_command = -1; |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 2311 | int int_port = 0; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2312 | |
| 2313 | int i; |
| 2314 | int arg_errors = 0; |
| 2315 | |
| 2316 | for (i = 1; i < argc; i++) { |
| 2317 | if (is_opt(argv[i], "--help") || is_opt(argv[i], "-h")) { |
| 2318 | usage(); |
| 2319 | exit(0); |
| 2320 | } else if (is_opt(argv[i], "-d")) { |
| 2321 | debuglevel++; |
| 2322 | } else if (is_opt(argv[i], "-D")) { |
| 2323 | show_shared_mem = True; |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2324 | } else if (is_opt(argv[i], "-l")) { |
| 2325 | show_list = True; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2326 | } else if (is_opt(argv[i], "--pid=")) { |
| 2327 | int newpid; |
| 2328 | if (!numeric_val(argv[i], &newpid)) { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2329 | fprintf (stderr, "invalid --pid argument %s\n", argv[i]); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2330 | arg_errors++; |
| 2331 | } else if (arg_pid != -1) { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2332 | fprintf (stderr, "multiple --pid arguments given\n"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2333 | arg_errors++; |
| 2334 | } else { |
| 2335 | arg_pid = newpid; |
| 2336 | } |
| 2337 | } else if (is_opt(argv[i], "--wait=")) { |
| 2338 | if (!numeric_val(argv[i], &check_trials)) { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2339 | fprintf (stderr, "invalid --wait argument %s\n", argv[i]); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2340 | arg_errors++; |
| 2341 | } |
| 2342 | } else if (is_opt(argv[i], "--max-invoke-ms=")) { |
| 2343 | if (!numeric_val(argv[i], &max_invoke_ms)) { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2344 | fprintf (stderr, "invalid --max-invoke-ms argument %s\n", argv[i]); |
| 2345 | arg_errors++; |
| 2346 | } |
| 2347 | } else if (is_opt(argv[i], "--cmd-time-out=")) { |
| 2348 | if (!numeric_val(argv[i], &cmd_time_out)) { |
| 2349 | fprintf (stderr, "invalid --cmd-time-out argument %s\n", argv[i]); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2350 | arg_errors++; |
| 2351 | } |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 2352 | } else if (is_opt(argv[i], "--port=")) { |
| 2353 | if (!numeric_val(argv[i], &int_port)) { |
| 2354 | fprintf (stderr, "invalid --port argument %s\n", argv[i]); |
| 2355 | arg_errors++; |
| 2356 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2357 | } else if (is_opt(argv[i], "--vgdb-prefix=")) { |
| 2358 | vgdb_prefix = argv[i] + 14; |
| 2359 | } else if (is_opt(argv[i], "-c")) { |
| 2360 | last_command++; |
| 2361 | commands[last_command] = vmalloc (1); |
| 2362 | commands[last_command][0] = '\0'; |
| 2363 | } else if (0 == strncmp(argv[i], "-", 1)) { |
| 2364 | fprintf (stderr, "unknown or invalid argument %s\n", argv[i]); |
| 2365 | arg_errors++; |
| 2366 | } else { |
| 2367 | int len; |
| 2368 | if (last_command == -1) { |
| 2369 | /* only one command, no -c command indicator */ |
| 2370 | last_command++; |
| 2371 | commands[last_command] = vmalloc (1); |
| 2372 | commands[last_command][0] = '\0'; |
| 2373 | } |
| 2374 | len = strlen(commands[last_command]); |
| 2375 | commands[last_command] = vrealloc (commands[last_command], |
| 2376 | len + 1 + strlen(argv[i]) + 1); |
| 2377 | if (len > 0) |
| 2378 | strcat (commands[last_command], " "); |
| 2379 | strcat (commands[last_command], argv[i]); |
| 2380 | if (packet_len_for_command(commands[last_command]) > PBUFSIZ) { |
| 2381 | fprintf (stderr, "command %s too long\n", commands[last_command]); |
| 2382 | arg_errors++; |
| 2383 | } |
| 2384 | |
| 2385 | } |
| 2386 | } |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2387 | |
florian | b985e2d | 2011-09-29 03:03:45 +0000 | [diff] [blame] | 2388 | if (vgdb_prefix == NULL) |
| 2389 | vgdb_prefix = vgdb_prefix_default(); |
| 2390 | |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2391 | if (isatty(0) |
| 2392 | && !show_shared_mem |
| 2393 | && !show_list |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 2394 | && int_port == 0 |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2395 | && last_command == -1) { |
| 2396 | arg_errors++; |
| 2397 | fprintf (stderr, |
| 2398 | "Using vgdb standalone implies to give -D or -l or a COMMAND\n"); |
| 2399 | } |
| 2400 | |
| 2401 | if (show_shared_mem && show_list) { |
| 2402 | arg_errors++; |
| 2403 | fprintf (stderr, |
| 2404 | "Can't use both -D and -l options\n"); |
| 2405 | } |
| 2406 | |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2407 | if (max_invoke_ms > 0 |
| 2408 | && cmd_time_out != NEVER |
| 2409 | && (cmd_time_out * 1000) <= max_invoke_ms) { |
| 2410 | arg_errors++; |
| 2411 | fprintf (stderr, |
| 2412 | "--max-invoke-ms must be < --cmd-time-out * 1000\n"); |
| 2413 | } |
| 2414 | |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2415 | if (show_list && arg_pid != -1) { |
| 2416 | arg_errors++; |
| 2417 | fprintf (stderr, |
| 2418 | "Can't use both --pid and -l options\n"); |
| 2419 | } |
| 2420 | |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 2421 | if (int_port > 0 && last_command != -1) { |
| 2422 | arg_errors++; |
| 2423 | fprintf (stderr, |
| 2424 | "Can't use --port to send commands\n"); |
| 2425 | } |
| 2426 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2427 | if (arg_errors > 0) { |
| 2428 | fprintf (stderr, "args error. Try `vgdb --help` for more information\n"); |
| 2429 | exit(1); |
| 2430 | } |
| 2431 | |
| 2432 | *p_show_shared_mem = show_shared_mem; |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2433 | *p_show_list = show_list; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2434 | *p_arg_pid = arg_pid; |
| 2435 | *p_check_trials = check_trials; |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 2436 | *p_port = int_port; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2437 | *p_last_command = last_command; |
| 2438 | } |
| 2439 | |
| 2440 | int main(int argc, char** argv) |
| 2441 | { |
| 2442 | int i; |
| 2443 | int pid; |
| 2444 | |
| 2445 | Bool show_shared_mem; |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2446 | Bool show_list; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2447 | int arg_pid; |
| 2448 | int check_trials; |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 2449 | int in_port; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2450 | int last_command; |
| 2451 | char *commands[argc]; // we will never have more commands than args. |
| 2452 | |
| 2453 | parse_options(argc, argv, |
| 2454 | &show_shared_mem, |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2455 | &show_list, |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2456 | &arg_pid, |
| 2457 | &check_trials, |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 2458 | &in_port, |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2459 | &last_command, |
| 2460 | commands); |
| 2461 | |
| 2462 | /* when we are working as a relay for gdb, handle some signals by |
| 2463 | only reporting them (according to debug level). Also handle these |
| 2464 | when ptrace will be used: vgdb must clean up the ptrace effect before |
| 2465 | dying. */ |
| 2466 | if (max_invoke_ms > 0 || last_command == -1) |
| 2467 | install_handlers(); |
| 2468 | |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2469 | pid = search_arg_pid (arg_pid, check_trials, show_list); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2470 | |
| 2471 | prepare_fifos_and_shared_mem(pid); |
| 2472 | |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 2473 | if (in_port > 0) |
| 2474 | wait_for_gdb_connect(in_port); |
| 2475 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2476 | if (show_shared_mem) { |
| 2477 | fprintf(stderr, |
| 2478 | "vgdb %d " |
| 2479 | "written_by_vgdb %d " |
| 2480 | "seen_by_valgrind %d\n" |
| 2481 | "vgdb pid %d\n", |
| 2482 | VS_vgdb_pid, |
| 2483 | VS_written_by_vgdb, |
| 2484 | VS_seen_by_valgrind, |
| 2485 | VS_vgdb_pid); |
| 2486 | exit (0); |
| 2487 | } |
| 2488 | |
| 2489 | if (last_command >= 0) { |
| 2490 | standalone_send_commands(pid, last_command, commands); |
| 2491 | } else { |
| 2492 | gdb_relay(pid); |
| 2493 | } |
| 2494 | |
| 2495 | |
| 2496 | free (from_gdb_to_pid); |
| 2497 | free (to_gdb_from_pid); |
| 2498 | free (shared_mem); |
| 2499 | |
| 2500 | for (i = 0; i <= last_command; i++) |
| 2501 | free (commands[i]); |
| 2502 | return 0; |
| 2503 | } |