Redesign mock environment for gdbarch selftests
[external/binutils.git] / gdb / progspace.c
1 /* Program and address space management, for GDB, the GNU debugger.
2
3    Copyright (C) 2009-2017 Free Software Foundation, Inc.
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"
21 #include "gdbcmd.h"
22 #include "objfiles.h"
23 #include "arch-utils.h"
24 #include "gdbcore.h"
25 #include "solib.h"
26 #include "gdbthread.h"
27
28 /* The last program space number assigned.  */
29 int last_program_space_num = 0;
30
31 /* The head of the program spaces list.  */
32 struct program_space *program_spaces;
33
34 /* Pointer to the current program space.  */
35 struct program_space *current_program_space;
36
37 /* The last address space number assigned.  */
38 static int highest_address_space_num;
39
40 \f
41
42 /* Keep a registry of per-program_space data-pointers required by other GDB
43    modules.  */
44
45 DEFINE_REGISTRY (program_space, REGISTRY_ACCESS_FIELD)
46
47 /* Keep a registry of per-address_space data-pointers required by other GDB
48    modules.  */
49
50 DEFINE_REGISTRY (address_space, REGISTRY_ACCESS_FIELD)
51
52 \f
53
54 /* Create a new address space object, and add it to the list.  */
55
56 struct address_space *
57 new_address_space (void)
58 {
59   struct address_space *aspace;
60
61   aspace = XCNEW (struct address_space);
62   aspace->num = ++highest_address_space_num;
63   address_space_alloc_data (aspace);
64
65   return aspace;
66 }
67
68 /* Maybe create a new address space object, and add it to the list, or
69    return a pointer to an existing address space, in case inferiors
70    share an address space on this target system.  */
71
72 struct address_space *
73 maybe_new_address_space (void)
74 {
75   int shared_aspace = gdbarch_has_shared_address_space (target_gdbarch ());
76
77   if (shared_aspace)
78     {
79       /* Just return the first in the list.  */
80       return program_spaces->aspace;
81     }
82
83   return new_address_space ();
84 }
85
86 static void
87 free_address_space (struct address_space *aspace)
88 {
89   address_space_free_data (aspace);
90   xfree (aspace);
91 }
92
93 int
94 address_space_num (struct address_space *aspace)
95 {
96   return aspace->num;
97 }
98
99 /* Start counting over from scratch.  */
100
101 static void
102 init_address_spaces (void)
103 {
104   highest_address_space_num = 0;
105 }
106
107 \f
108
109 /* Adds a new empty program space to the program space list, and binds
110    it to ASPACE.  Returns the pointer to the new object.  */
111
112 struct program_space *
113 add_program_space (struct address_space *aspace)
114 {
115   struct program_space *pspace;
116
117   pspace = XCNEW (struct program_space);
118
119   pspace->num = ++last_program_space_num;
120   pspace->aspace = aspace;
121
122   program_space_alloc_data (pspace);
123
124   if (program_spaces == NULL)
125     program_spaces = pspace;
126   else
127     {
128       struct program_space *last;
129
130       for (last = program_spaces; last->next != NULL; last = last->next)
131         ;
132       last->next = pspace;
133     }
134
135   return pspace;
136 }
137
138 /* Releases program space PSPACE, and all its contents (shared
139    libraries, objfiles, and any other references to the PSPACE in
140    other modules).  It is an internal error to call this when PSPACE
141    is the current program space, since there should always be a
142    program space.  */
143
144 static void
145 release_program_space (struct program_space *pspace)
146 {
147   gdb_assert (pspace != current_program_space);
148
149   scoped_restore_current_program_space restore_pspace;
150
151   set_current_program_space (pspace);
152
153   breakpoint_program_space_exit (pspace);
154   no_shared_libraries (NULL, 0);
155   exec_close ();
156   free_all_objfiles ();
157   if (!gdbarch_has_shared_address_space (target_gdbarch ()))
158     free_address_space (pspace->aspace);
159   clear_section_table (&pspace->target_sections);
160   clear_program_space_solib_cache (pspace);
161     /* Discard any data modules have associated with the PSPACE.  */
162   program_space_free_data (pspace);
163   xfree (pspace);
164 }
165
166 /* Copies program space SRC to DEST.  Copies the main executable file,
167    and the main symbol file.  Returns DEST.  */
168
169 struct program_space *
170 clone_program_space (struct program_space *dest, struct program_space *src)
171 {
172   scoped_restore_current_program_space restore_pspace;
173
174   set_current_program_space (dest);
175
176   if (src->pspace_exec_filename != NULL)
177     exec_file_attach (src->pspace_exec_filename, 0);
178
179   if (src->symfile_object_file != NULL)
180     symbol_file_add_main (objfile_name (src->symfile_object_file), 0);
181
182   return dest;
183 }
184
185 /* Sets PSPACE as the current program space.  It is the caller's
186    responsibility to make sure that the currently selected
187    inferior/thread matches the selected program space.  */
188
189 void
190 set_current_program_space (struct program_space *pspace)
191 {
192   if (current_program_space == pspace)
193     return;
194
195   gdb_assert (pspace != NULL);
196
197   current_program_space = pspace;
198
199   /* Different symbols change our view of the frame chain.  */
200   reinit_frame_cache ();
201 }
202
203 /* Returns true iff there's no inferior bound to PSPACE.  */
204
205 int
206 program_space_empty_p (struct program_space *pspace)
207 {
208   if (find_inferior_for_program_space (pspace) != NULL)
209       return 0;
210
211   return 1;
212 }
213
214 /* Remove a program space from the program spaces list and release it.  It is
215    an error to call this function while PSPACE is the current program space. */
216
217 void
218 delete_program_space (struct program_space *pspace)
219 {
220   struct program_space *ss, **ss_link;
221   gdb_assert (pspace != NULL);
222   gdb_assert (pspace != current_program_space);
223
224   ss = program_spaces;
225   ss_link = &program_spaces;
226   while (ss != NULL)
227     {
228       if (ss == pspace)
229         {
230           *ss_link = ss->next;
231           break;
232         }
233
234       ss_link = &ss->next;
235       ss = *ss_link;
236     }
237
238   release_program_space (pspace);
239 }
240
241 /* Prints the list of program spaces and their details on UIOUT.  If
242    REQUESTED is not -1, it's the ID of the pspace that should be
243    printed.  Otherwise, all spaces are printed.  */
244
245 static void
246 print_program_space (struct ui_out *uiout, int requested)
247 {
248   struct program_space *pspace;
249   int count = 0;
250
251   /* Compute number of pspaces we will print.  */
252   ALL_PSPACES (pspace)
253     {
254       if (requested != -1 && pspace->num != requested)
255         continue;
256
257       ++count;
258     }
259
260   /* There should always be at least one.  */
261   gdb_assert (count > 0);
262
263   ui_out_emit_table table_emitter (uiout, 3, count, "pspaces");
264   uiout->table_header (1, ui_left, "current", "");
265   uiout->table_header (4, ui_left, "id", "Id");
266   uiout->table_header (17, ui_left, "exec", "Executable");
267   uiout->table_body ();
268
269   ALL_PSPACES (pspace)
270     {
271       struct inferior *inf;
272       int printed_header;
273
274       if (requested != -1 && requested != pspace->num)
275         continue;
276
277       ui_out_emit_tuple tuple_emitter (uiout, NULL);
278
279       if (pspace == current_program_space)
280         uiout->field_string ("current", "*");
281       else
282         uiout->field_skip ("current");
283
284       uiout->field_int ("id", pspace->num);
285
286       if (pspace->pspace_exec_filename)
287         uiout->field_string ("exec", pspace->pspace_exec_filename);
288       else
289         uiout->field_skip ("exec");
290
291       /* Print extra info that doesn't really fit in tabular form.
292          Currently, we print the list of inferiors bound to a pspace.
293          There can be more than one inferior bound to the same pspace,
294          e.g., both parent/child inferiors in a vfork, or, on targets
295          that share pspaces between inferiors.  */
296       printed_header = 0;
297       for (inf = inferior_list; inf; inf = inf->next)
298         if (inf->pspace == pspace)
299           {
300             if (!printed_header)
301               {
302                 printed_header = 1;
303                 printf_filtered ("\n\tBound inferiors: ID %d (%s)",
304                                  inf->num,
305                                  target_pid_to_str (pid_to_ptid (inf->pid)));
306               }
307             else
308               printf_filtered (", ID %d (%s)",
309                                inf->num,
310                                target_pid_to_str (pid_to_ptid (inf->pid)));
311           }
312
313       uiout->text ("\n");
314     }
315 }
316
317 /* Boolean test for an already-known program space id.  */
318
319 static int
320 valid_program_space_id (int num)
321 {
322   struct program_space *pspace;
323
324   ALL_PSPACES (pspace)
325     if (pspace->num == num)
326       return 1;
327
328   return 0;
329 }
330
331 /* If ARGS is NULL or empty, print information about all program
332    spaces.  Otherwise, ARGS is a text representation of a LONG
333    indicating which the program space to print information about.  */
334
335 static void
336 maintenance_info_program_spaces_command (const char *args, int from_tty)
337 {
338   int requested = -1;
339
340   if (args && *args)
341     {
342       requested = parse_and_eval_long (args);
343       if (!valid_program_space_id (requested))
344         error (_("program space ID %d not known."), requested);
345     }
346
347   print_program_space (current_uiout, requested);
348 }
349
350 /* Simply returns the count of program spaces.  */
351
352 int
353 number_of_program_spaces (void)
354 {
355   struct program_space *pspace;
356   int count = 0;
357
358   ALL_PSPACES (pspace)
359     count++;
360
361   return count;
362 }
363
364 /* Update all program spaces matching to address spaces.  The user may
365    have created several program spaces, and loaded executables into
366    them before connecting to the target interface that will create the
367    inferiors.  All that happens before GDB has a chance to know if the
368    inferiors will share an address space or not.  Call this after
369    having connected to the target interface and having fetched the
370    target description, to fixup the program/address spaces mappings.
371
372    It is assumed that there are no bound inferiors yet, otherwise,
373    they'd be left with stale referenced to released aspaces.  */
374
375 void
376 update_address_spaces (void)
377 {
378   int shared_aspace = gdbarch_has_shared_address_space (target_gdbarch ());
379   struct program_space *pspace;
380   struct inferior *inf;
381
382   init_address_spaces ();
383
384   if (shared_aspace)
385     {
386       struct address_space *aspace = new_address_space ();
387
388       free_address_space (current_program_space->aspace);
389       ALL_PSPACES (pspace)
390         pspace->aspace = aspace;
391     }
392   else
393     ALL_PSPACES (pspace)
394       {
395         free_address_space (pspace->aspace);
396         pspace->aspace = new_address_space ();
397       }
398
399   for (inf = inferior_list; inf; inf = inf->next)
400     if (gdbarch_has_global_solist (target_gdbarch ()))
401       inf->aspace = maybe_new_address_space ();
402     else
403       inf->aspace = inf->pspace->aspace;
404 }
405
406 \f
407
408 /* See progspace.h.  */
409
410 void
411 clear_program_space_solib_cache (struct program_space *pspace)
412 {
413   VEC_free (so_list_ptr, pspace->added_solibs);
414
415   free_char_ptr_vec (pspace->deleted_solibs);
416   pspace->deleted_solibs = NULL;
417 }
418
419 \f
420
421 void
422 initialize_progspace (void)
423 {
424   add_cmd ("program-spaces", class_maintenance,
425            maintenance_info_program_spaces_command,
426            _("Info about currently known program spaces."),
427            &maintenanceinfolist);
428
429   /* There's always one program space.  Note that this function isn't
430      an automatic _initialize_foo function, since other
431      _initialize_foo routines may need to install their per-pspace
432      data keys.  We can only allocate a progspace when all those
433      modules have done that.  Do this before
434      initialize_current_architecture, because that accesses exec_bfd,
435      which in turn dereferences current_program_space.  */
436   current_program_space = add_program_space (new_address_space ());
437 }