| 1 | /* Main code for remote server for GDB. |
| 2 | Copyright (C) 1989-2014 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include "server.h" |
| 20 | #include "gdbthread.h" |
| 21 | #include "agent.h" |
| 22 | #include "notif.h" |
| 23 | #include "tdesc.h" |
| 24 | #include "rsp-low.h" |
| 25 | |
| 26 | #include <ctype.h> |
| 27 | #include <unistd.h> |
| 28 | #if HAVE_SIGNAL_H |
| 29 | #include <signal.h> |
| 30 | #endif |
| 31 | #include "gdb_vecs.h" |
| 32 | #include "gdb_wait.h" |
| 33 | #include "btrace-common.h" |
| 34 | #include "filestuff.h" |
| 35 | #include "tracepoint.h" |
| 36 | #include "dll.h" |
| 37 | #include "hostio.h" |
| 38 | |
| 39 | /* The thread set with an `Hc' packet. `Hc' is deprecated in favor of |
| 40 | `vCont'. Note the multi-process extensions made `vCont' a |
| 41 | requirement, so `Hc pPID.TID' is pretty much undefined. So |
| 42 | CONT_THREAD can be null_ptid for no `Hc' thread, minus_one_ptid for |
| 43 | resuming all threads of the process (again, `Hc' isn't used for |
| 44 | multi-process), or a specific thread ptid_t. */ |
| 45 | ptid_t cont_thread; |
| 46 | |
| 47 | /* The thread set with an `Hg' packet. */ |
| 48 | ptid_t general_thread; |
| 49 | |
| 50 | int server_waiting; |
| 51 | |
| 52 | static int extended_protocol; |
| 53 | static int response_needed; |
| 54 | static int exit_requested; |
| 55 | |
| 56 | /* --once: Exit after the first connection has closed. */ |
| 57 | int run_once; |
| 58 | |
| 59 | int multi_process; |
| 60 | int non_stop; |
| 61 | |
| 62 | /* Whether we should attempt to disable the operating system's address |
| 63 | space randomization feature before starting an inferior. */ |
| 64 | int disable_randomization = 1; |
| 65 | |
| 66 | static char **program_argv, **wrapper_argv; |
| 67 | |
| 68 | int pass_signals[GDB_SIGNAL_LAST]; |
| 69 | int program_signals[GDB_SIGNAL_LAST]; |
| 70 | int program_signals_p; |
| 71 | |
| 72 | /* The PID of the originally created or attached inferior. Used to |
| 73 | send signals to the process when GDB sends us an asynchronous interrupt |
| 74 | (user hitting Control-C in the client), and to wait for the child to exit |
| 75 | when no longer debugging it. */ |
| 76 | |
| 77 | unsigned long signal_pid; |
| 78 | |
| 79 | #ifdef SIGTTOU |
| 80 | /* A file descriptor for the controlling terminal. */ |
| 81 | int terminal_fd; |
| 82 | |
| 83 | /* TERMINAL_FD's original foreground group. */ |
| 84 | pid_t old_foreground_pgrp; |
| 85 | |
| 86 | /* Hand back terminal ownership to the original foreground group. */ |
| 87 | |
| 88 | static void |
| 89 | restore_old_foreground_pgrp (void) |
| 90 | { |
| 91 | tcsetpgrp (terminal_fd, old_foreground_pgrp); |
| 92 | } |
| 93 | #endif |
| 94 | |
| 95 | /* Set if you want to disable optional thread related packets support |
| 96 | in gdbserver, for the sake of testing GDB against stubs that don't |
| 97 | support them. */ |
| 98 | int disable_packet_vCont; |
| 99 | int disable_packet_Tthread; |
| 100 | int disable_packet_qC; |
| 101 | int disable_packet_qfThreadInfo; |
| 102 | |
| 103 | /* Last status reported to GDB. */ |
| 104 | static struct target_waitstatus last_status; |
| 105 | static ptid_t last_ptid; |
| 106 | |
| 107 | static char *own_buf; |
| 108 | static unsigned char *mem_buf; |
| 109 | |
| 110 | /* A sub-class of 'struct notif_event' for stop, holding information |
| 111 | relative to a single stop reply. We keep a queue of these to |
| 112 | push to GDB in non-stop mode. */ |
| 113 | |
| 114 | struct vstop_notif |
| 115 | { |
| 116 | struct notif_event base; |
| 117 | |
| 118 | /* Thread or process that got the event. */ |
| 119 | ptid_t ptid; |
| 120 | |
| 121 | /* Event info. */ |
| 122 | struct target_waitstatus status; |
| 123 | }; |
| 124 | |
| 125 | DEFINE_QUEUE_P (notif_event_p); |
| 126 | |
| 127 | /* Put a stop reply to the stop reply queue. */ |
| 128 | |
| 129 | static void |
| 130 | queue_stop_reply (ptid_t ptid, struct target_waitstatus *status) |
| 131 | { |
| 132 | struct vstop_notif *new_notif = xmalloc (sizeof (*new_notif)); |
| 133 | |
| 134 | new_notif->ptid = ptid; |
| 135 | new_notif->status = *status; |
| 136 | |
| 137 | notif_event_enque (¬if_stop, (struct notif_event *) new_notif); |
| 138 | } |
| 139 | |
| 140 | static int |
| 141 | remove_all_on_match_pid (QUEUE (notif_event_p) *q, |
| 142 | QUEUE_ITER (notif_event_p) *iter, |
| 143 | struct notif_event *event, |
| 144 | void *data) |
| 145 | { |
| 146 | int *pid = data; |
| 147 | |
| 148 | if (*pid == -1 |
| 149 | || ptid_get_pid (((struct vstop_notif *) event)->ptid) == *pid) |
| 150 | { |
| 151 | if (q->free_func != NULL) |
| 152 | q->free_func (event); |
| 153 | |
| 154 | QUEUE_remove_elem (notif_event_p, q, iter); |
| 155 | } |
| 156 | |
| 157 | return 1; |
| 158 | } |
| 159 | |
| 160 | /* Get rid of the currently pending stop replies for PID. If PID is |
| 161 | -1, then apply to all processes. */ |
| 162 | |
| 163 | static void |
| 164 | discard_queued_stop_replies (int pid) |
| 165 | { |
| 166 | QUEUE_iterate (notif_event_p, notif_stop.queue, |
| 167 | remove_all_on_match_pid, &pid); |
| 168 | } |
| 169 | |
| 170 | static void |
| 171 | vstop_notif_reply (struct notif_event *event, char *own_buf) |
| 172 | { |
| 173 | struct vstop_notif *vstop = (struct vstop_notif *) event; |
| 174 | |
| 175 | prepare_resume_reply (own_buf, vstop->ptid, &vstop->status); |
| 176 | } |
| 177 | |
| 178 | struct notif_server notif_stop = |
| 179 | { |
| 180 | "vStopped", "Stop", NULL, vstop_notif_reply, |
| 181 | }; |
| 182 | |
| 183 | static int |
| 184 | target_running (void) |
| 185 | { |
| 186 | return get_first_thread () != NULL; |
| 187 | } |
| 188 | |
| 189 | static int |
| 190 | start_inferior (char **argv) |
| 191 | { |
| 192 | char **new_argv = argv; |
| 193 | |
| 194 | if (wrapper_argv != NULL) |
| 195 | { |
| 196 | int i, count = 1; |
| 197 | |
| 198 | for (i = 0; wrapper_argv[i] != NULL; i++) |
| 199 | count++; |
| 200 | for (i = 0; argv[i] != NULL; i++) |
| 201 | count++; |
| 202 | new_argv = alloca (sizeof (char *) * count); |
| 203 | count = 0; |
| 204 | for (i = 0; wrapper_argv[i] != NULL; i++) |
| 205 | new_argv[count++] = wrapper_argv[i]; |
| 206 | for (i = 0; argv[i] != NULL; i++) |
| 207 | new_argv[count++] = argv[i]; |
| 208 | new_argv[count] = NULL; |
| 209 | } |
| 210 | |
| 211 | if (debug_threads) |
| 212 | { |
| 213 | int i; |
| 214 | for (i = 0; new_argv[i]; ++i) |
| 215 | debug_printf ("new_argv[%d] = \"%s\"\n", i, new_argv[i]); |
| 216 | debug_flush (); |
| 217 | } |
| 218 | |
| 219 | #ifdef SIGTTOU |
| 220 | signal (SIGTTOU, SIG_DFL); |
| 221 | signal (SIGTTIN, SIG_DFL); |
| 222 | #endif |
| 223 | |
| 224 | signal_pid = create_inferior (new_argv[0], new_argv); |
| 225 | |
| 226 | /* FIXME: we don't actually know at this point that the create |
| 227 | actually succeeded. We won't know that until we wait. */ |
| 228 | fprintf (stderr, "Process %s created; pid = %ld\n", argv[0], |
| 229 | signal_pid); |
| 230 | fflush (stderr); |
| 231 | |
| 232 | #ifdef SIGTTOU |
| 233 | signal (SIGTTOU, SIG_IGN); |
| 234 | signal (SIGTTIN, SIG_IGN); |
| 235 | terminal_fd = fileno (stderr); |
| 236 | old_foreground_pgrp = tcgetpgrp (terminal_fd); |
| 237 | tcsetpgrp (terminal_fd, signal_pid); |
| 238 | atexit (restore_old_foreground_pgrp); |
| 239 | #endif |
| 240 | |
| 241 | if (wrapper_argv != NULL) |
| 242 | { |
| 243 | struct thread_resume resume_info; |
| 244 | |
| 245 | memset (&resume_info, 0, sizeof (resume_info)); |
| 246 | resume_info.thread = pid_to_ptid (signal_pid); |
| 247 | resume_info.kind = resume_continue; |
| 248 | resume_info.sig = 0; |
| 249 | |
| 250 | last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0); |
| 251 | |
| 252 | if (last_status.kind != TARGET_WAITKIND_STOPPED) |
| 253 | return signal_pid; |
| 254 | |
| 255 | do |
| 256 | { |
| 257 | (*the_target->resume) (&resume_info, 1); |
| 258 | |
| 259 | last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0); |
| 260 | if (last_status.kind != TARGET_WAITKIND_STOPPED) |
| 261 | return signal_pid; |
| 262 | |
| 263 | current_thread->last_resume_kind = resume_stop; |
| 264 | current_thread->last_status = last_status; |
| 265 | } |
| 266 | while (last_status.value.sig != GDB_SIGNAL_TRAP); |
| 267 | |
| 268 | return signal_pid; |
| 269 | } |
| 270 | |
| 271 | /* Wait till we are at 1st instruction in program, return new pid |
| 272 | (assuming success). */ |
| 273 | last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0); |
| 274 | |
| 275 | if (last_status.kind != TARGET_WAITKIND_EXITED |
| 276 | && last_status.kind != TARGET_WAITKIND_SIGNALLED) |
| 277 | { |
| 278 | current_thread->last_resume_kind = resume_stop; |
| 279 | current_thread->last_status = last_status; |
| 280 | } |
| 281 | |
| 282 | return signal_pid; |
| 283 | } |
| 284 | |
| 285 | static int |
| 286 | attach_inferior (int pid) |
| 287 | { |
| 288 | /* myattach should return -1 if attaching is unsupported, |
| 289 | 0 if it succeeded, and call error() otherwise. */ |
| 290 | |
| 291 | if (myattach (pid) != 0) |
| 292 | return -1; |
| 293 | |
| 294 | fprintf (stderr, "Attached; pid = %d\n", pid); |
| 295 | fflush (stderr); |
| 296 | |
| 297 | /* FIXME - It may be that we should get the SIGNAL_PID from the |
| 298 | attach function, so that it can be the main thread instead of |
| 299 | whichever we were told to attach to. */ |
| 300 | signal_pid = pid; |
| 301 | |
| 302 | if (!non_stop) |
| 303 | { |
| 304 | last_ptid = mywait (pid_to_ptid (pid), &last_status, 0, 0); |
| 305 | |
| 306 | /* GDB knows to ignore the first SIGSTOP after attaching to a running |
| 307 | process using the "attach" command, but this is different; it's |
| 308 | just using "target remote". Pretend it's just starting up. */ |
| 309 | if (last_status.kind == TARGET_WAITKIND_STOPPED |
| 310 | && last_status.value.sig == GDB_SIGNAL_STOP) |
| 311 | last_status.value.sig = GDB_SIGNAL_TRAP; |
| 312 | |
| 313 | current_thread->last_resume_kind = resume_stop; |
| 314 | current_thread->last_status = last_status; |
| 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | extern int remote_debug; |
| 321 | |
| 322 | /* Decode a qXfer read request. Return 0 if everything looks OK, |
| 323 | or -1 otherwise. */ |
| 324 | |
| 325 | static int |
| 326 | decode_xfer_read (char *buf, CORE_ADDR *ofs, unsigned int *len) |
| 327 | { |
| 328 | /* After the read marker and annex, qXfer looks like a |
| 329 | traditional 'm' packet. */ |
| 330 | decode_m_packet (buf, ofs, len); |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | static int |
| 336 | decode_xfer (char *buf, char **object, char **rw, char **annex, char **offset) |
| 337 | { |
| 338 | /* Extract and NUL-terminate the object. */ |
| 339 | *object = buf; |
| 340 | while (*buf && *buf != ':') |
| 341 | buf++; |
| 342 | if (*buf == '\0') |
| 343 | return -1; |
| 344 | *buf++ = 0; |
| 345 | |
| 346 | /* Extract and NUL-terminate the read/write action. */ |
| 347 | *rw = buf; |
| 348 | while (*buf && *buf != ':') |
| 349 | buf++; |
| 350 | if (*buf == '\0') |
| 351 | return -1; |
| 352 | *buf++ = 0; |
| 353 | |
| 354 | /* Extract and NUL-terminate the annex. */ |
| 355 | *annex = buf; |
| 356 | while (*buf && *buf != ':') |
| 357 | buf++; |
| 358 | if (*buf == '\0') |
| 359 | return -1; |
| 360 | *buf++ = 0; |
| 361 | |
| 362 | *offset = buf; |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | /* Write the response to a successful qXfer read. Returns the |
| 367 | length of the (binary) data stored in BUF, corresponding |
| 368 | to as much of DATA/LEN as we could fit. IS_MORE controls |
| 369 | the first character of the response. */ |
| 370 | static int |
| 371 | write_qxfer_response (char *buf, const void *data, int len, int is_more) |
| 372 | { |
| 373 | int out_len; |
| 374 | |
| 375 | if (is_more) |
| 376 | buf[0] = 'm'; |
| 377 | else |
| 378 | buf[0] = 'l'; |
| 379 | |
| 380 | return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len, |
| 381 | PBUFSIZ - 2) + 1; |
| 382 | } |
| 383 | |
| 384 | /* Handle btrace enabling. */ |
| 385 | |
| 386 | static const char * |
| 387 | handle_btrace_enable (struct thread_info *thread) |
| 388 | { |
| 389 | if (thread->btrace != NULL) |
| 390 | return "E.Btrace already enabled."; |
| 391 | |
| 392 | thread->btrace = target_enable_btrace (thread->entry.id); |
| 393 | if (thread->btrace == NULL) |
| 394 | return "E.Could not enable btrace."; |
| 395 | |
| 396 | return NULL; |
| 397 | } |
| 398 | |
| 399 | /* Handle btrace disabling. */ |
| 400 | |
| 401 | static const char * |
| 402 | handle_btrace_disable (struct thread_info *thread) |
| 403 | { |
| 404 | |
| 405 | if (thread->btrace == NULL) |
| 406 | return "E.Branch tracing not enabled."; |
| 407 | |
| 408 | if (target_disable_btrace (thread->btrace) != 0) |
| 409 | return "E.Could not disable branch tracing."; |
| 410 | |
| 411 | thread->btrace = NULL; |
| 412 | return NULL; |
| 413 | } |
| 414 | |
| 415 | /* Handle the "Qbtrace" packet. */ |
| 416 | |
| 417 | static int |
| 418 | handle_btrace_general_set (char *own_buf) |
| 419 | { |
| 420 | struct thread_info *thread; |
| 421 | const char *err; |
| 422 | char *op; |
| 423 | |
| 424 | if (strncmp ("Qbtrace:", own_buf, strlen ("Qbtrace:")) != 0) |
| 425 | return 0; |
| 426 | |
| 427 | op = own_buf + strlen ("Qbtrace:"); |
| 428 | |
| 429 | if (!target_supports_btrace ()) |
| 430 | { |
| 431 | strcpy (own_buf, "E.Target does not support branch tracing."); |
| 432 | return -1; |
| 433 | } |
| 434 | |
| 435 | if (ptid_equal (general_thread, null_ptid) |
| 436 | || ptid_equal (general_thread, minus_one_ptid)) |
| 437 | { |
| 438 | strcpy (own_buf, "E.Must select a single thread."); |
| 439 | return -1; |
| 440 | } |
| 441 | |
| 442 | thread = find_thread_ptid (general_thread); |
| 443 | if (thread == NULL) |
| 444 | { |
| 445 | strcpy (own_buf, "E.No such thread."); |
| 446 | return -1; |
| 447 | } |
| 448 | |
| 449 | err = NULL; |
| 450 | |
| 451 | if (strcmp (op, "bts") == 0) |
| 452 | err = handle_btrace_enable (thread); |
| 453 | else if (strcmp (op, "off") == 0) |
| 454 | err = handle_btrace_disable (thread); |
| 455 | else |
| 456 | err = "E.Bad Qbtrace operation. Use bts or off."; |
| 457 | |
| 458 | if (err != 0) |
| 459 | strcpy (own_buf, err); |
| 460 | else |
| 461 | write_ok (own_buf); |
| 462 | |
| 463 | return 1; |
| 464 | } |
| 465 | |
| 466 | /* Handle all of the extended 'Q' packets. */ |
| 467 | |
| 468 | static void |
| 469 | handle_general_set (char *own_buf) |
| 470 | { |
| 471 | if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0) |
| 472 | { |
| 473 | int numsigs = (int) GDB_SIGNAL_LAST, i; |
| 474 | const char *p = own_buf + strlen ("QPassSignals:"); |
| 475 | CORE_ADDR cursig; |
| 476 | |
| 477 | p = decode_address_to_semicolon (&cursig, p); |
| 478 | for (i = 0; i < numsigs; i++) |
| 479 | { |
| 480 | if (i == cursig) |
| 481 | { |
| 482 | pass_signals[i] = 1; |
| 483 | if (*p == '\0') |
| 484 | /* Keep looping, to clear the remaining signals. */ |
| 485 | cursig = -1; |
| 486 | else |
| 487 | p = decode_address_to_semicolon (&cursig, p); |
| 488 | } |
| 489 | else |
| 490 | pass_signals[i] = 0; |
| 491 | } |
| 492 | strcpy (own_buf, "OK"); |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | if (strncmp ("QProgramSignals:", own_buf, strlen ("QProgramSignals:")) == 0) |
| 497 | { |
| 498 | int numsigs = (int) GDB_SIGNAL_LAST, i; |
| 499 | const char *p = own_buf + strlen ("QProgramSignals:"); |
| 500 | CORE_ADDR cursig; |
| 501 | |
| 502 | program_signals_p = 1; |
| 503 | |
| 504 | p = decode_address_to_semicolon (&cursig, p); |
| 505 | for (i = 0; i < numsigs; i++) |
| 506 | { |
| 507 | if (i == cursig) |
| 508 | { |
| 509 | program_signals[i] = 1; |
| 510 | if (*p == '\0') |
| 511 | /* Keep looping, to clear the remaining signals. */ |
| 512 | cursig = -1; |
| 513 | else |
| 514 | p = decode_address_to_semicolon (&cursig, p); |
| 515 | } |
| 516 | else |
| 517 | program_signals[i] = 0; |
| 518 | } |
| 519 | strcpy (own_buf, "OK"); |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | if (strcmp (own_buf, "QStartNoAckMode") == 0) |
| 524 | { |
| 525 | if (remote_debug) |
| 526 | { |
| 527 | fprintf (stderr, "[noack mode enabled]\n"); |
| 528 | fflush (stderr); |
| 529 | } |
| 530 | |
| 531 | noack_mode = 1; |
| 532 | write_ok (own_buf); |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | if (strncmp (own_buf, "QNonStop:", 9) == 0) |
| 537 | { |
| 538 | char *mode = own_buf + 9; |
| 539 | int req = -1; |
| 540 | char *req_str; |
| 541 | |
| 542 | if (strcmp (mode, "0") == 0) |
| 543 | req = 0; |
| 544 | else if (strcmp (mode, "1") == 0) |
| 545 | req = 1; |
| 546 | else |
| 547 | { |
| 548 | /* We don't know what this mode is, so complain to |
| 549 | GDB. */ |
| 550 | fprintf (stderr, "Unknown non-stop mode requested: %s\n", |
| 551 | own_buf); |
| 552 | write_enn (own_buf); |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | req_str = req ? "non-stop" : "all-stop"; |
| 557 | if (start_non_stop (req) != 0) |
| 558 | { |
| 559 | fprintf (stderr, "Setting %s mode failed\n", req_str); |
| 560 | write_enn (own_buf); |
| 561 | return; |
| 562 | } |
| 563 | |
| 564 | non_stop = req; |
| 565 | |
| 566 | if (remote_debug) |
| 567 | fprintf (stderr, "[%s mode enabled]\n", req_str); |
| 568 | |
| 569 | write_ok (own_buf); |
| 570 | return; |
| 571 | } |
| 572 | |
| 573 | if (strncmp ("QDisableRandomization:", own_buf, |
| 574 | strlen ("QDisableRandomization:")) == 0) |
| 575 | { |
| 576 | char *packet = own_buf + strlen ("QDisableRandomization:"); |
| 577 | ULONGEST setting; |
| 578 | |
| 579 | unpack_varlen_hex (packet, &setting); |
| 580 | disable_randomization = setting; |
| 581 | |
| 582 | if (remote_debug) |
| 583 | { |
| 584 | if (disable_randomization) |
| 585 | fprintf (stderr, "[address space randomization disabled]\n"); |
| 586 | else |
| 587 | fprintf (stderr, "[address space randomization enabled]\n"); |
| 588 | } |
| 589 | |
| 590 | write_ok (own_buf); |
| 591 | return; |
| 592 | } |
| 593 | |
| 594 | if (target_supports_tracepoints () |
| 595 | && handle_tracepoint_general_set (own_buf)) |
| 596 | return; |
| 597 | |
| 598 | if (strncmp ("QAgent:", own_buf, strlen ("QAgent:")) == 0) |
| 599 | { |
| 600 | char *mode = own_buf + strlen ("QAgent:"); |
| 601 | int req = 0; |
| 602 | |
| 603 | if (strcmp (mode, "0") == 0) |
| 604 | req = 0; |
| 605 | else if (strcmp (mode, "1") == 0) |
| 606 | req = 1; |
| 607 | else |
| 608 | { |
| 609 | /* We don't know what this value is, so complain to GDB. */ |
| 610 | sprintf (own_buf, "E.Unknown QAgent value"); |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | /* Update the flag. */ |
| 615 | use_agent = req; |
| 616 | if (remote_debug) |
| 617 | fprintf (stderr, "[%s agent]\n", req ? "Enable" : "Disable"); |
| 618 | write_ok (own_buf); |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | if (handle_btrace_general_set (own_buf)) |
| 623 | return; |
| 624 | |
| 625 | /* Otherwise we didn't know what packet it was. Say we didn't |
| 626 | understand it. */ |
| 627 | own_buf[0] = 0; |
| 628 | } |
| 629 | |
| 630 | static const char * |
| 631 | get_features_xml (const char *annex) |
| 632 | { |
| 633 | const struct target_desc *desc = current_target_desc (); |
| 634 | |
| 635 | /* `desc->xmltarget' defines what to return when looking for the |
| 636 | "target.xml" file. Its contents can either be verbatim XML code |
| 637 | (prefixed with a '@') or else the name of the actual XML file to |
| 638 | be used in place of "target.xml". |
| 639 | |
| 640 | This variable is set up from the auto-generated |
| 641 | init_registers_... routine for the current target. */ |
| 642 | |
| 643 | if (desc->xmltarget != NULL && strcmp (annex, "target.xml") == 0) |
| 644 | { |
| 645 | if (*desc->xmltarget == '@') |
| 646 | return desc->xmltarget + 1; |
| 647 | else |
| 648 | annex = desc->xmltarget; |
| 649 | } |
| 650 | |
| 651 | #ifdef USE_XML |
| 652 | { |
| 653 | extern const char *const xml_builtin[][2]; |
| 654 | int i; |
| 655 | |
| 656 | /* Look for the annex. */ |
| 657 | for (i = 0; xml_builtin[i][0] != NULL; i++) |
| 658 | if (strcmp (annex, xml_builtin[i][0]) == 0) |
| 659 | break; |
| 660 | |
| 661 | if (xml_builtin[i][0] != NULL) |
| 662 | return xml_builtin[i][1]; |
| 663 | } |
| 664 | #endif |
| 665 | |
| 666 | return NULL; |
| 667 | } |
| 668 | |
| 669 | void |
| 670 | monitor_show_help (void) |
| 671 | { |
| 672 | monitor_output ("The following monitor commands are supported:\n"); |
| 673 | monitor_output (" set debug <0|1>\n"); |
| 674 | monitor_output (" Enable general debugging messages\n"); |
| 675 | monitor_output (" set debug-hw-points <0|1>\n"); |
| 676 | monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n"); |
| 677 | monitor_output (" set remote-debug <0|1>\n"); |
| 678 | monitor_output (" Enable remote protocol debugging messages\n"); |
| 679 | monitor_output (" set debug-format option1[,option2,...]\n"); |
| 680 | monitor_output (" Add additional information to debugging messages\n"); |
| 681 | monitor_output (" Options: all, none"); |
| 682 | monitor_output (", timestamp"); |
| 683 | monitor_output ("\n"); |
| 684 | monitor_output (" exit\n"); |
| 685 | monitor_output (" Quit GDBserver\n"); |
| 686 | } |
| 687 | |
| 688 | /* Read trace frame or inferior memory. Returns the number of bytes |
| 689 | actually read, zero when no further transfer is possible, and -1 on |
| 690 | error. Return of a positive value smaller than LEN does not |
| 691 | indicate there's no more to be read, only the end of the transfer. |
| 692 | E.g., when GDB reads memory from a traceframe, a first request may |
| 693 | be served from a memory block that does not cover the whole request |
| 694 | length. A following request gets the rest served from either |
| 695 | another block (of the same traceframe) or from the read-only |
| 696 | regions. */ |
| 697 | |
| 698 | static int |
| 699 | gdb_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len) |
| 700 | { |
| 701 | int res; |
| 702 | |
| 703 | if (current_traceframe >= 0) |
| 704 | { |
| 705 | ULONGEST nbytes; |
| 706 | ULONGEST length = len; |
| 707 | |
| 708 | if (traceframe_read_mem (current_traceframe, |
| 709 | memaddr, myaddr, len, &nbytes)) |
| 710 | return -1; |
| 711 | /* Data read from trace buffer, we're done. */ |
| 712 | if (nbytes > 0) |
| 713 | return nbytes; |
| 714 | if (!in_readonly_region (memaddr, length)) |
| 715 | return -1; |
| 716 | /* Otherwise we have a valid readonly case, fall through. */ |
| 717 | /* (assume no half-trace half-real blocks for now) */ |
| 718 | } |
| 719 | |
| 720 | res = prepare_to_access_memory (); |
| 721 | if (res == 0) |
| 722 | { |
| 723 | res = read_inferior_memory (memaddr, myaddr, len); |
| 724 | done_accessing_memory (); |
| 725 | |
| 726 | return res == 0 ? len : -1; |
| 727 | } |
| 728 | else |
| 729 | return -1; |
| 730 | } |
| 731 | |
| 732 | /* Write trace frame or inferior memory. Actually, writing to trace |
| 733 | frames is forbidden. */ |
| 734 | |
| 735 | static int |
| 736 | gdb_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len) |
| 737 | { |
| 738 | if (current_traceframe >= 0) |
| 739 | return EIO; |
| 740 | else |
| 741 | { |
| 742 | int ret; |
| 743 | |
| 744 | ret = prepare_to_access_memory (); |
| 745 | if (ret == 0) |
| 746 | { |
| 747 | ret = write_inferior_memory (memaddr, myaddr, len); |
| 748 | done_accessing_memory (); |
| 749 | } |
| 750 | return ret; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | /* Subroutine of handle_search_memory to simplify it. */ |
| 755 | |
| 756 | static int |
| 757 | handle_search_memory_1 (CORE_ADDR start_addr, CORE_ADDR search_space_len, |
| 758 | gdb_byte *pattern, unsigned pattern_len, |
| 759 | gdb_byte *search_buf, |
| 760 | unsigned chunk_size, unsigned search_buf_size, |
| 761 | CORE_ADDR *found_addrp) |
| 762 | { |
| 763 | /* Prime the search buffer. */ |
| 764 | |
| 765 | if (gdb_read_memory (start_addr, search_buf, search_buf_size) |
| 766 | != search_buf_size) |
| 767 | { |
| 768 | warning ("Unable to access %ld bytes of target " |
| 769 | "memory at 0x%lx, halting search.", |
| 770 | (long) search_buf_size, (long) start_addr); |
| 771 | return -1; |
| 772 | } |
| 773 | |
| 774 | /* Perform the search. |
| 775 | |
| 776 | The loop is kept simple by allocating [N + pattern-length - 1] bytes. |
| 777 | When we've scanned N bytes we copy the trailing bytes to the start and |
| 778 | read in another N bytes. */ |
| 779 | |
| 780 | while (search_space_len >= pattern_len) |
| 781 | { |
| 782 | gdb_byte *found_ptr; |
| 783 | unsigned nr_search_bytes = (search_space_len < search_buf_size |
| 784 | ? search_space_len |
| 785 | : search_buf_size); |
| 786 | |
| 787 | found_ptr = memmem (search_buf, nr_search_bytes, pattern, pattern_len); |
| 788 | |
| 789 | if (found_ptr != NULL) |
| 790 | { |
| 791 | CORE_ADDR found_addr = start_addr + (found_ptr - search_buf); |
| 792 | *found_addrp = found_addr; |
| 793 | return 1; |
| 794 | } |
| 795 | |
| 796 | /* Not found in this chunk, skip to next chunk. */ |
| 797 | |
| 798 | /* Don't let search_space_len wrap here, it's unsigned. */ |
| 799 | if (search_space_len >= chunk_size) |
| 800 | search_space_len -= chunk_size; |
| 801 | else |
| 802 | search_space_len = 0; |
| 803 | |
| 804 | if (search_space_len >= pattern_len) |
| 805 | { |
| 806 | unsigned keep_len = search_buf_size - chunk_size; |
| 807 | CORE_ADDR read_addr = start_addr + chunk_size + keep_len; |
| 808 | int nr_to_read; |
| 809 | |
| 810 | /* Copy the trailing part of the previous iteration to the front |
| 811 | of the buffer for the next iteration. */ |
| 812 | memcpy (search_buf, search_buf + chunk_size, keep_len); |
| 813 | |
| 814 | nr_to_read = (search_space_len - keep_len < chunk_size |
| 815 | ? search_space_len - keep_len |
| 816 | : chunk_size); |
| 817 | |
| 818 | if (gdb_read_memory (read_addr, search_buf + keep_len, |
| 819 | nr_to_read) != search_buf_size) |
| 820 | { |
| 821 | warning ("Unable to access %ld bytes of target memory " |
| 822 | "at 0x%lx, halting search.", |
| 823 | (long) nr_to_read, (long) read_addr); |
| 824 | return -1; |
| 825 | } |
| 826 | |
| 827 | start_addr += chunk_size; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | /* Not found. */ |
| 832 | |
| 833 | return 0; |
| 834 | } |
| 835 | |
| 836 | /* Handle qSearch:memory packets. */ |
| 837 | |
| 838 | static void |
| 839 | handle_search_memory (char *own_buf, int packet_len) |
| 840 | { |
| 841 | CORE_ADDR start_addr; |
| 842 | CORE_ADDR search_space_len; |
| 843 | gdb_byte *pattern; |
| 844 | unsigned int pattern_len; |
| 845 | /* NOTE: also defined in find.c testcase. */ |
| 846 | #define SEARCH_CHUNK_SIZE 16000 |
| 847 | const unsigned chunk_size = SEARCH_CHUNK_SIZE; |
| 848 | /* Buffer to hold memory contents for searching. */ |
| 849 | gdb_byte *search_buf; |
| 850 | unsigned search_buf_size; |
| 851 | int found; |
| 852 | CORE_ADDR found_addr; |
| 853 | int cmd_name_len = sizeof ("qSearch:memory:") - 1; |
| 854 | |
| 855 | pattern = malloc (packet_len); |
| 856 | if (pattern == NULL) |
| 857 | { |
| 858 | error ("Unable to allocate memory to perform the search"); |
| 859 | strcpy (own_buf, "E00"); |
| 860 | return; |
| 861 | } |
| 862 | if (decode_search_memory_packet (own_buf + cmd_name_len, |
| 863 | packet_len - cmd_name_len, |
| 864 | &start_addr, &search_space_len, |
| 865 | pattern, &pattern_len) < 0) |
| 866 | { |
| 867 | free (pattern); |
| 868 | error ("Error in parsing qSearch:memory packet"); |
| 869 | strcpy (own_buf, "E00"); |
| 870 | return; |
| 871 | } |
| 872 | |
| 873 | search_buf_size = chunk_size + pattern_len - 1; |
| 874 | |
| 875 | /* No point in trying to allocate a buffer larger than the search space. */ |
| 876 | if (search_space_len < search_buf_size) |
| 877 | search_buf_size = search_space_len; |
| 878 | |
| 879 | search_buf = malloc (search_buf_size); |
| 880 | if (search_buf == NULL) |
| 881 | { |
| 882 | free (pattern); |
| 883 | error ("Unable to allocate memory to perform the search"); |
| 884 | strcpy (own_buf, "E00"); |
| 885 | return; |
| 886 | } |
| 887 | |
| 888 | found = handle_search_memory_1 (start_addr, search_space_len, |
| 889 | pattern, pattern_len, |
| 890 | search_buf, chunk_size, search_buf_size, |
| 891 | &found_addr); |
| 892 | |
| 893 | if (found > 0) |
| 894 | sprintf (own_buf, "1,%lx", (long) found_addr); |
| 895 | else if (found == 0) |
| 896 | strcpy (own_buf, "0"); |
| 897 | else |
| 898 | strcpy (own_buf, "E00"); |
| 899 | |
| 900 | free (search_buf); |
| 901 | free (pattern); |
| 902 | } |
| 903 | |
| 904 | #define require_running(BUF) \ |
| 905 | if (!target_running ()) \ |
| 906 | { \ |
| 907 | write_enn (BUF); \ |
| 908 | return; \ |
| 909 | } |
| 910 | |
| 911 | /* Parse options to --debug-format= and "monitor set debug-format". |
| 912 | ARG is the text after "--debug-format=" or "monitor set debug-format". |
| 913 | IS_MONITOR is non-zero if we're invoked via "monitor set debug-format". |
| 914 | This triggers calls to monitor_output. |
| 915 | The result is NULL if all options were parsed ok, otherwise an error |
| 916 | message which the caller must free. |
| 917 | |
| 918 | N.B. These commands affect all debug format settings, they are not |
| 919 | cumulative. If a format is not specified, it is turned off. |
| 920 | However, we don't go to extra trouble with things like |
| 921 | "monitor set debug-format all,none,timestamp". |
| 922 | Instead we just parse them one at a time, in order. |
| 923 | |
| 924 | The syntax for "monitor set debug" we support here is not identical |
| 925 | to gdb's "set debug foo on|off" because we also use this function to |
| 926 | parse "--debug-format=foo,bar". */ |
| 927 | |
| 928 | static char * |
| 929 | parse_debug_format_options (const char *arg, int is_monitor) |
| 930 | { |
| 931 | VEC (char_ptr) *options; |
| 932 | int ix; |
| 933 | char *option; |
| 934 | |
| 935 | /* First turn all debug format options off. */ |
| 936 | debug_timestamp = 0; |
| 937 | |
| 938 | /* First remove leading spaces, for "monitor set debug-format". */ |
| 939 | while (isspace (*arg)) |
| 940 | ++arg; |
| 941 | |
| 942 | options = delim_string_to_char_ptr_vec (arg, ','); |
| 943 | |
| 944 | for (ix = 0; VEC_iterate (char_ptr, options, ix, option); ++ix) |
| 945 | { |
| 946 | if (strcmp (option, "all") == 0) |
| 947 | { |
| 948 | debug_timestamp = 1; |
| 949 | if (is_monitor) |
| 950 | monitor_output ("All extra debug format options enabled.\n"); |
| 951 | } |
| 952 | else if (strcmp (option, "none") == 0) |
| 953 | { |
| 954 | debug_timestamp = 0; |
| 955 | if (is_monitor) |
| 956 | monitor_output ("All extra debug format options disabled.\n"); |
| 957 | } |
| 958 | else if (strcmp (option, "timestamp") == 0) |
| 959 | { |
| 960 | debug_timestamp = 1; |
| 961 | if (is_monitor) |
| 962 | monitor_output ("Timestamps will be added to debug output.\n"); |
| 963 | } |
| 964 | else if (*option == '\0') |
| 965 | { |
| 966 | /* An empty option, e.g., "--debug-format=foo,,bar", is ignored. */ |
| 967 | continue; |
| 968 | } |
| 969 | else |
| 970 | { |
| 971 | char *msg = xstrprintf ("Unknown debug-format argument: \"%s\"\n", |
| 972 | option); |
| 973 | |
| 974 | free_char_ptr_vec (options); |
| 975 | return msg; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | free_char_ptr_vec (options); |
| 980 | return NULL; |
| 981 | } |
| 982 | |
| 983 | /* Handle monitor commands not handled by target-specific handlers. */ |
| 984 | |
| 985 | static void |
| 986 | handle_monitor_command (char *mon, char *own_buf) |
| 987 | { |
| 988 | if (strcmp (mon, "set debug 1") == 0) |
| 989 | { |
| 990 | debug_threads = 1; |
| 991 | monitor_output ("Debug output enabled.\n"); |
| 992 | } |
| 993 | else if (strcmp (mon, "set debug 0") == 0) |
| 994 | { |
| 995 | debug_threads = 0; |
| 996 | monitor_output ("Debug output disabled.\n"); |
| 997 | } |
| 998 | else if (strcmp (mon, "set debug-hw-points 1") == 0) |
| 999 | { |
| 1000 | show_debug_regs = 1; |
| 1001 | monitor_output ("H/W point debugging output enabled.\n"); |
| 1002 | } |
| 1003 | else if (strcmp (mon, "set debug-hw-points 0") == 0) |
| 1004 | { |
| 1005 | show_debug_regs = 0; |
| 1006 | monitor_output ("H/W point debugging output disabled.\n"); |
| 1007 | } |
| 1008 | else if (strcmp (mon, "set remote-debug 1") == 0) |
| 1009 | { |
| 1010 | remote_debug = 1; |
| 1011 | monitor_output ("Protocol debug output enabled.\n"); |
| 1012 | } |
| 1013 | else if (strcmp (mon, "set remote-debug 0") == 0) |
| 1014 | { |
| 1015 | remote_debug = 0; |
| 1016 | monitor_output ("Protocol debug output disabled.\n"); |
| 1017 | } |
| 1018 | else if (strncmp (mon, "set debug-format ", |
| 1019 | sizeof ("set debug-format ") - 1) == 0) |
| 1020 | { |
| 1021 | char *error_msg |
| 1022 | = parse_debug_format_options (mon + sizeof ("set debug-format ") - 1, |
| 1023 | 1); |
| 1024 | |
| 1025 | if (error_msg != NULL) |
| 1026 | { |
| 1027 | monitor_output (error_msg); |
| 1028 | monitor_show_help (); |
| 1029 | write_enn (own_buf); |
| 1030 | xfree (error_msg); |
| 1031 | } |
| 1032 | } |
| 1033 | else if (strcmp (mon, "help") == 0) |
| 1034 | monitor_show_help (); |
| 1035 | else if (strcmp (mon, "exit") == 0) |
| 1036 | exit_requested = 1; |
| 1037 | else |
| 1038 | { |
| 1039 | monitor_output ("Unknown monitor command.\n\n"); |
| 1040 | monitor_show_help (); |
| 1041 | write_enn (own_buf); |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | /* Associates a callback with each supported qXfer'able object. */ |
| 1046 | |
| 1047 | struct qxfer |
| 1048 | { |
| 1049 | /* The object this handler handles. */ |
| 1050 | const char *object; |
| 1051 | |
| 1052 | /* Request that the target transfer up to LEN 8-bit bytes of the |
| 1053 | target's OBJECT. The OFFSET, for a seekable object, specifies |
| 1054 | the starting point. The ANNEX can be used to provide additional |
| 1055 | data-specific information to the target. |
| 1056 | |
| 1057 | Return the number of bytes actually transfered, zero when no |
| 1058 | further transfer is possible, -1 on error, -2 when the transfer |
| 1059 | is not supported, and -3 on a verbose error message that should |
| 1060 | be preserved. Return of a positive value smaller than LEN does |
| 1061 | not indicate the end of the object, only the end of the transfer. |
| 1062 | |
| 1063 | One, and only one, of readbuf or writebuf must be non-NULL. */ |
| 1064 | int (*xfer) (const char *annex, |
| 1065 | gdb_byte *readbuf, const gdb_byte *writebuf, |
| 1066 | ULONGEST offset, LONGEST len); |
| 1067 | }; |
| 1068 | |
| 1069 | /* Handle qXfer:auxv:read. */ |
| 1070 | |
| 1071 | static int |
| 1072 | handle_qxfer_auxv (const char *annex, |
| 1073 | gdb_byte *readbuf, const gdb_byte *writebuf, |
| 1074 | ULONGEST offset, LONGEST len) |
| 1075 | { |
| 1076 | if (the_target->read_auxv == NULL || writebuf != NULL) |
| 1077 | return -2; |
| 1078 | |
| 1079 | if (annex[0] != '\0' || !target_running ()) |
| 1080 | return -1; |
| 1081 | |
| 1082 | return (*the_target->read_auxv) (offset, readbuf, len); |
| 1083 | } |
| 1084 | |
| 1085 | /* Handle qXfer:features:read. */ |
| 1086 | |
| 1087 | static int |
| 1088 | handle_qxfer_features (const char *annex, |
| 1089 | gdb_byte *readbuf, const gdb_byte *writebuf, |
| 1090 | ULONGEST offset, LONGEST len) |
| 1091 | { |
| 1092 | const char *document; |
| 1093 | size_t total_len; |
| 1094 | |
| 1095 | if (writebuf != NULL) |
| 1096 | return -2; |
| 1097 | |
| 1098 | if (!target_running ()) |
| 1099 | return -1; |
| 1100 | |
| 1101 | /* Grab the correct annex. */ |
| 1102 | document = get_features_xml (annex); |
| 1103 | if (document == NULL) |
| 1104 | return -1; |
| 1105 | |
| 1106 | total_len = strlen (document); |
| 1107 | |
| 1108 | if (offset > total_len) |
| 1109 | return -1; |
| 1110 | |
| 1111 | if (offset + len > total_len) |
| 1112 | len = total_len - offset; |
| 1113 | |
| 1114 | memcpy (readbuf, document + offset, len); |
| 1115 | return len; |
| 1116 | } |
| 1117 | |
| 1118 | /* Worker routine for handle_qxfer_libraries. |
| 1119 | Add to the length pointed to by ARG a conservative estimate of the |
| 1120 | length needed to transmit the file name of INF. */ |
| 1121 | |
| 1122 | static void |
| 1123 | accumulate_file_name_length (struct inferior_list_entry *inf, void *arg) |
| 1124 | { |
| 1125 | struct dll_info *dll = (struct dll_info *) inf; |
| 1126 | unsigned int *total_len = arg; |
| 1127 | |
| 1128 | /* Over-estimate the necessary memory. Assume that every character |
| 1129 | in the library name must be escaped. */ |
| 1130 | *total_len += 128 + 6 * strlen (dll->name); |
| 1131 | } |
| 1132 | |
| 1133 | /* Worker routine for handle_qxfer_libraries. |
| 1134 | Emit the XML to describe the library in INF. */ |
| 1135 | |
| 1136 | static void |
| 1137 | emit_dll_description (struct inferior_list_entry *inf, void *arg) |
| 1138 | { |
| 1139 | struct dll_info *dll = (struct dll_info *) inf; |
| 1140 | char **p_ptr = arg; |
| 1141 | char *p = *p_ptr; |
| 1142 | char *name; |
| 1143 | |
| 1144 | strcpy (p, " <library name=\""); |
| 1145 | p = p + strlen (p); |
| 1146 | name = xml_escape_text (dll->name); |
| 1147 | strcpy (p, name); |
| 1148 | free (name); |
| 1149 | p = p + strlen (p); |
| 1150 | strcpy (p, "\"><segment address=\""); |
| 1151 | p = p + strlen (p); |
| 1152 | sprintf (p, "0x%lx", (long) dll->base_addr); |
| 1153 | p = p + strlen (p); |
| 1154 | strcpy (p, "\"/></library>\n"); |
| 1155 | p = p + strlen (p); |
| 1156 | |
| 1157 | *p_ptr = p; |
| 1158 | } |
| 1159 | |
| 1160 | /* Handle qXfer:libraries:read. */ |
| 1161 | |
| 1162 | static int |
| 1163 | handle_qxfer_libraries (const char *annex, |
| 1164 | gdb_byte *readbuf, const gdb_byte *writebuf, |
| 1165 | ULONGEST offset, LONGEST len) |
| 1166 | { |
| 1167 | unsigned int total_len; |
| 1168 | char *document, *p; |
| 1169 | |
| 1170 | if (writebuf != NULL) |
| 1171 | return -2; |
| 1172 | |
| 1173 | if (annex[0] != '\0' || !target_running ()) |
| 1174 | return -1; |
| 1175 | |
| 1176 | total_len = 64; |
| 1177 | for_each_inferior_with_data (&all_dlls, accumulate_file_name_length, |
| 1178 | &total_len); |
| 1179 | |
| 1180 | document = malloc (total_len); |
| 1181 | if (document == NULL) |
| 1182 | return -1; |
| 1183 | |
| 1184 | strcpy (document, "<library-list>\n"); |
| 1185 | p = document + strlen (document); |
| 1186 | |
| 1187 | for_each_inferior_with_data (&all_dlls, emit_dll_description, &p); |
| 1188 | |
| 1189 | strcpy (p, "</library-list>\n"); |
| 1190 | |
| 1191 | total_len = strlen (document); |
| 1192 | |
| 1193 | if (offset > total_len) |
| 1194 | { |
| 1195 | free (document); |
| 1196 | return -1; |
| 1197 | } |
| 1198 | |
| 1199 | if (offset + len > total_len) |
| 1200 | len = total_len - offset; |
| 1201 | |
| 1202 | memcpy (readbuf, document + offset, len); |
| 1203 | free (document); |
| 1204 | return len; |
| 1205 | } |
| 1206 | |
| 1207 | /* Handle qXfer:libraries-svr4:read. */ |
| 1208 | |
| 1209 | static int |
| 1210 | handle_qxfer_libraries_svr4 (const char *annex, |
| 1211 | gdb_byte *readbuf, const gdb_byte *writebuf, |
| 1212 | ULONGEST offset, LONGEST len) |
| 1213 | { |
| 1214 | if (writebuf != NULL) |
| 1215 | return -2; |
| 1216 | |
| 1217 | if (!target_running () || the_target->qxfer_libraries_svr4 == NULL) |
| 1218 | return -1; |
| 1219 | |
| 1220 | return the_target->qxfer_libraries_svr4 (annex, readbuf, writebuf, offset, len); |
| 1221 | } |
| 1222 | |
| 1223 | /* Handle qXfer:osadata:read. */ |
| 1224 | |
| 1225 | static int |
| 1226 | handle_qxfer_osdata (const char *annex, |
| 1227 | gdb_byte *readbuf, const gdb_byte *writebuf, |
| 1228 | ULONGEST offset, LONGEST len) |
| 1229 | { |
| 1230 | if (the_target->qxfer_osdata == NULL || writebuf != NULL) |
| 1231 | return -2; |
| 1232 | |
| 1233 | return (*the_target->qxfer_osdata) (annex, readbuf, NULL, offset, len); |
| 1234 | } |
| 1235 | |
| 1236 | /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */ |
| 1237 | |
| 1238 | static int |
| 1239 | handle_qxfer_siginfo (const char *annex, |
| 1240 | gdb_byte *readbuf, const gdb_byte *writebuf, |
| 1241 | ULONGEST offset, LONGEST len) |
| 1242 | { |
| 1243 | if (the_target->qxfer_siginfo == NULL) |
| 1244 | return -2; |
| 1245 | |
| 1246 | if (annex[0] != '\0' || !target_running ()) |
| 1247 | return -1; |
| 1248 | |
| 1249 | return (*the_target->qxfer_siginfo) (annex, readbuf, writebuf, offset, len); |
| 1250 | } |
| 1251 | |
| 1252 | /* Handle qXfer:spu:read and qXfer:spu:write. */ |
| 1253 | |
| 1254 | static int |
| 1255 | handle_qxfer_spu (const char *annex, |
| 1256 | gdb_byte *readbuf, const gdb_byte *writebuf, |
| 1257 | ULONGEST offset, LONGEST len) |
| 1258 | { |
| 1259 | if (the_target->qxfer_spu == NULL) |
| 1260 | return -2; |
| 1261 | |
| 1262 | if (!target_running ()) |
| 1263 | return -1; |
| 1264 | |
| 1265 | return (*the_target->qxfer_spu) (annex, readbuf, writebuf, offset, len); |
| 1266 | } |
| 1267 | |
| 1268 | /* Handle qXfer:statictrace:read. */ |
| 1269 | |
| 1270 | static int |
| 1271 | handle_qxfer_statictrace (const char *annex, |
| 1272 | gdb_byte *readbuf, const gdb_byte *writebuf, |
| 1273 | ULONGEST offset, LONGEST len) |
| 1274 | { |
| 1275 | ULONGEST nbytes; |
| 1276 | |
| 1277 | if (writebuf != NULL) |
| 1278 | return -2; |
| 1279 | |
| 1280 | if (annex[0] != '\0' || !target_running () || current_traceframe == -1) |
| 1281 | return -1; |
| 1282 | |
| 1283 | if (traceframe_read_sdata (current_traceframe, offset, |
| 1284 | readbuf, len, &nbytes)) |
| 1285 | return -1; |
| 1286 | return nbytes; |
| 1287 | } |
| 1288 | |
| 1289 | /* Helper for handle_qxfer_threads_proper. |
| 1290 | Emit the XML to describe the thread of INF. */ |
| 1291 | |
| 1292 | static void |
| 1293 | handle_qxfer_threads_worker (struct inferior_list_entry *inf, void *arg) |
| 1294 | { |
| 1295 | struct thread_info *thread = (struct thread_info *) inf; |
| 1296 | struct buffer *buffer = arg; |
| 1297 | ptid_t ptid = thread_to_gdb_id (thread); |
| 1298 | char ptid_s[100]; |
| 1299 | int core = target_core_of_thread (ptid); |
| 1300 | char core_s[21]; |
| 1301 | |
| 1302 | write_ptid (ptid_s, ptid); |
| 1303 | |
| 1304 | if (core != -1) |
| 1305 | { |
| 1306 | sprintf (core_s, "%d", core); |
| 1307 | buffer_xml_printf (buffer, "<thread id=\"%s\" core=\"%s\"/>\n", |
| 1308 | ptid_s, core_s); |
| 1309 | } |
| 1310 | else |
| 1311 | { |
| 1312 | buffer_xml_printf (buffer, "<thread id=\"%s\"/>\n", |
| 1313 | ptid_s); |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | /* Helper for handle_qxfer_threads. */ |
| 1318 | |
| 1319 | static void |
| 1320 | handle_qxfer_threads_proper (struct buffer *buffer) |
| 1321 | { |
| 1322 | buffer_grow_str (buffer, "<threads>\n"); |
| 1323 | |
| 1324 | for_each_inferior_with_data (&all_threads, handle_qxfer_threads_worker, |
| 1325 | buffer); |
| 1326 | |
| 1327 | buffer_grow_str0 (buffer, "</threads>\n"); |
| 1328 | } |
| 1329 | |
| 1330 | /* Handle qXfer:threads:read. */ |
| 1331 | |
| 1332 | static int |
| 1333 | handle_qxfer_threads (const char *annex, |
| 1334 | gdb_byte *readbuf, const gdb_byte *writebuf, |
| 1335 | ULONGEST offset, LONGEST len) |
| 1336 | { |
| 1337 | static char *result = 0; |
| 1338 | static unsigned int result_length = 0; |
| 1339 | |
| 1340 | if (writebuf != NULL) |
| 1341 | return -2; |
| 1342 | |
| 1343 | if (!target_running () || annex[0] != '\0') |
| 1344 | return -1; |
| 1345 | |
| 1346 | if (offset == 0) |
| 1347 | { |
| 1348 | struct buffer buffer; |
| 1349 | /* When asked for data at offset 0, generate everything and store into |
| 1350 | 'result'. Successive reads will be served off 'result'. */ |
| 1351 | if (result) |
| 1352 | free (result); |
| 1353 | |
| 1354 | buffer_init (&buffer); |
| 1355 | |
| 1356 | handle_qxfer_threads_proper (&buffer); |
| 1357 | |
| 1358 | result = buffer_finish (&buffer); |
| 1359 | result_length = strlen (result); |
| 1360 | buffer_free (&buffer); |
| 1361 | } |
| 1362 | |
| 1363 | if (offset >= result_length) |
| 1364 | { |
| 1365 | /* We're out of data. */ |
| 1366 | free (result); |
| 1367 | result = NULL; |
| 1368 | result_length = 0; |
| 1369 | return 0; |
| 1370 | } |
| 1371 | |
| 1372 | if (len > result_length - offset) |
| 1373 | len = result_length - offset; |
| 1374 | |
| 1375 | memcpy (readbuf, result + offset, len); |
| 1376 | |
| 1377 | return len; |
| 1378 | } |
| 1379 | |
| 1380 | /* Handle qXfer:traceframe-info:read. */ |
| 1381 | |
| 1382 | static int |
| 1383 | handle_qxfer_traceframe_info (const char *annex, |
| 1384 | gdb_byte *readbuf, const gdb_byte *writebuf, |
| 1385 | ULONGEST offset, LONGEST len) |
| 1386 | { |
| 1387 | static char *result = 0; |
| 1388 | static unsigned int result_length = 0; |
| 1389 | |
| 1390 | if (writebuf != NULL) |
| 1391 | return -2; |
| 1392 | |
| 1393 | if (!target_running () || annex[0] != '\0' || current_traceframe == -1) |
| 1394 | return -1; |
| 1395 | |
| 1396 | if (offset == 0) |
| 1397 | { |
| 1398 | struct buffer buffer; |
| 1399 | |
| 1400 | /* When asked for data at offset 0, generate everything and |
| 1401 | store into 'result'. Successive reads will be served off |
| 1402 | 'result'. */ |
| 1403 | free (result); |
| 1404 | |
| 1405 | buffer_init (&buffer); |
| 1406 | |
| 1407 | traceframe_read_info (current_traceframe, &buffer); |
| 1408 | |
| 1409 | result = buffer_finish (&buffer); |
| 1410 | result_length = strlen (result); |
| 1411 | buffer_free (&buffer); |
| 1412 | } |
| 1413 | |
| 1414 | if (offset >= result_length) |
| 1415 | { |
| 1416 | /* We're out of data. */ |
| 1417 | free (result); |
| 1418 | result = NULL; |
| 1419 | result_length = 0; |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
| 1423 | if (len > result_length - offset) |
| 1424 | len = result_length - offset; |
| 1425 | |
| 1426 | memcpy (readbuf, result + offset, len); |
| 1427 | return len; |
| 1428 | } |
| 1429 | |
| 1430 | /* Handle qXfer:fdpic:read. */ |
| 1431 | |
| 1432 | static int |
| 1433 | handle_qxfer_fdpic (const char *annex, gdb_byte *readbuf, |
| 1434 | const gdb_byte *writebuf, ULONGEST offset, LONGEST len) |
| 1435 | { |
| 1436 | if (the_target->read_loadmap == NULL) |
| 1437 | return -2; |
| 1438 | |
| 1439 | if (!target_running ()) |
| 1440 | return -1; |
| 1441 | |
| 1442 | return (*the_target->read_loadmap) (annex, offset, readbuf, len); |
| 1443 | } |
| 1444 | |
| 1445 | /* Handle qXfer:btrace:read. */ |
| 1446 | |
| 1447 | static int |
| 1448 | handle_qxfer_btrace (const char *annex, |
| 1449 | gdb_byte *readbuf, const gdb_byte *writebuf, |
| 1450 | ULONGEST offset, LONGEST len) |
| 1451 | { |
| 1452 | static struct buffer cache; |
| 1453 | struct thread_info *thread; |
| 1454 | int type, result; |
| 1455 | |
| 1456 | if (the_target->read_btrace == NULL || writebuf != NULL) |
| 1457 | return -2; |
| 1458 | |
| 1459 | if (!target_running ()) |
| 1460 | return -1; |
| 1461 | |
| 1462 | if (ptid_equal (general_thread, null_ptid) |
| 1463 | || ptid_equal (general_thread, minus_one_ptid)) |
| 1464 | { |
| 1465 | strcpy (own_buf, "E.Must select a single thread."); |
| 1466 | return -3; |
| 1467 | } |
| 1468 | |
| 1469 | thread = find_thread_ptid (general_thread); |
| 1470 | if (thread == NULL) |
| 1471 | { |
| 1472 | strcpy (own_buf, "E.No such thread."); |
| 1473 | return -3; |
| 1474 | } |
| 1475 | |
| 1476 | if (thread->btrace == NULL) |
| 1477 | { |
| 1478 | strcpy (own_buf, "E.Btrace not enabled."); |
| 1479 | return -3; |
| 1480 | } |
| 1481 | |
| 1482 | if (strcmp (annex, "all") == 0) |
| 1483 | type = BTRACE_READ_ALL; |
| 1484 | else if (strcmp (annex, "new") == 0) |
| 1485 | type = BTRACE_READ_NEW; |
| 1486 | else if (strcmp (annex, "delta") == 0) |
| 1487 | type = BTRACE_READ_DELTA; |
| 1488 | else |
| 1489 | { |
| 1490 | strcpy (own_buf, "E.Bad annex."); |
| 1491 | return -3; |
| 1492 | } |
| 1493 | |
| 1494 | if (offset == 0) |
| 1495 | { |
| 1496 | buffer_free (&cache); |
| 1497 | |
| 1498 | result = target_read_btrace (thread->btrace, &cache, type); |
| 1499 | if (result != 0) |
| 1500 | { |
| 1501 | memcpy (own_buf, cache.buffer, cache.used_size); |
| 1502 | return -3; |
| 1503 | } |
| 1504 | } |
| 1505 | else if (offset > cache.used_size) |
| 1506 | { |
| 1507 | buffer_free (&cache); |
| 1508 | return -3; |
| 1509 | } |
| 1510 | |
| 1511 | if (len > cache.used_size - offset) |
| 1512 | len = cache.used_size - offset; |
| 1513 | |
| 1514 | memcpy (readbuf, cache.buffer + offset, len); |
| 1515 | |
| 1516 | return len; |
| 1517 | } |
| 1518 | |
| 1519 | static const struct qxfer qxfer_packets[] = |
| 1520 | { |
| 1521 | { "auxv", handle_qxfer_auxv }, |
| 1522 | { "btrace", handle_qxfer_btrace }, |
| 1523 | { "fdpic", handle_qxfer_fdpic}, |
| 1524 | { "features", handle_qxfer_features }, |
| 1525 | { "libraries", handle_qxfer_libraries }, |
| 1526 | { "libraries-svr4", handle_qxfer_libraries_svr4 }, |
| 1527 | { "osdata", handle_qxfer_osdata }, |
| 1528 | { "siginfo", handle_qxfer_siginfo }, |
| 1529 | { "spu", handle_qxfer_spu }, |
| 1530 | { "statictrace", handle_qxfer_statictrace }, |
| 1531 | { "threads", handle_qxfer_threads }, |
| 1532 | { "traceframe-info", handle_qxfer_traceframe_info }, |
| 1533 | }; |
| 1534 | |
| 1535 | static int |
| 1536 | handle_qxfer (char *own_buf, int packet_len, int *new_packet_len_p) |
| 1537 | { |
| 1538 | int i; |
| 1539 | char *object; |
| 1540 | char *rw; |
| 1541 | char *annex; |
| 1542 | char *offset; |
| 1543 | |
| 1544 | if (strncmp (own_buf, "qXfer:", 6) != 0) |
| 1545 | return 0; |
| 1546 | |
| 1547 | /* Grab the object, r/w and annex. */ |
| 1548 | if (decode_xfer (own_buf + 6, &object, &rw, &annex, &offset) < 0) |
| 1549 | { |
| 1550 | write_enn (own_buf); |
| 1551 | return 1; |
| 1552 | } |
| 1553 | |
| 1554 | for (i = 0; |
| 1555 | i < sizeof (qxfer_packets) / sizeof (qxfer_packets[0]); |
| 1556 | i++) |
| 1557 | { |
| 1558 | const struct qxfer *q = &qxfer_packets[i]; |
| 1559 | |
| 1560 | if (strcmp (object, q->object) == 0) |
| 1561 | { |
| 1562 | if (strcmp (rw, "read") == 0) |
| 1563 | { |
| 1564 | unsigned char *data; |
| 1565 | int n; |
| 1566 | CORE_ADDR ofs; |
| 1567 | unsigned int len; |
| 1568 | |
| 1569 | /* Grab the offset and length. */ |
| 1570 | if (decode_xfer_read (offset, &ofs, &len) < 0) |
| 1571 | { |
| 1572 | write_enn (own_buf); |
| 1573 | return 1; |
| 1574 | } |
| 1575 | |
| 1576 | /* Read one extra byte, as an indicator of whether there is |
| 1577 | more. */ |
| 1578 | if (len > PBUFSIZ - 2) |
| 1579 | len = PBUFSIZ - 2; |
| 1580 | data = malloc (len + 1); |
| 1581 | if (data == NULL) |
| 1582 | { |
| 1583 | write_enn (own_buf); |
| 1584 | return 1; |
| 1585 | } |
| 1586 | n = (*q->xfer) (annex, data, NULL, ofs, len + 1); |
| 1587 | if (n == -2) |
| 1588 | { |
| 1589 | free (data); |
| 1590 | return 0; |
| 1591 | } |
| 1592 | else if (n == -3) |
| 1593 | { |
| 1594 | /* Preserve error message. */ |
| 1595 | } |
| 1596 | else if (n < 0) |
| 1597 | write_enn (own_buf); |
| 1598 | else if (n > len) |
| 1599 | *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1); |
| 1600 | else |
| 1601 | *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0); |
| 1602 | |
| 1603 | free (data); |
| 1604 | return 1; |
| 1605 | } |
| 1606 | else if (strcmp (rw, "write") == 0) |
| 1607 | { |
| 1608 | int n; |
| 1609 | unsigned int len; |
| 1610 | CORE_ADDR ofs; |
| 1611 | unsigned char *data; |
| 1612 | |
| 1613 | strcpy (own_buf, "E00"); |
| 1614 | data = malloc (packet_len - (offset - own_buf)); |
| 1615 | if (data == NULL) |
| 1616 | { |
| 1617 | write_enn (own_buf); |
| 1618 | return 1; |
| 1619 | } |
| 1620 | if (decode_xfer_write (offset, packet_len - (offset - own_buf), |
| 1621 | &ofs, &len, data) < 0) |
| 1622 | { |
| 1623 | free (data); |
| 1624 | write_enn (own_buf); |
| 1625 | return 1; |
| 1626 | } |
| 1627 | |
| 1628 | n = (*q->xfer) (annex, NULL, data, ofs, len); |
| 1629 | if (n == -2) |
| 1630 | { |
| 1631 | free (data); |
| 1632 | return 0; |
| 1633 | } |
| 1634 | else if (n == -3) |
| 1635 | { |
| 1636 | /* Preserve error message. */ |
| 1637 | } |
| 1638 | else if (n < 0) |
| 1639 | write_enn (own_buf); |
| 1640 | else |
| 1641 | sprintf (own_buf, "%x", n); |
| 1642 | |
| 1643 | free (data); |
| 1644 | return 1; |
| 1645 | } |
| 1646 | |
| 1647 | return 0; |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | return 0; |
| 1652 | } |
| 1653 | |
| 1654 | /* Table used by the crc32 function to calcuate the checksum. */ |
| 1655 | |
| 1656 | static unsigned int crc32_table[256] = |
| 1657 | {0, 0}; |
| 1658 | |
| 1659 | /* Compute 32 bit CRC from inferior memory. |
| 1660 | |
| 1661 | On success, return 32 bit CRC. |
| 1662 | On failure, return (unsigned long long) -1. */ |
| 1663 | |
| 1664 | static unsigned long long |
| 1665 | crc32 (CORE_ADDR base, int len, unsigned int crc) |
| 1666 | { |
| 1667 | if (!crc32_table[1]) |
| 1668 | { |
| 1669 | /* Initialize the CRC table and the decoding table. */ |
| 1670 | int i, j; |
| 1671 | unsigned int c; |
| 1672 | |
| 1673 | for (i = 0; i < 256; i++) |
| 1674 | { |
| 1675 | for (c = i << 24, j = 8; j > 0; --j) |
| 1676 | c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1); |
| 1677 | crc32_table[i] = c; |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | while (len--) |
| 1682 | { |
| 1683 | unsigned char byte = 0; |
| 1684 | |
| 1685 | /* Return failure if memory read fails. */ |
| 1686 | if (read_inferior_memory (base, &byte, 1) != 0) |
| 1687 | return (unsigned long long) -1; |
| 1688 | |
| 1689 | crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ byte) & 255]; |
| 1690 | base++; |
| 1691 | } |
| 1692 | return (unsigned long long) crc; |
| 1693 | } |
| 1694 | |
| 1695 | /* Handle all of the extended 'q' packets. */ |
| 1696 | |
| 1697 | void |
| 1698 | handle_query (char *own_buf, int packet_len, int *new_packet_len_p) |
| 1699 | { |
| 1700 | static struct inferior_list_entry *thread_ptr; |
| 1701 | |
| 1702 | /* Reply the current thread id. */ |
| 1703 | if (strcmp ("qC", own_buf) == 0 && !disable_packet_qC) |
| 1704 | { |
| 1705 | ptid_t gdb_id; |
| 1706 | require_running (own_buf); |
| 1707 | |
| 1708 | if (!ptid_equal (general_thread, null_ptid) |
| 1709 | && !ptid_equal (general_thread, minus_one_ptid)) |
| 1710 | gdb_id = general_thread; |
| 1711 | else |
| 1712 | { |
| 1713 | thread_ptr = get_first_inferior (&all_threads); |
| 1714 | gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr); |
| 1715 | } |
| 1716 | |
| 1717 | sprintf (own_buf, "QC"); |
| 1718 | own_buf += 2; |
| 1719 | write_ptid (own_buf, gdb_id); |
| 1720 | return; |
| 1721 | } |
| 1722 | |
| 1723 | if (strcmp ("qSymbol::", own_buf) == 0) |
| 1724 | { |
| 1725 | /* GDB is suggesting new symbols have been loaded. This may |
| 1726 | mean a new shared library has been detected as loaded, so |
| 1727 | take the opportunity to check if breakpoints we think are |
| 1728 | inserted, still are. Note that it isn't guaranteed that |
| 1729 | we'll see this when a shared library is loaded, and nor will |
| 1730 | we see this for unloads (although breakpoints in unloaded |
| 1731 | libraries shouldn't trigger), as GDB may not find symbols for |
| 1732 | the library at all. We also re-validate breakpoints when we |
| 1733 | see a second GDB breakpoint for the same address, and or when |
| 1734 | we access breakpoint shadows. */ |
| 1735 | validate_breakpoints (); |
| 1736 | |
| 1737 | if (target_supports_tracepoints ()) |
| 1738 | tracepoint_look_up_symbols (); |
| 1739 | |
| 1740 | if (target_running () && the_target->look_up_symbols != NULL) |
| 1741 | (*the_target->look_up_symbols) (); |
| 1742 | |
| 1743 | strcpy (own_buf, "OK"); |
| 1744 | return; |
| 1745 | } |
| 1746 | |
| 1747 | if (!disable_packet_qfThreadInfo) |
| 1748 | { |
| 1749 | if (strcmp ("qfThreadInfo", own_buf) == 0) |
| 1750 | { |
| 1751 | ptid_t gdb_id; |
| 1752 | |
| 1753 | require_running (own_buf); |
| 1754 | thread_ptr = get_first_inferior (&all_threads); |
| 1755 | |
| 1756 | *own_buf++ = 'm'; |
| 1757 | gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr); |
| 1758 | write_ptid (own_buf, gdb_id); |
| 1759 | thread_ptr = thread_ptr->next; |
| 1760 | return; |
| 1761 | } |
| 1762 | |
| 1763 | if (strcmp ("qsThreadInfo", own_buf) == 0) |
| 1764 | { |
| 1765 | ptid_t gdb_id; |
| 1766 | |
| 1767 | require_running (own_buf); |
| 1768 | if (thread_ptr != NULL) |
| 1769 | { |
| 1770 | *own_buf++ = 'm'; |
| 1771 | gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr); |
| 1772 | write_ptid (own_buf, gdb_id); |
| 1773 | thread_ptr = thread_ptr->next; |
| 1774 | return; |
| 1775 | } |
| 1776 | else |
| 1777 | { |
| 1778 | sprintf (own_buf, "l"); |
| 1779 | return; |
| 1780 | } |
| 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | if (the_target->read_offsets != NULL |
| 1785 | && strcmp ("qOffsets", own_buf) == 0) |
| 1786 | { |
| 1787 | CORE_ADDR text, data; |
| 1788 | |
| 1789 | require_running (own_buf); |
| 1790 | if (the_target->read_offsets (&text, &data)) |
| 1791 | sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX", |
| 1792 | (long)text, (long)data, (long)data); |
| 1793 | else |
| 1794 | write_enn (own_buf); |
| 1795 | |
| 1796 | return; |
| 1797 | } |
| 1798 | |
| 1799 | /* Protocol features query. */ |
| 1800 | if (strncmp ("qSupported", own_buf, 10) == 0 |
| 1801 | && (own_buf[10] == ':' || own_buf[10] == '\0')) |
| 1802 | { |
| 1803 | char *p = &own_buf[10]; |
| 1804 | int gdb_supports_qRelocInsn = 0; |
| 1805 | |
| 1806 | /* Start processing qSupported packet. */ |
| 1807 | target_process_qsupported (NULL); |
| 1808 | |
| 1809 | /* Process each feature being provided by GDB. The first |
| 1810 | feature will follow a ':', and latter features will follow |
| 1811 | ';'. */ |
| 1812 | if (*p == ':') |
| 1813 | { |
| 1814 | char **qsupported = NULL; |
| 1815 | int count = 0; |
| 1816 | int i; |
| 1817 | |
| 1818 | /* Two passes, to avoid nested strtok calls in |
| 1819 | target_process_qsupported. */ |
| 1820 | for (p = strtok (p + 1, ";"); |
| 1821 | p != NULL; |
| 1822 | p = strtok (NULL, ";")) |
| 1823 | { |
| 1824 | count++; |
| 1825 | qsupported = xrealloc (qsupported, count * sizeof (char *)); |
| 1826 | qsupported[count - 1] = xstrdup (p); |
| 1827 | } |
| 1828 | |
| 1829 | for (i = 0; i < count; i++) |
| 1830 | { |
| 1831 | p = qsupported[i]; |
| 1832 | if (strcmp (p, "multiprocess+") == 0) |
| 1833 | { |
| 1834 | /* GDB supports and wants multi-process support if |
| 1835 | possible. */ |
| 1836 | if (target_supports_multi_process ()) |
| 1837 | multi_process = 1; |
| 1838 | } |
| 1839 | else if (strcmp (p, "qRelocInsn+") == 0) |
| 1840 | { |
| 1841 | /* GDB supports relocate instruction requests. */ |
| 1842 | gdb_supports_qRelocInsn = 1; |
| 1843 | } |
| 1844 | else |
| 1845 | target_process_qsupported (p); |
| 1846 | |
| 1847 | free (p); |
| 1848 | } |
| 1849 | |
| 1850 | free (qsupported); |
| 1851 | } |
| 1852 | |
| 1853 | sprintf (own_buf, |
| 1854 | "PacketSize=%x;QPassSignals+;QProgramSignals+", |
| 1855 | PBUFSIZ - 1); |
| 1856 | |
| 1857 | if (the_target->qxfer_libraries_svr4 != NULL) |
| 1858 | strcat (own_buf, ";qXfer:libraries-svr4:read+" |
| 1859 | ";augmented-libraries-svr4-read+"); |
| 1860 | else |
| 1861 | { |
| 1862 | /* We do not have any hook to indicate whether the non-SVR4 target |
| 1863 | backend supports qXfer:libraries:read, so always report it. */ |
| 1864 | strcat (own_buf, ";qXfer:libraries:read+"); |
| 1865 | } |
| 1866 | |
| 1867 | if (the_target->read_auxv != NULL) |
| 1868 | strcat (own_buf, ";qXfer:auxv:read+"); |
| 1869 | |
| 1870 | if (the_target->qxfer_spu != NULL) |
| 1871 | strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+"); |
| 1872 | |
| 1873 | if (the_target->qxfer_siginfo != NULL) |
| 1874 | strcat (own_buf, ";qXfer:siginfo:read+;qXfer:siginfo:write+"); |
| 1875 | |
| 1876 | if (the_target->read_loadmap != NULL) |
| 1877 | strcat (own_buf, ";qXfer:fdpic:read+"); |
| 1878 | |
| 1879 | /* We always report qXfer:features:read, as targets may |
| 1880 | install XML files on a subsequent call to arch_setup. |
| 1881 | If we reported to GDB on startup that we don't support |
| 1882 | qXfer:feature:read at all, we will never be re-queried. */ |
| 1883 | strcat (own_buf, ";qXfer:features:read+"); |
| 1884 | |
| 1885 | if (transport_is_reliable) |
| 1886 | strcat (own_buf, ";QStartNoAckMode+"); |
| 1887 | |
| 1888 | if (the_target->qxfer_osdata != NULL) |
| 1889 | strcat (own_buf, ";qXfer:osdata:read+"); |
| 1890 | |
| 1891 | if (target_supports_multi_process ()) |
| 1892 | strcat (own_buf, ";multiprocess+"); |
| 1893 | |
| 1894 | if (target_supports_non_stop ()) |
| 1895 | strcat (own_buf, ";QNonStop+"); |
| 1896 | |
| 1897 | if (target_supports_disable_randomization ()) |
| 1898 | strcat (own_buf, ";QDisableRandomization+"); |
| 1899 | |
| 1900 | strcat (own_buf, ";qXfer:threads:read+"); |
| 1901 | |
| 1902 | if (target_supports_tracepoints ()) |
| 1903 | { |
| 1904 | strcat (own_buf, ";ConditionalTracepoints+"); |
| 1905 | strcat (own_buf, ";TraceStateVariables+"); |
| 1906 | strcat (own_buf, ";TracepointSource+"); |
| 1907 | strcat (own_buf, ";DisconnectedTracing+"); |
| 1908 | if (gdb_supports_qRelocInsn && target_supports_fast_tracepoints ()) |
| 1909 | strcat (own_buf, ";FastTracepoints+"); |
| 1910 | strcat (own_buf, ";StaticTracepoints+"); |
| 1911 | strcat (own_buf, ";InstallInTrace+"); |
| 1912 | strcat (own_buf, ";qXfer:statictrace:read+"); |
| 1913 | strcat (own_buf, ";qXfer:traceframe-info:read+"); |
| 1914 | strcat (own_buf, ";EnableDisableTracepoints+"); |
| 1915 | strcat (own_buf, ";QTBuffer:size+"); |
| 1916 | strcat (own_buf, ";tracenz+"); |
| 1917 | } |
| 1918 | |
| 1919 | /* Support target-side breakpoint conditions and commands. */ |
| 1920 | strcat (own_buf, ";ConditionalBreakpoints+"); |
| 1921 | strcat (own_buf, ";BreakpointCommands+"); |
| 1922 | |
| 1923 | if (target_supports_agent ()) |
| 1924 | strcat (own_buf, ";QAgent+"); |
| 1925 | |
| 1926 | if (target_supports_btrace ()) |
| 1927 | { |
| 1928 | strcat (own_buf, ";Qbtrace:bts+"); |
| 1929 | strcat (own_buf, ";Qbtrace:off+"); |
| 1930 | strcat (own_buf, ";qXfer:btrace:read+"); |
| 1931 | } |
| 1932 | |
| 1933 | return; |
| 1934 | } |
| 1935 | |
| 1936 | /* Thread-local storage support. */ |
| 1937 | if (the_target->get_tls_address != NULL |
| 1938 | && strncmp ("qGetTLSAddr:", own_buf, 12) == 0) |
| 1939 | { |
| 1940 | char *p = own_buf + 12; |
| 1941 | CORE_ADDR parts[2], address = 0; |
| 1942 | int i, err; |
| 1943 | ptid_t ptid = null_ptid; |
| 1944 | |
| 1945 | require_running (own_buf); |
| 1946 | |
| 1947 | for (i = 0; i < 3; i++) |
| 1948 | { |
| 1949 | char *p2; |
| 1950 | int len; |
| 1951 | |
| 1952 | if (p == NULL) |
| 1953 | break; |
| 1954 | |
| 1955 | p2 = strchr (p, ','); |
| 1956 | if (p2) |
| 1957 | { |
| 1958 | len = p2 - p; |
| 1959 | p2++; |
| 1960 | } |
| 1961 | else |
| 1962 | { |
| 1963 | len = strlen (p); |
| 1964 | p2 = NULL; |
| 1965 | } |
| 1966 | |
| 1967 | if (i == 0) |
| 1968 | ptid = read_ptid (p, NULL); |
| 1969 | else |
| 1970 | decode_address (&parts[i - 1], p, len); |
| 1971 | p = p2; |
| 1972 | } |
| 1973 | |
| 1974 | if (p != NULL || i < 3) |
| 1975 | err = 1; |
| 1976 | else |
| 1977 | { |
| 1978 | struct thread_info *thread = find_thread_ptid (ptid); |
| 1979 | |
| 1980 | if (thread == NULL) |
| 1981 | err = 2; |
| 1982 | else |
| 1983 | err = the_target->get_tls_address (thread, parts[0], parts[1], |
| 1984 | &address); |
| 1985 | } |
| 1986 | |
| 1987 | if (err == 0) |
| 1988 | { |
| 1989 | strcpy (own_buf, paddress(address)); |
| 1990 | return; |
| 1991 | } |
| 1992 | else if (err > 0) |
| 1993 | { |
| 1994 | write_enn (own_buf); |
| 1995 | return; |
| 1996 | } |
| 1997 | |
| 1998 | /* Otherwise, pretend we do not understand this packet. */ |
| 1999 | } |
| 2000 | |
| 2001 | /* Windows OS Thread Information Block address support. */ |
| 2002 | if (the_target->get_tib_address != NULL |
| 2003 | && strncmp ("qGetTIBAddr:", own_buf, 12) == 0) |
| 2004 | { |
| 2005 | char *annex; |
| 2006 | int n; |
| 2007 | CORE_ADDR tlb; |
| 2008 | ptid_t ptid = read_ptid (own_buf + 12, &annex); |
| 2009 | |
| 2010 | n = (*the_target->get_tib_address) (ptid, &tlb); |
| 2011 | if (n == 1) |
| 2012 | { |
| 2013 | strcpy (own_buf, paddress(tlb)); |
| 2014 | return; |
| 2015 | } |
| 2016 | else if (n == 0) |
| 2017 | { |
| 2018 | write_enn (own_buf); |
| 2019 | return; |
| 2020 | } |
| 2021 | return; |
| 2022 | } |
| 2023 | |
| 2024 | /* Handle "monitor" commands. */ |
| 2025 | if (strncmp ("qRcmd,", own_buf, 6) == 0) |
| 2026 | { |
| 2027 | char *mon = malloc (PBUFSIZ); |
| 2028 | int len = strlen (own_buf + 6); |
| 2029 | |
| 2030 | if (mon == NULL) |
| 2031 | { |
| 2032 | write_enn (own_buf); |
| 2033 | return; |
| 2034 | } |
| 2035 | |
| 2036 | if ((len % 2) != 0 |
| 2037 | || hex2bin (own_buf + 6, (gdb_byte *) mon, len / 2) != len / 2) |
| 2038 | { |
| 2039 | write_enn (own_buf); |
| 2040 | free (mon); |
| 2041 | return; |
| 2042 | } |
| 2043 | mon[len / 2] = '\0'; |
| 2044 | |
| 2045 | write_ok (own_buf); |
| 2046 | |
| 2047 | if (the_target->handle_monitor_command == NULL |
| 2048 | || (*the_target->handle_monitor_command) (mon) == 0) |
| 2049 | /* Default processing. */ |
| 2050 | handle_monitor_command (mon, own_buf); |
| 2051 | |
| 2052 | free (mon); |
| 2053 | return; |
| 2054 | } |
| 2055 | |
| 2056 | if (strncmp ("qSearch:memory:", own_buf, |
| 2057 | sizeof ("qSearch:memory:") - 1) == 0) |
| 2058 | { |
| 2059 | require_running (own_buf); |
| 2060 | handle_search_memory (own_buf, packet_len); |
| 2061 | return; |
| 2062 | } |
| 2063 | |
| 2064 | if (strcmp (own_buf, "qAttached") == 0 |
| 2065 | || strncmp (own_buf, "qAttached:", sizeof ("qAttached:") - 1) == 0) |
| 2066 | { |
| 2067 | struct process_info *process; |
| 2068 | |
| 2069 | if (own_buf[sizeof ("qAttached") - 1]) |
| 2070 | { |
| 2071 | int pid = strtoul (own_buf + sizeof ("qAttached:") - 1, NULL, 16); |
| 2072 | process = (struct process_info *) |
| 2073 | find_inferior_id (&all_processes, pid_to_ptid (pid)); |
| 2074 | } |
| 2075 | else |
| 2076 | { |
| 2077 | require_running (own_buf); |
| 2078 | process = current_process (); |
| 2079 | } |
| 2080 | |
| 2081 | if (process == NULL) |
| 2082 | { |
| 2083 | write_enn (own_buf); |
| 2084 | return; |
| 2085 | } |
| 2086 | |
| 2087 | strcpy (own_buf, process->attached ? "1" : "0"); |
| 2088 | return; |
| 2089 | } |
| 2090 | |
| 2091 | if (strncmp ("qCRC:", own_buf, 5) == 0) |
| 2092 | { |
| 2093 | /* CRC check (compare-section). */ |
| 2094 | char *comma; |
| 2095 | ULONGEST base; |
| 2096 | int len; |
| 2097 | unsigned long long crc; |
| 2098 | |
| 2099 | require_running (own_buf); |
| 2100 | comma = unpack_varlen_hex (own_buf + 5, &base); |
| 2101 | if (*comma++ != ',') |
| 2102 | { |
| 2103 | write_enn (own_buf); |
| 2104 | return; |
| 2105 | } |
| 2106 | len = strtoul (comma, NULL, 16); |
| 2107 | crc = crc32 (base, len, 0xffffffff); |
| 2108 | /* Check for memory failure. */ |
| 2109 | if (crc == (unsigned long long) -1) |
| 2110 | { |
| 2111 | write_enn (own_buf); |
| 2112 | return; |
| 2113 | } |
| 2114 | sprintf (own_buf, "C%lx", (unsigned long) crc); |
| 2115 | return; |
| 2116 | } |
| 2117 | |
| 2118 | if (handle_qxfer (own_buf, packet_len, new_packet_len_p)) |
| 2119 | return; |
| 2120 | |
| 2121 | if (target_supports_tracepoints () && handle_tracepoint_query (own_buf)) |
| 2122 | return; |
| 2123 | |
| 2124 | /* Otherwise we didn't know what packet it was. Say we didn't |
| 2125 | understand it. */ |
| 2126 | own_buf[0] = 0; |
| 2127 | } |
| 2128 | |
| 2129 | static void gdb_wants_all_threads_stopped (void); |
| 2130 | static void resume (struct thread_resume *actions, size_t n); |
| 2131 | |
| 2132 | /* The callback that is passed to visit_actioned_threads. */ |
| 2133 | typedef int (visit_actioned_threads_callback_ftype) |
| 2134 | (const struct thread_resume *, struct thread_info *); |
| 2135 | |
| 2136 | /* Struct to pass data to visit_actioned_threads. */ |
| 2137 | |
| 2138 | struct visit_actioned_threads_data |
| 2139 | { |
| 2140 | const struct thread_resume *actions; |
| 2141 | size_t num_actions; |
| 2142 | visit_actioned_threads_callback_ftype *callback; |
| 2143 | }; |
| 2144 | |
| 2145 | /* Call CALLBACK for any thread to which ACTIONS applies to. Returns |
| 2146 | true if CALLBACK returns true. Returns false if no matching thread |
| 2147 | is found or CALLBACK results false. |
| 2148 | Note: This function is itself a callback for find_inferior. */ |
| 2149 | |
| 2150 | static int |
| 2151 | visit_actioned_threads (struct inferior_list_entry *entry, void *datap) |
| 2152 | { |
| 2153 | struct visit_actioned_threads_data *data = datap; |
| 2154 | const struct thread_resume *actions = data->actions; |
| 2155 | size_t num_actions = data->num_actions; |
| 2156 | visit_actioned_threads_callback_ftype *callback = data->callback; |
| 2157 | size_t i; |
| 2158 | |
| 2159 | for (i = 0; i < num_actions; i++) |
| 2160 | { |
| 2161 | const struct thread_resume *action = &actions[i]; |
| 2162 | |
| 2163 | if (ptid_equal (action->thread, minus_one_ptid) |
| 2164 | || ptid_equal (action->thread, entry->id) |
| 2165 | || ((ptid_get_pid (action->thread) |
| 2166 | == ptid_get_pid (entry->id)) |
| 2167 | && ptid_get_lwp (action->thread) == -1)) |
| 2168 | { |
| 2169 | struct thread_info *thread = (struct thread_info *) entry; |
| 2170 | |
| 2171 | if ((*callback) (action, thread)) |
| 2172 | return 1; |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | return 0; |
| 2177 | } |
| 2178 | |
| 2179 | /* Callback for visit_actioned_threads. If the thread has a pending |
| 2180 | status to report, report it now. */ |
| 2181 | |
| 2182 | static int |
| 2183 | handle_pending_status (const struct thread_resume *resumption, |
| 2184 | struct thread_info *thread) |
| 2185 | { |
| 2186 | if (thread->status_pending_p) |
| 2187 | { |
| 2188 | thread->status_pending_p = 0; |
| 2189 | |
| 2190 | last_status = thread->last_status; |
| 2191 | last_ptid = thread->entry.id; |
| 2192 | prepare_resume_reply (own_buf, last_ptid, &last_status); |
| 2193 | return 1; |
| 2194 | } |
| 2195 | return 0; |
| 2196 | } |
| 2197 | |
| 2198 | /* Parse vCont packets. */ |
| 2199 | void |
| 2200 | handle_v_cont (char *own_buf) |
| 2201 | { |
| 2202 | char *p, *q; |
| 2203 | int n = 0, i = 0; |
| 2204 | struct thread_resume *resume_info; |
| 2205 | struct thread_resume default_action = {{0}}; |
| 2206 | |
| 2207 | /* Count the number of semicolons in the packet. There should be one |
| 2208 | for every action. */ |
| 2209 | p = &own_buf[5]; |
| 2210 | while (p) |
| 2211 | { |
| 2212 | n++; |
| 2213 | p++; |
| 2214 | p = strchr (p, ';'); |
| 2215 | } |
| 2216 | |
| 2217 | resume_info = malloc (n * sizeof (resume_info[0])); |
| 2218 | if (resume_info == NULL) |
| 2219 | goto err; |
| 2220 | |
| 2221 | p = &own_buf[5]; |
| 2222 | while (*p) |
| 2223 | { |
| 2224 | p++; |
| 2225 | |
| 2226 | memset (&resume_info[i], 0, sizeof resume_info[i]); |
| 2227 | |
| 2228 | if (p[0] == 's' || p[0] == 'S') |
| 2229 | resume_info[i].kind = resume_step; |
| 2230 | else if (p[0] == 'r') |
| 2231 | resume_info[i].kind = resume_step; |
| 2232 | else if (p[0] == 'c' || p[0] == 'C') |
| 2233 | resume_info[i].kind = resume_continue; |
| 2234 | else if (p[0] == 't') |
| 2235 | resume_info[i].kind = resume_stop; |
| 2236 | else |
| 2237 | goto err; |
| 2238 | |
| 2239 | if (p[0] == 'S' || p[0] == 'C') |
| 2240 | { |
| 2241 | int sig; |
| 2242 | sig = strtol (p + 1, &q, 16); |
| 2243 | if (p == q) |
| 2244 | goto err; |
| 2245 | p = q; |
| 2246 | |
| 2247 | if (!gdb_signal_to_host_p (sig)) |
| 2248 | goto err; |
| 2249 | resume_info[i].sig = gdb_signal_to_host (sig); |
| 2250 | } |
| 2251 | else if (p[0] == 'r') |
| 2252 | { |
| 2253 | ULONGEST addr; |
| 2254 | |
| 2255 | p = unpack_varlen_hex (p + 1, &addr); |
| 2256 | resume_info[i].step_range_start = addr; |
| 2257 | |
| 2258 | if (*p != ',') |
| 2259 | goto err; |
| 2260 | |
| 2261 | p = unpack_varlen_hex (p + 1, &addr); |
| 2262 | resume_info[i].step_range_end = addr; |
| 2263 | } |
| 2264 | else |
| 2265 | { |
| 2266 | p = p + 1; |
| 2267 | } |
| 2268 | |
| 2269 | if (p[0] == 0) |
| 2270 | { |
| 2271 | resume_info[i].thread = minus_one_ptid; |
| 2272 | default_action = resume_info[i]; |
| 2273 | |
| 2274 | /* Note: we don't increment i here, we'll overwrite this entry |
| 2275 | the next time through. */ |
| 2276 | } |
| 2277 | else if (p[0] == ':') |
| 2278 | { |
| 2279 | ptid_t ptid = read_ptid (p + 1, &q); |
| 2280 | |
| 2281 | if (p == q) |
| 2282 | goto err; |
| 2283 | p = q; |
| 2284 | if (p[0] != ';' && p[0] != 0) |
| 2285 | goto err; |
| 2286 | |
| 2287 | resume_info[i].thread = ptid; |
| 2288 | |
| 2289 | i++; |
| 2290 | } |
| 2291 | } |
| 2292 | |
| 2293 | if (i < n) |
| 2294 | resume_info[i] = default_action; |
| 2295 | |
| 2296 | set_desired_thread (0); |
| 2297 | |
| 2298 | resume (resume_info, n); |
| 2299 | free (resume_info); |
| 2300 | return; |
| 2301 | |
| 2302 | err: |
| 2303 | write_enn (own_buf); |
| 2304 | free (resume_info); |
| 2305 | return; |
| 2306 | } |
| 2307 | |
| 2308 | /* Resume target with ACTIONS, an array of NUM_ACTIONS elements. */ |
| 2309 | |
| 2310 | static void |
| 2311 | resume (struct thread_resume *actions, size_t num_actions) |
| 2312 | { |
| 2313 | if (!non_stop) |
| 2314 | { |
| 2315 | /* Check if among the threads that GDB wants actioned, there's |
| 2316 | one with a pending status to report. If so, skip actually |
| 2317 | resuming/stopping and report the pending event |
| 2318 | immediately. */ |
| 2319 | struct visit_actioned_threads_data data; |
| 2320 | |
| 2321 | data.actions = actions; |
| 2322 | data.num_actions = num_actions; |
| 2323 | data.callback = handle_pending_status; |
| 2324 | if (find_inferior (&all_threads, visit_actioned_threads, &data) != NULL) |
| 2325 | return; |
| 2326 | |
| 2327 | enable_async_io (); |
| 2328 | } |
| 2329 | |
| 2330 | (*the_target->resume) (actions, num_actions); |
| 2331 | |
| 2332 | if (non_stop) |
| 2333 | write_ok (own_buf); |
| 2334 | else |
| 2335 | { |
| 2336 | last_ptid = mywait (minus_one_ptid, &last_status, 0, 1); |
| 2337 | |
| 2338 | if (last_status.kind == TARGET_WAITKIND_NO_RESUMED) |
| 2339 | { |
| 2340 | /* No proper RSP support for this yet. At least return |
| 2341 | error. */ |
| 2342 | sprintf (own_buf, "E.No unwaited-for children left."); |
| 2343 | disable_async_io (); |
| 2344 | return; |
| 2345 | } |
| 2346 | |
| 2347 | if (last_status.kind != TARGET_WAITKIND_EXITED |
| 2348 | && last_status.kind != TARGET_WAITKIND_SIGNALLED |
| 2349 | && last_status.kind != TARGET_WAITKIND_NO_RESUMED) |
| 2350 | current_thread->last_status = last_status; |
| 2351 | |
| 2352 | /* From the client's perspective, all-stop mode always stops all |
| 2353 | threads implicitly (and the target backend has already done |
| 2354 | so by now). Tag all threads as "want-stopped", so we don't |
| 2355 | resume them implicitly without the client telling us to. */ |
| 2356 | gdb_wants_all_threads_stopped (); |
| 2357 | prepare_resume_reply (own_buf, last_ptid, &last_status); |
| 2358 | disable_async_io (); |
| 2359 | |
| 2360 | if (last_status.kind == TARGET_WAITKIND_EXITED |
| 2361 | || last_status.kind == TARGET_WAITKIND_SIGNALLED) |
| 2362 | mourn_inferior (find_process_pid (ptid_get_pid (last_ptid))); |
| 2363 | } |
| 2364 | } |
| 2365 | |
| 2366 | /* Attach to a new program. Return 1 if successful, 0 if failure. */ |
| 2367 | int |
| 2368 | handle_v_attach (char *own_buf) |
| 2369 | { |
| 2370 | int pid; |
| 2371 | |
| 2372 | pid = strtol (own_buf + 8, NULL, 16); |
| 2373 | if (pid != 0 && attach_inferior (pid) == 0) |
| 2374 | { |
| 2375 | /* Don't report shared library events after attaching, even if |
| 2376 | some libraries are preloaded. GDB will always poll the |
| 2377 | library list. Avoids the "stopped by shared library event" |
| 2378 | notice on the GDB side. */ |
| 2379 | dlls_changed = 0; |
| 2380 | |
| 2381 | if (non_stop) |
| 2382 | { |
| 2383 | /* In non-stop, we don't send a resume reply. Stop events |
| 2384 | will follow up using the normal notification |
| 2385 | mechanism. */ |
| 2386 | write_ok (own_buf); |
| 2387 | } |
| 2388 | else |
| 2389 | prepare_resume_reply (own_buf, last_ptid, &last_status); |
| 2390 | |
| 2391 | return 1; |
| 2392 | } |
| 2393 | else |
| 2394 | { |
| 2395 | write_enn (own_buf); |
| 2396 | return 0; |
| 2397 | } |
| 2398 | } |
| 2399 | |
| 2400 | /* Run a new program. Return 1 if successful, 0 if failure. */ |
| 2401 | static int |
| 2402 | handle_v_run (char *own_buf) |
| 2403 | { |
| 2404 | char *p, *next_p, **new_argv; |
| 2405 | int i, new_argc; |
| 2406 | |
| 2407 | new_argc = 0; |
| 2408 | for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';')) |
| 2409 | { |
| 2410 | p++; |
| 2411 | new_argc++; |
| 2412 | } |
| 2413 | |
| 2414 | new_argv = calloc (new_argc + 2, sizeof (char *)); |
| 2415 | if (new_argv == NULL) |
| 2416 | { |
| 2417 | write_enn (own_buf); |
| 2418 | return 0; |
| 2419 | } |
| 2420 | |
| 2421 | i = 0; |
| 2422 | for (p = own_buf + strlen ("vRun;"); *p; p = next_p) |
| 2423 | { |
| 2424 | next_p = strchr (p, ';'); |
| 2425 | if (next_p == NULL) |
| 2426 | next_p = p + strlen (p); |
| 2427 | |
| 2428 | if (i == 0 && p == next_p) |
| 2429 | new_argv[i] = NULL; |
| 2430 | else |
| 2431 | { |
| 2432 | /* FIXME: Fail request if out of memory instead of dying. */ |
| 2433 | new_argv[i] = xmalloc (1 + (next_p - p) / 2); |
| 2434 | hex2bin (p, (gdb_byte *) new_argv[i], (next_p - p) / 2); |
| 2435 | new_argv[i][(next_p - p) / 2] = '\0'; |
| 2436 | } |
| 2437 | |
| 2438 | if (*next_p) |
| 2439 | next_p++; |
| 2440 | i++; |
| 2441 | } |
| 2442 | new_argv[i] = NULL; |
| 2443 | |
| 2444 | if (new_argv[0] == NULL) |
| 2445 | { |
| 2446 | /* GDB didn't specify a program to run. Use the program from the |
| 2447 | last run with the new argument list. */ |
| 2448 | |
| 2449 | if (program_argv == NULL) |
| 2450 | { |
| 2451 | write_enn (own_buf); |
| 2452 | freeargv (new_argv); |
| 2453 | return 0; |
| 2454 | } |
| 2455 | |
| 2456 | new_argv[0] = strdup (program_argv[0]); |
| 2457 | if (new_argv[0] == NULL) |
| 2458 | { |
| 2459 | write_enn (own_buf); |
| 2460 | freeargv (new_argv); |
| 2461 | return 0; |
| 2462 | } |
| 2463 | } |
| 2464 | |
| 2465 | /* Free the old argv and install the new one. */ |
| 2466 | freeargv (program_argv); |
| 2467 | program_argv = new_argv; |
| 2468 | |
| 2469 | start_inferior (program_argv); |
| 2470 | if (last_status.kind == TARGET_WAITKIND_STOPPED) |
| 2471 | { |
| 2472 | prepare_resume_reply (own_buf, last_ptid, &last_status); |
| 2473 | |
| 2474 | /* In non-stop, sending a resume reply doesn't set the general |
| 2475 | thread, but GDB assumes a vRun sets it (this is so GDB can |
| 2476 | query which is the main thread of the new inferior. */ |
| 2477 | if (non_stop) |
| 2478 | general_thread = last_ptid; |
| 2479 | |
| 2480 | return 1; |
| 2481 | } |
| 2482 | else |
| 2483 | { |
| 2484 | write_enn (own_buf); |
| 2485 | return 0; |
| 2486 | } |
| 2487 | } |
| 2488 | |
| 2489 | /* Kill process. Return 1 if successful, 0 if failure. */ |
| 2490 | int |
| 2491 | handle_v_kill (char *own_buf) |
| 2492 | { |
| 2493 | int pid; |
| 2494 | char *p = &own_buf[6]; |
| 2495 | if (multi_process) |
| 2496 | pid = strtol (p, NULL, 16); |
| 2497 | else |
| 2498 | pid = signal_pid; |
| 2499 | if (pid != 0 && kill_inferior (pid) == 0) |
| 2500 | { |
| 2501 | last_status.kind = TARGET_WAITKIND_SIGNALLED; |
| 2502 | last_status.value.sig = GDB_SIGNAL_KILL; |
| 2503 | last_ptid = pid_to_ptid (pid); |
| 2504 | discard_queued_stop_replies (pid); |
| 2505 | write_ok (own_buf); |
| 2506 | return 1; |
| 2507 | } |
| 2508 | else |
| 2509 | { |
| 2510 | write_enn (own_buf); |
| 2511 | return 0; |
| 2512 | } |
| 2513 | } |
| 2514 | |
| 2515 | /* Handle all of the extended 'v' packets. */ |
| 2516 | void |
| 2517 | handle_v_requests (char *own_buf, int packet_len, int *new_packet_len) |
| 2518 | { |
| 2519 | if (!disable_packet_vCont) |
| 2520 | { |
| 2521 | if (strncmp (own_buf, "vCont;", 6) == 0) |
| 2522 | { |
| 2523 | require_running (own_buf); |
| 2524 | handle_v_cont (own_buf); |
| 2525 | return; |
| 2526 | } |
| 2527 | |
| 2528 | if (strncmp (own_buf, "vCont?", 6) == 0) |
| 2529 | { |
| 2530 | strcpy (own_buf, "vCont;c;C;s;S;t"); |
| 2531 | if (target_supports_range_stepping ()) |
| 2532 | { |
| 2533 | own_buf = own_buf + strlen (own_buf); |
| 2534 | strcpy (own_buf, ";r"); |
| 2535 | } |
| 2536 | return; |
| 2537 | } |
| 2538 | } |
| 2539 | |
| 2540 | if (strncmp (own_buf, "vFile:", 6) == 0 |
| 2541 | && handle_vFile (own_buf, packet_len, new_packet_len)) |
| 2542 | return; |
| 2543 | |
| 2544 | if (strncmp (own_buf, "vAttach;", 8) == 0) |
| 2545 | { |
| 2546 | if ((!extended_protocol || !multi_process) && target_running ()) |
| 2547 | { |
| 2548 | fprintf (stderr, "Already debugging a process\n"); |
| 2549 | write_enn (own_buf); |
| 2550 | return; |
| 2551 | } |
| 2552 | handle_v_attach (own_buf); |
| 2553 | return; |
| 2554 | } |
| 2555 | |
| 2556 | if (strncmp (own_buf, "vRun;", 5) == 0) |
| 2557 | { |
| 2558 | if ((!extended_protocol || !multi_process) && target_running ()) |
| 2559 | { |
| 2560 | fprintf (stderr, "Already debugging a process\n"); |
| 2561 | write_enn (own_buf); |
| 2562 | return; |
| 2563 | } |
| 2564 | handle_v_run (own_buf); |
| 2565 | return; |
| 2566 | } |
| 2567 | |
| 2568 | if (strncmp (own_buf, "vKill;", 6) == 0) |
| 2569 | { |
| 2570 | if (!target_running ()) |
| 2571 | { |
| 2572 | fprintf (stderr, "No process to kill\n"); |
| 2573 | write_enn (own_buf); |
| 2574 | return; |
| 2575 | } |
| 2576 | handle_v_kill (own_buf); |
| 2577 | return; |
| 2578 | } |
| 2579 | |
| 2580 | if (handle_notif_ack (own_buf, packet_len)) |
| 2581 | return; |
| 2582 | |
| 2583 | /* Otherwise we didn't know what packet it was. Say we didn't |
| 2584 | understand it. */ |
| 2585 | own_buf[0] = 0; |
| 2586 | return; |
| 2587 | } |
| 2588 | |
| 2589 | /* Resume thread and wait for another event. In non-stop mode, |
| 2590 | don't really wait here, but return immediatelly to the event |
| 2591 | loop. */ |
| 2592 | static void |
| 2593 | myresume (char *own_buf, int step, int sig) |
| 2594 | { |
| 2595 | struct thread_resume resume_info[2]; |
| 2596 | int n = 0; |
| 2597 | int valid_cont_thread; |
| 2598 | |
| 2599 | set_desired_thread (0); |
| 2600 | |
| 2601 | valid_cont_thread = (!ptid_equal (cont_thread, null_ptid) |
| 2602 | && !ptid_equal (cont_thread, minus_one_ptid)); |
| 2603 | |
| 2604 | if (step || sig || valid_cont_thread) |
| 2605 | { |
| 2606 | resume_info[0].thread = current_ptid; |
| 2607 | if (step) |
| 2608 | resume_info[0].kind = resume_step; |
| 2609 | else |
| 2610 | resume_info[0].kind = resume_continue; |
| 2611 | resume_info[0].sig = sig; |
| 2612 | n++; |
| 2613 | } |
| 2614 | |
| 2615 | if (!valid_cont_thread) |
| 2616 | { |
| 2617 | resume_info[n].thread = minus_one_ptid; |
| 2618 | resume_info[n].kind = resume_continue; |
| 2619 | resume_info[n].sig = 0; |
| 2620 | n++; |
| 2621 | } |
| 2622 | |
| 2623 | resume (resume_info, n); |
| 2624 | } |
| 2625 | |
| 2626 | /* Callback for for_each_inferior. Make a new stop reply for each |
| 2627 | stopped thread. */ |
| 2628 | |
| 2629 | static int |
| 2630 | queue_stop_reply_callback (struct inferior_list_entry *entry, void *arg) |
| 2631 | { |
| 2632 | struct thread_info *thread = (struct thread_info *) entry; |
| 2633 | |
| 2634 | /* For now, assume targets that don't have this callback also don't |
| 2635 | manage the thread's last_status field. */ |
| 2636 | if (the_target->thread_stopped == NULL) |
| 2637 | { |
| 2638 | struct vstop_notif *new_notif = xmalloc (sizeof (*new_notif)); |
| 2639 | |
| 2640 | new_notif->ptid = entry->id; |
| 2641 | new_notif->status = thread->last_status; |
| 2642 | /* Pass the last stop reply back to GDB, but don't notify |
| 2643 | yet. */ |
| 2644 | notif_event_enque (¬if_stop, |
| 2645 | (struct notif_event *) new_notif); |
| 2646 | } |
| 2647 | else |
| 2648 | { |
| 2649 | if (thread_stopped (thread)) |
| 2650 | { |
| 2651 | if (debug_threads) |
| 2652 | { |
| 2653 | char *status_string |
| 2654 | = target_waitstatus_to_string (&thread->last_status); |
| 2655 | |
| 2656 | debug_printf ("Reporting thread %s as already stopped with %s\n", |
| 2657 | target_pid_to_str (entry->id), |
| 2658 | status_string); |
| 2659 | |
| 2660 | xfree (status_string); |
| 2661 | } |
| 2662 | |
| 2663 | gdb_assert (thread->last_status.kind != TARGET_WAITKIND_IGNORE); |
| 2664 | |
| 2665 | /* Pass the last stop reply back to GDB, but don't notify |
| 2666 | yet. */ |
| 2667 | queue_stop_reply (entry->id, &thread->last_status); |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | return 0; |
| 2672 | } |
| 2673 | |
| 2674 | /* Set this inferior threads's state as "want-stopped". We won't |
| 2675 | resume this thread until the client gives us another action for |
| 2676 | it. */ |
| 2677 | |
| 2678 | static void |
| 2679 | gdb_wants_thread_stopped (struct inferior_list_entry *entry) |
| 2680 | { |
| 2681 | struct thread_info *thread = (struct thread_info *) entry; |
| 2682 | |
| 2683 | thread->last_resume_kind = resume_stop; |
| 2684 | |
| 2685 | if (thread->last_status.kind == TARGET_WAITKIND_IGNORE) |
| 2686 | { |
| 2687 | /* Most threads are stopped implicitly (all-stop); tag that with |
| 2688 | signal 0. */ |
| 2689 | thread->last_status.kind = TARGET_WAITKIND_STOPPED; |
| 2690 | thread->last_status.value.sig = GDB_SIGNAL_0; |
| 2691 | } |
| 2692 | } |
| 2693 | |
| 2694 | /* Set all threads' states as "want-stopped". */ |
| 2695 | |
| 2696 | static void |
| 2697 | gdb_wants_all_threads_stopped (void) |
| 2698 | { |
| 2699 | for_each_inferior (&all_threads, gdb_wants_thread_stopped); |
| 2700 | } |
| 2701 | |
| 2702 | /* Clear the gdb_detached flag of every process. */ |
| 2703 | |
| 2704 | static void |
| 2705 | gdb_reattached_process (struct inferior_list_entry *entry) |
| 2706 | { |
| 2707 | struct process_info *process = (struct process_info *) entry; |
| 2708 | |
| 2709 | process->gdb_detached = 0; |
| 2710 | } |
| 2711 | |
| 2712 | /* Callback for for_each_inferior. Clear the thread's pending status |
| 2713 | flag. */ |
| 2714 | |
| 2715 | static void |
| 2716 | clear_pending_status_callback (struct inferior_list_entry *entry) |
| 2717 | { |
| 2718 | struct thread_info *thread = (struct thread_info *) entry; |
| 2719 | |
| 2720 | thread->status_pending_p = 0; |
| 2721 | } |
| 2722 | |
| 2723 | /* Callback for for_each_inferior. If the thread is stopped with an |
| 2724 | interesting event, mark it as having a pending event. */ |
| 2725 | |
| 2726 | static void |
| 2727 | set_pending_status_callback (struct inferior_list_entry *entry) |
| 2728 | { |
| 2729 | struct thread_info *thread = (struct thread_info *) entry; |
| 2730 | |
| 2731 | if (thread->last_status.kind != TARGET_WAITKIND_STOPPED |
| 2732 | || (thread->last_status.value.sig != GDB_SIGNAL_0 |
| 2733 | /* A breakpoint, watchpoint or finished step from a previous |
| 2734 | GDB run isn't considered interesting for a new GDB run. |
| 2735 | If we left those pending, the new GDB could consider them |
| 2736 | random SIGTRAPs. This leaves out real async traps. We'd |
| 2737 | have to peek into the (target-specific) siginfo to |
| 2738 | distinguish those. */ |
| 2739 | && thread->last_status.value.sig != GDB_SIGNAL_TRAP)) |
| 2740 | thread->status_pending_p = 1; |
| 2741 | } |
| 2742 | |
| 2743 | /* Callback for find_inferior. Return true if ENTRY (a thread) has a |
| 2744 | pending status to report to GDB. */ |
| 2745 | |
| 2746 | static int |
| 2747 | find_status_pending_thread_callback (struct inferior_list_entry *entry, void *data) |
| 2748 | { |
| 2749 | struct thread_info *thread = (struct thread_info *) entry; |
| 2750 | |
| 2751 | return thread->status_pending_p; |
| 2752 | } |
| 2753 | |
| 2754 | /* Status handler for the '?' packet. */ |
| 2755 | |
| 2756 | static void |
| 2757 | handle_status (char *own_buf) |
| 2758 | { |
| 2759 | /* GDB is connected, don't forward events to the target anymore. */ |
| 2760 | for_each_inferior (&all_processes, gdb_reattached_process); |
| 2761 | |
| 2762 | /* In non-stop mode, we must send a stop reply for each stopped |
| 2763 | thread. In all-stop mode, just send one for the first stopped |
| 2764 | thread we find. */ |
| 2765 | |
| 2766 | if (non_stop) |
| 2767 | { |
| 2768 | find_inferior (&all_threads, queue_stop_reply_callback, NULL); |
| 2769 | |
| 2770 | /* The first is sent immediatly. OK is sent if there is no |
| 2771 | stopped thread, which is the same handling of the vStopped |
| 2772 | packet (by design). */ |
| 2773 | notif_write_event (¬if_stop, own_buf); |
| 2774 | } |
| 2775 | else |
| 2776 | { |
| 2777 | struct inferior_list_entry *thread = NULL; |
| 2778 | |
| 2779 | pause_all (0); |
| 2780 | stabilize_threads (); |
| 2781 | gdb_wants_all_threads_stopped (); |
| 2782 | |
| 2783 | /* We can only report one status, but we might be coming out of |
| 2784 | non-stop -- if more than one thread is stopped with |
| 2785 | interesting events, leave events for the threads we're not |
| 2786 | reporting now pending. They'll be reported the next time the |
| 2787 | threads are resumed. Start by marking all interesting events |
| 2788 | as pending. */ |
| 2789 | for_each_inferior (&all_threads, set_pending_status_callback); |
| 2790 | |
| 2791 | /* Prefer the last thread that reported an event to GDB (even if |
| 2792 | that was a GDB_SIGNAL_TRAP). */ |
| 2793 | if (last_status.kind != TARGET_WAITKIND_IGNORE |
| 2794 | && last_status.kind != TARGET_WAITKIND_EXITED |
| 2795 | && last_status.kind != TARGET_WAITKIND_SIGNALLED) |
| 2796 | thread = find_inferior_id (&all_threads, last_ptid); |
| 2797 | |
| 2798 | /* If the last event thread is not found for some reason, look |
| 2799 | for some other thread that might have an event to report. */ |
| 2800 | if (thread == NULL) |
| 2801 | thread = find_inferior (&all_threads, |
| 2802 | find_status_pending_thread_callback, NULL); |
| 2803 | |
| 2804 | /* If we're still out of luck, simply pick the first thread in |
| 2805 | the thread list. */ |
| 2806 | if (thread == NULL) |
| 2807 | thread = get_first_inferior (&all_threads); |
| 2808 | |
| 2809 | if (thread != NULL) |
| 2810 | { |
| 2811 | struct thread_info *tp = (struct thread_info *) thread; |
| 2812 | |
| 2813 | /* We're reporting this event, so it's no longer |
| 2814 | pending. */ |
| 2815 | tp->status_pending_p = 0; |
| 2816 | |
| 2817 | /* GDB assumes the current thread is the thread we're |
| 2818 | reporting the status for. */ |
| 2819 | general_thread = thread->id; |
| 2820 | set_desired_thread (1); |
| 2821 | |
| 2822 | gdb_assert (tp->last_status.kind != TARGET_WAITKIND_IGNORE); |
| 2823 | prepare_resume_reply (own_buf, tp->entry.id, &tp->last_status); |
| 2824 | } |
| 2825 | else |
| 2826 | strcpy (own_buf, "W00"); |
| 2827 | } |
| 2828 | } |
| 2829 | |
| 2830 | static void |
| 2831 | gdbserver_version (void) |
| 2832 | { |
| 2833 | printf ("GNU gdbserver %s%s\n" |
| 2834 | "Copyright (C) 2014 Free Software Foundation, Inc.\n" |
| 2835 | "gdbserver is free software, covered by the " |
| 2836 | "GNU General Public License.\n" |
| 2837 | "This gdbserver was configured as \"%s\"\n", |
| 2838 | PKGVERSION, version, host_name); |
| 2839 | } |
| 2840 | |
| 2841 | static void |
| 2842 | gdbserver_usage (FILE *stream) |
| 2843 | { |
| 2844 | fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n" |
| 2845 | "\tgdbserver [OPTIONS] --attach COMM PID\n" |
| 2846 | "\tgdbserver [OPTIONS] --multi COMM\n" |
| 2847 | "\n" |
| 2848 | "COMM may either be a tty device (for serial debugging), or \n" |
| 2849 | "HOST:PORT to listen for a TCP connection.\n" |
| 2850 | "\n" |
| 2851 | "Options:\n" |
| 2852 | " --debug Enable general debugging output.\n" |
| 2853 | " --debug-format=opt1[,opt2,...]\n" |
| 2854 | " Specify extra content in debugging output.\n" |
| 2855 | " Options:\n" |
| 2856 | " all\n" |
| 2857 | " none\n" |
| 2858 | " timestamp\n" |
| 2859 | " --remote-debug Enable remote protocol debugging output.\n" |
| 2860 | " --version Display version information and exit.\n" |
| 2861 | " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n" |
| 2862 | " --once Exit after the first connection has " |
| 2863 | "closed.\n"); |
| 2864 | if (REPORT_BUGS_TO[0] && stream == stdout) |
| 2865 | fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO); |
| 2866 | } |
| 2867 | |
| 2868 | static void |
| 2869 | gdbserver_show_disableable (FILE *stream) |
| 2870 | { |
| 2871 | fprintf (stream, "Disableable packets:\n" |
| 2872 | " vCont \tAll vCont packets\n" |
| 2873 | " qC \tQuerying the current thread\n" |
| 2874 | " qfThreadInfo\tThread listing\n" |
| 2875 | " Tthread \tPassing the thread specifier in the " |
| 2876 | "T stop reply packet\n" |
| 2877 | " threads \tAll of the above\n"); |
| 2878 | } |
| 2879 | |
| 2880 | |
| 2881 | #undef require_running |
| 2882 | #define require_running(BUF) \ |
| 2883 | if (!target_running ()) \ |
| 2884 | { \ |
| 2885 | write_enn (BUF); \ |
| 2886 | break; \ |
| 2887 | } |
| 2888 | |
| 2889 | static int |
| 2890 | first_thread_of (struct inferior_list_entry *entry, void *args) |
| 2891 | { |
| 2892 | int pid = * (int *) args; |
| 2893 | |
| 2894 | if (ptid_get_pid (entry->id) == pid) |
| 2895 | return 1; |
| 2896 | |
| 2897 | return 0; |
| 2898 | } |
| 2899 | |
| 2900 | static void |
| 2901 | kill_inferior_callback (struct inferior_list_entry *entry) |
| 2902 | { |
| 2903 | struct process_info *process = (struct process_info *) entry; |
| 2904 | int pid = ptid_get_pid (process->entry.id); |
| 2905 | |
| 2906 | kill_inferior (pid); |
| 2907 | discard_queued_stop_replies (pid); |
| 2908 | } |
| 2909 | |
| 2910 | /* Callback for for_each_inferior to detach or kill the inferior, |
| 2911 | depending on whether we attached to it or not. |
| 2912 | We inform the user whether we're detaching or killing the process |
| 2913 | as this is only called when gdbserver is about to exit. */ |
| 2914 | |
| 2915 | static void |
| 2916 | detach_or_kill_inferior_callback (struct inferior_list_entry *entry) |
| 2917 | { |
| 2918 | struct process_info *process = (struct process_info *) entry; |
| 2919 | int pid = ptid_get_pid (process->entry.id); |
| 2920 | |
| 2921 | if (process->attached) |
| 2922 | detach_inferior (pid); |
| 2923 | else |
| 2924 | kill_inferior (pid); |
| 2925 | |
| 2926 | discard_queued_stop_replies (pid); |
| 2927 | } |
| 2928 | |
| 2929 | /* for_each_inferior callback for detach_or_kill_for_exit to print |
| 2930 | the pids of started inferiors. */ |
| 2931 | |
| 2932 | static void |
| 2933 | print_started_pid (struct inferior_list_entry *entry) |
| 2934 | { |
| 2935 | struct process_info *process = (struct process_info *) entry; |
| 2936 | |
| 2937 | if (! process->attached) |
| 2938 | { |
| 2939 | int pid = ptid_get_pid (process->entry.id); |
| 2940 | fprintf (stderr, " %d", pid); |
| 2941 | } |
| 2942 | } |
| 2943 | |
| 2944 | /* for_each_inferior callback for detach_or_kill_for_exit to print |
| 2945 | the pids of attached inferiors. */ |
| 2946 | |
| 2947 | static void |
| 2948 | print_attached_pid (struct inferior_list_entry *entry) |
| 2949 | { |
| 2950 | struct process_info *process = (struct process_info *) entry; |
| 2951 | |
| 2952 | if (process->attached) |
| 2953 | { |
| 2954 | int pid = ptid_get_pid (process->entry.id); |
| 2955 | fprintf (stderr, " %d", pid); |
| 2956 | } |
| 2957 | } |
| 2958 | |
| 2959 | /* Call this when exiting gdbserver with possible inferiors that need |
| 2960 | to be killed or detached from. */ |
| 2961 | |
| 2962 | static void |
| 2963 | detach_or_kill_for_exit (void) |
| 2964 | { |
| 2965 | /* First print a list of the inferiors we will be killing/detaching. |
| 2966 | This is to assist the user, for example, in case the inferior unexpectedly |
| 2967 | dies after we exit: did we screw up or did the inferior exit on its own? |
| 2968 | Having this info will save some head-scratching. */ |
| 2969 | |
| 2970 | if (have_started_inferiors_p ()) |
| 2971 | { |
| 2972 | fprintf (stderr, "Killing process(es):"); |
| 2973 | for_each_inferior (&all_processes, print_started_pid); |
| 2974 | fprintf (stderr, "\n"); |
| 2975 | } |
| 2976 | if (have_attached_inferiors_p ()) |
| 2977 | { |
| 2978 | fprintf (stderr, "Detaching process(es):"); |
| 2979 | for_each_inferior (&all_processes, print_attached_pid); |
| 2980 | fprintf (stderr, "\n"); |
| 2981 | } |
| 2982 | |
| 2983 | /* Now we can kill or detach the inferiors. */ |
| 2984 | |
| 2985 | for_each_inferior (&all_processes, detach_or_kill_inferior_callback); |
| 2986 | } |
| 2987 | |
| 2988 | /* Value that will be passed to exit(3) when gdbserver exits. */ |
| 2989 | static int exit_code; |
| 2990 | |
| 2991 | /* Cleanup version of detach_or_kill_for_exit. */ |
| 2992 | |
| 2993 | static void |
| 2994 | detach_or_kill_for_exit_cleanup (void *ignore) |
| 2995 | { |
| 2996 | volatile struct gdb_exception exception; |
| 2997 | |
| 2998 | TRY_CATCH (exception, RETURN_MASK_ALL) |
| 2999 | { |
| 3000 | detach_or_kill_for_exit (); |
| 3001 | } |
| 3002 | |
| 3003 | if (exception.reason < 0) |
| 3004 | { |
| 3005 | fflush (stdout); |
| 3006 | fprintf (stderr, "Detach or kill failed: %s\n", exception.message); |
| 3007 | exit_code = 1; |
| 3008 | } |
| 3009 | } |
| 3010 | |
| 3011 | /* Main function. This is called by the real "main" function, |
| 3012 | wrapped in a TRY_CATCH that handles any uncaught exceptions. */ |
| 3013 | |
| 3014 | static void ATTRIBUTE_NORETURN |
| 3015 | captured_main (int argc, char *argv[]) |
| 3016 | { |
| 3017 | int bad_attach; |
| 3018 | int pid; |
| 3019 | char *arg_end, *port; |
| 3020 | char **next_arg = &argv[1]; |
| 3021 | volatile int multi_mode = 0; |
| 3022 | volatile int attach = 0; |
| 3023 | int was_running; |
| 3024 | |
| 3025 | while (*next_arg != NULL && **next_arg == '-') |
| 3026 | { |
| 3027 | if (strcmp (*next_arg, "--version") == 0) |
| 3028 | { |
| 3029 | gdbserver_version (); |
| 3030 | exit (0); |
| 3031 | } |
| 3032 | else if (strcmp (*next_arg, "--help") == 0) |
| 3033 | { |
| 3034 | gdbserver_usage (stdout); |
| 3035 | exit (0); |
| 3036 | } |
| 3037 | else if (strcmp (*next_arg, "--attach") == 0) |
| 3038 | attach = 1; |
| 3039 | else if (strcmp (*next_arg, "--multi") == 0) |
| 3040 | multi_mode = 1; |
| 3041 | else if (strcmp (*next_arg, "--wrapper") == 0) |
| 3042 | { |
| 3043 | next_arg++; |
| 3044 | |
| 3045 | wrapper_argv = next_arg; |
| 3046 | while (*next_arg != NULL && strcmp (*next_arg, "--") != 0) |
| 3047 | next_arg++; |
| 3048 | |
| 3049 | if (next_arg == wrapper_argv || *next_arg == NULL) |
| 3050 | { |
| 3051 | gdbserver_usage (stderr); |
| 3052 | exit (1); |
| 3053 | } |
| 3054 | |
| 3055 | /* Consume the "--". */ |
| 3056 | *next_arg = NULL; |
| 3057 | } |
| 3058 | else if (strcmp (*next_arg, "--debug") == 0) |
| 3059 | debug_threads = 1; |
| 3060 | else if (strncmp (*next_arg, |
| 3061 | "--debug-format=", |
| 3062 | sizeof ("--debug-format=") - 1) == 0) |
| 3063 | { |
| 3064 | char *error_msg |
| 3065 | = parse_debug_format_options ((*next_arg) |
| 3066 | + sizeof ("--debug-format=") - 1, 0); |
| 3067 | |
| 3068 | if (error_msg != NULL) |
| 3069 | { |
| 3070 | fprintf (stderr, "%s", error_msg); |
| 3071 | exit (1); |
| 3072 | } |
| 3073 | } |
| 3074 | else if (strcmp (*next_arg, "--remote-debug") == 0) |
| 3075 | remote_debug = 1; |
| 3076 | else if (strcmp (*next_arg, "--disable-packet") == 0) |
| 3077 | { |
| 3078 | gdbserver_show_disableable (stdout); |
| 3079 | exit (0); |
| 3080 | } |
| 3081 | else if (strncmp (*next_arg, |
| 3082 | "--disable-packet=", |
| 3083 | sizeof ("--disable-packet=") - 1) == 0) |
| 3084 | { |
| 3085 | char *packets, *tok; |
| 3086 | |
| 3087 | packets = *next_arg += sizeof ("--disable-packet=") - 1; |
| 3088 | for (tok = strtok (packets, ","); |
| 3089 | tok != NULL; |
| 3090 | tok = strtok (NULL, ",")) |
| 3091 | { |
| 3092 | if (strcmp ("vCont", tok) == 0) |
| 3093 | disable_packet_vCont = 1; |
| 3094 | else if (strcmp ("Tthread", tok) == 0) |
| 3095 | disable_packet_Tthread = 1; |
| 3096 | else if (strcmp ("qC", tok) == 0) |
| 3097 | disable_packet_qC = 1; |
| 3098 | else if (strcmp ("qfThreadInfo", tok) == 0) |
| 3099 | disable_packet_qfThreadInfo = 1; |
| 3100 | else if (strcmp ("threads", tok) == 0) |
| 3101 | { |
| 3102 | disable_packet_vCont = 1; |
| 3103 | disable_packet_Tthread = 1; |
| 3104 | disable_packet_qC = 1; |
| 3105 | disable_packet_qfThreadInfo = 1; |
| 3106 | } |
| 3107 | else |
| 3108 | { |
| 3109 | fprintf (stderr, "Don't know how to disable \"%s\".\n\n", |
| 3110 | tok); |
| 3111 | gdbserver_show_disableable (stderr); |
| 3112 | exit (1); |
| 3113 | } |
| 3114 | } |
| 3115 | } |
| 3116 | else if (strcmp (*next_arg, "-") == 0) |
| 3117 | { |
| 3118 | /* "-" specifies a stdio connection and is a form of port |
| 3119 | specification. */ |
| 3120 | *next_arg = STDIO_CONNECTION_NAME; |
| 3121 | break; |
| 3122 | } |
| 3123 | else if (strcmp (*next_arg, "--disable-randomization") == 0) |
| 3124 | disable_randomization = 1; |
| 3125 | else if (strcmp (*next_arg, "--no-disable-randomization") == 0) |
| 3126 | disable_randomization = 0; |
| 3127 | else if (strcmp (*next_arg, "--once") == 0) |
| 3128 | run_once = 1; |
| 3129 | else |
| 3130 | { |
| 3131 | fprintf (stderr, "Unknown argument: %s\n", *next_arg); |
| 3132 | exit (1); |
| 3133 | } |
| 3134 | |
| 3135 | next_arg++; |
| 3136 | continue; |
| 3137 | } |
| 3138 | |
| 3139 | port = *next_arg; |
| 3140 | next_arg++; |
| 3141 | if (port == NULL || (!attach && !multi_mode && *next_arg == NULL)) |
| 3142 | { |
| 3143 | gdbserver_usage (stderr); |
| 3144 | exit (1); |
| 3145 | } |
| 3146 | |
| 3147 | /* Remember stdio descriptors. LISTEN_DESC must not be listed, it will be |
| 3148 | opened by remote_prepare. */ |
| 3149 | notice_open_fds (); |
| 3150 | |
| 3151 | /* We need to know whether the remote connection is stdio before |
| 3152 | starting the inferior. Inferiors created in this scenario have |
| 3153 | stdin,stdout redirected. So do this here before we call |
| 3154 | start_inferior. */ |
| 3155 | remote_prepare (port); |
| 3156 | |
| 3157 | bad_attach = 0; |
| 3158 | pid = 0; |
| 3159 | |
| 3160 | /* --attach used to come after PORT, so allow it there for |
| 3161 | compatibility. */ |
| 3162 | if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0) |
| 3163 | { |
| 3164 | attach = 1; |
| 3165 | next_arg++; |
| 3166 | } |
| 3167 | |
| 3168 | if (attach |
| 3169 | && (*next_arg == NULL |
| 3170 | || (*next_arg)[0] == '\0' |
| 3171 | || (pid = strtoul (*next_arg, &arg_end, 0)) == 0 |
| 3172 | || *arg_end != '\0' |
| 3173 | || next_arg[1] != NULL)) |
| 3174 | bad_attach = 1; |
| 3175 | |
| 3176 | if (bad_attach) |
| 3177 | { |
| 3178 | gdbserver_usage (stderr); |
| 3179 | exit (1); |
| 3180 | } |
| 3181 | |
| 3182 | initialize_async_io (); |
| 3183 | initialize_low (); |
| 3184 | initialize_event_loop (); |
| 3185 | if (target_supports_tracepoints ()) |
| 3186 | initialize_tracepoint (); |
| 3187 | |
| 3188 | own_buf = xmalloc (PBUFSIZ + 1); |
| 3189 | mem_buf = xmalloc (PBUFSIZ); |
| 3190 | |
| 3191 | if (pid == 0 && *next_arg != NULL) |
| 3192 | { |
| 3193 | int i, n; |
| 3194 | |
| 3195 | n = argc - (next_arg - argv); |
| 3196 | program_argv = xmalloc (sizeof (char *) * (n + 1)); |
| 3197 | for (i = 0; i < n; i++) |
| 3198 | program_argv[i] = xstrdup (next_arg[i]); |
| 3199 | program_argv[i] = NULL; |
| 3200 | |
| 3201 | /* Wait till we are at first instruction in program. */ |
| 3202 | start_inferior (program_argv); |
| 3203 | |
| 3204 | /* We are now (hopefully) stopped at the first instruction of |
| 3205 | the target process. This assumes that the target process was |
| 3206 | successfully created. */ |
| 3207 | } |
| 3208 | else if (pid != 0) |
| 3209 | { |
| 3210 | if (attach_inferior (pid) == -1) |
| 3211 | error ("Attaching not supported on this target"); |
| 3212 | |
| 3213 | /* Otherwise succeeded. */ |
| 3214 | } |
| 3215 | else |
| 3216 | { |
| 3217 | last_status.kind = TARGET_WAITKIND_EXITED; |
| 3218 | last_status.value.integer = 0; |
| 3219 | last_ptid = minus_one_ptid; |
| 3220 | } |
| 3221 | make_cleanup (detach_or_kill_for_exit_cleanup, NULL); |
| 3222 | |
| 3223 | initialize_notif (); |
| 3224 | |
| 3225 | /* Don't report shared library events on the initial connection, |
| 3226 | even if some libraries are preloaded. Avoids the "stopped by |
| 3227 | shared library event" notice on gdb side. */ |
| 3228 | dlls_changed = 0; |
| 3229 | |
| 3230 | if (last_status.kind == TARGET_WAITKIND_EXITED |
| 3231 | || last_status.kind == TARGET_WAITKIND_SIGNALLED) |
| 3232 | was_running = 0; |
| 3233 | else |
| 3234 | was_running = 1; |
| 3235 | |
| 3236 | if (!was_running && !multi_mode) |
| 3237 | error ("No program to debug"); |
| 3238 | |
| 3239 | while (1) |
| 3240 | { |
| 3241 | volatile struct gdb_exception exception; |
| 3242 | |
| 3243 | noack_mode = 0; |
| 3244 | multi_process = 0; |
| 3245 | /* Be sure we're out of tfind mode. */ |
| 3246 | current_traceframe = -1; |
| 3247 | cont_thread = null_ptid; |
| 3248 | |
| 3249 | remote_open (port); |
| 3250 | |
| 3251 | TRY_CATCH (exception, RETURN_MASK_ERROR) |
| 3252 | { |
| 3253 | /* Wait for events. This will return when all event sources |
| 3254 | are removed from the event loop. */ |
| 3255 | start_event_loop (); |
| 3256 | |
| 3257 | /* If an exit was requested (using the "monitor exit" |
| 3258 | command), terminate now. The only other way to get |
| 3259 | here is for getpkt to fail; close the connection |
| 3260 | and reopen it at the top of the loop. */ |
| 3261 | |
| 3262 | if (exit_requested || run_once) |
| 3263 | throw_quit ("Quit"); |
| 3264 | |
| 3265 | fprintf (stderr, |
| 3266 | "Remote side has terminated connection. " |
| 3267 | "GDBserver will reopen the connection.\n"); |
| 3268 | |
| 3269 | /* Get rid of any pending statuses. An eventual reconnection |
| 3270 | (by the same GDB instance or another) will refresh all its |
| 3271 | state from scratch. */ |
| 3272 | discard_queued_stop_replies (-1); |
| 3273 | for_each_inferior (&all_threads, |
| 3274 | clear_pending_status_callback); |
| 3275 | |
| 3276 | if (tracing) |
| 3277 | { |
| 3278 | if (disconnected_tracing) |
| 3279 | { |
| 3280 | /* Try to enable non-stop/async mode, so we we can |
| 3281 | both wait for an async socket accept, and handle |
| 3282 | async target events simultaneously. There's also |
| 3283 | no point either in having the target always stop |
| 3284 | all threads, when we're going to pass signals |
| 3285 | down without informing GDB. */ |
| 3286 | if (!non_stop) |
| 3287 | { |
| 3288 | if (start_non_stop (1)) |
| 3289 | non_stop = 1; |
| 3290 | |
| 3291 | /* Detaching implicitly resumes all threads; |
| 3292 | simply disconnecting does not. */ |
| 3293 | } |
| 3294 | } |
| 3295 | else |
| 3296 | { |
| 3297 | fprintf (stderr, |
| 3298 | "Disconnected tracing disabled; " |
| 3299 | "stopping trace run.\n"); |
| 3300 | stop_tracing (); |
| 3301 | } |
| 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | if (exception.reason == RETURN_ERROR) |
| 3306 | { |
| 3307 | if (response_needed) |
| 3308 | { |
| 3309 | write_enn (own_buf); |
| 3310 | putpkt (own_buf); |
| 3311 | } |
| 3312 | } |
| 3313 | } |
| 3314 | } |
| 3315 | |
| 3316 | /* Main function. */ |
| 3317 | |
| 3318 | int |
| 3319 | main (int argc, char *argv[]) |
| 3320 | { |
| 3321 | volatile struct gdb_exception exception; |
| 3322 | |
| 3323 | TRY_CATCH (exception, RETURN_MASK_ALL) |
| 3324 | { |
| 3325 | captured_main (argc, argv); |
| 3326 | } |
| 3327 | |
| 3328 | /* captured_main should never return. */ |
| 3329 | gdb_assert (exception.reason < 0); |
| 3330 | |
| 3331 | if (exception.reason == RETURN_ERROR) |
| 3332 | { |
| 3333 | fflush (stdout); |
| 3334 | fprintf (stderr, "%s\n", exception.message); |
| 3335 | fprintf (stderr, "Exiting\n"); |
| 3336 | exit_code = 1; |
| 3337 | } |
| 3338 | |
| 3339 | exit (exit_code); |
| 3340 | } |
| 3341 | |
| 3342 | /* Skip PACKET until the next semi-colon (or end of string). */ |
| 3343 | |
| 3344 | static void |
| 3345 | skip_to_semicolon (char **packet) |
| 3346 | { |
| 3347 | while (**packet != '\0' && **packet != ';') |
| 3348 | (*packet)++; |
| 3349 | } |
| 3350 | |
| 3351 | /* Process options coming from Z packets for a breakpoint. PACKET is |
| 3352 | the packet buffer. *PACKET is updated to point to the first char |
| 3353 | after the last processed option. */ |
| 3354 | |
| 3355 | static void |
| 3356 | process_point_options (struct breakpoint *bp, char **packet) |
| 3357 | { |
| 3358 | char *dataptr = *packet; |
| 3359 | int persist; |
| 3360 | |
| 3361 | /* Check if data has the correct format. */ |
| 3362 | if (*dataptr != ';') |
| 3363 | return; |
| 3364 | |
| 3365 | dataptr++; |
| 3366 | |
| 3367 | while (*dataptr) |
| 3368 | { |
| 3369 | if (*dataptr == ';') |
| 3370 | ++dataptr; |
| 3371 | |
| 3372 | if (*dataptr == 'X') |
| 3373 | { |
| 3374 | /* Conditional expression. */ |
| 3375 | if (debug_threads) |
| 3376 | debug_printf ("Found breakpoint condition.\n"); |
| 3377 | if (!add_breakpoint_condition (bp, &dataptr)) |
| 3378 | skip_to_semicolon (&dataptr); |
| 3379 | } |
| 3380 | else if (strncmp (dataptr, "cmds:", strlen ("cmds:")) == 0) |
| 3381 | { |
| 3382 | dataptr += strlen ("cmds:"); |
| 3383 | if (debug_threads) |
| 3384 | debug_printf ("Found breakpoint commands %s.\n", dataptr); |
| 3385 | persist = (*dataptr == '1'); |
| 3386 | dataptr += 2; |
| 3387 | if (add_breakpoint_commands (bp, &dataptr, persist)) |
| 3388 | skip_to_semicolon (&dataptr); |
| 3389 | } |
| 3390 | else |
| 3391 | { |
| 3392 | fprintf (stderr, "Unknown token %c, ignoring.\n", |
| 3393 | *dataptr); |
| 3394 | /* Skip tokens until we find one that we recognize. */ |
| 3395 | skip_to_semicolon (&dataptr); |
| 3396 | } |
| 3397 | } |
| 3398 | *packet = dataptr; |
| 3399 | } |
| 3400 | |
| 3401 | /* Event loop callback that handles a serial event. The first byte in |
| 3402 | the serial buffer gets us here. We expect characters to arrive at |
| 3403 | a brisk pace, so we read the rest of the packet with a blocking |
| 3404 | getpkt call. */ |
| 3405 | |
| 3406 | static int |
| 3407 | process_serial_event (void) |
| 3408 | { |
| 3409 | char ch; |
| 3410 | int i = 0; |
| 3411 | int signal; |
| 3412 | unsigned int len; |
| 3413 | int res; |
| 3414 | CORE_ADDR mem_addr; |
| 3415 | int pid; |
| 3416 | unsigned char sig; |
| 3417 | int packet_len; |
| 3418 | int new_packet_len = -1; |
| 3419 | |
| 3420 | /* Used to decide when gdbserver should exit in |
| 3421 | multi-mode/remote. */ |
| 3422 | static int have_ran = 0; |
| 3423 | |
| 3424 | if (!have_ran) |
| 3425 | have_ran = target_running (); |
| 3426 | |
| 3427 | disable_async_io (); |
| 3428 | |
| 3429 | response_needed = 0; |
| 3430 | packet_len = getpkt (own_buf); |
| 3431 | if (packet_len <= 0) |
| 3432 | { |
| 3433 | remote_close (); |
| 3434 | /* Force an event loop break. */ |
| 3435 | return -1; |
| 3436 | } |
| 3437 | response_needed = 1; |
| 3438 | |
| 3439 | i = 0; |
| 3440 | ch = own_buf[i++]; |
| 3441 | switch (ch) |
| 3442 | { |
| 3443 | case 'q': |
| 3444 | handle_query (own_buf, packet_len, &new_packet_len); |
| 3445 | break; |
| 3446 | case 'Q': |
| 3447 | handle_general_set (own_buf); |
| 3448 | break; |
| 3449 | case 'D': |
| 3450 | require_running (own_buf); |
| 3451 | |
| 3452 | if (multi_process) |
| 3453 | { |
| 3454 | i++; /* skip ';' */ |
| 3455 | pid = strtol (&own_buf[i], NULL, 16); |
| 3456 | } |
| 3457 | else |
| 3458 | pid = ptid_get_pid (current_ptid); |
| 3459 | |
| 3460 | if ((tracing && disconnected_tracing) || any_persistent_commands ()) |
| 3461 | { |
| 3462 | struct thread_resume resume_info; |
| 3463 | struct process_info *process = find_process_pid (pid); |
| 3464 | |
| 3465 | if (process == NULL) |
| 3466 | { |
| 3467 | write_enn (own_buf); |
| 3468 | break; |
| 3469 | } |
| 3470 | |
| 3471 | if (tracing && disconnected_tracing) |
| 3472 | fprintf (stderr, |
| 3473 | "Disconnected tracing in effect, " |
| 3474 | "leaving gdbserver attached to the process\n"); |
| 3475 | |
| 3476 | if (any_persistent_commands ()) |
| 3477 | fprintf (stderr, |
| 3478 | "Persistent commands are present, " |
| 3479 | "leaving gdbserver attached to the process\n"); |
| 3480 | |
| 3481 | /* Make sure we're in non-stop/async mode, so we we can both |
| 3482 | wait for an async socket accept, and handle async target |
| 3483 | events simultaneously. There's also no point either in |
| 3484 | having the target stop all threads, when we're going to |
| 3485 | pass signals down without informing GDB. */ |
| 3486 | if (!non_stop) |
| 3487 | { |
| 3488 | if (debug_threads) |
| 3489 | debug_printf ("Forcing non-stop mode\n"); |
| 3490 | |
| 3491 | non_stop = 1; |
| 3492 | start_non_stop (1); |
| 3493 | } |
| 3494 | |
| 3495 | process->gdb_detached = 1; |
| 3496 | |
| 3497 | /* Detaching implicitly resumes all threads. */ |
| 3498 | resume_info.thread = minus_one_ptid; |
| 3499 | resume_info.kind = resume_continue; |
| 3500 | resume_info.sig = 0; |
| 3501 | (*the_target->resume) (&resume_info, 1); |
| 3502 | |
| 3503 | write_ok (own_buf); |
| 3504 | break; /* from switch/case */ |
| 3505 | } |
| 3506 | |
| 3507 | fprintf (stderr, "Detaching from process %d\n", pid); |
| 3508 | stop_tracing (); |
| 3509 | if (detach_inferior (pid) != 0) |
| 3510 | write_enn (own_buf); |
| 3511 | else |
| 3512 | { |
| 3513 | discard_queued_stop_replies (pid); |
| 3514 | write_ok (own_buf); |
| 3515 | |
| 3516 | if (extended_protocol) |
| 3517 | { |
| 3518 | /* Treat this like a normal program exit. */ |
| 3519 | last_status.kind = TARGET_WAITKIND_EXITED; |
| 3520 | last_status.value.integer = 0; |
| 3521 | last_ptid = pid_to_ptid (pid); |
| 3522 | |
| 3523 | current_thread = NULL; |
| 3524 | } |
| 3525 | else |
| 3526 | { |
| 3527 | putpkt (own_buf); |
| 3528 | remote_close (); |
| 3529 | |
| 3530 | /* If we are attached, then we can exit. Otherwise, we |
| 3531 | need to hang around doing nothing, until the child is |
| 3532 | gone. */ |
| 3533 | join_inferior (pid); |
| 3534 | exit (0); |
| 3535 | } |
| 3536 | } |
| 3537 | break; |
| 3538 | case '!': |
| 3539 | extended_protocol = 1; |
| 3540 | write_ok (own_buf); |
| 3541 | break; |
| 3542 | case '?': |
| 3543 | handle_status (own_buf); |
| 3544 | break; |
| 3545 | case 'H': |
| 3546 | if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') |
| 3547 | { |
| 3548 | ptid_t gdb_id, thread_id; |
| 3549 | int pid; |
| 3550 | |
| 3551 | require_running (own_buf); |
| 3552 | |
| 3553 | gdb_id = read_ptid (&own_buf[2], NULL); |
| 3554 | |
| 3555 | pid = ptid_get_pid (gdb_id); |
| 3556 | |
| 3557 | if (ptid_equal (gdb_id, null_ptid) |
| 3558 | || ptid_equal (gdb_id, minus_one_ptid)) |
| 3559 | thread_id = null_ptid; |
| 3560 | else if (pid != 0 |
| 3561 | && ptid_equal (pid_to_ptid (pid), |
| 3562 | gdb_id)) |
| 3563 | { |
| 3564 | struct thread_info *thread = |
| 3565 | (struct thread_info *) find_inferior (&all_threads, |
| 3566 | first_thread_of, |
| 3567 | &pid); |
| 3568 | if (!thread) |
| 3569 | { |
| 3570 | write_enn (own_buf); |
| 3571 | break; |
| 3572 | } |
| 3573 | |
| 3574 | thread_id = thread->entry.id; |
| 3575 | } |
| 3576 | else |
| 3577 | { |
| 3578 | thread_id = gdb_id_to_thread_id (gdb_id); |
| 3579 | if (ptid_equal (thread_id, null_ptid)) |
| 3580 | { |
| 3581 | write_enn (own_buf); |
| 3582 | break; |
| 3583 | } |
| 3584 | } |
| 3585 | |
| 3586 | if (own_buf[1] == 'g') |
| 3587 | { |
| 3588 | if (ptid_equal (thread_id, null_ptid)) |
| 3589 | { |
| 3590 | /* GDB is telling us to choose any thread. Check if |
| 3591 | the currently selected thread is still valid. If |
| 3592 | it is not, select the first available. */ |
| 3593 | struct thread_info *thread = |
| 3594 | (struct thread_info *) find_inferior_id (&all_threads, |
| 3595 | general_thread); |
| 3596 | if (thread == NULL) |
| 3597 | { |
| 3598 | thread = get_first_thread (); |
| 3599 | thread_id = thread->entry.id; |
| 3600 | } |
| 3601 | } |
| 3602 | |
| 3603 | general_thread = thread_id; |
| 3604 | set_desired_thread (1); |
| 3605 | } |
| 3606 | else if (own_buf[1] == 'c') |
| 3607 | cont_thread = thread_id; |
| 3608 | |
| 3609 | write_ok (own_buf); |
| 3610 | } |
| 3611 | else |
| 3612 | { |
| 3613 | /* Silently ignore it so that gdb can extend the protocol |
| 3614 | without compatibility headaches. */ |
| 3615 | own_buf[0] = '\0'; |
| 3616 | } |
| 3617 | break; |
| 3618 | case 'g': |
| 3619 | require_running (own_buf); |
| 3620 | if (current_traceframe >= 0) |
| 3621 | { |
| 3622 | struct regcache *regcache |
| 3623 | = new_register_cache (current_target_desc ()); |
| 3624 | |
| 3625 | if (fetch_traceframe_registers (current_traceframe, |
| 3626 | regcache, -1) == 0) |
| 3627 | registers_to_string (regcache, own_buf); |
| 3628 | else |
| 3629 | write_enn (own_buf); |
| 3630 | free_register_cache (regcache); |
| 3631 | } |
| 3632 | else |
| 3633 | { |
| 3634 | struct regcache *regcache; |
| 3635 | |
| 3636 | set_desired_thread (1); |
| 3637 | regcache = get_thread_regcache (current_thread, 1); |
| 3638 | registers_to_string (regcache, own_buf); |
| 3639 | } |
| 3640 | break; |
| 3641 | case 'G': |
| 3642 | require_running (own_buf); |
| 3643 | if (current_traceframe >= 0) |
| 3644 | write_enn (own_buf); |
| 3645 | else |
| 3646 | { |
| 3647 | struct regcache *regcache; |
| 3648 | |
| 3649 | set_desired_thread (1); |
| 3650 | regcache = get_thread_regcache (current_thread, 1); |
| 3651 | registers_from_string (regcache, &own_buf[1]); |
| 3652 | write_ok (own_buf); |
| 3653 | } |
| 3654 | break; |
| 3655 | case 'm': |
| 3656 | require_running (own_buf); |
| 3657 | decode_m_packet (&own_buf[1], &mem_addr, &len); |
| 3658 | res = gdb_read_memory (mem_addr, mem_buf, len); |
| 3659 | if (res < 0) |
| 3660 | write_enn (own_buf); |
| 3661 | else |
| 3662 | bin2hex (mem_buf, own_buf, res); |
| 3663 | break; |
| 3664 | case 'M': |
| 3665 | require_running (own_buf); |
| 3666 | decode_M_packet (&own_buf[1], &mem_addr, &len, &mem_buf); |
| 3667 | if (gdb_write_memory (mem_addr, mem_buf, len) == 0) |
| 3668 | write_ok (own_buf); |
| 3669 | else |
| 3670 | write_enn (own_buf); |
| 3671 | break; |
| 3672 | case 'X': |
| 3673 | require_running (own_buf); |
| 3674 | if (decode_X_packet (&own_buf[1], packet_len - 1, |
| 3675 | &mem_addr, &len, &mem_buf) < 0 |
| 3676 | || gdb_write_memory (mem_addr, mem_buf, len) != 0) |
| 3677 | write_enn (own_buf); |
| 3678 | else |
| 3679 | write_ok (own_buf); |
| 3680 | break; |
| 3681 | case 'C': |
| 3682 | require_running (own_buf); |
| 3683 | hex2bin (own_buf + 1, &sig, 1); |
| 3684 | if (gdb_signal_to_host_p (sig)) |
| 3685 | signal = gdb_signal_to_host (sig); |
| 3686 | else |
| 3687 | signal = 0; |
| 3688 | myresume (own_buf, 0, signal); |
| 3689 | break; |
| 3690 | case 'S': |
| 3691 | require_running (own_buf); |
| 3692 | hex2bin (own_buf + 1, &sig, 1); |
| 3693 | if (gdb_signal_to_host_p (sig)) |
| 3694 | signal = gdb_signal_to_host (sig); |
| 3695 | else |
| 3696 | signal = 0; |
| 3697 | myresume (own_buf, 1, signal); |
| 3698 | break; |
| 3699 | case 'c': |
| 3700 | require_running (own_buf); |
| 3701 | signal = 0; |
| 3702 | myresume (own_buf, 0, signal); |
| 3703 | break; |
| 3704 | case 's': |
| 3705 | require_running (own_buf); |
| 3706 | signal = 0; |
| 3707 | myresume (own_buf, 1, signal); |
| 3708 | break; |
| 3709 | case 'Z': /* insert_ ... */ |
| 3710 | /* Fallthrough. */ |
| 3711 | case 'z': /* remove_ ... */ |
| 3712 | { |
| 3713 | char *dataptr; |
| 3714 | ULONGEST addr; |
| 3715 | int len; |
| 3716 | char type = own_buf[1]; |
| 3717 | int res; |
| 3718 | const int insert = ch == 'Z'; |
| 3719 | char *p = &own_buf[3]; |
| 3720 | |
| 3721 | p = unpack_varlen_hex (p, &addr); |
| 3722 | len = strtol (p + 1, &dataptr, 16); |
| 3723 | |
| 3724 | if (insert) |
| 3725 | { |
| 3726 | struct breakpoint *bp; |
| 3727 | |
| 3728 | bp = set_gdb_breakpoint (type, addr, len, &res); |
| 3729 | if (bp != NULL) |
| 3730 | { |
| 3731 | res = 0; |
| 3732 | |
| 3733 | /* GDB may have sent us a list of *point parameters to |
| 3734 | be evaluated on the target's side. Read such list |
| 3735 | here. If we already have a list of parameters, GDB |
| 3736 | is telling us to drop that list and use this one |
| 3737 | instead. */ |
| 3738 | clear_breakpoint_conditions_and_commands (bp); |
| 3739 | process_point_options (bp, &dataptr); |
| 3740 | } |
| 3741 | } |
| 3742 | else |
| 3743 | res = delete_gdb_breakpoint (type, addr, len); |
| 3744 | |
| 3745 | if (res == 0) |
| 3746 | write_ok (own_buf); |
| 3747 | else if (res == 1) |
| 3748 | /* Unsupported. */ |
| 3749 | own_buf[0] = '\0'; |
| 3750 | else |
| 3751 | write_enn (own_buf); |
| 3752 | break; |
| 3753 | } |
| 3754 | case 'k': |
| 3755 | response_needed = 0; |
| 3756 | if (!target_running ()) |
| 3757 | /* The packet we received doesn't make sense - but we can't |
| 3758 | reply to it, either. */ |
| 3759 | return 0; |
| 3760 | |
| 3761 | fprintf (stderr, "Killing all inferiors\n"); |
| 3762 | for_each_inferior (&all_processes, kill_inferior_callback); |
| 3763 | |
| 3764 | /* When using the extended protocol, we wait with no program |
| 3765 | running. The traditional protocol will exit instead. */ |
| 3766 | if (extended_protocol) |
| 3767 | { |
| 3768 | last_status.kind = TARGET_WAITKIND_EXITED; |
| 3769 | last_status.value.sig = GDB_SIGNAL_KILL; |
| 3770 | return 0; |
| 3771 | } |
| 3772 | else |
| 3773 | exit (0); |
| 3774 | |
| 3775 | case 'T': |
| 3776 | { |
| 3777 | ptid_t gdb_id, thread_id; |
| 3778 | |
| 3779 | require_running (own_buf); |
| 3780 | |
| 3781 | gdb_id = read_ptid (&own_buf[1], NULL); |
| 3782 | thread_id = gdb_id_to_thread_id (gdb_id); |
| 3783 | if (ptid_equal (thread_id, null_ptid)) |
| 3784 | { |
| 3785 | write_enn (own_buf); |
| 3786 | break; |
| 3787 | } |
| 3788 | |
| 3789 | if (mythread_alive (thread_id)) |
| 3790 | write_ok (own_buf); |
| 3791 | else |
| 3792 | write_enn (own_buf); |
| 3793 | } |
| 3794 | break; |
| 3795 | case 'R': |
| 3796 | response_needed = 0; |
| 3797 | |
| 3798 | /* Restarting the inferior is only supported in the extended |
| 3799 | protocol. */ |
| 3800 | if (extended_protocol) |
| 3801 | { |
| 3802 | if (target_running ()) |
| 3803 | for_each_inferior (&all_processes, |
| 3804 | kill_inferior_callback); |
| 3805 | fprintf (stderr, "GDBserver restarting\n"); |
| 3806 | |
| 3807 | /* Wait till we are at 1st instruction in prog. */ |
| 3808 | if (program_argv != NULL) |
| 3809 | start_inferior (program_argv); |
| 3810 | else |
| 3811 | { |
| 3812 | last_status.kind = TARGET_WAITKIND_EXITED; |
| 3813 | last_status.value.sig = GDB_SIGNAL_KILL; |
| 3814 | } |
| 3815 | return 0; |
| 3816 | } |
| 3817 | else |
| 3818 | { |
| 3819 | /* It is a request we don't understand. Respond with an |
| 3820 | empty packet so that gdb knows that we don't support this |
| 3821 | request. */ |
| 3822 | own_buf[0] = '\0'; |
| 3823 | break; |
| 3824 | } |
| 3825 | case 'v': |
| 3826 | /* Extended (long) request. */ |
| 3827 | handle_v_requests (own_buf, packet_len, &new_packet_len); |
| 3828 | break; |
| 3829 | |
| 3830 | default: |
| 3831 | /* It is a request we don't understand. Respond with an empty |
| 3832 | packet so that gdb knows that we don't support this |
| 3833 | request. */ |
| 3834 | own_buf[0] = '\0'; |
| 3835 | break; |
| 3836 | } |
| 3837 | |
| 3838 | if (new_packet_len != -1) |
| 3839 | putpkt_binary (own_buf, new_packet_len); |
| 3840 | else |
| 3841 | putpkt (own_buf); |
| 3842 | |
| 3843 | response_needed = 0; |
| 3844 | |
| 3845 | if (!extended_protocol && have_ran && !target_running ()) |
| 3846 | { |
| 3847 | /* In non-stop, defer exiting until GDB had a chance to query |
| 3848 | the whole vStopped list (until it gets an OK). */ |
| 3849 | if (QUEUE_is_empty (notif_event_p, notif_stop.queue)) |
| 3850 | { |
| 3851 | /* Be transparent when GDB is connected through stdio -- no |
| 3852 | need to spam GDB's console. */ |
| 3853 | if (!remote_connection_is_stdio ()) |
| 3854 | fprintf (stderr, "GDBserver exiting\n"); |
| 3855 | remote_close (); |
| 3856 | exit (0); |
| 3857 | } |
| 3858 | } |
| 3859 | |
| 3860 | if (exit_requested) |
| 3861 | return -1; |
| 3862 | |
| 3863 | return 0; |
| 3864 | } |
| 3865 | |
| 3866 | /* Event-loop callback for serial events. */ |
| 3867 | |
| 3868 | int |
| 3869 | handle_serial_event (int err, gdb_client_data client_data) |
| 3870 | { |
| 3871 | if (debug_threads) |
| 3872 | debug_printf ("handling possible serial event\n"); |
| 3873 | |
| 3874 | /* Really handle it. */ |
| 3875 | if (process_serial_event () < 0) |
| 3876 | return -1; |
| 3877 | |
| 3878 | /* Be sure to not change the selected thread behind GDB's back. |
| 3879 | Important in the non-stop mode asynchronous protocol. */ |
| 3880 | set_desired_thread (1); |
| 3881 | |
| 3882 | return 0; |
| 3883 | } |
| 3884 | |
| 3885 | /* Event-loop callback for target events. */ |
| 3886 | |
| 3887 | int |
| 3888 | handle_target_event (int err, gdb_client_data client_data) |
| 3889 | { |
| 3890 | if (debug_threads) |
| 3891 | debug_printf ("handling possible target event\n"); |
| 3892 | |
| 3893 | last_ptid = mywait (minus_one_ptid, &last_status, |
| 3894 | TARGET_WNOHANG, 1); |
| 3895 | |
| 3896 | if (last_status.kind == TARGET_WAITKIND_NO_RESUMED) |
| 3897 | { |
| 3898 | /* No RSP support for this yet. */ |
| 3899 | } |
| 3900 | else if (last_status.kind != TARGET_WAITKIND_IGNORE) |
| 3901 | { |
| 3902 | int pid = ptid_get_pid (last_ptid); |
| 3903 | struct process_info *process = find_process_pid (pid); |
| 3904 | int forward_event = !gdb_connected () || process->gdb_detached; |
| 3905 | |
| 3906 | if (last_status.kind == TARGET_WAITKIND_EXITED |
| 3907 | || last_status.kind == TARGET_WAITKIND_SIGNALLED) |
| 3908 | { |
| 3909 | mark_breakpoints_out (process); |
| 3910 | mourn_inferior (process); |
| 3911 | } |
| 3912 | else |
| 3913 | { |
| 3914 | /* We're reporting this thread as stopped. Update its |
| 3915 | "want-stopped" state to what the client wants, until it |
| 3916 | gets a new resume action. */ |
| 3917 | current_thread->last_resume_kind = resume_stop; |
| 3918 | current_thread->last_status = last_status; |
| 3919 | } |
| 3920 | |
| 3921 | if (forward_event) |
| 3922 | { |
| 3923 | if (!target_running ()) |
| 3924 | { |
| 3925 | /* The last process exited. We're done. */ |
| 3926 | exit (0); |
| 3927 | } |
| 3928 | |
| 3929 | if (last_status.kind == TARGET_WAITKIND_STOPPED) |
| 3930 | { |
| 3931 | /* A thread stopped with a signal, but gdb isn't |
| 3932 | connected to handle it. Pass it down to the |
| 3933 | inferior, as if it wasn't being traced. */ |
| 3934 | struct thread_resume resume_info; |
| 3935 | |
| 3936 | if (debug_threads) |
| 3937 | debug_printf ("GDB not connected; forwarding event %d for" |
| 3938 | " [%s]\n", |
| 3939 | (int) last_status.kind, |
| 3940 | target_pid_to_str (last_ptid)); |
| 3941 | |
| 3942 | resume_info.thread = last_ptid; |
| 3943 | resume_info.kind = resume_continue; |
| 3944 | resume_info.sig = gdb_signal_to_host (last_status.value.sig); |
| 3945 | (*the_target->resume) (&resume_info, 1); |
| 3946 | } |
| 3947 | else if (debug_threads) |
| 3948 | debug_printf ("GDB not connected; ignoring event %d for [%s]\n", |
| 3949 | (int) last_status.kind, |
| 3950 | target_pid_to_str (last_ptid)); |
| 3951 | } |
| 3952 | else |
| 3953 | { |
| 3954 | struct vstop_notif *vstop_notif |
| 3955 | = xmalloc (sizeof (struct vstop_notif)); |
| 3956 | |
| 3957 | vstop_notif->status = last_status; |
| 3958 | vstop_notif->ptid = last_ptid; |
| 3959 | /* Push Stop notification. */ |
| 3960 | notif_push (¬if_stop, |
| 3961 | (struct notif_event *) vstop_notif); |
| 3962 | } |
| 3963 | } |
| 3964 | |
| 3965 | /* Be sure to not change the selected thread behind GDB's back. |
| 3966 | Important in the non-stop mode asynchronous protocol. */ |
| 3967 | set_desired_thread (1); |
| 3968 | |
| 3969 | return 0; |
| 3970 | } |