1 /* MI Interpreter Definitions and Commands for GDB, the GNU debugger. 2 3 Copyright (C) 2002-2020 Free Software Foundation, Inc. 4 5 This file is part of GDB. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20 #include "defs.h" 21 22 #include "mi-interp.h" 23 24 #include "interps.h" 25 #include "event-top.h" 26 #include "gdbsupport/event-loop.h" 27 #include "inferior.h" 28 #include "infrun.h" 29 #include "ui-out.h" 30 #include "top.h" 31 #include "mi-main.h" 32 #include "mi-cmds.h" 33 #include "mi-out.h" 34 #include "mi-console.h" 35 #include "mi-common.h" 36 #include "observable.h" 37 #include "gdbthread.h" 38 #include "solist.h" 39 #include "objfiles.h" 40 #include "tracepoint.h" 41 #include "cli-out.h" 42 #include "thread-fsm.h" 43 #include "cli/cli-interp.h" 44 #include "gdbsupport/scope-exit.h" 45 46 /* These are the interpreter setup, etc. functions for the MI 47 interpreter. */ 48 49 static void mi_execute_command_wrapper (const char *cmd); 50 static void mi_execute_command_input_handler 51 (gdb::unique_xmalloc_ptr<char> &&cmd); 52 53 /* These are hooks that we put in place while doing interpreter_exec 54 so we can report interesting things that happened "behind the MI's 55 back" in this command. */ 56 57 static int mi_interp_query_hook (const char *ctlstr, va_list ap) 58 ATTRIBUTE_PRINTF (1, 0); 59 60 static void mi_insert_notify_hooks (void); 61 static void mi_remove_notify_hooks (void); 62 63 static void mi_on_signal_received (enum gdb_signal siggnal); 64 static void mi_on_end_stepping_range (void); 65 static void mi_on_signal_exited (enum gdb_signal siggnal); 66 static void mi_on_exited (int exitstatus); 67 static void mi_on_normal_stop (struct bpstats *bs, int print_frame); 68 static void mi_on_no_history (void); 69 70 static void mi_new_thread (struct thread_info *t); 71 static void mi_thread_exit (struct thread_info *t, int silent); 72 static void mi_record_changed (struct inferior*, int, const char *, 73 const char *); 74 static void mi_inferior_added (struct inferior *inf); 75 static void mi_inferior_appeared (struct inferior *inf); 76 static void mi_inferior_exit (struct inferior *inf); 77 static void mi_inferior_removed (struct inferior *inf); 78 static void mi_on_resume (ptid_t ptid); 79 static void mi_solib_loaded (struct so_list *solib); 80 static void mi_solib_unloaded (struct so_list *solib); 81 static void mi_about_to_proceed (void); 82 static void mi_traceframe_changed (int tfnum, int tpnum); 83 static void mi_tsv_created (const struct trace_state_variable *tsv); 84 static void mi_tsv_deleted (const struct trace_state_variable *tsv); 85 static void mi_tsv_modified (const struct trace_state_variable *tsv); 86 static void mi_breakpoint_created (struct breakpoint *b); 87 static void mi_breakpoint_deleted (struct breakpoint *b); 88 static void mi_breakpoint_modified (struct breakpoint *b); 89 static void mi_command_param_changed (const char *param, const char *value); 90 static void mi_memory_changed (struct inferior *inf, CORE_ADDR memaddr, 91 ssize_t len, const bfd_byte *myaddr); 92 static void mi_on_sync_execution_done (void); 93 94 /* Display the MI prompt. */ 95 96 static void 97 display_mi_prompt (struct mi_interp *mi) 98 { 99 struct ui *ui = current_ui; 100 101 fputs_unfiltered ("(gdb) \n", mi->raw_stdout); 102 gdb_flush (mi->raw_stdout); 103 ui->prompt_state = PROMPTED; 104 } 105 106 /* Returns the INTERP's data cast as mi_interp if INTERP is an MI, and 107 returns NULL otherwise. */ 108 109 static struct mi_interp * 110 as_mi_interp (struct interp *interp) 111 { 112 return dynamic_cast<mi_interp *> (interp); 113 } 114 115 void 116 mi_interp::init (bool top_level) 117 { 118 mi_interp *mi = this; 119 120 /* Store the current output channel, so that we can create a console 121 channel that encapsulates and prefixes all gdb_output-type bits 122 coming from the rest of the debugger. */ 123 mi->raw_stdout = gdb_stdout; 124 125 /* Create MI console channels, each with a different prefix so they 126 can be distinguished. */ 127 mi->out = new mi_console_file (mi->raw_stdout, "~", '"'); 128 mi->err = new mi_console_file (mi->raw_stdout, "&", '"'); 129 mi->log = mi->err; 130 mi->targ = new mi_console_file (mi->raw_stdout, "@", '"'); 131 mi->event_channel = new mi_console_file (mi->raw_stdout, "=", 0); 132 mi->mi_uiout = mi_out_new (name ()); 133 gdb_assert (mi->mi_uiout != nullptr); 134 mi->cli_uiout = cli_out_new (mi->out); 135 136 if (top_level) 137 { 138 /* The initial inferior is created before this function is called, so we 139 need to report it explicitly when initializing the top-level MI 140 interpreter. 141 142 This is also called when additional MI interpreters are added (using 143 the new-ui command), when multiple inferiors possibly exist, so we need 144 to use iteration to report all the inferiors. mi_inferior_added can't 145 be used, because it would print the event on all the other MI UIs. */ 146 147 for (inferior *inf : all_inferiors ()) 148 { 149 target_terminal::scoped_restore_terminal_state term_state; 150 target_terminal::ours_for_output (); 151 152 fprintf_unfiltered (mi->event_channel, 153 "thread-group-added,id=\"i%d\"", 154 inf->num); 155 156 gdb_flush (mi->event_channel); 157 } 158 } 159 } 160 161 void 162 mi_interp::resume () 163 { 164 struct mi_interp *mi = this; 165 struct ui *ui = current_ui; 166 167 /* As per hack note in mi_interpreter_init, swap in the output 168 channels... */ 169 gdb_setup_readline (0); 170 171 ui->call_readline = gdb_readline_no_editing_callback; 172 ui->input_handler = mi_execute_command_input_handler; 173 174 gdb_stdout = mi->out; 175 /* Route error and log output through the MI. */ 176 gdb_stderr = mi->err; 177 gdb_stdlog = mi->log; 178 /* Route target output through the MI. */ 179 gdb_stdtarg = mi->targ; 180 /* Route target error through the MI as well. */ 181 gdb_stdtargerr = mi->targ; 182 183 /* Replace all the hooks that we know about. There really needs to 184 be a better way of doing this... */ 185 clear_interpreter_hooks (); 186 187 deprecated_show_load_progress = mi_load_progress; 188 } 189 190 void 191 mi_interp::suspend () 192 { 193 gdb_disable_readline (); 194 } 195 196 gdb_exception 197 mi_interp::exec (const char *command) 198 { 199 mi_execute_command_wrapper (command); 200 return gdb_exception (); 201 } 202 203 void 204 mi_cmd_interpreter_exec (const char *command, char **argv, int argc) 205 { 206 struct interp *interp_to_use; 207 int i; 208 209 if (argc < 2) 210 error (_("-interpreter-exec: " 211 "Usage: -interpreter-exec interp command")); 212 213 interp_to_use = interp_lookup (current_ui, argv[0]); 214 if (interp_to_use == NULL) 215 error (_("-interpreter-exec: could not find interpreter \"%s\""), 216 argv[0]); 217 218 /* Note that unlike the CLI version of this command, we don't 219 actually set INTERP_TO_USE as the current interpreter, as we 220 still want gdb_stdout, etc. to point at MI streams. */ 221 222 /* Insert the MI out hooks, making sure to also call the 223 interpreter's hooks if it has any. */ 224 /* KRS: We shouldn't need this... Events should be installed and 225 they should just ALWAYS fire something out down the MI 226 channel. */ 227 mi_insert_notify_hooks (); 228 229 /* Now run the code. */ 230 231 SCOPE_EXIT 232 { 233 mi_remove_notify_hooks (); 234 }; 235 236 for (i = 1; i < argc; i++) 237 { 238 struct gdb_exception e = interp_exec (interp_to_use, argv[i]); 239 240 if (e.reason < 0) 241 error ("%s", e.what ()); 242 } 243 } 244 245 /* This inserts a number of hooks that are meant to produce 246 async-notify ("=") MI messages while running commands in another 247 interpreter using mi_interpreter_exec. The canonical use for this 248 is to allow access to the gdb CLI interpreter from within the MI, 249 while still producing MI style output when actions in the CLI 250 command change GDB's state. */ 251 252 static void 253 mi_insert_notify_hooks (void) 254 { 255 deprecated_query_hook = mi_interp_query_hook; 256 } 257 258 static void 259 mi_remove_notify_hooks (void) 260 { 261 deprecated_query_hook = NULL; 262 } 263 264 static int 265 mi_interp_query_hook (const char *ctlstr, va_list ap) 266 { 267 return 1; 268 } 269 270 static void 271 mi_execute_command_wrapper (const char *cmd) 272 { 273 struct ui *ui = current_ui; 274 275 mi_execute_command (cmd, ui->instream == ui->stdin_stream); 276 } 277 278 /* Observer for the synchronous_command_done notification. */ 279 280 static void 281 mi_on_sync_execution_done (void) 282 { 283 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 284 285 if (mi == NULL) 286 return; 287 288 /* If MI is sync, then output the MI prompt now, indicating we're 289 ready for further input. */ 290 if (!mi_async_p ()) 291 display_mi_prompt (mi); 292 } 293 294 /* mi_execute_command_wrapper wrapper suitable for INPUT_HANDLER. */ 295 296 static void 297 mi_execute_command_input_handler (gdb::unique_xmalloc_ptr<char> &&cmd) 298 { 299 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 300 struct ui *ui = current_ui; 301 302 ui->prompt_state = PROMPT_NEEDED; 303 304 mi_execute_command_wrapper (cmd.get ()); 305 306 /* Print a prompt, indicating we're ready for further input, unless 307 we just started a synchronous command. In that case, we're about 308 to go back to the event loop and will output the prompt in the 309 'synchronous_command_done' observer when the target next 310 stops. */ 311 if (ui->prompt_state == PROMPT_NEEDED) 312 display_mi_prompt (mi); 313 } 314 315 void 316 mi_interp::pre_command_loop () 317 { 318 struct mi_interp *mi = this; 319 320 /* Turn off 8 bit strings in quoted output. Any character with the 321 high bit set is printed using C's octal format. */ 322 sevenbit_strings = 1; 323 324 /* Tell the world that we're alive. */ 325 display_mi_prompt (mi); 326 } 327 328 static void 329 mi_new_thread (struct thread_info *t) 330 { 331 SWITCH_THRU_ALL_UIS () 332 { 333 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 334 335 if (mi == NULL) 336 continue; 337 338 target_terminal::scoped_restore_terminal_state term_state; 339 target_terminal::ours_for_output (); 340 341 fprintf_unfiltered (mi->event_channel, 342 "thread-created,id=\"%d\",group-id=\"i%d\"", 343 t->global_num, t->inf->num); 344 gdb_flush (mi->event_channel); 345 } 346 } 347 348 static void 349 mi_thread_exit (struct thread_info *t, int silent) 350 { 351 if (silent) 352 return; 353 354 SWITCH_THRU_ALL_UIS () 355 { 356 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 357 358 if (mi == NULL) 359 continue; 360 361 target_terminal::scoped_restore_terminal_state term_state; 362 target_terminal::ours_for_output (); 363 fprintf_unfiltered (mi->event_channel, 364 "thread-exited,id=\"%d\",group-id=\"i%d\"", 365 t->global_num, t->inf->num); 366 gdb_flush (mi->event_channel); 367 } 368 } 369 370 /* Emit notification on changing the state of record. */ 371 372 static void 373 mi_record_changed (struct inferior *inferior, int started, const char *method, 374 const char *format) 375 { 376 SWITCH_THRU_ALL_UIS () 377 { 378 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 379 380 if (mi == NULL) 381 continue; 382 383 target_terminal::scoped_restore_terminal_state term_state; 384 target_terminal::ours_for_output (); 385 386 if (started) 387 { 388 if (format != NULL) 389 { 390 fprintf_unfiltered (mi->event_channel, 391 "record-started,thread-group=\"i%d\"," 392 "method=\"%s\",format=\"%s\"", 393 inferior->num, method, format); 394 } 395 else 396 { 397 fprintf_unfiltered (mi->event_channel, 398 "record-started,thread-group=\"i%d\"," 399 "method=\"%s\"", 400 inferior->num, method); 401 } 402 } 403 else 404 { 405 fprintf_unfiltered (mi->event_channel, 406 "record-stopped,thread-group=\"i%d\"", 407 inferior->num); 408 } 409 410 gdb_flush (mi->event_channel); 411 } 412 } 413 414 static void 415 mi_inferior_added (struct inferior *inf) 416 { 417 SWITCH_THRU_ALL_UIS () 418 { 419 struct interp *interp; 420 struct mi_interp *mi; 421 422 /* We'll be called once for the initial inferior, before the top 423 level interpreter is set. */ 424 interp = top_level_interpreter (); 425 if (interp == NULL) 426 continue; 427 428 mi = as_mi_interp (interp); 429 if (mi == NULL) 430 continue; 431 432 target_terminal::scoped_restore_terminal_state term_state; 433 target_terminal::ours_for_output (); 434 435 fprintf_unfiltered (mi->event_channel, 436 "thread-group-added,id=\"i%d\"", 437 inf->num); 438 gdb_flush (mi->event_channel); 439 } 440 } 441 442 static void 443 mi_inferior_appeared (struct inferior *inf) 444 { 445 SWITCH_THRU_ALL_UIS () 446 { 447 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 448 449 if (mi == NULL) 450 continue; 451 452 target_terminal::scoped_restore_terminal_state term_state; 453 target_terminal::ours_for_output (); 454 455 fprintf_unfiltered (mi->event_channel, 456 "thread-group-started,id=\"i%d\",pid=\"%d\"", 457 inf->num, inf->pid); 458 gdb_flush (mi->event_channel); 459 } 460 } 461 462 static void 463 mi_inferior_exit (struct inferior *inf) 464 { 465 SWITCH_THRU_ALL_UIS () 466 { 467 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 468 469 if (mi == NULL) 470 continue; 471 472 target_terminal::scoped_restore_terminal_state term_state; 473 target_terminal::ours_for_output (); 474 475 if (inf->has_exit_code) 476 fprintf_unfiltered (mi->event_channel, 477 "thread-group-exited,id=\"i%d\",exit-code=\"%s\"", 478 inf->num, int_string (inf->exit_code, 8, 0, 0, 1)); 479 else 480 fprintf_unfiltered (mi->event_channel, 481 "thread-group-exited,id=\"i%d\"", inf->num); 482 483 gdb_flush (mi->event_channel); 484 } 485 } 486 487 static void 488 mi_inferior_removed (struct inferior *inf) 489 { 490 SWITCH_THRU_ALL_UIS () 491 { 492 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 493 494 if (mi == NULL) 495 continue; 496 497 target_terminal::scoped_restore_terminal_state term_state; 498 target_terminal::ours_for_output (); 499 500 fprintf_unfiltered (mi->event_channel, 501 "thread-group-removed,id=\"i%d\"", 502 inf->num); 503 gdb_flush (mi->event_channel); 504 } 505 } 506 507 /* Return the MI interpreter, if it is active -- either because it's 508 the top-level interpreter or the interpreter executing the current 509 command. Returns NULL if the MI interpreter is not being used. */ 510 511 static struct mi_interp * 512 find_mi_interp (void) 513 { 514 struct mi_interp *mi; 515 516 mi = as_mi_interp (top_level_interpreter ()); 517 if (mi != NULL) 518 return mi; 519 520 mi = as_mi_interp (command_interp ()); 521 if (mi != NULL) 522 return mi; 523 524 return NULL; 525 } 526 527 /* Observers for several run control events that print why the 528 inferior has stopped to both the MI event channel and to the MI 529 console. If the MI interpreter is not active, print nothing. */ 530 531 /* Observer for the signal_received notification. */ 532 533 static void 534 mi_on_signal_received (enum gdb_signal siggnal) 535 { 536 SWITCH_THRU_ALL_UIS () 537 { 538 struct mi_interp *mi = find_mi_interp (); 539 540 if (mi == NULL) 541 continue; 542 543 print_signal_received_reason (mi->mi_uiout, siggnal); 544 print_signal_received_reason (mi->cli_uiout, siggnal); 545 } 546 } 547 548 /* Observer for the end_stepping_range notification. */ 549 550 static void 551 mi_on_end_stepping_range (void) 552 { 553 SWITCH_THRU_ALL_UIS () 554 { 555 struct mi_interp *mi = find_mi_interp (); 556 557 if (mi == NULL) 558 continue; 559 560 print_end_stepping_range_reason (mi->mi_uiout); 561 print_end_stepping_range_reason (mi->cli_uiout); 562 } 563 } 564 565 /* Observer for the signal_exited notification. */ 566 567 static void 568 mi_on_signal_exited (enum gdb_signal siggnal) 569 { 570 SWITCH_THRU_ALL_UIS () 571 { 572 struct mi_interp *mi = find_mi_interp (); 573 574 if (mi == NULL) 575 continue; 576 577 print_signal_exited_reason (mi->mi_uiout, siggnal); 578 print_signal_exited_reason (mi->cli_uiout, siggnal); 579 } 580 } 581 582 /* Observer for the exited notification. */ 583 584 static void 585 mi_on_exited (int exitstatus) 586 { 587 SWITCH_THRU_ALL_UIS () 588 { 589 struct mi_interp *mi = find_mi_interp (); 590 591 if (mi == NULL) 592 continue; 593 594 print_exited_reason (mi->mi_uiout, exitstatus); 595 print_exited_reason (mi->cli_uiout, exitstatus); 596 } 597 } 598 599 /* Observer for the no_history notification. */ 600 601 static void 602 mi_on_no_history (void) 603 { 604 SWITCH_THRU_ALL_UIS () 605 { 606 struct mi_interp *mi = find_mi_interp (); 607 608 if (mi == NULL) 609 continue; 610 611 print_no_history_reason (mi->mi_uiout); 612 print_no_history_reason (mi->cli_uiout); 613 } 614 } 615 616 static void 617 mi_on_normal_stop_1 (struct bpstats *bs, int print_frame) 618 { 619 /* Since this can be called when CLI command is executing, 620 using cli interpreter, be sure to use MI uiout for output, 621 not the current one. */ 622 struct ui_out *mi_uiout = top_level_interpreter ()->interp_ui_out (); 623 struct mi_interp *mi = (struct mi_interp *) top_level_interpreter (); 624 625 if (print_frame) 626 { 627 struct thread_info *tp; 628 int core; 629 struct interp *console_interp; 630 631 tp = inferior_thread (); 632 633 if (tp->thread_fsm != NULL 634 && tp->thread_fsm->finished_p ()) 635 { 636 enum async_reply_reason reason; 637 638 reason = tp->thread_fsm->async_reply_reason (); 639 mi_uiout->field_string ("reason", async_reason_lookup (reason)); 640 } 641 642 console_interp = interp_lookup (current_ui, INTERP_CONSOLE); 643 /* We only want to print the displays once, and we want it to 644 look just how it would on the console, so we use this to 645 decide whether the MI stop should include them. */ 646 bool console_print = should_print_stop_to_console (console_interp, tp); 647 print_stop_event (mi_uiout, !console_print); 648 649 if (console_print) 650 print_stop_event (mi->cli_uiout); 651 652 mi_uiout->field_signed ("thread-id", tp->global_num); 653 if (non_stop) 654 { 655 ui_out_emit_list list_emitter (mi_uiout, "stopped-threads"); 656 657 mi_uiout->field_signed (NULL, tp->global_num); 658 } 659 else 660 mi_uiout->field_string ("stopped-threads", "all"); 661 662 core = target_core_of_thread (tp->ptid); 663 if (core != -1) 664 mi_uiout->field_signed ("core", core); 665 } 666 667 fputs_unfiltered ("*stopped", mi->raw_stdout); 668 mi_out_put (mi_uiout, mi->raw_stdout); 669 mi_out_rewind (mi_uiout); 670 mi_print_timing_maybe (mi->raw_stdout); 671 fputs_unfiltered ("\n", mi->raw_stdout); 672 gdb_flush (mi->raw_stdout); 673 } 674 675 static void 676 mi_on_normal_stop (struct bpstats *bs, int print_frame) 677 { 678 SWITCH_THRU_ALL_UIS () 679 { 680 if (as_mi_interp (top_level_interpreter ()) == NULL) 681 continue; 682 683 mi_on_normal_stop_1 (bs, print_frame); 684 } 685 } 686 687 static void 688 mi_about_to_proceed (void) 689 { 690 /* Suppress output while calling an inferior function. */ 691 692 if (inferior_ptid != null_ptid) 693 { 694 struct thread_info *tp = inferior_thread (); 695 696 if (tp->control.in_infcall) 697 return; 698 } 699 700 mi_proceeded = 1; 701 } 702 703 /* When the element is non-zero, no MI notifications will be emitted in 704 response to the corresponding observers. */ 705 706 struct mi_suppress_notification mi_suppress_notification = 707 { 708 0, 709 0, 710 0, 711 0, 712 }; 713 714 /* Emit notification on changing a traceframe. */ 715 716 static void 717 mi_traceframe_changed (int tfnum, int tpnum) 718 { 719 if (mi_suppress_notification.traceframe) 720 return; 721 722 SWITCH_THRU_ALL_UIS () 723 { 724 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 725 726 if (mi == NULL) 727 continue; 728 729 target_terminal::scoped_restore_terminal_state term_state; 730 target_terminal::ours_for_output (); 731 732 if (tfnum >= 0) 733 fprintf_unfiltered (mi->event_channel, "traceframe-changed," 734 "num=\"%d\",tracepoint=\"%d\"\n", 735 tfnum, tpnum); 736 else 737 fprintf_unfiltered (mi->event_channel, "traceframe-changed,end"); 738 739 gdb_flush (mi->event_channel); 740 } 741 } 742 743 /* Emit notification on creating a trace state variable. */ 744 745 static void 746 mi_tsv_created (const struct trace_state_variable *tsv) 747 { 748 SWITCH_THRU_ALL_UIS () 749 { 750 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 751 752 if (mi == NULL) 753 continue; 754 755 target_terminal::scoped_restore_terminal_state term_state; 756 target_terminal::ours_for_output (); 757 758 fprintf_unfiltered (mi->event_channel, "tsv-created," 759 "name=\"%s\",initial=\"%s\"\n", 760 tsv->name.c_str (), plongest (tsv->initial_value)); 761 762 gdb_flush (mi->event_channel); 763 } 764 } 765 766 /* Emit notification on deleting a trace state variable. */ 767 768 static void 769 mi_tsv_deleted (const struct trace_state_variable *tsv) 770 { 771 SWITCH_THRU_ALL_UIS () 772 { 773 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 774 775 if (mi == NULL) 776 continue; 777 778 target_terminal::scoped_restore_terminal_state term_state; 779 target_terminal::ours_for_output (); 780 781 if (tsv != NULL) 782 fprintf_unfiltered (mi->event_channel, "tsv-deleted," 783 "name=\"%s\"\n", tsv->name.c_str ()); 784 else 785 fprintf_unfiltered (mi->event_channel, "tsv-deleted\n"); 786 787 gdb_flush (mi->event_channel); 788 } 789 } 790 791 /* Emit notification on modifying a trace state variable. */ 792 793 static void 794 mi_tsv_modified (const struct trace_state_variable *tsv) 795 { 796 SWITCH_THRU_ALL_UIS () 797 { 798 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 799 struct ui_out *mi_uiout; 800 801 if (mi == NULL) 802 continue; 803 804 mi_uiout = top_level_interpreter ()->interp_ui_out (); 805 806 target_terminal::scoped_restore_terminal_state term_state; 807 target_terminal::ours_for_output (); 808 809 fprintf_unfiltered (mi->event_channel, 810 "tsv-modified"); 811 812 mi_uiout->redirect (mi->event_channel); 813 814 mi_uiout->field_string ("name", tsv->name); 815 mi_uiout->field_string ("initial", 816 plongest (tsv->initial_value)); 817 if (tsv->value_known) 818 mi_uiout->field_string ("current", plongest (tsv->value)); 819 820 mi_uiout->redirect (NULL); 821 822 gdb_flush (mi->event_channel); 823 } 824 } 825 826 /* Print breakpoint BP on MI's event channel. */ 827 828 static void 829 mi_print_breakpoint_for_event (struct mi_interp *mi, breakpoint *bp) 830 { 831 ui_out *mi_uiout = mi->interp_ui_out (); 832 833 /* We want the output from print_breakpoint to go to 834 mi->event_channel. One approach would be to just call 835 print_breakpoint, and then use mi_out_put to send the current 836 content of mi_uiout into mi->event_channel. However, that will 837 break if anything is output to mi_uiout prior to calling the 838 breakpoint_created notifications. So, we use 839 ui_out_redirect. */ 840 mi_uiout->redirect (mi->event_channel); 841 842 try 843 { 844 scoped_restore restore_uiout 845 = make_scoped_restore (¤t_uiout, mi_uiout); 846 847 print_breakpoint (bp); 848 } 849 catch (const gdb_exception &ex) 850 { 851 exception_print (gdb_stderr, ex); 852 } 853 854 mi_uiout->redirect (NULL); 855 } 856 857 /* Emit notification about a created breakpoint. */ 858 859 static void 860 mi_breakpoint_created (struct breakpoint *b) 861 { 862 if (mi_suppress_notification.breakpoint) 863 return; 864 865 if (b->number <= 0) 866 return; 867 868 SWITCH_THRU_ALL_UIS () 869 { 870 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 871 872 if (mi == NULL) 873 continue; 874 875 target_terminal::scoped_restore_terminal_state term_state; 876 target_terminal::ours_for_output (); 877 878 fprintf_unfiltered (mi->event_channel, 879 "breakpoint-created"); 880 mi_print_breakpoint_for_event (mi, b); 881 882 gdb_flush (mi->event_channel); 883 } 884 } 885 886 /* Emit notification about deleted breakpoint. */ 887 888 static void 889 mi_breakpoint_deleted (struct breakpoint *b) 890 { 891 if (mi_suppress_notification.breakpoint) 892 return; 893 894 if (b->number <= 0) 895 return; 896 897 SWITCH_THRU_ALL_UIS () 898 { 899 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 900 901 if (mi == NULL) 902 continue; 903 904 target_terminal::scoped_restore_terminal_state term_state; 905 target_terminal::ours_for_output (); 906 907 fprintf_unfiltered (mi->event_channel, "breakpoint-deleted,id=\"%d\"", 908 b->number); 909 910 gdb_flush (mi->event_channel); 911 } 912 } 913 914 /* Emit notification about modified breakpoint. */ 915 916 static void 917 mi_breakpoint_modified (struct breakpoint *b) 918 { 919 if (mi_suppress_notification.breakpoint) 920 return; 921 922 if (b->number <= 0) 923 return; 924 925 SWITCH_THRU_ALL_UIS () 926 { 927 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 928 929 if (mi == NULL) 930 continue; 931 932 target_terminal::scoped_restore_terminal_state term_state; 933 target_terminal::ours_for_output (); 934 fprintf_unfiltered (mi->event_channel, 935 "breakpoint-modified"); 936 mi_print_breakpoint_for_event (mi, b); 937 938 gdb_flush (mi->event_channel); 939 } 940 } 941 942 static void 943 mi_output_running (struct thread_info *thread) 944 { 945 SWITCH_THRU_ALL_UIS () 946 { 947 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 948 949 if (mi == NULL) 950 continue; 951 952 fprintf_unfiltered (mi->raw_stdout, 953 "*running,thread-id=\"%d\"\n", 954 thread->global_num); 955 } 956 } 957 958 /* Return true if there are multiple inferiors loaded. This is used 959 for backwards compatibility -- if there's only one inferior, output 960 "all", otherwise, output each resumed thread individually. */ 961 962 static bool 963 multiple_inferiors_p () 964 { 965 int count = 0; 966 for (inferior *inf ATTRIBUTE_UNUSED : all_non_exited_inferiors ()) 967 { 968 count++; 969 if (count > 1) 970 return true; 971 } 972 973 return false; 974 } 975 976 static void 977 mi_on_resume_1 (struct mi_interp *mi, 978 process_stratum_target *targ, ptid_t ptid) 979 { 980 /* To cater for older frontends, emit ^running, but do it only once 981 per each command. We do it here, since at this point we know 982 that the target was successfully resumed, and in non-async mode, 983 we won't return back to MI interpreter code until the target 984 is done running, so delaying the output of "^running" until then 985 will make it impossible for frontend to know what's going on. 986 987 In future (MI3), we'll be outputting "^done" here. */ 988 if (!running_result_record_printed && mi_proceeded) 989 { 990 fprintf_unfiltered (mi->raw_stdout, "%s^running\n", 991 current_token ? current_token : ""); 992 } 993 994 /* Backwards compatibility. If doing a wildcard resume and there's 995 only one inferior, output "all", otherwise, output each resumed 996 thread individually. */ 997 if ((ptid == minus_one_ptid || ptid.is_pid ()) 998 && !multiple_inferiors_p ()) 999 fprintf_unfiltered (mi->raw_stdout, "*running,thread-id=\"all\"\n"); 1000 else 1001 for (thread_info *tp : all_non_exited_threads (targ, ptid)) 1002 mi_output_running (tp); 1003 1004 if (!running_result_record_printed && mi_proceeded) 1005 { 1006 running_result_record_printed = 1; 1007 /* This is what gdb used to do historically -- printing prompt 1008 even if it cannot actually accept any input. This will be 1009 surely removed for MI3, and may be removed even earlier. */ 1010 if (current_ui->prompt_state == PROMPT_BLOCKED) 1011 fputs_unfiltered ("(gdb) \n", mi->raw_stdout); 1012 } 1013 gdb_flush (mi->raw_stdout); 1014 } 1015 1016 static void 1017 mi_on_resume (ptid_t ptid) 1018 { 1019 struct thread_info *tp = NULL; 1020 1021 process_stratum_target *target = current_inferior ()->process_target (); 1022 if (ptid == minus_one_ptid || ptid.is_pid ()) 1023 tp = inferior_thread (); 1024 else 1025 tp = find_thread_ptid (target, ptid); 1026 1027 /* Suppress output while calling an inferior function. */ 1028 if (tp->control.in_infcall) 1029 return; 1030 1031 SWITCH_THRU_ALL_UIS () 1032 { 1033 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 1034 1035 if (mi == NULL) 1036 continue; 1037 1038 target_terminal::scoped_restore_terminal_state term_state; 1039 target_terminal::ours_for_output (); 1040 1041 mi_on_resume_1 (mi, target, ptid); 1042 } 1043 } 1044 1045 /* See mi-interp.h. */ 1046 1047 void 1048 mi_output_solib_attribs (ui_out *uiout, struct so_list *solib) 1049 { 1050 struct gdbarch *gdbarch = target_gdbarch (); 1051 1052 uiout->field_string ("id", solib->so_original_name); 1053 uiout->field_string ("target-name", solib->so_original_name); 1054 uiout->field_string ("host-name", solib->so_name); 1055 uiout->field_signed ("symbols-loaded", solib->symbols_loaded); 1056 if (!gdbarch_has_global_solist (target_gdbarch ())) 1057 uiout->field_fmt ("thread-group", "i%d", current_inferior ()->num); 1058 1059 ui_out_emit_list list_emitter (uiout, "ranges"); 1060 ui_out_emit_tuple tuple_emitter (uiout, NULL); 1061 if (solib->addr_high != 0) 1062 { 1063 uiout->field_core_addr ("from", gdbarch, solib->addr_low); 1064 uiout->field_core_addr ("to", gdbarch, solib->addr_high); 1065 } 1066 } 1067 1068 static void 1069 mi_solib_loaded (struct so_list *solib) 1070 { 1071 SWITCH_THRU_ALL_UIS () 1072 { 1073 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 1074 struct ui_out *uiout; 1075 1076 if (mi == NULL) 1077 continue; 1078 1079 uiout = top_level_interpreter ()->interp_ui_out (); 1080 1081 target_terminal::scoped_restore_terminal_state term_state; 1082 target_terminal::ours_for_output (); 1083 1084 fprintf_unfiltered (mi->event_channel, "library-loaded"); 1085 1086 uiout->redirect (mi->event_channel); 1087 1088 mi_output_solib_attribs (uiout, solib); 1089 1090 uiout->redirect (NULL); 1091 1092 gdb_flush (mi->event_channel); 1093 } 1094 } 1095 1096 static void 1097 mi_solib_unloaded (struct so_list *solib) 1098 { 1099 SWITCH_THRU_ALL_UIS () 1100 { 1101 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 1102 struct ui_out *uiout; 1103 1104 if (mi == NULL) 1105 continue; 1106 1107 uiout = top_level_interpreter ()->interp_ui_out (); 1108 1109 target_terminal::scoped_restore_terminal_state term_state; 1110 target_terminal::ours_for_output (); 1111 1112 fprintf_unfiltered (mi->event_channel, "library-unloaded"); 1113 1114 uiout->redirect (mi->event_channel); 1115 1116 uiout->field_string ("id", solib->so_original_name); 1117 uiout->field_string ("target-name", solib->so_original_name); 1118 uiout->field_string ("host-name", solib->so_name); 1119 if (!gdbarch_has_global_solist (target_gdbarch ())) 1120 { 1121 uiout->field_fmt ("thread-group", "i%d", current_inferior ()->num); 1122 } 1123 1124 uiout->redirect (NULL); 1125 1126 gdb_flush (mi->event_channel); 1127 } 1128 } 1129 1130 /* Emit notification about the command parameter change. */ 1131 1132 static void 1133 mi_command_param_changed (const char *param, const char *value) 1134 { 1135 if (mi_suppress_notification.cmd_param_changed) 1136 return; 1137 1138 SWITCH_THRU_ALL_UIS () 1139 { 1140 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 1141 struct ui_out *mi_uiout; 1142 1143 if (mi == NULL) 1144 continue; 1145 1146 mi_uiout = top_level_interpreter ()->interp_ui_out (); 1147 1148 target_terminal::scoped_restore_terminal_state term_state; 1149 target_terminal::ours_for_output (); 1150 1151 fprintf_unfiltered (mi->event_channel, "cmd-param-changed"); 1152 1153 mi_uiout->redirect (mi->event_channel); 1154 1155 mi_uiout->field_string ("param", param); 1156 mi_uiout->field_string ("value", value); 1157 1158 mi_uiout->redirect (NULL); 1159 1160 gdb_flush (mi->event_channel); 1161 } 1162 } 1163 1164 /* Emit notification about the target memory change. */ 1165 1166 static void 1167 mi_memory_changed (struct inferior *inferior, CORE_ADDR memaddr, 1168 ssize_t len, const bfd_byte *myaddr) 1169 { 1170 if (mi_suppress_notification.memory) 1171 return; 1172 1173 SWITCH_THRU_ALL_UIS () 1174 { 1175 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 1176 struct ui_out *mi_uiout; 1177 struct obj_section *sec; 1178 1179 if (mi == NULL) 1180 continue; 1181 1182 mi_uiout = top_level_interpreter ()->interp_ui_out (); 1183 1184 target_terminal::scoped_restore_terminal_state term_state; 1185 target_terminal::ours_for_output (); 1186 1187 fprintf_unfiltered (mi->event_channel, "memory-changed"); 1188 1189 mi_uiout->redirect (mi->event_channel); 1190 1191 mi_uiout->field_fmt ("thread-group", "i%d", inferior->num); 1192 mi_uiout->field_core_addr ("addr", target_gdbarch (), memaddr); 1193 mi_uiout->field_string ("len", hex_string (len)); 1194 1195 /* Append 'type=code' into notification if MEMADDR falls in the range of 1196 sections contain code. */ 1197 sec = find_pc_section (memaddr); 1198 if (sec != NULL && sec->objfile != NULL) 1199 { 1200 flagword flags = bfd_section_flags (sec->the_bfd_section); 1201 1202 if (flags & SEC_CODE) 1203 mi_uiout->field_string ("type", "code"); 1204 } 1205 1206 mi_uiout->redirect (NULL); 1207 1208 gdb_flush (mi->event_channel); 1209 } 1210 } 1211 1212 /* Emit an event when the selection context (inferior, thread, frame) 1213 changed. */ 1214 1215 static void 1216 mi_user_selected_context_changed (user_selected_what selection) 1217 { 1218 struct thread_info *tp; 1219 1220 /* Don't send an event if we're responding to an MI command. */ 1221 if (mi_suppress_notification.user_selected_context) 1222 return; 1223 1224 if (inferior_ptid != null_ptid) 1225 tp = inferior_thread (); 1226 else 1227 tp = NULL; 1228 1229 SWITCH_THRU_ALL_UIS () 1230 { 1231 struct mi_interp *mi = as_mi_interp (top_level_interpreter ()); 1232 struct ui_out *mi_uiout; 1233 1234 if (mi == NULL) 1235 continue; 1236 1237 mi_uiout = top_level_interpreter ()->interp_ui_out (); 1238 1239 mi_uiout->redirect (mi->event_channel); 1240 ui_out_redirect_pop redirect_popper (mi_uiout); 1241 1242 target_terminal::scoped_restore_terminal_state term_state; 1243 target_terminal::ours_for_output (); 1244 1245 if (selection & USER_SELECTED_INFERIOR) 1246 print_selected_inferior (mi->cli_uiout); 1247 1248 if (tp != NULL 1249 && (selection & (USER_SELECTED_THREAD | USER_SELECTED_FRAME))) 1250 { 1251 print_selected_thread_frame (mi->cli_uiout, selection); 1252 1253 fprintf_unfiltered (mi->event_channel, 1254 "thread-selected,id=\"%d\"", 1255 tp->global_num); 1256 1257 if (tp->state != THREAD_RUNNING) 1258 { 1259 if (has_stack_frames ()) 1260 print_stack_frame_to_uiout (mi_uiout, get_selected_frame (NULL), 1261 1, SRC_AND_LOC, 1); 1262 } 1263 } 1264 1265 gdb_flush (mi->event_channel); 1266 } 1267 } 1268 1269 ui_out * 1270 mi_interp::interp_ui_out () 1271 { 1272 return this->mi_uiout; 1273 } 1274 1275 /* Do MI-specific logging actions; save raw_stdout, and change all 1276 the consoles to use the supplied ui-file(s). */ 1277 1278 void 1279 mi_interp::set_logging (ui_file_up logfile, bool logging_redirect, 1280 bool debug_redirect) 1281 { 1282 struct mi_interp *mi = this; 1283 1284 if (logfile != NULL) 1285 { 1286 mi->saved_raw_stdout = mi->raw_stdout; 1287 1288 /* If something is being redirected, then grab logfile. */ 1289 ui_file *logfile_p = nullptr; 1290 if (logging_redirect || debug_redirect) 1291 { 1292 logfile_p = logfile.get (); 1293 mi->saved_raw_file_to_delete = logfile_p; 1294 } 1295 1296 /* If something is not being redirected, then a tee containing both the 1297 logfile and stdout. */ 1298 ui_file *tee = nullptr; 1299 if (!logging_redirect || !debug_redirect) 1300 { 1301 tee = new tee_file (mi->raw_stdout, std::move (logfile)); 1302 mi->saved_raw_file_to_delete = tee; 1303 } 1304 1305 mi->raw_stdout = logging_redirect ? logfile_p : tee; 1306 mi->raw_stdlog = debug_redirect ? logfile_p : tee; 1307 } 1308 else 1309 { 1310 delete mi->saved_raw_file_to_delete; 1311 mi->raw_stdout = mi->saved_raw_stdout; 1312 mi->saved_raw_stdout = nullptr; 1313 mi->saved_raw_file_to_delete = nullptr; 1314 } 1315 1316 mi->out->set_raw (mi->raw_stdout); 1317 mi->err->set_raw (mi->raw_stdout); 1318 mi->log->set_raw (mi->raw_stdout); 1319 mi->targ->set_raw (mi->raw_stdout); 1320 mi->event_channel->set_raw (mi->raw_stdout); 1321 } 1322 1323 /* Factory for MI interpreters. */ 1324 1325 static struct interp * 1326 mi_interp_factory (const char *name) 1327 { 1328 return new mi_interp (name); 1329 } 1330 1331 void _initialize_mi_interp (); 1332 void 1333 _initialize_mi_interp () 1334 { 1335 /* The various interpreter levels. */ 1336 interp_factory_register (INTERP_MI1, mi_interp_factory); 1337 interp_factory_register (INTERP_MI2, mi_interp_factory); 1338 interp_factory_register (INTERP_MI3, mi_interp_factory); 1339 interp_factory_register (INTERP_MI, mi_interp_factory); 1340 1341 gdb::observers::signal_received.attach (mi_on_signal_received); 1342 gdb::observers::end_stepping_range.attach (mi_on_end_stepping_range); 1343 gdb::observers::signal_exited.attach (mi_on_signal_exited); 1344 gdb::observers::exited.attach (mi_on_exited); 1345 gdb::observers::no_history.attach (mi_on_no_history); 1346 gdb::observers::new_thread.attach (mi_new_thread); 1347 gdb::observers::thread_exit.attach (mi_thread_exit); 1348 gdb::observers::inferior_added.attach (mi_inferior_added); 1349 gdb::observers::inferior_appeared.attach (mi_inferior_appeared); 1350 gdb::observers::inferior_exit.attach (mi_inferior_exit); 1351 gdb::observers::inferior_removed.attach (mi_inferior_removed); 1352 gdb::observers::record_changed.attach (mi_record_changed); 1353 gdb::observers::normal_stop.attach (mi_on_normal_stop); 1354 gdb::observers::target_resumed.attach (mi_on_resume); 1355 gdb::observers::solib_loaded.attach (mi_solib_loaded); 1356 gdb::observers::solib_unloaded.attach (mi_solib_unloaded); 1357 gdb::observers::about_to_proceed.attach (mi_about_to_proceed); 1358 gdb::observers::traceframe_changed.attach (mi_traceframe_changed); 1359 gdb::observers::tsv_created.attach (mi_tsv_created); 1360 gdb::observers::tsv_deleted.attach (mi_tsv_deleted); 1361 gdb::observers::tsv_modified.attach (mi_tsv_modified); 1362 gdb::observers::breakpoint_created.attach (mi_breakpoint_created); 1363 gdb::observers::breakpoint_deleted.attach (mi_breakpoint_deleted); 1364 gdb::observers::breakpoint_modified.attach (mi_breakpoint_modified); 1365 gdb::observers::command_param_changed.attach (mi_command_param_changed); 1366 gdb::observers::memory_changed.attach (mi_memory_changed); 1367 gdb::observers::sync_execution_done.attach (mi_on_sync_execution_done); 1368 gdb::observers::user_selected_context_changed.attach 1369 (mi_user_selected_context_changed); 1370 } 1371