1 /* Multi-process control for GDB, the GNU debugger. 2 3 Copyright (C) 2008-2023 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 #include "exec.h" 22 #include "inferior.h" 23 #include "target.h" 24 #include "command.h" 25 #include "completer.h" 26 #include "gdbcmd.h" 27 #include "gdbthread.h" 28 #include "ui-out.h" 29 #include "observable.h" 30 #include "gdbcore.h" 31 #include "symfile.h" 32 #include "gdbsupport/environ.h" 33 #include "cli/cli-utils.h" 34 #include "arch-utils.h" 35 #include "target-descriptions.h" 36 #include "target-connection.h" 37 #include "readline/tilde.h" 38 #include "progspace-and-thread.h" 39 #include "gdbsupport/buildargv.h" 40 #include "cli/cli-style.h" 41 42 intrusive_list<inferior> inferior_list; 43 static int highest_inferior_num; 44 45 /* See inferior.h. */ 46 bool print_inferior_events = true; 47 48 /* The Current Inferior. This is a strong reference. I.e., whenever 49 an inferior is the current inferior, its refcount is 50 incremented. */ 51 static inferior_ref current_inferior_; 52 53 struct inferior* 54 current_inferior (void) 55 { 56 return current_inferior_.get (); 57 } 58 59 void 60 set_current_inferior (struct inferior *inf) 61 { 62 /* There's always an inferior. */ 63 gdb_assert (inf != NULL); 64 65 current_inferior_ = inferior_ref::new_reference (inf); 66 } 67 68 private_inferior::~private_inferior () = default; 69 70 inferior::~inferior () 71 { 72 inferior *inf = this; 73 74 /* Before the inferior is deleted, all target_ops should be popped from 75 the target stack, this leaves just the dummy_target behind. If this 76 is not done, then any target left in the target stack will be left 77 with an artificially high reference count. As the dummy_target is 78 still on the target stack then we are about to loose a reference to 79 that target, leaving its reference count artificially high. However, 80 this is not critical as the dummy_target is a singleton. */ 81 gdb_assert (m_target_stack.top ()->stratum () == dummy_stratum); 82 83 m_continuations.clear (); 84 target_desc_info_free (inf->tdesc_info); 85 } 86 87 inferior::inferior (int pid_) 88 : num (++highest_inferior_num), 89 pid (pid_), 90 environment (gdb_environ::from_host_environ ()) 91 { 92 m_target_stack.push (get_dummy_target ()); 93 } 94 95 /* See inferior.h. */ 96 97 int 98 inferior::unpush_target (struct target_ops *t) 99 { 100 /* If unpushing the process stratum target from the inferior while threads 101 exist in the inferior, ensure that we don't leave any threads of the 102 inferior in the target's "resumed with pending wait status" list. 103 104 See also the comment in set_thread_exited. */ 105 if (t->stratum () == process_stratum) 106 { 107 process_stratum_target *proc_target = as_process_stratum_target (t); 108 109 for (thread_info *thread : this->non_exited_threads ()) 110 proc_target->maybe_remove_resumed_with_pending_wait_status (thread); 111 } 112 113 return m_target_stack.unpush (t); 114 } 115 116 /* See inferior.h. */ 117 118 void 119 inferior::unpush_target_and_assert (struct target_ops *target) 120 { 121 gdb_assert (current_inferior () == this); 122 123 if (!unpush_target (target)) 124 internal_error ("pop_all_targets couldn't find target %s\n", 125 target->shortname ()); 126 } 127 128 /* See inferior.h. */ 129 130 void 131 inferior::pop_all_targets_above (enum strata stratum) 132 { 133 /* Unpushing a target might cause it to close. Some targets currently 134 rely on the current_inferior being set for their ::close method, so we 135 temporarily switch inferior now. */ 136 scoped_restore_current_pspace_and_thread restore_pspace_and_thread; 137 switch_to_inferior_no_thread (this); 138 139 while (top_target ()->stratum () > stratum) 140 unpush_target_and_assert (top_target ()); 141 } 142 143 /* See inferior.h. */ 144 145 void 146 inferior::pop_all_targets_at_and_above (enum strata stratum) 147 { 148 /* Unpushing a target might cause it to close. Some targets currently 149 rely on the current_inferior being set for their ::close method, so we 150 temporarily switch inferior now. */ 151 scoped_restore_current_pspace_and_thread restore_pspace_and_thread; 152 switch_to_inferior_no_thread (this); 153 154 while (top_target ()->stratum () >= stratum) 155 unpush_target_and_assert (top_target ()); 156 } 157 158 void 159 inferior::set_tty (std::string terminal_name) 160 { 161 m_terminal = std::move (terminal_name); 162 } 163 164 const std::string & 165 inferior::tty () 166 { 167 return m_terminal; 168 } 169 170 void 171 inferior::add_continuation (std::function<void ()> &&cont) 172 { 173 m_continuations.emplace_front (std::move (cont)); 174 } 175 176 void 177 inferior::do_all_continuations () 178 { 179 while (!m_continuations.empty ()) 180 { 181 auto iter = m_continuations.begin (); 182 (*iter) (); 183 m_continuations.erase (iter); 184 } 185 } 186 187 struct inferior * 188 add_inferior_silent (int pid) 189 { 190 inferior *inf = new inferior (pid); 191 192 inferior_list.push_back (*inf); 193 194 gdb::observers::inferior_added.notify (inf); 195 196 if (pid != 0) 197 inferior_appeared (inf, pid); 198 199 return inf; 200 } 201 202 struct inferior * 203 add_inferior (int pid) 204 { 205 struct inferior *inf = add_inferior_silent (pid); 206 207 if (print_inferior_events) 208 { 209 if (pid != 0) 210 gdb_printf (_("[New inferior %d (%s)]\n"), 211 inf->num, 212 target_pid_to_str (ptid_t (pid)).c_str ()); 213 else 214 gdb_printf (_("[New inferior %d]\n"), inf->num); 215 } 216 217 return inf; 218 } 219 220 /* See inferior.h. */ 221 222 void 223 inferior::clear_thread_list (bool silent) 224 { 225 thread_list.clear_and_dispose ([=] (thread_info *thr) 226 { 227 threads_debug_printf ("deleting thread %s, silent = %d", 228 thr->ptid.to_string ().c_str (), silent); 229 set_thread_exited (thr, silent); 230 if (thr->deletable ()) 231 delete thr; 232 }); 233 ptid_thread_map.clear (); 234 } 235 236 void 237 delete_inferior (struct inferior *inf) 238 { 239 inf->clear_thread_list (true); 240 241 auto it = inferior_list.iterator_to (*inf); 242 inferior_list.erase (it); 243 244 gdb::observers::inferior_removed.notify (inf); 245 246 /* Pop all targets now, this ensures that inferior::unpush is called 247 correctly. As pop_all_targets ends up making a temporary switch to 248 inferior INF then we need to make this call before we delete the 249 program space, which we do below. */ 250 inf->pop_all_targets (); 251 252 /* If this program space is rendered useless, remove it. */ 253 if (inf->pspace->empty ()) 254 delete inf->pspace; 255 256 delete inf; 257 } 258 259 /* If SILENT then be quiet -- don't announce a inferior exit, or the 260 exit of its threads. */ 261 262 static void 263 exit_inferior_1 (struct inferior *inf, int silent) 264 { 265 inf->clear_thread_list (silent); 266 267 gdb::observers::inferior_exit.notify (inf); 268 269 inf->pid = 0; 270 inf->fake_pid_p = false; 271 inf->priv = NULL; 272 273 if (inf->vfork_parent != NULL) 274 { 275 inf->vfork_parent->vfork_child = NULL; 276 inf->vfork_parent = NULL; 277 } 278 if (inf->vfork_child != NULL) 279 { 280 inf->vfork_child->vfork_parent = NULL; 281 inf->vfork_child = NULL; 282 } 283 284 inf->pending_detach = false; 285 /* Reset it. */ 286 inf->control = inferior_control_state (NO_STOP_QUIETLY); 287 288 /* Clear the register cache and the frame cache. */ 289 registers_changed (); 290 reinit_frame_cache (); 291 } 292 293 void 294 exit_inferior (inferior *inf) 295 { 296 exit_inferior_1 (inf, 0); 297 } 298 299 void 300 exit_inferior_silent (inferior *inf) 301 { 302 exit_inferior_1 (inf, 1); 303 } 304 305 /* See inferior.h. */ 306 307 void 308 detach_inferior (inferior *inf) 309 { 310 /* Save the pid, since exit_inferior_1 will reset it. */ 311 int pid = inf->pid; 312 313 exit_inferior_1 (inf, 0); 314 315 if (print_inferior_events) 316 gdb_printf (_("[Inferior %d (%s) detached]\n"), 317 inf->num, 318 target_pid_to_str (ptid_t (pid)).c_str ()); 319 } 320 321 void 322 inferior_appeared (struct inferior *inf, int pid) 323 { 324 /* If this is the first inferior with threads, reset the global 325 thread id. */ 326 delete_exited_threads (); 327 if (!any_thread_p ()) 328 init_thread_list (); 329 330 inf->pid = pid; 331 inf->has_exit_code = false; 332 inf->exit_code = 0; 333 334 gdb::observers::inferior_appeared.notify (inf); 335 } 336 337 struct inferior * 338 find_inferior_id (int num) 339 { 340 for (inferior *inf : all_inferiors ()) 341 if (inf->num == num) 342 return inf; 343 344 return NULL; 345 } 346 347 struct inferior * 348 find_inferior_pid (process_stratum_target *targ, int pid) 349 { 350 /* Looking for inferior pid == 0 is always wrong, and indicative of 351 a bug somewhere else. There may be more than one with pid == 0, 352 for instance. */ 353 gdb_assert (pid != 0); 354 355 for (inferior *inf : all_inferiors (targ)) 356 if (inf->pid == pid) 357 return inf; 358 359 return NULL; 360 } 361 362 /* See inferior.h */ 363 364 struct inferior * 365 find_inferior_ptid (process_stratum_target *targ, ptid_t ptid) 366 { 367 return find_inferior_pid (targ, ptid.pid ()); 368 } 369 370 /* See inferior.h. */ 371 372 struct inferior * 373 find_inferior_for_program_space (struct program_space *pspace) 374 { 375 struct inferior *cur_inf = current_inferior (); 376 377 if (cur_inf->pspace == pspace) 378 return cur_inf; 379 380 for (inferior *inf : all_inferiors ()) 381 if (inf->pspace == pspace) 382 return inf; 383 384 return NULL; 385 } 386 387 int 388 have_inferiors (void) 389 { 390 for (inferior *inf ATTRIBUTE_UNUSED : all_non_exited_inferiors ()) 391 return 1; 392 393 return 0; 394 } 395 396 /* Return the number of live inferiors. We account for the case 397 where an inferior might have a non-zero pid but no threads, as 398 in the middle of a 'mourn' operation. */ 399 400 int 401 number_of_live_inferiors (process_stratum_target *proc_target) 402 { 403 int num_inf = 0; 404 405 for (inferior *inf : all_non_exited_inferiors (proc_target)) 406 if (inf->has_execution ()) 407 for (thread_info *tp ATTRIBUTE_UNUSED : inf->non_exited_threads ()) 408 { 409 /* Found a live thread in this inferior, go to the next 410 inferior. */ 411 ++num_inf; 412 break; 413 } 414 415 return num_inf; 416 } 417 418 /* Return true if there is at least one live inferior. */ 419 420 int 421 have_live_inferiors (void) 422 { 423 return number_of_live_inferiors (NULL) > 0; 424 } 425 426 /* Prune away any unused inferiors, and then prune away no longer used 427 program spaces. */ 428 429 void 430 prune_inferiors (void) 431 { 432 for (inferior *inf : all_inferiors_safe ()) 433 { 434 if (!inf->deletable () 435 || !inf->removable 436 || inf->pid != 0) 437 continue; 438 439 delete_inferior (inf); 440 } 441 } 442 443 /* Simply returns the count of inferiors. */ 444 445 int 446 number_of_inferiors (void) 447 { 448 auto rng = all_inferiors (); 449 return std::distance (rng.begin (), rng.end ()); 450 } 451 452 /* Converts an inferior process id to a string. Like 453 target_pid_to_str, but special cases the null process. */ 454 455 static std::string 456 inferior_pid_to_str (int pid) 457 { 458 if (pid != 0) 459 return target_pid_to_str (ptid_t (pid)); 460 else 461 return _("<null>"); 462 } 463 464 /* See inferior.h. */ 465 466 void 467 print_selected_inferior (struct ui_out *uiout) 468 { 469 struct inferior *inf = current_inferior (); 470 const char *filename = inf->pspace->exec_filename.get (); 471 472 if (filename == NULL) 473 filename = _("<noexec>"); 474 475 uiout->message (_("[Switching to inferior %d [%s] (%s)]\n"), 476 inf->num, inferior_pid_to_str (inf->pid).c_str (), filename); 477 } 478 479 /* Helper for print_inferior. Returns the 'connection-id' string for 480 PROC_TARGET. */ 481 482 static std::string 483 uiout_field_connection (process_stratum_target *proc_target) 484 { 485 if (proc_target == NULL) 486 return {}; 487 else 488 { 489 std::string conn_str = make_target_connection_string (proc_target); 490 return string_printf ("%d (%s)", proc_target->connection_number, 491 conn_str.c_str ()); 492 } 493 } 494 495 /* Prints the list of inferiors and their details on UIOUT. This is a 496 version of 'info_inferior_command' suitable for use from MI. 497 498 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the 499 inferiors that should be printed. Otherwise, all inferiors are 500 printed. */ 501 502 static void 503 print_inferior (struct ui_out *uiout, const char *requested_inferiors) 504 { 505 int inf_count = 0; 506 size_t connection_id_len = 20; 507 508 /* Compute number of inferiors we will print. */ 509 for (inferior *inf : all_inferiors ()) 510 { 511 if (!number_is_in_list (requested_inferiors, inf->num)) 512 continue; 513 514 std::string conn = uiout_field_connection (inf->process_target ()); 515 if (connection_id_len < conn.size ()) 516 connection_id_len = conn.size (); 517 518 ++inf_count; 519 } 520 521 if (inf_count == 0) 522 { 523 uiout->message ("No inferiors.\n"); 524 return; 525 } 526 527 ui_out_emit_table table_emitter (uiout, 5, inf_count, "inferiors"); 528 uiout->table_header (1, ui_left, "current", ""); 529 uiout->table_header (4, ui_left, "number", "Num"); 530 uiout->table_header (17, ui_left, "target-id", "Description"); 531 uiout->table_header (connection_id_len, ui_left, 532 "connection-id", "Connection"); 533 uiout->table_header (17, ui_left, "exec", "Executable"); 534 535 uiout->table_body (); 536 537 /* Restore the current thread after the loop because we switch the 538 inferior in the loop. */ 539 scoped_restore_current_pspace_and_thread restore_pspace_thread; 540 inferior *current_inf = current_inferior (); 541 for (inferior *inf : all_inferiors ()) 542 { 543 if (!number_is_in_list (requested_inferiors, inf->num)) 544 continue; 545 546 ui_out_emit_tuple tuple_emitter (uiout, NULL); 547 548 if (inf == current_inf) 549 uiout->field_string ("current", "*"); 550 else 551 uiout->field_skip ("current"); 552 553 uiout->field_signed ("number", inf->num); 554 555 /* Because target_pid_to_str uses the current inferior, 556 switch the inferior. */ 557 switch_to_inferior_no_thread (inf); 558 559 uiout->field_string ("target-id", inferior_pid_to_str (inf->pid)); 560 561 std::string conn = uiout_field_connection (inf->process_target ()); 562 uiout->field_string ("connection-id", conn); 563 564 if (inf->pspace->exec_filename != nullptr) 565 uiout->field_string ("exec", inf->pspace->exec_filename.get (), 566 file_name_style.style ()); 567 else 568 uiout->field_skip ("exec"); 569 570 /* Print extra info that isn't really fit to always present in 571 tabular form. Currently we print the vfork parent/child 572 relationships, if any. */ 573 if (inf->vfork_parent) 574 { 575 uiout->text (_("\n\tis vfork child of inferior ")); 576 uiout->field_signed ("vfork-parent", inf->vfork_parent->num); 577 } 578 if (inf->vfork_child) 579 { 580 uiout->text (_("\n\tis vfork parent of inferior ")); 581 uiout->field_signed ("vfork-child", inf->vfork_child->num); 582 } 583 584 uiout->text ("\n"); 585 } 586 } 587 588 static void 589 detach_inferior_command (const char *args, int from_tty) 590 { 591 if (!args || !*args) 592 error (_("Requires argument (inferior id(s) to detach)")); 593 594 scoped_restore_current_thread restore_thread; 595 596 number_or_range_parser parser (args); 597 while (!parser.finished ()) 598 { 599 int num = parser.get_number (); 600 601 inferior *inf = find_inferior_id (num); 602 if (inf == NULL) 603 { 604 warning (_("Inferior ID %d not known."), num); 605 continue; 606 } 607 608 if (inf->pid == 0) 609 { 610 warning (_("Inferior ID %d is not running."), num); 611 continue; 612 } 613 614 thread_info *tp = any_thread_of_inferior (inf); 615 if (tp == NULL) 616 { 617 warning (_("Inferior ID %d has no threads."), num); 618 continue; 619 } 620 621 switch_to_thread (tp); 622 623 detach_command (NULL, from_tty); 624 } 625 } 626 627 static void 628 kill_inferior_command (const char *args, int from_tty) 629 { 630 if (!args || !*args) 631 error (_("Requires argument (inferior id(s) to kill)")); 632 633 scoped_restore_current_thread restore_thread; 634 635 number_or_range_parser parser (args); 636 while (!parser.finished ()) 637 { 638 int num = parser.get_number (); 639 640 inferior *inf = find_inferior_id (num); 641 if (inf == NULL) 642 { 643 warning (_("Inferior ID %d not known."), num); 644 continue; 645 } 646 647 if (inf->pid == 0) 648 { 649 warning (_("Inferior ID %d is not running."), num); 650 continue; 651 } 652 653 thread_info *tp = any_thread_of_inferior (inf); 654 if (tp == NULL) 655 { 656 warning (_("Inferior ID %d has no threads."), num); 657 continue; 658 } 659 660 switch_to_thread (tp); 661 662 target_kill (); 663 } 664 665 bfd_cache_close_all (); 666 } 667 668 /* See inferior.h. */ 669 670 void 671 switch_to_inferior_no_thread (inferior *inf) 672 { 673 set_current_inferior (inf); 674 switch_to_no_thread (); 675 set_current_program_space (inf->pspace); 676 } 677 678 static void 679 inferior_command (const char *args, int from_tty) 680 { 681 struct inferior *inf; 682 int num; 683 684 if (args == nullptr) 685 { 686 inf = current_inferior (); 687 gdb_assert (inf != nullptr); 688 const char *filename = inf->pspace->exec_filename.get (); 689 690 if (filename == nullptr) 691 filename = _("<noexec>"); 692 693 gdb_printf (_("[Current inferior is %d [%s] (%s)]\n"), 694 inf->num, inferior_pid_to_str (inf->pid).c_str (), 695 filename); 696 } 697 else 698 { 699 num = parse_and_eval_long (args); 700 701 inf = find_inferior_id (num); 702 if (inf == NULL) 703 error (_("Inferior ID %d not known."), num); 704 705 if (inf->pid != 0) 706 { 707 if (inf != current_inferior ()) 708 { 709 thread_info *tp = any_thread_of_inferior (inf); 710 if (tp == NULL) 711 error (_("Inferior has no threads.")); 712 713 switch_to_thread (tp); 714 } 715 716 gdb::observers::user_selected_context_changed.notify 717 (USER_SELECTED_INFERIOR 718 | USER_SELECTED_THREAD 719 | USER_SELECTED_FRAME); 720 } 721 else 722 { 723 switch_to_inferior_no_thread (inf); 724 725 gdb::observers::user_selected_context_changed.notify 726 (USER_SELECTED_INFERIOR); 727 } 728 } 729 } 730 731 /* Print information about currently known inferiors. */ 732 733 static void 734 info_inferiors_command (const char *args, int from_tty) 735 { 736 print_inferior (current_uiout, args); 737 } 738 739 /* remove-inferior ID */ 740 741 static void 742 remove_inferior_command (const char *args, int from_tty) 743 { 744 if (args == NULL || *args == '\0') 745 error (_("Requires an argument (inferior id(s) to remove)")); 746 747 number_or_range_parser parser (args); 748 while (!parser.finished ()) 749 { 750 int num = parser.get_number (); 751 struct inferior *inf = find_inferior_id (num); 752 753 if (inf == NULL) 754 { 755 warning (_("Inferior ID %d not known."), num); 756 continue; 757 } 758 759 if (!inf->deletable ()) 760 { 761 warning (_("Can not remove current inferior %d."), num); 762 continue; 763 } 764 765 if (inf->pid != 0) 766 { 767 warning (_("Can not remove active inferior %d."), num); 768 continue; 769 } 770 771 delete_inferior (inf); 772 } 773 } 774 775 struct inferior * 776 add_inferior_with_spaces (void) 777 { 778 struct address_space *aspace; 779 struct program_space *pspace; 780 struct inferior *inf; 781 782 /* If all inferiors share an address space on this system, this 783 doesn't really return a new address space; otherwise, it 784 really does. */ 785 aspace = maybe_new_address_space (); 786 pspace = new program_space (aspace); 787 inf = add_inferior (0); 788 inf->pspace = pspace; 789 inf->aspace = pspace->aspace; 790 791 /* Setup the inferior's initial arch, based on information obtained 792 from the global "set ..." options. */ 793 gdbarch_info info; 794 inf->gdbarch = gdbarch_find_by_info (info); 795 /* The "set ..." options reject invalid settings, so we should 796 always have a valid arch by now. */ 797 gdb_assert (inf->gdbarch != NULL); 798 799 return inf; 800 } 801 802 /* See inferior.h. */ 803 804 void 805 switch_to_inferior_and_push_target (inferior *new_inf, 806 bool no_connection, inferior *org_inf) 807 { 808 process_stratum_target *proc_target = org_inf->process_target (); 809 810 /* Switch over temporarily, while reading executable and 811 symbols. */ 812 switch_to_inferior_no_thread (new_inf); 813 814 /* Reuse the target for new inferior. */ 815 if (!no_connection && proc_target != NULL) 816 { 817 new_inf->push_target (proc_target); 818 gdb_printf (_("Added inferior %d on connection %d (%s)\n"), 819 new_inf->num, 820 proc_target->connection_number, 821 make_target_connection_string (proc_target).c_str ()); 822 } 823 else 824 gdb_printf (_("Added inferior %d\n"), new_inf->num); 825 } 826 827 /* add-inferior [-copies N] [-exec FILENAME] [-no-connection] */ 828 829 static void 830 add_inferior_command (const char *args, int from_tty) 831 { 832 int i, copies = 1; 833 gdb::unique_xmalloc_ptr<char> exec; 834 symfile_add_flags add_flags = 0; 835 bool no_connection = false; 836 837 if (from_tty) 838 add_flags |= SYMFILE_VERBOSE; 839 840 if (args) 841 { 842 gdb_argv built_argv (args); 843 844 for (char **argv = built_argv.get (); *argv != NULL; argv++) 845 { 846 if (**argv == '-') 847 { 848 if (strcmp (*argv, "-copies") == 0) 849 { 850 ++argv; 851 if (!*argv) 852 error (_("No argument to -copies")); 853 copies = parse_and_eval_long (*argv); 854 } 855 else if (strcmp (*argv, "-no-connection") == 0) 856 no_connection = true; 857 else if (strcmp (*argv, "-exec") == 0) 858 { 859 ++argv; 860 if (!*argv) 861 error (_("No argument to -exec")); 862 exec.reset (tilde_expand (*argv)); 863 } 864 } 865 else 866 error (_("Invalid argument")); 867 } 868 } 869 870 inferior *orginf = current_inferior (); 871 872 scoped_restore_current_pspace_and_thread restore_pspace_thread; 873 874 for (i = 0; i < copies; ++i) 875 { 876 inferior *inf = add_inferior_with_spaces (); 877 878 switch_to_inferior_and_push_target (inf, no_connection, orginf); 879 880 if (exec != NULL) 881 { 882 exec_file_attach (exec.get (), from_tty); 883 symbol_file_add_main (exec.get (), add_flags); 884 } 885 } 886 } 887 888 /* clone-inferior [-copies N] [ID] [-no-connection] */ 889 890 static void 891 clone_inferior_command (const char *args, int from_tty) 892 { 893 int i, copies = 1; 894 struct inferior *orginf = NULL; 895 bool no_connection = false; 896 897 if (args) 898 { 899 gdb_argv built_argv (args); 900 901 char **argv = built_argv.get (); 902 for (; *argv != NULL; argv++) 903 { 904 if (**argv == '-') 905 { 906 if (strcmp (*argv, "-copies") == 0) 907 { 908 ++argv; 909 if (!*argv) 910 error (_("No argument to -copies")); 911 copies = parse_and_eval_long (*argv); 912 913 if (copies < 0) 914 error (_("Invalid copies number")); 915 } 916 else if (strcmp (*argv, "-no-connection") == 0) 917 no_connection = true; 918 } 919 else 920 { 921 if (orginf == NULL) 922 { 923 int num; 924 925 /* The first non-option (-) argument specified the 926 program space ID. */ 927 num = parse_and_eval_long (*argv); 928 orginf = find_inferior_id (num); 929 930 if (orginf == NULL) 931 error (_("Inferior ID %d not known."), num); 932 continue; 933 } 934 else 935 error (_("Invalid argument")); 936 } 937 } 938 } 939 940 /* If no inferior id was specified, then the user wants to clone the 941 current inferior. */ 942 if (orginf == NULL) 943 orginf = current_inferior (); 944 945 scoped_restore_current_pspace_and_thread restore_pspace_thread; 946 947 for (i = 0; i < copies; ++i) 948 { 949 struct address_space *aspace; 950 struct program_space *pspace; 951 struct inferior *inf; 952 953 /* If all inferiors share an address space on this system, this 954 doesn't really return a new address space; otherwise, it 955 really does. */ 956 aspace = maybe_new_address_space (); 957 pspace = new program_space (aspace); 958 inf = add_inferior (0); 959 inf->pspace = pspace; 960 inf->aspace = pspace->aspace; 961 inf->gdbarch = orginf->gdbarch; 962 963 switch_to_inferior_and_push_target (inf, no_connection, orginf); 964 965 /* If the original inferior had a user specified target 966 description, make the clone use it too. */ 967 if (target_desc_info_from_user_p (inf->tdesc_info)) 968 copy_inferior_target_desc_info (inf, orginf); 969 970 clone_program_space (pspace, orginf->pspace); 971 972 /* Copy properties from the original inferior to the new one. */ 973 inf->set_args (orginf->args ()); 974 inf->set_cwd (orginf->cwd ()); 975 inf->set_tty (orginf->tty ()); 976 for (const std::string &set_var : orginf->environment.user_set_env ()) 977 { 978 /* set_var has the form NAME=value. Split on the first '='. */ 979 const std::string::size_type pos = set_var.find ('='); 980 gdb_assert (pos != std::string::npos); 981 const std::string varname = set_var.substr (0, pos); 982 inf->environment.set 983 (varname.c_str (), orginf->environment.get (varname.c_str ())); 984 } 985 for (const std::string &unset_var 986 : orginf->environment.user_unset_env ()) 987 inf->environment.unset (unset_var.c_str ()); 988 } 989 } 990 991 /* Print notices when new inferiors are created and die. */ 992 static void 993 show_print_inferior_events (struct ui_file *file, int from_tty, 994 struct cmd_list_element *c, const char *value) 995 { 996 gdb_printf (file, _("Printing of inferior events is %s.\n"), value); 997 } 998 999 /* Return a new value for the selected inferior's id. */ 1000 1001 static struct value * 1002 inferior_id_make_value (struct gdbarch *gdbarch, struct internalvar *var, 1003 void *ignore) 1004 { 1005 struct inferior *inf = current_inferior (); 1006 1007 return value_from_longest (builtin_type (gdbarch)->builtin_int, inf->num); 1008 } 1009 1010 /* Implementation of `$_inferior' variable. */ 1011 1012 static const struct internalvar_funcs inferior_funcs = 1013 { 1014 inferior_id_make_value, 1015 NULL, 1016 }; 1017 1018 1019 1020 void 1021 initialize_inferiors (void) 1022 { 1023 struct cmd_list_element *c = NULL; 1024 1025 /* There's always one inferior. Note that this function isn't an 1026 automatic _initialize_foo function, since other _initialize_foo 1027 routines may need to install their per-inferior data keys. We 1028 can only allocate an inferior when all those modules have done 1029 that. Do this after initialize_progspace, due to the 1030 current_program_space reference. */ 1031 set_current_inferior (add_inferior_silent (0)); 1032 current_inferior_->pspace = current_program_space; 1033 current_inferior_->aspace = current_program_space->aspace; 1034 /* The architecture will be initialized shortly, by 1035 initialize_current_architecture. */ 1036 1037 add_info ("inferiors", info_inferiors_command, 1038 _("Print a list of inferiors being managed.\n\ 1039 Usage: info inferiors [ID]...\n\ 1040 If IDs are specified, the list is limited to just those inferiors.\n\ 1041 By default all inferiors are displayed.")); 1042 1043 c = add_com ("add-inferior", no_class, add_inferior_command, _("\ 1044 Add a new inferior.\n\ 1045 Usage: add-inferior [-copies N] [-exec FILENAME] [-no-connection]\n\ 1046 N is the optional number of inferiors to add, default is 1.\n\ 1047 FILENAME is the file name of the executable to use\n\ 1048 as main program.\n\ 1049 By default, the new inferior inherits the current inferior's connection.\n\ 1050 If -no-connection is specified, the new inferior begins with\n\ 1051 no target connection yet.")); 1052 set_cmd_completer (c, filename_completer); 1053 1054 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\ 1055 Remove inferior ID (or list of IDs).\n\ 1056 Usage: remove-inferiors ID...")); 1057 1058 add_com ("clone-inferior", no_class, clone_inferior_command, _("\ 1059 Clone inferior ID.\n\ 1060 Usage: clone-inferior [-copies N] [-no-connection] [ID]\n\ 1061 Add N copies of inferior ID. The new inferiors have the same\n\ 1062 executable loaded as the copied inferior. If -copies is not specified,\n\ 1063 adds 1 copy. If ID is not specified, it is the current inferior\n\ 1064 that is cloned.\n\ 1065 By default, the new inferiors inherit the copied inferior's connection.\n\ 1066 If -no-connection is specified, the new inferiors begin with\n\ 1067 no target connection yet.")); 1068 1069 add_cmd ("inferiors", class_run, detach_inferior_command, _("\ 1070 Detach from inferior ID (or list of IDS).\n\ 1071 Usage; detach inferiors ID..."), 1072 &detachlist); 1073 1074 add_cmd ("inferiors", class_run, kill_inferior_command, _("\ 1075 Kill inferior ID (or list of IDs).\n\ 1076 Usage: kill inferiors ID..."), 1077 &killlist); 1078 1079 add_cmd ("inferior", class_run, inferior_command, _("\ 1080 Use this command to switch between inferiors.\n\ 1081 Usage: inferior ID\n\ 1082 The new inferior ID must be currently known."), 1083 &cmdlist); 1084 1085 add_setshow_boolean_cmd ("inferior-events", no_class, 1086 &print_inferior_events, _("\ 1087 Set printing of inferior events (such as inferior start and exit)."), _("\ 1088 Show printing of inferior events (such as inferior start and exit)."), NULL, 1089 NULL, 1090 show_print_inferior_events, 1091 &setprintlist, &showprintlist); 1092 1093 create_internalvar_type_lazy ("_inferior", &inferior_funcs, NULL); 1094 } 1095