gdb/progspace.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Program and address space management, for GDB, the GNU debugger.

  2.    Copyright (C) 2009-2015 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 "gdbcmd.h"
  16. #include "objfiles.h"
  17. #include "arch-utils.h"
  18. #include "gdbcore.h"
  19. #include "solib.h"
  20. #include "gdbthread.h"

  21. /* The last program space number assigned.  */
  22. int last_program_space_num = 0;

  23. /* The head of the program spaces list.  */
  24. struct program_space *program_spaces;

  25. /* Pointer to the current program space.  */
  26. struct program_space *current_program_space;

  27. /* The last address space number assigned.  */
  28. static int highest_address_space_num;



  29. /* Keep a registry of per-program_space data-pointers required by other GDB
  30.    modules.  */

  31. DEFINE_REGISTRY (program_space, REGISTRY_ACCESS_FIELD)

  32. /* An address space.  It is used for comparing if pspaces/inferior/threads
  33.    see the same address space and for associating caches to each address
  34.    space.  */

  35. struct address_space
  36. {
  37.   int num;

  38.   /* Per aspace data-pointers required by other GDB modules.  */
  39.   REGISTRY_FIELDS;
  40. };

  41. /* Keep a registry of per-address_space data-pointers required by other GDB
  42.    modules.  */

  43. DEFINE_REGISTRY (address_space, REGISTRY_ACCESS_FIELD)



  44. /* Create a new address space object, and add it to the list.  */

  45. struct address_space *
  46. new_address_space (void)
  47. {
  48.   struct address_space *aspace;

  49.   aspace = XCNEW (struct address_space);
  50.   aspace->num = ++highest_address_space_num;
  51.   address_space_alloc_data (aspace);

  52.   return aspace;
  53. }

  54. /* Maybe create a new address space object, and add it to the list, or
  55.    return a pointer to an existing address space, in case inferiors
  56.    share an address space on this target system.  */

  57. struct address_space *
  58. maybe_new_address_space (void)
  59. {
  60.   int shared_aspace = gdbarch_has_shared_address_space (target_gdbarch ());

  61.   if (shared_aspace)
  62.     {
  63.       /* Just return the first in the list.  */
  64.       return program_spaces->aspace;
  65.     }

  66.   return new_address_space ();
  67. }

  68. static void
  69. free_address_space (struct address_space *aspace)
  70. {
  71.   address_space_free_data (aspace);
  72.   xfree (aspace);
  73. }

  74. int
  75. address_space_num (struct address_space *aspace)
  76. {
  77.   return aspace->num;
  78. }

  79. /* Start counting over from scratch.  */

  80. static void
  81. init_address_spaces (void)
  82. {
  83.   highest_address_space_num = 0;
  84. }



  85. /* Adds a new empty program space to the program space list, and binds
  86.    it to ASPACE.  Returns the pointer to the new object.  */

  87. struct program_space *
  88. add_program_space (struct address_space *aspace)
  89. {
  90.   struct program_space *pspace;

  91.   pspace = XCNEW (struct program_space);

  92.   pspace->num = ++last_program_space_num;
  93.   pspace->aspace = aspace;

  94.   program_space_alloc_data (pspace);

  95.   pspace->next = program_spaces;
  96.   program_spaces = pspace;

  97.   return pspace;
  98. }

  99. /* Releases program space PSPACE, and all its contents (shared
  100.    libraries, objfiles, and any other references to the PSPACE in
  101.    other modules).  It is an internal error to call this when PSPACE
  102.    is the current program space, since there should always be a
  103.    program space.  */

  104. static void
  105. release_program_space (struct program_space *pspace)
  106. {
  107.   struct cleanup *old_chain = save_current_program_space ();

  108.   gdb_assert (pspace != current_program_space);

  109.   set_current_program_space (pspace);

  110.   breakpoint_program_space_exit (pspace);
  111.   no_shared_libraries (NULL, 0);
  112.   exec_close ();
  113.   free_all_objfiles ();
  114.   if (!gdbarch_has_shared_address_space (target_gdbarch ()))
  115.     free_address_space (pspace->aspace);
  116.   clear_section_table (&pspace->target_sections);
  117.   clear_program_space_solib_cache (pspace);
  118.     /* Discard any data modules have associated with the PSPACE.  */
  119.   program_space_free_data (pspace);
  120.   xfree (pspace);

  121.   do_cleanups (old_chain);
  122. }

  123. /* Copies program space SRC to DEST.  Copies the main executable file,
  124.    and the main symbol file.  Returns DEST.  */

  125. struct program_space *
  126. clone_program_space (struct program_space *dest, struct program_space *src)
  127. {
  128.   struct cleanup *old_chain;

  129.   old_chain = save_current_program_space ();

  130.   set_current_program_space (dest);

  131.   if (src->pspace_exec_filename != NULL)
  132.     exec_file_attach (src->pspace_exec_filename, 0);

  133.   if (src->symfile_object_file != NULL)
  134.     symbol_file_add_main (objfile_name (src->symfile_object_file), 0);

  135.   do_cleanups (old_chain);
  136.   return dest;
  137. }

  138. /* Sets PSPACE as the current program space.  It is the caller's
  139.    responsibility to make sure that the currently selected
  140.    inferior/thread matches the selected program space.  */

  141. void
  142. set_current_program_space (struct program_space *pspace)
  143. {
  144.   if (current_program_space == pspace)
  145.     return;

  146.   gdb_assert (pspace != NULL);

  147.   current_program_space = pspace;

  148.   /* Different symbols change our view of the frame chain.  */
  149.   reinit_frame_cache ();
  150. }

  151. /* A cleanups callback, helper for save_current_program_space
  152.    below.  */

  153. static void
  154. restore_program_space (void *arg)
  155. {
  156.   struct program_space *saved_pspace = arg;

  157.   set_current_program_space (saved_pspace);
  158. }

  159. /* Save the current program space so that it may be restored by a later
  160.    call to do_cleanups.  Returns the struct cleanup pointer needed for
  161.    later doing the cleanup.  */

  162. struct cleanup *
  163. save_current_program_space (void)
  164. {
  165.   struct cleanup *old_chain = make_cleanup (restore_program_space,
  166.                                             current_program_space);

  167.   return old_chain;
  168. }

  169. /* Returns true iff there's no inferior bound to PSPACE.  */

  170. static int
  171. pspace_empty_p (struct program_space *pspace)
  172. {
  173.   if (find_inferior_for_program_space (pspace) != NULL)
  174.       return 0;

  175.   return 1;
  176. }

  177. /* Prune away automatically added program spaces that aren't required
  178.    anymore.  */

  179. void
  180. prune_program_spaces (void)
  181. {
  182.   struct program_space *ss, **ss_link;
  183.   struct program_space *current = current_program_space;

  184.   ss = program_spaces;
  185.   ss_link = &program_spaces;
  186.   while (ss)
  187.     {
  188.       if (ss == current || !pspace_empty_p (ss))
  189.         {
  190.           ss_link = &ss->next;
  191.           ss = *ss_link;
  192.           continue;
  193.         }

  194.       *ss_link = ss->next;
  195.       release_program_space (ss);
  196.       ss = *ss_link;
  197.     }
  198. }

  199. /* Prints the list of program spaces and their details on UIOUT.  If
  200.    REQUESTED is not -1, it's the ID of the pspace that should be
  201.    printed.  Otherwise, all spaces are printed.  */

  202. static void
  203. print_program_space (struct ui_out *uiout, int requested)
  204. {
  205.   struct program_space *pspace;
  206.   int count = 0;
  207.   struct cleanup *old_chain;

  208.   /* Compute number of pspaces we will print.  */
  209.   ALL_PSPACES (pspace)
  210.     {
  211.       if (requested != -1 && pspace->num != requested)
  212.         continue;

  213.       ++count;
  214.     }

  215.   /* There should always be at least one.  */
  216.   gdb_assert (count > 0);

  217.   old_chain = make_cleanup_ui_out_table_begin_end (uiout, 3, count, "pspaces");
  218.   ui_out_table_header (uiout, 1, ui_left, "current", "");
  219.   ui_out_table_header (uiout, 4, ui_left, "id", "Id");
  220.   ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
  221.   ui_out_table_body (uiout);

  222.   ALL_PSPACES (pspace)
  223.     {
  224.       struct cleanup *chain2;
  225.       struct inferior *inf;
  226.       int printed_header;

  227.       if (requested != -1 && requested != pspace->num)
  228.         continue;

  229.       chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);

  230.       if (pspace == current_program_space)
  231.         ui_out_field_string (uiout, "current", "*");
  232.       else
  233.         ui_out_field_skip (uiout, "current");

  234.       ui_out_field_int (uiout, "id", pspace->num);

  235.       if (pspace->pspace_exec_filename)
  236.         ui_out_field_string (uiout, "exec", pspace->pspace_exec_filename);
  237.       else
  238.         ui_out_field_skip (uiout, "exec");

  239.       /* Print extra info that doesn't really fit in tabular form.
  240.          Currently, we print the list of inferiors bound to a pspace.
  241.          There can be more than one inferior bound to the same pspace,
  242.          e.g., both parent/child inferiors in a vfork, or, on targets
  243.          that share pspaces between inferiors.  */
  244.       printed_header = 0;
  245.       for (inf = inferior_list; inf; inf = inf->next)
  246.         if (inf->pspace == pspace)
  247.           {
  248.             if (!printed_header)
  249.               {
  250.                 printed_header = 1;
  251.                 printf_filtered ("\n\tBound inferiors: ID %d (%s)",
  252.                                  inf->num,
  253.                                  target_pid_to_str (pid_to_ptid (inf->pid)));
  254.               }
  255.             else
  256.               printf_filtered (", ID %d (%s)",
  257.                                inf->num,
  258.                                target_pid_to_str (pid_to_ptid (inf->pid)));
  259.           }

  260.       ui_out_text (uiout, "\n");
  261.       do_cleanups (chain2);
  262.     }

  263.   do_cleanups (old_chain);
  264. }

  265. /* Boolean test for an already-known program space id.  */

  266. static int
  267. valid_program_space_id (int num)
  268. {
  269.   struct program_space *pspace;

  270.   ALL_PSPACES (pspace)
  271.     if (pspace->num == num)
  272.       return 1;

  273.   return 0;
  274. }

  275. /* If ARGS is NULL or empty, print information about all program
  276.    spaces.  Otherwise, ARGS is a text representation of a LONG
  277.    indicating which the program space to print information about.  */

  278. static void
  279. maintenance_info_program_spaces_command (char *args, int from_tty)
  280. {
  281.   int requested = -1;

  282.   if (args && *args)
  283.     {
  284.       requested = parse_and_eval_long (args);
  285.       if (!valid_program_space_id (requested))
  286.         error (_("program space ID %d not known."), requested);
  287.     }

  288.   print_program_space (current_uiout, requested);
  289. }

  290. /* Simply returns the count of program spaces.  */

  291. int
  292. number_of_program_spaces (void)
  293. {
  294.   struct program_space *pspace;
  295.   int count = 0;

  296.   ALL_PSPACES (pspace)
  297.     count++;

  298.   return count;
  299. }

  300. /* Update all program spaces matching to address spaces.  The user may
  301.    have created several program spaces, and loaded executables into
  302.    them before connecting to the target interface that will create the
  303.    inferiors.  All that happens before GDB has a chance to know if the
  304.    inferiors will share an address space or not.  Call this after
  305.    having connected to the target interface and having fetched the
  306.    target description, to fixup the program/address spaces mappings.

  307.    It is assumed that there are no bound inferiors yet, otherwise,
  308.    they'd be left with stale referenced to released aspaces.  */

  309. void
  310. update_address_spaces (void)
  311. {
  312.   int shared_aspace = gdbarch_has_shared_address_space (target_gdbarch ());
  313.   struct program_space *pspace;
  314.   struct inferior *inf;

  315.   init_address_spaces ();

  316.   if (shared_aspace)
  317.     {
  318.       struct address_space *aspace = new_address_space ();

  319.       free_address_space (current_program_space->aspace);
  320.       ALL_PSPACES (pspace)
  321.         pspace->aspace = aspace;
  322.     }
  323.   else
  324.     ALL_PSPACES (pspace)
  325.       {
  326.         free_address_space (pspace->aspace);
  327.         pspace->aspace = new_address_space ();
  328.       }

  329.   for (inf = inferior_list; inf; inf = inf->next)
  330.     if (gdbarch_has_global_solist (target_gdbarch ()))
  331.       inf->aspace = maybe_new_address_space ();
  332.     else
  333.       inf->aspace = inf->pspace->aspace;
  334. }

  335. /* Save the current program space so that it may be restored by a later
  336.    call to do_cleanups.  Returns the struct cleanup pointer needed for
  337.    later doing the cleanup.  */

  338. struct cleanup *
  339. save_current_space_and_thread (void)
  340. {
  341.   struct cleanup *old_chain;

  342.   /* If restoring to null thread, we need to restore the pspace as
  343.      well, hence, we need to save the current program space first.  */
  344.   old_chain = save_current_program_space ();
  345.   /* There's no need to save the current inferior here.
  346.      That is handled by make_cleanup_restore_current_thread.  */
  347.   make_cleanup_restore_current_thread ();

  348.   return old_chain;
  349. }

  350. /* See progspace.h  */

  351. void
  352. switch_to_program_space_and_thread (struct program_space *pspace)
  353. {
  354.   struct inferior *inf;

  355.   inf = find_inferior_for_program_space (pspace);
  356.   if (inf != NULL && inf->pid != 0)
  357.     {
  358.       struct thread_info *tp;

  359.       tp = any_live_thread_of_process (inf->pid);
  360.       if (tp != NULL)
  361.         {
  362.           switch_to_thread (tp->ptid);
  363.           /* Switching thread switches pspace implicitly.  We're
  364.              done.  */
  365.           return;
  366.         }
  367.     }

  368.   switch_to_thread (null_ptid);
  369.   set_current_program_space (pspace);
  370. }



  371. /* See progspace.h.  */

  372. void
  373. clear_program_space_solib_cache (struct program_space *pspace)
  374. {
  375.   VEC_free (so_list_ptr, pspace->added_solibs);

  376.   free_char_ptr_vec (pspace->deleted_solibs);
  377.   pspace->deleted_solibs = NULL;
  378. }



  379. void
  380. initialize_progspace (void)
  381. {
  382.   add_cmd ("program-spaces", class_maintenance,
  383.            maintenance_info_program_spaces_command,
  384.            _("Info about currently known program spaces."),
  385.            &maintenanceinfolist);

  386.   /* There's always one program space.  Note that this function isn't
  387.      an automatic _initialize_foo function, since other
  388.      _initialize_foo routines may need to install their per-pspace
  389.      data keys.  We can only allocate a progspace when all those
  390.      modules have done that.  Do this before
  391.      initialize_current_architecture, because that accesses exec_bfd,
  392.      which in turn dereferences current_program_space.  */
  393.   current_program_space = add_program_space (new_address_space ());
  394. }