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 | 0f157dd | 2013-10-18 14:27:36 +0000 | [diff] [blame] | 9 | Copyright (C) 2011-2013 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 | |
philippe | 3c761f0 | 2013-12-01 14:56:28 +0000 | [diff] [blame] | 29 | #include "vgdb.h" |
| 30 | |
florian | b985e2d | 2011-09-29 03:03:45 +0000 | [diff] [blame] | 31 | #include "config.h" |
sewardj | cbe3850 | 2011-07-12 11:59:11 +0000 | [diff] [blame] | 32 | |
philippe | 3c761f0 | 2013-12-01 14:56:28 +0000 | [diff] [blame] | 33 | #include <assert.h> |
| 34 | #include <dirent.h> |
| 35 | #include <errno.h> |
| 36 | #include <fcntl.h> |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 37 | #include <limits.h> |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 38 | #include <poll.h> |
| 39 | #include <pthread.h> |
philippe | 3c761f0 | 2013-12-01 14:56:28 +0000 | [diff] [blame] | 40 | #include <signal.h> |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 41 | #include <stdlib.h> |
| 42 | #include <stdio.h> |
philippe | 3c761f0 | 2013-12-01 14:56:28 +0000 | [diff] [blame] | 43 | #include <string.h> |
| 44 | #include <unistd.h> |
| 45 | #include <netinet/in.h> |
| 46 | #include <sys/mman.h> |
| 47 | #include <sys/socket.h> |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 48 | #include <sys/stat.h> |
| 49 | #include <sys/time.h> |
philippe | 3c761f0 | 2013-12-01 14:56:28 +0000 | [diff] [blame] | 50 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 51 | /* vgdb has two usages: |
| 52 | 1. relay application between gdb and the gdbserver embedded in valgrind. |
| 53 | 2. standalone to send monitor commands to a running valgrind-ified process |
| 54 | |
| 55 | It is made of a main program which reads arguments. If no |
| 56 | arguments are given or only --pid and --vgdb-prefix, then usage 1 is |
| 57 | assumed. |
| 58 | |
| 59 | As relay application, vgdb reads bytes from gdb on stdin and |
| 60 | writes these bytes to valgrind. Bytes read from valgrind are |
| 61 | written to gdb on stdout. Read/Write from/to valgrind is done |
| 62 | using FIFOs. There is one thread reading from stdin, writing to |
| 63 | valgrind on a FIFO. There is one thread reading from valgrind on a |
| 64 | FIFO, writing to gdb on stdout |
| 65 | |
| 66 | As a standalone utility, vgdb builds command packets to write to valgrind, |
| 67 | sends it and reads the reply. The same two threads are used to write/read. |
| 68 | Once all the commands are sent and their replies received, vgdb will exit. |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 69 | */ |
| 70 | |
philippe | 3c761f0 | 2013-12-01 14:56:28 +0000 | [diff] [blame] | 71 | int debuglevel; |
| 72 | struct timeval dbgtv; |
florian | b985e2d | 2011-09-29 03:03:45 +0000 | [diff] [blame] | 73 | static char *vgdb_prefix = NULL; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 74 | |
| 75 | /* Will be set to True when any condition indicating we have to shutdown |
| 76 | is encountered. */ |
philippe | 3c761f0 | 2013-12-01 14:56:28 +0000 | [diff] [blame] | 77 | Bool shutting_down = False; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 78 | |
philippe | 3c761f0 | 2013-12-01 14:56:28 +0000 | [diff] [blame] | 79 | VgdbShared32 *shared32; |
| 80 | VgdbShared64 *shared64; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 81 | #define VS_written_by_vgdb (shared32 != NULL ? \ |
| 82 | shared32->written_by_vgdb \ |
| 83 | : shared64->written_by_vgdb) |
| 84 | #define VS_seen_by_valgrind (shared32 != NULL ? \ |
| 85 | shared32->seen_by_valgrind \ |
| 86 | : shared64->seen_by_valgrind) |
| 87 | |
| 88 | #define VS_vgdb_pid (shared32 != NULL ? shared32->vgdb_pid : shared64->vgdb_pid) |
| 89 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 90 | void *vmalloc(size_t size) |
| 91 | { |
| 92 | void * mem = malloc(size); |
| 93 | if (mem == NULL) |
| 94 | XERROR (errno, "can't allocate memory\n"); |
| 95 | return mem; |
| 96 | } |
| 97 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 98 | void *vrealloc(void *ptr,size_t size) |
| 99 | { |
| 100 | void * mem = realloc(ptr, size); |
| 101 | if (mem == NULL) |
| 102 | XERROR (errno, "can't reallocate memory\n"); |
| 103 | return mem; |
| 104 | } |
| 105 | |
florian | b985e2d | 2011-09-29 03:03:45 +0000 | [diff] [blame] | 106 | /* Return the name of a directory for temporary files. */ |
| 107 | static |
| 108 | const char *vgdb_tmpdir(void) |
| 109 | { |
| 110 | const char *tmpdir; |
| 111 | |
| 112 | tmpdir = getenv("TMPDIR"); |
philippe | ae52a93 | 2012-01-13 21:36:46 +0000 | [diff] [blame] | 113 | if (tmpdir == NULL || *tmpdir == '\0') |
| 114 | tmpdir = VG_TMPDIR; |
| 115 | if (tmpdir == NULL || *tmpdir == '\0') |
| 116 | tmpdir = "/tmp"; /* fallback */ |
florian | b985e2d | 2011-09-29 03:03:45 +0000 | [diff] [blame] | 117 | |
| 118 | return tmpdir; |
| 119 | } |
| 120 | |
philippe | ae52a93 | 2012-01-13 21:36:46 +0000 | [diff] [blame] | 121 | /* 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] | 122 | to communicate with valgrind */ |
| 123 | static |
| 124 | char *vgdb_prefix_default(void) |
| 125 | { |
philippe | ae52a93 | 2012-01-13 21:36:46 +0000 | [diff] [blame] | 126 | static HChar *prefix; |
florian | b985e2d | 2011-09-29 03:03:45 +0000 | [diff] [blame] | 127 | |
philippe | ae52a93 | 2012-01-13 21:36:46 +0000 | [diff] [blame] | 128 | if (prefix == NULL) { |
| 129 | const char *tmpdir = vgdb_tmpdir(); |
| 130 | prefix = vmalloc(strlen(tmpdir) + strlen("/vgdb-pipe") + 1); |
| 131 | strcpy(prefix, tmpdir); |
| 132 | strcat(prefix, "/vgdb-pipe"); |
| 133 | } |
florian | b985e2d | 2011-09-29 03:03:45 +0000 | [diff] [blame] | 134 | return prefix; |
| 135 | } |
| 136 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 137 | /* add nrw to the written_by_vgdb field of shared32 or shared64 */ |
| 138 | static |
| 139 | void add_written(int nrw) |
| 140 | { |
| 141 | if (shared32 != NULL) |
| 142 | shared32->written_by_vgdb += nrw; |
| 143 | else if (shared64 != NULL) |
| 144 | shared64->written_by_vgdb += nrw; |
| 145 | else |
| 146 | assert(0); |
| 147 | } |
| 148 | |
| 149 | static int shared_mem_fd = -1; |
| 150 | static |
| 151 | void map_vgdbshared (char* shared_mem) |
| 152 | { |
| 153 | struct stat fdstat; |
| 154 | void **s; |
| 155 | shared_mem_fd = open(shared_mem, O_RDWR); |
| 156 | /* shared_mem_fd will not be closed till vgdb exits. */ |
| 157 | |
| 158 | if (shared_mem_fd == -1) |
| 159 | XERROR (errno, "error opening %s shared memory file\n", shared_mem); |
| 160 | |
| 161 | if (fstat(shared_mem_fd, &fdstat) != 0) |
| 162 | XERROR (errno, "fstat"); |
| 163 | |
| 164 | if (fdstat.st_size == sizeof(VgdbShared64)) |
| 165 | s = (void*) &shared64; |
| 166 | else if (fdstat.st_size == sizeof(VgdbShared32)) |
| 167 | s = (void*) &shared32; |
| 168 | else |
| 169 | #if VEX_HOST_WORDSIZE == 8 |
| 170 | XERROR (0, |
| 171 | "error size shared memory file %s.\n" |
| 172 | "expecting size %d (64bits) or %d (32bits) got %ld.\n", |
| 173 | shared_mem, |
| 174 | (int) sizeof(VgdbShared64), (int) sizeof(VgdbShared32), |
| 175 | (long int)fdstat.st_size); |
| 176 | #elif VEX_HOST_WORDSIZE == 4 |
| 177 | XERROR (0, |
| 178 | "error size shared memory file %s.\n" |
| 179 | "expecting size %d (32bits) got %ld.\n", |
| 180 | shared_mem, |
| 181 | (int) sizeof(VgdbShared32), |
| 182 | fdstat.st_size); |
| 183 | #else |
| 184 | # error "unexpected wordsize" |
| 185 | #endif |
| 186 | |
| 187 | #if VEX_HOST_WORDSIZE == 4 |
| 188 | if (shared64 != NULL) |
| 189 | XERROR (0, "cannot use 32 bits vgdb with a 64bits valgrind process\n"); |
| 190 | /* But we can use a 64 bits vgdb with a 32 bits valgrind */ |
| 191 | #endif |
| 192 | |
| 193 | *s = (void*) mmap (NULL, fdstat.st_size, |
| 194 | PROT_READ|PROT_WRITE, MAP_SHARED, |
| 195 | shared_mem_fd, 0); |
| 196 | |
| 197 | if (*s == (void *) -1) |
| 198 | XERROR (errno, "error mmap shared memory file %s\n", shared_mem); |
| 199 | |
| 200 | } |
| 201 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 202 | /* This function loops till shutting_down becomes true. In this loop, |
| 203 | it verifies if valgrind process is reading the characters written |
| 204 | by vgdb. The verification is done every max_invoke_ms ms. If |
philippe | 3c761f0 | 2013-12-01 14:56:28 +0000 | [diff] [blame] | 205 | valgrind is not reading characters, it will use invoker_invoke_gdbserver |
| 206 | to ensure that the gdbserver code is called soon by valgrind. */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 207 | static int max_invoke_ms = 100; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 208 | #define NEVER 99999999 |
| 209 | static int cmd_time_out = NEVER; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 210 | static |
| 211 | void *invoke_gdbserver_in_valgrind(void *v_pid) |
| 212 | { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 213 | struct timeval cmd_max_end_time; |
| 214 | Bool cmd_started = False; |
| 215 | struct timeval invoke_time; |
| 216 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 217 | int pid = *(int *)v_pid; |
| 218 | int written_by_vgdb_before_sleep; |
| 219 | int seen_by_valgrind_before_sleep; |
| 220 | |
| 221 | int invoked_written = -1; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 222 | unsigned int usecs; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 223 | |
philippe | 3c761f0 | 2013-12-01 14:56:28 +0000 | [diff] [blame] | 224 | pthread_cleanup_push(invoker_cleanup_restore_and_detach, v_pid); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 225 | |
| 226 | while (!shutting_down) { |
| 227 | written_by_vgdb_before_sleep = VS_written_by_vgdb; |
| 228 | seen_by_valgrind_before_sleep = VS_seen_by_valgrind; |
| 229 | DEBUG(3, |
| 230 | "written_by_vgdb_before_sleep %d " |
| 231 | "seen_by_valgrind_before_sleep %d\n", |
| 232 | written_by_vgdb_before_sleep, |
| 233 | seen_by_valgrind_before_sleep); |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 234 | if (cmd_time_out != NEVER |
| 235 | && !cmd_started |
| 236 | && written_by_vgdb_before_sleep > seen_by_valgrind_before_sleep) { |
| 237 | /* A command was started. Record the time at which it was started. */ |
| 238 | DEBUG(1, "IO for command started\n"); |
| 239 | gettimeofday(&cmd_max_end_time, NULL); |
| 240 | cmd_max_end_time.tv_sec += cmd_time_out; |
| 241 | cmd_started = True; |
| 242 | } |
| 243 | if (max_invoke_ms > 0) { |
| 244 | usecs = 1000 * max_invoke_ms; |
| 245 | gettimeofday(&invoke_time, NULL); |
| 246 | invoke_time.tv_sec += max_invoke_ms / 1000; |
| 247 | invoke_time.tv_usec += 1000 * (max_invoke_ms % 1000); |
| 248 | invoke_time.tv_sec += invoke_time.tv_usec / (1000 * 1000); |
| 249 | invoke_time.tv_usec = invoke_time.tv_usec % (1000 * 1000); |
| 250 | } else { |
| 251 | usecs = 0; |
| 252 | } |
| 253 | if (cmd_started) { |
| 254 | // 0 usecs here means the thread just has to check gdbserver eats |
| 255 | // the characters in <= cmd_time_out seconds. |
| 256 | // We will just wait by 1 second max at a time. |
| 257 | if (usecs == 0 || usecs > 1000 * 1000) |
| 258 | usecs = 1000 * 1000; |
| 259 | } |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 260 | usleep(usecs); |
| 261 | |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 262 | /* If nothing happened during our sleep, let's try to wake up valgrind |
| 263 | or check for cmd time out. */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 264 | if (written_by_vgdb_before_sleep == VS_written_by_vgdb |
| 265 | && seen_by_valgrind_before_sleep == VS_seen_by_valgrind |
| 266 | && VS_written_by_vgdb > VS_seen_by_valgrind) { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 267 | struct timeval now; |
| 268 | gettimeofday(&now, NULL); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 269 | DEBUG(2, |
| 270 | "after sleep " |
| 271 | "written_by_vgdb %d " |
| 272 | "seen_by_valgrind %d " |
| 273 | "invoked_written %d\n", |
| 274 | VS_written_by_vgdb, |
| 275 | VS_seen_by_valgrind, |
| 276 | invoked_written); |
| 277 | /* if the pid does not exist anymore, we better stop */ |
| 278 | if (kill(pid, 0) != 0) |
| 279 | XERROR (errno, |
| 280 | "invoke_gdbserver_in_valgrind: " |
| 281 | "check for pid %d existence failed\n", pid); |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 282 | if (cmd_started) { |
| 283 | if (timercmp (&now, &cmd_max_end_time, >)) |
| 284 | XERROR (0, |
| 285 | "pid %d did not handle a command in %d seconds\n", |
| 286 | pid, cmd_time_out); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 287 | } |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 288 | if (max_invoke_ms > 0 && timercmp (&now, &invoke_time, >=)) { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 289 | /* only need to wake up if the nr written has changed since |
| 290 | last invoke. */ |
| 291 | if (invoked_written != written_by_vgdb_before_sleep) { |
philippe | 3c761f0 | 2013-12-01 14:56:28 +0000 | [diff] [blame] | 292 | if (invoker_invoke_gdbserver(pid)) { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 293 | /* If invoke succesful, no need to invoke again |
| 294 | for the same value of written_by_vgdb_before_sleep. */ |
| 295 | invoked_written = written_by_vgdb_before_sleep; |
| 296 | } |
| 297 | } |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 298 | } |
| 299 | } else { |
| 300 | // Something happened => restart timer check. |
| 301 | if (cmd_time_out != NEVER) { |
| 302 | DEBUG(2, "some IO was done => restart command\n"); |
| 303 | cmd_started = False; |
| 304 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | pthread_cleanup_pop(0); |
| 308 | return NULL; |
| 309 | } |
| 310 | |
| 311 | static |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 312 | int open_fifo (const char* name, int flags, const char* desc) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 313 | { |
| 314 | int fd; |
| 315 | DEBUG(1, "opening %s %s\n", name, desc); |
| 316 | fd = open(name, flags); |
| 317 | if (fd == -1) |
| 318 | XERROR (errno, "error opening %s %s\n", name, desc); |
| 319 | |
| 320 | DEBUG(1, "opened %s %s fd %d\n", name, desc, fd); |
| 321 | return fd; |
| 322 | } |
| 323 | |
| 324 | /* acquire a lock on the first byte of the given fd. If not successful, |
| 325 | exits with error. |
| 326 | This allows to avoid having two vgdb speaking with the same Valgrind |
| 327 | gdbserver as this causes serious headaches to the protocol. */ |
| 328 | static |
| 329 | void acquire_lock (int fd, int valgrind_pid) |
| 330 | { |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 331 | struct flock fl; |
| 332 | fl.l_type = F_WRLCK; |
| 333 | fl.l_whence = SEEK_SET; |
| 334 | fl.l_start = 0; |
| 335 | fl.l_len = 1; |
| 336 | if (fcntl(fd, F_SETLK, &fl) < 0) { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 337 | if (errno == EAGAIN || errno == EACCES) { |
| 338 | XERROR(errno, |
| 339 | "Cannot acquire lock.\n" |
| 340 | "Probably vgdb pid %d already speaks with Valgrind pid %d\n", |
| 341 | VS_vgdb_pid, |
| 342 | valgrind_pid); |
| 343 | } else { |
| 344 | XERROR(errno, "cannot acquire lock.\n"); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /* Here, we have the lock. It will be released when fd will be closed. */ |
| 349 | /* We indicate our pid to Valgrind gdbserver */ |
| 350 | if (shared32 != NULL) |
| 351 | shared32->vgdb_pid = getpid(); |
| 352 | else if (shared64 != NULL) |
| 353 | shared64->vgdb_pid = getpid(); |
| 354 | else |
| 355 | assert(0); |
| 356 | } |
| 357 | |
| 358 | #define PBUFSIZ 16384 /* keep in sync with server.h */ |
| 359 | |
| 360 | /* read some characters from fd. |
| 361 | Returns the nr of characters read, -1 if error. |
| 362 | desc is a string used in tracing */ |
| 363 | static |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 364 | int read_buf (int fd, char* buf, const char* desc) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 365 | { |
| 366 | int nrread; |
| 367 | DEBUG(2, "reading %s\n", desc); |
| 368 | nrread = read(fd, buf, PBUFSIZ); |
| 369 | if (nrread == -1) { |
| 370 | ERROR (errno, "error reading %s\n", desc); |
| 371 | return -1; |
| 372 | } |
| 373 | buf[nrread] = '\0'; |
| 374 | DEBUG(2, "read %s %s\n", desc, buf); |
| 375 | return nrread; |
| 376 | } |
| 377 | |
| 378 | /* write size bytes from buf to fd. |
| 379 | desc is a description of the action for which the write is done. |
| 380 | If notify, then add size to the shared cntr indicating to the |
| 381 | valgrind process that there is new data. |
| 382 | Returns True if write is ok, False if there was a problem. */ |
| 383 | static |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 384 | 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] | 385 | { |
| 386 | int nrwritten; |
| 387 | int nrw; |
philippe | 66bc6e5 | 2012-10-31 15:07:39 +0000 | [diff] [blame] | 388 | DEBUG(2, "writing %s len %d %.*s notify: %d\n", desc, size, |
| 389 | size, buf, notify); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 390 | nrwritten = 0; |
| 391 | while (nrwritten < size) { |
| 392 | nrw = write (fd, buf+nrwritten, size - nrwritten); |
| 393 | if (nrw == -1) { |
| 394 | ERROR(errno, "error write %s\n", desc); |
| 395 | return False; |
| 396 | } |
| 397 | nrwritten = nrwritten + nrw; |
| 398 | if (notify) |
| 399 | add_written(nrw); |
| 400 | } |
| 401 | return True; |
| 402 | } |
| 403 | |
| 404 | typedef enum { |
| 405 | FROM_GDB, |
| 406 | TO_GDB, |
| 407 | FROM_PID, |
| 408 | TO_PID } ConnectionKind; |
| 409 | static const int NumConnectionKind = TO_PID+1; |
| 410 | static |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 411 | const char *ppConnectionKind (ConnectionKind con) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 412 | { |
| 413 | switch (con) { |
| 414 | case FROM_GDB: return "FROM_GDB"; |
| 415 | case TO_GDB: return "TO_GDB"; |
| 416 | case FROM_PID: return "FROM_PID"; |
| 417 | case TO_PID: return "TO_PID"; |
| 418 | default: return "invalid connection kind"; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | static char *shared_mem; |
| 423 | |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 424 | static int from_gdb = 0; /* stdin by default, changed if --port is given. */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 425 | static char *from_gdb_to_pid; /* fifo name to write gdb command to pid */ |
| 426 | /* Returns True in case read/write operations were done properly. |
| 427 | Returns False in case of error. |
| 428 | to_pid is the file descriptor to write to the process pid. */ |
| 429 | static |
| 430 | Bool read_from_gdb_write_to_pid(int to_pid) |
| 431 | { |
philippe | 1d76a80 | 2011-12-26 21:21:37 +0000 | [diff] [blame] | 432 | char buf[PBUFSIZ+1]; // +1 for trailing \0 |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 433 | int nrread; |
| 434 | |
| 435 | nrread = read_buf(from_gdb, buf, "from gdb on stdin"); |
| 436 | if (nrread <= 0) { |
| 437 | if (nrread == 0) |
| 438 | DEBUG(1, "read 0 bytes from gdb => assume exit\n"); |
| 439 | else |
| 440 | DEBUG(1, "error reading bytes from gdb\n"); |
| 441 | close (from_gdb); |
| 442 | shutting_down = True; |
| 443 | return False; |
| 444 | } |
| 445 | return write_buf(to_pid, buf, nrread, "to_pid", /* notify */ True); |
| 446 | } |
| 447 | |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 448 | static int to_gdb = 1; /* stdout by default, changed if --port is given. */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 449 | static char *to_gdb_from_pid; /* fifo name to read pid replies */ |
| 450 | /* Returns True in case read/write operations were done properly. |
| 451 | Returns False in case of error. |
| 452 | from_pid is the file descriptor to read data from the process pid. */ |
| 453 | static |
| 454 | Bool read_from_pid_write_to_gdb(int from_pid) |
| 455 | { |
philippe | 1d76a80 | 2011-12-26 21:21:37 +0000 | [diff] [blame] | 456 | char buf[PBUFSIZ+1]; // +1 for trailing \0 |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 457 | int nrread; |
| 458 | |
| 459 | nrread = read_buf(from_pid, buf, "from pid"); |
| 460 | if (nrread <= 0) { |
| 461 | if (nrread == 0) |
| 462 | DEBUG(1, "read 0 bytes from pid => assume exit\n"); |
| 463 | else |
| 464 | DEBUG(1, "error reading bytes from pid\n"); |
| 465 | close (from_pid); |
| 466 | shutting_down = True; |
| 467 | return False; |
| 468 | } |
| 469 | return write_buf(to_gdb, buf, nrread, "to_gdb", /* notify */ False); |
| 470 | } |
| 471 | |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 472 | static |
| 473 | void wait_for_gdb_connect (int in_port) |
| 474 | { |
| 475 | struct sockaddr_in addr; |
| 476 | |
| 477 | int listen_gdb = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 478 | int gdb_connect; |
| 479 | |
| 480 | if (-1 == listen_gdb) { |
| 481 | XERROR(errno, "cannot create socket"); |
| 482 | } |
| 483 | |
| 484 | memset(&addr, 0, sizeof(addr)); |
| 485 | |
| 486 | addr.sin_family = AF_INET; |
| 487 | addr.sin_port = htons((unsigned short int)in_port); |
| 488 | addr.sin_addr.s_addr = INADDR_ANY; |
| 489 | |
| 490 | if (-1 == bind(listen_gdb,(struct sockaddr *)&addr, sizeof(addr))) { |
| 491 | XERROR(errno, "bind failed"); |
| 492 | } |
| 493 | fprintf(stderr, "listening on port %d ...", in_port); |
| 494 | fflush(stderr); |
| 495 | if (-1 == listen(listen_gdb, 1)) { |
| 496 | XERROR(errno, "error listen failed"); |
| 497 | } |
| 498 | |
| 499 | gdb_connect = accept(listen_gdb, NULL, NULL); |
| 500 | if (gdb_connect < 0) { |
| 501 | XERROR(errno, "accept failed"); |
| 502 | } |
| 503 | fprintf(stderr, "connected.\n"); |
| 504 | fflush(stderr); |
| 505 | close(listen_gdb); |
| 506 | from_gdb = gdb_connect; |
| 507 | to_gdb = gdb_connect; |
| 508 | } |
| 509 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 510 | /* prepares the FIFOs filenames, map the shared memory. */ |
| 511 | static |
| 512 | void prepare_fifos_and_shared_mem(int pid) |
| 513 | { |
florian | ab8630f | 2011-09-29 21:20:49 +0000 | [diff] [blame] | 514 | const HChar *user, *host; |
| 515 | unsigned len; |
| 516 | |
| 517 | user = getenv("LOGNAME"); |
| 518 | if (user == NULL) user = getenv("USER"); |
| 519 | if (user == NULL) user = "???"; |
| 520 | |
| 521 | host = getenv("HOST"); |
| 522 | if (host == NULL) host = getenv("HOSTNAME"); |
| 523 | if (host == NULL) host = "???"; |
| 524 | |
| 525 | len = strlen(vgdb_prefix) + strlen(user) + strlen(host) + 40; |
| 526 | from_gdb_to_pid = vmalloc (len); |
| 527 | to_gdb_from_pid = vmalloc (len); |
| 528 | shared_mem = vmalloc (len); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 529 | /* below 3 lines must match the equivalent in remote-utils.c */ |
florian | ab8630f | 2011-09-29 21:20:49 +0000 | [diff] [blame] | 530 | sprintf(from_gdb_to_pid, "%s-from-vgdb-to-%d-by-%s-on-%s", vgdb_prefix, |
| 531 | pid, user, host); |
| 532 | sprintf(to_gdb_from_pid, "%s-to-vgdb-from-%d-by-%s-on-%s", vgdb_prefix, |
| 533 | pid, user, host); |
| 534 | sprintf(shared_mem, "%s-shared-mem-vgdb-%d-by-%s-on-%s", vgdb_prefix, |
| 535 | pid, user, host); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 536 | DEBUG (1, "vgdb: using %s %s %s\n", |
| 537 | from_gdb_to_pid, to_gdb_from_pid, shared_mem); |
| 538 | |
| 539 | map_vgdbshared(shared_mem); |
| 540 | } |
| 541 | |
| 542 | /* Convert hex digit A to a number. */ |
| 543 | |
| 544 | static int |
| 545 | fromhex (int a) |
| 546 | { |
| 547 | if (a >= '0' && a <= '9') |
| 548 | return a - '0'; |
| 549 | else if (a >= 'a' && a <= 'f') |
| 550 | return a - 'a' + 10; |
| 551 | else |
| 552 | XERROR(0, "Reply contains invalid hex digit %c\n", a); |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | /* Returns next char from fd. -1 if error, -2 if EOF. |
| 557 | NB: must always call it with the same fd */ |
| 558 | static int |
| 559 | readchar (int fd) |
| 560 | { |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 561 | static char buf[PBUFSIZ+1]; // +1 for trailing \0 |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 562 | static int bufcnt = 0; |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 563 | static unsigned char *bufp; |
| 564 | // unsigned bufp to e.g. avoid having 255 converted to int -1 |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 565 | |
| 566 | if (bufcnt-- > 0) |
| 567 | return *bufp++; |
| 568 | |
philippe | 1d76a80 | 2011-12-26 21:21:37 +0000 | [diff] [blame] | 569 | bufcnt = read_buf (fd, buf, "static buf readchar"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 570 | |
| 571 | if (bufcnt <= 0) { |
| 572 | if (bufcnt == 0) { |
| 573 | fprintf (stderr, "readchar: Got EOF\n"); |
| 574 | return -2; |
| 575 | } else { |
| 576 | ERROR (errno, "readchar\n"); |
| 577 | return -1; |
| 578 | } |
| 579 | } |
| 580 | |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 581 | bufp = (unsigned char *)buf; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 582 | bufcnt--; |
| 583 | return *bufp++; |
| 584 | } |
| 585 | |
| 586 | /* Read a packet from fromfd, with error checking, |
| 587 | and store it in BUF. |
| 588 | Returns length of packet, or -1 if error or -2 if EOF. |
| 589 | Writes ack on ackfd */ |
| 590 | |
| 591 | static int |
| 592 | getpkt (char *buf, int fromfd, int ackfd) |
| 593 | { |
| 594 | char *bp; |
| 595 | unsigned char csum, c1, c2; |
| 596 | int c; |
| 597 | |
| 598 | while (1) { |
| 599 | csum = 0; |
| 600 | |
| 601 | while (1) { |
| 602 | c = readchar (fromfd); |
| 603 | if (c == '$') |
| 604 | break; |
| 605 | DEBUG(2, "[getpkt: discarding char '%c']\n", c); |
| 606 | if (c < 0) |
| 607 | return c; |
| 608 | } |
| 609 | |
| 610 | bp = buf; |
| 611 | while (1) { |
| 612 | c = readchar (fromfd); |
| 613 | if (c < 0) |
| 614 | return c; |
| 615 | if (c == '#') |
| 616 | break; |
| 617 | if (c == '*') { |
| 618 | int repeat; |
| 619 | int r; |
| 620 | int prev; |
| 621 | prev = *(bp-1); |
| 622 | csum += c; |
| 623 | repeat = readchar (fromfd); |
| 624 | csum += repeat; |
| 625 | for (r = 0; r < repeat - 29; r ++) |
| 626 | *bp++ = prev; |
| 627 | } else { |
| 628 | *bp++ = c; |
| 629 | csum += c; |
| 630 | } |
| 631 | } |
| 632 | *bp = 0; |
| 633 | |
| 634 | c1 = fromhex (readchar (fromfd)); |
| 635 | c2 = fromhex (readchar (fromfd)); |
| 636 | |
| 637 | if (csum == (c1 << 4) + c2) |
| 638 | break; |
| 639 | |
| 640 | fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n", |
| 641 | (c1 << 4) + c2, csum, buf); |
| 642 | if (write (ackfd, "-", 1) != 1) |
| 643 | ERROR(0, "error when writing - (nack)\n"); |
| 644 | else |
| 645 | add_written(1); |
| 646 | } |
| 647 | |
| 648 | DEBUG(2, "getpkt (\"%s\"); [sending ack] \n", buf); |
| 649 | if (write (ackfd, "+", 1) != 1) |
| 650 | ERROR(0, "error when writing + (ack)\n"); |
| 651 | else |
| 652 | add_written(1); |
| 653 | return bp - buf; |
| 654 | } |
| 655 | |
| 656 | static int sigint = 0; |
| 657 | static int sigterm = 0; |
| 658 | static int sigpipe = 0; |
| 659 | static int sighup = 0; |
| 660 | static int sigusr1 = 0; |
| 661 | static int sigalrm = 0; |
| 662 | static int sigusr1_fd = -1; |
| 663 | static pthread_t invoke_gdbserver_in_valgrind_thread; |
| 664 | |
| 665 | static |
| 666 | void received_signal (int signum) |
| 667 | { |
| 668 | if (signum == SIGINT) |
| 669 | sigint++; |
| 670 | else if (signum == SIGUSR1) { |
| 671 | sigusr1++; |
| 672 | if (sigusr1_fd >= 0) { |
| 673 | char control_c = '\003'; |
| 674 | write_buf(sigusr1_fd, &control_c, 1, |
| 675 | "write \\003 on SIGUSR1", /* notify */ True); |
| 676 | } |
| 677 | } |
| 678 | else if (signum == SIGTERM) { |
| 679 | shutting_down = True; |
| 680 | sigterm++; |
| 681 | } else if (signum == SIGHUP) { |
| 682 | shutting_down = True; |
| 683 | sighup++; |
| 684 | } else if (signum == SIGPIPE) { |
| 685 | sigpipe++; |
| 686 | } else if (signum == SIGALRM) { |
| 687 | sigalrm++; |
dejanj | 9c6b05d | 2013-12-27 09:06:55 +0000 | [diff] [blame] | 688 | #if defined(VGPV_arm_linux_android) || defined(VGPV_x86_linux_android) \ |
| 689 | || defined(VGPV_mips32_linux_android) |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 690 | /* Android has no pthread_cancel. As it also does not have |
philippe | 3c761f0 | 2013-12-01 14:56:28 +0000 | [diff] [blame] | 691 | an invoker implementation, there is no need for cleanup action. |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 692 | So, we just do nothing. */ |
| 693 | DEBUG(1, "sigalrm received, no action on android\n"); |
| 694 | #else |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 695 | /* Note: we cannot directly invoke restore_and_detach : this must |
| 696 | be done by the thread that has attached. |
| 697 | We have in this thread pushed a cleanup handler that will |
| 698 | cleanup what is needed. */ |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 699 | DEBUG(1, "pthread_cancel invoke_gdbserver_in_valgrind_thread\n"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 700 | pthread_cancel(invoke_gdbserver_in_valgrind_thread); |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 701 | #endif |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 702 | } else { |
| 703 | ERROR(0, "unexpected signal %d\n", signum); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | /* install the signal handlers allowing e.g. vgdb to cleanup in |
| 708 | case of termination. */ |
| 709 | static |
| 710 | void install_handlers(void) |
| 711 | { |
| 712 | struct sigaction action, oldaction; |
| 713 | |
| 714 | action.sa_handler = received_signal; |
| 715 | sigemptyset (&action.sa_mask); |
| 716 | action.sa_flags = 0; |
| 717 | |
| 718 | /* SIGINT: when user types C-c in gdb, this sends |
| 719 | a SIGINT to vgdb + causes a character to be sent to remote gdbserver. |
| 720 | The later is enough to wakeup the valgrind process. */ |
| 721 | if (sigaction (SIGINT, &action, &oldaction) != 0) |
| 722 | XERROR (errno, "vgdb error sigaction SIGINT\n"); |
| 723 | /* We might do something more intelligent than just |
| 724 | reporting this SIGINT E.g. behave similarly to the gdb: two |
| 725 | control-C without feedback from the debugged process would |
| 726 | mean to stop debugging it. */ |
| 727 | |
| 728 | /* SIGUSR1: this is used to facilitate automatic testing. When |
| 729 | vgdb receives this signal, it will simulate the user typing C-c. */ |
| 730 | if (sigaction (SIGUSR1, &action, &oldaction) != 0) |
| 731 | XERROR (errno, "vgdb error sigaction SIGUSR1\n"); |
| 732 | |
| 733 | |
| 734 | /* SIGTERM: can receive this signal (e.g. from gdb) to terminate vgdb |
| 735 | when detaching or similar. A clean shutdown will be done as both |
| 736 | the read and write side will detect an end of file. */ |
| 737 | if (sigaction (SIGTERM, &action, &oldaction) != 0) |
| 738 | XERROR (errno, "vgdb error sigaction SIGTERM\n"); |
| 739 | |
| 740 | /* SIGPIPE: can receive this signal when gdb detaches or kill the |
| 741 | process debugged: gdb will close its pipes to vgdb. vgdb |
| 742 | must resist to this signal to allow a clean shutdown. */ |
| 743 | if (sigaction (SIGPIPE, &action, &oldaction) != 0) |
| 744 | XERROR (errno, "vgdb error sigaction SIGPIPE\n"); |
| 745 | |
| 746 | /* SIGALRM: in case invoke thread is blocked, alarm is used |
| 747 | to cleanup. */ |
| 748 | if (sigaction (SIGALRM, &action, &oldaction) != 0) |
| 749 | XERROR (errno, "vgdb error sigaction SIGALRM\n"); |
| 750 | } |
| 751 | |
| 752 | /* close the FIFOs provided connections, terminate the invoker thread. */ |
| 753 | static |
| 754 | void close_connection(int to_pid, int from_pid) |
| 755 | { |
| 756 | DEBUG(1, "nr received signals: sigint %d sigterm %d sighup %d sigpipe %d\n", |
| 757 | sigint, sigterm, sighup, sigpipe); |
| 758 | /* Note that we do not forward sigterm to the valgrind process: |
| 759 | a sigterm signal is (probably) received from gdb if the user wants to |
| 760 | kill the debugged process. The kill instruction has been given to |
| 761 | the valgrind process, which should execute a clean exit. */ |
| 762 | |
| 763 | /* We first close the connection to pid. The pid will then |
| 764 | terminates its gdbserver work. We keep the from pid |
| 765 | fifo opened till the invoker thread is finished. |
| 766 | This allows the gdbserver to finish sending its last reply. */ |
| 767 | if (close(to_pid) != 0) |
| 768 | ERROR(errno, "close to_pid\n"); |
| 769 | |
| 770 | /* if there is a task that was busy trying to wake up valgrind |
| 771 | process, we wait for it to be terminated otherwise threads |
| 772 | in the valgrind process can stay stopped if vgdb main |
| 773 | exits before the invoke thread had time to detach from |
| 774 | all valgrind threads. */ |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 775 | if (max_invoke_ms > 0 || cmd_time_out != NEVER) { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 776 | int join; |
| 777 | |
| 778 | /* It is surprisingly complex to properly shutdown or exit the |
| 779 | valgrind process in which gdbserver has been invoked through |
| 780 | ptrace. In the normal case (gdb detaches from the process, |
| 781 | or process is continued), the valgrind process will reach the |
| 782 | breakpoint place. Using ptrace, vgdb will ensure the |
| 783 | previous activity of the process is resumed (e.g. restart a |
| 784 | blocking system call). The special case is when gdb asks the |
| 785 | valgrind process to exit (using either the "kill" command or |
| 786 | "monitor exit"). In such a case, the valgrind process will |
| 787 | call exit. But a ptraced process will be blocked in exit, |
| 788 | waiting for the ptracing process to detach or die. vgdb |
| 789 | cannot detach unconditionally as otherwise, in the normal |
sewardj | b2572b5 | 2011-06-26 09:36:38 +0000 | [diff] [blame] | 790 | case, the valgrind process would stop abnormally with SIGSTOP |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 791 | (as vgdb would not be there to catch it). vgdb can also not |
| 792 | die unconditionally otherwise again, similar problem. So, we |
| 793 | assume that most of the time, we arrive here in the normal |
| 794 | case, and so, the breakpoint has been encountered by the |
| 795 | valgrind process, so the invoker thread will exit and the |
| 796 | join will succeed. For the "kill" case, we cause an alarm |
| 797 | signal to be sent after a few seconds. This means that in the |
| 798 | normal case, the gdbserver code in valgrind process must have |
| 799 | returned the control in less than the alarm nr of seconds, |
sewardj | b2572b5 | 2011-06-26 09:36:38 +0000 | [diff] [blame] | 800 | otherwise, valgrind will stop abnormally with SIGSTOP. */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 801 | (void) alarm (3); |
| 802 | |
| 803 | DEBUG(1, "joining with invoke_gdbserver_in_valgrind_thread\n"); |
| 804 | join = pthread_join(invoke_gdbserver_in_valgrind_thread, NULL); |
| 805 | if (join != 0) |
| 806 | XERROR |
| 807 | (join, |
| 808 | "vgdb error pthread_join invoke_gdbserver_in_valgrind_thread\n"); |
| 809 | } |
| 810 | if (close(from_pid) != 0) |
| 811 | ERROR(errno, "close from_pid\n"); |
| 812 | } |
| 813 | |
| 814 | /* Relay data between gdb and Valgrind gdbserver, till EOF or an |
| 815 | error is encountered. */ |
| 816 | static |
| 817 | void gdb_relay (int pid) |
| 818 | { |
| 819 | int from_pid = -1; /* fd to read from pid */ |
| 820 | int to_pid = -1; /* fd to write to pid */ |
| 821 | |
| 822 | int shutdown_loop = 0; |
| 823 | fprintf (stderr, "relaying data between gdb and process %d\n", pid); |
| 824 | fflush (stderr); |
| 825 | |
| 826 | if (max_invoke_ms > 0) |
| 827 | pthread_create(&invoke_gdbserver_in_valgrind_thread, NULL, |
| 828 | invoke_gdbserver_in_valgrind, (void *) &pid); |
| 829 | to_pid = open_fifo(from_gdb_to_pid, O_WRONLY, "write to pid"); |
| 830 | acquire_lock (shared_mem_fd, pid); |
| 831 | |
| 832 | from_pid = open_fifo (to_gdb_from_pid, O_RDONLY|O_NONBLOCK, |
| 833 | "read mode from pid"); |
| 834 | |
| 835 | sigusr1_fd = to_pid; /* allow simulating user typing control-c */ |
| 836 | |
| 837 | while (1) { |
| 838 | ConnectionKind ck; |
| 839 | int ret; |
| 840 | struct pollfd pollfds[NumConnectionKind]; |
| 841 | |
| 842 | /* watch data written by gdb, watch POLLERR on both gdb fd */ |
| 843 | pollfds[FROM_GDB].fd = from_gdb; |
| 844 | pollfds[FROM_GDB].events = POLLIN; |
| 845 | pollfds[FROM_GDB].revents = 0; |
| 846 | pollfds[TO_GDB].fd = to_gdb; |
| 847 | pollfds[TO_GDB].events = 0; |
| 848 | pollfds[TO_GDB].revents = 0; |
| 849 | |
| 850 | /* watch data written by pid, watch POLLERR on both pid fd */ |
| 851 | pollfds[FROM_PID].fd = from_pid; |
| 852 | pollfds[FROM_PID].events = POLLIN; |
| 853 | pollfds[FROM_PID].revents = 0; |
| 854 | pollfds[TO_PID].fd = to_pid; |
| 855 | pollfds[TO_PID].events = 0; |
| 856 | pollfds[TO_PID].revents = 0; |
| 857 | |
| 858 | ret = poll(pollfds, |
| 859 | NumConnectionKind, |
| 860 | (shutting_down ? |
| 861 | 1 /* one second */ |
| 862 | : -1 /* infinite */)); |
| 863 | DEBUG(2, "poll ret %d errno %d\n", ret, errno); |
| 864 | |
| 865 | /* check for unexpected error */ |
| 866 | if (ret <= 0 && errno != EINTR) { |
| 867 | ERROR (errno, "unexpected poll ret %d\n", ret); |
| 868 | shutting_down = True; |
| 869 | break; |
| 870 | } |
| 871 | |
| 872 | /* check for data to read */ |
| 873 | for (ck = 0; ck < NumConnectionKind; ck ++) { |
| 874 | if (pollfds[ck].revents & POLLIN) { |
| 875 | switch (ck) { |
| 876 | case FROM_GDB: |
| 877 | if (!read_from_gdb_write_to_pid(to_pid)) |
| 878 | shutting_down = True; |
| 879 | break; |
philippe | 1d76a80 | 2011-12-26 21:21:37 +0000 | [diff] [blame] | 880 | case FROM_PID: |
| 881 | if (!read_from_pid_write_to_gdb(from_pid)) |
| 882 | shutting_down = True; |
| 883 | break; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 884 | default: XERROR(0, "unexpected POLLIN on %s\n", |
| 885 | ppConnectionKind(ck)); |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | /* check for an fd being in error condition */ |
| 891 | for (ck = 0; ck < NumConnectionKind; ck ++) { |
| 892 | if (pollfds[ck].revents & POLLERR) { |
| 893 | DEBUG(1, "connection %s fd %d POLLERR error condition\n", |
| 894 | ppConnectionKind(ck), pollfds[ck].fd); |
philippe | 3c761f0 | 2013-12-01 14:56:28 +0000 | [diff] [blame] | 895 | invoker_valgrind_dying(); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 896 | shutting_down = True; |
| 897 | } |
| 898 | if (pollfds[ck].revents & POLLHUP) { |
| 899 | DEBUG(1, "connection %s fd %d POLLHUP error condition\n", |
| 900 | ppConnectionKind(ck), pollfds[ck].fd); |
philippe | 3c761f0 | 2013-12-01 14:56:28 +0000 | [diff] [blame] | 901 | invoker_valgrind_dying(); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 902 | shutting_down = True; |
| 903 | } |
| 904 | if (pollfds[ck].revents & POLLNVAL) { |
| 905 | DEBUG(1, "connection %s fd %d POLLNVAL error condition\n", |
| 906 | ppConnectionKind(ck), pollfds[ck].fd); |
philippe | 3c761f0 | 2013-12-01 14:56:28 +0000 | [diff] [blame] | 907 | invoker_valgrind_dying(); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 908 | shutting_down = True; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | if (shutting_down) { |
| 913 | /* we let some time to the final packets to be transferred */ |
| 914 | shutdown_loop++; |
| 915 | if (shutdown_loop > 3) |
| 916 | break; |
| 917 | } |
| 918 | } |
| 919 | close_connection(to_pid, from_pid); |
| 920 | } |
| 921 | |
| 922 | static int packet_len_for_command(char *cmd) |
| 923 | { |
| 924 | /* cmd will be send as a packet $qRcmd,xxxx....................xx#cc */ |
| 925 | return 7+ 2*strlen(cmd) +3 + 1; |
| 926 | } |
| 927 | |
| 928 | /* hyper-minimal protocol implementation that |
| 929 | sends the provided commands (using qRcmd packets) |
| 930 | and read and display their replies. */ |
| 931 | static |
| 932 | void standalone_send_commands(int pid, |
| 933 | int last_command, |
| 934 | char *commands[] ) |
| 935 | { |
| 936 | int from_pid = -1; /* fd to read from pid */ |
| 937 | int to_pid = -1; /* fd to write to pid */ |
| 938 | |
| 939 | int i; |
| 940 | int hi; |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 941 | char hex[3]; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 942 | unsigned char cksum; |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 943 | char *hexcommand; |
| 944 | char buf[PBUFSIZ+1]; // +1 for trailing \0 |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 945 | int buflen; |
| 946 | int nc; |
| 947 | |
| 948 | |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 949 | if (max_invoke_ms > 0 || cmd_time_out != NEVER) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 950 | pthread_create(&invoke_gdbserver_in_valgrind_thread, NULL, |
| 951 | invoke_gdbserver_in_valgrind, (void *) &pid); |
| 952 | |
| 953 | to_pid = open_fifo(from_gdb_to_pid, O_WRONLY, "write to pid"); |
| 954 | acquire_lock (shared_mem_fd, pid); |
| 955 | |
| 956 | /* first send a C-c \003 to pid, so that it wakes up the process |
| 957 | After that, we can open the fifo from the pid in read mode |
| 958 | We then start to wait for packets (normally first a resume reply) |
| 959 | At that point, we send our command and expect replies */ |
| 960 | buf[0] = '\003'; |
philippe | def5aae | 2014-07-08 22:28:26 +0000 | [diff] [blame^] | 961 | i = 0; |
| 962 | while (!write_buf(to_pid, buf, 1, |
| 963 | "write \\003 to wake up", /* notify */ True)) { |
| 964 | /* If write fails, retries up to 10 times every 0.5 seconds |
| 965 | This aims at solving the race condition described in |
| 966 | remote-utils.c remote_finish function. */ |
| 967 | usleep(500*1000); |
| 968 | i++; |
| 969 | if (i >= 10) |
| 970 | XERROR (errno, "failed to send wake up char after 10 trials\n"); |
| 971 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 972 | from_pid = open_fifo(to_gdb_from_pid, O_RDONLY, |
| 973 | "read cmd result from pid"); |
| 974 | |
| 975 | for (nc = 0; nc <= last_command; nc++) { |
| 976 | fprintf (stderr, "sending command %s to pid %d\n", commands[nc], pid); |
| 977 | fflush (stderr); |
| 978 | |
| 979 | /* prepare hexcommand $qRcmd,xxxx....................xx#cc */ |
| 980 | hexcommand = vmalloc (packet_len_for_command(commands[nc])); |
| 981 | hexcommand[0] = 0; |
| 982 | strcat (hexcommand, "$qRcmd,"); |
| 983 | for (i = 0; i < strlen(commands[nc]); i++) { |
philippe | adfb8a7 | 2012-11-01 17:00:12 +0000 | [diff] [blame] | 984 | sprintf(hex, "%02x", (unsigned char) commands[nc][i]); |
| 985 | // Need to use unsigned char, to avoid sign extension. |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 986 | strcat (hexcommand, hex); |
| 987 | } |
| 988 | /* checksum (but without the $) */ |
| 989 | cksum = 0; |
| 990 | for (hi = 1; hi < strlen(hexcommand); hi++) |
| 991 | cksum+=hexcommand[hi]; |
| 992 | strcat(hexcommand, "#"); |
| 993 | sprintf(hex, "%02x", cksum); |
| 994 | strcat(hexcommand, hex); |
| 995 | write_buf(to_pid, hexcommand, strlen(hexcommand), |
| 996 | "writing hex command to pid", /* notify */ True); |
| 997 | |
| 998 | /* we exit of the below loop explicitely when the command has |
| 999 | been handled or because a signal handler will set |
| 1000 | shutting_down. */ |
| 1001 | while (!shutting_down) { |
| 1002 | buflen = getpkt(buf, from_pid, to_pid); |
| 1003 | if (buflen < 0) { |
| 1004 | ERROR (0, "error reading packet\n"); |
| 1005 | if (buflen == -2) |
philippe | 3c761f0 | 2013-12-01 14:56:28 +0000 | [diff] [blame] | 1006 | invoker_valgrind_dying(); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1007 | break; |
| 1008 | } |
| 1009 | if (strlen(buf) == 0) { |
| 1010 | DEBUG(0, "empty packet rcvd (packet qRcmd not recognised?)\n"); |
| 1011 | break; |
| 1012 | } |
| 1013 | if (strcmp(buf, "OK") == 0) { |
| 1014 | DEBUG(1, "OK packet rcvd\n"); |
| 1015 | break; |
| 1016 | } |
| 1017 | if (buf[0] == 'E') { |
| 1018 | DEBUG(0, |
| 1019 | "E NN error packet rcvd: %s (unknown monitor command?)\n", |
| 1020 | buf); |
| 1021 | break; |
| 1022 | } |
| 1023 | if (buf[0] == 'W') { |
| 1024 | DEBUG(0, "W stopped packet rcvd: %s\n", buf); |
| 1025 | break; |
| 1026 | } |
| 1027 | if (buf[0] == 'T') { |
| 1028 | DEBUG(1, "T resume reply packet received: %s\n", buf); |
| 1029 | continue; |
| 1030 | } |
| 1031 | |
| 1032 | /* must be here an O packet with hex encoded string reply |
| 1033 | => decode and print it */ |
| 1034 | if (buf[0] != 'O') { |
| 1035 | DEBUG(0, "expecting O packet, received: %s\n", buf); |
| 1036 | continue; |
| 1037 | } |
| 1038 | { |
| 1039 | char buf_print[buflen/2 + 1]; |
| 1040 | for (i = 1; i < buflen; i = i + 2) |
| 1041 | buf_print[i/2] = (fromhex(*(buf+i)) << 4) |
| 1042 | + fromhex(*(buf+i+1)); |
| 1043 | buf_print[buflen/2] = 0; |
| 1044 | printf("%s", buf_print); |
| 1045 | fflush(stdout); |
| 1046 | } |
| 1047 | } |
| 1048 | free (hexcommand); |
| 1049 | } |
| 1050 | shutting_down = True; |
| 1051 | |
| 1052 | close_connection(to_pid, from_pid); |
| 1053 | } |
| 1054 | |
| 1055 | /* report to user the existence of a vgdb-able valgrind process |
| 1056 | with given pid */ |
| 1057 | static |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1058 | void report_pid (int pid, Bool on_stdout) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1059 | { |
| 1060 | char cmdline_file[100]; |
| 1061 | char cmdline[1000]; |
| 1062 | int fd; |
| 1063 | int i, sz; |
| 1064 | |
| 1065 | sprintf(cmdline_file, "/proc/%d/cmdline", pid); |
| 1066 | fd = open (cmdline_file, O_RDONLY); |
| 1067 | if (fd == -1) { |
| 1068 | DEBUG(1, "error opening cmdline file %s %s\n", |
| 1069 | cmdline_file, strerror(errno)); |
philippe | 36c01ef | 2013-01-15 23:09:41 +0000 | [diff] [blame] | 1070 | sprintf(cmdline, "(could not open process command line)"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1071 | } else { |
| 1072 | sz = read(fd, cmdline, 1000); |
| 1073 | for (i = 0; i < sz; i++) |
| 1074 | if (cmdline[i] == 0) |
| 1075 | cmdline[i] = ' '; |
philippe | 36c01ef | 2013-01-15 23:09:41 +0000 | [diff] [blame] | 1076 | if (sz >= 0) |
| 1077 | cmdline[sz] = 0; |
| 1078 | else { |
| 1079 | DEBUG(1, "error reading cmdline file %s %s\n", |
| 1080 | cmdline_file, strerror(errno)); |
| 1081 | sprintf(cmdline, "(could not read process command line)"); |
| 1082 | } |
sewardj | d8c12f1 | 2011-05-11 22:54:57 +0000 | [diff] [blame] | 1083 | close (fd); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1084 | } |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1085 | fprintf((on_stdout ? stdout : stderr), "use --pid=%d for %s\n", pid, cmdline); |
| 1086 | fflush((on_stdout ? stdout : stderr)); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1089 | static |
| 1090 | void usage(void) |
| 1091 | { |
| 1092 | fprintf(stderr, |
| 1093 | "Usage: vgdb [OPTION]... [[-c] COMMAND]...\n" |
| 1094 | "vgdb (valgrind gdb) has two usages\n" |
| 1095 | " 1. standalone to send monitor commands to a Valgrind gdbserver.\n" |
| 1096 | " The OPTION(s) must be followed by the command to send\n" |
| 1097 | " To send more than one command, separate the commands with -c\n" |
| 1098 | " 2. relay application between gdb and a Valgrind gdbserver.\n" |
| 1099 | " Only OPTION(s) can be given.\n" |
| 1100 | "\n" |
| 1101 | " OPTIONS are [--pid=<number>] [--vgdb-prefix=<prefix>]\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1102 | " [--wait=<number>] [--max-invoke-ms=<number>]\n" |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1103 | " [--port=<portnr>\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1104 | " [--cmd-time-out=<number>] [-l] [-D] [-d]\n" |
| 1105 | " \n" |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1106 | " --pid arg must be given if multiple Valgrind gdbservers are found.\n" |
| 1107 | " --vgdb-prefix arg must be given to both Valgrind and vgdb utility\n" |
philippe | 5507d22 | 2013-08-06 20:45:20 +0000 | [diff] [blame] | 1108 | " if you want to change the prefix (default %s) for the FIFOs communication\n" |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1109 | " between the Valgrind gdbserver and vgdb.\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1110 | " --wait (default 0) tells vgdb to check during the specified number\n" |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1111 | " of seconds if a Valgrind gdbserver can be found.\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1112 | " --max-invoke-ms (default 100) gives the nr of milli-seconds after which vgdb\n" |
| 1113 | " will force the invocation of the Valgrind gdbserver (if the Valgrind\n" |
| 1114 | " process is blocked in a system call).\n" |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1115 | " --port instructs vgdb to listen for gdb on the specified port nr.\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1116 | " --cmd-time-out (default 99999999) tells vgdb to exit if the found Valgrind\n" |
| 1117 | " gdbserver has not processed a command after number seconds\n" |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1118 | " -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] | 1119 | " -D arg tells to show shared mem status and then exit.\n" |
| 1120 | " -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] | 1121 | "\n" |
| 1122 | " -h --help shows this message\n" |
| 1123 | " To get help from the Valgrind gdbserver, use vgdb help\n" |
philippe | 5507d22 | 2013-08-06 20:45:20 +0000 | [diff] [blame] | 1124 | "\n", vgdb_prefix_default() |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1125 | ); |
philippe | 3c761f0 | 2013-12-01 14:56:28 +0000 | [diff] [blame] | 1126 | invoker_restrictions_msg(); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1129 | /* 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] | 1130 | and then exits. |
| 1131 | |
| 1132 | else if arg_pid == -1, waits maximum check_trials seconds to discover |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1133 | a valgrind pid appearing. |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1134 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1135 | Otherwise verify arg_pid is valid and corresponds to a Valgrind process |
| 1136 | with gdbserver activated. |
| 1137 | |
| 1138 | Returns the pid to work with |
| 1139 | or exits in case of error (e.g. no pid found corresponding to arg_pid */ |
| 1140 | |
| 1141 | static |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1142 | int search_arg_pid(int arg_pid, int check_trials, Bool show_list) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1143 | { |
| 1144 | int i; |
| 1145 | int pid = -1; |
| 1146 | |
| 1147 | if (arg_pid == 0 || arg_pid < -1) { |
| 1148 | fprintf (stderr, "vgdb error: invalid pid %d given\n", arg_pid); |
| 1149 | exit (1); |
| 1150 | } else { |
| 1151 | /* search for a matching named fifo. |
| 1152 | If we have been given a pid, we will check that the matching FIFO is |
| 1153 | there (or wait the nr of check_trials for this to appear). |
| 1154 | If no pid has been given, then if we find only one FIFO, |
| 1155 | we will use this to build the pid to use. |
| 1156 | If we find multiple processes with valid FIFO, we report them and will |
| 1157 | exit with an error. */ |
| 1158 | DIR *vgdb_dir; |
| 1159 | char *vgdb_dir_name = vmalloc (strlen (vgdb_prefix) + 3); |
| 1160 | struct dirent *f; |
| 1161 | int is; |
| 1162 | int nr_valid_pid = 0; |
| 1163 | const char *suffix = "-from-vgdb-to-"; /* followed by pid */ |
| 1164 | char *vgdb_format = vmalloc (strlen(vgdb_prefix) + strlen(suffix) + 1); |
| 1165 | |
| 1166 | strcpy (vgdb_format, vgdb_prefix); |
| 1167 | strcat (vgdb_format, suffix); |
| 1168 | |
philippe | 5507d22 | 2013-08-06 20:45:20 +0000 | [diff] [blame] | 1169 | if (strchr(vgdb_prefix, '/') != NULL) { |
| 1170 | strcpy (vgdb_dir_name, vgdb_prefix); |
| 1171 | for (is = strlen(vgdb_prefix) - 1; is >= 0; is--) |
| 1172 | if (vgdb_dir_name[is] == '/') { |
| 1173 | vgdb_dir_name[is+1] = '\0'; |
| 1174 | break; |
| 1175 | } |
| 1176 | } else { |
| 1177 | strcpy (vgdb_dir_name, ""); |
| 1178 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1179 | |
| 1180 | DEBUG(1, "searching pid in directory %s format %s\n", |
| 1181 | vgdb_dir_name, vgdb_format); |
| 1182 | |
| 1183 | /* try to find FIFOs with valid pid. |
| 1184 | On exit of the loop, pid is set to: |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1185 | 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] | 1186 | -1 if no FIFOs matching a running process is found |
| 1187 | -2 if multiple FIFOs of running processes are found |
| 1188 | otherwise it is set to the (only) pid found that can be debugged |
| 1189 | */ |
| 1190 | for (i = 0; i < check_trials; i++) { |
| 1191 | DEBUG(1, "check_trial %d \n", i); |
| 1192 | if (i > 0) |
| 1193 | /* wait one second before checking again */ |
| 1194 | sleep(1); |
| 1195 | |
philippe | 5507d22 | 2013-08-06 20:45:20 +0000 | [diff] [blame] | 1196 | vgdb_dir = opendir (strlen (vgdb_dir_name) ? vgdb_dir_name : "./"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1197 | if (vgdb_dir == NULL) |
| 1198 | XERROR (errno, |
| 1199 | "vgdb error: opening directory %s searching vgdb fifo\n", |
| 1200 | vgdb_dir_name); |
| 1201 | |
| 1202 | errno = 0; /* avoid complain if vgdb_dir is empty */ |
| 1203 | while ((f = readdir (vgdb_dir))) { |
| 1204 | struct stat st; |
philippe | bb4827b | 2012-04-06 11:51:04 +0000 | [diff] [blame] | 1205 | char pathname[strlen(vgdb_dir_name) + strlen(f->d_name) + 1]; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1206 | char *wrongpid; |
| 1207 | int newpid; |
| 1208 | |
| 1209 | strcpy (pathname, vgdb_dir_name); |
| 1210 | strcat (pathname, f->d_name); |
philippe | bb4827b | 2012-04-06 11:51:04 +0000 | [diff] [blame] | 1211 | DEBUG(3, "checking pathname is FIFO %s\n", pathname); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1212 | if (stat (pathname, &st) != 0) { |
| 1213 | if (debuglevel >= 3) |
| 1214 | ERROR (errno, "vgdb error: stat %s searching vgdb fifo\n", |
| 1215 | pathname); |
| 1216 | } else if (S_ISFIFO (st.st_mode)) { |
philippe | bb4827b | 2012-04-06 11:51:04 +0000 | [diff] [blame] | 1217 | DEBUG(3, "trying FIFO %s\n", pathname); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1218 | if (strncmp (pathname, vgdb_format, |
| 1219 | strlen (vgdb_format)) == 0) { |
| 1220 | newpid = strtol(pathname + strlen (vgdb_format), |
| 1221 | &wrongpid, 10); |
florian | ab8630f | 2011-09-29 21:20:49 +0000 | [diff] [blame] | 1222 | if (*wrongpid == '-' && newpid > 0 |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1223 | && kill (newpid, 0) == 0) { |
| 1224 | nr_valid_pid++; |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1225 | if (show_list) { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1226 | report_pid (newpid, /*on_stdout*/ True); |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1227 | pid = newpid; |
| 1228 | } else if (arg_pid != -1) { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1229 | if (arg_pid == newpid) { |
| 1230 | pid = newpid; |
| 1231 | } |
| 1232 | } else if (nr_valid_pid > 1) { |
| 1233 | if (nr_valid_pid == 2) { |
| 1234 | fprintf |
| 1235 | (stderr, |
| 1236 | "no --pid= arg given" |
| 1237 | " and multiple valgrind pids found:\n"); |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1238 | report_pid (pid, /*on_stdout*/ False); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1239 | } |
| 1240 | pid = -2; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1241 | report_pid (newpid, /*on_stdout*/ False); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1242 | } else { |
| 1243 | pid = newpid; |
| 1244 | } |
| 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | errno = 0; /* avoid complain if at the end of vgdb_dir */ |
| 1249 | } |
| 1250 | if (f == NULL && errno != 0) |
| 1251 | XERROR (errno, "vgdb error: reading directory %s for vgdb fifo\n", |
| 1252 | vgdb_dir_name); |
| 1253 | |
| 1254 | closedir (vgdb_dir); |
| 1255 | if (pid != -1) |
| 1256 | break; |
| 1257 | } |
| 1258 | |
| 1259 | free (vgdb_dir_name); |
| 1260 | free (vgdb_format); |
| 1261 | } |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1262 | |
| 1263 | if (show_list) { |
| 1264 | exit (1); |
| 1265 | } else if (pid == -1) { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1266 | if (arg_pid == -1) |
| 1267 | fprintf (stderr, "vgdb error: no FIFO found and no pid given\n"); |
| 1268 | else |
| 1269 | fprintf (stderr, "vgdb error: no FIFO found matching pid %d\n", |
| 1270 | arg_pid); |
| 1271 | exit (1); |
| 1272 | } |
| 1273 | else if (pid == -2) { |
| 1274 | /* no arg_pid given, multiple FIFOs found */ |
| 1275 | exit (1); |
| 1276 | } |
| 1277 | else { |
| 1278 | return pid; |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | /* return true if the numeric value of an option of the |
| 1283 | form --xxxxxxxxx=<number> could properly be extracted |
| 1284 | from arg. If True is returned, *value contains the |
| 1285 | extracted value.*/ |
| 1286 | static |
| 1287 | Bool numeric_val(char* arg, int *value) |
| 1288 | { |
| 1289 | const char *eq_pos = strchr(arg, '='); |
| 1290 | char *wrong; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1291 | long long int long_value; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1292 | |
| 1293 | if (eq_pos == NULL) |
| 1294 | return False; |
| 1295 | |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1296 | long_value = strtoll(eq_pos+1, &wrong, 10); |
| 1297 | if (long_value < 0 || long_value > INT_MAX) |
| 1298 | return False; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1299 | if (*wrong) |
| 1300 | return False; |
| 1301 | |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1302 | *value = (int) long_value; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1303 | return True; |
| 1304 | } |
| 1305 | |
| 1306 | /* true if arg matches the provided option */ |
| 1307 | static |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 1308 | Bool is_opt(char* arg, const char *option) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1309 | { |
| 1310 | int option_len = strlen(option); |
| 1311 | if (option[option_len-1] == '=') |
| 1312 | return (0 == strncmp(option, arg, option_len)); |
| 1313 | else |
| 1314 | return (0 == strcmp(option, arg)); |
| 1315 | } |
| 1316 | |
| 1317 | /* Parse command lines options. If error(s), exits. |
| 1318 | Otherwise returns the options in *p_... args. |
| 1319 | commands must be big enough for the commands extracted from argv. |
| 1320 | On return, *p_last_command gives the position in commands where |
| 1321 | the last command has been allocated (using vmalloc). */ |
| 1322 | static |
| 1323 | void parse_options(int argc, char** argv, |
| 1324 | Bool *p_show_shared_mem, |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1325 | Bool *p_show_list, |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1326 | int *p_arg_pid, |
| 1327 | int *p_check_trials, |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1328 | int *p_port, |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1329 | int *p_last_command, |
| 1330 | char *commands[]) |
| 1331 | { |
| 1332 | Bool show_shared_mem = False; |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1333 | Bool show_list = False; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1334 | int arg_pid = -1; |
| 1335 | int check_trials = 1; |
| 1336 | int last_command = -1; |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1337 | int int_port = 0; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1338 | |
| 1339 | int i; |
| 1340 | int arg_errors = 0; |
| 1341 | |
| 1342 | for (i = 1; i < argc; i++) { |
| 1343 | if (is_opt(argv[i], "--help") || is_opt(argv[i], "-h")) { |
| 1344 | usage(); |
| 1345 | exit(0); |
| 1346 | } else if (is_opt(argv[i], "-d")) { |
| 1347 | debuglevel++; |
| 1348 | } else if (is_opt(argv[i], "-D")) { |
| 1349 | show_shared_mem = True; |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1350 | } else if (is_opt(argv[i], "-l")) { |
| 1351 | show_list = True; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1352 | } else if (is_opt(argv[i], "--pid=")) { |
| 1353 | int newpid; |
| 1354 | if (!numeric_val(argv[i], &newpid)) { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1355 | fprintf (stderr, "invalid --pid argument %s\n", argv[i]); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1356 | arg_errors++; |
| 1357 | } else if (arg_pid != -1) { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1358 | fprintf (stderr, "multiple --pid arguments given\n"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1359 | arg_errors++; |
| 1360 | } else { |
| 1361 | arg_pid = newpid; |
| 1362 | } |
| 1363 | } else if (is_opt(argv[i], "--wait=")) { |
| 1364 | if (!numeric_val(argv[i], &check_trials)) { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1365 | fprintf (stderr, "invalid --wait argument %s\n", argv[i]); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1366 | arg_errors++; |
| 1367 | } |
| 1368 | } else if (is_opt(argv[i], "--max-invoke-ms=")) { |
| 1369 | if (!numeric_val(argv[i], &max_invoke_ms)) { |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1370 | fprintf (stderr, "invalid --max-invoke-ms argument %s\n", argv[i]); |
| 1371 | arg_errors++; |
| 1372 | } |
| 1373 | } else if (is_opt(argv[i], "--cmd-time-out=")) { |
| 1374 | if (!numeric_val(argv[i], &cmd_time_out)) { |
| 1375 | fprintf (stderr, "invalid --cmd-time-out argument %s\n", argv[i]); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1376 | arg_errors++; |
| 1377 | } |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1378 | } else if (is_opt(argv[i], "--port=")) { |
| 1379 | if (!numeric_val(argv[i], &int_port)) { |
| 1380 | fprintf (stderr, "invalid --port argument %s\n", argv[i]); |
| 1381 | arg_errors++; |
| 1382 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1383 | } else if (is_opt(argv[i], "--vgdb-prefix=")) { |
| 1384 | vgdb_prefix = argv[i] + 14; |
| 1385 | } else if (is_opt(argv[i], "-c")) { |
| 1386 | last_command++; |
| 1387 | commands[last_command] = vmalloc (1); |
| 1388 | commands[last_command][0] = '\0'; |
| 1389 | } else if (0 == strncmp(argv[i], "-", 1)) { |
| 1390 | fprintf (stderr, "unknown or invalid argument %s\n", argv[i]); |
| 1391 | arg_errors++; |
| 1392 | } else { |
| 1393 | int len; |
| 1394 | if (last_command == -1) { |
| 1395 | /* only one command, no -c command indicator */ |
| 1396 | last_command++; |
| 1397 | commands[last_command] = vmalloc (1); |
| 1398 | commands[last_command][0] = '\0'; |
| 1399 | } |
| 1400 | len = strlen(commands[last_command]); |
| 1401 | commands[last_command] = vrealloc (commands[last_command], |
| 1402 | len + 1 + strlen(argv[i]) + 1); |
| 1403 | if (len > 0) |
| 1404 | strcat (commands[last_command], " "); |
| 1405 | strcat (commands[last_command], argv[i]); |
| 1406 | if (packet_len_for_command(commands[last_command]) > PBUFSIZ) { |
| 1407 | fprintf (stderr, "command %s too long\n", commands[last_command]); |
| 1408 | arg_errors++; |
| 1409 | } |
| 1410 | |
| 1411 | } |
| 1412 | } |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1413 | |
florian | b985e2d | 2011-09-29 03:03:45 +0000 | [diff] [blame] | 1414 | if (vgdb_prefix == NULL) |
| 1415 | vgdb_prefix = vgdb_prefix_default(); |
| 1416 | |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1417 | if (isatty(0) |
| 1418 | && !show_shared_mem |
| 1419 | && !show_list |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1420 | && int_port == 0 |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1421 | && last_command == -1) { |
| 1422 | arg_errors++; |
| 1423 | fprintf (stderr, |
| 1424 | "Using vgdb standalone implies to give -D or -l or a COMMAND\n"); |
| 1425 | } |
| 1426 | |
| 1427 | if (show_shared_mem && show_list) { |
| 1428 | arg_errors++; |
| 1429 | fprintf (stderr, |
| 1430 | "Can't use both -D and -l options\n"); |
| 1431 | } |
| 1432 | |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1433 | if (max_invoke_ms > 0 |
| 1434 | && cmd_time_out != NEVER |
| 1435 | && (cmd_time_out * 1000) <= max_invoke_ms) { |
| 1436 | arg_errors++; |
| 1437 | fprintf (stderr, |
| 1438 | "--max-invoke-ms must be < --cmd-time-out * 1000\n"); |
| 1439 | } |
| 1440 | |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1441 | if (show_list && arg_pid != -1) { |
| 1442 | arg_errors++; |
| 1443 | fprintf (stderr, |
| 1444 | "Can't use both --pid and -l options\n"); |
| 1445 | } |
| 1446 | |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1447 | if (int_port > 0 && last_command != -1) { |
| 1448 | arg_errors++; |
| 1449 | fprintf (stderr, |
| 1450 | "Can't use --port to send commands\n"); |
| 1451 | } |
| 1452 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1453 | if (arg_errors > 0) { |
| 1454 | fprintf (stderr, "args error. Try `vgdb --help` for more information\n"); |
| 1455 | exit(1); |
| 1456 | } |
| 1457 | |
| 1458 | *p_show_shared_mem = show_shared_mem; |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1459 | *p_show_list = show_list; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1460 | *p_arg_pid = arg_pid; |
| 1461 | *p_check_trials = check_trials; |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1462 | *p_port = int_port; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1463 | *p_last_command = last_command; |
| 1464 | } |
| 1465 | |
| 1466 | int main(int argc, char** argv) |
| 1467 | { |
| 1468 | int i; |
| 1469 | int pid; |
| 1470 | |
| 1471 | Bool show_shared_mem; |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1472 | Bool show_list; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1473 | int arg_pid; |
| 1474 | int check_trials; |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1475 | int in_port; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1476 | int last_command; |
| 1477 | char *commands[argc]; // we will never have more commands than args. |
| 1478 | |
| 1479 | parse_options(argc, argv, |
| 1480 | &show_shared_mem, |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1481 | &show_list, |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1482 | &arg_pid, |
| 1483 | &check_trials, |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1484 | &in_port, |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1485 | &last_command, |
| 1486 | commands); |
| 1487 | |
| 1488 | /* when we are working as a relay for gdb, handle some signals by |
| 1489 | only reporting them (according to debug level). Also handle these |
| 1490 | when ptrace will be used: vgdb must clean up the ptrace effect before |
| 1491 | dying. */ |
| 1492 | if (max_invoke_ms > 0 || last_command == -1) |
| 1493 | install_handlers(); |
| 1494 | |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1495 | pid = search_arg_pid (arg_pid, check_trials, show_list); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1496 | |
| 1497 | prepare_fifos_and_shared_mem(pid); |
| 1498 | |
sewardj | 992d3cc | 2011-10-22 20:38:08 +0000 | [diff] [blame] | 1499 | if (in_port > 0) |
| 1500 | wait_for_gdb_connect(in_port); |
| 1501 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1502 | if (show_shared_mem) { |
| 1503 | fprintf(stderr, |
| 1504 | "vgdb %d " |
| 1505 | "written_by_vgdb %d " |
| 1506 | "seen_by_valgrind %d\n" |
| 1507 | "vgdb pid %d\n", |
| 1508 | VS_vgdb_pid, |
| 1509 | VS_written_by_vgdb, |
| 1510 | VS_seen_by_valgrind, |
| 1511 | VS_vgdb_pid); |
| 1512 | exit (0); |
| 1513 | } |
| 1514 | |
| 1515 | if (last_command >= 0) { |
| 1516 | standalone_send_commands(pid, last_command, commands); |
| 1517 | } else { |
| 1518 | gdb_relay(pid); |
| 1519 | } |
| 1520 | |
| 1521 | |
| 1522 | free (from_gdb_to_pid); |
| 1523 | free (to_gdb_from_pid); |
| 1524 | free (shared_mem); |
| 1525 | |
| 1526 | for (i = 0; i <= last_command; i++) |
| 1527 | free (commands[i]); |
| 1528 | return 0; |
| 1529 | } |