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