]>
Commit | Line | Data |
---|---|---|
b77209e0 PA |
1 | /* Multi-process control for GDB, the GNU debugger. |
2 | ||
d01e8234 | 3 | Copyright (C) 2008-2025 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 | ||
6c95b8df | 20 | #include "exec.h" |
b77209e0 PA |
21 | #include "inferior.h" |
22 | #include "target.h" | |
23 | #include "command.h" | |
06da564e | 24 | #include "completer.h" |
5b9707eb | 25 | #include "cli/cli-cmds.h" |
b77209e0 PA |
26 | #include "gdbthread.h" |
27 | #include "ui-out.h" | |
76727919 | 28 | #include "observable.h" |
6c95b8df PA |
29 | #include "gdbcore.h" |
30 | #include "symfile.h" | |
268a13a5 | 31 | #include "gdbsupport/environ.h" |
c82c0b55 | 32 | #include "cli/cli-utils.h" |
6ecd4729 PA |
33 | #include "arch-utils.h" |
34 | #include "target-descriptions.h" | |
53cf95c3 | 35 | #include "target-connection.h" |
ebc73070 | 36 | #include "gdbsupport/gdb_tilde_expand.h" |
5ed8105e | 37 | #include "progspace-and-thread.h" |
7904e961 | 38 | #include "gdbsupport/buildargv.h" |
972f7a4b | 39 | #include "cli/cli-style.h" |
023c6d45 | 40 | #include "interps.h" |
b77209e0 | 41 | |
08bdefb5 | 42 | intrusive_list<inferior> inferior_list; |
b77209e0 PA |
43 | static int highest_inferior_num; |
44 | ||
f67c0c91 | 45 | /* See inferior.h. */ |
491144b5 | 46 | bool print_inferior_events = true; |
b77209e0 | 47 | |
3a3fd0fd PA |
48 | /* The Current Inferior. This is a strong reference. I.e., whenever |
49 | an inferior is the current inferior, its refcount is | |
50 | incremented. */ | |
51107df5 | 51 | static inferior_ref current_inferior_; |
6c95b8df | 52 | |
b77209e0 PA |
53 | struct inferior* |
54 | current_inferior (void) | |
55 | { | |
51107df5 | 56 | return current_inferior_.get (); |
6c95b8df PA |
57 | } |
58 | ||
59 | void | |
60 | set_current_inferior (struct inferior *inf) | |
61 | { | |
62 | /* There's always an inferior. */ | |
63 | gdb_assert (inf != NULL); | |
64 | ||
51107df5 | 65 | current_inferior_ = inferior_ref::new_reference (inf); |
6c95b8df PA |
66 | } |
67 | ||
089354bb SM |
68 | private_inferior::~private_inferior () = default; |
69 | ||
0550c955 | 70 | inferior::~inferior () |
b77209e0 | 71 | { |
740a579f AB |
72 | /* Before the inferior is deleted, all target_ops should be popped from |
73 | the target stack, this leaves just the dummy_target behind. If this | |
74 | is not done, then any target left in the target stack will be left | |
75 | with an artificially high reference count. As the dummy_target is | |
76 | still on the target stack then we are about to loose a reference to | |
77 | that target, leaving its reference count artificially high. However, | |
78 | this is not critical as the dummy_target is a singleton. */ | |
79 | gdb_assert (m_target_stack.top ()->stratum () == dummy_stratum); | |
80 | ||
4efeb0d3 | 81 | m_continuations.clear (); |
0550c955 PA |
82 | } |
83 | ||
84 | inferior::inferior (int pid_) | |
85 | : num (++highest_inferior_num), | |
86 | pid (pid_), | |
08b8a139 | 87 | environment (gdb_environ::from_host_environ ()) |
0550c955 | 88 | { |
5b6d1e4f | 89 | m_target_stack.push (get_dummy_target ()); |
b77209e0 PA |
90 | } |
91 | ||
a66f7298 SM |
92 | /* See inferior.h. */ |
93 | ||
94 | int | |
95 | inferior::unpush_target (struct target_ops *t) | |
96 | { | |
97 | /* If unpushing the process stratum target from the inferior while threads | |
98 | exist in the inferior, ensure that we don't leave any threads of the | |
99 | inferior in the target's "resumed with pending wait status" list. | |
100 | ||
101 | See also the comment in set_thread_exited. */ | |
102 | if (t->stratum () == process_stratum) | |
103 | { | |
104 | process_stratum_target *proc_target = as_process_stratum_target (t); | |
105 | ||
106 | for (thread_info *thread : this->non_exited_threads ()) | |
107 | proc_target->maybe_remove_resumed_with_pending_wait_status (thread); | |
108 | } | |
109 | ||
110 | return m_target_stack.unpush (t); | |
111 | } | |
112 | ||
c8181f70 AB |
113 | /* See inferior.h. */ |
114 | ||
115 | void | |
116 | inferior::unpush_target_and_assert (struct target_ops *target) | |
117 | { | |
118 | gdb_assert (current_inferior () == this); | |
119 | ||
120 | if (!unpush_target (target)) | |
121 | internal_error ("pop_all_targets couldn't find target %s\n", | |
122 | target->shortname ()); | |
123 | } | |
124 | ||
125 | /* See inferior.h. */ | |
126 | ||
127 | void | |
128 | inferior::pop_all_targets_above (enum strata stratum) | |
129 | { | |
130 | /* Unpushing a target might cause it to close. Some targets currently | |
131 | rely on the current_inferior being set for their ::close method, so we | |
132 | temporarily switch inferior now. */ | |
133 | scoped_restore_current_pspace_and_thread restore_pspace_and_thread; | |
134 | switch_to_inferior_no_thread (this); | |
135 | ||
136 | while (top_target ()->stratum () > stratum) | |
137 | unpush_target_and_assert (top_target ()); | |
138 | } | |
139 | ||
140 | /* See inferior.h. */ | |
141 | ||
142 | void | |
143 | inferior::pop_all_targets_at_and_above (enum strata stratum) | |
144 | { | |
145 | /* Unpushing a target might cause it to close. Some targets currently | |
146 | rely on the current_inferior being set for their ::close method, so we | |
147 | temporarily switch inferior now. */ | |
148 | scoped_restore_current_pspace_and_thread restore_pspace_and_thread; | |
149 | switch_to_inferior_no_thread (this); | |
150 | ||
151 | while (top_target ()->stratum () >= stratum) | |
152 | unpush_target_and_assert (top_target ()); | |
153 | } | |
154 | ||
05779d57 | 155 | void |
4e93ea6e | 156 | inferior::set_tty (std::string terminal_name) |
05779d57 | 157 | { |
4e93ea6e | 158 | m_terminal = std::move (terminal_name); |
05779d57 PA |
159 | } |
160 | ||
4e93ea6e | 161 | const std::string & |
05779d57 PA |
162 | inferior::tty () |
163 | { | |
4e93ea6e | 164 | return m_terminal; |
05779d57 PA |
165 | } |
166 | ||
7d3b43a1 TT |
167 | /* See inferior.h. */ |
168 | ||
169 | void | |
512ca2fc AB |
170 | inferior::set_args (gdb::array_view<char * const> args, |
171 | bool escape_shell_char) | |
7d3b43a1 | 172 | { |
512ca2fc | 173 | set_args (construct_inferior_arguments (args, escape_shell_char)); |
7d3b43a1 TT |
174 | } |
175 | ||
72c4529c SM |
176 | void |
177 | inferior::set_arch (gdbarch *arch) | |
178 | { | |
179 | gdb_assert (arch != nullptr); | |
180 | gdb_assert (gdbarch_initialized_p (arch)); | |
181 | m_gdbarch = arch; | |
d6bfbb52 SM |
182 | |
183 | process_stratum_target *proc_target = this->process_target (); | |
184 | if (proc_target != nullptr) | |
185 | registers_changed_ptid (proc_target, ptid_t (this->pid)); | |
72c4529c SM |
186 | } |
187 | ||
4efeb0d3 TBA |
188 | void |
189 | inferior::add_continuation (std::function<void ()> &&cont) | |
190 | { | |
191 | m_continuations.emplace_front (std::move (cont)); | |
192 | } | |
193 | ||
194 | void | |
195 | inferior::do_all_continuations () | |
196 | { | |
197 | while (!m_continuations.empty ()) | |
198 | { | |
199 | auto iter = m_continuations.begin (); | |
200 | (*iter) (); | |
201 | m_continuations.erase (iter); | |
202 | } | |
203 | } | |
204 | ||
023c6d45 SM |
205 | /* Notify interpreters and observers that inferior INF was added. */ |
206 | ||
207 | static void | |
208 | notify_inferior_added (inferior *inf) | |
209 | { | |
210 | interps_notify_inferior_added (inf); | |
211 | gdb::observers::inferior_added.notify (inf); | |
212 | } | |
213 | ||
b77209e0 PA |
214 | struct inferior * |
215 | add_inferior_silent (int pid) | |
216 | { | |
0550c955 | 217 | inferior *inf = new inferior (pid); |
b05b1202 | 218 | |
08bdefb5 | 219 | inferior_list.push_back (*inf); |
b77209e0 | 220 | |
023c6d45 | 221 | notify_inferior_added (inf); |
a79b8f6e | 222 | |
6c95b8df PA |
223 | if (pid != 0) |
224 | inferior_appeared (inf, pid); | |
a562dc8f | 225 | |
b77209e0 PA |
226 | return inf; |
227 | } | |
228 | ||
229 | struct inferior * | |
230 | add_inferior (int pid) | |
231 | { | |
232 | struct inferior *inf = add_inferior_silent (pid); | |
233 | ||
234 | if (print_inferior_events) | |
151bb4a5 PA |
235 | { |
236 | if (pid != 0) | |
6cb06a8c TT |
237 | gdb_printf (_("[New inferior %d (%s)]\n"), |
238 | inf->num, | |
239 | target_pid_to_str (ptid_t (pid)).c_str ()); | |
151bb4a5 | 240 | else |
6cb06a8c | 241 | gdb_printf (_("[New inferior %d]\n"), inf->num); |
151bb4a5 | 242 | } |
b77209e0 PA |
243 | |
244 | return inf; | |
245 | } | |
246 | ||
bf809310 PA |
247 | /* See inferior.h. */ |
248 | ||
3c8af02f SM |
249 | thread_info * |
250 | inferior::find_thread (ptid_t ptid) | |
251 | { | |
252 | auto it = this->ptid_thread_map.find (ptid); | |
253 | if (it != this->ptid_thread_map.end ()) | |
254 | return it->second; | |
255 | else | |
256 | return nullptr; | |
257 | } | |
258 | ||
259 | /* See inferior.h. */ | |
260 | ||
bf809310 | 261 | void |
604fe2d9 | 262 | inferior::clear_thread_list () |
bf809310 PA |
263 | { |
264 | thread_list.clear_and_dispose ([=] (thread_info *thr) | |
265 | { | |
604fe2d9 PA |
266 | threads_debug_printf ("deleting thread %s", |
267 | thr->ptid.to_string ().c_str ()); | |
9d7d58e7 | 268 | set_thread_exited (thr, {}, true /* silent */); |
bf809310 PA |
269 | if (thr->deletable ()) |
270 | delete thr; | |
271 | }); | |
922cc93d | 272 | ptid_thread_map.clear (); |
bf809310 PA |
273 | } |
274 | ||
2646bfa7 SM |
275 | /* Notify interpreters and observers that inferior INF was removed. */ |
276 | ||
277 | static void | |
278 | notify_inferior_removed (inferior *inf) | |
279 | { | |
280 | interps_notify_inferior_removed (inf); | |
281 | gdb::observers::inferior_removed.notify (inf); | |
282 | } | |
283 | ||
a79b8f6e | 284 | void |
08bdefb5 | 285 | delete_inferior (struct inferior *inf) |
b77209e0 | 286 | { |
604fe2d9 | 287 | inf->clear_thread_list (); |
4a92f99b | 288 | |
08bdefb5 PA |
289 | auto it = inferior_list.iterator_to (*inf); |
290 | inferior_list.erase (it); | |
7e1789f5 | 291 | |
2646bfa7 | 292 | notify_inferior_removed (inf); |
a79b8f6e | 293 | |
740a579f AB |
294 | /* Pop all targets now, this ensures that inferior::unpush is called |
295 | correctly. As pop_all_targets ends up making a temporary switch to | |
296 | inferior INF then we need to make this call before we delete the | |
297 | program space, which we do below. */ | |
298 | inf->pop_all_targets (); | |
299 | ||
7a41607e | 300 | /* If this program space is rendered useless, remove it. */ |
004eecfd | 301 | if (inf->pspace->empty ()) |
381ce63f | 302 | delete inf->pspace; |
ef3f321b | 303 | |
0550c955 | 304 | delete inf; |
ef3f321b SM |
305 | } |
306 | ||
d38086cc SM |
307 | /* Notify interpreters and observers that inferior INF disappeared. */ |
308 | ||
309 | static void | |
310 | notify_inferior_disappeared (inferior *inf) | |
311 | { | |
312 | interps_notify_inferior_disappeared (inf); | |
313 | gdb::observers::inferior_exit.notify (inf); | |
314 | } | |
315 | ||
9324bfea | 316 | /* See inferior.h. */ |
6c95b8df | 317 | |
9324bfea AB |
318 | void |
319 | exit_inferior (struct inferior *inf) | |
6c95b8df | 320 | { |
604fe2d9 | 321 | inf->clear_thread_list (); |
6c95b8df | 322 | |
d38086cc | 323 | notify_inferior_disappeared (inf); |
6c95b8df PA |
324 | |
325 | inf->pid = 0; | |
9ab8741a | 326 | inf->fake_pid_p = false; |
ef4a3395 PA |
327 | inf->priv = NULL; |
328 | ||
6c95b8df PA |
329 | if (inf->vfork_parent != NULL) |
330 | { | |
331 | inf->vfork_parent->vfork_child = NULL; | |
332 | inf->vfork_parent = NULL; | |
333 | } | |
68c9da30 PA |
334 | if (inf->vfork_child != NULL) |
335 | { | |
336 | inf->vfork_child->vfork_parent = NULL; | |
337 | inf->vfork_child = NULL; | |
338 | } | |
8cf64490 | 339 | |
30220b46 | 340 | inf->pending_detach = false; |
85046ae2 | 341 | /* Reset it. */ |
ee841dd8 | 342 | inf->control = inferior_control_state (NO_STOP_QUIETLY); |
66452beb PW |
343 | |
344 | /* Clear the register cache and the frame cache. */ | |
345 | registers_changed (); | |
346 | reinit_frame_cache (); | |
6c95b8df PA |
347 | } |
348 | ||
bc09b0c1 SM |
349 | /* See inferior.h. */ |
350 | ||
b77209e0 | 351 | void |
bc09b0c1 | 352 | detach_inferior (inferior *inf) |
b77209e0 | 353 | { |
9324bfea | 354 | /* Save the pid, since exit_inferior will reset it. */ |
bc09b0c1 | 355 | int pid = inf->pid; |
abbb1732 | 356 | |
9324bfea | 357 | exit_inferior (inf); |
b77209e0 PA |
358 | |
359 | if (print_inferior_events) | |
6cb06a8c TT |
360 | gdb_printf (_("[Inferior %d (%s) detached]\n"), |
361 | inf->num, | |
362 | target_pid_to_str (ptid_t (pid)).c_str ()); | |
b77209e0 PA |
363 | } |
364 | ||
0c613e17 SM |
365 | /* Notify interpreters and observers that inferior INF appeared. */ |
366 | ||
367 | static void | |
368 | notify_inferior_appeared (inferior *inf) | |
369 | { | |
370 | interps_notify_inferior_appeared (inf); | |
371 | gdb::observers::inferior_appeared.notify (inf); | |
372 | } | |
373 | ||
6c95b8df PA |
374 | void |
375 | inferior_appeared (struct inferior *inf, int pid) | |
376 | { | |
08036331 PA |
377 | /* If this is the first inferior with threads, reset the global |
378 | thread id. */ | |
873657b9 | 379 | delete_exited_threads (); |
08036331 PA |
380 | if (!any_thread_p ()) |
381 | init_thread_list (); | |
382 | ||
6c95b8df | 383 | inf->pid = pid; |
30220b46 | 384 | inf->has_exit_code = false; |
2ddf4301 | 385 | inf->exit_code = 0; |
6c95b8df | 386 | |
0c613e17 | 387 | notify_inferior_appeared (inf); |
6c95b8df PA |
388 | } |
389 | ||
6c95b8df | 390 | struct inferior * |
b77209e0 PA |
391 | find_inferior_id (int num) |
392 | { | |
08036331 | 393 | for (inferior *inf : all_inferiors ()) |
b77209e0 PA |
394 | if (inf->num == num) |
395 | return inf; | |
396 | ||
397 | return NULL; | |
398 | } | |
399 | ||
400 | struct inferior * | |
5b6d1e4f | 401 | find_inferior_pid (process_stratum_target *targ, int pid) |
b77209e0 | 402 | { |
6c95b8df PA |
403 | /* Looking for inferior pid == 0 is always wrong, and indicative of |
404 | a bug somewhere else. There may be more than one with pid == 0, | |
405 | for instance. */ | |
406 | gdb_assert (pid != 0); | |
407 | ||
5b6d1e4f | 408 | for (inferior *inf : all_inferiors (targ)) |
b77209e0 PA |
409 | if (inf->pid == pid) |
410 | return inf; | |
411 | ||
412 | return NULL; | |
413 | } | |
414 | ||
c9657e70 SM |
415 | /* See inferior.h */ |
416 | ||
417 | struct inferior * | |
5b6d1e4f | 418 | find_inferior_ptid (process_stratum_target *targ, ptid_t ptid) |
c9657e70 | 419 | { |
5b6d1e4f | 420 | return find_inferior_pid (targ, ptid.pid ()); |
c9657e70 SM |
421 | } |
422 | ||
32990ada | 423 | /* See inferior.h. */ |
6c95b8df PA |
424 | |
425 | struct inferior * | |
426 | find_inferior_for_program_space (struct program_space *pspace) | |
427 | { | |
08036331 | 428 | struct inferior *cur_inf = current_inferior (); |
32990ada | 429 | |
08036331 PA |
430 | if (cur_inf->pspace == pspace) |
431 | return cur_inf; | |
6c95b8df | 432 | |
08036331 PA |
433 | for (inferior *inf : all_inferiors ()) |
434 | if (inf->pspace == pspace) | |
435 | return inf; | |
6c95b8df PA |
436 | |
437 | return NULL; | |
438 | } | |
439 | ||
b77209e0 PA |
440 | int |
441 | have_inferiors (void) | |
442 | { | |
08036331 PA |
443 | for (inferior *inf ATTRIBUTE_UNUSED : all_non_exited_inferiors ()) |
444 | return 1; | |
6c95b8df PA |
445 | |
446 | return 0; | |
b77209e0 PA |
447 | } |
448 | ||
8020350c DB |
449 | /* Return the number of live inferiors. We account for the case |
450 | where an inferior might have a non-zero pid but no threads, as | |
451 | in the middle of a 'mourn' operation. */ | |
452 | ||
c35b1492 | 453 | int |
5b6d1e4f | 454 | number_of_live_inferiors (process_stratum_target *proc_target) |
c35b1492 | 455 | { |
8020350c | 456 | int num_inf = 0; |
6c95b8df | 457 | |
5b6d1e4f | 458 | for (inferior *inf : all_non_exited_inferiors (proc_target)) |
5018ce90 | 459 | if (inf->has_execution ()) |
08036331 PA |
460 | for (thread_info *tp ATTRIBUTE_UNUSED : inf->non_exited_threads ()) |
461 | { | |
462 | /* Found a live thread in this inferior, go to the next | |
463 | inferior. */ | |
464 | ++num_inf; | |
465 | break; | |
466 | } | |
cd2effb2 | 467 | |
8020350c DB |
468 | return num_inf; |
469 | } | |
470 | ||
471 | /* Return true if there is at least one live inferior. */ | |
472 | ||
473 | int | |
474 | have_live_inferiors (void) | |
475 | { | |
5b6d1e4f | 476 | return number_of_live_inferiors (NULL) > 0; |
6c95b8df PA |
477 | } |
478 | ||
bed8455c DE |
479 | /* Prune away any unused inferiors, and then prune away no longer used |
480 | program spaces. */ | |
6c95b8df PA |
481 | |
482 | void | |
483 | prune_inferiors (void) | |
484 | { | |
08bdefb5 | 485 | for (inferior *inf : all_inferiors_safe ()) |
6c95b8df | 486 | { |
08bdefb5 PA |
487 | if (!inf->deletable () |
488 | || !inf->removable | |
489 | || inf->pid != 0) | |
490 | continue; | |
6c95b8df | 491 | |
08bdefb5 | 492 | delete_inferior (inf); |
6c95b8df | 493 | } |
6c95b8df PA |
494 | } |
495 | ||
496 | /* Simply returns the count of inferiors. */ | |
497 | ||
498 | int | |
499 | number_of_inferiors (void) | |
500 | { | |
08036331 PA |
501 | auto rng = all_inferiors (); |
502 | return std::distance (rng.begin (), rng.end ()); | |
c35b1492 PA |
503 | } |
504 | ||
db2b9fdd PA |
505 | /* Converts an inferior process id to a string. Like |
506 | target_pid_to_str, but special cases the null process. */ | |
507 | ||
a068643d | 508 | static std::string |
db2b9fdd PA |
509 | inferior_pid_to_str (int pid) |
510 | { | |
511 | if (pid != 0) | |
f2907e49 | 512 | return target_pid_to_str (ptid_t (pid)); |
db2b9fdd PA |
513 | else |
514 | return _("<null>"); | |
515 | } | |
516 | ||
4034d0ff AT |
517 | /* See inferior.h. */ |
518 | ||
519 | void | |
520 | print_selected_inferior (struct ui_out *uiout) | |
521 | { | |
4034d0ff | 522 | struct inferior *inf = current_inferior (); |
9ad8c583 | 523 | const char *filename = inf->pspace->exec_filename (); |
112e8700 | 524 | |
53488a6e TS |
525 | if (filename == NULL) |
526 | filename = _("<noexec>"); | |
112e8700 SM |
527 | |
528 | uiout->message (_("[Switching to inferior %d [%s] (%s)]\n"), | |
a068643d | 529 | inf->num, inferior_pid_to_str (inf->pid).c_str (), filename); |
4034d0ff AT |
530 | } |
531 | ||
121b3efd PA |
532 | /* Helper for print_inferior. Returns the 'connection-id' string for |
533 | PROC_TARGET. */ | |
534 | ||
535 | static std::string | |
536 | uiout_field_connection (process_stratum_target *proc_target) | |
537 | { | |
538 | if (proc_target == NULL) | |
53cf95c3 | 539 | return {}; |
121b3efd PA |
540 | else |
541 | { | |
38c0c0ca | 542 | std::string conn_str = make_target_connection_string (proc_target); |
53cf95c3 AB |
543 | return string_printf ("%d (%s)", proc_target->connection_number, |
544 | conn_str.c_str ()); | |
121b3efd PA |
545 | } |
546 | } | |
547 | ||
b77209e0 PA |
548 | /* Prints the list of inferiors and their details on UIOUT. This is a |
549 | version of 'info_inferior_command' suitable for use from MI. | |
550 | ||
c82c0b55 MS |
551 | If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the |
552 | inferiors that should be printed. Otherwise, all inferiors are | |
553 | printed. */ | |
554 | ||
555 | static void | |
1d12d88f | 556 | print_inferior (struct ui_out *uiout, const char *requested_inferiors) |
b77209e0 | 557 | { |
8bb318c6 | 558 | int inf_count = 0; |
121b3efd | 559 | size_t connection_id_len = 20; |
b77209e0 | 560 | |
8bb318c6 | 561 | /* Compute number of inferiors we will print. */ |
08036331 | 562 | for (inferior *inf : all_inferiors ()) |
8bb318c6 | 563 | { |
c82c0b55 | 564 | if (!number_is_in_list (requested_inferiors, inf->num)) |
8bb318c6 TT |
565 | continue; |
566 | ||
121b3efd PA |
567 | std::string conn = uiout_field_connection (inf->process_target ()); |
568 | if (connection_id_len < conn.size ()) | |
569 | connection_id_len = conn.size (); | |
570 | ||
8bb318c6 TT |
571 | ++inf_count; |
572 | } | |
573 | ||
574 | if (inf_count == 0) | |
575 | { | |
112e8700 | 576 | uiout->message ("No inferiors.\n"); |
8bb318c6 TT |
577 | return; |
578 | } | |
579 | ||
121b3efd | 580 | ui_out_emit_table table_emitter (uiout, 5, inf_count, "inferiors"); |
112e8700 SM |
581 | uiout->table_header (1, ui_left, "current", ""); |
582 | uiout->table_header (4, ui_left, "number", "Num"); | |
583 | uiout->table_header (17, ui_left, "target-id", "Description"); | |
121b3efd PA |
584 | uiout->table_header (connection_id_len, ui_left, |
585 | "connection-id", "Connection"); | |
112e8700 | 586 | uiout->table_header (17, ui_left, "exec", "Executable"); |
b77209e0 | 587 | |
112e8700 | 588 | uiout->table_body (); |
d9ebdab7 TBA |
589 | |
590 | /* Restore the current thread after the loop because we switch the | |
591 | inferior in the loop. */ | |
592 | scoped_restore_current_pspace_and_thread restore_pspace_thread; | |
593 | inferior *current_inf = current_inferior (); | |
08036331 | 594 | for (inferior *inf : all_inferiors ()) |
b77209e0 | 595 | { |
c82c0b55 | 596 | if (!number_is_in_list (requested_inferiors, inf->num)) |
b77209e0 PA |
597 | continue; |
598 | ||
2e783024 | 599 | ui_out_emit_tuple tuple_emitter (uiout, NULL); |
b77209e0 | 600 | |
d9ebdab7 | 601 | if (inf == current_inf) |
112e8700 | 602 | uiout->field_string ("current", "*"); |
b77209e0 | 603 | else |
112e8700 | 604 | uiout->field_skip ("current"); |
b77209e0 | 605 | |
381befee | 606 | uiout->field_signed ("number", inf->num); |
6c95b8df | 607 | |
328d42d8 | 608 | /* Because target_pid_to_str uses the current inferior, |
d9ebdab7 TBA |
609 | switch the inferior. */ |
610 | switch_to_inferior_no_thread (inf); | |
611 | ||
112e8700 | 612 | uiout->field_string ("target-id", inferior_pid_to_str (inf->pid)); |
6c95b8df | 613 | |
121b3efd | 614 | std::string conn = uiout_field_connection (inf->process_target ()); |
8dd8c8d4 | 615 | uiout->field_string ("connection-id", conn); |
121b3efd | 616 | |
9ad8c583 SM |
617 | if (inf->pspace->exec_filename () != nullptr) |
618 | uiout->field_string ("exec", inf->pspace->exec_filename (), | |
972f7a4b | 619 | file_name_style.style ()); |
6c95b8df | 620 | else |
112e8700 | 621 | uiout->field_skip ("exec"); |
6c95b8df PA |
622 | |
623 | /* Print extra info that isn't really fit to always present in | |
624 | tabular form. Currently we print the vfork parent/child | |
625 | relationships, if any. */ | |
626 | if (inf->vfork_parent) | |
627 | { | |
112e8700 | 628 | uiout->text (_("\n\tis vfork child of inferior ")); |
381befee | 629 | uiout->field_signed ("vfork-parent", inf->vfork_parent->num); |
6c95b8df PA |
630 | } |
631 | if (inf->vfork_child) | |
632 | { | |
112e8700 | 633 | uiout->text (_("\n\tis vfork parent of inferior ")); |
381befee | 634 | uiout->field_signed ("vfork-child", inf->vfork_child->num); |
6c95b8df | 635 | } |
b77209e0 | 636 | |
112e8700 | 637 | uiout->text ("\n"); |
b77209e0 | 638 | } |
b77209e0 PA |
639 | } |
640 | ||
2277426b | 641 | static void |
e503b191 | 642 | detach_inferior_command (const char *args, int from_tty) |
2277426b | 643 | { |
2277426b | 644 | if (!args || !*args) |
af624141 | 645 | error (_("Requires argument (inferior id(s) to detach)")); |
2277426b | 646 | |
cfaa8f76 TBA |
647 | scoped_restore_current_thread restore_thread; |
648 | ||
bfd28288 PA |
649 | number_or_range_parser parser (args); |
650 | while (!parser.finished ()) | |
af624141 | 651 | { |
bfd28288 | 652 | int num = parser.get_number (); |
2277426b | 653 | |
00431a78 PA |
654 | inferior *inf = find_inferior_id (num); |
655 | if (inf == NULL) | |
af624141 MS |
656 | { |
657 | warning (_("Inferior ID %d not known."), num); | |
658 | continue; | |
659 | } | |
2277426b | 660 | |
00431a78 | 661 | if (inf->pid == 0) |
e3ae3c43 PP |
662 | { |
663 | warning (_("Inferior ID %d is not running."), num); | |
664 | continue; | |
665 | } | |
2277426b | 666 | |
00431a78 PA |
667 | thread_info *tp = any_thread_of_inferior (inf); |
668 | if (tp == NULL) | |
af624141 MS |
669 | { |
670 | warning (_("Inferior ID %d has no threads."), num); | |
671 | continue; | |
672 | } | |
2277426b | 673 | |
00431a78 | 674 | switch_to_thread (tp); |
2277426b | 675 | |
af624141 MS |
676 | detach_command (NULL, from_tty); |
677 | } | |
2277426b PA |
678 | } |
679 | ||
680 | static void | |
e503b191 | 681 | kill_inferior_command (const char *args, int from_tty) |
2277426b | 682 | { |
2277426b | 683 | if (!args || !*args) |
af624141 | 684 | error (_("Requires argument (inferior id(s) to kill)")); |
2277426b | 685 | |
cfaa8f76 TBA |
686 | scoped_restore_current_thread restore_thread; |
687 | ||
bfd28288 PA |
688 | number_or_range_parser parser (args); |
689 | while (!parser.finished ()) | |
af624141 | 690 | { |
bfd28288 | 691 | int num = parser.get_number (); |
2277426b | 692 | |
00431a78 PA |
693 | inferior *inf = find_inferior_id (num); |
694 | if (inf == NULL) | |
af624141 MS |
695 | { |
696 | warning (_("Inferior ID %d not known."), num); | |
697 | continue; | |
698 | } | |
2277426b | 699 | |
00431a78 | 700 | if (inf->pid == 0) |
e3ae3c43 PP |
701 | { |
702 | warning (_("Inferior ID %d is not running."), num); | |
703 | continue; | |
704 | } | |
2277426b | 705 | |
00431a78 PA |
706 | thread_info *tp = any_thread_of_inferior (inf); |
707 | if (tp == NULL) | |
af624141 MS |
708 | { |
709 | warning (_("Inferior ID %d has no threads."), num); | |
710 | continue; | |
711 | } | |
2277426b | 712 | |
00431a78 | 713 | switch_to_thread (tp); |
2277426b | 714 | |
af624141 MS |
715 | target_kill (); |
716 | } | |
2277426b PA |
717 | } |
718 | ||
db2d40f7 PA |
719 | /* See inferior.h. */ |
720 | ||
721 | void | |
722 | switch_to_inferior_no_thread (inferior *inf) | |
723 | { | |
724 | set_current_inferior (inf); | |
725 | switch_to_no_thread (); | |
726 | set_current_program_space (inf->pspace); | |
727 | } | |
728 | ||
348da456 SM |
729 | /* See regcache.h. */ |
730 | ||
6b09f134 | 731 | std::optional<scoped_restore_current_thread> |
348da456 SM |
732 | maybe_switch_inferior (inferior *inf) |
733 | { | |
6b09f134 | 734 | std::optional<scoped_restore_current_thread> maybe_restore_thread; |
348da456 SM |
735 | if (inf != current_inferior ()) |
736 | { | |
737 | maybe_restore_thread.emplace (); | |
738 | switch_to_inferior_no_thread (inf); | |
739 | } | |
740 | ||
741 | return maybe_restore_thread; | |
742 | } | |
743 | ||
2277426b | 744 | static void |
e503b191 | 745 | inferior_command (const char *args, int from_tty) |
2277426b | 746 | { |
6c95b8df PA |
747 | struct inferior *inf; |
748 | int num; | |
2277426b | 749 | |
2e3773ff | 750 | if (args == nullptr) |
2277426b | 751 | { |
2e3773ff LS |
752 | inf = current_inferior (); |
753 | gdb_assert (inf != nullptr); | |
9ad8c583 | 754 | const char *filename = inf->pspace->exec_filename (); |
2277426b | 755 | |
2e3773ff LS |
756 | if (filename == nullptr) |
757 | filename = _("<noexec>"); | |
6c95b8df | 758 | |
6cb06a8c TT |
759 | gdb_printf (_("[Current inferior is %d [%s] (%s)]\n"), |
760 | inf->num, inferior_pid_to_str (inf->pid).c_str (), | |
761 | filename); | |
2277426b | 762 | } |
6c95b8df PA |
763 | else |
764 | { | |
2e3773ff LS |
765 | num = parse_and_eval_long (args); |
766 | ||
767 | inf = find_inferior_id (num); | |
768 | if (inf == NULL) | |
769 | error (_("Inferior ID %d not known."), num); | |
2277426b | 770 | |
2e3773ff LS |
771 | if (inf->pid != 0) |
772 | { | |
773 | if (inf != current_inferior ()) | |
774 | { | |
775 | thread_info *tp = any_thread_of_inferior (inf); | |
776 | if (tp == NULL) | |
777 | error (_("Inferior has no threads.")); | |
778 | ||
779 | switch_to_thread (tp); | |
780 | } | |
781 | ||
77cd03e2 | 782 | notify_user_selected_context_changed |
2e3773ff LS |
783 | (USER_SELECTED_INFERIOR |
784 | | USER_SELECTED_THREAD | |
785 | | USER_SELECTED_FRAME); | |
786 | } | |
787 | else | |
788 | { | |
789 | switch_to_inferior_no_thread (inf); | |
790 | ||
77cd03e2 | 791 | notify_user_selected_context_changed |
2e3773ff LS |
792 | (USER_SELECTED_INFERIOR); |
793 | } | |
9747e37e TV |
794 | |
795 | /* Switching current inferior may have made one of the inferiors | |
796 | prunable, so prune it. */ | |
797 | prune_inferiors (); | |
2277426b PA |
798 | } |
799 | } | |
800 | ||
b77209e0 PA |
801 | /* Print information about currently known inferiors. */ |
802 | ||
803 | static void | |
1d12d88f | 804 | info_inferiors_command (const char *args, int from_tty) |
b77209e0 | 805 | { |
79a45e25 | 806 | print_inferior (current_uiout, args); |
b77209e0 PA |
807 | } |
808 | ||
6c95b8df PA |
809 | /* remove-inferior ID */ |
810 | ||
70221824 | 811 | static void |
0b39b52e | 812 | remove_inferior_command (const char *args, int from_tty) |
6c95b8df | 813 | { |
af624141 MS |
814 | if (args == NULL || *args == '\0') |
815 | error (_("Requires an argument (inferior id(s) to remove)")); | |
6c95b8df | 816 | |
bfd28288 PA |
817 | number_or_range_parser parser (args); |
818 | while (!parser.finished ()) | |
af624141 | 819 | { |
bfd28288 PA |
820 | int num = parser.get_number (); |
821 | struct inferior *inf = find_inferior_id (num); | |
6c95b8df | 822 | |
af624141 MS |
823 | if (inf == NULL) |
824 | { | |
825 | warning (_("Inferior ID %d not known."), num); | |
826 | continue; | |
827 | } | |
828 | ||
3a3fd0fd | 829 | if (!inf->deletable ()) |
af624141 | 830 | { |
eb2332d7 | 831 | warning (_("Can not remove current inferior %d."), num); |
af624141 MS |
832 | continue; |
833 | } | |
8fa067af | 834 | |
af624141 MS |
835 | if (inf->pid != 0) |
836 | { | |
837 | warning (_("Can not remove active inferior %d."), num); | |
838 | continue; | |
839 | } | |
6c95b8df | 840 | |
7a41607e | 841 | delete_inferior (inf); |
af624141 | 842 | } |
6c95b8df PA |
843 | } |
844 | ||
a79b8f6e VP |
845 | struct inferior * |
846 | add_inferior_with_spaces (void) | |
847 | { | |
a79b8f6e VP |
848 | struct program_space *pspace; |
849 | struct inferior *inf; | |
850 | ||
851 | /* If all inferiors share an address space on this system, this | |
852 | doesn't really return a new address space; otherwise, it | |
853 | really does. */ | |
f9582a22 | 854 | pspace = new program_space (maybe_new_address_space ()); |
a79b8f6e VP |
855 | inf = add_inferior (0); |
856 | inf->pspace = pspace; | |
857 | inf->aspace = pspace->aspace; | |
858 | ||
6ecd4729 PA |
859 | /* Setup the inferior's initial arch, based on information obtained |
860 | from the global "set ..." options. */ | |
b447dd03 | 861 | gdbarch_info info; |
27b1f19f | 862 | inf->set_arch (gdbarch_find_by_info (info)); |
6ecd4729 PA |
863 | /* The "set ..." options reject invalid settings, so we should |
864 | always have a valid arch by now. */ | |
27b1f19f | 865 | gdb_assert (inf->arch () != nullptr); |
6ecd4729 | 866 | |
a79b8f6e VP |
867 | return inf; |
868 | } | |
6c95b8df | 869 | |
d43bd54d | 870 | /* See inferior.h. */ |
5b6d1e4f | 871 | |
d43bd54d | 872 | void |
5b6d1e4f PA |
873 | switch_to_inferior_and_push_target (inferior *new_inf, |
874 | bool no_connection, inferior *org_inf) | |
875 | { | |
876 | process_stratum_target *proc_target = org_inf->process_target (); | |
877 | ||
878 | /* Switch over temporarily, while reading executable and | |
879 | symbols. */ | |
880 | switch_to_inferior_no_thread (new_inf); | |
881 | ||
882 | /* Reuse the target for new inferior. */ | |
883 | if (!no_connection && proc_target != NULL) | |
121b3efd | 884 | { |
02980c56 | 885 | new_inf->push_target (proc_target); |
53cf95c3 AB |
886 | gdb_printf (_("Added inferior %d on connection %d (%s)\n"), |
887 | new_inf->num, | |
888 | proc_target->connection_number, | |
889 | make_target_connection_string (proc_target).c_str ()); | |
121b3efd PA |
890 | } |
891 | else | |
6cb06a8c | 892 | gdb_printf (_("Added inferior %d\n"), new_inf->num); |
5b6d1e4f PA |
893 | } |
894 | ||
ebc73070 AB |
895 | /* Option values for the "add-inferior" command. */ |
896 | ||
897 | struct add_inferior_opts | |
898 | { | |
899 | /* When true the new inferiors are started without a connection. */ | |
900 | bool no_connection = false; | |
901 | ||
902 | /* The number of new inferiors to add. */ | |
903 | unsigned int num_copies = 1; | |
904 | ||
905 | /* When non-empty, this is the executable for the new inferiors. */ | |
906 | std::string exec_filename; | |
907 | }; | |
908 | ||
909 | /* Option definitions for the "add-inferior" command. */ | |
910 | ||
911 | static const gdb::option::option_def add_inferior_option_defs[] = { | |
912 | gdb::option::uinteger_option_def<add_inferior_opts> { | |
913 | "copies", | |
914 | [] (add_inferior_opts *opts) { return &opts->num_copies; }, | |
915 | (show_value_ftype *) nullptr, /* show_cmd_cb */ | |
916 | N_("\ | |
917 | The number of inferiors to add. The default is 1."), | |
918 | }, | |
919 | ||
920 | gdb::option::filename_option_def<add_inferior_opts> { | |
921 | "exec", | |
922 | [] (add_inferior_opts *opts) { return &opts->exec_filename; }, | |
923 | nullptr, /* show_cmd_cb */ | |
924 | N_("\ | |
925 | FILENAME is the file name of the executable to use as the\n\ | |
926 | main program."), | |
927 | }, | |
928 | ||
929 | gdb::option::flag_option_def<add_inferior_opts> { | |
930 | "no-connection", | |
931 | [] (add_inferior_opts *opts) { return &opts->no_connection; }, | |
932 | N_("\ | |
933 | If specified, the new inferiors begin with no target connection.\n\ | |
934 | Without this flag the new inferiors inherit the current inferior's\n\ | |
935 | connection."), | |
936 | }, | |
937 | }; | |
938 | ||
939 | /* Create the option_def_group for the "add-inferior" command. */ | |
940 | ||
941 | static inline gdb::option::option_def_group | |
942 | make_add_inferior_options_def_group (add_inferior_opts *opts) | |
943 | { | |
944 | return {{add_inferior_option_defs}, opts}; | |
945 | } | |
946 | ||
947 | /* Completion for the "add-inferior" command. */ | |
948 | ||
949 | static void | |
950 | add_inferior_completer (struct cmd_list_element *cmd, | |
951 | completion_tracker &tracker, | |
952 | const char *text, const char * /* word */) | |
953 | { | |
954 | /* The only completion offered is for the command options. */ | |
955 | const auto group = make_add_inferior_options_def_group (nullptr); | |
956 | gdb::option::complete_options | |
957 | (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group); | |
958 | } | |
959 | ||
5b6d1e4f | 960 | /* add-inferior [-copies N] [-exec FILENAME] [-no-connection] */ |
6c95b8df | 961 | |
70221824 | 962 | static void |
0b39b52e | 963 | add_inferior_command (const char *args, int from_tty) |
6c95b8df | 964 | { |
ebc73070 AB |
965 | add_inferior_opts opts; |
966 | const auto group = make_add_inferior_options_def_group (&opts); | |
967 | gdb::option::process_options | |
968 | (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group); | |
969 | ||
970 | /* If an executable was given then perform tilde expansion. */ | |
971 | if (!opts.exec_filename.empty ()) | |
972 | opts.exec_filename = gdb_tilde_expand (opts.exec_filename); | |
6c95b8df | 973 | |
ebc73070 | 974 | symfile_add_flags add_flags = 0; |
ecf45d2c SL |
975 | if (from_tty) |
976 | add_flags |= SYMFILE_VERBOSE; | |
977 | ||
5b6d1e4f PA |
978 | inferior *orginf = current_inferior (); |
979 | ||
5ed8105e | 980 | scoped_restore_current_pspace_and_thread restore_pspace_thread; |
6c95b8df | 981 | |
ebc73070 | 982 | for (unsigned int i = 0; i < opts.num_copies; ++i) |
6c95b8df | 983 | { |
5b6d1e4f | 984 | inferior *inf = add_inferior_with_spaces (); |
6c95b8df | 985 | |
ebc73070 | 986 | switch_to_inferior_and_push_target (inf, opts.no_connection, orginf); |
6c95b8df | 987 | |
ebc73070 | 988 | if (!opts.exec_filename.empty ()) |
6c95b8df | 989 | { |
ebc73070 AB |
990 | const char *exec = opts.exec_filename.c_str (); |
991 | exec_file_attach (exec, from_tty); | |
992 | symbol_file_add_main (exec, add_flags); | |
6c95b8df PA |
993 | } |
994 | } | |
6c95b8df PA |
995 | } |
996 | ||
ebc73070 AB |
997 | /* Option values for the "clone-inferior" command. */ |
998 | ||
999 | struct clone_inferior_opts | |
1000 | { | |
1001 | /* When true the new inferiors are started without a connection. */ | |
1002 | bool no_connection = false; | |
1003 | ||
1004 | /* The number of new inferiors to create by cloning. */ | |
1005 | unsigned int num_copies = 1; | |
1006 | }; | |
1007 | ||
1008 | ||
1009 | /* Option definitions for the "clone-inferior" command. */ | |
1010 | ||
1011 | static const gdb::option::option_def clone_inferior_option_defs[] = { | |
1012 | gdb::option::uinteger_option_def<clone_inferior_opts> { | |
1013 | "copies", | |
1014 | [] (clone_inferior_opts *opts) { return &opts->num_copies; }, | |
1015 | (show_value_ftype *) nullptr, /* show_cmd_cb */ | |
1016 | N_("\ | |
1017 | The number of copies of inferior ID to create. The default is 1."), | |
1018 | }, | |
1019 | ||
1020 | gdb::option::flag_option_def<clone_inferior_opts> { | |
1021 | "no-connection", | |
1022 | [] (clone_inferior_opts *opts) { return &opts->no_connection; }, | |
1023 | N_("\ | |
1024 | If specified, the new inferiors begin with no target connection.\n\ | |
1025 | Without this flag the new inferiors to inherit the copied inferior's\n\ | |
1026 | connection."), | |
1027 | }, | |
1028 | }; | |
1029 | ||
1030 | /* Create the option_def_group for the "clone-inferior" command. */ | |
1031 | ||
1032 | static inline gdb::option::option_def_group | |
1033 | make_clone_inferior_options_def_group (clone_inferior_opts *opts) | |
1034 | { | |
1035 | return {{clone_inferior_option_defs}, opts}; | |
1036 | } | |
1037 | ||
1038 | /* Completion for the "clone-inferior" command. */ | |
1039 | ||
1040 | static void | |
1041 | clone_inferior_completer (struct cmd_list_element *cmd, | |
1042 | completion_tracker &tracker, | |
1043 | const char *text, const char * /* word */) | |
1044 | { | |
1045 | /* The only completion offered is for the command options. */ | |
1046 | const auto group = make_clone_inferior_options_def_group (nullptr); | |
1047 | gdb::option::complete_options | |
1048 | (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group); | |
1049 | } | |
1050 | ||
1051 | /* clone-inferior [-copies N] [-no-connection] [ID] */ | |
6c95b8df | 1052 | |
70221824 | 1053 | static void |
0b39b52e | 1054 | clone_inferior_command (const char *args, int from_tty) |
6c95b8df | 1055 | { |
ebc73070 AB |
1056 | clone_inferior_opts opts; |
1057 | const auto group = make_clone_inferior_options_def_group (&opts); | |
1058 | gdb::option::process_options | |
1059 | (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group); | |
6c95b8df | 1060 | |
ebc73070 AB |
1061 | struct inferior *orginf = NULL; |
1062 | if (args != nullptr && *args != '\0') | |
6c95b8df | 1063 | { |
ebc73070 | 1064 | gdb_argv argv (args); |
6c95b8df | 1065 | |
ebc73070 AB |
1066 | gdb_assert (argv.count () > 0); |
1067 | ||
1068 | for (const char *arg : argv) | |
6c95b8df | 1069 | { |
ebc73070 | 1070 | if (orginf == nullptr) |
6c95b8df | 1071 | { |
ebc73070 AB |
1072 | /* The first non-option argument specifies the number of the |
1073 | inferior to clone. */ | |
1074 | int num = parse_and_eval_long (arg); | |
1075 | orginf = find_inferior_id (num); | |
1076 | ||
1077 | if (orginf == nullptr) | |
1078 | error (_("Inferior ID %d not known."), num); | |
6c95b8df PA |
1079 | } |
1080 | else | |
ebc73070 | 1081 | error (_("Unexpected argument: %s."), arg); |
6c95b8df PA |
1082 | } |
1083 | } | |
ebc73070 AB |
1084 | else |
1085 | { | |
1086 | /* If no inferior id was specified, then the user wants to clone the | |
1087 | current inferior. */ | |
1088 | orginf = current_inferior (); | |
1089 | } | |
6c95b8df | 1090 | |
ebc73070 | 1091 | gdb_assert (orginf != nullptr); |
6c95b8df | 1092 | |
5ed8105e | 1093 | scoped_restore_current_pspace_and_thread restore_pspace_thread; |
6c95b8df | 1094 | |
ebc73070 | 1095 | for (unsigned int i = 0; i < opts.num_copies; ++i) |
6c95b8df | 1096 | { |
6c95b8df PA |
1097 | struct program_space *pspace; |
1098 | struct inferior *inf; | |
1099 | ||
1100 | /* If all inferiors share an address space on this system, this | |
1101 | doesn't really return a new address space; otherwise, it | |
1102 | really does. */ | |
f9582a22 | 1103 | pspace = new program_space (maybe_new_address_space ()); |
6c95b8df PA |
1104 | inf = add_inferior (0); |
1105 | inf->pspace = pspace; | |
1106 | inf->aspace = pspace->aspace; | |
27b1f19f | 1107 | inf->set_arch (orginf->arch ()); |
6ecd4729 | 1108 | |
ebc73070 | 1109 | switch_to_inferior_and_push_target (inf, opts.no_connection, orginf); |
5b6d1e4f | 1110 | |
6ecd4729 PA |
1111 | /* If the original inferior had a user specified target |
1112 | description, make the clone use it too. */ | |
5a19bfd6 | 1113 | if (inf->tdesc_info.from_user_p ()) |
57768366 | 1114 | inf->tdesc_info = orginf->tdesc_info; |
6c95b8df | 1115 | |
6c95b8df | 1116 | clone_program_space (pspace, orginf->pspace); |
003aae07 LS |
1117 | |
1118 | /* Copy properties from the original inferior to the new one. */ | |
1119 | inf->set_args (orginf->args ()); | |
1120 | inf->set_cwd (orginf->cwd ()); | |
1121 | inf->set_tty (orginf->tty ()); | |
1122 | for (const std::string &set_var : orginf->environment.user_set_env ()) | |
1123 | { | |
1124 | /* set_var has the form NAME=value. Split on the first '='. */ | |
1125 | const std::string::size_type pos = set_var.find ('='); | |
1126 | gdb_assert (pos != std::string::npos); | |
1127 | const std::string varname = set_var.substr (0, pos); | |
1128 | inf->environment.set | |
1129 | (varname.c_str (), orginf->environment.get (varname.c_str ())); | |
1130 | } | |
1131 | for (const std::string &unset_var | |
1132 | : orginf->environment.user_unset_env ()) | |
1133 | inf->environment.unset (unset_var.c_str ()); | |
c6ae6045 SM |
1134 | |
1135 | gdb::observers::inferior_cloned.notify (orginf, inf); | |
6c95b8df | 1136 | } |
6c95b8df PA |
1137 | } |
1138 | ||
b77209e0 PA |
1139 | /* Print notices when new inferiors are created and die. */ |
1140 | static void | |
1141 | show_print_inferior_events (struct ui_file *file, int from_tty, | |
1142 | struct cmd_list_element *c, const char *value) | |
1143 | { | |
6cb06a8c | 1144 | gdb_printf (file, _("Printing of inferior events is %s.\n"), value); |
b77209e0 PA |
1145 | } |
1146 | ||
e3940304 PA |
1147 | /* Return a new value for the selected inferior's id. */ |
1148 | ||
1149 | static struct value * | |
1150 | inferior_id_make_value (struct gdbarch *gdbarch, struct internalvar *var, | |
1151 | void *ignore) | |
1152 | { | |
1153 | struct inferior *inf = current_inferior (); | |
1154 | ||
1155 | return value_from_longest (builtin_type (gdbarch)->builtin_int, inf->num); | |
1156 | } | |
1157 | ||
1158 | /* Implementation of `$_inferior' variable. */ | |
1159 | ||
1160 | static const struct internalvar_funcs inferior_funcs = | |
1161 | { | |
1162 | inferior_id_make_value, | |
1163 | NULL, | |
e3940304 PA |
1164 | }; |
1165 | ||
7b21ae94 | 1166 | /* See inferior.h. */ |
6c95b8df | 1167 | |
6c95b8df | 1168 | void |
7b21ae94 | 1169 | initialize_inferiors () |
6c95b8df | 1170 | { |
06da564e EZ |
1171 | struct cmd_list_element *c = NULL; |
1172 | ||
6c95b8df PA |
1173 | /* There's always one inferior. Note that this function isn't an |
1174 | automatic _initialize_foo function, since other _initialize_foo | |
1175 | routines may need to install their per-inferior data keys. We | |
1176 | can only allocate an inferior when all those modules have done | |
1177 | that. Do this after initialize_progspace, due to the | |
1178 | current_program_space reference. */ | |
51107df5 | 1179 | set_current_inferior (add_inferior_silent (0)); |
6c95b8df PA |
1180 | current_inferior_->pspace = current_program_space; |
1181 | current_inferior_->aspace = current_program_space->aspace; | |
6ecd4729 PA |
1182 | /* The architecture will be initialized shortly, by |
1183 | initialize_current_architecture. */ | |
6c95b8df | 1184 | |
a3c25011 TT |
1185 | add_info ("inferiors", info_inferiors_command, |
1186 | _("Print a list of inferiors being managed.\n\ | |
1187 | Usage: info inferiors [ID]...\n\ | |
1188 | If IDs are specified, the list is limited to just those inferiors.\n\ | |
1189 | By default all inferiors are displayed.")); | |
b77209e0 | 1190 | |
ebc73070 AB |
1191 | const auto add_inf_opts = make_add_inferior_options_def_group (nullptr); |
1192 | static std::string add_inferior_command_help | |
1193 | = gdb::option::build_help (_("\ | |
6c95b8df | 1194 | Add a new inferior.\n\ |
ebc73070 AB |
1195 | Usage: add-inferior [-copies NUMBER] [-exec FILENAME] [-no-connection]\n\ |
1196 | \n\ | |
1197 | Options:\n\ | |
1198 | %OPTIONS%"), add_inf_opts); | |
1199 | c = add_com ("add-inferior", no_class, add_inferior_command, | |
1200 | add_inferior_command_help.c_str ()); | |
1201 | set_cmd_completer_handle_brkchars (c, add_inferior_completer); | |
6c95b8df | 1202 | |
af624141 MS |
1203 | add_com ("remove-inferiors", no_class, remove_inferior_command, _("\ |
1204 | Remove inferior ID (or list of IDs).\n\ | |
1205 | Usage: remove-inferiors ID...")); | |
6c95b8df | 1206 | |
ebc73070 AB |
1207 | const auto clone_inf_opts = make_clone_inferior_options_def_group (nullptr); |
1208 | static std::string clone_inferior_command_help | |
1209 | = gdb::option::build_help (_("\ | |
1210 | Clone an existing inferior.\n\ | |
1211 | Usage: clone-inferior [-copies NUMBER] [-no-connection] [ID]\n\ | |
1212 | ID is the inferior number to clone, this can be found with the\n\ | |
1213 | 'info inferiors' command. If no ID is specified, then the current\n\ | |
1214 | inferior is cloned.\n\ | |
1215 | \n\ | |
1216 | Options:\n\ | |
1217 | %OPTIONS%"), clone_inf_opts); | |
1218 | c = add_com ("clone-inferior", no_class, clone_inferior_command, | |
1219 | clone_inferior_command_help.c_str ()); | |
1220 | set_cmd_completer_handle_brkchars (c, clone_inferior_completer); | |
2277426b | 1221 | |
af624141 | 1222 | add_cmd ("inferiors", class_run, detach_inferior_command, _("\ |
a3c25011 TT |
1223 | Detach from inferior ID (or list of IDS).\n\ |
1224 | Usage; detach inferiors ID..."), | |
2277426b PA |
1225 | &detachlist); |
1226 | ||
af624141 | 1227 | add_cmd ("inferiors", class_run, kill_inferior_command, _("\ |
a3c25011 TT |
1228 | Kill inferior ID (or list of IDs).\n\ |
1229 | Usage: kill inferiors ID..."), | |
2277426b PA |
1230 | &killlist); |
1231 | ||
1232 | add_cmd ("inferior", class_run, inferior_command, _("\ | |
1233 | Use this command to switch between inferiors.\n\ | |
a3c25011 | 1234 | Usage: inferior ID\n\ |
2277426b PA |
1235 | The new inferior ID must be currently known."), |
1236 | &cmdlist); | |
6c95b8df PA |
1237 | |
1238 | add_setshow_boolean_cmd ("inferior-events", no_class, | |
dda83cd7 | 1239 | &print_inferior_events, _("\ |
e3abbe7e PW |
1240 | Set printing of inferior events (such as inferior start and exit)."), _("\ |
1241 | Show printing of inferior events (such as inferior start and exit)."), NULL, | |
dda83cd7 SM |
1242 | NULL, |
1243 | show_print_inferior_events, | |
1244 | &setprintlist, &showprintlist); | |
6c95b8df | 1245 | |
e3940304 | 1246 | create_internalvar_type_lazy ("_inferior", &inferior_funcs, NULL); |
b77209e0 | 1247 | } |