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 #include "pub_core_execontext.h" 31 #include "pub_core_syswrap.h" // VG_(show_open_fds) 32 #include "pub_core_scheduler.h" 33 #include "pub_core_transtab.h" 34 #include "pub_core_debuginfo.h" 35 #include "pub_core_addrinfo.h" 36 #include "pub_core_aspacemgr.h" 37 38 unsigned long cont_thread; 39 unsigned long general_thread; 40 unsigned long step_thread; 41 unsigned long thread_from_wait; 42 unsigned long old_thread_from_wait; 43 44 int pass_signals[TARGET_SIGNAL_LAST]; /* indexed by gdb signal nr */ 45 46 /* for a gdbserver integrated in valgrind, resuming the process consists 47 in returning the control to valgrind. 48 The guess process resumes its execution. 49 Then at the next error or break or ..., valgrind calls gdbserver again. 50 A resume reply packet must then be built to inform GDB that the 51 resume request is finished. 52 resume_reply_packet_needed records the fact that the next call to gdbserver 53 must send a resume packet to gdb. */ 54 static Bool resume_reply_packet_needed = False; 55 56 VG_MINIMAL_JMP_BUF(toplevel); 57 58 /* Decode a qXfer read request. Return 0 if everything looks OK, 59 or -1 otherwise. */ 60 61 static 62 int decode_xfer_read (char *buf, const char **annex, CORE_ADDR *ofs, unsigned int *len) 63 { 64 /* Extract and NUL-terminate the annex. */ 65 *annex = buf; 66 while (*buf && *buf != ':') 67 buf++; 68 if (*buf == '\0') 69 return -1; 70 *buf++ = 0; 71 72 /* After the read/write marker and annex, qXfer looks like a 73 traditional 'm' packet. */ 74 decode_m_packet (buf, ofs, len); 75 76 return 0; 77 } 78 79 /* Write the response to a successful qXfer read. Returns the 80 length of the (binary) data stored in BUF, corresponding 81 to as much of DATA/LEN as we could fit. IS_MORE controls 82 the first character of the response. */ 83 static 84 int write_qxfer_response (char *buf, unsigned char *data, int len, int is_more) 85 { 86 int out_len; 87 88 if (is_more) 89 buf[0] = 'm'; 90 else 91 buf[0] = 'l'; 92 93 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len, 94 PBUFSIZ - POVERHSIZ - 1) + 1; 95 } 96 97 static Bool initial_valgrind_sink_saved = False; 98 /* True <=> valgrind log sink saved in initial_valgrind_sink */ 99 static OutputSink initial_valgrind_sink; 100 101 static Bool command_output_to_log = False; 102 /* True <=> command output goes to log instead of gdb */ 103 104 void reset_valgrind_sink(const char *info) 105 { 106 if (VG_(log_output_sink).fd != initial_valgrind_sink.fd 107 && initial_valgrind_sink_saved) { 108 VG_(log_output_sink).fd = initial_valgrind_sink.fd; 109 VG_(umsg) ("Reset valgrind output to log (%s)\n", 110 (info = NULL ? "" : info)); 111 } 112 } 113 114 void print_to_initial_valgrind_sink (const char *msg) 115 { 116 vg_assert (initial_valgrind_sink_saved); 117 VG_(write) (initial_valgrind_sink.fd, msg, strlen(msg)); 118 } 119 120 121 static 122 void kill_request (const char *msg) 123 { 124 VG_(umsg) ("%s", msg); 125 VG_(exit) (0); 126 } 127 128 // s is a NULL terminated string made of O or more words (separated by spaces). 129 // Returns a pointer to the Nth word in s. 130 // If Nth word does not exist, return a pointer to the last (0) byte of s. 131 static 132 const char *wordn (const char *s, int n) 133 { 134 int word_seen = 0; 135 Bool searching_word = True; 136 137 while (*s) { 138 if (*s == ' ') 139 searching_word = True; 140 else { 141 if (searching_word) { 142 searching_word = False; 143 word_seen++; 144 if (word_seen == n) 145 return s; 146 } 147 } 148 s++; 149 } 150 return s; 151 } 152 153 void VG_(print_all_stats) (Bool memory_stats, Bool tool_stats) 154 { 155 if (memory_stats) { 156 VG_(message)(Vg_DebugMsg, "\n"); 157 VG_(message)(Vg_DebugMsg, 158 "------ Valgrind's internal memory use stats follow ------\n" ); 159 VG_(sanity_check_malloc_all)(); 160 VG_(message) 161 (Vg_DebugMsg, 162 "------ %'13llu bytes have already been mmap-ed ANONYMOUS.\n", 163 VG_(am_get_anonsize_total)()); 164 VG_(print_all_arena_stats)(); 165 if (VG_(clo_profile_heap)) 166 VG_(print_arena_cc_analysis) (); 167 VG_(message)(Vg_DebugMsg, "\n"); 168 } 169 170 VG_(print_translation_stats)(); 171 VG_(print_tt_tc_stats)(); 172 VG_(print_scheduler_stats)(); 173 VG_(print_ExeContext_stats)( False /* with_stacktraces */ ); 174 VG_(print_errormgr_stats)(); 175 if (tool_stats && VG_(needs).print_stats) { 176 VG_TDICT_CALL(tool_print_stats); 177 } 178 } 179 180 /* handle_gdb_valgrind_command handles the provided mon string command. 181 If command is recognised, return 1 else return 0. 182 Note that in case of ambiguous command, 1 is returned. 183 184 *sink_wanted_at_return is modified if one of the commands 185 'v.set *_output' is handled. 186 */ 187 static 188 int handle_gdb_valgrind_command (char *mon, OutputSink *sink_wanted_at_return) 189 { 190 UWord ret = 0; 191 char s[strlen(mon)+1]; /* copy for strtok_r */ 192 char *wcmd; 193 HChar *ssaveptr; 194 const char *endptr; 195 int kwdid; 196 int int_value; 197 198 vg_assert (initial_valgrind_sink_saved); 199 200 strcpy (s, mon); 201 wcmd = strtok_r (s, " ", &ssaveptr); 202 /* NB: if possible, avoid introducing a new command below which 203 starts with the same 3 first letters as an already existing 204 command. This ensures a shorter abbreviation for the user. */ 205 switch (VG_(keyword_id) ("help v.set v.info v.wait v.kill v.translate" 206 " v.do", 207 wcmd, kwd_report_duplicated_matches)) { 208 case -2: 209 ret = 1; 210 break; 211 case -1: 212 break; 213 case 0: /* help */ 214 ret = 1; 215 wcmd = strtok_r (NULL, " ", &ssaveptr); 216 if (wcmd == NULL) { 217 int_value = 0; 218 } else { 219 switch (VG_(keyword_id) ("debug", wcmd, kwd_report_all)) { 220 case -2: int_value = 0; break; 221 case -1: int_value = 0; break; 222 case 0: int_value = 1; break; 223 default: vg_assert (0); 224 } 225 } 226 227 VG_(gdb_printf) ( 228 "general valgrind monitor commands:\n" 229 " help [debug] : monitor command help. With debug: + debugging commands\n" 230 " v.wait [<ms>] : sleep <ms> (default 0) then continue\n" 231 " v.info all_errors : show all errors found so far\n" 232 " v.info last_error : show last error found\n" 233 " v.info location <addr> : show information about location <addr>\n" 234 " v.info n_errs_found [msg] : show the nr of errors found so far and the given msg\n" 235 " v.info open_fds : show open file descriptors (only if --track-fds=yes)\n" 236 " v.kill : kill the Valgrind process\n" 237 " v.set gdb_output : set valgrind output to gdb\n" 238 " v.set log_output : set valgrind output to log\n" 239 " v.set mixed_output : set valgrind output to log, interactive output to gdb\n" 240 " v.set merge-recursive-frames <num> : merge recursive calls in max <num> frames\n" 241 " v.set vgdb-error <errornr> : debug me at error >= <errornr> \n"); 242 if (int_value) { VG_(gdb_printf) ( 243 "debugging valgrind internals monitor commands:\n" 244 " v.do expensive_sanity_check_general : do an expensive sanity check now\n" 245 " v.info gdbserver_status : show gdbserver status\n" 246 " v.info memory [aspacemgr] : show valgrind heap memory stats\n" 247 " (with aspacemgr arg, also shows valgrind segments on log output)\n" 248 " v.info exectxt : show stacktraces and stats of all execontexts\n" 249 " v.info scheduler : show valgrind thread state and stacktrace\n" 250 " v.info stats : show various valgrind and tool stats\n" 251 " v.info unwind <addr> [<len>] : show unwind debug info for <addr> .. <addr+len>\n" 252 " v.set debuglog <level> : set valgrind debug log level to <level>\n" 253 " v.set hostvisibility [yes*|no] : (en/dis)ables access by gdb/gdbserver to\n" 254 " Valgrind internal host status/memory\n" 255 " v.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n" 256 " (default traceflags 0b00100000 : show after instrumentation)\n" 257 " An additional flag 0b100000000 allows to show gdbserver instrumentation\n"); 258 } 259 break; 260 case 1: /* v.set */ 261 ret = 1; 262 wcmd = strtok_r (NULL, " ", &ssaveptr); 263 switch (kwdid = VG_(keyword_id) 264 ("vgdb-error debuglog merge-recursive-frames" 265 " gdb_output log_output mixed_output hostvisibility", 266 wcmd, kwd_report_all)) { 267 case -2: 268 case -1: 269 break; 270 case 0: /* vgdb-error */ 271 case 1: /* debuglog */ 272 case 2: /* merge-recursive-frames */ 273 wcmd = strtok_r (NULL, " ", &ssaveptr); 274 if (wcmd == NULL) { 275 int_value = 0; 276 endptr = "empty"; /* to report an error below */ 277 } else { 278 HChar *the_end; 279 int_value = strtol (wcmd, &the_end, 10); 280 endptr = the_end; 281 } 282 if (*endptr != '\0') { 283 VG_(gdb_printf) ("missing or malformed integer value\n"); 284 } else if (kwdid == 0) { 285 VG_(printf) ("vgdb-error value changed from %d to %d\n", 286 VG_(dyn_vgdb_error), int_value); 287 VG_(dyn_vgdb_error) = int_value; 288 } else if (kwdid == 1) { 289 VG_(printf) ("debuglog value changed from %d to %d\n", 290 VG_(debugLog_getLevel)(), int_value); 291 VG_(debugLog_startup) (int_value, "gdbsrv"); 292 } else if (kwdid == 2) { 293 VG_(printf) 294 ("merge-recursive-frames value changed from %d to %d\n", 295 VG_(clo_merge_recursive_frames), int_value); 296 VG_(clo_merge_recursive_frames) = int_value; 297 } else { 298 vg_assert (0); 299 } 300 break; 301 case 3: /* gdb_output */ 302 (*sink_wanted_at_return).fd = -2; 303 command_output_to_log = False; 304 VG_(gdb_printf) ("valgrind output will go to gdb\n"); 305 break; 306 case 4: /* log_output */ 307 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd; 308 command_output_to_log = True; 309 VG_(gdb_printf) ("valgrind output will go to log\n"); 310 break; 311 case 5: /* mixed output */ 312 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd; 313 command_output_to_log = False; 314 VG_(gdb_printf) 315 ("valgrind output will go to log, " 316 "interactive output will go to gdb\n"); 317 break; 318 case 6: /* hostvisibility */ 319 wcmd = strtok_r (NULL, " ", &ssaveptr); 320 if (wcmd != NULL) { 321 switch (VG_(keyword_id) ("yes no", wcmd, kwd_report_all)) { 322 case -2: 323 case -1: break; 324 case 0: 325 hostvisibility = True; 326 break; 327 case 1: 328 hostvisibility = False; 329 break; 330 default: vg_assert (0); 331 } 332 } else { 333 hostvisibility = True; 334 } 335 if (hostvisibility) { 336 const DebugInfo *tooldi 337 = VG_(find_DebugInfo) ((Addr)handle_gdb_valgrind_command); 338 /* Normally, we should always find the tooldi. In case we 339 do not, suggest a 'likely somewhat working' address: */ 340 const Addr tool_text_start 341 = tooldi ? 342 VG_(DebugInfo_get_text_avma) (tooldi) : 0x38000000; 343 const NSegment *toolseg 344 = tooldi ? 345 VG_(am_find_nsegment) (VG_(DebugInfo_get_text_avma) (tooldi)) 346 : NULL; 347 VG_(gdb_printf) 348 ("Enabled access to Valgrind memory/status by GDB\n" 349 "If not yet done, tell GDB which valgrind file(s) to use, " 350 "typically:\n" 351 "add-symbol-file %s %p\n", 352 toolseg ? VG_(am_get_filename)(toolseg) 353 : "<toolfile> <address> e.g.", 354 (void*)tool_text_start); 355 } else 356 VG_(gdb_printf) 357 ("Disabled access to Valgrind memory/status by GDB\n"); 358 break; 359 default: 360 vg_assert (0); 361 } 362 break; 363 case 2: /* v.info */ { 364 ret = 1; 365 wcmd = strtok_r (NULL, " ", &ssaveptr); 366 switch (kwdid = VG_(keyword_id) 367 ("all_errors n_errs_found last_error gdbserver_status memory" 368 " scheduler stats open_fds exectxt location unwind", 369 wcmd, kwd_report_all)) { 370 case -2: 371 case -1: 372 break; 373 case 0: // all_errors 374 // A verbosity of minimum 2 is needed to show the errors. 375 VG_(show_all_errors)(/* verbosity */ 2, /* xml */ False); 376 break; 377 case 1: // n_errs_found 378 VG_(printf) ("n_errs_found %u n_errs_shown %u (vgdb-error %d) %s\n", 379 VG_(get_n_errs_found) (), 380 VG_(get_n_errs_shown) (), 381 VG_(dyn_vgdb_error), 382 wordn (mon, 3)); 383 break; 384 case 2: // last_error 385 VG_(show_last_error)(); 386 break; 387 case 3: // gdbserver_status 388 VG_(gdbserver_status_output)(); 389 break; 390 case 4: /* memory */ 391 VG_(printf) ("%'13llu bytes have already been mmap-ed ANONYMOUS.\n", 392 VG_(am_get_anonsize_total)()); 393 VG_(print_all_arena_stats) (); 394 if (VG_(clo_profile_heap)) 395 VG_(print_arena_cc_analysis) (); 396 wcmd = strtok_r (NULL, " ", &ssaveptr); 397 if (wcmd != NULL) { 398 switch (VG_(keyword_id) ("aspacemgr", wcmd, kwd_report_all)) { 399 case -2: 400 case -1: break; 401 case 0: 402 VG_(am_show_nsegments) (0, "gdbserver v.info memory aspacemgr"); 403 break; 404 default: vg_assert (0); 405 } 406 } 407 408 ret = 1; 409 break; 410 case 5: /* scheduler */ 411 VG_(show_sched_status) (True, // host_stacktrace 412 True, // stack_usage 413 True); // exited_threads 414 ret = 1; 415 break; 416 case 6: /* stats */ 417 VG_(print_all_stats)(False, /* Memory stats */ 418 True /* Tool stats */); 419 ret = 1; 420 break; 421 case 7: /* open_fds */ 422 if (VG_(clo_track_fds)) 423 VG_(show_open_fds) (""); 424 else 425 VG_(gdb_printf) 426 ("Valgrind must be started with --track-fds=yes" 427 " to show open fds\n"); 428 ret = 1; 429 break; 430 case 8: /* exectxt */ 431 VG_(print_ExeContext_stats) (True /* with_stacktraces */); 432 ret = 1; 433 break; 434 case 9: { /* location */ 435 /* Note: we prefer 'v.info location' and not 'v.info address' as 436 v.info address is inconsistent with the GDB (native) 437 command 'info address' which gives the address for a symbol. 438 GDB equivalent command of 'v.info location' is 'info symbol'. */ 439 Addr address; 440 SizeT dummy_sz = 0x1234; 441 if (VG_(strtok_get_address_and_size) (&address, 442 &dummy_sz, &ssaveptr)) { 443 // If tool provides location information, use that. 444 if (VG_(needs).info_location) { 445 VG_TDICT_CALL(tool_info_location, address); 446 } 447 // If tool does not provide location info, use the common one. 448 // Also use the common to compare with tool when debug log is set. 449 if (!VG_(needs).info_location || VG_(debugLog_getLevel)() > 0 ) { 450 AddrInfo ai; 451 ai.tag = Addr_Undescribed; 452 VG_(describe_addr) (address, &ai); 453 VG_(pp_addrinfo) (address, &ai); 454 VG_(clear_addrinfo) (&ai); 455 } 456 } 457 ret = 1; 458 break; 459 } 460 case 10: { /* unwind */ 461 Addr address; 462 SizeT sz = 1; 463 if (VG_(strtok_get_address_and_size) (&address, 464 &sz, &ssaveptr)) { 465 VG_(ppUnwindInfo) (address, address + sz - 1); 466 } 467 ret = 1; 468 break; 469 } 470 471 default: 472 vg_assert(0); 473 } 474 break; 475 } 476 case 3: /* v.wait */ 477 wcmd = strtok_r (NULL, " ", &ssaveptr); 478 if (wcmd != NULL) { 479 int_value = strtol (wcmd, NULL, 10); 480 VG_(printf) ("gdbserver: continuing in %d ms ...\n", int_value); 481 VG_(poll)(NULL, 0, int_value); 482 } 483 VG_(printf) ("gdbserver: continuing after wait ...\n"); 484 ret = 1; 485 break; 486 case 4: /* v.kill */ 487 kill_request ("monitor command request to kill this process\n"); 488 break; 489 case 5: { /* v.translate */ 490 Addr address; 491 SizeT verbosity = 0x20; 492 493 ret = 1; 494 495 if (VG_(strtok_get_address_and_size) (&address, &verbosity, &ssaveptr)) { 496 /* we need to force the output to log for the translation trace, 497 as low level VEX tracing cannot be redirected to gdb. */ 498 int saved_command_output_to_log = command_output_to_log; 499 int saved_fd = VG_(log_output_sink).fd; 500 Bool single_stepping_on_entry = valgrind_single_stepping(); 501 int vex_verbosity = verbosity & 0xff; 502 VG_(log_output_sink).fd = initial_valgrind_sink.fd; 503 if ((verbosity & 0x100) && !single_stepping_on_entry) { 504 valgrind_set_single_stepping(True); 505 // to force gdbserver instrumentation. 506 } 507 # if defined(VGA_arm) 508 // on arm, we need to (potentially) convert this address 509 // to the thumb form. 510 address = thumb_pc (address); 511 # endif 512 513 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/, 514 address, 515 /*debugging*/True, 516 (Int) vex_verbosity, 517 /*bbs_done*/0, 518 /*allow redir?*/True); 519 if ((verbosity & 0x100) && !single_stepping_on_entry) { 520 valgrind_set_single_stepping(False); 521 // reset single stepping. 522 } 523 command_output_to_log = saved_command_output_to_log; 524 VG_(log_output_sink).fd = saved_fd; 525 } 526 break; 527 } 528 529 case 6: /* v.do */ 530 ret = 1; 531 wcmd = strtok_r (NULL, " ", &ssaveptr); 532 switch (VG_(keyword_id) ("expensive_sanity_check_general", 533 wcmd, kwd_report_all)) { 534 case -2: 535 case -1: break; 536 case 0: { /* expensive_sanity_check_general */ 537 // Temporarily bump up sanity level to check e.g. the malloc arenas. 538 const Int save_clo_sanity_level = VG_(clo_sanity_level); 539 if (VG_(clo_sanity_level) < 4) VG_(clo_sanity_level) = 4; 540 VG_(sanity_check_general) (/* force_expensive */ True); 541 VG_(clo_sanity_level) = save_clo_sanity_level; 542 break; 543 } 544 default: vg_assert (0); 545 } 546 break; 547 548 default: 549 vg_assert (0); 550 } 551 return ret; 552 } 553 554 /* handle_gdb_monitor_command handles the provided mon string command, 555 which can be either a "standard" valgrind monitor command 556 or a tool specific monitor command. 557 If command recognised, return 1 else return 0. 558 Note that in case of ambiguous command, 1 is returned. 559 */ 560 static 561 int handle_gdb_monitor_command (char *mon) 562 { 563 UWord ret = 0; 564 UWord tool_ret = 0; 565 // initially, we assume that when returning, the desired sink is the 566 // one we have when entering. It can however be changed by the standard 567 // valgrind command handling. 568 OutputSink sink_wanted_at_return = VG_(log_output_sink); 569 // When using gdbserver, we temporarily disable xml output. 570 Bool save_clo_xml = VG_(clo_xml); 571 VG_(clo_xml) = False; 572 573 if (!initial_valgrind_sink_saved) { 574 /* first time we enter here, we save the valgrind default log sink */ 575 initial_valgrind_sink = sink_wanted_at_return; 576 initial_valgrind_sink_saved = True; 577 } 578 579 if (!command_output_to_log) 580 VG_(log_output_sink).fd = -2; /* redirect to monitor_output */ 581 582 ret = handle_gdb_valgrind_command (mon, &sink_wanted_at_return); 583 584 /* Even if command was recognised by valgrind core, we call the 585 tool command handler : this is needed to handle help command 586 and/or to let the tool do some additional processing of a 587 valgrind standard command. Note however that if valgrind 588 recognised the command, we will always return success. */ 589 if (VG_(needs).client_requests) { 590 /* If the tool reports an error when handling a monitor command, 591 we need to avoid calling gdbserver during this command 592 handling. So, we temporarily set VG_(dyn_vgdb_error) to 593 a huge value to ensure m_errormgr.c does not call gdbserver. */ 594 Int save_dyn_vgdb_error = VG_(dyn_vgdb_error); 595 UWord arg[2]; 596 VG_(dyn_vgdb_error) = 999999999; 597 arg[0] = (UWord) VG_USERREQ__GDB_MONITOR_COMMAND; 598 arg[1] = (UWord) mon; 599 VG_TDICT_CALL(tool_handle_client_request, VG_(running_tid), arg, 600 &tool_ret); 601 VG_(dyn_vgdb_error) = save_dyn_vgdb_error; 602 } 603 604 VG_(message_flush) (); 605 606 /* restore or set the desired output */ 607 VG_(log_output_sink).fd = sink_wanted_at_return.fd; 608 VG_(clo_xml) = save_clo_xml; 609 610 if (ret | tool_ret) 611 return 1; 612 else 613 return 0; 614 } 615 616 617 /* Handle all of the extended 'Q' packets. */ 618 static 619 void handle_set (char *arg_own_buf, int *new_packet_len_p) 620 { 621 if (strcmp ("QStartNoAckMode", arg_own_buf) == 0) { 622 noack_mode = True; 623 write_ok (arg_own_buf); 624 return; 625 } 626 627 if (strncmp ("QPassSignals:", arg_own_buf, 13) == 0) { 628 int i; 629 char *from, *to; 630 char *end = arg_own_buf + strlen(arg_own_buf); 631 CORE_ADDR sig; 632 for (i = 0; i < TARGET_SIGNAL_LAST; i++) 633 pass_signals[i] = 0; 634 635 from = arg_own_buf + 13; 636 while (from < end) { 637 to = strchr(from, ';'); 638 if (to == NULL) to = end; 639 decode_address (&sig, from, to - from); 640 pass_signals[(int)sig] = 1; 641 dlog(3, "pass_signal gdb_nr %d %s\n", 642 (int)sig, target_signal_to_name(sig)); 643 from = to; 644 if (*from == ';') from++; 645 } 646 write_ok (arg_own_buf); 647 return; 648 } 649 /* Otherwise we didn't know what packet it was. Say we didn't 650 understand it. */ 651 arg_own_buf[0] = 0; 652 } 653 654 Bool VG_(client_monitor_command) (HChar *cmd) 655 { 656 const Bool connected = remote_connected(); 657 const int saved_command_output_to_log = command_output_to_log; 658 Bool handled; 659 660 if (!connected) 661 command_output_to_log = True; 662 handled = handle_gdb_monitor_command (cmd); 663 if (!connected) { 664 // reset the log output unless cmd changed it. 665 if (command_output_to_log) 666 command_output_to_log = saved_command_output_to_log; 667 } 668 if (handled) 669 return False; // recognised 670 else 671 return True; // not recognised 672 } 673 674 /* Handle all of the extended 'q' packets. */ 675 static 676 void handle_query (char *arg_own_buf, int *new_packet_len_p) 677 { 678 static struct inferior_list_entry *thread_ptr; 679 680 /* thread local storage query */ 681 if (strncmp ("qGetTLSAddr:", arg_own_buf, 12) == 0) { 682 char *from, *to; 683 char *end = arg_own_buf + strlen(arg_own_buf); 684 unsigned long gdb_id; 685 CORE_ADDR lm; 686 CORE_ADDR offset; 687 struct thread_info *ti; 688 689 from = arg_own_buf + 12; 690 to = strchr(from, ','); 691 *to = 0; 692 gdb_id = strtoul (from, NULL, 16); 693 from = to + 1; 694 to = strchr(from, ','); 695 decode_address (&offset, from, to - from); 696 from = to + 1; 697 to = end; 698 decode_address (&lm, from, to - from); 699 dlog(2, "qGetTLSAddr thread %lu offset %p lm %p\n", 700 gdb_id, (void*)offset, (void*)lm); 701 702 ti = gdb_id_to_thread (gdb_id); 703 if (ti != NULL) { 704 ThreadState *tst; 705 Addr tls_addr; 706 707 tst = (ThreadState *) inferior_target_data (ti); 708 if (valgrind_get_tls_addr(tst, offset, lm, &tls_addr)) { 709 VG_(sprintf) (arg_own_buf, "%lx", tls_addr); 710 return; 711 } 712 // else we will report we do not support qGetTLSAddr 713 } else { 714 write_enn (arg_own_buf); 715 return; 716 } 717 } 718 719 /* qRcmd, monitor command handling. */ 720 if (strncmp ("qRcmd,", arg_own_buf, 6) == 0) { 721 char *p = arg_own_buf + 6; 722 int cmdlen = strlen(p)/2; 723 char cmd[cmdlen+1]; 724 725 if (unhexify (cmd, p, cmdlen) != cmdlen) { 726 write_enn (arg_own_buf); 727 return; 728 } 729 cmd[cmdlen] = '\0'; 730 731 if (handle_gdb_monitor_command (cmd)) { 732 write_ok (arg_own_buf); 733 return; 734 } else { 735 /* cmd not recognised */ 736 VG_(gdb_printf) 737 ("command '%s' not recognised\n" 738 "In gdb, try 'monitor help'\n" 739 "In a shell, try 'vgdb help'\n", 740 cmd); 741 write_ok (arg_own_buf); 742 return; 743 } 744 } 745 746 /* provide some valgrind specific info in return to qThreadExtraInfo. */ 747 if (strncmp ("qThreadExtraInfo,", arg_own_buf, 17) == 0) { 748 unsigned long gdb_id; 749 struct thread_info *ti; 750 ThreadState *tst; 751 752 gdb_id = strtoul (&arg_own_buf[17], NULL, 16); 753 ti = gdb_id_to_thread (gdb_id); 754 if (ti != NULL) { 755 tst = (ThreadState *) inferior_target_data (ti); 756 /* Additional info is the tid, the thread status and the thread's 757 name, if any. */ 758 SizeT len = strlen(VG_(name_of_ThreadStatus)(tst->status)) + 20; 759 if (tst->thread_name) len += strlen(tst->thread_name); 760 /* As the string will be hexified and copied into own_buf we need 761 to limit the length to avoid buffer overflow. */ 762 if (len * 2 > (PBUFSIZ + POVERHSIZ)) 763 len = (PBUFSIZ + POVERHSIZ) / 2; 764 char status[len]; 765 if (tst->thread_name) { 766 VG_(snprintf) (status, sizeof(status), "tid %u %s %s", 767 tst->tid, 768 VG_(name_of_ThreadStatus)(tst->status), 769 tst->thread_name); 770 } else { 771 VG_(snprintf) (status, sizeof(status), "tid %u %s", 772 tst->tid, 773 VG_(name_of_ThreadStatus)(tst->status)); 774 } 775 hexify (arg_own_buf, status, strlen(status)); 776 return; 777 } else { 778 write_enn (arg_own_buf); 779 return; 780 } 781 } 782 783 if (strcmp ("qAttached", arg_own_buf) == 0) { 784 /* tell gdb to always detach, never kill the process */ 785 arg_own_buf[0] = '1'; 786 arg_own_buf[1] = 0; 787 return; 788 } 789 790 if (strcmp ("qSymbol::", arg_own_buf) == 0) { 791 /* We have no symbol to read. */ 792 write_ok (arg_own_buf); 793 return; 794 } 795 796 if (strcmp ("qfThreadInfo", arg_own_buf) == 0) { 797 thread_ptr = all_threads.head; 798 VG_(sprintf) (arg_own_buf, "m%x", 799 thread_to_gdb_id ((struct thread_info *)thread_ptr)); 800 thread_ptr = thread_ptr->next; 801 return; 802 } 803 804 if (strcmp ("qsThreadInfo", arg_own_buf) == 0) { 805 if (thread_ptr != NULL) { 806 VG_(sprintf) (arg_own_buf, "m%x", 807 thread_to_gdb_id ((struct thread_info *)thread_ptr)); 808 thread_ptr = thread_ptr->next; 809 return; 810 } else { 811 VG_(sprintf) (arg_own_buf, "l"); 812 return; 813 } 814 } 815 816 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL 817 && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) { 818 CORE_ADDR ofs; 819 unsigned int len, doc_len; 820 const char *annex = NULL; 821 // First, the annex is extracted from the packet received. 822 // Then, it is replaced by the corresponding file name. 823 int fd; 824 825 /* Grab the annex, offset, and length. */ 826 if (decode_xfer_read (arg_own_buf + 20, &annex, &ofs, &len) < 0) { 827 strcpy (arg_own_buf, "E00"); 828 return; 829 } 830 831 if (strcmp (annex, "target.xml") == 0) { 832 annex = valgrind_target_xml(VG_(clo_vgdb_shadow_registers)); 833 if (annex != NULL && VG_(clo_vgdb_shadow_registers)) { 834 /* Ensure the shadow registers are initialized. */ 835 initialize_shadow_low(True); 836 } 837 if (annex == NULL) { 838 strcpy (arg_own_buf, "E00"); 839 return; 840 } 841 } 842 843 { 844 char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex) + 1]; 845 struct vg_stat stat_doc; 846 char toread[len]; 847 int len_read; 848 849 VG_(sprintf)(doc, "%s/%s", VG_(libdir), annex); 850 fd = VG_(fd_open) (doc, VKI_O_RDONLY, 0); 851 if (fd == -1) { 852 strcpy (arg_own_buf, "E00"); 853 return; 854 } 855 if (VG_(fstat) (fd, &stat_doc) != 0) { 856 VG_(close) (fd); 857 strcpy (arg_own_buf, "E00"); 858 return; 859 } 860 doc_len = stat_doc.size; 861 862 if (len > PBUFSIZ - POVERHSIZ) 863 len = PBUFSIZ - POVERHSIZ; 864 865 if (ofs > doc_len) { 866 write_enn (arg_own_buf); 867 VG_(close) (fd); 868 return; 869 } 870 VG_(lseek) (fd, ofs, VKI_SEEK_SET); 871 len_read = VG_(read) (fd, toread, len); 872 *new_packet_len_p = write_qxfer_response (arg_own_buf, 873 (unsigned char *)toread, 874 len_read, 875 ofs + len_read < doc_len); 876 VG_(close) (fd); 877 return; 878 } 879 } 880 881 if (strncmp ("qXfer:auxv:read:", arg_own_buf, 16) == 0) { 882 unsigned char *data; 883 int n; 884 CORE_ADDR ofs; 885 unsigned int len; 886 const char *annex; 887 888 /* Reject any annex; grab the offset and length. */ 889 if (decode_xfer_read (arg_own_buf + 16, &annex, &ofs, &len) < 0 890 || annex[0] != '\0') { 891 strcpy (arg_own_buf, "E00"); 892 return; 893 } 894 895 if (len > PBUFSIZ - POVERHSIZ) 896 len = PBUFSIZ - POVERHSIZ; 897 data = malloc (len); 898 899 { 900 UWord *client_auxv = VG_(client_auxv); 901 unsigned int client_auxv_len = 0; 902 while (*client_auxv != 0) { 903 dlog(4, "auxv %llu %llx\n", 904 (ULong)*client_auxv, 905 (ULong)*(client_auxv+1)); 906 client_auxv++; 907 client_auxv++; 908 client_auxv_len += 2 * sizeof(UWord); 909 } 910 client_auxv_len += 2 * sizeof(UWord); 911 dlog(4, "auxv len %u\n", client_auxv_len); 912 913 if (ofs >= client_auxv_len) 914 n = -1; 915 else { 916 n = client_auxv_len - ofs; 917 VG_(memcpy) (data, (unsigned char *) VG_(client_auxv), n); 918 } 919 } 920 921 if (n < 0) 922 write_enn (arg_own_buf); 923 else if (n > len) 924 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1); 925 else 926 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0); 927 928 free (data); 929 930 return; 931 } 932 933 if (strncmp ("qXfer:exec-file:read:", arg_own_buf, 21) == 0) { 934 unsigned char *data; 935 int n; 936 CORE_ADDR ofs; 937 unsigned int len; 938 const char *annex; 939 unsigned long pid; 940 const HChar *name; 941 942 /* grab the annex, offset and length. */ 943 if (decode_xfer_read (arg_own_buf + 21, &annex, &ofs, &len) < 0) { 944 strcpy (arg_own_buf, "E00"); 945 return; 946 } 947 948 /* Reject any annex with invalid/unexpected pid */ 949 if (strlen(annex) > 0) 950 pid = strtoul (annex, NULL, 16); 951 else 952 pid = 0; 953 if ((int)pid != VG_(getpid)() && pid != 0) { 954 VG_(sprintf) (arg_own_buf, 955 "E.Valgrind gdbserver pid is %d." 956 " Cannot give info for pid %d", 957 VG_(getpid)(), (int) pid); 958 return; 959 } 960 961 if (len > PBUFSIZ - 2) 962 len = PBUFSIZ - 2; 963 data = malloc (len); 964 965 if (!VG_(resolve_filename)(VG_(cl_exec_fd), &name)) { 966 VG_(sprintf) (arg_own_buf, 967 "E.Valgrind gdbserver could not" 968 " resolve pid %d exec filename.", 969 VG_(getpid)()); 970 return; 971 } 972 973 if (ofs >= strlen(name)) 974 n = -1; 975 else { 976 n = strlen(name) - ofs; 977 VG_(memcpy) (data, name, n); 978 } 979 980 if (n < 0) 981 write_enn (arg_own_buf); 982 else if (n > len) 983 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1); 984 else 985 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0); 986 987 free (data); 988 989 return; 990 } 991 992 if (strncmp ("qXfer:siginfo:read:", arg_own_buf, 19) == 0) { 993 vki_siginfo_t info; 994 int n; 995 CORE_ADDR ofs; 996 unsigned int len; 997 const char *annex; 998 999 /* Reject any annex; grab the offset and length. */ 1000 if (decode_xfer_read (arg_own_buf + 19, &annex, &ofs, &len) < 0 1001 || annex[0] != '\0') { 1002 strcpy (arg_own_buf, "E00"); 1003 return; 1004 } 1005 1006 if (len > PBUFSIZ - POVERHSIZ) 1007 len = PBUFSIZ - POVERHSIZ; 1008 1009 gdbserver_pending_signal_to_report(&info); 1010 1011 if (ofs >= sizeof(info)) 1012 n = -1; 1013 else 1014 n = sizeof(info) - ofs; 1015 1016 if (n < 0) 1017 write_enn (arg_own_buf); 1018 else if (n > len) 1019 *new_packet_len_p = write_qxfer_response (arg_own_buf, 1020 (unsigned char *)&info, 1021 len, 1); 1022 else 1023 *new_packet_len_p = write_qxfer_response (arg_own_buf, 1024 (unsigned char *)&info, 1025 n, 0); 1026 1027 return; 1028 } 1029 1030 /* Protocol features query. */ 1031 if (strncmp ("qSupported", arg_own_buf, 10) == 0 1032 && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) { 1033 VG_(sprintf) (arg_own_buf, "PacketSize=%x", (UInt)PBUFSIZ - 1); 1034 /* Note: max packet size including frame and checksum, but without 1035 trailing null byte, which is not sent/received. */ 1036 1037 strcat (arg_own_buf, ";QStartNoAckMode+"); 1038 strcat (arg_own_buf, ";QPassSignals+"); 1039 if (VG_(client_auxv)) 1040 strcat (arg_own_buf, ";qXfer:auxv:read+"); 1041 1042 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL) { 1043 strcat (arg_own_buf, ";qXfer:features:read+"); 1044 /* if a new gdb connects to us, we have to reset the register 1045 set to the normal register sets to allow this new gdb to 1046 decide to use or not the shadow registers. 1047 1048 Note that the reset is only done for gdb that are sending 1049 qSupported packets. If a user first connected with a recent 1050 gdb using shadow registers and then with a very old gdb 1051 that does not use qSupported packet, then the old gdb will 1052 not properly connect. */ 1053 initialize_shadow_low(False); 1054 } 1055 strcat (arg_own_buf, ";qXfer:exec-file:read+"); 1056 strcat (arg_own_buf, ";qXfer:siginfo:read+"); 1057 return; 1058 } 1059 1060 /* Otherwise we didn't know what packet it was. Say we didn't 1061 understand it. */ 1062 arg_own_buf[0] = 0; 1063 } 1064 1065 /* Handle all of the extended 'v' packets. */ 1066 static 1067 void handle_v_requests (char *arg_own_buf, char *status, int *zignal) 1068 { 1069 /* vcont packet code from gdb 6.6 removed */ 1070 1071 /* Otherwise we didn't know what packet it was. Say we didn't 1072 understand it. */ 1073 arg_own_buf[0] = 0; 1074 return; 1075 } 1076 1077 static 1078 void myresume (int step, int sig) 1079 { 1080 struct thread_resume resume_info[2]; 1081 int n = 0; 1082 1083 if (step || sig) { 1084 resume_info[0].step = step; 1085 resume_info[0].sig = sig; 1086 n++; 1087 } 1088 resume_info[n].step = 0; 1089 resume_info[n].sig = 0; 1090 1091 resume_reply_packet_needed = True; 1092 valgrind_resume (resume_info); 1093 } 1094 1095 /* server_main global variables */ 1096 static char *own_buf; 1097 static unsigned char *mem_buf; 1098 1099 void gdbserver_init (void) 1100 { 1101 dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version); 1102 noack_mode = False; 1103 valgrind_initialize_target (); 1104 // After a fork, gdbserver_init can be called again. 1105 // We do not have to re-malloc the buffers in such a case. 1106 if (own_buf == NULL) 1107 own_buf = malloc (PBUFSIZ+POVERHSIZ); 1108 if (mem_buf == NULL) 1109 mem_buf = malloc (PBUFSIZ+POVERHSIZ); 1110 // Note: normally, we should only malloc PBUFSIZ. However, 1111 // GDB has a bug, and in some cases, sends e.g. 'm' packets 1112 // asking for slightly more than the PacketSize given at 1113 // connection initialisation. So, we bypass the GDB bug 1114 // by allocating slightly more. 1115 } 1116 1117 void gdbserver_terminate (void) 1118 { 1119 /* last call to gdbserver is cleanup call */ 1120 if (VG_MINIMAL_SETJMP(toplevel)) { 1121 dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n"); 1122 return; 1123 } 1124 remote_close(); 1125 } 1126 1127 void server_main (void) 1128 { 1129 static char status; 1130 static int zignal; 1131 1132 char ch; 1133 int i = 0; 1134 unsigned int len; 1135 CORE_ADDR mem_addr; 1136 1137 zignal = valgrind_wait (&status); 1138 if (VG_MINIMAL_SETJMP(toplevel)) { 1139 dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n"); 1140 } 1141 while (1) { 1142 unsigned char sig; 1143 int packet_len; 1144 int new_packet_len = -1; 1145 1146 if (resume_reply_packet_needed) { 1147 /* Send the resume reply to reply to last GDB resume 1148 request. */ 1149 resume_reply_packet_needed = False; 1150 prepare_resume_reply (own_buf, status, zignal); 1151 putpkt (own_buf); 1152 } 1153 1154 /* If our status is terminal (exit or fatal signal) get out 1155 as quickly as we can. We won't be able to handle any request 1156 anymore. */ 1157 if (status == 'W' || status == 'X') { 1158 return; 1159 } 1160 1161 packet_len = getpkt (own_buf); 1162 if (packet_len <= 0) 1163 break; 1164 1165 i = 0; 1166 ch = own_buf[i++]; 1167 switch (ch) { 1168 case 'Q': 1169 handle_set (own_buf, &new_packet_len); 1170 break; 1171 case 'q': 1172 handle_query (own_buf, &new_packet_len); 1173 break; 1174 case 'd': 1175 /* set/unset debugging is done through valgrind debug level. */ 1176 own_buf[0] = '\0'; 1177 break; 1178 case 'D': 1179 reset_valgrind_sink("gdb detaching from process"); 1180 1181 /* When detaching or kill the process, gdb expects to get 1182 an packet OK back. Any other output will make gdb 1183 believes detach did not work. */ 1184 write_ok (own_buf); 1185 putpkt (own_buf); 1186 remote_finish (reset_after_error); 1187 remote_open (VG_(clo_vgdb_prefix)); 1188 myresume (0, 0); 1189 resume_reply_packet_needed = False; 1190 return; 1191 case '!': 1192 /* We can not use the extended protocol with valgrind, 1193 because we can not restart the running 1194 program. So return unrecognized. */ 1195 own_buf[0] = '\0'; 1196 break; 1197 case '?': 1198 prepare_resume_reply (own_buf, status, zignal); 1199 break; 1200 case 'H': 1201 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') { 1202 unsigned long gdb_id, thread_id; 1203 1204 gdb_id = strtoul (&own_buf[2], NULL, 16); 1205 thread_id = gdb_id_to_thread_id (gdb_id); 1206 if (thread_id == 0) { 1207 write_enn (own_buf); 1208 break; 1209 } 1210 1211 if (own_buf[1] == 'g') { 1212 general_thread = thread_id; 1213 set_desired_inferior (1); 1214 } else if (own_buf[1] == 'c') { 1215 cont_thread = thread_id; 1216 } else if (own_buf[1] == 's') { 1217 step_thread = thread_id; 1218 } 1219 1220 write_ok (own_buf); 1221 } else { 1222 /* Silently ignore it so that gdb can extend the protocol 1223 without compatibility headaches. */ 1224 own_buf[0] = '\0'; 1225 } 1226 break; 1227 case 'g': 1228 set_desired_inferior (1); 1229 registers_to_string (own_buf); 1230 break; 1231 case 'G': 1232 set_desired_inferior (1); 1233 registers_from_string (&own_buf[1]); 1234 write_ok (own_buf); 1235 break; 1236 case 'P': { 1237 int regno; 1238 char *regbytes; 1239 Bool mod; 1240 ThreadState *tst; 1241 regno = strtol(&own_buf[1], NULL, 16); 1242 regbytes = strchr(&own_buf[0], '=') + 1; 1243 set_desired_inferior (1); 1244 tst = (ThreadState *) inferior_target_data (current_inferior); 1245 /* Only accept changing registers in "runnable state3. 1246 In fact, it would be ok to change most of the registers 1247 except a few "sensitive" registers such as the PC, SP, BP. 1248 We assume we do not need to very specific here, and that we 1249 can just refuse all of these. */ 1250 if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) { 1251 supply_register_from_string (regno, regbytes, &mod); 1252 write_ok (own_buf); 1253 } else { 1254 /* at least from gdb 6.6 onwards, an E. error 1255 reply is shown to the user. So, we do an error 1256 msg which both is accepted by gdb as an error msg 1257 and is readable by the user. */ 1258 VG_(sprintf) 1259 (own_buf, 1260 "E.\n" 1261 "ERROR changing register %s regno %d\n" 1262 "gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n" 1263 "set pc, calling from gdb a function in the debugged process, ...)\n" 1264 "can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n" 1265 "Thread status is %s\n", 1266 find_register_by_number (regno)->name, regno, 1267 VG_(name_of_ThreadStatus)(tst->status)); 1268 if (VG_(clo_verbosity) > 1) 1269 VG_(umsg) ("%s\n", own_buf); 1270 } 1271 break; 1272 } 1273 case 'm': 1274 decode_m_packet (&own_buf[1], &mem_addr, &len); 1275 if (valgrind_read_memory (mem_addr, mem_buf, len) == 0) 1276 convert_int_to_ascii (mem_buf, own_buf, len); 1277 else 1278 write_enn (own_buf); 1279 break; 1280 case 'M': 1281 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); 1282 if (valgrind_write_memory (mem_addr, mem_buf, len) == 0) 1283 write_ok (own_buf); 1284 else 1285 write_enn (own_buf); 1286 break; 1287 case 'X': 1288 if (decode_X_packet (&own_buf[1], packet_len - 1, 1289 &mem_addr, &len, mem_buf) < 0 1290 || valgrind_write_memory (mem_addr, mem_buf, len) != 0) 1291 write_enn (own_buf); 1292 else 1293 write_ok (own_buf); 1294 break; 1295 case 'C': 1296 convert_ascii_to_int (own_buf + 1, &sig, 1); 1297 if (target_signal_to_host_p (sig)) 1298 zignal = target_signal_to_host (sig); 1299 else 1300 zignal = 0; 1301 set_desired_inferior (0); 1302 myresume (0, zignal); 1303 return; // return control to valgrind 1304 case 'S': 1305 convert_ascii_to_int (own_buf + 1, &sig, 1); 1306 if (target_signal_to_host_p (sig)) 1307 zignal = target_signal_to_host (sig); 1308 else 1309 zignal = 0; 1310 set_desired_inferior (0); 1311 myresume (1, zignal); 1312 return; // return control to valgrind 1313 case 'c': 1314 set_desired_inferior (0); 1315 myresume (0, 0); 1316 return; // return control to valgrind 1317 case 's': 1318 set_desired_inferior (0); 1319 myresume (1, 0); 1320 return; // return control to valgrind 1321 case 'Z': { 1322 char *lenptr; 1323 char *dataptr; 1324 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); 1325 int zlen = strtol (lenptr + 1, &dataptr, 16); 1326 char type = own_buf[1]; 1327 1328 if (type < '0' || type > '4') { 1329 /* Watchpoint command type unrecognized. */ 1330 own_buf[0] = '\0'; 1331 } else { 1332 int res; 1333 1334 res = valgrind_insert_watchpoint (type, addr, zlen); 1335 if (res == 0) 1336 write_ok (own_buf); 1337 else if (res == 1) 1338 /* Unsupported. */ 1339 own_buf[0] = '\0'; 1340 else 1341 write_enn (own_buf); 1342 } 1343 break; 1344 } 1345 case 'z': { 1346 char *lenptr; 1347 char *dataptr; 1348 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); 1349 int zlen = strtol (lenptr + 1, &dataptr, 16); 1350 char type = own_buf[1]; 1351 1352 if (type < '0' || type > '4') { 1353 /* Watchpoint command type unrecognized. */ 1354 own_buf[0] = '\0'; 1355 } else { 1356 int res; 1357 1358 res = valgrind_remove_watchpoint (type, addr, zlen); 1359 if (res == 0) 1360 write_ok (own_buf); 1361 else if (res == 1) 1362 /* Unsupported. */ 1363 own_buf[0] = '\0'; 1364 else 1365 write_enn (own_buf); 1366 } 1367 break; 1368 } 1369 case 'k': 1370 kill_request("Gdb request to kill this process\n"); 1371 break; 1372 case 'T': { 1373 unsigned long gdb_id, thread_id; 1374 1375 gdb_id = strtoul (&own_buf[1], NULL, 16); 1376 thread_id = gdb_id_to_thread_id (gdb_id); 1377 if (thread_id == 0) { 1378 write_enn (own_buf); 1379 break; 1380 } 1381 1382 if (valgrind_thread_alive (thread_id)) 1383 write_ok (own_buf); 1384 else 1385 write_enn (own_buf); 1386 break; 1387 } 1388 case 'R': 1389 /* Restarting the inferior is only supported in the 1390 extended protocol. 1391 => It is a request we don't understand. Respond with an 1392 empty packet so that gdb knows that we don't support this 1393 request. */ 1394 own_buf[0] = '\0'; 1395 break; 1396 case 'v': 1397 /* Extended (long) request. */ 1398 handle_v_requests (own_buf, &status, &zignal); 1399 break; 1400 default: 1401 /* It is a request we don't understand. Respond with an 1402 empty packet so that gdb knows that we don't support this 1403 request. */ 1404 own_buf[0] = '\0'; 1405 break; 1406 } 1407 1408 if (new_packet_len != -1) 1409 putpkt_binary (own_buf, new_packet_len); 1410 else 1411 putpkt (own_buf); 1412 1413 if (status == 'W') 1414 VG_(umsg) ("\nChild exited with status %d\n", zignal); 1415 if (status == 'X') 1416 VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n", 1417 (UInt)target_signal_to_host (zignal), 1418 target_signal_to_name (zignal)); 1419 if (status == 'W' || status == 'X') { 1420 VG_(umsg) ("Process exiting\n"); 1421 VG_(exit) (0); 1422 } 1423 } 1424 1425 /* We come here when getpkt fails => close the connection, 1426 and re-open. Then return control to valgrind. 1427 We return the control to valgrind as we assume that 1428 the connection was closed due to vgdb having finished 1429 to execute a command. */ 1430 if (VG_(clo_verbosity) > 1) 1431 VG_(umsg) ("Remote side has terminated connection. " 1432 "GDBserver will reopen the connection.\n"); 1433 remote_finish (reset_after_error); 1434 remote_open (VG_(clo_vgdb_prefix)); 1435 myresume (0, 0); 1436 resume_reply_packet_needed = False; 1437 return; 1438 } 1439