1 /* Main code for remote server for GDB. 2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003, 3 2004, 2005, 2006, 2011 4 Free Software Foundation, Inc. 5 6 This file is part of GDB. 7 It has been modified to integrate it in valgrind 8 9 This program is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 2 of the License, or 12 (at your option) any later version. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program; if not, write to the Free Software 21 Foundation, Inc., 51 Franklin Street, Fifth Floor, 22 Boston, MA 02110-1301, USA. */ 23 24 #include "server.h" 25 #include "regdef.h" 26 #include "pub_core_options.h" 27 #include "pub_core_translate.h" 28 #include "pub_core_mallocfree.h" 29 #include "pub_core_initimg.h" 30 31 unsigned long cont_thread; 32 unsigned long general_thread; 33 unsigned long step_thread; 34 unsigned long thread_from_wait; 35 unsigned long old_thread_from_wait; 36 37 int pass_signals[TARGET_SIGNAL_LAST]; 38 39 /* for a gdbserver integrated in valgrind, resuming the process consists 40 in returning the control to valgrind. 41 Then at the next error or break or ..., valgrind calls gdbserver again. 42 A resume packet must then be built. 43 resume_packet_needed records the fact that the next call to gdbserver 44 must send a resume packet to gdb. */ 45 static Bool resume_packet_needed = False; 46 47 VG_MINIMAL_JMP_BUF(toplevel); 48 49 /* Decode a qXfer read request. Return 0 if everything looks OK, 50 or -1 otherwise. */ 51 52 static 53 int decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len) 54 { 55 /* Extract and NUL-terminate the annex. */ 56 *annex = buf; 57 while (*buf && *buf != ':') 58 buf++; 59 if (*buf == '\0') 60 return -1; 61 *buf++ = 0; 62 63 /* After the read/write marker and annex, qXfer looks like a 64 traditional 'm' packet. */ 65 decode_m_packet (buf, ofs, len); 66 67 return 0; 68 } 69 70 /* Write the response to a successful qXfer read. Returns the 71 length of the (binary) data stored in BUF, corresponding 72 to as much of DATA/LEN as we could fit. IS_MORE controls 73 the first character of the response. */ 74 static 75 int write_qxfer_response (char *buf, unsigned char *data, int len, int is_more) 76 { 77 int out_len; 78 79 if (is_more) 80 buf[0] = 'm'; 81 else 82 buf[0] = 'l'; 83 84 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len, 85 PBUFSIZ - POVERHSIZ - 1) + 1; 86 } 87 88 static Bool initial_valgrind_sink_saved = False; 89 /* True <=> valgrind log sink saved in initial_valgrind_sink */ 90 static OutputSink initial_valgrind_sink; 91 92 static Bool command_output_to_log = False; 93 /* True <=> command output goes to log instead of gdb */ 94 95 void reset_valgrind_sink(char *info) 96 { 97 if (VG_(log_output_sink).fd != initial_valgrind_sink.fd 98 && initial_valgrind_sink_saved) { 99 VG_(log_output_sink).fd = initial_valgrind_sink.fd; 100 VG_(umsg) ("Reset valgrind output to log (%s)\n", 101 (info = NULL ? "" : info)); 102 } 103 } 104 105 static 106 void kill_request (char *msg) 107 { 108 VG_(umsg) ("%s", msg); 109 remote_close(); 110 VG_(exit) (0); 111 } 112 113 /* handle_gdb_valgrind_command handles the provided mon string command. 114 If command is recognised, return 1 else return 0. 115 Note that in case of ambiguous command, 1 is returned. 116 117 *sink_wanted_at_return is modified if one of the commands 118 'v.set *_output' is handled. 119 */ 120 static 121 int handle_gdb_valgrind_command (char* mon, OutputSink* sink_wanted_at_return) 122 { 123 UWord ret = 0; 124 char s[strlen(mon)+1]; /* copy for strtok_r */ 125 char* wcmd; 126 Char* ssaveptr; 127 char* endptr; 128 int kwdid; 129 int int_value; 130 131 vg_assert (initial_valgrind_sink_saved); 132 133 strcpy (s, mon); 134 wcmd = strtok_r (s, " ", &ssaveptr); 135 /* NB: if possible, avoid introducing a new command below which 136 starts with the same 3 first letters as an already existing 137 command. This ensures a shorter abbreviation for the user. */ 138 switch (VG_(keyword_id) ("help v.set v.info v.wait v.kill v.translate", 139 wcmd, kwd_report_duplicated_matches)) { 140 case -2: 141 ret = 1; 142 break; 143 case -1: 144 break; 145 case 0: /* help */ 146 ret = 1; 147 wcmd = strtok_r (NULL, " ", &ssaveptr); 148 if (wcmd == NULL) { 149 int_value = 0; 150 } else { 151 switch (VG_(keyword_id) ("debug", wcmd, kwd_report_all)) { 152 case -2: int_value = 0; break; 153 case -1: int_value = 0; break; 154 case 0: int_value = 1; break; 155 default: tl_assert (0); 156 } 157 } 158 159 VG_(gdb_printf) ( 160 "general valgrind monitor commands:\n" 161 " help [debug] : monitor command help. With debug: + debugging commands\n" 162 " v.wait [<ms>] : sleep <ms> (default 0) then continue\n" 163 " v.info all_errors : show all errors found so far\n" 164 " v.info last_error : show last error found\n" 165 " v.info n_errs_found : show the nr of errors found so far\n" 166 " v.kill : kill the Valgrind process\n" 167 " v.set gdb_output : set valgrind output to gdb\n" 168 " v.set log_output : set valgrind output to log\n" 169 " v.set mixed_output : set valgrind output to log, interactive output to gdb\n" 170 " v.set vgdb-error <errornr> : debug me at error >= <errornr> \n"); 171 if (int_value) { VG_(gdb_printf) ( 172 "debugging valgrind internals monitor commands:\n" 173 " v.info gdbserver_status : show gdbserver status\n" 174 " v.info memory : show valgrind heap memory stats\n" 175 " v.info scheduler : show valgrind thread state and stacktrace\n" 176 " v.set debuglog <level> : set valgrind debug log level to <level>\n" 177 " v.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n" 178 " (default traceflags 0b00100000 : show after instrumentation)\n" 179 " An additional flag 0b100000000 allows to show gdbserver instrumentation\n"); 180 } 181 break; 182 case 1: /* v.set */ 183 ret = 1; 184 wcmd = strtok_r (NULL, " ", &ssaveptr); 185 switch (kwdid = VG_(keyword_id) 186 ("vgdb-error debuglog gdb_output log_output mixed_output", 187 wcmd, kwd_report_all)) { 188 case -2: 189 case -1: 190 break; 191 case 0: /* vgdb-error */ 192 case 1: /* debuglog */ 193 wcmd = strtok_r (NULL, " ", &ssaveptr); 194 if (wcmd == NULL) { 195 int_value = 0; 196 endptr = "empty"; /* to report an error below */ 197 } else { 198 int_value = strtol (wcmd, &endptr, 10); 199 } 200 if (*endptr != '\0') { 201 VG_(gdb_printf) ("missing or malformed integer value\n"); 202 } else if (kwdid == 0) { 203 VG_(gdb_printf) ("vgdb-error value changed from %d to %d\n", 204 VG_(dyn_vgdb_error), int_value); 205 VG_(dyn_vgdb_error) = int_value; 206 } else if (kwdid == 1) { 207 VG_(gdb_printf) ("debuglog value changed from %d to %d\n", 208 VG_(debugLog_getLevel)(), int_value); 209 VG_(debugLog_startup) (int_value, "gdbsrv"); 210 } else { 211 vg_assert (0); 212 } 213 break; 214 case 2: /* gdb_output */ 215 (*sink_wanted_at_return).fd = -2; 216 command_output_to_log = False; 217 VG_(gdb_printf) ("valgrind output will go to gdb\n"); 218 break; 219 case 3: /* log_output */ 220 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd; 221 command_output_to_log = True; 222 VG_(gdb_printf) ("valgrind output will go to log\n"); 223 break; 224 case 4: /* mixed output */ 225 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd; 226 command_output_to_log = False; 227 VG_(gdb_printf) 228 ("valgrind output will go to log, interactive output will go to gdb\n"); 229 break; 230 default: 231 vg_assert (0); 232 } 233 break; 234 case 2: /* v.info */ { 235 ret = 1; 236 wcmd = strtok_r (NULL, " ", &ssaveptr); 237 switch (kwdid = VG_(keyword_id) 238 ("all_errors n_errs_found last_error gdbserver_status memory" 239 " scheduler", 240 wcmd, kwd_report_all)) { 241 case -2: 242 case -1: 243 break; 244 case 0: // all_errors 245 // A verbosity of minimum 2 is needed to show the errors. 246 VG_(show_all_errors)(/* verbosity */ 2, /* xml */ False); 247 break; 248 case 1: // n_errs_found 249 VG_(gdb_printf) ("n_errs_found %d (vgdb-error %d)\n", 250 VG_(get_n_errs_found) (), 251 VG_(dyn_vgdb_error)); 252 break; 253 case 2: // last_error 254 VG_(show_last_error)(); 255 break; 256 case 3: // gdbserver_status 257 VG_(gdbserver_status_output)(); 258 break; 259 case 4: /* memory */ 260 VG_(print_all_arena_stats) (); 261 if (VG_(clo_profile_heap)) 262 VG_(print_arena_cc_analysis) (); 263 ret = 1; 264 break; 265 case 5: /* scheduler */ 266 VG_(show_sched_status) (); 267 ret = 1; 268 break; 269 default: 270 vg_assert(0); 271 } 272 break; 273 } 274 case 3: /* v.wait */ 275 wcmd = strtok_r (NULL, " ", &ssaveptr); 276 if (wcmd != NULL) { 277 int_value = strtol (wcmd, &endptr, 10); 278 VG_(gdb_printf) ("gdbserver: continuing in %d ms ...\n", int_value); 279 VG_(poll)(NULL, 0, int_value); 280 } 281 VG_(gdb_printf) ("gdbserver: continuing after wait ...\n"); 282 ret = 1; 283 break; 284 case 4: /* v.kill */ 285 kill_request ("monitor command request to kill this process\n"); 286 break; 287 case 5: { /* v.translate */ 288 Addr address; 289 SizeT verbosity = 0x20; 290 291 ret = 1; 292 293 VG_(strtok_get_address_and_size) (&address, &verbosity, &ssaveptr); 294 if (address != (Addr) 0 || verbosity != 0) { 295 /* we need to force the output to log for the translation trace, 296 as low level VEX tracing cannot be redirected to gdb. */ 297 int saved_command_output_to_log = command_output_to_log; 298 int saved_fd = VG_(log_output_sink).fd; 299 Bool single_stepping_on_entry = valgrind_single_stepping(); 300 int vex_verbosity = verbosity & 0xff; 301 VG_(log_output_sink).fd = initial_valgrind_sink.fd; 302 if ((verbosity & 0x100) && !single_stepping_on_entry) { 303 valgrind_set_single_stepping(True); 304 // to force gdbserver instrumentation. 305 } 306 # if defined(VGA_arm) 307 // on arm, we need to (potentially) convert this address 308 // to the thumb form. 309 address = thumb_pc (address); 310 # endif 311 312 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/, 313 address, 314 /*debugging*/True, 315 (Int) vex_verbosity, 316 /*bbs_done*/0, 317 /*allow redir?*/True); 318 if ((verbosity & 0x100) && !single_stepping_on_entry) { 319 valgrind_set_single_stepping(False); 320 // reset single stepping. 321 } 322 command_output_to_log = saved_command_output_to_log; 323 VG_(log_output_sink).fd = saved_fd; 324 } 325 break; 326 } 327 328 default: 329 vg_assert (0); 330 } 331 return ret; 332 } 333 334 /* handle_gdb_monitor_command handles the provided mon string command, 335 which can be either a "standard" valgrind monitor command 336 or a tool specific monitor command. 337 If command recognised, return 1 else return 0. 338 Note that in case of ambiguous command, 1 is returned. 339 */ 340 static 341 int handle_gdb_monitor_command (char* mon) 342 { 343 UWord ret = 0; 344 UWord tool_ret = 0; 345 // initially, we assume that when returning, the desired sink is the 346 // one we have when entering. It can however be changed by the standard 347 // valgrind command handling. 348 OutputSink sink_wanted_at_return = VG_(log_output_sink); 349 350 if (!initial_valgrind_sink_saved) { 351 /* first time we enter here, we save the valgrind default log sink */ 352 initial_valgrind_sink = sink_wanted_at_return; 353 initial_valgrind_sink_saved = True; 354 } 355 356 if (!command_output_to_log) 357 VG_(log_output_sink).fd = -2; /* redirect to monitor_output */ 358 359 ret = handle_gdb_valgrind_command (mon, &sink_wanted_at_return); 360 361 /* Even if command was recognised by valgrind core, we call the 362 tool command handler : this is needed to handle help command 363 and/or to let the tool do some additional processing of a 364 valgrind standard command. Note however that if valgrind 365 recognised the command, we will always return success. */ 366 if (VG_(needs).client_requests) { 367 /* If the tool reports an error when handling a monitor command, 368 we need to avoid calling gdbserver during this command 369 handling. So, we temporarily set VG_(dyn_vgdb_error) to 370 a huge value to ensure m_errormgr.c does not call gdbserver. */ 371 Int save_dyn_vgdb_error = VG_(dyn_vgdb_error); 372 UWord arg[2]; 373 VG_(dyn_vgdb_error) = 999999999; 374 arg[0] = (UWord) VG_USERREQ__GDB_MONITOR_COMMAND; 375 arg[1] = (UWord) mon; 376 VG_TDICT_CALL(tool_handle_client_request, VG_(running_tid), arg, 377 &tool_ret); 378 VG_(dyn_vgdb_error) = save_dyn_vgdb_error; 379 } 380 381 /* restore or set the desired output */ 382 VG_(log_output_sink).fd = sink_wanted_at_return.fd; 383 if (ret | tool_ret) 384 return 1; 385 else 386 return 0; 387 } 388 389 390 /* Handle all of the extended 'Q' packets. */ 391 static 392 void handle_set (char *arg_own_buf, int *new_packet_len_p) 393 { 394 if (strcmp ("QStartNoAckMode", arg_own_buf) == 0) { 395 noack_mode = True; 396 write_ok (arg_own_buf); 397 return; 398 } 399 400 if (strncmp ("QPassSignals:", arg_own_buf, 13) == 0) { 401 int i; 402 char *from, *to; 403 char *end = arg_own_buf + strlen(arg_own_buf); 404 CORE_ADDR sig; 405 for (i = 0; i < TARGET_SIGNAL_LAST; i++) 406 pass_signals[i] = 0; 407 408 from = arg_own_buf + 13; 409 while (from < end) { 410 to = strchr(from, ';'); 411 if (to == NULL) to = end; 412 decode_address (&sig, from, to - from); 413 pass_signals[(int)sig] = 1; 414 dlog(1, "pass_signal %d\n", (int)sig); 415 from = to; 416 if (*from == ';') from++; 417 } 418 write_ok (arg_own_buf); 419 return; 420 } 421 /* Otherwise we didn't know what packet it was. Say we didn't 422 understand it. */ 423 arg_own_buf[0] = 0; 424 } 425 426 /* Handle all of the extended 'q' packets. */ 427 static 428 void handle_query (char *arg_own_buf, int *new_packet_len_p) 429 { 430 static struct inferior_list_entry *thread_ptr; 431 432 /* qRcmd, monitor command handling. */ 433 if (strncmp ("qRcmd,", arg_own_buf, 6) == 0) { 434 char *p = arg_own_buf + 6; 435 int cmdlen = strlen(p)/2; 436 char cmd[cmdlen+1]; 437 438 if (unhexify (cmd, p, cmdlen) != cmdlen) { 439 write_enn (arg_own_buf); 440 return; 441 } 442 cmd[cmdlen] = '\0'; 443 444 if (handle_gdb_monitor_command (cmd)) { 445 /* In case the command is from a standalone vgdb, 446 connection will be closed soon => flush the output. */ 447 VG_(message_flush) (); 448 write_ok (arg_own_buf); 449 return; 450 } else { 451 /* cmd not recognised */ 452 VG_(gdb_printf) 453 ("command '%s' not recognised\n" 454 "In gdb, try 'monitor help'\n" 455 "In a shell, try 'vgdb help'\n", 456 cmd); 457 write_ok (arg_own_buf); 458 return; 459 } 460 } 461 462 /* provide some valgrind specific info in return to qThreadExtraInfo. */ 463 if (strncmp ("qThreadExtraInfo,", arg_own_buf, 17) == 0) { 464 unsigned long gdb_id; 465 struct thread_info *ti; 466 ThreadState *tst; 467 char status[100]; 468 469 gdb_id = strtoul (&arg_own_buf[17], NULL, 16); 470 ti = gdb_id_to_thread (gdb_id); 471 if (ti != NULL) { 472 tst = (ThreadState *) inferior_target_data (ti); 473 /* Additional info is the tid and the thread status. */ 474 VG_(snprintf) (status, sizeof(status), "tid %d %s", 475 tst->tid, 476 VG_(name_of_ThreadStatus)(tst->status)); 477 hexify (arg_own_buf, status, strlen(status)); 478 return; 479 } else { 480 write_enn (arg_own_buf); 481 return; 482 } 483 } 484 485 if (strcmp ("qAttached", arg_own_buf) == 0) { 486 /* tell gdb to always detach, never kill the process */ 487 arg_own_buf[0] = '1'; 488 arg_own_buf[1] = 0; 489 return; 490 } 491 492 if (strcmp ("qSymbol::", arg_own_buf) == 0) { 493 /* We have no symbol to read. */ 494 write_ok (arg_own_buf); 495 return; 496 } 497 498 if (strcmp ("qfThreadInfo", arg_own_buf) == 0) { 499 thread_ptr = all_threads.head; 500 VG_(sprintf) (arg_own_buf, "m%x", 501 thread_to_gdb_id ((struct thread_info *)thread_ptr)); 502 thread_ptr = thread_ptr->next; 503 return; 504 } 505 506 if (strcmp ("qsThreadInfo", arg_own_buf) == 0) { 507 if (thread_ptr != NULL) { 508 VG_(sprintf) (arg_own_buf, "m%x", 509 thread_to_gdb_id ((struct thread_info *)thread_ptr)); 510 thread_ptr = thread_ptr->next; 511 return; 512 } else { 513 VG_(sprintf) (arg_own_buf, "l"); 514 return; 515 } 516 } 517 518 if ( ((*the_target->target_xml)() != NULL 519 || (*the_target->shadow_target_xml)() != NULL) 520 && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) { 521 CORE_ADDR ofs; 522 unsigned int len, doc_len; 523 char *annex = NULL; 524 // First, the annex is extracted from the packet received. 525 // Then, it is replaced by the corresponding file name. 526 int fd; 527 528 /* Grab the annex, offset, and length. */ 529 if (decode_xfer_read (arg_own_buf + 20, &annex, &ofs, &len) < 0) { 530 strcpy (arg_own_buf, "E00"); 531 return; 532 } 533 534 if (strcmp (annex, "target.xml") == 0) { 535 annex = NULL; // to replace it by the corresponding filename. 536 537 /* If VG_(clo_vgdb_shadow_registers), try to use 538 shadow_target_xml. Fallback to target_xml 539 if not defined. */ 540 if (VG_(clo_vgdb_shadow_registers)) { 541 annex = (*the_target->shadow_target_xml)(); 542 if (annex != NULL) 543 /* Ensure the shadow registers are initialized. */ 544 initialize_shadow_low(True); 545 } 546 if (annex == NULL) 547 annex = (*the_target->target_xml)(); 548 if (annex == NULL) { 549 strcpy (arg_own_buf, "E00"); 550 return; 551 } 552 } 553 554 { 555 char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex)]; 556 struct vg_stat stat_doc; 557 char toread[len]; 558 int len_read; 559 560 VG_(sprintf)(doc, "%s/%s", VG_(libdir), annex); 561 fd = VG_(fd_open) (doc, VKI_O_RDONLY, 0); 562 if (fd == -1) { 563 strcpy (arg_own_buf, "E00"); 564 return; 565 } 566 if (VG_(fstat) (fd, &stat_doc) != 0) { 567 VG_(close) (fd); 568 strcpy (arg_own_buf, "E00"); 569 return; 570 } 571 doc_len = stat_doc.size; 572 573 if (len > PBUFSIZ - POVERHSIZ) 574 len = PBUFSIZ - POVERHSIZ; 575 576 if (ofs > doc_len) { 577 write_enn (arg_own_buf); 578 VG_(close) (fd); 579 return; 580 } 581 VG_(lseek) (fd, ofs, VKI_SEEK_SET); 582 len_read = VG_(read) (fd, toread, len); 583 *new_packet_len_p = write_qxfer_response (arg_own_buf, toread, 584 len_read, ofs + len_read < doc_len); 585 VG_(close) (fd); 586 return; 587 } 588 } 589 590 if (strncmp ("qXfer:auxv:read:", arg_own_buf, 16) == 0) { 591 unsigned char *data; 592 int n; 593 CORE_ADDR ofs; 594 unsigned int len; 595 char *annex; 596 597 /* Reject any annex; grab the offset and length. */ 598 if (decode_xfer_read (arg_own_buf + 16, &annex, &ofs, &len) < 0 599 || annex[0] != '\0') { 600 strcpy (arg_own_buf, "E00"); 601 return; 602 } 603 604 if (len > PBUFSIZ - 2) 605 len = PBUFSIZ - 2; 606 data = malloc (len); 607 608 { 609 UWord *client_auxv = VG_(client_auxv); 610 unsigned int client_auxv_len = 0; 611 while (*client_auxv != 0) { 612 dlog(4, "auxv %lld %llx\n", 613 (ULong)*client_auxv, 614 (ULong)*(client_auxv+1)); 615 client_auxv++; 616 client_auxv++; 617 client_auxv_len += 2 * sizeof(UWord); 618 } 619 client_auxv_len += 2 * sizeof(UWord); 620 dlog(4, "auxv len %d\n", client_auxv_len); 621 622 if (ofs >= client_auxv_len) 623 n = -1; 624 else { 625 n = client_auxv_len - ofs; 626 VG_(memcpy) (data, (unsigned char *) VG_(client_auxv), n); 627 } 628 } 629 630 if (n < 0) 631 write_enn (arg_own_buf); 632 else if (n > len) 633 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1); 634 else 635 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0); 636 637 free (data); 638 639 return; 640 } 641 642 643 /* Protocol features query. */ 644 if (strncmp ("qSupported", arg_own_buf, 10) == 0 645 && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) { 646 VG_(sprintf) (arg_own_buf, "PacketSize=%x", PBUFSIZ - 1); 647 /* Note: max packet size including frame and checksum, but without 648 trailing null byte, which is not sent/received. */ 649 650 strcat (arg_own_buf, ";QStartNoAckMode+"); 651 strcat (arg_own_buf, ";QPassSignals+"); 652 if (VG_(client_auxv)) 653 strcat (arg_own_buf, ";qXfer:auxv:read+"); 654 655 if ((*the_target->target_xml)() != NULL 656 || (*the_target->shadow_target_xml)() != NULL) { 657 strcat (arg_own_buf, ";qXfer:features:read+"); 658 /* if a new gdb connects to us, we have to reset the register 659 set to the normal register sets to allow this new gdb to 660 decide to use or not the shadow registers. 661 662 Note that the reset is only done for gdb that are sending 663 qSupported packets. If a user first connected with a recent 664 gdb using shadow registers and then with a very old gdb 665 that does not use qSupported packet, then the old gdb will 666 not properly connect. */ 667 initialize_shadow_low(False); 668 } 669 return; 670 } 671 672 /* Otherwise we didn't know what packet it was. Say we didn't 673 understand it. */ 674 arg_own_buf[0] = 0; 675 } 676 677 /* Handle all of the extended 'v' packets. */ 678 static 679 void handle_v_requests (char *arg_own_buf, char *status, int *zignal) 680 { 681 /* vcont packet code from gdb 6.6 removed */ 682 683 /* Otherwise we didn't know what packet it was. Say we didn't 684 understand it. */ 685 arg_own_buf[0] = 0; 686 return; 687 } 688 689 static 690 void myresume (int step, int sig) 691 { 692 struct thread_resume resume_info[2]; 693 int n = 0; 694 695 if (step || sig || (cont_thread != 0 && cont_thread != -1)) { 696 resume_info[0].thread 697 = ((struct inferior_list_entry *) current_inferior)->id; 698 resume_info[0].step = step; 699 resume_info[0].sig = sig; 700 resume_info[0].leave_stopped = 0; 701 n++; 702 } 703 resume_info[n].thread = -1; 704 resume_info[n].step = 0; 705 resume_info[n].sig = 0; 706 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1); 707 708 resume_packet_needed = True; 709 (*the_target->resume) (resume_info); 710 } 711 712 /* server_main global variables */ 713 static char *own_buf; 714 static unsigned char *mem_buf; 715 716 void gdbserver_init (void) 717 { 718 dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version); 719 noack_mode = False; 720 initialize_low (); 721 own_buf = malloc (PBUFSIZ); 722 mem_buf = malloc (PBUFSIZ); 723 } 724 725 void gdbserver_terminate (void) 726 { 727 /* last call to gdbserver is cleanup call */ 728 if (VG_MINIMAL_SETJMP(toplevel)) { 729 dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n"); 730 return; 731 } 732 remote_close(); 733 } 734 735 void server_main (void) 736 { 737 static char status; 738 static int zignal; 739 740 char ch; 741 int i = 0; 742 unsigned int len; 743 CORE_ADDR mem_addr; 744 745 zignal = mywait (&status); 746 if (VG_MINIMAL_SETJMP(toplevel)) { 747 dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n"); 748 } 749 while (1) { 750 unsigned char sig; 751 int packet_len; 752 int new_packet_len = -1; 753 754 if (resume_packet_needed) { 755 resume_packet_needed = False; 756 prepare_resume_reply (own_buf, status, zignal); 757 putpkt (own_buf); 758 } 759 760 packet_len = getpkt (own_buf); 761 if (packet_len <= 0) 762 break; 763 764 i = 0; 765 ch = own_buf[i++]; 766 switch (ch) { 767 case 'Q': 768 handle_set (own_buf, &new_packet_len); 769 break; 770 case 'q': 771 handle_query (own_buf, &new_packet_len); 772 break; 773 case 'd': 774 /* set/unset debugging is done through valgrind debug level. */ 775 own_buf[0] = '\0'; 776 break; 777 case 'D': 778 reset_valgrind_sink("gdb detaching from process"); 779 780 /* When detaching or kill the process, gdb expects to get 781 an packet OK back. Any other output will make gdb 782 believes detach did not work. */ 783 write_ok (own_buf); 784 putpkt (own_buf); 785 remote_finish (reset_after_error); 786 remote_open (VG_(clo_vgdb_prefix)); 787 myresume (0, 0); 788 resume_packet_needed = False; 789 return; 790 case '!': 791 /* We can not use the extended protocol with valgrind, 792 because we can not restart the running 793 program. So return unrecognized. */ 794 own_buf[0] = '\0'; 795 break; 796 case '?': 797 prepare_resume_reply (own_buf, status, zignal); 798 break; 799 case 'H': 800 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') { 801 unsigned long gdb_id, thread_id; 802 803 gdb_id = strtoul (&own_buf[2], NULL, 16); 804 thread_id = gdb_id_to_thread_id (gdb_id); 805 if (thread_id == 0) { 806 write_enn (own_buf); 807 break; 808 } 809 810 if (own_buf[1] == 'g') { 811 general_thread = thread_id; 812 set_desired_inferior (1); 813 } else if (own_buf[1] == 'c') { 814 cont_thread = thread_id; 815 } else if (own_buf[1] == 's') { 816 step_thread = thread_id; 817 } 818 819 write_ok (own_buf); 820 } else { 821 /* Silently ignore it so that gdb can extend the protocol 822 without compatibility headaches. */ 823 own_buf[0] = '\0'; 824 } 825 break; 826 case 'g': 827 set_desired_inferior (1); 828 registers_to_string (own_buf); 829 break; 830 case 'G': 831 set_desired_inferior (1); 832 registers_from_string (&own_buf[1]); 833 write_ok (own_buf); 834 break; 835 case 'P': { 836 int regno; 837 char *regbytes; 838 Bool mod; 839 ThreadState *tst; 840 regno = strtol(&own_buf[1], NULL, 16); 841 regbytes = strchr(&own_buf[0], '=') + 1; 842 set_desired_inferior (1); 843 tst = (ThreadState *) inferior_target_data (current_inferior); 844 /* Only accept changing registers in "runnable state3. 845 In fact, it would be ok to change most of the registers 846 except a few "sensitive" registers such as the PC, SP, BP. 847 We assume we do not need to very specific here, and that we 848 can just refuse all of these. */ 849 if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) { 850 supply_register_from_string (regno, regbytes, &mod); 851 write_ok (own_buf); 852 } else { 853 /* at least from gdb 6.6 onwards, an E. error 854 reply is shown to the user. So, we do an error 855 msg which both is accepted by gdb as an error msg 856 and is readable by the user. */ 857 VG_(sprintf) 858 (own_buf, 859 "E.\n" 860 "ERROR changing register %s regno %d\n" 861 "gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n" 862 "set pc, calling from gdb a function in the debugged process, ...)\n" 863 "can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n" 864 "Thread status is %s\n", 865 find_register_by_number (regno)->name, regno, 866 VG_(name_of_ThreadStatus)(tst->status)); 867 if (VG_(clo_verbosity) > 1) 868 VG_(umsg) ("%s\n", own_buf); 869 } 870 break; 871 } 872 case 'm': 873 decode_m_packet (&own_buf[1], &mem_addr, &len); 874 if (read_inferior_memory (mem_addr, mem_buf, len) == 0) 875 convert_int_to_ascii (mem_buf, own_buf, len); 876 else 877 write_enn (own_buf); 878 break; 879 case 'M': 880 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); 881 if (write_inferior_memory (mem_addr, mem_buf, len) == 0) 882 write_ok (own_buf); 883 else 884 write_enn (own_buf); 885 break; 886 case 'X': 887 if (decode_X_packet (&own_buf[1], packet_len - 1, 888 &mem_addr, &len, mem_buf) < 0 889 || write_inferior_memory (mem_addr, mem_buf, len) != 0) 890 write_enn (own_buf); 891 else 892 write_ok (own_buf); 893 break; 894 case 'C': 895 convert_ascii_to_int (own_buf + 1, &sig, 1); 896 if (target_signal_to_host_p (sig)) 897 zignal = target_signal_to_host (sig); 898 else 899 zignal = 0; 900 set_desired_inferior (0); 901 myresume (0, zignal); 902 return; // return control to valgrind 903 case 'S': 904 convert_ascii_to_int (own_buf + 1, &sig, 1); 905 if (target_signal_to_host_p (sig)) 906 zignal = target_signal_to_host (sig); 907 else 908 zignal = 0; 909 set_desired_inferior (0); 910 myresume (1, zignal); 911 return; // return control to valgrind 912 case 'c': 913 set_desired_inferior (0); 914 myresume (0, 0); 915 return; // return control to valgrind 916 case 's': 917 set_desired_inferior (0); 918 myresume (1, 0); 919 return; // return control to valgrind 920 case 'Z': { 921 char *lenptr; 922 char *dataptr; 923 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); 924 int zlen = strtol (lenptr + 1, &dataptr, 16); 925 char type = own_buf[1]; 926 927 if (the_target->insert_watchpoint == NULL 928 || (type < '0' || type > '4')) { 929 /* No watchpoint support or not a watchpoint command; 930 unrecognized either way. */ 931 own_buf[0] = '\0'; 932 } else { 933 int res; 934 935 res = (*the_target->insert_watchpoint) (type, addr, zlen); 936 if (res == 0) 937 write_ok (own_buf); 938 else if (res == 1) 939 /* Unsupported. */ 940 own_buf[0] = '\0'; 941 else 942 write_enn (own_buf); 943 } 944 break; 945 } 946 case 'z': { 947 char *lenptr; 948 char *dataptr; 949 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); 950 int zlen = strtol (lenptr + 1, &dataptr, 16); 951 char type = own_buf[1]; 952 953 if (the_target->remove_watchpoint == NULL 954 || (type < '0' || type > '4')) { 955 /* No watchpoint support or not a watchpoint command; 956 unrecognized either way. */ 957 own_buf[0] = '\0'; 958 } else { 959 int res; 960 961 res = (*the_target->remove_watchpoint) (type, addr, zlen); 962 if (res == 0) 963 write_ok (own_buf); 964 else if (res == 1) 965 /* Unsupported. */ 966 own_buf[0] = '\0'; 967 else 968 write_enn (own_buf); 969 } 970 break; 971 } 972 case 'k': 973 kill_request("Gdb request to kill this process\n"); 974 break; 975 case 'T': { 976 unsigned long gdb_id, thread_id; 977 978 gdb_id = strtoul (&own_buf[1], NULL, 16); 979 thread_id = gdb_id_to_thread_id (gdb_id); 980 if (thread_id == 0) { 981 write_enn (own_buf); 982 break; 983 } 984 985 if (mythread_alive (thread_id)) 986 write_ok (own_buf); 987 else 988 write_enn (own_buf); 989 break; 990 } 991 case 'R': 992 /* Restarting the inferior is only supported in the 993 extended protocol. 994 => It is a request we don't understand. Respond with an 995 empty packet so that gdb knows that we don't support this 996 request. */ 997 own_buf[0] = '\0'; 998 break; 999 case 'v': 1000 /* Extended (long) request. */ 1001 handle_v_requests (own_buf, &status, &zignal); 1002 break; 1003 default: 1004 /* It is a request we don't understand. Respond with an 1005 empty packet so that gdb knows that we don't support this 1006 request. */ 1007 own_buf[0] = '\0'; 1008 break; 1009 } 1010 1011 if (new_packet_len != -1) 1012 putpkt_binary (own_buf, new_packet_len); 1013 else 1014 putpkt (own_buf); 1015 1016 if (status == 'W') 1017 VG_(umsg) ("\nChild exited with status %d\n", zignal); 1018 if (status == 'X') 1019 VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n", 1020 target_signal_to_host (zignal), 1021 target_signal_to_name (zignal)); 1022 if (status == 'W' || status == 'X') { 1023 VG_(umsg) ("Process exiting\n"); 1024 VG_(exit) (0); 1025 } 1026 } 1027 1028 /* We come here when getpkt fails => close the connection, 1029 and re-open. Then return control to valgrind. 1030 We return the control to valgrind as we assume that 1031 the connection was closed due to vgdb having finished 1032 to execute a command. */ 1033 if (VG_(clo_verbosity) > 1) 1034 VG_(umsg) ("Remote side has terminated connection. " 1035 "GDBserver will reopen the connection.\n"); 1036 remote_finish (reset_after_error); 1037 remote_open (VG_(clo_vgdb_prefix)); 1038 myresume (0, 0); 1039 resume_packet_needed = False; 1040 return; 1041 } 1042