| 1 | /* Multi-process control for GDB, the GNU debugger. |
| 2 | |
| 3 | Copyright (C) 2008-2015 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 "observer.h" |
| 30 | #include "gdbcore.h" |
| 31 | #include "symfile.h" |
| 32 | #include "environ.h" |
| 33 | #include "cli/cli-utils.h" |
| 34 | #include "continuations.h" |
| 35 | #include "arch-utils.h" |
| 36 | #include "target-descriptions.h" |
| 37 | #include "readline/tilde.h" |
| 38 | |
| 39 | void _initialize_inferiors (void); |
| 40 | |
| 41 | /* Keep a registry of per-inferior data-pointers required by other GDB |
| 42 | modules. */ |
| 43 | |
| 44 | DEFINE_REGISTRY (inferior, REGISTRY_ACCESS_FIELD) |
| 45 | |
| 46 | struct inferior *inferior_list = NULL; |
| 47 | static int highest_inferior_num; |
| 48 | |
| 49 | /* Print notices on inferior events (attach, detach, etc.), set with |
| 50 | `set print inferior-events'. */ |
| 51 | static int print_inferior_events = 0; |
| 52 | |
| 53 | /* The Current Inferior. */ |
| 54 | static struct inferior *current_inferior_ = NULL; |
| 55 | |
| 56 | struct inferior* |
| 57 | current_inferior (void) |
| 58 | { |
| 59 | return current_inferior_; |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | set_current_inferior (struct inferior *inf) |
| 64 | { |
| 65 | /* There's always an inferior. */ |
| 66 | gdb_assert (inf != NULL); |
| 67 | |
| 68 | current_inferior_ = inf; |
| 69 | } |
| 70 | |
| 71 | /* A cleanups callback, helper for save_current_program_space |
| 72 | below. */ |
| 73 | |
| 74 | static void |
| 75 | restore_inferior (void *arg) |
| 76 | { |
| 77 | struct inferior *saved_inferior = arg; |
| 78 | |
| 79 | set_current_inferior (saved_inferior); |
| 80 | } |
| 81 | |
| 82 | /* Save the current program space so that it may be restored by a later |
| 83 | call to do_cleanups. Returns the struct cleanup pointer needed for |
| 84 | later doing the cleanup. */ |
| 85 | |
| 86 | struct cleanup * |
| 87 | save_current_inferior (void) |
| 88 | { |
| 89 | struct cleanup *old_chain = make_cleanup (restore_inferior, |
| 90 | current_inferior_); |
| 91 | |
| 92 | return old_chain; |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | free_inferior (struct inferior *inf) |
| 97 | { |
| 98 | discard_all_inferior_continuations (inf); |
| 99 | inferior_free_data (inf); |
| 100 | xfree (inf->args); |
| 101 | xfree (inf->terminal); |
| 102 | free_environ (inf->environment); |
| 103 | target_desc_info_free (inf->tdesc_info); |
| 104 | xfree (inf->priv); |
| 105 | xfree (inf); |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | init_inferior_list (void) |
| 110 | { |
| 111 | struct inferior *inf, *infnext; |
| 112 | |
| 113 | highest_inferior_num = 0; |
| 114 | if (!inferior_list) |
| 115 | return; |
| 116 | |
| 117 | for (inf = inferior_list; inf; inf = infnext) |
| 118 | { |
| 119 | infnext = inf->next; |
| 120 | free_inferior (inf); |
| 121 | } |
| 122 | |
| 123 | inferior_list = NULL; |
| 124 | } |
| 125 | |
| 126 | struct inferior * |
| 127 | add_inferior_silent (int pid) |
| 128 | { |
| 129 | struct inferior *inf; |
| 130 | |
| 131 | inf = xmalloc (sizeof (*inf)); |
| 132 | memset (inf, 0, sizeof (*inf)); |
| 133 | inf->pid = pid; |
| 134 | |
| 135 | inf->control.stop_soon = NO_STOP_QUIETLY; |
| 136 | |
| 137 | inf->num = ++highest_inferior_num; |
| 138 | inf->next = inferior_list; |
| 139 | inferior_list = inf; |
| 140 | |
| 141 | inf->environment = make_environ (); |
| 142 | init_environ (inf->environment); |
| 143 | |
| 144 | inferior_alloc_data (inf); |
| 145 | |
| 146 | observer_notify_inferior_added (inf); |
| 147 | |
| 148 | if (pid != 0) |
| 149 | inferior_appeared (inf, pid); |
| 150 | |
| 151 | return inf; |
| 152 | } |
| 153 | |
| 154 | struct inferior * |
| 155 | add_inferior (int pid) |
| 156 | { |
| 157 | struct inferior *inf = add_inferior_silent (pid); |
| 158 | |
| 159 | if (print_inferior_events) |
| 160 | printf_unfiltered (_("[New inferior %d]\n"), pid); |
| 161 | |
| 162 | return inf; |
| 163 | } |
| 164 | |
| 165 | struct delete_thread_of_inferior_arg |
| 166 | { |
| 167 | int pid; |
| 168 | int silent; |
| 169 | }; |
| 170 | |
| 171 | static int |
| 172 | delete_thread_of_inferior (struct thread_info *tp, void *data) |
| 173 | { |
| 174 | struct delete_thread_of_inferior_arg *arg = data; |
| 175 | |
| 176 | if (ptid_get_pid (tp->ptid) == arg->pid) |
| 177 | { |
| 178 | if (arg->silent) |
| 179 | delete_thread_silent (tp->ptid); |
| 180 | else |
| 181 | delete_thread (tp->ptid); |
| 182 | } |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | delete_inferior (struct inferior *todel) |
| 189 | { |
| 190 | struct inferior *inf, *infprev; |
| 191 | struct delete_thread_of_inferior_arg arg; |
| 192 | |
| 193 | infprev = NULL; |
| 194 | |
| 195 | for (inf = inferior_list; inf; infprev = inf, inf = inf->next) |
| 196 | if (inf == todel) |
| 197 | break; |
| 198 | |
| 199 | if (!inf) |
| 200 | return; |
| 201 | |
| 202 | arg.pid = inf->pid; |
| 203 | arg.silent = 1; |
| 204 | |
| 205 | iterate_over_threads (delete_thread_of_inferior, &arg); |
| 206 | |
| 207 | if (infprev) |
| 208 | infprev->next = inf->next; |
| 209 | else |
| 210 | inferior_list = inf->next; |
| 211 | |
| 212 | observer_notify_inferior_removed (inf); |
| 213 | |
| 214 | /* If this program space is rendered useless, remove it. */ |
| 215 | if (program_space_empty_p (inf->pspace)) |
| 216 | delete_program_space (inf->pspace); |
| 217 | |
| 218 | free_inferior (inf); |
| 219 | } |
| 220 | |
| 221 | /* If SILENT then be quiet -- don't announce a inferior exit, or the |
| 222 | exit of its threads. */ |
| 223 | |
| 224 | static void |
| 225 | exit_inferior_1 (struct inferior *inftoex, int silent) |
| 226 | { |
| 227 | struct inferior *inf; |
| 228 | struct delete_thread_of_inferior_arg arg; |
| 229 | |
| 230 | for (inf = inferior_list; inf; inf = inf->next) |
| 231 | if (inf == inftoex) |
| 232 | break; |
| 233 | |
| 234 | if (!inf) |
| 235 | return; |
| 236 | |
| 237 | arg.pid = inf->pid; |
| 238 | arg.silent = silent; |
| 239 | |
| 240 | iterate_over_threads (delete_thread_of_inferior, &arg); |
| 241 | |
| 242 | /* Notify the observers before removing the inferior from the list, |
| 243 | so that the observers have a chance to look it up. */ |
| 244 | observer_notify_inferior_exit (inf); |
| 245 | |
| 246 | inf->pid = 0; |
| 247 | inf->fake_pid_p = 0; |
| 248 | if (inf->vfork_parent != NULL) |
| 249 | { |
| 250 | inf->vfork_parent->vfork_child = NULL; |
| 251 | inf->vfork_parent = NULL; |
| 252 | } |
| 253 | if (inf->vfork_child != NULL) |
| 254 | { |
| 255 | inf->vfork_child->vfork_parent = NULL; |
| 256 | inf->vfork_child = NULL; |
| 257 | } |
| 258 | |
| 259 | inf->pending_detach = 0; |
| 260 | } |
| 261 | |
| 262 | void |
| 263 | exit_inferior (int pid) |
| 264 | { |
| 265 | struct inferior *inf = find_inferior_pid (pid); |
| 266 | |
| 267 | exit_inferior_1 (inf, 0); |
| 268 | |
| 269 | if (print_inferior_events) |
| 270 | printf_unfiltered (_("[Inferior %d exited]\n"), pid); |
| 271 | } |
| 272 | |
| 273 | void |
| 274 | exit_inferior_silent (int pid) |
| 275 | { |
| 276 | struct inferior *inf = find_inferior_pid (pid); |
| 277 | |
| 278 | exit_inferior_1 (inf, 1); |
| 279 | } |
| 280 | |
| 281 | void |
| 282 | exit_inferior_num_silent (int num) |
| 283 | { |
| 284 | struct inferior *inf = find_inferior_id (num); |
| 285 | |
| 286 | exit_inferior_1 (inf, 1); |
| 287 | } |
| 288 | |
| 289 | void |
| 290 | detach_inferior (int pid) |
| 291 | { |
| 292 | struct inferior *inf = find_inferior_pid (pid); |
| 293 | |
| 294 | exit_inferior_1 (inf, 0); |
| 295 | |
| 296 | if (print_inferior_events) |
| 297 | printf_unfiltered (_("[Inferior %d detached]\n"), pid); |
| 298 | } |
| 299 | |
| 300 | void |
| 301 | inferior_appeared (struct inferior *inf, int pid) |
| 302 | { |
| 303 | inf->pid = pid; |
| 304 | inf->has_exit_code = 0; |
| 305 | inf->exit_code = 0; |
| 306 | |
| 307 | observer_notify_inferior_appeared (inf); |
| 308 | } |
| 309 | |
| 310 | void |
| 311 | discard_all_inferiors (void) |
| 312 | { |
| 313 | struct inferior *inf; |
| 314 | |
| 315 | for (inf = inferior_list; inf; inf = inf->next) |
| 316 | { |
| 317 | if (inf->pid != 0) |
| 318 | exit_inferior_silent (inf->pid); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | struct inferior * |
| 323 | find_inferior_id (int num) |
| 324 | { |
| 325 | struct inferior *inf; |
| 326 | |
| 327 | for (inf = inferior_list; inf; inf = inf->next) |
| 328 | if (inf->num == num) |
| 329 | return inf; |
| 330 | |
| 331 | return NULL; |
| 332 | } |
| 333 | |
| 334 | struct inferior * |
| 335 | find_inferior_pid (int pid) |
| 336 | { |
| 337 | struct inferior *inf; |
| 338 | |
| 339 | /* Looking for inferior pid == 0 is always wrong, and indicative of |
| 340 | a bug somewhere else. There may be more than one with pid == 0, |
| 341 | for instance. */ |
| 342 | gdb_assert (pid != 0); |
| 343 | |
| 344 | for (inf = inferior_list; inf; inf = inf->next) |
| 345 | if (inf->pid == pid) |
| 346 | return inf; |
| 347 | |
| 348 | return NULL; |
| 349 | } |
| 350 | |
| 351 | /* See inferior.h */ |
| 352 | |
| 353 | struct inferior * |
| 354 | find_inferior_ptid (ptid_t ptid) |
| 355 | { |
| 356 | return find_inferior_pid (ptid_get_pid (ptid)); |
| 357 | } |
| 358 | |
| 359 | /* See inferior.h. */ |
| 360 | |
| 361 | struct inferior * |
| 362 | find_inferior_for_program_space (struct program_space *pspace) |
| 363 | { |
| 364 | struct inferior *inf = current_inferior (); |
| 365 | |
| 366 | if (inf->pspace == pspace) |
| 367 | return inf; |
| 368 | |
| 369 | for (inf = inferior_list; inf != NULL; inf = inf->next) |
| 370 | { |
| 371 | if (inf->pspace == pspace) |
| 372 | return inf; |
| 373 | } |
| 374 | |
| 375 | return NULL; |
| 376 | } |
| 377 | |
| 378 | struct inferior * |
| 379 | iterate_over_inferiors (int (*callback) (struct inferior *, void *), |
| 380 | void *data) |
| 381 | { |
| 382 | struct inferior *inf, *infnext; |
| 383 | |
| 384 | for (inf = inferior_list; inf; inf = infnext) |
| 385 | { |
| 386 | infnext = inf->next; |
| 387 | if ((*callback) (inf, data)) |
| 388 | return inf; |
| 389 | } |
| 390 | |
| 391 | return NULL; |
| 392 | } |
| 393 | |
| 394 | int |
| 395 | valid_gdb_inferior_id (int num) |
| 396 | { |
| 397 | struct inferior *inf; |
| 398 | |
| 399 | for (inf = inferior_list; inf; inf = inf->next) |
| 400 | if (inf->num == num) |
| 401 | return 1; |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | int |
| 407 | pid_to_gdb_inferior_id (int pid) |
| 408 | { |
| 409 | struct inferior *inf; |
| 410 | |
| 411 | for (inf = inferior_list; inf; inf = inf->next) |
| 412 | if (inf->pid == pid) |
| 413 | return inf->num; |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | int |
| 419 | gdb_inferior_id_to_pid (int num) |
| 420 | { |
| 421 | struct inferior *inferior = find_inferior_id (num); |
| 422 | if (inferior) |
| 423 | return inferior->pid; |
| 424 | else |
| 425 | return -1; |
| 426 | } |
| 427 | |
| 428 | int |
| 429 | in_inferior_list (int pid) |
| 430 | { |
| 431 | struct inferior *inf; |
| 432 | |
| 433 | for (inf = inferior_list; inf; inf = inf->next) |
| 434 | if (inf->pid == pid) |
| 435 | return 1; |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | int |
| 441 | have_inferiors (void) |
| 442 | { |
| 443 | struct inferior *inf; |
| 444 | |
| 445 | for (inf = inferior_list; inf; inf = inf->next) |
| 446 | if (inf->pid != 0) |
| 447 | return 1; |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | int |
| 453 | have_live_inferiors (void) |
| 454 | { |
| 455 | struct inferior *inf; |
| 456 | |
| 457 | for (inf = inferior_list; inf; inf = inf->next) |
| 458 | if (inf->pid != 0) |
| 459 | { |
| 460 | struct thread_info *tp; |
| 461 | |
| 462 | tp = any_thread_of_process (inf->pid); |
| 463 | if (tp && target_has_execution_1 (tp->ptid)) |
| 464 | break; |
| 465 | } |
| 466 | |
| 467 | return inf != NULL; |
| 468 | } |
| 469 | |
| 470 | /* Prune away any unused inferiors, and then prune away no longer used |
| 471 | program spaces. */ |
| 472 | |
| 473 | void |
| 474 | prune_inferiors (void) |
| 475 | { |
| 476 | struct inferior *ss, **ss_link; |
| 477 | struct inferior *current = current_inferior (); |
| 478 | |
| 479 | ss = inferior_list; |
| 480 | ss_link = &inferior_list; |
| 481 | while (ss) |
| 482 | { |
| 483 | if (ss == current |
| 484 | || !ss->removable |
| 485 | || ss->pid != 0) |
| 486 | { |
| 487 | ss_link = &ss->next; |
| 488 | ss = *ss_link; |
| 489 | continue; |
| 490 | } |
| 491 | |
| 492 | *ss_link = ss->next; |
| 493 | delete_inferior (ss); |
| 494 | ss = *ss_link; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /* Simply returns the count of inferiors. */ |
| 499 | |
| 500 | int |
| 501 | number_of_inferiors (void) |
| 502 | { |
| 503 | struct inferior *inf; |
| 504 | int count = 0; |
| 505 | |
| 506 | for (inf = inferior_list; inf != NULL; inf = inf->next) |
| 507 | count++; |
| 508 | |
| 509 | return count; |
| 510 | } |
| 511 | |
| 512 | /* Converts an inferior process id to a string. Like |
| 513 | target_pid_to_str, but special cases the null process. */ |
| 514 | |
| 515 | static char * |
| 516 | inferior_pid_to_str (int pid) |
| 517 | { |
| 518 | if (pid != 0) |
| 519 | return target_pid_to_str (pid_to_ptid (pid)); |
| 520 | else |
| 521 | return _("<null>"); |
| 522 | } |
| 523 | |
| 524 | /* Prints the list of inferiors and their details on UIOUT. This is a |
| 525 | version of 'info_inferior_command' suitable for use from MI. |
| 526 | |
| 527 | If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the |
| 528 | inferiors that should be printed. Otherwise, all inferiors are |
| 529 | printed. */ |
| 530 | |
| 531 | static void |
| 532 | print_inferior (struct ui_out *uiout, char *requested_inferiors) |
| 533 | { |
| 534 | struct inferior *inf; |
| 535 | struct cleanup *old_chain; |
| 536 | int inf_count = 0; |
| 537 | |
| 538 | /* Compute number of inferiors we will print. */ |
| 539 | for (inf = inferior_list; inf; inf = inf->next) |
| 540 | { |
| 541 | if (!number_is_in_list (requested_inferiors, inf->num)) |
| 542 | continue; |
| 543 | |
| 544 | ++inf_count; |
| 545 | } |
| 546 | |
| 547 | if (inf_count == 0) |
| 548 | { |
| 549 | ui_out_message (uiout, 0, "No inferiors.\n"); |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count, |
| 554 | "inferiors"); |
| 555 | ui_out_table_header (uiout, 1, ui_left, "current", ""); |
| 556 | ui_out_table_header (uiout, 4, ui_left, "number", "Num"); |
| 557 | ui_out_table_header (uiout, 17, ui_left, "target-id", "Description"); |
| 558 | ui_out_table_header (uiout, 17, ui_left, "exec", "Executable"); |
| 559 | |
| 560 | ui_out_table_body (uiout); |
| 561 | for (inf = inferior_list; inf; inf = inf->next) |
| 562 | { |
| 563 | struct cleanup *chain2; |
| 564 | |
| 565 | if (!number_is_in_list (requested_inferiors, inf->num)) |
| 566 | continue; |
| 567 | |
| 568 | chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); |
| 569 | |
| 570 | if (inf == current_inferior ()) |
| 571 | ui_out_field_string (uiout, "current", "*"); |
| 572 | else |
| 573 | ui_out_field_skip (uiout, "current"); |
| 574 | |
| 575 | ui_out_field_int (uiout, "number", inf->num); |
| 576 | |
| 577 | ui_out_field_string (uiout, "target-id", |
| 578 | inferior_pid_to_str (inf->pid)); |
| 579 | |
| 580 | if (inf->pspace->pspace_exec_filename != NULL) |
| 581 | ui_out_field_string (uiout, "exec", inf->pspace->pspace_exec_filename); |
| 582 | else |
| 583 | ui_out_field_skip (uiout, "exec"); |
| 584 | |
| 585 | /* Print extra info that isn't really fit to always present in |
| 586 | tabular form. Currently we print the vfork parent/child |
| 587 | relationships, if any. */ |
| 588 | if (inf->vfork_parent) |
| 589 | { |
| 590 | ui_out_text (uiout, _("\n\tis vfork child of inferior ")); |
| 591 | ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num); |
| 592 | } |
| 593 | if (inf->vfork_child) |
| 594 | { |
| 595 | ui_out_text (uiout, _("\n\tis vfork parent of inferior ")); |
| 596 | ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num); |
| 597 | } |
| 598 | |
| 599 | ui_out_text (uiout, "\n"); |
| 600 | do_cleanups (chain2); |
| 601 | } |
| 602 | |
| 603 | do_cleanups (old_chain); |
| 604 | } |
| 605 | |
| 606 | static void |
| 607 | detach_inferior_command (char *args, int from_tty) |
| 608 | { |
| 609 | int num, pid; |
| 610 | struct thread_info *tp; |
| 611 | struct get_number_or_range_state state; |
| 612 | |
| 613 | if (!args || !*args) |
| 614 | error (_("Requires argument (inferior id(s) to detach)")); |
| 615 | |
| 616 | init_number_or_range (&state, args); |
| 617 | while (!state.finished) |
| 618 | { |
| 619 | num = get_number_or_range (&state); |
| 620 | |
| 621 | if (!valid_gdb_inferior_id (num)) |
| 622 | { |
| 623 | warning (_("Inferior ID %d not known."), num); |
| 624 | continue; |
| 625 | } |
| 626 | |
| 627 | pid = gdb_inferior_id_to_pid (num); |
| 628 | |
| 629 | tp = any_thread_of_process (pid); |
| 630 | if (!tp) |
| 631 | { |
| 632 | warning (_("Inferior ID %d has no threads."), num); |
| 633 | continue; |
| 634 | } |
| 635 | |
| 636 | switch_to_thread (tp->ptid); |
| 637 | |
| 638 | detach_command (NULL, from_tty); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | static void |
| 643 | kill_inferior_command (char *args, int from_tty) |
| 644 | { |
| 645 | int num, pid; |
| 646 | struct thread_info *tp; |
| 647 | struct get_number_or_range_state state; |
| 648 | |
| 649 | if (!args || !*args) |
| 650 | error (_("Requires argument (inferior id(s) to kill)")); |
| 651 | |
| 652 | init_number_or_range (&state, args); |
| 653 | while (!state.finished) |
| 654 | { |
| 655 | num = get_number_or_range (&state); |
| 656 | |
| 657 | if (!valid_gdb_inferior_id (num)) |
| 658 | { |
| 659 | warning (_("Inferior ID %d not known."), num); |
| 660 | continue; |
| 661 | } |
| 662 | |
| 663 | pid = gdb_inferior_id_to_pid (num); |
| 664 | |
| 665 | tp = any_thread_of_process (pid); |
| 666 | if (!tp) |
| 667 | { |
| 668 | warning (_("Inferior ID %d has no threads."), num); |
| 669 | continue; |
| 670 | } |
| 671 | |
| 672 | switch_to_thread (tp->ptid); |
| 673 | |
| 674 | target_kill (); |
| 675 | } |
| 676 | |
| 677 | bfd_cache_close_all (); |
| 678 | } |
| 679 | |
| 680 | static void |
| 681 | inferior_command (char *args, int from_tty) |
| 682 | { |
| 683 | struct inferior *inf; |
| 684 | int num; |
| 685 | |
| 686 | num = parse_and_eval_long (args); |
| 687 | |
| 688 | inf = find_inferior_id (num); |
| 689 | if (inf == NULL) |
| 690 | error (_("Inferior ID %d not known."), num); |
| 691 | |
| 692 | printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"), |
| 693 | inf->num, |
| 694 | inferior_pid_to_str (inf->pid), |
| 695 | (inf->pspace->pspace_exec_filename != NULL |
| 696 | ? inf->pspace->pspace_exec_filename |
| 697 | : _("<noexec>"))); |
| 698 | |
| 699 | if (inf->pid != 0) |
| 700 | { |
| 701 | if (inf->pid != ptid_get_pid (inferior_ptid)) |
| 702 | { |
| 703 | struct thread_info *tp; |
| 704 | |
| 705 | tp = any_thread_of_process (inf->pid); |
| 706 | if (!tp) |
| 707 | error (_("Inferior has no threads.")); |
| 708 | |
| 709 | switch_to_thread (tp->ptid); |
| 710 | } |
| 711 | |
| 712 | printf_filtered (_("[Switching to thread %d (%s)] "), |
| 713 | pid_to_thread_id (inferior_ptid), |
| 714 | target_pid_to_str (inferior_ptid)); |
| 715 | } |
| 716 | else |
| 717 | { |
| 718 | struct inferior *inf; |
| 719 | |
| 720 | inf = find_inferior_id (num); |
| 721 | set_current_inferior (inf); |
| 722 | switch_to_thread (null_ptid); |
| 723 | set_current_program_space (inf->pspace); |
| 724 | } |
| 725 | |
| 726 | if (inf->pid != 0 && is_running (inferior_ptid)) |
| 727 | ui_out_text (current_uiout, "(running)\n"); |
| 728 | else if (inf->pid != 0) |
| 729 | { |
| 730 | ui_out_text (current_uiout, "\n"); |
| 731 | print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | /* Print information about currently known inferiors. */ |
| 736 | |
| 737 | static void |
| 738 | info_inferiors_command (char *args, int from_tty) |
| 739 | { |
| 740 | print_inferior (current_uiout, args); |
| 741 | } |
| 742 | |
| 743 | /* remove-inferior ID */ |
| 744 | |
| 745 | static void |
| 746 | remove_inferior_command (char *args, int from_tty) |
| 747 | { |
| 748 | int num; |
| 749 | struct inferior *inf; |
| 750 | struct get_number_or_range_state state; |
| 751 | |
| 752 | if (args == NULL || *args == '\0') |
| 753 | error (_("Requires an argument (inferior id(s) to remove)")); |
| 754 | |
| 755 | init_number_or_range (&state, args); |
| 756 | while (!state.finished) |
| 757 | { |
| 758 | num = get_number_or_range (&state); |
| 759 | inf = find_inferior_id (num); |
| 760 | |
| 761 | if (inf == NULL) |
| 762 | { |
| 763 | warning (_("Inferior ID %d not known."), num); |
| 764 | continue; |
| 765 | } |
| 766 | |
| 767 | if (inf == current_inferior ()) |
| 768 | { |
| 769 | warning (_("Can not remove current symbol inferior %d."), num); |
| 770 | continue; |
| 771 | } |
| 772 | |
| 773 | if (inf->pid != 0) |
| 774 | { |
| 775 | warning (_("Can not remove active inferior %d."), num); |
| 776 | continue; |
| 777 | } |
| 778 | |
| 779 | delete_inferior (inf); |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | struct inferior * |
| 784 | add_inferior_with_spaces (void) |
| 785 | { |
| 786 | struct address_space *aspace; |
| 787 | struct program_space *pspace; |
| 788 | struct inferior *inf; |
| 789 | struct gdbarch_info info; |
| 790 | |
| 791 | /* If all inferiors share an address space on this system, this |
| 792 | doesn't really return a new address space; otherwise, it |
| 793 | really does. */ |
| 794 | aspace = maybe_new_address_space (); |
| 795 | pspace = add_program_space (aspace); |
| 796 | inf = add_inferior (0); |
| 797 | inf->pspace = pspace; |
| 798 | inf->aspace = pspace->aspace; |
| 799 | |
| 800 | /* Setup the inferior's initial arch, based on information obtained |
| 801 | from the global "set ..." options. */ |
| 802 | gdbarch_info_init (&info); |
| 803 | inf->gdbarch = gdbarch_find_by_info (info); |
| 804 | /* The "set ..." options reject invalid settings, so we should |
| 805 | always have a valid arch by now. */ |
| 806 | gdb_assert (inf->gdbarch != NULL); |
| 807 | |
| 808 | return inf; |
| 809 | } |
| 810 | |
| 811 | /* add-inferior [-copies N] [-exec FILENAME] */ |
| 812 | |
| 813 | static void |
| 814 | add_inferior_command (char *args, int from_tty) |
| 815 | { |
| 816 | int i, copies = 1; |
| 817 | char *exec = NULL; |
| 818 | char **argv; |
| 819 | struct cleanup *old_chain = make_cleanup (null_cleanup, NULL); |
| 820 | |
| 821 | if (args) |
| 822 | { |
| 823 | argv = gdb_buildargv (args); |
| 824 | make_cleanup_freeargv (argv); |
| 825 | |
| 826 | for (; *argv != NULL; argv++) |
| 827 | { |
| 828 | if (**argv == '-') |
| 829 | { |
| 830 | if (strcmp (*argv, "-copies") == 0) |
| 831 | { |
| 832 | ++argv; |
| 833 | if (!*argv) |
| 834 | error (_("No argument to -copies")); |
| 835 | copies = parse_and_eval_long (*argv); |
| 836 | } |
| 837 | else if (strcmp (*argv, "-exec") == 0) |
| 838 | { |
| 839 | ++argv; |
| 840 | if (!*argv) |
| 841 | error (_("No argument to -exec")); |
| 842 | exec = tilde_expand (*argv); |
| 843 | make_cleanup (xfree, exec); |
| 844 | } |
| 845 | } |
| 846 | else |
| 847 | error (_("Invalid argument")); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | save_current_space_and_thread (); |
| 852 | |
| 853 | for (i = 0; i < copies; ++i) |
| 854 | { |
| 855 | struct inferior *inf = add_inferior_with_spaces (); |
| 856 | |
| 857 | printf_filtered (_("Added inferior %d\n"), inf->num); |
| 858 | |
| 859 | if (exec != NULL) |
| 860 | { |
| 861 | /* Switch over temporarily, while reading executable and |
| 862 | symbols.q. */ |
| 863 | set_current_program_space (inf->pspace); |
| 864 | set_current_inferior (inf); |
| 865 | switch_to_thread (null_ptid); |
| 866 | |
| 867 | exec_file_attach (exec, from_tty); |
| 868 | symbol_file_add_main (exec, from_tty); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | do_cleanups (old_chain); |
| 873 | } |
| 874 | |
| 875 | /* clone-inferior [-copies N] [ID] */ |
| 876 | |
| 877 | static void |
| 878 | clone_inferior_command (char *args, int from_tty) |
| 879 | { |
| 880 | int i, copies = 1; |
| 881 | char **argv; |
| 882 | struct inferior *orginf = NULL; |
| 883 | struct cleanup *old_chain = make_cleanup (null_cleanup, NULL); |
| 884 | |
| 885 | if (args) |
| 886 | { |
| 887 | argv = gdb_buildargv (args); |
| 888 | make_cleanup_freeargv (argv); |
| 889 | |
| 890 | for (; *argv != NULL; argv++) |
| 891 | { |
| 892 | if (**argv == '-') |
| 893 | { |
| 894 | if (strcmp (*argv, "-copies") == 0) |
| 895 | { |
| 896 | ++argv; |
| 897 | if (!*argv) |
| 898 | error (_("No argument to -copies")); |
| 899 | copies = parse_and_eval_long (*argv); |
| 900 | |
| 901 | if (copies < 0) |
| 902 | error (_("Invalid copies number")); |
| 903 | } |
| 904 | } |
| 905 | else |
| 906 | { |
| 907 | if (orginf == NULL) |
| 908 | { |
| 909 | int num; |
| 910 | |
| 911 | /* The first non-option (-) argument specified the |
| 912 | program space ID. */ |
| 913 | num = parse_and_eval_long (*argv); |
| 914 | orginf = find_inferior_id (num); |
| 915 | |
| 916 | if (orginf == NULL) |
| 917 | error (_("Inferior ID %d not known."), num); |
| 918 | continue; |
| 919 | } |
| 920 | else |
| 921 | error (_("Invalid argument")); |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | /* If no inferior id was specified, then the user wants to clone the |
| 927 | current inferior. */ |
| 928 | if (orginf == NULL) |
| 929 | orginf = current_inferior (); |
| 930 | |
| 931 | save_current_space_and_thread (); |
| 932 | |
| 933 | for (i = 0; i < copies; ++i) |
| 934 | { |
| 935 | struct address_space *aspace; |
| 936 | struct program_space *pspace; |
| 937 | struct inferior *inf; |
| 938 | |
| 939 | /* If all inferiors share an address space on this system, this |
| 940 | doesn't really return a new address space; otherwise, it |
| 941 | really does. */ |
| 942 | aspace = maybe_new_address_space (); |
| 943 | pspace = add_program_space (aspace); |
| 944 | inf = add_inferior (0); |
| 945 | inf->pspace = pspace; |
| 946 | inf->aspace = pspace->aspace; |
| 947 | inf->gdbarch = orginf->gdbarch; |
| 948 | |
| 949 | /* If the original inferior had a user specified target |
| 950 | description, make the clone use it too. */ |
| 951 | if (target_desc_info_from_user_p (inf->tdesc_info)) |
| 952 | copy_inferior_target_desc_info (inf, orginf); |
| 953 | |
| 954 | printf_filtered (_("Added inferior %d.\n"), inf->num); |
| 955 | |
| 956 | set_current_inferior (inf); |
| 957 | switch_to_thread (null_ptid); |
| 958 | clone_program_space (pspace, orginf->pspace); |
| 959 | } |
| 960 | |
| 961 | do_cleanups (old_chain); |
| 962 | } |
| 963 | |
| 964 | /* Print notices when new inferiors are created and die. */ |
| 965 | static void |
| 966 | show_print_inferior_events (struct ui_file *file, int from_tty, |
| 967 | struct cmd_list_element *c, const char *value) |
| 968 | { |
| 969 | fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value); |
| 970 | } |
| 971 | |
| 972 | \f |
| 973 | |
| 974 | void |
| 975 | initialize_inferiors (void) |
| 976 | { |
| 977 | struct cmd_list_element *c = NULL; |
| 978 | |
| 979 | /* There's always one inferior. Note that this function isn't an |
| 980 | automatic _initialize_foo function, since other _initialize_foo |
| 981 | routines may need to install their per-inferior data keys. We |
| 982 | can only allocate an inferior when all those modules have done |
| 983 | that. Do this after initialize_progspace, due to the |
| 984 | current_program_space reference. */ |
| 985 | current_inferior_ = add_inferior (0); |
| 986 | current_inferior_->pspace = current_program_space; |
| 987 | current_inferior_->aspace = current_program_space->aspace; |
| 988 | /* The architecture will be initialized shortly, by |
| 989 | initialize_current_architecture. */ |
| 990 | |
| 991 | add_info ("inferiors", info_inferiors_command, |
| 992 | _("IDs of specified inferiors (all inferiors if no argument).")); |
| 993 | |
| 994 | c = add_com ("add-inferior", no_class, add_inferior_command, _("\ |
| 995 | Add a new inferior.\n\ |
| 996 | Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\ |
| 997 | N is the optional number of inferiors to add, default is 1.\n\ |
| 998 | FILENAME is the file name of the executable to use\n\ |
| 999 | as main program.")); |
| 1000 | set_cmd_completer (c, filename_completer); |
| 1001 | |
| 1002 | add_com ("remove-inferiors", no_class, remove_inferior_command, _("\ |
| 1003 | Remove inferior ID (or list of IDs).\n\ |
| 1004 | Usage: remove-inferiors ID...")); |
| 1005 | |
| 1006 | add_com ("clone-inferior", no_class, clone_inferior_command, _("\ |
| 1007 | Clone inferior ID.\n\ |
| 1008 | Usage: clone-inferior [-copies <N>] [ID]\n\ |
| 1009 | Add N copies of inferior ID. The new inferior has the same\n\ |
| 1010 | executable loaded as the copied inferior. If -copies is not specified,\n\ |
| 1011 | adds 1 copy. If ID is not specified, it is the current inferior\n\ |
| 1012 | that is cloned.")); |
| 1013 | |
| 1014 | add_cmd ("inferiors", class_run, detach_inferior_command, _("\ |
| 1015 | Detach from inferior ID (or list of IDS)."), |
| 1016 | &detachlist); |
| 1017 | |
| 1018 | add_cmd ("inferiors", class_run, kill_inferior_command, _("\ |
| 1019 | Kill inferior ID (or list of IDs)."), |
| 1020 | &killlist); |
| 1021 | |
| 1022 | add_cmd ("inferior", class_run, inferior_command, _("\ |
| 1023 | Use this command to switch between inferiors.\n\ |
| 1024 | The new inferior ID must be currently known."), |
| 1025 | &cmdlist); |
| 1026 | |
| 1027 | add_setshow_boolean_cmd ("inferior-events", no_class, |
| 1028 | &print_inferior_events, _("\ |
| 1029 | Set printing of inferior events (e.g., inferior start and exit)."), _("\ |
| 1030 | Show printing of inferior events (e.g., inferior start and exit)."), NULL, |
| 1031 | NULL, |
| 1032 | show_print_inferior_events, |
| 1033 | &setprintlist, &showprintlist); |
| 1034 | |
| 1035 | } |