1 /* Main code for remote server for GDB. 2 Copyright 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003, 2004 3 Free Software Foundation, Inc. 4 5 This file is part of GDB. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 2 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; if not, write to the Free Software 19 Foundation, Inc., 59 Temple Place - Suite 330, 20 Boston, MA 02111-1307, USA. */ 21 22 #include "server.h" 23 24 #include <unistd.h> 25 #include <signal.h> 26 #include <sys/wait.h> 27 28 int cont_thread; 29 int general_thread; 30 int step_thread; 31 int thread_from_wait; 32 int old_thread_from_wait; 33 int extended_protocol; 34 int server_waiting; 35 36 jmp_buf toplevel; 37 38 /* The PID of the originally created or attached inferior. Used to 39 send signals to the process when GDB sends us an asynchronous interrupt 40 (user hitting Control-C in the client), and to wait for the child to exit 41 when no longer debugging it. */ 42 43 int signal_pid; 44 45 static unsigned char 46 start_inferior (char *argv[], char *statusptr) 47 { 48 signal (SIGTTOU, SIG_DFL); 49 signal (SIGTTIN, SIG_DFL); 50 51 signal_pid = create_inferior (argv[0], argv); 52 53 fprintf (stderr, "Process %s created; pid = %d\n", argv[0], 54 signal_pid); 55 56 signal (SIGTTOU, SIG_IGN); 57 signal (SIGTTIN, SIG_IGN); 58 tcsetpgrp (fileno (stderr), signal_pid); 59 60 /* Wait till we are at 1st instruction in program, return signal number. */ 61 return mywait (statusptr, 0); 62 } 63 64 static int 65 attach_inferior (int pid, char *statusptr, unsigned char *sigptr) 66 { 67 /* myattach should return -1 if attaching is unsupported, 68 0 if it succeeded, and call error() otherwise. */ 69 70 if (myattach (pid) != 0) 71 return -1; 72 73 fprintf (stderr, "Attached; pid = %d\n", pid); 74 75 /* FIXME - It may be that we should get the SIGNAL_PID from the 76 attach function, so that it can be the main thread instead of 77 whichever we were told to attach to. */ 78 signal_pid = pid; 79 80 *sigptr = mywait (statusptr, 0); 81 82 return 0; 83 } 84 85 extern int remote_debug; 86 87 /* Handle all of the extended 'q' packets. */ 88 void 89 handle_query (char *own_buf) 90 { 91 static struct inferior_list_entry *thread_ptr; 92 93 if (strcmp ("qSymbol::", own_buf) == 0) 94 { 95 if (the_target->look_up_symbols != NULL) 96 (*the_target->look_up_symbols) (); 97 98 strcpy (own_buf, "OK"); 99 return; 100 } 101 102 if (strcmp ("qfThreadInfo", own_buf) == 0) 103 { 104 thread_ptr = all_threads.head; 105 sprintf (own_buf, "m%x", thread_ptr->id); 106 thread_ptr = thread_ptr->next; 107 return; 108 } 109 110 if (strcmp ("qsThreadInfo", own_buf) == 0) 111 { 112 if (thread_ptr != NULL) 113 { 114 sprintf (own_buf, "m%x", thread_ptr->id); 115 thread_ptr = thread_ptr->next; 116 return; 117 } 118 else 119 { 120 sprintf (own_buf, "l"); 121 return; 122 } 123 } 124 125 if (the_target->read_auxv != NULL 126 && strncmp ("qPart:auxv:read::", own_buf, 17) == 0) 127 { 128 char data[(PBUFSIZ - 1) / 2]; 129 CORE_ADDR ofs; 130 unsigned int len; 131 int n; 132 decode_m_packet (&own_buf[17], &ofs, &len); /* "OFS,LEN" */ 133 if (len > sizeof data) 134 len = sizeof data; 135 n = (*the_target->read_auxv) (ofs, data, len); 136 if (n == 0) 137 write_ok (own_buf); 138 else if (n < 0) 139 write_enn (own_buf); 140 else 141 convert_int_to_ascii (data, own_buf, n); 142 return; 143 } 144 145 /* Otherwise we didn't know what packet it was. Say we didn't 146 understand it. */ 147 own_buf[0] = 0; 148 } 149 150 /* Parse vCont packets. */ 151 void 152 handle_v_cont (char *own_buf, char *status, unsigned char *signal) 153 { 154 char *p, *q; 155 int n = 0, i = 0; 156 struct thread_resume *resume_info, default_action; 157 158 /* Count the number of semicolons in the packet. There should be one 159 for every action. */ 160 p = &own_buf[5]; 161 while (p) 162 { 163 n++; 164 p++; 165 p = strchr (p, ';'); 166 } 167 /* Allocate room for one extra action, for the default remain-stopped 168 behavior; if no default action is in the list, we'll need the extra 169 slot. */ 170 resume_info = malloc ((n + 1) * sizeof (resume_info[0])); 171 172 default_action.thread = -1; 173 default_action.leave_stopped = 1; 174 default_action.step = 0; 175 default_action.sig = 0; 176 177 p = &own_buf[5]; 178 i = 0; 179 while (*p) 180 { 181 p++; 182 183 resume_info[i].leave_stopped = 0; 184 185 if (p[0] == 's' || p[0] == 'S') 186 resume_info[i].step = 1; 187 else if (p[0] == 'c' || p[0] == 'C') 188 resume_info[i].step = 0; 189 else 190 goto err; 191 192 if (p[0] == 'S' || p[0] == 'C') 193 { 194 int sig; 195 sig = strtol (p + 1, &q, 16); 196 if (p == q) 197 goto err; 198 p = q; 199 200 if (!target_signal_to_host_p (sig)) 201 goto err; 202 resume_info[i].sig = target_signal_to_host (sig); 203 } 204 else 205 { 206 resume_info[i].sig = 0; 207 p = p + 1; 208 } 209 210 if (p[0] == 0) 211 { 212 resume_info[i].thread = -1; 213 default_action = resume_info[i]; 214 215 /* Note: we don't increment i here, we'll overwrite this entry 216 the next time through. */ 217 } 218 else if (p[0] == ':') 219 { 220 resume_info[i].thread = strtol (p + 1, &q, 16); 221 if (p == q) 222 goto err; 223 p = q; 224 if (p[0] != ';' && p[0] != 0) 225 goto err; 226 227 i++; 228 } 229 } 230 231 resume_info[i] = default_action; 232 233 /* Still used in occasional places in the backend. */ 234 if (n == 1 && resume_info[0].thread != -1) 235 cont_thread = resume_info[0].thread; 236 else 237 cont_thread = -1; 238 set_desired_inferior (0); 239 240 (*the_target->resume) (resume_info); 241 242 free (resume_info); 243 244 *signal = mywait (status, 1); 245 prepare_resume_reply (own_buf, *status, *signal); 246 return; 247 248 err: 249 /* No other way to report an error... */ 250 strcpy (own_buf, ""); 251 free (resume_info); 252 return; 253 } 254 255 /* Handle all of the extended 'v' packets. */ 256 void 257 handle_v_requests (char *own_buf, char *status, unsigned char *signal) 258 { 259 if (strncmp (own_buf, "vCont;", 6) == 0) 260 { 261 handle_v_cont (own_buf, status, signal); 262 return; 263 } 264 265 if (strncmp (own_buf, "vCont?", 6) == 0) 266 { 267 strcpy (own_buf, "vCont;c;C;s;S"); 268 return; 269 } 270 271 /* Otherwise we didn't know what packet it was. Say we didn't 272 understand it. */ 273 own_buf[0] = 0; 274 return; 275 } 276 277 void 278 myresume (int step, int sig) 279 { 280 struct thread_resume resume_info[2]; 281 int n = 0; 282 283 if (step || sig || cont_thread > 0) 284 { 285 resume_info[0].thread 286 = ((struct inferior_list_entry *) current_inferior)->id; 287 resume_info[0].step = step; 288 resume_info[0].sig = sig; 289 resume_info[0].leave_stopped = 0; 290 n++; 291 } 292 resume_info[n].thread = -1; 293 resume_info[n].step = 0; 294 resume_info[n].sig = 0; 295 resume_info[n].leave_stopped = (cont_thread > 0); 296 297 (*the_target->resume) (resume_info); 298 } 299 300 static int attached; 301 302 static void 303 gdbserver_usage (void) 304 { 305 error ("Usage:\tgdbserver COMM PROG [ARGS ...]\n" 306 "\tgdbserver COMM --attach PID\n" 307 "\n" 308 "COMM may either be a tty device (for serial debugging), or \n" 309 "HOST:PORT to listen for a TCP connection.\n"); 310 } 311 312 int 313 main (int argc, char *argv[]) 314 { 315 char ch, status, *own_buf, mem_buf[2000]; 316 int i = 0; 317 unsigned char signal; 318 unsigned int len; 319 CORE_ADDR mem_addr; 320 int bad_attach; 321 int pid; 322 char *arg_end; 323 324 if (setjmp (toplevel)) 325 { 326 fprintf (stderr, "Exiting\n"); 327 exit (1); 328 } 329 330 bad_attach = 0; 331 pid = 0; 332 attached = 0; 333 if (argc >= 3 && strcmp (argv[2], "--attach") == 0) 334 { 335 if (argc == 4 336 && argv[3] != '\0' 337 && (pid = strtoul (argv[3], &arg_end, 10)) != 0 338 && *arg_end == '\0') 339 { 340 ; 341 } 342 else 343 bad_attach = 1; 344 } 345 346 if (argc < 3 || bad_attach) 347 gdbserver_usage(); 348 349 initialize_low (); 350 351 own_buf = malloc (PBUFSIZ); 352 353 if (pid == 0) 354 { 355 /* Wait till we are at first instruction in program. */ 356 signal = start_inferior (&argv[2], &status); 357 358 /* We are now stopped at the first instruction of the target process */ 359 } 360 else 361 { 362 switch (attach_inferior (pid, &status, &signal)) 363 { 364 case -1: 365 error ("Attaching not supported on this target"); 366 break; 367 default: 368 attached = 1; 369 break; 370 } 371 } 372 373 while (1) 374 { 375 remote_open (argv[1]); 376 377 restart: 378 setjmp (toplevel); 379 while (getpkt (own_buf) > 0) 380 { 381 unsigned char sig; 382 i = 0; 383 ch = own_buf[i++]; 384 switch (ch) 385 { 386 case 'q': 387 handle_query (own_buf); 388 break; 389 case 'd': 390 remote_debug = !remote_debug; 391 break; 392 case 'D': 393 fprintf (stderr, "Detaching from inferior\n"); 394 detach_inferior (); 395 write_ok (own_buf); 396 putpkt (own_buf); 397 remote_close (); 398 399 /* If we are attached, then we can exit. Otherwise, we need to 400 hang around doing nothing, until the child is gone. */ 401 if (!attached) 402 { 403 int status, ret; 404 405 do { 406 ret = waitpid (signal_pid, &status, 0); 407 if (WIFEXITED (status) || WIFSIGNALED (status)) 408 break; 409 } while (ret != -1 || errno != ECHILD); 410 } 411 412 exit (0); 413 414 case '!': 415 if (attached == 0) 416 { 417 extended_protocol = 1; 418 prepare_resume_reply (own_buf, status, signal); 419 } 420 else 421 { 422 /* We can not use the extended protocol if we are 423 attached, because we can not restart the running 424 program. So return unrecognized. */ 425 own_buf[0] = '\0'; 426 } 427 break; 428 case '?': 429 prepare_resume_reply (own_buf, status, signal); 430 break; 431 case 'H': 432 switch (own_buf[1]) 433 { 434 case 'g': 435 general_thread = strtol (&own_buf[2], NULL, 16); 436 write_ok (own_buf); 437 set_desired_inferior (1); 438 break; 439 case 'c': 440 cont_thread = strtol (&own_buf[2], NULL, 16); 441 write_ok (own_buf); 442 break; 443 case 's': 444 step_thread = strtol (&own_buf[2], NULL, 16); 445 write_ok (own_buf); 446 break; 447 default: 448 /* Silently ignore it so that gdb can extend the protocol 449 without compatibility headaches. */ 450 own_buf[0] = '\0'; 451 break; 452 } 453 break; 454 case 'g': 455 set_desired_inferior (1); 456 registers_to_string (own_buf); 457 break; 458 case 'G': 459 set_desired_inferior (1); 460 registers_from_string (&own_buf[1]); 461 write_ok (own_buf); 462 break; 463 case 'm': 464 decode_m_packet (&own_buf[1], &mem_addr, &len); 465 if (read_inferior_memory (mem_addr, mem_buf, len) == 0) 466 convert_int_to_ascii (mem_buf, own_buf, len); 467 else 468 write_enn (own_buf); 469 break; 470 case 'M': 471 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); 472 if (write_inferior_memory (mem_addr, mem_buf, len) == 0) 473 write_ok (own_buf); 474 else 475 write_enn (own_buf); 476 break; 477 case 'C': 478 convert_ascii_to_int (own_buf + 1, &sig, 1); 479 if (target_signal_to_host_p (sig)) 480 signal = target_signal_to_host (sig); 481 else 482 signal = 0; 483 set_desired_inferior (0); 484 myresume (0, signal); 485 signal = mywait (&status, 1); 486 prepare_resume_reply (own_buf, status, signal); 487 break; 488 case 'S': 489 convert_ascii_to_int (own_buf + 1, &sig, 1); 490 if (target_signal_to_host_p (sig)) 491 signal = target_signal_to_host (sig); 492 else 493 signal = 0; 494 set_desired_inferior (0); 495 myresume (1, signal); 496 signal = mywait (&status, 1); 497 prepare_resume_reply (own_buf, status, signal); 498 break; 499 case 'c': 500 set_desired_inferior (0); 501 myresume (0, 0); 502 signal = mywait (&status, 1); 503 prepare_resume_reply (own_buf, status, signal); 504 break; 505 case 's': 506 set_desired_inferior (0); 507 myresume (1, 0); 508 signal = mywait (&status, 1); 509 prepare_resume_reply (own_buf, status, signal); 510 break; 511 case 'k': 512 fprintf (stderr, "Killing inferior\n"); 513 kill_inferior (); 514 /* When using the extended protocol, we start up a new 515 debugging session. The traditional protocol will 516 exit instead. */ 517 if (extended_protocol) 518 { 519 write_ok (own_buf); 520 fprintf (stderr, "GDBserver restarting\n"); 521 522 /* Wait till we are at 1st instruction in prog. */ 523 signal = start_inferior (&argv[2], &status); 524 goto restart; 525 break; 526 } 527 else 528 { 529 exit (0); 530 break; 531 } 532 case 'T': 533 if (mythread_alive (strtol (&own_buf[1], NULL, 16))) 534 write_ok (own_buf); 535 else 536 write_enn (own_buf); 537 break; 538 case 'R': 539 /* Restarting the inferior is only supported in the 540 extended protocol. */ 541 if (extended_protocol) 542 { 543 kill_inferior (); 544 write_ok (own_buf); 545 fprintf (stderr, "GDBserver restarting\n"); 546 547 /* Wait till we are at 1st instruction in prog. */ 548 signal = start_inferior (&argv[2], &status); 549 goto restart; 550 break; 551 } 552 else 553 { 554 /* It is a request we don't understand. Respond with an 555 empty packet so that gdb knows that we don't support this 556 request. */ 557 own_buf[0] = '\0'; 558 break; 559 } 560 case 'v': 561 /* Extended (long) request. */ 562 handle_v_requests (own_buf, &status, &signal); 563 break; 564 default: 565 /* It is a request we don't understand. Respond with an 566 empty packet so that gdb knows that we don't support this 567 request. */ 568 own_buf[0] = '\0'; 569 break; 570 } 571 572 putpkt (own_buf); 573 574 if (status == 'W') 575 fprintf (stderr, 576 "\nChild exited with status %d\n", signal); 577 if (status == 'X') 578 fprintf (stderr, "\nChild terminated with signal = 0x%x\n", 579 signal); 580 if (status == 'W' || status == 'X') 581 { 582 if (extended_protocol) 583 { 584 fprintf (stderr, "Killing inferior\n"); 585 kill_inferior (); 586 write_ok (own_buf); 587 fprintf (stderr, "GDBserver restarting\n"); 588 589 /* Wait till we are at 1st instruction in prog. */ 590 signal = start_inferior (&argv[2], &status); 591 goto restart; 592 break; 593 } 594 else 595 { 596 fprintf (stderr, "GDBserver exiting\n"); 597 exit (0); 598 } 599 } 600 } 601 602 /* We come here when getpkt fails. 603 604 For the extended remote protocol we exit (and this is the only 605 way we gracefully exit!). 606 607 For the traditional remote protocol close the connection, 608 and re-open it at the top of the loop. */ 609 if (extended_protocol) 610 { 611 remote_close (); 612 exit (0); 613 } 614 else 615 { 616 fprintf (stderr, "Remote side has terminated connection. " 617 "GDBserver will reopen the connection.\n"); 618 remote_close (); 619 } 620 } 621 } 622