GDB (xrefs)
Loading...
Searching...
No Matches
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-2023 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 "solist.h"
27#include "gdbthread.h"
28#include "inferior.h"
29#include <algorithm>
30#include "cli/cli-style.h"
31#include "observable.h"
32
33/* The last program space number assigned. */
35
36/* The head of the program spaces list. */
37std::vector<struct program_space *> program_spaces;
38
39/* Pointer to the current program space. */
41
42/* The last address space number assigned. */
44
45
46
47/* Create a new address space object, and add it to the list. */
48
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
58struct address_space *
60{
61 int shared_aspace = gdbarch_has_shared_address_space (target_gdbarch ());
62
63 if (shared_aspace)
64 {
65 /* Just return the first in the list. */
66 return program_spaces[0]->aspace;
67 }
68
69 return new address_space ();
70}
71
72/* Start counting over from scratch. */
73
74static void
79
80
81
82/* Remove a program space from the program spaces list. */
83
84static void
86{
87 gdb_assert (pspace != NULL);
88
89 auto iter = std::find (program_spaces.begin (), program_spaces.end (),
90 pspace);
91 gdb_assert (iter != program_spaces.end ());
92 program_spaces.erase (iter);
93}
94
95/* See progspace.h. */
96
99 aspace (aspace_)
100{
101 program_spaces.push_back (this);
103}
104
105/* See progspace.h. */
106
108{
109 gdb_assert (this != current_program_space);
110
113
115
117
119 no_shared_libraries (NULL, 0);
121 /* Defer breakpoint re-set because we don't want to create new
122 locations for this pspace which we're tearing down. */
125 delete this->aspace;
126}
127
128/* See progspace.h. */
129
130void
132{
133 /* Any objfile reference would become stale. */
134 for (struct so_list *so : current_program_space->solibs ())
135 gdb_assert (so->objfile == NULL);
136
137 while (!objfiles_list.empty ())
138 objfiles_list.front ()->unlink ();
139}
140
141/* See progspace.h. */
142
143void
144program_space::add_objfile (std::unique_ptr<objfile> &&objfile,
145 struct objfile *before)
146{
147 if (before == nullptr)
148 objfiles_list.push_back (std::move (objfile));
149 else
150 {
151 auto iter = std::find_if (objfiles_list.begin (), objfiles_list.end (),
152 [=] (const std::unique_ptr<::objfile> &objf)
153 {
154 return objf.get () == before;
155 });
156 gdb_assert (iter != objfiles_list.end ());
157 objfiles_list.insert (iter, std::move (objfile));
158 }
159}
160
161/* See progspace.h. */
162
163void
165{
166 /* Removing an objfile from the objfile list invalidates any frame
167 that was built using frame info found in the objfile. Reinit the
168 frame cache to get rid of any frame that might otherwise
169 reference stale info. */
171
172 auto iter = std::find_if (objfiles_list.begin (), objfiles_list.end (),
173 [=] (const std::unique_ptr<::objfile> &objf)
174 {
175 return objf.get () == objfile;
176 });
177 gdb_assert (iter != objfiles_list.end ());
178 objfiles_list.erase (iter);
179
181 symfile_object_file = NULL;
182}
183
184/* See progspace.h. */
185
186struct objfile *
188{
189 for (auto iter : objfiles ())
190 {
191 /* Don't check separate debug objfiles. */
192 if (iter->separate_debug_objfile_backlink != nullptr)
193 continue;
194 if (is_addr_in_objfile (address, iter))
195 return iter;
196 }
197 return nullptr;
198}
199
200/* See progspace.h. */
201
202void
204{
205 if (ebfd != nullptr)
206 {
207 /* Removing target sections may close the exec_ops target.
208 Clear ebfd before doing so to prevent recursion. */
209 ebfd.reset (nullptr);
210 ebfd_mtime = 0;
211
213
214 exec_filename.reset (nullptr);
215 }
216}
217
218/* Copies program space SRC to DEST. Copies the main executable file,
219 and the main symbol file. Returns DEST. */
220
221struct program_space *
223{
225
227
228 if (src->exec_filename != NULL)
229 exec_file_attach (src->exec_filename.get (), 0);
230
231 if (src->symfile_object_file != NULL)
234
235 return dest;
236}
237
238/* Sets PSPACE as the current program space. It is the caller's
239 responsibility to make sure that the currently selected
240 inferior/thread matches the selected program space. */
241
242void
244{
245 if (current_program_space == pspace)
246 return;
247
248 gdb_assert (pspace != NULL);
249
250 current_program_space = pspace;
251
252 /* Different symbols change our view of the frame chain. */
254}
255
256/* Returns true iff there's no inferior bound to PSPACE. */
257
258bool
260{
261 return find_inferior_for_program_space (this) == nullptr;
262}
263
264/* Prints the list of program spaces and their details on UIOUT. If
265 REQUESTED is not -1, it's the ID of the pspace that should be
266 printed. Otherwise, all spaces are printed. */
267
268static void
269print_program_space (struct ui_out *uiout, int requested)
270{
271 int count = 0;
272
273 /* Start with a minimum width of 17 for the executable name column. */
274 size_t longest_exec_name = 17;
275
276 /* Compute number of pspaces we will print. */
277 for (struct program_space *pspace : program_spaces)
278 {
279 if (requested != -1 && pspace->num != requested)
280 continue;
281
282 if (pspace->exec_filename != nullptr)
283 longest_exec_name = std::max (strlen (pspace->exec_filename.get ()),
284 longest_exec_name);
285
286 ++count;
287 }
288
289 /* There should always be at least one. */
290 gdb_assert (count > 0);
291
292 ui_out_emit_table table_emitter (uiout, 4, count, "pspaces");
293 uiout->table_header (1, ui_left, "current", "");
294 uiout->table_header (4, ui_left, "id", "Id");
295 uiout->table_header (longest_exec_name, ui_left, "exec", "Executable");
296 uiout->table_header (17, ui_left, "core", "Core File");
297 uiout->table_body ();
298
299 for (struct program_space *pspace : program_spaces)
300 {
301 int printed_header;
302
303 if (requested != -1 && requested != pspace->num)
304 continue;
305
306 ui_out_emit_tuple tuple_emitter (uiout, NULL);
307
308 if (pspace == current_program_space)
309 uiout->field_string ("current", "*");
310 else
311 uiout->field_skip ("current");
312
313 uiout->field_signed ("id", pspace->num);
314
315 if (pspace->exec_filename != nullptr)
316 uiout->field_string ("exec", pspace->exec_filename.get (),
318 else
319 uiout->field_skip ("exec");
320
321 if (pspace->cbfd != nullptr)
322 uiout->field_string ("core", bfd_get_filename (pspace->cbfd.get ()),
324 else
325 uiout->field_skip ("core");
326
327 /* Print extra info that doesn't really fit in tabular form.
328 Currently, we print the list of inferiors bound to a pspace.
329 There can be more than one inferior bound to the same pspace,
330 e.g., both parent/child inferiors in a vfork, or, on targets
331 that share pspaces between inferiors. */
332 printed_header = 0;
333
334 /* We're going to switch inferiors. */
335 scoped_restore_current_thread restore_thread;
336
337 for (inferior *inf : all_inferiors ())
338 if (inf->pspace == pspace)
339 {
340 /* Switch to inferior in order to call target methods. */
342
343 if (!printed_header)
344 {
345 printed_header = 1;
346 gdb_printf ("\n\tBound inferiors: ID %d (%s)",
347 inf->num,
348 target_pid_to_str (ptid_t (inf->pid)).c_str ());
349 }
350 else
351 gdb_printf (", ID %d (%s)",
352 inf->num,
353 target_pid_to_str (ptid_t (inf->pid)).c_str ());
354 }
355
356 uiout->text ("\n");
357 }
358}
359
360/* Boolean test for an already-known program space id. */
361
362static int
364{
365 for (struct program_space *pspace : program_spaces)
366 if (pspace->num == num)
367 return 1;
368
369 return 0;
370}
371
372/* If ARGS is NULL or empty, print information about all program
373 spaces. Otherwise, ARGS is a text representation of a LONG
374 indicating which the program space to print information about. */
375
376static void
377maintenance_info_program_spaces_command (const char *args, int from_tty)
378{
379 int requested = -1;
380
381 if (args && *args)
382 {
383 requested = parse_and_eval_long (args);
384 if (!valid_program_space_id (requested))
385 error (_("program space ID %d not known."), requested);
386 }
387
389}
390
391/* Update all program spaces matching to address spaces. The user may
392 have created several program spaces, and loaded executables into
393 them before connecting to the target interface that will create the
394 inferiors. All that happens before GDB has a chance to know if the
395 inferiors will share an address space or not. Call this after
396 having connected to the target interface and having fetched the
397 target description, to fixup the program/address spaces mappings.
398
399 It is assumed that there are no bound inferiors yet, otherwise,
400 they'd be left with stale referenced to released aspaces. */
401
402void
404{
405 int shared_aspace = gdbarch_has_shared_address_space (target_gdbarch ());
406
408
409 if (shared_aspace)
410 {
411 struct address_space *aspace = new address_space ();
412
414 for (struct program_space *pspace : program_spaces)
415 pspace->aspace = aspace;
416 }
417 else
418 for (struct program_space *pspace : program_spaces)
419 {
420 delete pspace->aspace;
421 pspace->aspace = new address_space ();
422 }
423
424 for (inferior *inf : all_inferiors ())
426 inf->aspace = maybe_new_address_space ();
427 else
428 inf->aspace = inf->pspace->aspace;
429}
430
431
432
433/* See progspace.h. */
434
435void
437{
438 added_solibs.clear ();
439 deleted_solibs.clear ();
440}
441
442
443
444void
446{
447 add_cmd ("program-spaces", class_maintenance,
449 _("Info about currently known program spaces."),
451
452 /* There's always one program space. Note that this function isn't
453 an automatic _initialize_foo function, since other
454 _initialize_foo routines may need to install their per-pspace
455 data keys. We can only allocate a progspace when all those
456 modules have done that. Do this before
457 initialize_current_architecture, because that accesses the ebfd
458 of current_program_space. */
460}
struct gdbarch * target_gdbarch(void)
void breakpoint_program_space_exit(struct program_space *pspace)
ui_file_style style() const
Definition cli-style.c:169
void field_string(const char *fldname, const char *string, const ui_file_style &style=ui_file_style())
Definition ui-out.c:511
void field_signed(const char *fldname, LONGEST value)
Definition ui-out.c:437
void field_skip(const char *fldname)
Definition ui-out.c:499
void text(const char *string)
Definition ui-out.c:566
void table_header(int width, ui_align align, const std::string &col_name, const std::string &col_hdr)
Definition ui-out.c:363
void table_body()
Definition ui-out.c:376
struct cmd_list_element * maintenanceinfolist
Definition cli-cmds.c:147
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:233
cli_style_option file_name_style
@ class_maintenance
Definition command.h:65
LONGEST parse_and_eval_long(const char *exp)
Definition eval.c:62
void exec_file_attach(const char *filename, int from_tty)
Definition exec.c:365
void reinit_frame_cache(void)
Definition frame.c:2107
int gdbarch_has_shared_address_space(struct gdbarch *gdbarch)
Definition gdbarch.c:4888
int gdbarch_has_global_solist(struct gdbarch *gdbarch)
Definition gdbarch.c:4854
struct inferior * find_inferior_for_program_space(struct program_space *pspace)
Definition inferior.c:414
void switch_to_inferior_no_thread(inferior *inf)
Definition inferior.c:712
all_inferiors_range all_inferiors(process_stratum_target *proc_target=nullptr)
Definition inferior.h:821
observable< program_space * > new_program_space
observable< program_space * > free_program_space
bool is_addr_in_objfile(CORE_ADDR addr, const struct objfile *objfile)
Definition objfiles.c:1206
const char * objfile_name(const struct objfile *objfile)
Definition objfiles.c:1259
struct program_space * current_program_space
Definition progspace.c:40
static void init_address_spaces(void)
Definition progspace.c:75
void set_current_program_space(struct program_space *pspace)
Definition progspace.c:243
static void maintenance_info_program_spaces_command(const char *args, int from_tty)
Definition progspace.c:377
struct program_space * clone_program_space(struct program_space *dest, struct program_space *src)
Definition progspace.c:222
static void remove_program_space(program_space *pspace)
Definition progspace.c:85
static int highest_address_space_num
Definition progspace.c:43
void initialize_progspace(void)
Definition progspace.c:445
struct address_space * maybe_new_address_space(void)
Definition progspace.c:59
static void print_program_space(struct ui_out *uiout, int requested)
Definition progspace.c:269
void update_address_spaces(void)
Definition progspace.c:403
static int last_program_space_num
Definition progspace.c:34
static int valid_program_space_id(int num)
Definition progspace.c:363
std::vector< struct program_space * > program_spaces
Definition progspace.c:37
struct program_space * current_program_space
Definition progspace.c:40
void set_current_program_space(struct program_space *pspace)
Definition progspace.c:243
std::vector< struct program_space * > program_spaces
Definition progspace.c:37
void no_shared_libraries(const char *ignored, int from_tty)
Definition solib.c:1284
int num() const
Definition progspace.h:396
Definition gnu-nat.c:153
pid_t pid
Definition gnu-nat.c:165
void remove_target_sections(void *owner)
Definition exec.c:654
objfiles_range objfiles()
Definition progspace.h:209
void remove_objfile(struct objfile *objfile)
Definition progspace.c:164
gdb_bfd_ref_ptr ebfd
Definition progspace.h:319
std::vector< std::string > deleted_solibs
Definition progspace.h:375
void exec_close()
Definition progspace.c:203
std::vector< struct so_list * > added_solibs
Definition progspace.h:371
void add_objfile(std::unique_ptr< objfile > &&objfile, struct objfile *before)
Definition progspace.c:144
struct objfile * objfile_for_address(CORE_ADDR address)
Definition progspace.c:187
struct address_space * aspace
Definition progspace.h:339
program_space(address_space *aspace)
Definition progspace.c:97
gdb::unique_xmalloc_ptr< char > exec_filename
Definition progspace.h:325
struct objfile * symfile_object_file
Definition progspace.h:357
std::list< std::unique_ptr< objfile > > objfiles_list
Definition progspace.h:360
void free_all_objfiles()
Definition progspace.c:131
void clear_solib_cache()
Definition progspace.c:436
so_list_range solibs() const
Definition progspace.h:260
@ SYMFILE_DEFER_BP_RESET
void symbol_file_add_main(const char *args, symfile_add_flags add_flags)
Definition symfile.c:1186
void clear_symtab_users(symfile_add_flags add_flags)
Definition symfile.c:2905
std::string target_pid_to_str(ptid_t ptid)
Definition target.c:2623
@ ui_left
Definition ui-out.h:45
#define current_uiout
Definition ui-out.h:40
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886