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