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