inferior.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. /* Multi-process control for GDB, the GNU debugger.
  2. Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "exec.h"
  16. #include "inferior.h"
  17. #include "target.h"
  18. #include "command.h"
  19. #include "completer.h"
  20. #include "gdbcmd.h"
  21. #include "gdbthread.h"
  22. #include "ui-out.h"
  23. #include "observable.h"
  24. #include "gdbcore.h"
  25. #include "symfile.h"
  26. #include "gdbsupport/environ.h"
  27. #include "cli/cli-utils.h"
  28. #include "arch-utils.h"
  29. #include "target-descriptions.h"
  30. #include "readline/tilde.h"
  31. #include "progspace-and-thread.h"
  32. #include "gdbsupport/buildargv.h"
  33. #include "cli/cli-style.h"
  34. /* Keep a registry of per-inferior data-pointers required by other GDB
  35. modules. */
  36. DEFINE_REGISTRY (inferior, REGISTRY_ACCESS_FIELD)
  37. intrusive_list<inferior> inferior_list;
  38. static int highest_inferior_num;
  39. /* See inferior.h. */
  40. bool print_inferior_events = true;
  41. /* The Current Inferior. This is a strong reference. I.e., whenever
  42. an inferior is the current inferior, its refcount is
  43. incremented. */
  44. static inferior_ref current_inferior_;
  45. struct inferior*
  46. current_inferior (void)
  47. {
  48. return current_inferior_.get ();
  49. }
  50. void
  51. set_current_inferior (struct inferior *inf)
  52. {
  53. /* There's always an inferior. */
  54. gdb_assert (inf != NULL);
  55. current_inferior_ = inferior_ref::new_reference (inf);
  56. }
  57. private_inferior::~private_inferior () = default;
  58. inferior::~inferior ()
  59. {
  60. inferior *inf = this;
  61. m_continuations.clear ();
  62. inferior_free_data (inf);
  63. target_desc_info_free (inf->tdesc_info);
  64. }
  65. inferior::inferior (int pid_)
  66. : num (++highest_inferior_num),
  67. pid (pid_),
  68. environment (gdb_environ::from_host_environ ()),
  69. registry_data ()
  70. {
  71. inferior_alloc_data (this);
  72. m_target_stack.push (get_dummy_target ());
  73. }
  74. /* See inferior.h. */
  75. int
  76. inferior::unpush_target (struct target_ops *t)
  77. {
  78. /* If unpushing the process stratum target from the inferior while threads
  79. exist in the inferior, ensure that we don't leave any threads of the
  80. inferior in the target's "resumed with pending wait status" list.
  81. See also the comment in set_thread_exited. */
  82. if (t->stratum () == process_stratum)
  83. {
  84. process_stratum_target *proc_target = as_process_stratum_target (t);
  85. for (thread_info *thread : this->non_exited_threads ())
  86. proc_target->maybe_remove_resumed_with_pending_wait_status (thread);
  87. }
  88. return m_target_stack.unpush (t);
  89. }
  90. void
  91. inferior::set_tty (std::string terminal_name)
  92. {
  93. m_terminal = std::move (terminal_name);
  94. }
  95. const std::string &
  96. inferior::tty ()
  97. {
  98. return m_terminal;
  99. }
  100. void
  101. inferior::add_continuation (std::function<void ()> &&cont)
  102. {
  103. m_continuations.emplace_front (std::move (cont));
  104. }
  105. void
  106. inferior::do_all_continuations ()
  107. {
  108. while (!m_continuations.empty ())
  109. {
  110. auto iter = m_continuations.begin ();
  111. (*iter) ();
  112. m_continuations.erase (iter);
  113. }
  114. }
  115. struct inferior *
  116. add_inferior_silent (int pid)
  117. {
  118. inferior *inf = new inferior (pid);
  119. inferior_list.push_back (*inf);
  120. gdb::observers::inferior_added.notify (inf);
  121. if (pid != 0)
  122. inferior_appeared (inf, pid);
  123. return inf;
  124. }
  125. struct inferior *
  126. add_inferior (int pid)
  127. {
  128. struct inferior *inf = add_inferior_silent (pid);
  129. if (print_inferior_events)
  130. {
  131. if (pid != 0)
  132. gdb_printf (_("[New inferior %d (%s)]\n"),
  133. inf->num,
  134. target_pid_to_str (ptid_t (pid)).c_str ());
  135. else
  136. gdb_printf (_("[New inferior %d]\n"), inf->num);
  137. }
  138. return inf;
  139. }
  140. /* See inferior.h. */
  141. void
  142. inferior::clear_thread_list (bool silent)
  143. {
  144. thread_list.clear_and_dispose ([=] (thread_info *thr)
  145. {
  146. threads_debug_printf ("deleting thread %s, silent = %d",
  147. thr->ptid.to_string ().c_str (), silent);
  148. set_thread_exited (thr, silent);
  149. if (thr->deletable ())
  150. delete thr;
  151. });
  152. ptid_thread_map.clear ();
  153. }
  154. void
  155. delete_inferior (struct inferior *inf)
  156. {
  157. inf->clear_thread_list (true);
  158. auto it = inferior_list.iterator_to (*inf);
  159. inferior_list.erase (it);
  160. gdb::observers::inferior_removed.notify (inf);
  161. /* If this program space is rendered useless, remove it. */
  162. if (inf->pspace->empty ())
  163. delete inf->pspace;
  164. delete inf;
  165. }
  166. /* If SILENT then be quiet -- don't announce a inferior exit, or the
  167. exit of its threads. */
  168. static void
  169. exit_inferior_1 (struct inferior *inf, int silent)
  170. {
  171. inf->clear_thread_list (silent);
  172. gdb::observers::inferior_exit.notify (inf);
  173. inf->pid = 0;
  174. inf->fake_pid_p = false;
  175. inf->priv = NULL;
  176. if (inf->vfork_parent != NULL)
  177. {
  178. inf->vfork_parent->vfork_child = NULL;
  179. inf->vfork_parent = NULL;
  180. }
  181. if (inf->vfork_child != NULL)
  182. {
  183. inf->vfork_child->vfork_parent = NULL;
  184. inf->vfork_child = NULL;
  185. }
  186. inf->pending_detach = 0;
  187. /* Reset it. */
  188. inf->control = inferior_control_state (NO_STOP_QUIETLY);
  189. /* Clear the register cache and the frame cache. */
  190. registers_changed ();
  191. reinit_frame_cache ();
  192. }
  193. void
  194. exit_inferior (inferior *inf)
  195. {
  196. exit_inferior_1 (inf, 0);
  197. }
  198. void
  199. exit_inferior_silent (inferior *inf)
  200. {
  201. exit_inferior_1 (inf, 1);
  202. }
  203. /* See inferior.h. */
  204. void
  205. detach_inferior (inferior *inf)
  206. {
  207. /* Save the pid, since exit_inferior_1 will reset it. */
  208. int pid = inf->pid;
  209. exit_inferior_1 (inf, 0);
  210. if (print_inferior_events)
  211. gdb_printf (_("[Inferior %d (%s) detached]\n"),
  212. inf->num,
  213. target_pid_to_str (ptid_t (pid)).c_str ());
  214. }
  215. void
  216. inferior_appeared (struct inferior *inf, int pid)
  217. {
  218. /* If this is the first inferior with threads, reset the global
  219. thread id. */
  220. delete_exited_threads ();
  221. if (!any_thread_p ())
  222. init_thread_list ();
  223. inf->pid = pid;
  224. inf->has_exit_code = 0;
  225. inf->exit_code = 0;
  226. gdb::observers::inferior_appeared.notify (inf);
  227. }
  228. struct inferior *
  229. find_inferior_id (int num)
  230. {
  231. for (inferior *inf : all_inferiors ())
  232. if (inf->num == num)
  233. return inf;
  234. return NULL;
  235. }
  236. struct inferior *
  237. find_inferior_pid (process_stratum_target *targ, int pid)
  238. {
  239. /* Looking for inferior pid == 0 is always wrong, and indicative of
  240. a bug somewhere else. There may be more than one with pid == 0,
  241. for instance. */
  242. gdb_assert (pid != 0);
  243. for (inferior *inf : all_inferiors (targ))
  244. if (inf->pid == pid)
  245. return inf;
  246. return NULL;
  247. }
  248. /* See inferior.h */
  249. struct inferior *
  250. find_inferior_ptid (process_stratum_target *targ, ptid_t ptid)
  251. {
  252. return find_inferior_pid (targ, ptid.pid ());
  253. }
  254. /* See inferior.h. */
  255. struct inferior *
  256. find_inferior_for_program_space (struct program_space *pspace)
  257. {
  258. struct inferior *cur_inf = current_inferior ();
  259. if (cur_inf->pspace == pspace)
  260. return cur_inf;
  261. for (inferior *inf : all_inferiors ())
  262. if (inf->pspace == pspace)
  263. return inf;
  264. return NULL;
  265. }
  266. int
  267. have_inferiors (void)
  268. {
  269. for (inferior *inf ATTRIBUTE_UNUSED : all_non_exited_inferiors ())
  270. return 1;
  271. return 0;
  272. }
  273. /* Return the number of live inferiors. We account for the case
  274. where an inferior might have a non-zero pid but no threads, as
  275. in the middle of a 'mourn' operation. */
  276. int
  277. number_of_live_inferiors (process_stratum_target *proc_target)
  278. {
  279. int num_inf = 0;
  280. for (inferior *inf : all_non_exited_inferiors (proc_target))
  281. if (inf->has_execution ())
  282. for (thread_info *tp ATTRIBUTE_UNUSED : inf->non_exited_threads ())
  283. {
  284. /* Found a live thread in this inferior, go to the next
  285. inferior. */
  286. ++num_inf;
  287. break;
  288. }
  289. return num_inf;
  290. }
  291. /* Return true if there is at least one live inferior. */
  292. int
  293. have_live_inferiors (void)
  294. {
  295. return number_of_live_inferiors (NULL) > 0;
  296. }
  297. /* Prune away any unused inferiors, and then prune away no longer used
  298. program spaces. */
  299. void
  300. prune_inferiors (void)
  301. {
  302. for (inferior *inf : all_inferiors_safe ())
  303. {
  304. if (!inf->deletable ()
  305. || !inf->removable
  306. || inf->pid != 0)
  307. continue;
  308. delete_inferior (inf);
  309. }
  310. }
  311. /* Simply returns the count of inferiors. */
  312. int
  313. number_of_inferiors (void)
  314. {
  315. auto rng = all_inferiors ();
  316. return std::distance (rng.begin (), rng.end ());
  317. }
  318. /* Converts an inferior process id to a string. Like
  319. target_pid_to_str, but special cases the null process. */
  320. static std::string
  321. inferior_pid_to_str (int pid)
  322. {
  323. if (pid != 0)
  324. return target_pid_to_str (ptid_t (pid));
  325. else
  326. return _("<null>");
  327. }
  328. /* See inferior.h. */
  329. void
  330. print_selected_inferior (struct ui_out *uiout)
  331. {
  332. struct inferior *inf = current_inferior ();
  333. const char *filename = inf->pspace->exec_filename.get ();
  334. if (filename == NULL)
  335. filename = _("<noexec>");
  336. uiout->message (_("[Switching to inferior %d [%s] (%s)]\n"),
  337. inf->num, inferior_pid_to_str (inf->pid).c_str (), filename);
  338. }
  339. /* Helper for print_inferior. Returns the 'connection-id' string for
  340. PROC_TARGET. */
  341. static std::string
  342. uiout_field_connection (process_stratum_target *proc_target)
  343. {
  344. if (proc_target == NULL)
  345. {
  346. return {};
  347. }
  348. else if (proc_target->connection_string () != NULL)
  349. {
  350. return string_printf ("%d (%s %s)",
  351. proc_target->connection_number,
  352. proc_target->shortname (),
  353. proc_target->connection_string ());
  354. }
  355. else
  356. {
  357. return string_printf ("%d (%s)",
  358. proc_target->connection_number,
  359. proc_target->shortname ());
  360. }
  361. }
  362. /* Prints the list of inferiors and their details on UIOUT. This is a
  363. version of 'info_inferior_command' suitable for use from MI.
  364. If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
  365. inferiors that should be printed. Otherwise, all inferiors are
  366. printed. */
  367. static void
  368. print_inferior (struct ui_out *uiout, const char *requested_inferiors)
  369. {
  370. int inf_count = 0;
  371. size_t connection_id_len = 20;
  372. /* Compute number of inferiors we will print. */
  373. for (inferior *inf : all_inferiors ())
  374. {
  375. if (!number_is_in_list (requested_inferiors, inf->num))
  376. continue;
  377. std::string conn = uiout_field_connection (inf->process_target ());
  378. if (connection_id_len < conn.size ())
  379. connection_id_len = conn.size ();
  380. ++inf_count;
  381. }
  382. if (inf_count == 0)
  383. {
  384. uiout->message ("No inferiors.\n");
  385. return;
  386. }
  387. ui_out_emit_table table_emitter (uiout, 5, inf_count, "inferiors");
  388. uiout->table_header (1, ui_left, "current", "");
  389. uiout->table_header (4, ui_left, "number", "Num");
  390. uiout->table_header (17, ui_left, "target-id", "Description");
  391. uiout->table_header (connection_id_len, ui_left,
  392. "connection-id", "Connection");
  393. uiout->table_header (17, ui_left, "exec", "Executable");
  394. uiout->table_body ();
  395. /* Restore the current thread after the loop because we switch the
  396. inferior in the loop. */
  397. scoped_restore_current_pspace_and_thread restore_pspace_thread;
  398. inferior *current_inf = current_inferior ();
  399. for (inferior *inf : all_inferiors ())
  400. {
  401. if (!number_is_in_list (requested_inferiors, inf->num))
  402. continue;
  403. ui_out_emit_tuple tuple_emitter (uiout, NULL);
  404. if (inf == current_inf)
  405. uiout->field_string ("current", "*");
  406. else
  407. uiout->field_skip ("current");
  408. uiout->field_signed ("number", inf->num);
  409. /* Because target_pid_to_str uses the current inferior,
  410. switch the inferior. */
  411. switch_to_inferior_no_thread (inf);
  412. uiout->field_string ("target-id", inferior_pid_to_str (inf->pid));
  413. std::string conn = uiout_field_connection (inf->process_target ());
  414. uiout->field_string ("connection-id", conn);
  415. if (inf->pspace->exec_filename != nullptr)
  416. uiout->field_string ("exec", inf->pspace->exec_filename.get (),
  417. file_name_style.style ());
  418. else
  419. uiout->field_skip ("exec");
  420. /* Print extra info that isn't really fit to always present in
  421. tabular form. Currently we print the vfork parent/child
  422. relationships, if any. */
  423. if (inf->vfork_parent)
  424. {
  425. uiout->text (_("\n\tis vfork child of inferior "));
  426. uiout->field_signed ("vfork-parent", inf->vfork_parent->num);
  427. }
  428. if (inf->vfork_child)
  429. {
  430. uiout->text (_("\n\tis vfork parent of inferior "));
  431. uiout->field_signed ("vfork-child", inf->vfork_child->num);
  432. }
  433. uiout->text ("\n");
  434. }
  435. }
  436. static void
  437. detach_inferior_command (const char *args, int from_tty)
  438. {
  439. if (!args || !*args)
  440. error (_("Requires argument (inferior id(s) to detach)"));
  441. scoped_restore_current_thread restore_thread;
  442. number_or_range_parser parser (args);
  443. while (!parser.finished ())
  444. {
  445. int num = parser.get_number ();
  446. inferior *inf = find_inferior_id (num);
  447. if (inf == NULL)
  448. {
  449. warning (_("Inferior ID %d not known."), num);
  450. continue;
  451. }
  452. if (inf->pid == 0)
  453. {
  454. warning (_("Inferior ID %d is not running."), num);
  455. continue;
  456. }
  457. thread_info *tp = any_thread_of_inferior (inf);
  458. if (tp == NULL)
  459. {
  460. warning (_("Inferior ID %d has no threads."), num);
  461. continue;
  462. }
  463. switch_to_thread (tp);
  464. detach_command (NULL, from_tty);
  465. }
  466. }
  467. static void
  468. kill_inferior_command (const char *args, int from_tty)
  469. {
  470. if (!args || !*args)
  471. error (_("Requires argument (inferior id(s) to kill)"));
  472. scoped_restore_current_thread restore_thread;
  473. number_or_range_parser parser (args);
  474. while (!parser.finished ())
  475. {
  476. int num = parser.get_number ();
  477. inferior *inf = find_inferior_id (num);
  478. if (inf == NULL)
  479. {
  480. warning (_("Inferior ID %d not known."), num);
  481. continue;
  482. }
  483. if (inf->pid == 0)
  484. {
  485. warning (_("Inferior ID %d is not running."), num);
  486. continue;
  487. }
  488. thread_info *tp = any_thread_of_inferior (inf);
  489. if (tp == NULL)
  490. {
  491. warning (_("Inferior ID %d has no threads."), num);
  492. continue;
  493. }
  494. switch_to_thread (tp);
  495. target_kill ();
  496. }
  497. bfd_cache_close_all ();
  498. }
  499. /* See inferior.h. */
  500. void
  501. switch_to_inferior_no_thread (inferior *inf)
  502. {
  503. set_current_inferior (inf);
  504. switch_to_no_thread ();
  505. set_current_program_space (inf->pspace);
  506. }
  507. static void
  508. inferior_command (const char *args, int from_tty)
  509. {
  510. struct inferior *inf;
  511. int num;
  512. if (args == nullptr)
  513. {
  514. inf = current_inferior ();
  515. gdb_assert (inf != nullptr);
  516. const char *filename = inf->pspace->exec_filename.get ();
  517. if (filename == nullptr)
  518. filename = _("<noexec>");
  519. gdb_printf (_("[Current inferior is %d [%s] (%s)]\n"),
  520. inf->num, inferior_pid_to_str (inf->pid).c_str (),
  521. filename);
  522. }
  523. else
  524. {
  525. num = parse_and_eval_long (args);
  526. inf = find_inferior_id (num);
  527. if (inf == NULL)
  528. error (_("Inferior ID %d not known."), num);
  529. if (inf->pid != 0)
  530. {
  531. if (inf != current_inferior ())
  532. {
  533. thread_info *tp = any_thread_of_inferior (inf);
  534. if (tp == NULL)
  535. error (_("Inferior has no threads."));
  536. switch_to_thread (tp);
  537. }
  538. gdb::observers::user_selected_context_changed.notify
  539. (USER_SELECTED_INFERIOR
  540. | USER_SELECTED_THREAD
  541. | USER_SELECTED_FRAME);
  542. }
  543. else
  544. {
  545. switch_to_inferior_no_thread (inf);
  546. gdb::observers::user_selected_context_changed.notify
  547. (USER_SELECTED_INFERIOR);
  548. }
  549. }
  550. }
  551. /* Print information about currently known inferiors. */
  552. static void
  553. info_inferiors_command (const char *args, int from_tty)
  554. {
  555. print_inferior (current_uiout, args);
  556. }
  557. /* remove-inferior ID */
  558. static void
  559. remove_inferior_command (const char *args, int from_tty)
  560. {
  561. if (args == NULL || *args == '\0')
  562. error (_("Requires an argument (inferior id(s) to remove)"));
  563. number_or_range_parser parser (args);
  564. while (!parser.finished ())
  565. {
  566. int num = parser.get_number ();
  567. struct inferior *inf = find_inferior_id (num);
  568. if (inf == NULL)
  569. {
  570. warning (_("Inferior ID %d not known."), num);
  571. continue;
  572. }
  573. if (!inf->deletable ())
  574. {
  575. warning (_("Can not remove current inferior %d."), num);
  576. continue;
  577. }
  578. if (inf->pid != 0)
  579. {
  580. warning (_("Can not remove active inferior %d."), num);
  581. continue;
  582. }
  583. delete_inferior (inf);
  584. }
  585. }
  586. struct inferior *
  587. add_inferior_with_spaces (void)
  588. {
  589. struct address_space *aspace;
  590. struct program_space *pspace;
  591. struct inferior *inf;
  592. /* If all inferiors share an address space on this system, this
  593. doesn't really return a new address space; otherwise, it
  594. really does. */
  595. aspace = maybe_new_address_space ();
  596. pspace = new program_space (aspace);
  597. inf = add_inferior (0);
  598. inf->pspace = pspace;
  599. inf->aspace = pspace->aspace;
  600. /* Setup the inferior's initial arch, based on information obtained
  601. from the global "set ..." options. */
  602. gdbarch_info info;
  603. inf->gdbarch = gdbarch_find_by_info (info);
  604. /* The "set ..." options reject invalid settings, so we should
  605. always have a valid arch by now. */
  606. gdb_assert (inf->gdbarch != NULL);
  607. return inf;
  608. }
  609. /* See inferior.h. */
  610. void
  611. switch_to_inferior_and_push_target (inferior *new_inf,
  612. bool no_connection, inferior *org_inf)
  613. {
  614. process_stratum_target *proc_target = org_inf->process_target ();
  615. /* Switch over temporarily, while reading executable and
  616. symbols. */
  617. switch_to_inferior_no_thread (new_inf);
  618. /* Reuse the target for new inferior. */
  619. if (!no_connection && proc_target != NULL)
  620. {
  621. new_inf->push_target (proc_target);
  622. if (proc_target->connection_string () != NULL)
  623. gdb_printf (_("Added inferior %d on connection %d (%s %s)\n"),
  624. new_inf->num,
  625. proc_target->connection_number,
  626. proc_target->shortname (),
  627. proc_target->connection_string ());
  628. else
  629. gdb_printf (_("Added inferior %d on connection %d (%s)\n"),
  630. new_inf->num,
  631. proc_target->connection_number,
  632. proc_target->shortname ());
  633. }
  634. else
  635. gdb_printf (_("Added inferior %d\n"), new_inf->num);
  636. }
  637. /* add-inferior [-copies N] [-exec FILENAME] [-no-connection] */
  638. static void
  639. add_inferior_command (const char *args, int from_tty)
  640. {
  641. int i, copies = 1;
  642. gdb::unique_xmalloc_ptr<char> exec;
  643. symfile_add_flags add_flags = 0;
  644. bool no_connection = false;
  645. if (from_tty)
  646. add_flags |= SYMFILE_VERBOSE;
  647. if (args)
  648. {
  649. gdb_argv built_argv (args);
  650. for (char **argv = built_argv.get (); *argv != NULL; argv++)
  651. {
  652. if (**argv == '-')
  653. {
  654. if (strcmp (*argv, "-copies") == 0)
  655. {
  656. ++argv;
  657. if (!*argv)
  658. error (_("No argument to -copies"));
  659. copies = parse_and_eval_long (*argv);
  660. }
  661. else if (strcmp (*argv, "-no-connection") == 0)
  662. no_connection = true;
  663. else if (strcmp (*argv, "-exec") == 0)
  664. {
  665. ++argv;
  666. if (!*argv)
  667. error (_("No argument to -exec"));
  668. exec.reset (tilde_expand (*argv));
  669. }
  670. }
  671. else
  672. error (_("Invalid argument"));
  673. }
  674. }
  675. inferior *orginf = current_inferior ();
  676. scoped_restore_current_pspace_and_thread restore_pspace_thread;
  677. for (i = 0; i < copies; ++i)
  678. {
  679. inferior *inf = add_inferior_with_spaces ();
  680. switch_to_inferior_and_push_target (inf, no_connection, orginf);
  681. if (exec != NULL)
  682. {
  683. exec_file_attach (exec.get (), from_tty);
  684. symbol_file_add_main (exec.get (), add_flags);
  685. }
  686. }
  687. }
  688. /* clone-inferior [-copies N] [ID] [-no-connection] */
  689. static void
  690. clone_inferior_command (const char *args, int from_tty)
  691. {
  692. int i, copies = 1;
  693. struct inferior *orginf = NULL;
  694. bool no_connection = false;
  695. if (args)
  696. {
  697. gdb_argv built_argv (args);
  698. char **argv = built_argv.get ();
  699. for (; *argv != NULL; argv++)
  700. {
  701. if (**argv == '-')
  702. {
  703. if (strcmp (*argv, "-copies") == 0)
  704. {
  705. ++argv;
  706. if (!*argv)
  707. error (_("No argument to -copies"));
  708. copies = parse_and_eval_long (*argv);
  709. if (copies < 0)
  710. error (_("Invalid copies number"));
  711. }
  712. else if (strcmp (*argv, "-no-connection") == 0)
  713. no_connection = true;
  714. }
  715. else
  716. {
  717. if (orginf == NULL)
  718. {
  719. int num;
  720. /* The first non-option (-) argument specified the
  721. program space ID. */
  722. num = parse_and_eval_long (*argv);
  723. orginf = find_inferior_id (num);
  724. if (orginf == NULL)
  725. error (_("Inferior ID %d not known."), num);
  726. continue;
  727. }
  728. else
  729. error (_("Invalid argument"));
  730. }
  731. }
  732. }
  733. /* If no inferior id was specified, then the user wants to clone the
  734. current inferior. */
  735. if (orginf == NULL)
  736. orginf = current_inferior ();
  737. scoped_restore_current_pspace_and_thread restore_pspace_thread;
  738. for (i = 0; i < copies; ++i)
  739. {
  740. struct address_space *aspace;
  741. struct program_space *pspace;
  742. struct inferior *inf;
  743. /* If all inferiors share an address space on this system, this
  744. doesn't really return a new address space; otherwise, it
  745. really does. */
  746. aspace = maybe_new_address_space ();
  747. pspace = new program_space (aspace);
  748. inf = add_inferior (0);
  749. inf->pspace = pspace;
  750. inf->aspace = pspace->aspace;
  751. inf->gdbarch = orginf->gdbarch;
  752. switch_to_inferior_and_push_target (inf, no_connection, orginf);
  753. /* If the original inferior had a user specified target
  754. description, make the clone use it too. */
  755. if (target_desc_info_from_user_p (inf->tdesc_info))
  756. copy_inferior_target_desc_info (inf, orginf);
  757. clone_program_space (pspace, orginf->pspace);
  758. /* Copy properties from the original inferior to the new one. */
  759. inf->set_args (orginf->args ());
  760. inf->set_cwd (orginf->cwd ());
  761. inf->set_tty (orginf->tty ());
  762. for (const std::string &set_var : orginf->environment.user_set_env ())
  763. {
  764. /* set_var has the form NAME=value. Split on the first '='. */
  765. const std::string::size_type pos = set_var.find ('=');
  766. gdb_assert (pos != std::string::npos);
  767. const std::string varname = set_var.substr (0, pos);
  768. inf->environment.set
  769. (varname.c_str (), orginf->environment.get (varname.c_str ()));
  770. }
  771. for (const std::string &unset_var
  772. : orginf->environment.user_unset_env ())
  773. inf->environment.unset (unset_var.c_str ());
  774. }
  775. }
  776. /* Print notices when new inferiors are created and die. */
  777. static void
  778. show_print_inferior_events (struct ui_file *file, int from_tty,
  779. struct cmd_list_element *c, const char *value)
  780. {
  781. gdb_printf (file, _("Printing of inferior events is %s.\n"), value);
  782. }
  783. /* Return a new value for the selected inferior's id. */
  784. static struct value *
  785. inferior_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
  786. void *ignore)
  787. {
  788. struct inferior *inf = current_inferior ();
  789. return value_from_longest (builtin_type (gdbarch)->builtin_int, inf->num);
  790. }
  791. /* Implementation of `$_inferior' variable. */
  792. static const struct internalvar_funcs inferior_funcs =
  793. {
  794. inferior_id_make_value,
  795. NULL,
  796. };
  797. void
  798. initialize_inferiors (void)
  799. {
  800. struct cmd_list_element *c = NULL;
  801. /* There's always one inferior. Note that this function isn't an
  802. automatic _initialize_foo function, since other _initialize_foo
  803. routines may need to install their per-inferior data keys. We
  804. can only allocate an inferior when all those modules have done
  805. that. Do this after initialize_progspace, due to the
  806. current_program_space reference. */
  807. set_current_inferior (add_inferior_silent (0));
  808. current_inferior_->pspace = current_program_space;
  809. current_inferior_->aspace = current_program_space->aspace;
  810. /* The architecture will be initialized shortly, by
  811. initialize_current_architecture. */
  812. add_info ("inferiors", info_inferiors_command,
  813. _("Print a list of inferiors being managed.\n\
  814. Usage: info inferiors [ID]...\n\
  815. If IDs are specified, the list is limited to just those inferiors.\n\
  816. By default all inferiors are displayed."));
  817. c = add_com ("add-inferior", no_class, add_inferior_command, _("\
  818. Add a new inferior.\n\
  819. Usage: add-inferior [-copies N] [-exec FILENAME] [-no-connection]\n\
  820. N is the optional number of inferiors to add, default is 1.\n\
  821. FILENAME is the file name of the executable to use\n\
  822. as main program.\n\
  823. By default, the new inferior inherits the current inferior's connection.\n\
  824. If -no-connection is specified, the new inferior begins with\n\
  825. no target connection yet."));
  826. set_cmd_completer (c, filename_completer);
  827. add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
  828. Remove inferior ID (or list of IDs).\n\
  829. Usage: remove-inferiors ID..."));
  830. add_com ("clone-inferior", no_class, clone_inferior_command, _("\
  831. Clone inferior ID.\n\
  832. Usage: clone-inferior [-copies N] [-no-connection] [ID]\n\
  833. Add N copies of inferior ID. The new inferiors have the same\n\
  834. executable loaded as the copied inferior. If -copies is not specified,\n\
  835. adds 1 copy. If ID is not specified, it is the current inferior\n\
  836. that is cloned.\n\
  837. By default, the new inferiors inherit the copied inferior's connection.\n\
  838. If -no-connection is specified, the new inferiors begin with\n\
  839. no target connection yet."));
  840. add_cmd ("inferiors", class_run, detach_inferior_command, _("\
  841. Detach from inferior ID (or list of IDS).\n\
  842. Usage; detach inferiors ID..."),
  843. &detachlist);
  844. add_cmd ("inferiors", class_run, kill_inferior_command, _("\
  845. Kill inferior ID (or list of IDs).\n\
  846. Usage: kill inferiors ID..."),
  847. &killlist);
  848. add_cmd ("inferior", class_run, inferior_command, _("\
  849. Use this command to switch between inferiors.\n\
  850. Usage: inferior ID\n\
  851. The new inferior ID must be currently known."),
  852. &cmdlist);
  853. add_setshow_boolean_cmd ("inferior-events", no_class,
  854. &print_inferior_events, _("\
  855. Set printing of inferior events (such as inferior start and exit)."), _("\
  856. Show printing of inferior events (such as inferior start and exit)."), NULL,
  857. NULL,
  858. show_print_inferior_events,
  859. &setprintlist, &showprintlist);
  860. create_internalvar_type_lazy ("_inferior", &inferior_funcs, NULL);
  861. }