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