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. */
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
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
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
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
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
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 }
target_gdbarch
struct gdbarch * target_gdbarch(void)
Definition: gdbarch.c:5467
no_shared_libraries
void no_shared_libraries(const char *ignored, int from_tty)
Definition: solib.c:1261
number_of_program_spaces
int number_of_program_spaces(void)
Definition: progspace.c:353
maybe_new_address_space
struct address_space * maybe_new_address_space(void)
Definition: progspace.c:73
DEFINE_REGISTRY
#define DEFINE_REGISTRY(TAG, ACCESS)
Definition: registry.h:154
ui_out::field_string
void field_string(const char *fldname, const char *string)
Definition: ui-out.c:544
ui_out_emit_type
Definition: ui-out.h:192
program_space::aspace
struct address_space * aspace
Definition: progspace.h:166
xfree
void xfree(void *)
free_all_objfiles
void free_all_objfiles(void)
Definition: objfiles.c:735
gdbarch_has_global_solist
int gdbarch_has_global_solist(struct gdbarch *gdbarch)
Definition: gdbarch.c:4602
program_space::symfile_object_file
struct objfile * symfile_object_file
Definition: progspace.h:184
maintenance_info_program_spaces_command
static void maintenance_info_program_spaces_command(const char *args, int from_tty)
Definition: progspace.c:336
maintenanceinfolist
struct cmd_list_element * maintenanceinfolist
Definition: cli-cmds.c:139
ui_out::field_skip
void field_skip(const char *fldname)
Definition: ui-out.c:532
ui_out::table_header
void table_header(int width, ui_align align, const std::string &col_name, const std::string &col_hdr)
Definition: ui-out.c:365
REGISTRY_ACCESS_FIELD
#define REGISTRY_ACCESS_FIELD(CONTAINER)
Definition: registry.h:85
inferior
Definition: inferior.h:310
add_cmd
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
so_list
Definition: solist.h:38
symbol_file_add_main
void symbol_file_add_main(const char *args, symfile_add_flags add_flags)
Definition: symfile.c:1288
program_space_empty_p
int program_space_empty_p(struct program_space *pspace)
Definition: progspace.c:206
_
#define _(String)
Definition: gdb_locale.h:35
delete_program_space
void delete_program_space(struct program_space *pspace)
Definition: progspace.c:218
address_space::num
int num
Definition: progspace.h:218
ui_left
Definition: ui-out.h:44
defs.h
breakpoint_program_space_exit
void breakpoint_program_space_exit(struct program_space *pspace)
Definition: breakpoint.c:2802
ui_out::text
void text(const char *string)
Definition: ui-out.c:581
arm-linux.inf
inf
Definition: arm-linux.py:13
printf_filtered
void printf_filtered(const char *format,...)
Definition: utils.c:2045
objfiles.h
arch-utils.h
program_space::num
int num
Definition: progspace.h:143
add_program_space
struct program_space * add_program_space(struct address_space *aspace)
Definition: progspace.c:113
set_current_program_space
void set_current_program_space(struct program_space *pspace)
Definition: progspace.c:190
print_program_space
static void print_program_space(struct ui_out *uiout, int requested)
Definition: progspace.c:246
valid_program_space_id
static int valid_program_space_id(int num)
Definition: progspace.c:320
inferior::pspace
struct program_space * pspace
Definition: inferior.h:349
initialize_progspace
void initialize_progspace(void)
Definition: progspace.c:422
free_char_ptr_vec
void free_char_ptr_vec(VEC(char_ptr) *char_ptr_vec)
Definition: gdb_vecs.c:32
program_space::target_sections
struct target_section_table target_sections
Definition: progspace.h:192
highest_address_space_num
static int highest_address_space_num
Definition: progspace.c:38
update_address_spaces
void update_address_spaces(void)
Definition: progspace.c:376
pid_to_ptid
ptid_t pid_to_ptid(int pid)
Definition: ptid.c:39
gdbthread.h
current_uiout
#define current_uiout
Definition: ui-out.h:39
last_program_space_num
int last_program_space_num
Definition: progspace.c:29
clear_program_space_solib_cache
void clear_program_space_solib_cache(struct program_space *pspace)
Definition: progspace.c:411
inferior_list
struct inferior * inferior_list
Definition: inferior.c:45
inf
Definition: gnu-nat.c:174
gdbarch_has_shared_address_space
int gdbarch_has_shared_address_space(struct gdbarch *gdbarch)
Definition: gdbarch.c:4636
solib.h
init_address_spaces
static void init_address_spaces(void)
Definition: progspace.c:102
inferior::num
int num
Definition: inferior.h:324
objfile_name
const char * objfile_name(const struct objfile *objfile)
Definition: objfiles.c:1557
clone_program_space
struct program_space * clone_program_space(struct program_space *dest, struct program_space *src)
Definition: progspace.c:170
gdb_assert
#define gdb_assert(expr)
Definition: gdb_assert.h:32
ui_out
Definition: ui-out.h:77
exec_file_attach
void exec_file_attach(const char *filename, int from_tty)
Definition: exec.c:244
ALL_PSPACES
#define ALL_PSPACES(pspace)
Definition: progspace.h:243
address_space
Definition: progspace.h:216
XCNEW
#define XCNEW(T)
Definition: poison.h:121
gdbcore.h
inf::pid
pid_t pid
Definition: gnu-nat.c:187
release_program_space
static void release_program_space(struct program_space *pspace)
Definition: progspace.c:145
ui_out::field_int
void field_int(const char *fldname, int value)
Definition: ui-out.c:486
ui_out::table_body
void table_body()
Definition: ui-out.c:379
scoped_restore_current_program_space
Definition: progspace.h:277
program_space::pspace_exec_filename
char * pspace_exec_filename
Definition: progspace.h:155
target_pid_to_str
const char * target_pid_to_str(ptid_t ptid)
Definition: target.c:2194
VEC_free
#define VEC_free(T, V)
Definition: vec.h:196
address_space_num
int address_space_num(struct address_space *aspace)
Definition: progspace.c:94
new_address_space
struct address_space * new_address_space(void)
Definition: progspace.c:57
current_program_space
struct program_space * current_program_space
Definition: progspace.c:35
exec_close
void exec_close(void)
Definition: exec.c:83
free_address_space
static void free_address_space(struct address_space *aspace)
Definition: progspace.c:87
find_inferior_for_program_space
struct inferior * find_inferior_for_program_space(struct program_space *pspace)
Definition: inferior.c:329
reinit_frame_cache
void reinit_frame_cache(void)
Definition: frame.c:1809
class_maintenance
Definition: command.h:43
gdbcmd.h
clear_section_table
void clear_section_table(struct target_section_table *table)
Definition: exec.c:472
ui_out_emit_table
Definition: ui-out.h:219
error
void error(const char *fmt,...)
Definition: errors.c:38
program_space::next
struct program_space * next
Definition: progspace.h:140
program_spaces
struct program_space * program_spaces
Definition: progspace.c:32
program_space
Definition: progspace.h:137
parse_and_eval_long
LONGEST parse_and_eval_long(const char *exp)
Definition: eval.c:111