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