GDB (xrefs)
/tmp/gdb-8.1/gdb/progspace.c
Go to the documentation of this file.
1 /* Program and address space management, for GDB, the GNU debugger.
2 
3  Copyright (C) 2009-2018 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. */
30 
31 /* The head of the program spaces list. */
33 
34 /* Pointer to the current program space. */
36 
37 /* The last address space number assigned. */
39 
40 
41 
42 /* Keep a registry of per-program_space data-pointers required by other GDB
43  modules. */
44 
46 
47 /* Keep a registry of per-address_space data-pointers required by other GDB
48  modules. */
49 
51 
52 
53 
54 /* Create a new address space object, and add it to the list. */
55 
56 struct address_space *
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 *
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
88 {
89  address_space_free_data (aspace);
90  xfree (aspace);
91 }
92 
93 int
95 {
96  return aspace->num;
97 }
98 
99 /* Start counting over from scratch. */
100 
101 static void
103 {
105 }
106 
107 
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 *
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
146 {
147  gdb_assert (pspace != current_program_space);
148 
150 
151  set_current_program_space (pspace);
152 
154  no_shared_libraries (NULL, 0);
155  exec_close ();
158  free_address_space (pspace->aspace);
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 *
171 {
173 
175 
176  if (src->pspace_exec_filename != NULL)
178 
179  if (src->symfile_object_file != NULL)
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
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. */
201 }
202 
203 /* Returns true iff there's no inferior bound to PSPACE. */
204 
205 int
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
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 
280  uiout->field_string ("current", "*");
281  else
282  uiout->field_skip ("current");
283 
284  uiout->field_int ("id", pspace->num);
285 
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,
306  }
307  else
308  printf_filtered (", ID %d (%s)",
309  inf->num,
311  }
312 
313  uiout->text ("\n");
314  }
315 }
316 
317 /* Boolean test for an already-known program space id. */
318 
319 static int
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
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
377 {
378  int shared_aspace = gdbarch_has_shared_address_space (target_gdbarch ());
379  struct program_space *pspace;
380  struct inferior *inf;
381 
383 
384  if (shared_aspace)
385  {
386  struct address_space *aspace = new_address_space ();
387 
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)
401  inf->aspace = maybe_new_address_space ();
402  else
403  inf->aspace = inf->pspace->aspace;
404 }
405 
406 
407 
408 /* See progspace.h. */
409 
410 void
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 
420 
421 void
423 {
424  add_cmd ("program-spaces", class_maintenance,
426  _("Info about currently known program spaces."),
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. */
437 }
struct gdbarch * target_gdbarch(void)
Definition: gdbarch.c:5467
void no_shared_libraries(const char *ignored, int from_tty)
Definition: solib.c:1261
int number_of_program_spaces(void)
Definition: progspace.c:353
struct address_space * maybe_new_address_space(void)
Definition: progspace.c:73
#define DEFINE_REGISTRY(TAG, ACCESS)
Definition: registry.h:154
void field_string(const char *fldname, const char *string)
Definition: ui-out.c:544
struct address_space * aspace
Definition: progspace.h:166
void xfree(void *)
void free_all_objfiles(void)
Definition: objfiles.c:735
int gdbarch_has_global_solist(struct gdbarch *gdbarch)
Definition: gdbarch.c:4602
struct objfile * symfile_object_file
Definition: progspace.h:184
static void maintenance_info_program_spaces_command(const char *args, int from_tty)
Definition: progspace.c:336
struct cmd_list_element * maintenanceinfolist
Definition: cli-cmds.c:139
void field_skip(const char *fldname)
Definition: ui-out.c:532
void table_header(int width, ui_align align, const std::string &col_name, const std::string &col_hdr)
Definition: ui-out.c:365
#define REGISTRY_ACCESS_FIELD(CONTAINER)
Definition: registry.h:85
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition: cli-decode.c:262
Definition: solist.h:38
void symbol_file_add_main(const char *args, symfile_add_flags add_flags)
Definition: symfile.c:1288
int program_space_empty_p(struct program_space *pspace)
Definition: progspace.c:206
#define _(String)
Definition: gdb_locale.h:35
void delete_program_space(struct program_space *pspace)
Definition: progspace.c:218
Definition: ui-out.h:44
void breakpoint_program_space_exit(struct program_space *pspace)
Definition: breakpoint.c:2802
void text(const char *string)
Definition: ui-out.c:581
void printf_filtered(const char *format,...)
Definition: utils.c:2045
struct program_space * add_program_space(struct address_space *aspace)
Definition: progspace.c:113
void set_current_program_space(struct program_space *pspace)
Definition: progspace.c:190
static void print_program_space(struct ui_out *uiout, int requested)
Definition: progspace.c:246
static int valid_program_space_id(int num)
Definition: progspace.c:320
struct program_space * pspace
Definition: inferior.h:349
void initialize_progspace(void)
Definition: progspace.c:422
void free_char_ptr_vec(VEC(char_ptr) *char_ptr_vec)
Definition: gdb_vecs.c:32
struct target_section_table target_sections
Definition: progspace.h:192
static int highest_address_space_num
Definition: progspace.c:38
void update_address_spaces(void)
Definition: progspace.c:376
ptid_t pid_to_ptid(int pid)
Definition: ptid.c:39
#define current_uiout
Definition: ui-out.h:39
int last_program_space_num
Definition: progspace.c:29
void clear_program_space_solib_cache(struct program_space *pspace)
Definition: progspace.c:411
struct inferior * inferior_list
Definition: inferior.c:45
Definition: gnu-nat.c:174
int gdbarch_has_shared_address_space(struct gdbarch *gdbarch)
Definition: gdbarch.c:4636
static void init_address_spaces(void)
Definition: progspace.c:102
int num
Definition: inferior.h:324
const char * objfile_name(const struct objfile *objfile)
Definition: objfiles.c:1557
struct program_space * clone_program_space(struct program_space *dest, struct program_space *src)
Definition: progspace.c:170
#define gdb_assert(expr)
Definition: gdb_assert.h:32
Definition: ui-out.h:77
void exec_file_attach(const char *filename, int from_tty)
Definition: exec.c:244
#define ALL_PSPACES(pspace)
Definition: progspace.h:243
#define XCNEW(T)
Definition: poison.h:121
pid_t pid
Definition: gnu-nat.c:187
static void release_program_space(struct program_space *pspace)
Definition: progspace.c:145
void field_int(const char *fldname, int value)
Definition: ui-out.c:486
void table_body()
Definition: ui-out.c:379
char * pspace_exec_filename
Definition: progspace.h:155
const char * target_pid_to_str(ptid_t ptid)
Definition: target.c:2194
#define VEC_free(T, V)
Definition: vec.h:196
int address_space_num(struct address_space *aspace)
Definition: progspace.c:94
struct address_space * new_address_space(void)
Definition: progspace.c:57
struct program_space * current_program_space
Definition: progspace.c:35
void exec_close(void)
Definition: exec.c:83
static void free_address_space(struct address_space *aspace)
Definition: progspace.c:87
struct inferior * find_inferior_for_program_space(struct program_space *pspace)
Definition: inferior.c:329
void reinit_frame_cache(void)
Definition: frame.c:1809
void clear_section_table(struct target_section_table *table)
Definition: exec.c:472
void error(const char *fmt,...)
Definition: errors.c:38
struct program_space * next
Definition: progspace.h:140
struct program_space * program_spaces
Definition: progspace.c:32
LONGEST parse_and_eval_long(const char *exp)
Definition: eval.c:111