| 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, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include "server.h" |
| 21 | |
| 22 | #if HAVE_UNISTD_H |
| 23 | #include <unistd.h> |
| 24 | #endif |
| 25 | #if HAVE_SIGNAL_H |
| 26 | #include <signal.h> |
| 27 | #endif |
| 28 | #if HAVE_SYS_WAIT_H |
| 29 | #include <sys/wait.h> |
| 30 | #endif |
| 31 | #if HAVE_MALLOC_H |
| 32 | #include <malloc.h> |
| 33 | #endif |
| 34 | |
| 35 | ptid_t cont_thread; |
| 36 | ptid_t general_thread; |
| 37 | ptid_t step_thread; |
| 38 | |
| 39 | int server_waiting; |
| 40 | |
| 41 | static int extended_protocol; |
| 42 | static int response_needed; |
| 43 | static int exit_requested; |
| 44 | |
| 45 | int multi_process; |
| 46 | int non_stop; |
| 47 | |
| 48 | static char **program_argv, **wrapper_argv; |
| 49 | |
| 50 | /* Enable miscellaneous debugging output. The name is historical - it |
| 51 | was originally used to debug LinuxThreads support. */ |
| 52 | int debug_threads; |
| 53 | |
| 54 | /* Enable debugging of h/w breakpoint/watchpoint support. */ |
| 55 | int debug_hw_points; |
| 56 | |
| 57 | int pass_signals[TARGET_SIGNAL_LAST]; |
| 58 | |
| 59 | jmp_buf toplevel; |
| 60 | |
| 61 | const char *gdbserver_xmltarget; |
| 62 | |
| 63 | /* The PID of the originally created or attached inferior. Used to |
| 64 | send signals to the process when GDB sends us an asynchronous interrupt |
| 65 | (user hitting Control-C in the client), and to wait for the child to exit |
| 66 | when no longer debugging it. */ |
| 67 | |
| 68 | unsigned long signal_pid; |
| 69 | |
| 70 | #ifdef SIGTTOU |
| 71 | /* A file descriptor for the controlling terminal. */ |
| 72 | int terminal_fd; |
| 73 | |
| 74 | /* TERMINAL_FD's original foreground group. */ |
| 75 | pid_t old_foreground_pgrp; |
| 76 | |
| 77 | /* Hand back terminal ownership to the original foreground group. */ |
| 78 | |
| 79 | static void |
| 80 | restore_old_foreground_pgrp (void) |
| 81 | { |
| 82 | tcsetpgrp (terminal_fd, old_foreground_pgrp); |
| 83 | } |
| 84 | #endif |
| 85 | |
| 86 | /* Set if you want to disable optional thread related packets support |
| 87 | in gdbserver, for the sake of testing GDB against stubs that don't |
| 88 | support them. */ |
| 89 | int disable_packet_vCont; |
| 90 | int disable_packet_Tthread; |
| 91 | int disable_packet_qC; |
| 92 | int disable_packet_qfThreadInfo; |
| 93 | |
| 94 | /* Last status reported to GDB. */ |
| 95 | static struct target_waitstatus last_status; |
| 96 | static ptid_t last_ptid; |
| 97 | |
| 98 | static char *own_buf; |
| 99 | static unsigned char *mem_buf; |
| 100 | |
| 101 | /* Structure holding information relative to a single stop reply. We |
| 102 | keep a queue of these (really a singly-linked list) to push to GDB |
| 103 | in non-stop mode. */ |
| 104 | struct vstop_notif |
| 105 | { |
| 106 | /* Pointer to next in list. */ |
| 107 | struct vstop_notif *next; |
| 108 | |
| 109 | /* Thread or process that got the event. */ |
| 110 | ptid_t ptid; |
| 111 | |
| 112 | /* Event info. */ |
| 113 | struct target_waitstatus status; |
| 114 | }; |
| 115 | |
| 116 | /* The pending stop replies list head. */ |
| 117 | static struct vstop_notif *notif_queue = NULL; |
| 118 | |
| 119 | /* Put a stop reply to the stop reply queue. */ |
| 120 | |
| 121 | static void |
| 122 | queue_stop_reply (ptid_t ptid, struct target_waitstatus *status) |
| 123 | { |
| 124 | struct vstop_notif *new_notif; |
| 125 | |
| 126 | new_notif = malloc (sizeof (*new_notif)); |
| 127 | new_notif->next = NULL; |
| 128 | new_notif->ptid = ptid; |
| 129 | new_notif->status = *status; |
| 130 | |
| 131 | if (notif_queue) |
| 132 | { |
| 133 | struct vstop_notif *tail; |
| 134 | for (tail = notif_queue; |
| 135 | tail && tail->next; |
| 136 | tail = tail->next) |
| 137 | ; |
| 138 | tail->next = new_notif; |
| 139 | } |
| 140 | else |
| 141 | notif_queue = new_notif; |
| 142 | |
| 143 | if (remote_debug) |
| 144 | { |
| 145 | int i = 0; |
| 146 | struct vstop_notif *n; |
| 147 | |
| 148 | for (n = notif_queue; n; n = n->next) |
| 149 | i++; |
| 150 | |
| 151 | fprintf (stderr, "pending stop replies: %d\n", i); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /* Place an event in the stop reply queue, and push a notification if |
| 156 | we aren't sending one yet. */ |
| 157 | |
| 158 | void |
| 159 | push_event (ptid_t ptid, struct target_waitstatus *status) |
| 160 | { |
| 161 | gdb_assert (status->kind != TARGET_WAITKIND_IGNORE); |
| 162 | |
| 163 | queue_stop_reply (ptid, status); |
| 164 | |
| 165 | /* If this is the first stop reply in the queue, then inform GDB |
| 166 | about it, by sending a Stop notification. */ |
| 167 | if (notif_queue->next == NULL) |
| 168 | { |
| 169 | char *p = own_buf; |
| 170 | strcpy (p, "Stop:"); |
| 171 | p += strlen (p); |
| 172 | prepare_resume_reply (p, |
| 173 | notif_queue->ptid, ¬if_queue->status); |
| 174 | putpkt_notif (own_buf); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /* Get rid of the currently pending stop replies for PID. If PID is |
| 179 | -1, then apply to all processes. */ |
| 180 | |
| 181 | static void |
| 182 | discard_queued_stop_replies (int pid) |
| 183 | { |
| 184 | struct vstop_notif *prev = NULL, *reply, *next; |
| 185 | |
| 186 | for (reply = notif_queue; reply; reply = next) |
| 187 | { |
| 188 | next = reply->next; |
| 189 | |
| 190 | if (pid == -1 |
| 191 | || ptid_get_pid (reply->ptid) == pid) |
| 192 | { |
| 193 | if (reply == notif_queue) |
| 194 | notif_queue = next; |
| 195 | else |
| 196 | prev->next = reply->next; |
| 197 | |
| 198 | free (reply); |
| 199 | } |
| 200 | else |
| 201 | prev = reply; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /* If there are more stop replies to push, push one now. */ |
| 206 | |
| 207 | static void |
| 208 | send_next_stop_reply (char *own_buf) |
| 209 | { |
| 210 | if (notif_queue) |
| 211 | prepare_resume_reply (own_buf, |
| 212 | notif_queue->ptid, |
| 213 | ¬if_queue->status); |
| 214 | else |
| 215 | write_ok (own_buf); |
| 216 | } |
| 217 | |
| 218 | static int |
| 219 | target_running (void) |
| 220 | { |
| 221 | return all_threads.head != NULL; |
| 222 | } |
| 223 | |
| 224 | static int |
| 225 | start_inferior (char **argv) |
| 226 | { |
| 227 | char **new_argv = argv; |
| 228 | |
| 229 | if (wrapper_argv != NULL) |
| 230 | { |
| 231 | int i, count = 1; |
| 232 | |
| 233 | for (i = 0; wrapper_argv[i] != NULL; i++) |
| 234 | count++; |
| 235 | for (i = 0; argv[i] != NULL; i++) |
| 236 | count++; |
| 237 | new_argv = alloca (sizeof (char *) * count); |
| 238 | count = 0; |
| 239 | for (i = 0; wrapper_argv[i] != NULL; i++) |
| 240 | new_argv[count++] = wrapper_argv[i]; |
| 241 | for (i = 0; argv[i] != NULL; i++) |
| 242 | new_argv[count++] = argv[i]; |
| 243 | new_argv[count] = NULL; |
| 244 | } |
| 245 | |
| 246 | if (debug_threads) |
| 247 | { |
| 248 | int i; |
| 249 | for (i = 0; new_argv[i]; ++i) |
| 250 | fprintf (stderr, "new_argv[%d] = \"%s\"\n", i, new_argv[i]); |
| 251 | fflush (stderr); |
| 252 | } |
| 253 | |
| 254 | #ifdef SIGTTOU |
| 255 | signal (SIGTTOU, SIG_DFL); |
| 256 | signal (SIGTTIN, SIG_DFL); |
| 257 | #endif |
| 258 | |
| 259 | signal_pid = create_inferior (new_argv[0], new_argv); |
| 260 | |
| 261 | /* FIXME: we don't actually know at this point that the create |
| 262 | actually succeeded. We won't know that until we wait. */ |
| 263 | fprintf (stderr, "Process %s created; pid = %ld\n", argv[0], |
| 264 | signal_pid); |
| 265 | fflush (stderr); |
| 266 | |
| 267 | #ifdef SIGTTOU |
| 268 | signal (SIGTTOU, SIG_IGN); |
| 269 | signal (SIGTTIN, SIG_IGN); |
| 270 | terminal_fd = fileno (stderr); |
| 271 | old_foreground_pgrp = tcgetpgrp (terminal_fd); |
| 272 | tcsetpgrp (terminal_fd, signal_pid); |
| 273 | atexit (restore_old_foreground_pgrp); |
| 274 | #endif |
| 275 | |
| 276 | if (wrapper_argv != NULL) |
| 277 | { |
| 278 | struct thread_resume resume_info; |
| 279 | ptid_t ptid; |
| 280 | |
| 281 | resume_info.thread = pid_to_ptid (signal_pid); |
| 282 | resume_info.kind = resume_continue; |
| 283 | resume_info.sig = 0; |
| 284 | |
| 285 | ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0); |
| 286 | |
| 287 | if (last_status.kind != TARGET_WAITKIND_STOPPED) |
| 288 | return signal_pid; |
| 289 | |
| 290 | do |
| 291 | { |
| 292 | (*the_target->resume) (&resume_info, 1); |
| 293 | |
| 294 | mywait (pid_to_ptid (signal_pid), &last_status, 0, 0); |
| 295 | if (last_status.kind != TARGET_WAITKIND_STOPPED) |
| 296 | return signal_pid; |
| 297 | |
| 298 | current_inferior->last_resume_kind = resume_stop; |
| 299 | current_inferior->last_status = last_status; |
| 300 | } |
| 301 | while (last_status.value.sig != TARGET_SIGNAL_TRAP); |
| 302 | |
| 303 | current_inferior->last_resume_kind = resume_stop; |
| 304 | current_inferior->last_status = last_status; |
| 305 | return signal_pid; |
| 306 | } |
| 307 | |
| 308 | /* Wait till we are at 1st instruction in program, return new pid |
| 309 | (assuming success). */ |
| 310 | last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0); |
| 311 | |
| 312 | if (last_status.kind != TARGET_WAITKIND_EXITED |
| 313 | && last_status.kind != TARGET_WAITKIND_SIGNALLED) |
| 314 | { |
| 315 | current_inferior->last_resume_kind = resume_stop; |
| 316 | current_inferior->last_status = last_status; |
| 317 | } |
| 318 | |
| 319 | return signal_pid; |
| 320 | } |
| 321 | |
| 322 | static int |
| 323 | attach_inferior (int pid) |
| 324 | { |
| 325 | /* myattach should return -1 if attaching is unsupported, |
| 326 | 0 if it succeeded, and call error() otherwise. */ |
| 327 | |
| 328 | if (myattach (pid) != 0) |
| 329 | return -1; |
| 330 | |
| 331 | fprintf (stderr, "Attached; pid = %d\n", pid); |
| 332 | fflush (stderr); |
| 333 | |
| 334 | /* FIXME - It may be that we should get the SIGNAL_PID from the |
| 335 | attach function, so that it can be the main thread instead of |
| 336 | whichever we were told to attach to. */ |
| 337 | signal_pid = pid; |
| 338 | |
| 339 | if (!non_stop) |
| 340 | { |
| 341 | last_ptid = mywait (pid_to_ptid (pid), &last_status, 0, 0); |
| 342 | |
| 343 | /* GDB knows to ignore the first SIGSTOP after attaching to a running |
| 344 | process using the "attach" command, but this is different; it's |
| 345 | just using "target remote". Pretend it's just starting up. */ |
| 346 | if (last_status.kind == TARGET_WAITKIND_STOPPED |
| 347 | && last_status.value.sig == TARGET_SIGNAL_STOP) |
| 348 | last_status.value.sig = TARGET_SIGNAL_TRAP; |
| 349 | |
| 350 | current_inferior->last_resume_kind = resume_stop; |
| 351 | current_inferior->last_status = last_status; |
| 352 | } |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | extern int remote_debug; |
| 358 | |
| 359 | /* Decode a qXfer read request. Return 0 if everything looks OK, |
| 360 | or -1 otherwise. */ |
| 361 | |
| 362 | static int |
| 363 | decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len) |
| 364 | { |
| 365 | /* Extract and NUL-terminate the annex. */ |
| 366 | *annex = buf; |
| 367 | while (*buf && *buf != ':') |
| 368 | buf++; |
| 369 | if (*buf == '\0') |
| 370 | return -1; |
| 371 | *buf++ = 0; |
| 372 | |
| 373 | /* After the read marker and annex, qXfer looks like a |
| 374 | traditional 'm' packet. */ |
| 375 | decode_m_packet (buf, ofs, len); |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | /* Write the response to a successful qXfer read. Returns the |
| 381 | length of the (binary) data stored in BUF, corresponding |
| 382 | to as much of DATA/LEN as we could fit. IS_MORE controls |
| 383 | the first character of the response. */ |
| 384 | static int |
| 385 | write_qxfer_response (char *buf, const void *data, int len, int is_more) |
| 386 | { |
| 387 | int out_len; |
| 388 | |
| 389 | if (is_more) |
| 390 | buf[0] = 'm'; |
| 391 | else |
| 392 | buf[0] = 'l'; |
| 393 | |
| 394 | return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len, |
| 395 | PBUFSIZ - 2) + 1; |
| 396 | } |
| 397 | |
| 398 | /* Handle all of the extended 'Q' packets. */ |
| 399 | |
| 400 | static void |
| 401 | handle_general_set (char *own_buf) |
| 402 | { |
| 403 | if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0) |
| 404 | { |
| 405 | int numsigs = (int) TARGET_SIGNAL_LAST, i; |
| 406 | const char *p = own_buf + strlen ("QPassSignals:"); |
| 407 | CORE_ADDR cursig; |
| 408 | |
| 409 | p = decode_address_to_semicolon (&cursig, p); |
| 410 | for (i = 0; i < numsigs; i++) |
| 411 | { |
| 412 | if (i == cursig) |
| 413 | { |
| 414 | pass_signals[i] = 1; |
| 415 | if (*p == '\0') |
| 416 | /* Keep looping, to clear the remaining signals. */ |
| 417 | cursig = -1; |
| 418 | else |
| 419 | p = decode_address_to_semicolon (&cursig, p); |
| 420 | } |
| 421 | else |
| 422 | pass_signals[i] = 0; |
| 423 | } |
| 424 | strcpy (own_buf, "OK"); |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | if (strcmp (own_buf, "QStartNoAckMode") == 0) |
| 429 | { |
| 430 | if (remote_debug) |
| 431 | { |
| 432 | fprintf (stderr, "[noack mode enabled]\n"); |
| 433 | fflush (stderr); |
| 434 | } |
| 435 | |
| 436 | noack_mode = 1; |
| 437 | write_ok (own_buf); |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | if (strncmp (own_buf, "QNonStop:", 9) == 0) |
| 442 | { |
| 443 | char *mode = own_buf + 9; |
| 444 | int req = -1; |
| 445 | char *req_str; |
| 446 | |
| 447 | if (strcmp (mode, "0") == 0) |
| 448 | req = 0; |
| 449 | else if (strcmp (mode, "1") == 0) |
| 450 | req = 1; |
| 451 | else |
| 452 | { |
| 453 | /* We don't know what this mode is, so complain to |
| 454 | GDB. */ |
| 455 | fprintf (stderr, "Unknown non-stop mode requested: %s\n", |
| 456 | own_buf); |
| 457 | write_enn (own_buf); |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | req_str = req ? "non-stop" : "all-stop"; |
| 462 | if (start_non_stop (req) != 0) |
| 463 | { |
| 464 | fprintf (stderr, "Setting %s mode failed\n", req_str); |
| 465 | write_enn (own_buf); |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | non_stop = req; |
| 470 | |
| 471 | if (remote_debug) |
| 472 | fprintf (stderr, "[%s mode enabled]\n", req_str); |
| 473 | |
| 474 | write_ok (own_buf); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | if (target_supports_tracepoints () |
| 479 | && handle_tracepoint_general_set (own_buf)) |
| 480 | return; |
| 481 | |
| 482 | /* Otherwise we didn't know what packet it was. Say we didn't |
| 483 | understand it. */ |
| 484 | own_buf[0] = 0; |
| 485 | } |
| 486 | |
| 487 | static const char * |
| 488 | get_features_xml (const char *annex) |
| 489 | { |
| 490 | /* gdbserver_xmltarget defines what to return when looking |
| 491 | for the "target.xml" file. Its contents can either be |
| 492 | verbatim XML code (prefixed with a '@') or else the name |
| 493 | of the actual XML file to be used in place of "target.xml". |
| 494 | |
| 495 | This variable is set up from the auto-generated |
| 496 | init_registers_... routine for the current target. */ |
| 497 | |
| 498 | if (gdbserver_xmltarget |
| 499 | && strcmp (annex, "target.xml") == 0) |
| 500 | { |
| 501 | if (*gdbserver_xmltarget == '@') |
| 502 | return gdbserver_xmltarget + 1; |
| 503 | else |
| 504 | annex = gdbserver_xmltarget; |
| 505 | } |
| 506 | |
| 507 | #ifdef USE_XML |
| 508 | { |
| 509 | extern const char *const xml_builtin[][2]; |
| 510 | int i; |
| 511 | |
| 512 | /* Look for the annex. */ |
| 513 | for (i = 0; xml_builtin[i][0] != NULL; i++) |
| 514 | if (strcmp (annex, xml_builtin[i][0]) == 0) |
| 515 | break; |
| 516 | |
| 517 | if (xml_builtin[i][0] != NULL) |
| 518 | return xml_builtin[i][1]; |
| 519 | } |
| 520 | #endif |
| 521 | |
| 522 | return NULL; |
| 523 | } |
| 524 | |
| 525 | void |
| 526 | monitor_show_help (void) |
| 527 | { |
| 528 | monitor_output ("The following monitor commands are supported:\n"); |
| 529 | monitor_output (" set debug <0|1>\n"); |
| 530 | monitor_output (" Enable general debugging messages\n"); |
| 531 | monitor_output (" set debug-hw-points <0|1>\n"); |
| 532 | monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n"); |
| 533 | monitor_output (" set remote-debug <0|1>\n"); |
| 534 | monitor_output (" Enable remote protocol debugging messages\n"); |
| 535 | monitor_output (" exit\n"); |
| 536 | monitor_output (" Quit GDBserver\n"); |
| 537 | } |
| 538 | |
| 539 | /* Read trace frame or inferior memory. */ |
| 540 | |
| 541 | static int |
| 542 | gdb_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len) |
| 543 | { |
| 544 | int ret; |
| 545 | |
| 546 | if (current_traceframe >= 0) |
| 547 | { |
| 548 | ULONGEST nbytes; |
| 549 | ULONGEST length = len; |
| 550 | |
| 551 | if (traceframe_read_mem (current_traceframe, |
| 552 | memaddr, myaddr, len, &nbytes)) |
| 553 | return EIO; |
| 554 | /* Data read from trace buffer, we're done. */ |
| 555 | if (nbytes == length) |
| 556 | return 0; |
| 557 | if (!in_readonly_region (memaddr, length)) |
| 558 | return EIO; |
| 559 | /* Otherwise we have a valid readonly case, fall through. */ |
| 560 | /* (assume no half-trace half-real blocks for now) */ |
| 561 | } |
| 562 | |
| 563 | ret = prepare_to_access_memory (); |
| 564 | if (ret == 0) |
| 565 | { |
| 566 | ret = read_inferior_memory (memaddr, myaddr, len); |
| 567 | unprepare_to_access_memory (); |
| 568 | } |
| 569 | |
| 570 | return ret; |
| 571 | } |
| 572 | |
| 573 | /* Write trace frame or inferior memory. Actually, writing to trace |
| 574 | frames is forbidden. */ |
| 575 | |
| 576 | static int |
| 577 | gdb_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len) |
| 578 | { |
| 579 | if (current_traceframe >= 0) |
| 580 | return EIO; |
| 581 | else |
| 582 | { |
| 583 | int ret; |
| 584 | |
| 585 | ret = prepare_to_access_memory (); |
| 586 | if (ret == 0) |
| 587 | { |
| 588 | ret = write_inferior_memory (memaddr, myaddr, len); |
| 589 | unprepare_to_access_memory (); |
| 590 | } |
| 591 | return ret; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /* Subroutine of handle_search_memory to simplify it. */ |
| 596 | |
| 597 | static int |
| 598 | handle_search_memory_1 (CORE_ADDR start_addr, CORE_ADDR search_space_len, |
| 599 | gdb_byte *pattern, unsigned pattern_len, |
| 600 | gdb_byte *search_buf, |
| 601 | unsigned chunk_size, unsigned search_buf_size, |
| 602 | CORE_ADDR *found_addrp) |
| 603 | { |
| 604 | /* Prime the search buffer. */ |
| 605 | |
| 606 | if (gdb_read_memory (start_addr, search_buf, search_buf_size) != 0) |
| 607 | { |
| 608 | warning ("Unable to access target memory at 0x%lx, halting search.", |
| 609 | (long) start_addr); |
| 610 | return -1; |
| 611 | } |
| 612 | |
| 613 | /* Perform the search. |
| 614 | |
| 615 | The loop is kept simple by allocating [N + pattern-length - 1] bytes. |
| 616 | When we've scanned N bytes we copy the trailing bytes to the start and |
| 617 | read in another N bytes. */ |
| 618 | |
| 619 | while (search_space_len >= pattern_len) |
| 620 | { |
| 621 | gdb_byte *found_ptr; |
| 622 | unsigned nr_search_bytes = (search_space_len < search_buf_size |
| 623 | ? search_space_len |
| 624 | : search_buf_size); |
| 625 | |
| 626 | found_ptr = memmem (search_buf, nr_search_bytes, pattern, pattern_len); |
| 627 | |
| 628 | if (found_ptr != NULL) |
| 629 | { |
| 630 | CORE_ADDR found_addr = start_addr + (found_ptr - search_buf); |
| 631 | *found_addrp = found_addr; |
| 632 | return 1; |
| 633 | } |
| 634 | |
| 635 | /* Not found in this chunk, skip to next chunk. */ |
| 636 | |
| 637 | /* Don't let search_space_len wrap here, it's unsigned. */ |
| 638 | if (search_space_len >= chunk_size) |
| 639 | search_space_len -= chunk_size; |
| 640 | else |
| 641 | search_space_len = 0; |
| 642 | |
| 643 | if (search_space_len >= pattern_len) |
| 644 | { |
| 645 | unsigned keep_len = search_buf_size - chunk_size; |
| 646 | CORE_ADDR read_addr = start_addr + chunk_size + keep_len; |
| 647 | int nr_to_read; |
| 648 | |
| 649 | /* Copy the trailing part of the previous iteration to the front |
| 650 | of the buffer for the next iteration. */ |
| 651 | memcpy (search_buf, search_buf + chunk_size, keep_len); |
| 652 | |
| 653 | nr_to_read = (search_space_len - keep_len < chunk_size |
| 654 | ? search_space_len - keep_len |
| 655 | : chunk_size); |
| 656 | |
| 657 | if (gdb_read_memory (read_addr, search_buf + keep_len, |
| 658 | nr_to_read) != 0) |
| 659 | { |
| 660 | warning ("Unable to access target memory at 0x%lx, halting search.", |
| 661 | (long) read_addr); |
| 662 | return -1; |
| 663 | } |
| 664 | |
| 665 | start_addr += chunk_size; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | /* Not found. */ |
| 670 | |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | /* Handle qSearch:memory packets. */ |
| 675 | |
| 676 | static void |
| 677 | handle_search_memory (char *own_buf, int packet_len) |
| 678 | { |
| 679 | CORE_ADDR start_addr; |
| 680 | CORE_ADDR search_space_len; |
| 681 | gdb_byte *pattern; |
| 682 | unsigned int pattern_len; |
| 683 | /* NOTE: also defined in find.c testcase. */ |
| 684 | #define SEARCH_CHUNK_SIZE 16000 |
| 685 | const unsigned chunk_size = SEARCH_CHUNK_SIZE; |
| 686 | /* Buffer to hold memory contents for searching. */ |
| 687 | gdb_byte *search_buf; |
| 688 | unsigned search_buf_size; |
| 689 | int found; |
| 690 | CORE_ADDR found_addr; |
| 691 | int cmd_name_len = sizeof ("qSearch:memory:") - 1; |
| 692 | |
| 693 | pattern = malloc (packet_len); |
| 694 | if (pattern == NULL) |
| 695 | { |
| 696 | error ("Unable to allocate memory to perform the search"); |
| 697 | strcpy (own_buf, "E00"); |
| 698 | return; |
| 699 | } |
| 700 | if (decode_search_memory_packet (own_buf + cmd_name_len, |
| 701 | packet_len - cmd_name_len, |
| 702 | &start_addr, &search_space_len, |
| 703 | pattern, &pattern_len) < 0) |
| 704 | { |
| 705 | free (pattern); |
| 706 | error ("Error in parsing qSearch:memory packet"); |
| 707 | strcpy (own_buf, "E00"); |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | search_buf_size = chunk_size + pattern_len - 1; |
| 712 | |
| 713 | /* No point in trying to allocate a buffer larger than the search space. */ |
| 714 | if (search_space_len < search_buf_size) |
| 715 | search_buf_size = search_space_len; |
| 716 | |
| 717 | search_buf = malloc (search_buf_size); |
| 718 | if (search_buf == NULL) |
| 719 | { |
| 720 | free (pattern); |
| 721 | error ("Unable to allocate memory to perform the search"); |
| 722 | strcpy (own_buf, "E00"); |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | found = handle_search_memory_1 (start_addr, search_space_len, |
| 727 | pattern, pattern_len, |
| 728 | search_buf, chunk_size, search_buf_size, |
| 729 | &found_addr); |
| 730 | |
| 731 | if (found > 0) |
| 732 | sprintf (own_buf, "1,%lx", (long) found_addr); |
| 733 | else if (found == 0) |
| 734 | strcpy (own_buf, "0"); |
| 735 | else |
| 736 | strcpy (own_buf, "E00"); |
| 737 | |
| 738 | free (search_buf); |
| 739 | free (pattern); |
| 740 | } |
| 741 | |
| 742 | #define require_running(BUF) \ |
| 743 | if (!target_running ()) \ |
| 744 | { \ |
| 745 | write_enn (BUF); \ |
| 746 | return; \ |
| 747 | } |
| 748 | |
| 749 | /* Handle monitor commands not handled by target-specific handlers. */ |
| 750 | |
| 751 | static void |
| 752 | handle_monitor_command (char *mon) |
| 753 | { |
| 754 | if (strcmp (mon, "set debug 1") == 0) |
| 755 | { |
| 756 | debug_threads = 1; |
| 757 | monitor_output ("Debug output enabled.\n"); |
| 758 | } |
| 759 | else if (strcmp (mon, "set debug 0") == 0) |
| 760 | { |
| 761 | debug_threads = 0; |
| 762 | monitor_output ("Debug output disabled.\n"); |
| 763 | } |
| 764 | else if (strcmp (mon, "set debug-hw-points 1") == 0) |
| 765 | { |
| 766 | debug_hw_points = 1; |
| 767 | monitor_output ("H/W point debugging output enabled.\n"); |
| 768 | } |
| 769 | else if (strcmp (mon, "set debug-hw-points 0") == 0) |
| 770 | { |
| 771 | debug_hw_points = 0; |
| 772 | monitor_output ("H/W point debugging output disabled.\n"); |
| 773 | } |
| 774 | else if (strcmp (mon, "set remote-debug 1") == 0) |
| 775 | { |
| 776 | remote_debug = 1; |
| 777 | monitor_output ("Protocol debug output enabled.\n"); |
| 778 | } |
| 779 | else if (strcmp (mon, "set remote-debug 0") == 0) |
| 780 | { |
| 781 | remote_debug = 0; |
| 782 | monitor_output ("Protocol debug output disabled.\n"); |
| 783 | } |
| 784 | else if (strcmp (mon, "help") == 0) |
| 785 | monitor_show_help (); |
| 786 | else if (strcmp (mon, "exit") == 0) |
| 787 | exit_requested = 1; |
| 788 | else |
| 789 | { |
| 790 | monitor_output ("Unknown monitor command.\n\n"); |
| 791 | monitor_show_help (); |
| 792 | write_enn (own_buf); |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | static void |
| 797 | handle_threads_qxfer_proper (struct buffer *buffer) |
| 798 | { |
| 799 | struct inferior_list_entry *thread; |
| 800 | |
| 801 | buffer_grow_str (buffer, "<threads>\n"); |
| 802 | |
| 803 | for (thread = all_threads.head; thread; thread = thread->next) |
| 804 | { |
| 805 | ptid_t ptid = thread_to_gdb_id ((struct thread_info *)thread); |
| 806 | char ptid_s[100]; |
| 807 | int core = -1; |
| 808 | char core_s[21]; |
| 809 | |
| 810 | write_ptid (ptid_s, ptid); |
| 811 | |
| 812 | if (the_target->core_of_thread) |
| 813 | core = (*the_target->core_of_thread) (ptid); |
| 814 | |
| 815 | if (core != -1) |
| 816 | { |
| 817 | sprintf (core_s, "%d", core); |
| 818 | buffer_xml_printf (buffer, "<thread id=\"%s\" core=\"%s\"/>\n", |
| 819 | ptid_s, core_s); |
| 820 | } |
| 821 | else |
| 822 | { |
| 823 | buffer_xml_printf (buffer, "<thread id=\"%s\"/>\n", |
| 824 | ptid_s); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | buffer_grow_str0 (buffer, "</threads>\n"); |
| 829 | } |
| 830 | |
| 831 | static int |
| 832 | handle_threads_qxfer (const char *annex, |
| 833 | unsigned char *readbuf, |
| 834 | CORE_ADDR offset, int length) |
| 835 | { |
| 836 | static char *result = 0; |
| 837 | static unsigned int result_length = 0; |
| 838 | |
| 839 | if (annex && strcmp (annex, "") != 0) |
| 840 | return 0; |
| 841 | |
| 842 | if (offset == 0) |
| 843 | { |
| 844 | struct buffer buffer; |
| 845 | /* When asked for data at offset 0, generate everything and store into |
| 846 | 'result'. Successive reads will be served off 'result'. */ |
| 847 | if (result) |
| 848 | free (result); |
| 849 | |
| 850 | buffer_init (&buffer); |
| 851 | |
| 852 | handle_threads_qxfer_proper (&buffer); |
| 853 | |
| 854 | result = buffer_finish (&buffer); |
| 855 | result_length = strlen (result); |
| 856 | buffer_free (&buffer); |
| 857 | } |
| 858 | |
| 859 | if (offset >= result_length) |
| 860 | { |
| 861 | /* We're out of data. */ |
| 862 | free (result); |
| 863 | result = NULL; |
| 864 | result_length = 0; |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | if (length > result_length - offset) |
| 869 | length = result_length - offset; |
| 870 | |
| 871 | memcpy (readbuf, result + offset, length); |
| 872 | |
| 873 | return length; |
| 874 | |
| 875 | } |
| 876 | |
| 877 | /* Table used by the crc32 function to calcuate the checksum. */ |
| 878 | |
| 879 | static unsigned int crc32_table[256] = |
| 880 | {0, 0}; |
| 881 | |
| 882 | /* Compute 32 bit CRC from inferior memory. |
| 883 | |
| 884 | On success, return 32 bit CRC. |
| 885 | On failure, return (unsigned long long) -1. */ |
| 886 | |
| 887 | static unsigned long long |
| 888 | crc32 (CORE_ADDR base, int len, unsigned int crc) |
| 889 | { |
| 890 | if (!crc32_table[1]) |
| 891 | { |
| 892 | /* Initialize the CRC table and the decoding table. */ |
| 893 | int i, j; |
| 894 | unsigned int c; |
| 895 | |
| 896 | for (i = 0; i < 256; i++) |
| 897 | { |
| 898 | for (c = i << 24, j = 8; j > 0; --j) |
| 899 | c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1); |
| 900 | crc32_table[i] = c; |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | while (len--) |
| 905 | { |
| 906 | unsigned char byte = 0; |
| 907 | |
| 908 | /* Return failure if memory read fails. */ |
| 909 | if (read_inferior_memory (base, &byte, 1) != 0) |
| 910 | return (unsigned long long) -1; |
| 911 | |
| 912 | crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ byte) & 255]; |
| 913 | base++; |
| 914 | } |
| 915 | return (unsigned long long) crc; |
| 916 | } |
| 917 | |
| 918 | /* Handle all of the extended 'q' packets. */ |
| 919 | void |
| 920 | handle_query (char *own_buf, int packet_len, int *new_packet_len_p) |
| 921 | { |
| 922 | static struct inferior_list_entry *thread_ptr; |
| 923 | |
| 924 | /* Reply the current thread id. */ |
| 925 | if (strcmp ("qC", own_buf) == 0 && !disable_packet_qC) |
| 926 | { |
| 927 | ptid_t gdb_id; |
| 928 | require_running (own_buf); |
| 929 | |
| 930 | if (!ptid_equal (general_thread, null_ptid) |
| 931 | && !ptid_equal (general_thread, minus_one_ptid)) |
| 932 | gdb_id = general_thread; |
| 933 | else |
| 934 | { |
| 935 | thread_ptr = all_threads.head; |
| 936 | gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr); |
| 937 | } |
| 938 | |
| 939 | sprintf (own_buf, "QC"); |
| 940 | own_buf += 2; |
| 941 | own_buf = write_ptid (own_buf, gdb_id); |
| 942 | return; |
| 943 | } |
| 944 | |
| 945 | if (strcmp ("qSymbol::", own_buf) == 0) |
| 946 | { |
| 947 | /* GDB is suggesting new symbols have been loaded. This may |
| 948 | mean a new shared library has been detected as loaded, so |
| 949 | take the opportunity to check if breakpoints we think are |
| 950 | inserted, still are. Note that it isn't guaranteed that |
| 951 | we'll see this when a shared library is loaded, and nor will |
| 952 | we see this for unloads (although breakpoints in unloaded |
| 953 | libraries shouldn't trigger), as GDB may not find symbols for |
| 954 | the library at all. We also re-validate breakpoints when we |
| 955 | see a second GDB breakpoint for the same address, and or when |
| 956 | we access breakpoint shadows. */ |
| 957 | validate_breakpoints (); |
| 958 | |
| 959 | if (target_supports_tracepoints ()) |
| 960 | tracepoint_look_up_symbols (); |
| 961 | |
| 962 | if (target_running () && the_target->look_up_symbols != NULL) |
| 963 | (*the_target->look_up_symbols) (); |
| 964 | |
| 965 | strcpy (own_buf, "OK"); |
| 966 | return; |
| 967 | } |
| 968 | |
| 969 | if (!disable_packet_qfThreadInfo) |
| 970 | { |
| 971 | if (strcmp ("qfThreadInfo", own_buf) == 0) |
| 972 | { |
| 973 | ptid_t gdb_id; |
| 974 | |
| 975 | require_running (own_buf); |
| 976 | thread_ptr = all_threads.head; |
| 977 | |
| 978 | *own_buf++ = 'm'; |
| 979 | gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr); |
| 980 | write_ptid (own_buf, gdb_id); |
| 981 | thread_ptr = thread_ptr->next; |
| 982 | return; |
| 983 | } |
| 984 | |
| 985 | if (strcmp ("qsThreadInfo", own_buf) == 0) |
| 986 | { |
| 987 | ptid_t gdb_id; |
| 988 | |
| 989 | require_running (own_buf); |
| 990 | if (thread_ptr != NULL) |
| 991 | { |
| 992 | *own_buf++ = 'm'; |
| 993 | gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr); |
| 994 | write_ptid (own_buf, gdb_id); |
| 995 | thread_ptr = thread_ptr->next; |
| 996 | return; |
| 997 | } |
| 998 | else |
| 999 | { |
| 1000 | sprintf (own_buf, "l"); |
| 1001 | return; |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | if (the_target->read_offsets != NULL |
| 1007 | && strcmp ("qOffsets", own_buf) == 0) |
| 1008 | { |
| 1009 | CORE_ADDR text, data; |
| 1010 | |
| 1011 | require_running (own_buf); |
| 1012 | if (the_target->read_offsets (&text, &data)) |
| 1013 | sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX", |
| 1014 | (long)text, (long)data, (long)data); |
| 1015 | else |
| 1016 | write_enn (own_buf); |
| 1017 | |
| 1018 | return; |
| 1019 | } |
| 1020 | |
| 1021 | if (the_target->qxfer_spu != NULL |
| 1022 | && strncmp ("qXfer:spu:read:", own_buf, 15) == 0) |
| 1023 | { |
| 1024 | char *annex; |
| 1025 | int n; |
| 1026 | unsigned int len; |
| 1027 | CORE_ADDR ofs; |
| 1028 | unsigned char *spu_buf; |
| 1029 | |
| 1030 | require_running (own_buf); |
| 1031 | strcpy (own_buf, "E00"); |
| 1032 | if (decode_xfer_read (own_buf + 15, &annex, &ofs, &len) < 0) |
| 1033 | return; |
| 1034 | if (len > PBUFSIZ - 2) |
| 1035 | len = PBUFSIZ - 2; |
| 1036 | spu_buf = malloc (len + 1); |
| 1037 | if (!spu_buf) |
| 1038 | return; |
| 1039 | |
| 1040 | n = (*the_target->qxfer_spu) (annex, spu_buf, NULL, ofs, len + 1); |
| 1041 | if (n < 0) |
| 1042 | write_enn (own_buf); |
| 1043 | else if (n > len) |
| 1044 | *new_packet_len_p = write_qxfer_response (own_buf, spu_buf, len, 1); |
| 1045 | else |
| 1046 | *new_packet_len_p = write_qxfer_response (own_buf, spu_buf, n, 0); |
| 1047 | |
| 1048 | free (spu_buf); |
| 1049 | return; |
| 1050 | } |
| 1051 | |
| 1052 | if (the_target->qxfer_spu != NULL |
| 1053 | && strncmp ("qXfer:spu:write:", own_buf, 16) == 0) |
| 1054 | { |
| 1055 | char *annex; |
| 1056 | int n; |
| 1057 | unsigned int len; |
| 1058 | CORE_ADDR ofs; |
| 1059 | unsigned char *spu_buf; |
| 1060 | |
| 1061 | require_running (own_buf); |
| 1062 | strcpy (own_buf, "E00"); |
| 1063 | spu_buf = malloc (packet_len - 15); |
| 1064 | if (!spu_buf) |
| 1065 | return; |
| 1066 | if (decode_xfer_write (own_buf + 16, packet_len - 16, &annex, |
| 1067 | &ofs, &len, spu_buf) < 0) |
| 1068 | { |
| 1069 | free (spu_buf); |
| 1070 | return; |
| 1071 | } |
| 1072 | |
| 1073 | n = (*the_target->qxfer_spu) |
| 1074 | (annex, NULL, (unsigned const char *)spu_buf, ofs, len); |
| 1075 | if (n < 0) |
| 1076 | write_enn (own_buf); |
| 1077 | else |
| 1078 | sprintf (own_buf, "%x", n); |
| 1079 | |
| 1080 | free (spu_buf); |
| 1081 | return; |
| 1082 | } |
| 1083 | |
| 1084 | if (the_target->read_auxv != NULL |
| 1085 | && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0) |
| 1086 | { |
| 1087 | unsigned char *data; |
| 1088 | int n; |
| 1089 | CORE_ADDR ofs; |
| 1090 | unsigned int len; |
| 1091 | char *annex; |
| 1092 | |
| 1093 | require_running (own_buf); |
| 1094 | |
| 1095 | /* Reject any annex; grab the offset and length. */ |
| 1096 | if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0 |
| 1097 | || annex[0] != '\0') |
| 1098 | { |
| 1099 | strcpy (own_buf, "E00"); |
| 1100 | return; |
| 1101 | } |
| 1102 | |
| 1103 | /* Read one extra byte, as an indicator of whether there is |
| 1104 | more. */ |
| 1105 | if (len > PBUFSIZ - 2) |
| 1106 | len = PBUFSIZ - 2; |
| 1107 | data = malloc (len + 1); |
| 1108 | if (data == NULL) |
| 1109 | { |
| 1110 | write_enn (own_buf); |
| 1111 | return; |
| 1112 | } |
| 1113 | n = (*the_target->read_auxv) (ofs, data, len + 1); |
| 1114 | if (n < 0) |
| 1115 | write_enn (own_buf); |
| 1116 | else if (n > len) |
| 1117 | *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1); |
| 1118 | else |
| 1119 | *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0); |
| 1120 | |
| 1121 | free (data); |
| 1122 | |
| 1123 | return; |
| 1124 | } |
| 1125 | |
| 1126 | if (strncmp ("qXfer:features:read:", own_buf, 20) == 0) |
| 1127 | { |
| 1128 | CORE_ADDR ofs; |
| 1129 | unsigned int len, total_len; |
| 1130 | const char *document; |
| 1131 | char *annex; |
| 1132 | |
| 1133 | require_running (own_buf); |
| 1134 | |
| 1135 | /* Grab the annex, offset, and length. */ |
| 1136 | if (decode_xfer_read (own_buf + 20, &annex, &ofs, &len) < 0) |
| 1137 | { |
| 1138 | strcpy (own_buf, "E00"); |
| 1139 | return; |
| 1140 | } |
| 1141 | |
| 1142 | /* Now grab the correct annex. */ |
| 1143 | document = get_features_xml (annex); |
| 1144 | if (document == NULL) |
| 1145 | { |
| 1146 | strcpy (own_buf, "E00"); |
| 1147 | return; |
| 1148 | } |
| 1149 | |
| 1150 | total_len = strlen (document); |
| 1151 | if (len > PBUFSIZ - 2) |
| 1152 | len = PBUFSIZ - 2; |
| 1153 | |
| 1154 | if (ofs > total_len) |
| 1155 | write_enn (own_buf); |
| 1156 | else if (len < total_len - ofs) |
| 1157 | *new_packet_len_p = write_qxfer_response (own_buf, document + ofs, |
| 1158 | len, 1); |
| 1159 | else |
| 1160 | *new_packet_len_p = write_qxfer_response (own_buf, document + ofs, |
| 1161 | total_len - ofs, 0); |
| 1162 | |
| 1163 | return; |
| 1164 | } |
| 1165 | |
| 1166 | if (strncmp ("qXfer:libraries:read:", own_buf, 21) == 0) |
| 1167 | { |
| 1168 | CORE_ADDR ofs; |
| 1169 | unsigned int len, total_len; |
| 1170 | char *document, *p; |
| 1171 | struct inferior_list_entry *dll_ptr; |
| 1172 | char *annex; |
| 1173 | |
| 1174 | require_running (own_buf); |
| 1175 | |
| 1176 | /* Reject any annex; grab the offset and length. */ |
| 1177 | if (decode_xfer_read (own_buf + 21, &annex, &ofs, &len) < 0 |
| 1178 | || annex[0] != '\0') |
| 1179 | { |
| 1180 | strcpy (own_buf, "E00"); |
| 1181 | return; |
| 1182 | } |
| 1183 | |
| 1184 | /* Over-estimate the necessary memory. Assume that every character |
| 1185 | in the library name must be escaped. */ |
| 1186 | total_len = 64; |
| 1187 | for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next) |
| 1188 | total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name); |
| 1189 | |
| 1190 | document = malloc (total_len); |
| 1191 | if (document == NULL) |
| 1192 | { |
| 1193 | write_enn (own_buf); |
| 1194 | return; |
| 1195 | } |
| 1196 | strcpy (document, "<library-list>\n"); |
| 1197 | p = document + strlen (document); |
| 1198 | |
| 1199 | for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next) |
| 1200 | { |
| 1201 | struct dll_info *dll = (struct dll_info *) dll_ptr; |
| 1202 | char *name; |
| 1203 | |
| 1204 | strcpy (p, " <library name=\""); |
| 1205 | p = p + strlen (p); |
| 1206 | name = xml_escape_text (dll->name); |
| 1207 | strcpy (p, name); |
| 1208 | free (name); |
| 1209 | p = p + strlen (p); |
| 1210 | strcpy (p, "\"><segment address=\""); |
| 1211 | p = p + strlen (p); |
| 1212 | sprintf (p, "0x%lx", (long) dll->base_addr); |
| 1213 | p = p + strlen (p); |
| 1214 | strcpy (p, "\"/></library>\n"); |
| 1215 | p = p + strlen (p); |
| 1216 | } |
| 1217 | |
| 1218 | strcpy (p, "</library-list>\n"); |
| 1219 | |
| 1220 | total_len = strlen (document); |
| 1221 | if (len > PBUFSIZ - 2) |
| 1222 | len = PBUFSIZ - 2; |
| 1223 | |
| 1224 | if (ofs > total_len) |
| 1225 | write_enn (own_buf); |
| 1226 | else if (len < total_len - ofs) |
| 1227 | *new_packet_len_p = write_qxfer_response (own_buf, document + ofs, |
| 1228 | len, 1); |
| 1229 | else |
| 1230 | *new_packet_len_p = write_qxfer_response (own_buf, document + ofs, |
| 1231 | total_len - ofs, 0); |
| 1232 | |
| 1233 | free (document); |
| 1234 | return; |
| 1235 | } |
| 1236 | |
| 1237 | if (the_target->qxfer_osdata != NULL |
| 1238 | && strncmp ("qXfer:osdata:read:", own_buf, 18) == 0) |
| 1239 | { |
| 1240 | char *annex; |
| 1241 | int n; |
| 1242 | unsigned int len; |
| 1243 | CORE_ADDR ofs; |
| 1244 | unsigned char *workbuf; |
| 1245 | |
| 1246 | strcpy (own_buf, "E00"); |
| 1247 | if (decode_xfer_read (own_buf + 18, &annex, &ofs, &len) < 0) |
| 1248 | return; |
| 1249 | if (len > PBUFSIZ - 2) |
| 1250 | len = PBUFSIZ - 2; |
| 1251 | workbuf = malloc (len + 1); |
| 1252 | if (!workbuf) |
| 1253 | return; |
| 1254 | |
| 1255 | n = (*the_target->qxfer_osdata) (annex, workbuf, NULL, ofs, len + 1); |
| 1256 | if (n < 0) |
| 1257 | write_enn (own_buf); |
| 1258 | else if (n > len) |
| 1259 | *new_packet_len_p = write_qxfer_response (own_buf, workbuf, len, 1); |
| 1260 | else |
| 1261 | *new_packet_len_p = write_qxfer_response (own_buf, workbuf, n, 0); |
| 1262 | |
| 1263 | free (workbuf); |
| 1264 | return; |
| 1265 | } |
| 1266 | |
| 1267 | if (the_target->qxfer_siginfo != NULL |
| 1268 | && strncmp ("qXfer:siginfo:read:", own_buf, 19) == 0) |
| 1269 | { |
| 1270 | unsigned char *data; |
| 1271 | int n; |
| 1272 | CORE_ADDR ofs; |
| 1273 | unsigned int len; |
| 1274 | char *annex; |
| 1275 | |
| 1276 | require_running (own_buf); |
| 1277 | |
| 1278 | /* Reject any annex; grab the offset and length. */ |
| 1279 | if (decode_xfer_read (own_buf + 19, &annex, &ofs, &len) < 0 |
| 1280 | || annex[0] != '\0') |
| 1281 | { |
| 1282 | strcpy (own_buf, "E00"); |
| 1283 | return; |
| 1284 | } |
| 1285 | |
| 1286 | /* Read one extra byte, as an indicator of whether there is |
| 1287 | more. */ |
| 1288 | if (len > PBUFSIZ - 2) |
| 1289 | len = PBUFSIZ - 2; |
| 1290 | data = malloc (len + 1); |
| 1291 | if (!data) |
| 1292 | return; |
| 1293 | n = (*the_target->qxfer_siginfo) (annex, data, NULL, ofs, len + 1); |
| 1294 | if (n < 0) |
| 1295 | write_enn (own_buf); |
| 1296 | else if (n > len) |
| 1297 | *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1); |
| 1298 | else |
| 1299 | *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0); |
| 1300 | |
| 1301 | free (data); |
| 1302 | return; |
| 1303 | } |
| 1304 | |
| 1305 | if (the_target->qxfer_siginfo != NULL |
| 1306 | && strncmp ("qXfer:siginfo:write:", own_buf, 20) == 0) |
| 1307 | { |
| 1308 | char *annex; |
| 1309 | int n; |
| 1310 | unsigned int len; |
| 1311 | CORE_ADDR ofs; |
| 1312 | unsigned char *data; |
| 1313 | |
| 1314 | require_running (own_buf); |
| 1315 | |
| 1316 | strcpy (own_buf, "E00"); |
| 1317 | data = malloc (packet_len - 19); |
| 1318 | if (!data) |
| 1319 | return; |
| 1320 | if (decode_xfer_write (own_buf + 20, packet_len - 20, &annex, |
| 1321 | &ofs, &len, data) < 0) |
| 1322 | { |
| 1323 | free (data); |
| 1324 | return; |
| 1325 | } |
| 1326 | |
| 1327 | n = (*the_target->qxfer_siginfo) |
| 1328 | (annex, NULL, (unsigned const char *)data, ofs, len); |
| 1329 | if (n < 0) |
| 1330 | write_enn (own_buf); |
| 1331 | else |
| 1332 | sprintf (own_buf, "%x", n); |
| 1333 | |
| 1334 | free (data); |
| 1335 | return; |
| 1336 | } |
| 1337 | |
| 1338 | if (strncmp ("qXfer:threads:read:", own_buf, 19) == 0) |
| 1339 | { |
| 1340 | unsigned char *data; |
| 1341 | int n; |
| 1342 | CORE_ADDR ofs; |
| 1343 | unsigned int len; |
| 1344 | char *annex; |
| 1345 | |
| 1346 | require_running (own_buf); |
| 1347 | |
| 1348 | /* Reject any annex; grab the offset and length. */ |
| 1349 | if (decode_xfer_read (own_buf + 19, &annex, &ofs, &len) < 0 |
| 1350 | || annex[0] != '\0') |
| 1351 | { |
| 1352 | strcpy (own_buf, "E00"); |
| 1353 | return; |
| 1354 | } |
| 1355 | |
| 1356 | /* Read one extra byte, as an indicator of whether there is |
| 1357 | more. */ |
| 1358 | if (len > PBUFSIZ - 2) |
| 1359 | len = PBUFSIZ - 2; |
| 1360 | data = malloc (len + 1); |
| 1361 | if (!data) |
| 1362 | return; |
| 1363 | n = handle_threads_qxfer (annex, data, ofs, len + 1); |
| 1364 | if (n < 0) |
| 1365 | write_enn (own_buf); |
| 1366 | else if (n > len) |
| 1367 | *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1); |
| 1368 | else |
| 1369 | *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0); |
| 1370 | |
| 1371 | free (data); |
| 1372 | return; |
| 1373 | } |
| 1374 | |
| 1375 | if (strncmp ("qXfer:statictrace:read:", own_buf, |
| 1376 | sizeof ("qXfer:statictrace:read:") -1) == 0) |
| 1377 | { |
| 1378 | unsigned char *data; |
| 1379 | CORE_ADDR ofs; |
| 1380 | unsigned int len; |
| 1381 | char *annex; |
| 1382 | ULONGEST nbytes; |
| 1383 | |
| 1384 | require_running (own_buf); |
| 1385 | |
| 1386 | if (current_traceframe == -1) |
| 1387 | { |
| 1388 | write_enn (own_buf); |
| 1389 | return; |
| 1390 | } |
| 1391 | |
| 1392 | /* Reject any annex; grab the offset and length. */ |
| 1393 | if (decode_xfer_read (own_buf + sizeof ("qXfer:statictrace:read:") -1, |
| 1394 | &annex, &ofs, &len) < 0 |
| 1395 | || annex[0] != '\0') |
| 1396 | { |
| 1397 | strcpy (own_buf, "E00"); |
| 1398 | return; |
| 1399 | } |
| 1400 | |
| 1401 | /* Read one extra byte, as an indicator of whether there is |
| 1402 | more. */ |
| 1403 | if (len > PBUFSIZ - 2) |
| 1404 | len = PBUFSIZ - 2; |
| 1405 | data = malloc (len + 1); |
| 1406 | if (!data) |
| 1407 | return; |
| 1408 | |
| 1409 | if (traceframe_read_sdata (current_traceframe, ofs, |
| 1410 | data, len + 1, &nbytes)) |
| 1411 | write_enn (own_buf); |
| 1412 | else if (nbytes > len) |
| 1413 | *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1); |
| 1414 | else |
| 1415 | *new_packet_len_p = write_qxfer_response (own_buf, data, nbytes, 0); |
| 1416 | |
| 1417 | free (data); |
| 1418 | return; |
| 1419 | } |
| 1420 | |
| 1421 | /* Protocol features query. */ |
| 1422 | if (strncmp ("qSupported", own_buf, 10) == 0 |
| 1423 | && (own_buf[10] == ':' || own_buf[10] == '\0')) |
| 1424 | { |
| 1425 | char *p = &own_buf[10]; |
| 1426 | int gdb_supports_qRelocInsn = 0; |
| 1427 | |
| 1428 | /* Start processing qSupported packet. */ |
| 1429 | target_process_qsupported (NULL); |
| 1430 | |
| 1431 | /* Process each feature being provided by GDB. The first |
| 1432 | feature will follow a ':', and latter features will follow |
| 1433 | ';'. */ |
| 1434 | if (*p == ':') |
| 1435 | { |
| 1436 | char **qsupported = NULL; |
| 1437 | int count = 0; |
| 1438 | int i; |
| 1439 | |
| 1440 | /* Two passes, to avoid nested strtok calls in |
| 1441 | target_process_qsupported. */ |
| 1442 | for (p = strtok (p + 1, ";"); |
| 1443 | p != NULL; |
| 1444 | p = strtok (NULL, ";")) |
| 1445 | { |
| 1446 | count++; |
| 1447 | qsupported = xrealloc (qsupported, count * sizeof (char *)); |
| 1448 | qsupported[count - 1] = xstrdup (p); |
| 1449 | } |
| 1450 | |
| 1451 | for (i = 0; i < count; i++) |
| 1452 | { |
| 1453 | p = qsupported[i]; |
| 1454 | if (strcmp (p, "multiprocess+") == 0) |
| 1455 | { |
| 1456 | /* GDB supports and wants multi-process support if |
| 1457 | possible. */ |
| 1458 | if (target_supports_multi_process ()) |
| 1459 | multi_process = 1; |
| 1460 | } |
| 1461 | else if (strcmp (p, "qRelocInsn+") == 0) |
| 1462 | { |
| 1463 | /* GDB supports relocate instruction requests. */ |
| 1464 | gdb_supports_qRelocInsn = 1; |
| 1465 | } |
| 1466 | else |
| 1467 | target_process_qsupported (p); |
| 1468 | |
| 1469 | free (p); |
| 1470 | } |
| 1471 | |
| 1472 | free (qsupported); |
| 1473 | } |
| 1474 | |
| 1475 | sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1); |
| 1476 | |
| 1477 | /* We do not have any hook to indicate whether the target backend |
| 1478 | supports qXfer:libraries:read, so always report it. */ |
| 1479 | strcat (own_buf, ";qXfer:libraries:read+"); |
| 1480 | |
| 1481 | if (the_target->read_auxv != NULL) |
| 1482 | strcat (own_buf, ";qXfer:auxv:read+"); |
| 1483 | |
| 1484 | if (the_target->qxfer_spu != NULL) |
| 1485 | strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+"); |
| 1486 | |
| 1487 | if (the_target->qxfer_siginfo != NULL) |
| 1488 | strcat (own_buf, ";qXfer:siginfo:read+;qXfer:siginfo:write+"); |
| 1489 | |
| 1490 | /* We always report qXfer:features:read, as targets may |
| 1491 | install XML files on a subsequent call to arch_setup. |
| 1492 | If we reported to GDB on startup that we don't support |
| 1493 | qXfer:feature:read at all, we will never be re-queried. */ |
| 1494 | strcat (own_buf, ";qXfer:features:read+"); |
| 1495 | |
| 1496 | if (transport_is_reliable) |
| 1497 | strcat (own_buf, ";QStartNoAckMode+"); |
| 1498 | |
| 1499 | if (the_target->qxfer_osdata != NULL) |
| 1500 | strcat (own_buf, ";qXfer:osdata:read+"); |
| 1501 | |
| 1502 | if (target_supports_multi_process ()) |
| 1503 | strcat (own_buf, ";multiprocess+"); |
| 1504 | |
| 1505 | if (target_supports_non_stop ()) |
| 1506 | strcat (own_buf, ";QNonStop+"); |
| 1507 | |
| 1508 | strcat (own_buf, ";qXfer:threads:read+"); |
| 1509 | |
| 1510 | if (target_supports_tracepoints ()) |
| 1511 | { |
| 1512 | strcat (own_buf, ";ConditionalTracepoints+"); |
| 1513 | strcat (own_buf, ";TraceStateVariables+"); |
| 1514 | strcat (own_buf, ";TracepointSource+"); |
| 1515 | strcat (own_buf, ";DisconnectedTracing+"); |
| 1516 | if (gdb_supports_qRelocInsn && target_supports_fast_tracepoints ()) |
| 1517 | strcat (own_buf, ";FastTracepoints+"); |
| 1518 | strcat (own_buf, ";StaticTracepoints+"); |
| 1519 | strcat (own_buf, ";qXfer:statictrace:read+"); |
| 1520 | } |
| 1521 | |
| 1522 | return; |
| 1523 | } |
| 1524 | |
| 1525 | /* Thread-local storage support. */ |
| 1526 | if (the_target->get_tls_address != NULL |
| 1527 | && strncmp ("qGetTLSAddr:", own_buf, 12) == 0) |
| 1528 | { |
| 1529 | char *p = own_buf + 12; |
| 1530 | CORE_ADDR parts[2], address = 0; |
| 1531 | int i, err; |
| 1532 | ptid_t ptid = null_ptid; |
| 1533 | |
| 1534 | require_running (own_buf); |
| 1535 | |
| 1536 | for (i = 0; i < 3; i++) |
| 1537 | { |
| 1538 | char *p2; |
| 1539 | int len; |
| 1540 | |
| 1541 | if (p == NULL) |
| 1542 | break; |
| 1543 | |
| 1544 | p2 = strchr (p, ','); |
| 1545 | if (p2) |
| 1546 | { |
| 1547 | len = p2 - p; |
| 1548 | p2++; |
| 1549 | } |
| 1550 | else |
| 1551 | { |
| 1552 | len = strlen (p); |
| 1553 | p2 = NULL; |
| 1554 | } |
| 1555 | |
| 1556 | if (i == 0) |
| 1557 | ptid = read_ptid (p, NULL); |
| 1558 | else |
| 1559 | decode_address (&parts[i - 1], p, len); |
| 1560 | p = p2; |
| 1561 | } |
| 1562 | |
| 1563 | if (p != NULL || i < 3) |
| 1564 | err = 1; |
| 1565 | else |
| 1566 | { |
| 1567 | struct thread_info *thread = find_thread_ptid (ptid); |
| 1568 | |
| 1569 | if (thread == NULL) |
| 1570 | err = 2; |
| 1571 | else |
| 1572 | err = the_target->get_tls_address (thread, parts[0], parts[1], |
| 1573 | &address); |
| 1574 | } |
| 1575 | |
| 1576 | if (err == 0) |
| 1577 | { |
| 1578 | strcpy (own_buf, paddress(address)); |
| 1579 | return; |
| 1580 | } |
| 1581 | else if (err > 0) |
| 1582 | { |
| 1583 | write_enn (own_buf); |
| 1584 | return; |
| 1585 | } |
| 1586 | |
| 1587 | /* Otherwise, pretend we do not understand this packet. */ |
| 1588 | } |
| 1589 | |
| 1590 | /* Windows OS Thread Information Block address support. */ |
| 1591 | if (the_target->get_tib_address != NULL |
| 1592 | && strncmp ("qGetTIBAddr:", own_buf, 12) == 0) |
| 1593 | { |
| 1594 | char *annex; |
| 1595 | int n; |
| 1596 | CORE_ADDR tlb; |
| 1597 | ptid_t ptid = read_ptid (own_buf + 12, &annex); |
| 1598 | |
| 1599 | n = (*the_target->get_tib_address) (ptid, &tlb); |
| 1600 | if (n == 1) |
| 1601 | { |
| 1602 | strcpy (own_buf, paddress(tlb)); |
| 1603 | return; |
| 1604 | } |
| 1605 | else if (n == 0) |
| 1606 | { |
| 1607 | write_enn (own_buf); |
| 1608 | return; |
| 1609 | } |
| 1610 | return; |
| 1611 | } |
| 1612 | |
| 1613 | /* Handle "monitor" commands. */ |
| 1614 | if (strncmp ("qRcmd,", own_buf, 6) == 0) |
| 1615 | { |
| 1616 | char *mon = malloc (PBUFSIZ); |
| 1617 | int len = strlen (own_buf + 6); |
| 1618 | |
| 1619 | if (mon == NULL) |
| 1620 | { |
| 1621 | write_enn (own_buf); |
| 1622 | return; |
| 1623 | } |
| 1624 | |
| 1625 | if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2) |
| 1626 | { |
| 1627 | write_enn (own_buf); |
| 1628 | free (mon); |
| 1629 | return; |
| 1630 | } |
| 1631 | mon[len / 2] = '\0'; |
| 1632 | |
| 1633 | write_ok (own_buf); |
| 1634 | |
| 1635 | if (the_target->handle_monitor_command == NULL |
| 1636 | || (*the_target->handle_monitor_command) (mon) == 0) |
| 1637 | /* Default processing. */ |
| 1638 | handle_monitor_command (mon); |
| 1639 | |
| 1640 | free (mon); |
| 1641 | return; |
| 1642 | } |
| 1643 | |
| 1644 | if (strncmp ("qSearch:memory:", own_buf, sizeof ("qSearch:memory:") - 1) == 0) |
| 1645 | { |
| 1646 | require_running (own_buf); |
| 1647 | handle_search_memory (own_buf, packet_len); |
| 1648 | return; |
| 1649 | } |
| 1650 | |
| 1651 | if (strcmp (own_buf, "qAttached") == 0 |
| 1652 | || strncmp (own_buf, "qAttached:", sizeof ("qAttached:") - 1) == 0) |
| 1653 | { |
| 1654 | struct process_info *process; |
| 1655 | |
| 1656 | if (own_buf[sizeof ("qAttached") - 1]) |
| 1657 | { |
| 1658 | int pid = strtoul (own_buf + sizeof ("qAttached:") - 1, NULL, 16); |
| 1659 | process = (struct process_info *) |
| 1660 | find_inferior_id (&all_processes, pid_to_ptid (pid)); |
| 1661 | } |
| 1662 | else |
| 1663 | { |
| 1664 | require_running (own_buf); |
| 1665 | process = current_process (); |
| 1666 | } |
| 1667 | |
| 1668 | if (process == NULL) |
| 1669 | { |
| 1670 | write_enn (own_buf); |
| 1671 | return; |
| 1672 | } |
| 1673 | |
| 1674 | strcpy (own_buf, process->attached ? "1" : "0"); |
| 1675 | return; |
| 1676 | } |
| 1677 | |
| 1678 | if (strncmp ("qCRC:", own_buf, 5) == 0) |
| 1679 | { |
| 1680 | /* CRC check (compare-section). */ |
| 1681 | char *comma; |
| 1682 | CORE_ADDR base; |
| 1683 | int len; |
| 1684 | unsigned long long crc; |
| 1685 | |
| 1686 | require_running (own_buf); |
| 1687 | base = strtoul (own_buf + 5, &comma, 16); |
| 1688 | if (*comma++ != ',') |
| 1689 | { |
| 1690 | write_enn (own_buf); |
| 1691 | return; |
| 1692 | } |
| 1693 | len = strtoul (comma, NULL, 16); |
| 1694 | crc = crc32 (base, len, 0xffffffff); |
| 1695 | /* Check for memory failure. */ |
| 1696 | if (crc == (unsigned long long) -1) |
| 1697 | { |
| 1698 | write_enn (own_buf); |
| 1699 | return; |
| 1700 | } |
| 1701 | sprintf (own_buf, "C%lx", (unsigned long) crc); |
| 1702 | return; |
| 1703 | } |
| 1704 | |
| 1705 | if (target_supports_tracepoints () && handle_tracepoint_query (own_buf)) |
| 1706 | return; |
| 1707 | |
| 1708 | /* Otherwise we didn't know what packet it was. Say we didn't |
| 1709 | understand it. */ |
| 1710 | own_buf[0] = 0; |
| 1711 | } |
| 1712 | |
| 1713 | static void gdb_wants_all_threads_stopped (void); |
| 1714 | |
| 1715 | /* Parse vCont packets. */ |
| 1716 | void |
| 1717 | handle_v_cont (char *own_buf) |
| 1718 | { |
| 1719 | char *p, *q; |
| 1720 | int n = 0, i = 0; |
| 1721 | struct thread_resume *resume_info; |
| 1722 | struct thread_resume default_action = {{0}}; |
| 1723 | |
| 1724 | /* Count the number of semicolons in the packet. There should be one |
| 1725 | for every action. */ |
| 1726 | p = &own_buf[5]; |
| 1727 | while (p) |
| 1728 | { |
| 1729 | n++; |
| 1730 | p++; |
| 1731 | p = strchr (p, ';'); |
| 1732 | } |
| 1733 | |
| 1734 | resume_info = malloc (n * sizeof (resume_info[0])); |
| 1735 | if (resume_info == NULL) |
| 1736 | goto err; |
| 1737 | |
| 1738 | p = &own_buf[5]; |
| 1739 | while (*p) |
| 1740 | { |
| 1741 | p++; |
| 1742 | |
| 1743 | if (p[0] == 's' || p[0] == 'S') |
| 1744 | resume_info[i].kind = resume_step; |
| 1745 | else if (p[0] == 'c' || p[0] == 'C') |
| 1746 | resume_info[i].kind = resume_continue; |
| 1747 | else if (p[0] == 't') |
| 1748 | resume_info[i].kind = resume_stop; |
| 1749 | else |
| 1750 | goto err; |
| 1751 | |
| 1752 | if (p[0] == 'S' || p[0] == 'C') |
| 1753 | { |
| 1754 | int sig; |
| 1755 | sig = strtol (p + 1, &q, 16); |
| 1756 | if (p == q) |
| 1757 | goto err; |
| 1758 | p = q; |
| 1759 | |
| 1760 | if (!target_signal_to_host_p (sig)) |
| 1761 | goto err; |
| 1762 | resume_info[i].sig = target_signal_to_host (sig); |
| 1763 | } |
| 1764 | else |
| 1765 | { |
| 1766 | resume_info[i].sig = 0; |
| 1767 | p = p + 1; |
| 1768 | } |
| 1769 | |
| 1770 | if (p[0] == 0) |
| 1771 | { |
| 1772 | resume_info[i].thread = minus_one_ptid; |
| 1773 | default_action = resume_info[i]; |
| 1774 | |
| 1775 | /* Note: we don't increment i here, we'll overwrite this entry |
| 1776 | the next time through. */ |
| 1777 | } |
| 1778 | else if (p[0] == ':') |
| 1779 | { |
| 1780 | ptid_t ptid = read_ptid (p + 1, &q); |
| 1781 | |
| 1782 | if (p == q) |
| 1783 | goto err; |
| 1784 | p = q; |
| 1785 | if (p[0] != ';' && p[0] != 0) |
| 1786 | goto err; |
| 1787 | |
| 1788 | resume_info[i].thread = ptid; |
| 1789 | |
| 1790 | i++; |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | if (i < n) |
| 1795 | resume_info[i] = default_action; |
| 1796 | |
| 1797 | /* Still used in occasional places in the backend. */ |
| 1798 | if (n == 1 |
| 1799 | && !ptid_equal (resume_info[0].thread, minus_one_ptid) |
| 1800 | && resume_info[0].kind != resume_stop) |
| 1801 | cont_thread = resume_info[0].thread; |
| 1802 | else |
| 1803 | cont_thread = minus_one_ptid; |
| 1804 | set_desired_inferior (0); |
| 1805 | |
| 1806 | if (!non_stop) |
| 1807 | enable_async_io (); |
| 1808 | |
| 1809 | (*the_target->resume) (resume_info, n); |
| 1810 | |
| 1811 | free (resume_info); |
| 1812 | |
| 1813 | if (non_stop) |
| 1814 | write_ok (own_buf); |
| 1815 | else |
| 1816 | { |
| 1817 | last_ptid = mywait (minus_one_ptid, &last_status, 0, 1); |
| 1818 | |
| 1819 | if (last_status.kind != TARGET_WAITKIND_EXITED |
| 1820 | && last_status.kind != TARGET_WAITKIND_SIGNALLED) |
| 1821 | current_inferior->last_status = last_status; |
| 1822 | |
| 1823 | /* From the client's perspective, all-stop mode always stops all |
| 1824 | threads implicitly (and the target backend has already done |
| 1825 | so by now). Tag all threads as "want-stopped", so we don't |
| 1826 | resume them implicitly without the client telling us to. */ |
| 1827 | gdb_wants_all_threads_stopped (); |
| 1828 | prepare_resume_reply (own_buf, last_ptid, &last_status); |
| 1829 | disable_async_io (); |
| 1830 | |
| 1831 | if (last_status.kind == TARGET_WAITKIND_EXITED |
| 1832 | || last_status.kind == TARGET_WAITKIND_SIGNALLED) |
| 1833 | mourn_inferior (find_process_pid (ptid_get_pid (last_ptid))); |
| 1834 | } |
| 1835 | return; |
| 1836 | |
| 1837 | err: |
| 1838 | write_enn (own_buf); |
| 1839 | free (resume_info); |
| 1840 | return; |
| 1841 | } |
| 1842 | |
| 1843 | /* Attach to a new program. Return 1 if successful, 0 if failure. */ |
| 1844 | int |
| 1845 | handle_v_attach (char *own_buf) |
| 1846 | { |
| 1847 | int pid; |
| 1848 | |
| 1849 | pid = strtol (own_buf + 8, NULL, 16); |
| 1850 | if (pid != 0 && attach_inferior (pid) == 0) |
| 1851 | { |
| 1852 | /* Don't report shared library events after attaching, even if |
| 1853 | some libraries are preloaded. GDB will always poll the |
| 1854 | library list. Avoids the "stopped by shared library event" |
| 1855 | notice on the GDB side. */ |
| 1856 | dlls_changed = 0; |
| 1857 | |
| 1858 | if (non_stop) |
| 1859 | { |
| 1860 | /* In non-stop, we don't send a resume reply. Stop events |
| 1861 | will follow up using the normal notification |
| 1862 | mechanism. */ |
| 1863 | write_ok (own_buf); |
| 1864 | } |
| 1865 | else |
| 1866 | prepare_resume_reply (own_buf, last_ptid, &last_status); |
| 1867 | |
| 1868 | return 1; |
| 1869 | } |
| 1870 | else |
| 1871 | { |
| 1872 | write_enn (own_buf); |
| 1873 | return 0; |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | /* Run a new program. Return 1 if successful, 0 if failure. */ |
| 1878 | static int |
| 1879 | handle_v_run (char *own_buf) |
| 1880 | { |
| 1881 | char *p, *next_p, **new_argv; |
| 1882 | int i, new_argc; |
| 1883 | |
| 1884 | new_argc = 0; |
| 1885 | for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';')) |
| 1886 | { |
| 1887 | p++; |
| 1888 | new_argc++; |
| 1889 | } |
| 1890 | |
| 1891 | new_argv = calloc (new_argc + 2, sizeof (char *)); |
| 1892 | if (new_argv == NULL) |
| 1893 | { |
| 1894 | write_enn (own_buf); |
| 1895 | return 0; |
| 1896 | } |
| 1897 | |
| 1898 | i = 0; |
| 1899 | for (p = own_buf + strlen ("vRun;"); *p; p = next_p) |
| 1900 | { |
| 1901 | next_p = strchr (p, ';'); |
| 1902 | if (next_p == NULL) |
| 1903 | next_p = p + strlen (p); |
| 1904 | |
| 1905 | if (i == 0 && p == next_p) |
| 1906 | new_argv[i] = NULL; |
| 1907 | else |
| 1908 | { |
| 1909 | /* FIXME: Fail request if out of memory instead of dying. */ |
| 1910 | new_argv[i] = xmalloc (1 + (next_p - p) / 2); |
| 1911 | unhexify (new_argv[i], p, (next_p - p) / 2); |
| 1912 | new_argv[i][(next_p - p) / 2] = '\0'; |
| 1913 | } |
| 1914 | |
| 1915 | if (*next_p) |
| 1916 | next_p++; |
| 1917 | i++; |
| 1918 | } |
| 1919 | new_argv[i] = NULL; |
| 1920 | |
| 1921 | if (new_argv[0] == NULL) |
| 1922 | { |
| 1923 | /* GDB didn't specify a program to run. Use the program from the |
| 1924 | last run with the new argument list. */ |
| 1925 | |
| 1926 | if (program_argv == NULL) |
| 1927 | { |
| 1928 | /* FIXME: new_argv memory leak */ |
| 1929 | write_enn (own_buf); |
| 1930 | return 0; |
| 1931 | } |
| 1932 | |
| 1933 | new_argv[0] = strdup (program_argv[0]); |
| 1934 | if (new_argv[0] == NULL) |
| 1935 | { |
| 1936 | /* FIXME: new_argv memory leak */ |
| 1937 | write_enn (own_buf); |
| 1938 | return 0; |
| 1939 | } |
| 1940 | } |
| 1941 | |
| 1942 | /* Free the old argv and install the new one. */ |
| 1943 | freeargv (program_argv); |
| 1944 | program_argv = new_argv; |
| 1945 | |
| 1946 | start_inferior (program_argv); |
| 1947 | if (last_status.kind == TARGET_WAITKIND_STOPPED) |
| 1948 | { |
| 1949 | prepare_resume_reply (own_buf, last_ptid, &last_status); |
| 1950 | |
| 1951 | /* In non-stop, sending a resume reply doesn't set the general |
| 1952 | thread, but GDB assumes a vRun sets it (this is so GDB can |
| 1953 | query which is the main thread of the new inferior. */ |
| 1954 | if (non_stop) |
| 1955 | general_thread = last_ptid; |
| 1956 | |
| 1957 | return 1; |
| 1958 | } |
| 1959 | else |
| 1960 | { |
| 1961 | write_enn (own_buf); |
| 1962 | return 0; |
| 1963 | } |
| 1964 | } |
| 1965 | |
| 1966 | /* Kill process. Return 1 if successful, 0 if failure. */ |
| 1967 | int |
| 1968 | handle_v_kill (char *own_buf) |
| 1969 | { |
| 1970 | int pid; |
| 1971 | char *p = &own_buf[6]; |
| 1972 | if (multi_process) |
| 1973 | pid = strtol (p, NULL, 16); |
| 1974 | else |
| 1975 | pid = signal_pid; |
| 1976 | if (pid != 0 && kill_inferior (pid) == 0) |
| 1977 | { |
| 1978 | last_status.kind = TARGET_WAITKIND_SIGNALLED; |
| 1979 | last_status.value.sig = TARGET_SIGNAL_KILL; |
| 1980 | last_ptid = pid_to_ptid (pid); |
| 1981 | discard_queued_stop_replies (pid); |
| 1982 | write_ok (own_buf); |
| 1983 | return 1; |
| 1984 | } |
| 1985 | else |
| 1986 | { |
| 1987 | write_enn (own_buf); |
| 1988 | return 0; |
| 1989 | } |
| 1990 | } |
| 1991 | |
| 1992 | /* Handle a 'vStopped' packet. */ |
| 1993 | static void |
| 1994 | handle_v_stopped (char *own_buf) |
| 1995 | { |
| 1996 | /* If we're waiting for GDB to acknowledge a pending stop reply, |
| 1997 | consider that done. */ |
| 1998 | if (notif_queue) |
| 1999 | { |
| 2000 | struct vstop_notif *head; |
| 2001 | |
| 2002 | if (remote_debug) |
| 2003 | fprintf (stderr, "vStopped: acking %s\n", |
| 2004 | target_pid_to_str (notif_queue->ptid)); |
| 2005 | |
| 2006 | head = notif_queue; |
| 2007 | notif_queue = notif_queue->next; |
| 2008 | free (head); |
| 2009 | } |
| 2010 | |
| 2011 | /* Push another stop reply, or if there are no more left, an OK. */ |
| 2012 | send_next_stop_reply (own_buf); |
| 2013 | } |
| 2014 | |
| 2015 | /* Handle all of the extended 'v' packets. */ |
| 2016 | void |
| 2017 | handle_v_requests (char *own_buf, int packet_len, int *new_packet_len) |
| 2018 | { |
| 2019 | if (!disable_packet_vCont) |
| 2020 | { |
| 2021 | if (strncmp (own_buf, "vCont;", 6) == 0) |
| 2022 | { |
| 2023 | require_running (own_buf); |
| 2024 | handle_v_cont (own_buf); |
| 2025 | return; |
| 2026 | } |
| 2027 | |
| 2028 | if (strncmp (own_buf, "vCont?", 6) == 0) |
| 2029 | { |
| 2030 | strcpy (own_buf, "vCont;c;C;s;S;t"); |
| 2031 | return; |
| 2032 | } |
| 2033 | } |
| 2034 | |
| 2035 | if (strncmp (own_buf, "vFile:", 6) == 0 |
| 2036 | && handle_vFile (own_buf, packet_len, new_packet_len)) |
| 2037 | return; |
| 2038 | |
| 2039 | if (strncmp (own_buf, "vAttach;", 8) == 0) |
| 2040 | { |
| 2041 | if (!multi_process && target_running ()) |
| 2042 | { |
| 2043 | fprintf (stderr, "Already debugging a process\n"); |
| 2044 | write_enn (own_buf); |
| 2045 | return; |
| 2046 | } |
| 2047 | handle_v_attach (own_buf); |
| 2048 | return; |
| 2049 | } |
| 2050 | |
| 2051 | if (strncmp (own_buf, "vRun;", 5) == 0) |
| 2052 | { |
| 2053 | if (!multi_process && target_running ()) |
| 2054 | { |
| 2055 | fprintf (stderr, "Already debugging a process\n"); |
| 2056 | write_enn (own_buf); |
| 2057 | return; |
| 2058 | } |
| 2059 | handle_v_run (own_buf); |
| 2060 | return; |
| 2061 | } |
| 2062 | |
| 2063 | if (strncmp (own_buf, "vKill;", 6) == 0) |
| 2064 | { |
| 2065 | if (!target_running ()) |
| 2066 | { |
| 2067 | fprintf (stderr, "No process to kill\n"); |
| 2068 | write_enn (own_buf); |
| 2069 | return; |
| 2070 | } |
| 2071 | handle_v_kill (own_buf); |
| 2072 | return; |
| 2073 | } |
| 2074 | |
| 2075 | if (strncmp (own_buf, "vStopped", 8) == 0) |
| 2076 | { |
| 2077 | handle_v_stopped (own_buf); |
| 2078 | return; |
| 2079 | } |
| 2080 | |
| 2081 | /* Otherwise we didn't know what packet it was. Say we didn't |
| 2082 | understand it. */ |
| 2083 | own_buf[0] = 0; |
| 2084 | return; |
| 2085 | } |
| 2086 | |
| 2087 | /* Resume inferior and wait for another event. In non-stop mode, |
| 2088 | don't really wait here, but return immediatelly to the event |
| 2089 | loop. */ |
| 2090 | static void |
| 2091 | myresume (char *own_buf, int step, int sig) |
| 2092 | { |
| 2093 | struct thread_resume resume_info[2]; |
| 2094 | int n = 0; |
| 2095 | int valid_cont_thread; |
| 2096 | |
| 2097 | set_desired_inferior (0); |
| 2098 | |
| 2099 | valid_cont_thread = (!ptid_equal (cont_thread, null_ptid) |
| 2100 | && !ptid_equal (cont_thread, minus_one_ptid)); |
| 2101 | |
| 2102 | if (step || sig || valid_cont_thread) |
| 2103 | { |
| 2104 | resume_info[0].thread |
| 2105 | = ((struct inferior_list_entry *) current_inferior)->id; |
| 2106 | if (step) |
| 2107 | resume_info[0].kind = resume_step; |
| 2108 | else |
| 2109 | resume_info[0].kind = resume_continue; |
| 2110 | resume_info[0].sig = sig; |
| 2111 | n++; |
| 2112 | } |
| 2113 | |
| 2114 | if (!valid_cont_thread) |
| 2115 | { |
| 2116 | resume_info[n].thread = minus_one_ptid; |
| 2117 | resume_info[n].kind = resume_continue; |
| 2118 | resume_info[n].sig = 0; |
| 2119 | n++; |
| 2120 | } |
| 2121 | |
| 2122 | if (!non_stop) |
| 2123 | enable_async_io (); |
| 2124 | |
| 2125 | (*the_target->resume) (resume_info, n); |
| 2126 | |
| 2127 | if (non_stop) |
| 2128 | write_ok (own_buf); |
| 2129 | else |
| 2130 | { |
| 2131 | last_ptid = mywait (minus_one_ptid, &last_status, 0, 1); |
| 2132 | |
| 2133 | if (last_status.kind != TARGET_WAITKIND_EXITED |
| 2134 | && last_status.kind != TARGET_WAITKIND_SIGNALLED) |
| 2135 | { |
| 2136 | current_inferior->last_resume_kind = resume_stop; |
| 2137 | current_inferior->last_status = last_status; |
| 2138 | } |
| 2139 | |
| 2140 | prepare_resume_reply (own_buf, last_ptid, &last_status); |
| 2141 | disable_async_io (); |
| 2142 | |
| 2143 | if (last_status.kind == TARGET_WAITKIND_EXITED |
| 2144 | || last_status.kind == TARGET_WAITKIND_SIGNALLED) |
| 2145 | mourn_inferior (find_process_pid (ptid_get_pid (last_ptid))); |
| 2146 | } |
| 2147 | } |
| 2148 | |
| 2149 | /* Callback for for_each_inferior. Make a new stop reply for each |
| 2150 | stopped thread. */ |
| 2151 | |
| 2152 | static int |
| 2153 | queue_stop_reply_callback (struct inferior_list_entry *entry, void *arg) |
| 2154 | { |
| 2155 | struct thread_info *thread = (struct thread_info *) entry; |
| 2156 | |
| 2157 | /* For now, assume targets that don't have this callback also don't |
| 2158 | manage the thread's last_status field. */ |
| 2159 | if (the_target->thread_stopped == NULL) |
| 2160 | { |
| 2161 | struct target_waitstatus status; |
| 2162 | |
| 2163 | status.kind = TARGET_WAITKIND_STOPPED; |
| 2164 | status.value.sig = TARGET_SIGNAL_TRAP; |
| 2165 | |
| 2166 | /* Pass the last stop reply back to GDB, but don't notify |
| 2167 | yet. */ |
| 2168 | queue_stop_reply (entry->id, &thread->last_status); |
| 2169 | } |
| 2170 | else |
| 2171 | { |
| 2172 | if (thread_stopped (thread)) |
| 2173 | { |
| 2174 | if (debug_threads) |
| 2175 | fprintf (stderr, "Reporting thread %s as already stopped with %s\n", |
| 2176 | target_pid_to_str (entry->id), |
| 2177 | target_waitstatus_to_string (&thread->last_status)); |
| 2178 | |
| 2179 | gdb_assert (thread->last_status.kind != TARGET_WAITKIND_IGNORE); |
| 2180 | |
| 2181 | /* Pass the last stop reply back to GDB, but don't notify |
| 2182 | yet. */ |
| 2183 | queue_stop_reply (entry->id, &thread->last_status); |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | return 0; |
| 2188 | } |
| 2189 | |
| 2190 | /* Set this inferior threads's state as "want-stopped". We won't |
| 2191 | resume this thread until the client gives us another action for |
| 2192 | it. */ |
| 2193 | |
| 2194 | static void |
| 2195 | gdb_wants_thread_stopped (struct inferior_list_entry *entry) |
| 2196 | { |
| 2197 | struct thread_info *thread = (struct thread_info *) entry; |
| 2198 | |
| 2199 | thread->last_resume_kind = resume_stop; |
| 2200 | |
| 2201 | if (thread->last_status.kind == TARGET_WAITKIND_IGNORE) |
| 2202 | { |
| 2203 | /* Most threads are stopped implicitly (all-stop); tag that with |
| 2204 | signal 0. */ |
| 2205 | thread->last_status.kind = TARGET_WAITKIND_STOPPED; |
| 2206 | thread->last_status.value.sig = TARGET_SIGNAL_0; |
| 2207 | } |
| 2208 | } |
| 2209 | |
| 2210 | /* Set all threads' states as "want-stopped". */ |
| 2211 | |
| 2212 | static void |
| 2213 | gdb_wants_all_threads_stopped (void) |
| 2214 | { |
| 2215 | for_each_inferior (&all_threads, gdb_wants_thread_stopped); |
| 2216 | } |
| 2217 | |
| 2218 | /* Clear the gdb_detached flag of every process. */ |
| 2219 | |
| 2220 | static void |
| 2221 | gdb_reattached_process (struct inferior_list_entry *entry) |
| 2222 | { |
| 2223 | struct process_info *process = (struct process_info *) entry; |
| 2224 | |
| 2225 | process->gdb_detached = 0; |
| 2226 | } |
| 2227 | |
| 2228 | /* Status handler for the '?' packet. */ |
| 2229 | |
| 2230 | static void |
| 2231 | handle_status (char *own_buf) |
| 2232 | { |
| 2233 | /* GDB is connected, don't forward events to the target anymore. */ |
| 2234 | for_each_inferior (&all_processes, gdb_reattached_process); |
| 2235 | |
| 2236 | /* In non-stop mode, we must send a stop reply for each stopped |
| 2237 | thread. In all-stop mode, just send one for the first stopped |
| 2238 | thread we find. */ |
| 2239 | |
| 2240 | if (non_stop) |
| 2241 | { |
| 2242 | discard_queued_stop_replies (-1); |
| 2243 | find_inferior (&all_threads, queue_stop_reply_callback, NULL); |
| 2244 | |
| 2245 | /* The first is sent immediatly. OK is sent if there is no |
| 2246 | stopped thread, which is the same handling of the vStopped |
| 2247 | packet (by design). */ |
| 2248 | send_next_stop_reply (own_buf); |
| 2249 | } |
| 2250 | else |
| 2251 | { |
| 2252 | pause_all (0); |
| 2253 | stabilize_threads (); |
| 2254 | gdb_wants_all_threads_stopped (); |
| 2255 | |
| 2256 | if (all_threads.head) |
| 2257 | { |
| 2258 | struct target_waitstatus status; |
| 2259 | |
| 2260 | status.kind = TARGET_WAITKIND_STOPPED; |
| 2261 | status.value.sig = TARGET_SIGNAL_TRAP; |
| 2262 | prepare_resume_reply (own_buf, |
| 2263 | all_threads.head->id, &status); |
| 2264 | } |
| 2265 | else |
| 2266 | strcpy (own_buf, "W00"); |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | static void |
| 2271 | gdbserver_version (void) |
| 2272 | { |
| 2273 | printf ("GNU gdbserver %s%s\n" |
| 2274 | "Copyright (C) 2010 Free Software Foundation, Inc.\n" |
| 2275 | "gdbserver is free software, covered by the GNU General Public License.\n" |
| 2276 | "This gdbserver was configured as \"%s\"\n", |
| 2277 | PKGVERSION, version, host_name); |
| 2278 | } |
| 2279 | |
| 2280 | static void |
| 2281 | gdbserver_usage (FILE *stream) |
| 2282 | { |
| 2283 | fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n" |
| 2284 | "\tgdbserver [OPTIONS] --attach COMM PID\n" |
| 2285 | "\tgdbserver [OPTIONS] --multi COMM\n" |
| 2286 | "\n" |
| 2287 | "COMM may either be a tty device (for serial debugging), or \n" |
| 2288 | "HOST:PORT to listen for a TCP connection.\n" |
| 2289 | "\n" |
| 2290 | "Options:\n" |
| 2291 | " --debug Enable general debugging output.\n" |
| 2292 | " --remote-debug Enable remote protocol debugging output.\n" |
| 2293 | " --version Display version information and exit.\n" |
| 2294 | " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"); |
| 2295 | if (REPORT_BUGS_TO[0] && stream == stdout) |
| 2296 | fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO); |
| 2297 | } |
| 2298 | |
| 2299 | static void |
| 2300 | gdbserver_show_disableable (FILE *stream) |
| 2301 | { |
| 2302 | fprintf (stream, "Disableable packets:\n" |
| 2303 | " vCont \tAll vCont packets\n" |
| 2304 | " qC \tQuerying the current thread\n" |
| 2305 | " qfThreadInfo\tThread listing\n" |
| 2306 | " Tthread \tPassing the thread specifier in the T stop reply packet\n" |
| 2307 | " threads \tAll of the above\n"); |
| 2308 | } |
| 2309 | |
| 2310 | |
| 2311 | #undef require_running |
| 2312 | #define require_running(BUF) \ |
| 2313 | if (!target_running ()) \ |
| 2314 | { \ |
| 2315 | write_enn (BUF); \ |
| 2316 | break; \ |
| 2317 | } |
| 2318 | |
| 2319 | static int |
| 2320 | first_thread_of (struct inferior_list_entry *entry, void *args) |
| 2321 | { |
| 2322 | int pid = * (int *) args; |
| 2323 | |
| 2324 | if (ptid_get_pid (entry->id) == pid) |
| 2325 | return 1; |
| 2326 | |
| 2327 | return 0; |
| 2328 | } |
| 2329 | |
| 2330 | static void |
| 2331 | kill_inferior_callback (struct inferior_list_entry *entry) |
| 2332 | { |
| 2333 | struct process_info *process = (struct process_info *) entry; |
| 2334 | int pid = ptid_get_pid (process->head.id); |
| 2335 | |
| 2336 | kill_inferior (pid); |
| 2337 | discard_queued_stop_replies (pid); |
| 2338 | } |
| 2339 | |
| 2340 | /* Callback for for_each_inferior to detach or kill the inferior, |
| 2341 | depending on whether we attached to it or not. |
| 2342 | We inform the user whether we're detaching or killing the process |
| 2343 | as this is only called when gdbserver is about to exit. */ |
| 2344 | |
| 2345 | static void |
| 2346 | detach_or_kill_inferior_callback (struct inferior_list_entry *entry) |
| 2347 | { |
| 2348 | struct process_info *process = (struct process_info *) entry; |
| 2349 | int pid = ptid_get_pid (process->head.id); |
| 2350 | |
| 2351 | if (process->attached) |
| 2352 | detach_inferior (pid); |
| 2353 | else |
| 2354 | kill_inferior (pid); |
| 2355 | |
| 2356 | discard_queued_stop_replies (pid); |
| 2357 | } |
| 2358 | |
| 2359 | /* for_each_inferior callback for detach_or_kill_for_exit to print |
| 2360 | the pids of started inferiors. */ |
| 2361 | |
| 2362 | static void |
| 2363 | print_started_pid (struct inferior_list_entry *entry) |
| 2364 | { |
| 2365 | struct process_info *process = (struct process_info *) entry; |
| 2366 | |
| 2367 | if (! process->attached) |
| 2368 | { |
| 2369 | int pid = ptid_get_pid (process->head.id); |
| 2370 | fprintf (stderr, " %d", pid); |
| 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | /* for_each_inferior callback for detach_or_kill_for_exit to print |
| 2375 | the pids of attached inferiors. */ |
| 2376 | |
| 2377 | static void |
| 2378 | print_attached_pid (struct inferior_list_entry *entry) |
| 2379 | { |
| 2380 | struct process_info *process = (struct process_info *) entry; |
| 2381 | |
| 2382 | if (process->attached) |
| 2383 | { |
| 2384 | int pid = ptid_get_pid (process->head.id); |
| 2385 | fprintf (stderr, " %d", pid); |
| 2386 | } |
| 2387 | } |
| 2388 | |
| 2389 | /* Call this when exiting gdbserver with possible inferiors that need |
| 2390 | to be killed or detached from. */ |
| 2391 | |
| 2392 | static void |
| 2393 | detach_or_kill_for_exit (void) |
| 2394 | { |
| 2395 | /* First print a list of the inferiors we will be killing/detaching. |
| 2396 | This is to assist the user, for example, in case the inferior unexpectedly |
| 2397 | dies after we exit: did we screw up or did the inferior exit on its own? |
| 2398 | Having this info will save some head-scratching. */ |
| 2399 | |
| 2400 | if (have_started_inferiors_p ()) |
| 2401 | { |
| 2402 | fprintf (stderr, "Killing process(es):"); |
| 2403 | for_each_inferior (&all_processes, print_started_pid); |
| 2404 | fprintf (stderr, "\n"); |
| 2405 | } |
| 2406 | if (have_attached_inferiors_p ()) |
| 2407 | { |
| 2408 | fprintf (stderr, "Detaching process(es):"); |
| 2409 | for_each_inferior (&all_processes, print_attached_pid); |
| 2410 | fprintf (stderr, "\n"); |
| 2411 | } |
| 2412 | |
| 2413 | /* Now we can kill or detach the inferiors. */ |
| 2414 | |
| 2415 | for_each_inferior (&all_processes, detach_or_kill_inferior_callback); |
| 2416 | } |
| 2417 | |
| 2418 | static void |
| 2419 | join_inferiors_callback (struct inferior_list_entry *entry) |
| 2420 | { |
| 2421 | struct process_info *process = (struct process_info *) entry; |
| 2422 | |
| 2423 | /* If we are attached, then we can exit. Otherwise, we need to hang |
| 2424 | around doing nothing, until the child is gone. */ |
| 2425 | if (!process->attached) |
| 2426 | join_inferior (ptid_get_pid (process->head.id)); |
| 2427 | } |
| 2428 | |
| 2429 | int |
| 2430 | main (int argc, char *argv[]) |
| 2431 | { |
| 2432 | int bad_attach; |
| 2433 | int pid; |
| 2434 | char *arg_end, *port; |
| 2435 | char **next_arg = &argv[1]; |
| 2436 | int multi_mode = 0; |
| 2437 | int attach = 0; |
| 2438 | int was_running; |
| 2439 | |
| 2440 | while (*next_arg != NULL && **next_arg == '-') |
| 2441 | { |
| 2442 | if (strcmp (*next_arg, "--version") == 0) |
| 2443 | { |
| 2444 | gdbserver_version (); |
| 2445 | exit (0); |
| 2446 | } |
| 2447 | else if (strcmp (*next_arg, "--help") == 0) |
| 2448 | { |
| 2449 | gdbserver_usage (stdout); |
| 2450 | exit (0); |
| 2451 | } |
| 2452 | else if (strcmp (*next_arg, "--attach") == 0) |
| 2453 | attach = 1; |
| 2454 | else if (strcmp (*next_arg, "--multi") == 0) |
| 2455 | multi_mode = 1; |
| 2456 | else if (strcmp (*next_arg, "--wrapper") == 0) |
| 2457 | { |
| 2458 | next_arg++; |
| 2459 | |
| 2460 | wrapper_argv = next_arg; |
| 2461 | while (*next_arg != NULL && strcmp (*next_arg, "--") != 0) |
| 2462 | next_arg++; |
| 2463 | |
| 2464 | if (next_arg == wrapper_argv || *next_arg == NULL) |
| 2465 | { |
| 2466 | gdbserver_usage (stderr); |
| 2467 | exit (1); |
| 2468 | } |
| 2469 | |
| 2470 | /* Consume the "--". */ |
| 2471 | *next_arg = NULL; |
| 2472 | } |
| 2473 | else if (strcmp (*next_arg, "--debug") == 0) |
| 2474 | debug_threads = 1; |
| 2475 | else if (strcmp (*next_arg, "--remote-debug") == 0) |
| 2476 | remote_debug = 1; |
| 2477 | else if (strcmp (*next_arg, "--disable-packet") == 0) |
| 2478 | { |
| 2479 | gdbserver_show_disableable (stdout); |
| 2480 | exit (0); |
| 2481 | } |
| 2482 | else if (strncmp (*next_arg, |
| 2483 | "--disable-packet=", |
| 2484 | sizeof ("--disable-packet=") - 1) == 0) |
| 2485 | { |
| 2486 | char *packets, *tok; |
| 2487 | |
| 2488 | packets = *next_arg += sizeof ("--disable-packet=") - 1; |
| 2489 | for (tok = strtok (packets, ","); |
| 2490 | tok != NULL; |
| 2491 | tok = strtok (NULL, ",")) |
| 2492 | { |
| 2493 | if (strcmp ("vCont", tok) == 0) |
| 2494 | disable_packet_vCont = 1; |
| 2495 | else if (strcmp ("Tthread", tok) == 0) |
| 2496 | disable_packet_Tthread = 1; |
| 2497 | else if (strcmp ("qC", tok) == 0) |
| 2498 | disable_packet_qC = 1; |
| 2499 | else if (strcmp ("qfThreadInfo", tok) == 0) |
| 2500 | disable_packet_qfThreadInfo = 1; |
| 2501 | else if (strcmp ("threads", tok) == 0) |
| 2502 | { |
| 2503 | disable_packet_vCont = 1; |
| 2504 | disable_packet_Tthread = 1; |
| 2505 | disable_packet_qC = 1; |
| 2506 | disable_packet_qfThreadInfo = 1; |
| 2507 | } |
| 2508 | else |
| 2509 | { |
| 2510 | fprintf (stderr, "Don't know how to disable \"%s\".\n\n", |
| 2511 | tok); |
| 2512 | gdbserver_show_disableable (stderr); |
| 2513 | exit (1); |
| 2514 | } |
| 2515 | } |
| 2516 | } |
| 2517 | else |
| 2518 | { |
| 2519 | fprintf (stderr, "Unknown argument: %s\n", *next_arg); |
| 2520 | exit (1); |
| 2521 | } |
| 2522 | |
| 2523 | next_arg++; |
| 2524 | continue; |
| 2525 | } |
| 2526 | |
| 2527 | if (setjmp (toplevel)) |
| 2528 | { |
| 2529 | fprintf (stderr, "Exiting\n"); |
| 2530 | exit (1); |
| 2531 | } |
| 2532 | |
| 2533 | port = *next_arg; |
| 2534 | next_arg++; |
| 2535 | if (port == NULL || (!attach && !multi_mode && *next_arg == NULL)) |
| 2536 | { |
| 2537 | gdbserver_usage (stderr); |
| 2538 | exit (1); |
| 2539 | } |
| 2540 | |
| 2541 | bad_attach = 0; |
| 2542 | pid = 0; |
| 2543 | |
| 2544 | /* --attach used to come after PORT, so allow it there for |
| 2545 | compatibility. */ |
| 2546 | if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0) |
| 2547 | { |
| 2548 | attach = 1; |
| 2549 | next_arg++; |
| 2550 | } |
| 2551 | |
| 2552 | if (attach |
| 2553 | && (*next_arg == NULL |
| 2554 | || (*next_arg)[0] == '\0' |
| 2555 | || (pid = strtoul (*next_arg, &arg_end, 0)) == 0 |
| 2556 | || *arg_end != '\0' |
| 2557 | || next_arg[1] != NULL)) |
| 2558 | bad_attach = 1; |
| 2559 | |
| 2560 | if (bad_attach) |
| 2561 | { |
| 2562 | gdbserver_usage (stderr); |
| 2563 | exit (1); |
| 2564 | } |
| 2565 | |
| 2566 | initialize_inferiors (); |
| 2567 | initialize_async_io (); |
| 2568 | initialize_low (); |
| 2569 | if (target_supports_tracepoints ()) |
| 2570 | initialize_tracepoint (); |
| 2571 | |
| 2572 | own_buf = xmalloc (PBUFSIZ + 1); |
| 2573 | mem_buf = xmalloc (PBUFSIZ); |
| 2574 | |
| 2575 | if (pid == 0 && *next_arg != NULL) |
| 2576 | { |
| 2577 | int i, n; |
| 2578 | |
| 2579 | n = argc - (next_arg - argv); |
| 2580 | program_argv = xmalloc (sizeof (char *) * (n + 1)); |
| 2581 | for (i = 0; i < n; i++) |
| 2582 | program_argv[i] = xstrdup (next_arg[i]); |
| 2583 | program_argv[i] = NULL; |
| 2584 | |
| 2585 | /* Wait till we are at first instruction in program. */ |
| 2586 | start_inferior (program_argv); |
| 2587 | |
| 2588 | /* We are now (hopefully) stopped at the first instruction of |
| 2589 | the target process. This assumes that the target process was |
| 2590 | successfully created. */ |
| 2591 | } |
| 2592 | else if (pid != 0) |
| 2593 | { |
| 2594 | if (attach_inferior (pid) == -1) |
| 2595 | error ("Attaching not supported on this target"); |
| 2596 | |
| 2597 | /* Otherwise succeeded. */ |
| 2598 | } |
| 2599 | else |
| 2600 | { |
| 2601 | last_status.kind = TARGET_WAITKIND_EXITED; |
| 2602 | last_status.value.integer = 0; |
| 2603 | last_ptid = minus_one_ptid; |
| 2604 | } |
| 2605 | |
| 2606 | /* Don't report shared library events on the initial connection, |
| 2607 | even if some libraries are preloaded. Avoids the "stopped by |
| 2608 | shared library event" notice on gdb side. */ |
| 2609 | dlls_changed = 0; |
| 2610 | |
| 2611 | if (setjmp (toplevel)) |
| 2612 | { |
| 2613 | detach_or_kill_for_exit (); |
| 2614 | exit (1); |
| 2615 | } |
| 2616 | |
| 2617 | if (last_status.kind == TARGET_WAITKIND_EXITED |
| 2618 | || last_status.kind == TARGET_WAITKIND_SIGNALLED) |
| 2619 | was_running = 0; |
| 2620 | else |
| 2621 | was_running = 1; |
| 2622 | |
| 2623 | if (!was_running && !multi_mode) |
| 2624 | { |
| 2625 | fprintf (stderr, "No program to debug. GDBserver exiting.\n"); |
| 2626 | exit (1); |
| 2627 | } |
| 2628 | |
| 2629 | while (1) |
| 2630 | { |
| 2631 | noack_mode = 0; |
| 2632 | multi_process = 0; |
| 2633 | /* Be sure we're out of tfind mode. */ |
| 2634 | current_traceframe = -1; |
| 2635 | |
| 2636 | remote_open (port); |
| 2637 | |
| 2638 | if (setjmp (toplevel) != 0) |
| 2639 | { |
| 2640 | /* An error occurred. */ |
| 2641 | if (response_needed) |
| 2642 | { |
| 2643 | write_enn (own_buf); |
| 2644 | putpkt (own_buf); |
| 2645 | } |
| 2646 | } |
| 2647 | |
| 2648 | /* Wait for events. This will return when all event sources are |
| 2649 | removed from the event loop. */ |
| 2650 | start_event_loop (); |
| 2651 | |
| 2652 | /* If an exit was requested (using the "monitor exit" command), |
| 2653 | terminate now. The only other way to get here is for |
| 2654 | getpkt to fail; close the connection and reopen it at the |
| 2655 | top of the loop. */ |
| 2656 | |
| 2657 | if (exit_requested) |
| 2658 | { |
| 2659 | detach_or_kill_for_exit (); |
| 2660 | exit (0); |
| 2661 | } |
| 2662 | |
| 2663 | fprintf (stderr, |
| 2664 | "Remote side has terminated connection. " |
| 2665 | "GDBserver will reopen the connection.\n"); |
| 2666 | |
| 2667 | if (tracing) |
| 2668 | { |
| 2669 | if (disconnected_tracing) |
| 2670 | { |
| 2671 | /* Try to enable non-stop/async mode, so we we can both |
| 2672 | wait for an async socket accept, and handle async |
| 2673 | target events simultaneously. There's also no point |
| 2674 | either in having the target always stop all threads, |
| 2675 | when we're going to pass signals down without |
| 2676 | informing GDB. */ |
| 2677 | if (!non_stop) |
| 2678 | { |
| 2679 | if (start_non_stop (1)) |
| 2680 | non_stop = 1; |
| 2681 | |
| 2682 | /* Detaching implicitly resumes all threads; simply |
| 2683 | disconnecting does not. */ |
| 2684 | } |
| 2685 | } |
| 2686 | else |
| 2687 | { |
| 2688 | fprintf (stderr, |
| 2689 | "Disconnected tracing disabled; stopping trace run.\n"); |
| 2690 | stop_tracing (); |
| 2691 | } |
| 2692 | } |
| 2693 | } |
| 2694 | } |
| 2695 | |
| 2696 | /* Event loop callback that handles a serial event. The first byte in |
| 2697 | the serial buffer gets us here. We expect characters to arrive at |
| 2698 | a brisk pace, so we read the rest of the packet with a blocking |
| 2699 | getpkt call. */ |
| 2700 | |
| 2701 | static int |
| 2702 | process_serial_event (void) |
| 2703 | { |
| 2704 | char ch; |
| 2705 | int i = 0; |
| 2706 | int signal; |
| 2707 | unsigned int len; |
| 2708 | CORE_ADDR mem_addr; |
| 2709 | int pid; |
| 2710 | unsigned char sig; |
| 2711 | int packet_len; |
| 2712 | int new_packet_len = -1; |
| 2713 | |
| 2714 | /* Used to decide when gdbserver should exit in |
| 2715 | multi-mode/remote. */ |
| 2716 | static int have_ran = 0; |
| 2717 | |
| 2718 | if (!have_ran) |
| 2719 | have_ran = target_running (); |
| 2720 | |
| 2721 | disable_async_io (); |
| 2722 | |
| 2723 | response_needed = 0; |
| 2724 | packet_len = getpkt (own_buf); |
| 2725 | if (packet_len <= 0) |
| 2726 | { |
| 2727 | remote_close (); |
| 2728 | /* Force an event loop break. */ |
| 2729 | return -1; |
| 2730 | } |
| 2731 | response_needed = 1; |
| 2732 | |
| 2733 | i = 0; |
| 2734 | ch = own_buf[i++]; |
| 2735 | switch (ch) |
| 2736 | { |
| 2737 | case 'q': |
| 2738 | handle_query (own_buf, packet_len, &new_packet_len); |
| 2739 | break; |
| 2740 | case 'Q': |
| 2741 | handle_general_set (own_buf); |
| 2742 | break; |
| 2743 | case 'D': |
| 2744 | require_running (own_buf); |
| 2745 | |
| 2746 | if (multi_process) |
| 2747 | { |
| 2748 | i++; /* skip ';' */ |
| 2749 | pid = strtol (&own_buf[i], NULL, 16); |
| 2750 | } |
| 2751 | else |
| 2752 | pid = |
| 2753 | ptid_get_pid (((struct inferior_list_entry *) current_inferior)->id); |
| 2754 | |
| 2755 | if (tracing && disconnected_tracing) |
| 2756 | { |
| 2757 | struct thread_resume resume_info; |
| 2758 | struct process_info *process = find_process_pid (pid); |
| 2759 | |
| 2760 | if (process == NULL) |
| 2761 | { |
| 2762 | write_enn (own_buf); |
| 2763 | break; |
| 2764 | } |
| 2765 | |
| 2766 | fprintf (stderr, |
| 2767 | "Disconnected tracing in effect, " |
| 2768 | "leaving gdbserver attached to the process\n"); |
| 2769 | |
| 2770 | /* Make sure we're in non-stop/async mode, so we we can both |
| 2771 | wait for an async socket accept, and handle async target |
| 2772 | events simultaneously. There's also no point either in |
| 2773 | having the target stop all threads, when we're going to |
| 2774 | pass signals down without informing GDB. */ |
| 2775 | if (!non_stop) |
| 2776 | { |
| 2777 | if (debug_threads) |
| 2778 | fprintf (stderr, "Forcing non-stop mode\n"); |
| 2779 | |
| 2780 | non_stop = 1; |
| 2781 | start_non_stop (1); |
| 2782 | } |
| 2783 | |
| 2784 | process->gdb_detached = 1; |
| 2785 | |
| 2786 | /* Detaching implicitly resumes all threads. */ |
| 2787 | resume_info.thread = minus_one_ptid; |
| 2788 | resume_info.kind = resume_continue; |
| 2789 | resume_info.sig = 0; |
| 2790 | (*the_target->resume) (&resume_info, 1); |
| 2791 | |
| 2792 | write_ok (own_buf); |
| 2793 | break; /* from switch/case */ |
| 2794 | } |
| 2795 | |
| 2796 | fprintf (stderr, "Detaching from process %d\n", pid); |
| 2797 | stop_tracing (); |
| 2798 | if (detach_inferior (pid) != 0) |
| 2799 | write_enn (own_buf); |
| 2800 | else |
| 2801 | { |
| 2802 | discard_queued_stop_replies (pid); |
| 2803 | write_ok (own_buf); |
| 2804 | |
| 2805 | if (extended_protocol) |
| 2806 | { |
| 2807 | /* Treat this like a normal program exit. */ |
| 2808 | last_status.kind = TARGET_WAITKIND_EXITED; |
| 2809 | last_status.value.integer = 0; |
| 2810 | last_ptid = pid_to_ptid (pid); |
| 2811 | |
| 2812 | current_inferior = NULL; |
| 2813 | } |
| 2814 | else |
| 2815 | { |
| 2816 | putpkt (own_buf); |
| 2817 | remote_close (); |
| 2818 | |
| 2819 | /* If we are attached, then we can exit. Otherwise, we |
| 2820 | need to hang around doing nothing, until the child is |
| 2821 | gone. */ |
| 2822 | for_each_inferior (&all_processes, |
| 2823 | join_inferiors_callback); |
| 2824 | exit (0); |
| 2825 | } |
| 2826 | } |
| 2827 | break; |
| 2828 | case '!': |
| 2829 | extended_protocol = 1; |
| 2830 | write_ok (own_buf); |
| 2831 | break; |
| 2832 | case '?': |
| 2833 | handle_status (own_buf); |
| 2834 | break; |
| 2835 | case 'H': |
| 2836 | if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') |
| 2837 | { |
| 2838 | ptid_t gdb_id, thread_id; |
| 2839 | int pid; |
| 2840 | |
| 2841 | require_running (own_buf); |
| 2842 | |
| 2843 | gdb_id = read_ptid (&own_buf[2], NULL); |
| 2844 | |
| 2845 | pid = ptid_get_pid (gdb_id); |
| 2846 | |
| 2847 | if (ptid_equal (gdb_id, null_ptid) |
| 2848 | || ptid_equal (gdb_id, minus_one_ptid)) |
| 2849 | thread_id = null_ptid; |
| 2850 | else if (pid != 0 |
| 2851 | && ptid_equal (pid_to_ptid (pid), |
| 2852 | gdb_id)) |
| 2853 | { |
| 2854 | struct thread_info *thread = |
| 2855 | (struct thread_info *) find_inferior (&all_threads, |
| 2856 | first_thread_of, |
| 2857 | &pid); |
| 2858 | if (!thread) |
| 2859 | { |
| 2860 | write_enn (own_buf); |
| 2861 | break; |
| 2862 | } |
| 2863 | |
| 2864 | thread_id = ((struct inferior_list_entry *)thread)->id; |
| 2865 | } |
| 2866 | else |
| 2867 | { |
| 2868 | thread_id = gdb_id_to_thread_id (gdb_id); |
| 2869 | if (ptid_equal (thread_id, null_ptid)) |
| 2870 | { |
| 2871 | write_enn (own_buf); |
| 2872 | break; |
| 2873 | } |
| 2874 | } |
| 2875 | |
| 2876 | if (own_buf[1] == 'g') |
| 2877 | { |
| 2878 | if (ptid_equal (thread_id, null_ptid)) |
| 2879 | { |
| 2880 | /* GDB is telling us to choose any thread. Check if |
| 2881 | the currently selected thread is still valid. If |
| 2882 | it is not, select the first available. */ |
| 2883 | struct thread_info *thread = |
| 2884 | (struct thread_info *) find_inferior_id (&all_threads, |
| 2885 | general_thread); |
| 2886 | if (thread == NULL) |
| 2887 | thread_id = all_threads.head->id; |
| 2888 | } |
| 2889 | |
| 2890 | general_thread = thread_id; |
| 2891 | set_desired_inferior (1); |
| 2892 | } |
| 2893 | else if (own_buf[1] == 'c') |
| 2894 | cont_thread = thread_id; |
| 2895 | else if (own_buf[1] == 's') |
| 2896 | step_thread = thread_id; |
| 2897 | |
| 2898 | write_ok (own_buf); |
| 2899 | } |
| 2900 | else |
| 2901 | { |
| 2902 | /* Silently ignore it so that gdb can extend the protocol |
| 2903 | without compatibility headaches. */ |
| 2904 | own_buf[0] = '\0'; |
| 2905 | } |
| 2906 | break; |
| 2907 | case 'g': |
| 2908 | require_running (own_buf); |
| 2909 | if (current_traceframe >= 0) |
| 2910 | { |
| 2911 | struct regcache *regcache = new_register_cache (); |
| 2912 | |
| 2913 | if (fetch_traceframe_registers (current_traceframe, |
| 2914 | regcache, -1) == 0) |
| 2915 | registers_to_string (regcache, own_buf); |
| 2916 | else |
| 2917 | write_enn (own_buf); |
| 2918 | free_register_cache (regcache); |
| 2919 | } |
| 2920 | else |
| 2921 | { |
| 2922 | struct regcache *regcache; |
| 2923 | |
| 2924 | set_desired_inferior (1); |
| 2925 | regcache = get_thread_regcache (current_inferior, 1); |
| 2926 | registers_to_string (regcache, own_buf); |
| 2927 | } |
| 2928 | break; |
| 2929 | case 'G': |
| 2930 | require_running (own_buf); |
| 2931 | if (current_traceframe >= 0) |
| 2932 | write_enn (own_buf); |
| 2933 | else |
| 2934 | { |
| 2935 | struct regcache *regcache; |
| 2936 | |
| 2937 | set_desired_inferior (1); |
| 2938 | regcache = get_thread_regcache (current_inferior, 1); |
| 2939 | registers_from_string (regcache, &own_buf[1]); |
| 2940 | write_ok (own_buf); |
| 2941 | } |
| 2942 | break; |
| 2943 | case 'm': |
| 2944 | require_running (own_buf); |
| 2945 | decode_m_packet (&own_buf[1], &mem_addr, &len); |
| 2946 | if (gdb_read_memory (mem_addr, mem_buf, len) == 0) |
| 2947 | convert_int_to_ascii (mem_buf, own_buf, len); |
| 2948 | else |
| 2949 | write_enn (own_buf); |
| 2950 | break; |
| 2951 | case 'M': |
| 2952 | require_running (own_buf); |
| 2953 | decode_M_packet (&own_buf[1], &mem_addr, &len, &mem_buf); |
| 2954 | if (gdb_write_memory (mem_addr, mem_buf, len) == 0) |
| 2955 | write_ok (own_buf); |
| 2956 | else |
| 2957 | write_enn (own_buf); |
| 2958 | break; |
| 2959 | case 'X': |
| 2960 | require_running (own_buf); |
| 2961 | if (decode_X_packet (&own_buf[1], packet_len - 1, |
| 2962 | &mem_addr, &len, &mem_buf) < 0 |
| 2963 | || gdb_write_memory (mem_addr, mem_buf, len) != 0) |
| 2964 | write_enn (own_buf); |
| 2965 | else |
| 2966 | write_ok (own_buf); |
| 2967 | break; |
| 2968 | case 'C': |
| 2969 | require_running (own_buf); |
| 2970 | convert_ascii_to_int (own_buf + 1, &sig, 1); |
| 2971 | if (target_signal_to_host_p (sig)) |
| 2972 | signal = target_signal_to_host (sig); |
| 2973 | else |
| 2974 | signal = 0; |
| 2975 | myresume (own_buf, 0, signal); |
| 2976 | break; |
| 2977 | case 'S': |
| 2978 | require_running (own_buf); |
| 2979 | convert_ascii_to_int (own_buf + 1, &sig, 1); |
| 2980 | if (target_signal_to_host_p (sig)) |
| 2981 | signal = target_signal_to_host (sig); |
| 2982 | else |
| 2983 | signal = 0; |
| 2984 | myresume (own_buf, 1, signal); |
| 2985 | break; |
| 2986 | case 'c': |
| 2987 | require_running (own_buf); |
| 2988 | signal = 0; |
| 2989 | myresume (own_buf, 0, signal); |
| 2990 | break; |
| 2991 | case 's': |
| 2992 | require_running (own_buf); |
| 2993 | signal = 0; |
| 2994 | myresume (own_buf, 1, signal); |
| 2995 | break; |
| 2996 | case 'Z': /* insert_ ... */ |
| 2997 | /* Fallthrough. */ |
| 2998 | case 'z': /* remove_ ... */ |
| 2999 | { |
| 3000 | char *lenptr; |
| 3001 | char *dataptr; |
| 3002 | CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); |
| 3003 | int len = strtol (lenptr + 1, &dataptr, 16); |
| 3004 | char type = own_buf[1]; |
| 3005 | int res; |
| 3006 | const int insert = ch == 'Z'; |
| 3007 | |
| 3008 | /* Default to unrecognized/unsupported. */ |
| 3009 | res = 1; |
| 3010 | switch (type) |
| 3011 | { |
| 3012 | case '0': /* software-breakpoint */ |
| 3013 | case '1': /* hardware-breakpoint */ |
| 3014 | case '2': /* write watchpoint */ |
| 3015 | case '3': /* read watchpoint */ |
| 3016 | case '4': /* access watchpoint */ |
| 3017 | require_running (own_buf); |
| 3018 | if (insert && the_target->insert_point != NULL) |
| 3019 | res = (*the_target->insert_point) (type, addr, len); |
| 3020 | else if (!insert && the_target->remove_point != NULL) |
| 3021 | res = (*the_target->remove_point) (type, addr, len); |
| 3022 | break; |
| 3023 | default: |
| 3024 | break; |
| 3025 | } |
| 3026 | |
| 3027 | if (res == 0) |
| 3028 | write_ok (own_buf); |
| 3029 | else if (res == 1) |
| 3030 | /* Unsupported. */ |
| 3031 | own_buf[0] = '\0'; |
| 3032 | else |
| 3033 | write_enn (own_buf); |
| 3034 | break; |
| 3035 | } |
| 3036 | case 'k': |
| 3037 | response_needed = 0; |
| 3038 | if (!target_running ()) |
| 3039 | /* The packet we received doesn't make sense - but we can't |
| 3040 | reply to it, either. */ |
| 3041 | return 0; |
| 3042 | |
| 3043 | fprintf (stderr, "Killing all inferiors\n"); |
| 3044 | for_each_inferior (&all_processes, kill_inferior_callback); |
| 3045 | |
| 3046 | /* When using the extended protocol, we wait with no program |
| 3047 | running. The traditional protocol will exit instead. */ |
| 3048 | if (extended_protocol) |
| 3049 | { |
| 3050 | last_status.kind = TARGET_WAITKIND_EXITED; |
| 3051 | last_status.value.sig = TARGET_SIGNAL_KILL; |
| 3052 | return 0; |
| 3053 | } |
| 3054 | else |
| 3055 | exit (0); |
| 3056 | |
| 3057 | case 'T': |
| 3058 | { |
| 3059 | ptid_t gdb_id, thread_id; |
| 3060 | |
| 3061 | require_running (own_buf); |
| 3062 | |
| 3063 | gdb_id = read_ptid (&own_buf[1], NULL); |
| 3064 | thread_id = gdb_id_to_thread_id (gdb_id); |
| 3065 | if (ptid_equal (thread_id, null_ptid)) |
| 3066 | { |
| 3067 | write_enn (own_buf); |
| 3068 | break; |
| 3069 | } |
| 3070 | |
| 3071 | if (mythread_alive (thread_id)) |
| 3072 | write_ok (own_buf); |
| 3073 | else |
| 3074 | write_enn (own_buf); |
| 3075 | } |
| 3076 | break; |
| 3077 | case 'R': |
| 3078 | response_needed = 0; |
| 3079 | |
| 3080 | /* Restarting the inferior is only supported in the extended |
| 3081 | protocol. */ |
| 3082 | if (extended_protocol) |
| 3083 | { |
| 3084 | if (target_running ()) |
| 3085 | for_each_inferior (&all_processes, |
| 3086 | kill_inferior_callback); |
| 3087 | fprintf (stderr, "GDBserver restarting\n"); |
| 3088 | |
| 3089 | /* Wait till we are at 1st instruction in prog. */ |
| 3090 | if (program_argv != NULL) |
| 3091 | start_inferior (program_argv); |
| 3092 | else |
| 3093 | { |
| 3094 | last_status.kind = TARGET_WAITKIND_EXITED; |
| 3095 | last_status.value.sig = TARGET_SIGNAL_KILL; |
| 3096 | } |
| 3097 | return 0; |
| 3098 | } |
| 3099 | else |
| 3100 | { |
| 3101 | /* It is a request we don't understand. Respond with an |
| 3102 | empty packet so that gdb knows that we don't support this |
| 3103 | request. */ |
| 3104 | own_buf[0] = '\0'; |
| 3105 | break; |
| 3106 | } |
| 3107 | case 'v': |
| 3108 | /* Extended (long) request. */ |
| 3109 | handle_v_requests (own_buf, packet_len, &new_packet_len); |
| 3110 | break; |
| 3111 | |
| 3112 | default: |
| 3113 | /* It is a request we don't understand. Respond with an empty |
| 3114 | packet so that gdb knows that we don't support this |
| 3115 | request. */ |
| 3116 | own_buf[0] = '\0'; |
| 3117 | break; |
| 3118 | } |
| 3119 | |
| 3120 | if (new_packet_len != -1) |
| 3121 | putpkt_binary (own_buf, new_packet_len); |
| 3122 | else |
| 3123 | putpkt (own_buf); |
| 3124 | |
| 3125 | response_needed = 0; |
| 3126 | |
| 3127 | if (!extended_protocol && have_ran && !target_running ()) |
| 3128 | { |
| 3129 | /* In non-stop, defer exiting until GDB had a chance to query |
| 3130 | the whole vStopped list (until it gets an OK). */ |
| 3131 | if (!notif_queue) |
| 3132 | { |
| 3133 | fprintf (stderr, "GDBserver exiting\n"); |
| 3134 | remote_close (); |
| 3135 | exit (0); |
| 3136 | } |
| 3137 | } |
| 3138 | |
| 3139 | if (exit_requested) |
| 3140 | return -1; |
| 3141 | |
| 3142 | return 0; |
| 3143 | } |
| 3144 | |
| 3145 | /* Event-loop callback for serial events. */ |
| 3146 | |
| 3147 | int |
| 3148 | handle_serial_event (int err, gdb_client_data client_data) |
| 3149 | { |
| 3150 | if (debug_threads) |
| 3151 | fprintf (stderr, "handling possible serial event\n"); |
| 3152 | |
| 3153 | /* Really handle it. */ |
| 3154 | if (process_serial_event () < 0) |
| 3155 | return -1; |
| 3156 | |
| 3157 | /* Be sure to not change the selected inferior behind GDB's back. |
| 3158 | Important in the non-stop mode asynchronous protocol. */ |
| 3159 | set_desired_inferior (1); |
| 3160 | |
| 3161 | return 0; |
| 3162 | } |
| 3163 | |
| 3164 | /* Event-loop callback for target events. */ |
| 3165 | |
| 3166 | int |
| 3167 | handle_target_event (int err, gdb_client_data client_data) |
| 3168 | { |
| 3169 | if (debug_threads) |
| 3170 | fprintf (stderr, "handling possible target event\n"); |
| 3171 | |
| 3172 | last_ptid = mywait (minus_one_ptid, &last_status, |
| 3173 | TARGET_WNOHANG, 1); |
| 3174 | |
| 3175 | if (last_status.kind != TARGET_WAITKIND_IGNORE) |
| 3176 | { |
| 3177 | int pid = ptid_get_pid (last_ptid); |
| 3178 | struct process_info *process = find_process_pid (pid); |
| 3179 | int forward_event = !gdb_connected () || process->gdb_detached; |
| 3180 | |
| 3181 | if (last_status.kind == TARGET_WAITKIND_EXITED |
| 3182 | || last_status.kind == TARGET_WAITKIND_SIGNALLED) |
| 3183 | { |
| 3184 | mark_breakpoints_out (process); |
| 3185 | mourn_inferior (process); |
| 3186 | } |
| 3187 | else |
| 3188 | { |
| 3189 | /* We're reporting this thread as stopped. Update its |
| 3190 | "want-stopped" state to what the client wants, until it |
| 3191 | gets a new resume action. */ |
| 3192 | current_inferior->last_resume_kind = resume_stop; |
| 3193 | current_inferior->last_status = last_status; |
| 3194 | } |
| 3195 | |
| 3196 | if (forward_event) |
| 3197 | { |
| 3198 | if (!target_running ()) |
| 3199 | { |
| 3200 | /* The last process exited. We're done. */ |
| 3201 | exit (0); |
| 3202 | } |
| 3203 | |
| 3204 | if (last_status.kind == TARGET_WAITKIND_STOPPED) |
| 3205 | { |
| 3206 | /* A thread stopped with a signal, but gdb isn't |
| 3207 | connected to handle it. Pass it down to the |
| 3208 | inferior, as if it wasn't being traced. */ |
| 3209 | struct thread_resume resume_info; |
| 3210 | |
| 3211 | if (debug_threads) |
| 3212 | fprintf (stderr, |
| 3213 | "GDB not connected; forwarding event %d for [%s]\n", |
| 3214 | (int) last_status.kind, |
| 3215 | target_pid_to_str (last_ptid)); |
| 3216 | |
| 3217 | resume_info.thread = last_ptid; |
| 3218 | resume_info.kind = resume_continue; |
| 3219 | resume_info.sig = target_signal_to_host (last_status.value.sig); |
| 3220 | (*the_target->resume) (&resume_info, 1); |
| 3221 | } |
| 3222 | else if (debug_threads) |
| 3223 | fprintf (stderr, "GDB not connected; ignoring event %d for [%s]\n", |
| 3224 | (int) last_status.kind, |
| 3225 | target_pid_to_str (last_ptid)); |
| 3226 | } |
| 3227 | else |
| 3228 | { |
| 3229 | /* Something interesting. Tell GDB about it. */ |
| 3230 | push_event (last_ptid, &last_status); |
| 3231 | } |
| 3232 | } |
| 3233 | |
| 3234 | /* Be sure to not change the selected inferior behind GDB's back. |
| 3235 | Important in the non-stop mode asynchronous protocol. */ |
| 3236 | set_desired_inferior (1); |
| 3237 | |
| 3238 | return 0; |
| 3239 | } |