]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/progspace.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / progspace.c
CommitLineData
6c95b8df
PA
1/* Program and address space management, for GDB, the GNU debugger.
2
d01e8234 3 Copyright (C) 2009-2025 Free Software Foundation, Inc.
6c95b8df
PA
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
5b9707eb 20#include "cli/cli-cmds.h"
6c95b8df 21#include "objfiles.h"
6c95b8df
PA
22#include "gdbcore.h"
23#include "solib.h"
24#include "gdbthread.h"
00431a78 25#include "inferior.h"
d0801dd8 26#include <algorithm>
972f7a4b 27#include "cli/cli-style.h"
59912fb2 28#include "observable.h"
6c95b8df
PA
29
30/* The last program space number assigned. */
6bd434d6 31static int last_program_space_num = 0;
6c95b8df
PA
32
33/* The head of the program spaces list. */
94c93c35 34std::vector<struct program_space *> program_spaces;
6c95b8df
PA
35
36/* Pointer to the current program space. */
37struct program_space *current_program_space;
38
39/* The last address space number assigned. */
40static int highest_address_space_num;
41
8e260fc0
TT
42\f
43
6c95b8df
PA
44/* Create a new address space object, and add it to the list. */
45
b382c166
TT
46address_space::address_space ()
47 : m_num (++highest_address_space_num)
6c95b8df 48{
6c95b8df
PA
49}
50
51/* Maybe create a new address space object, and add it to the list, or
52 return a pointer to an existing address space, in case inferiors
53 share an address space on this target system. */
54
f9582a22
TV
55address_space_ref_ptr
56maybe_new_address_space ()
6c95b8df 57{
99d9c3b9
SM
58 int shared_aspace
59 = gdbarch_has_shared_address_space (current_inferior ()->arch ());
6c95b8df
PA
60
61 if (shared_aspace)
62 {
63 /* Just return the first in the list. */
94c93c35 64 return program_spaces[0]->aspace;
6c95b8df
PA
65 }
66
f9582a22 67 return new_address_space ();
6c95b8df
PA
68}
69
70/* Start counting over from scratch. */
71
72static void
73init_address_spaces (void)
74{
75 highest_address_space_num = 0;
76}
77
78\f
79
381ce63f
PA
80/* Remove a program space from the program spaces list. */
81
82static void
83remove_program_space (program_space *pspace)
84{
381ce63f
PA
85 gdb_assert (pspace != NULL);
86
94c93c35
TT
87 auto iter = std::find (program_spaces.begin (), program_spaces.end (),
88 pspace);
89 gdb_assert (iter != program_spaces.end ());
90 program_spaces.erase (iter);
381ce63f
PA
91}
92
93/* See progspace.h. */
94
f9582a22 95program_space::program_space (address_space_ref_ptr aspace_)
381ce63f 96 : num (++last_program_space_num),
f9582a22 97 aspace (std::move (aspace_))
381ce63f 98{
94c93c35 99 program_spaces.push_back (this);
59912fb2 100 gdb::observers::new_program_space.notify (this);
381ce63f
PA
101}
102
103/* See progspace.h. */
6c95b8df 104
564b1e3f 105program_space::~program_space ()
6c95b8df 106{
564b1e3f 107 gdb_assert (this != current_program_space);
6c95b8df 108
59912fb2 109 gdb::observers::free_program_space.notify (this);
381ce63f
PA
110 remove_program_space (this);
111
5ed8105e
PA
112 scoped_restore_current_program_space restore_pspace;
113
564b1e3f 114 set_current_program_space (this);
6c95b8df 115
564b1e3f 116 breakpoint_program_space_exit (this);
b8c9d0de 117 no_shared_libraries (this);
6c95b8df 118 free_all_objfiles ();
f3c469b9
PA
119 /* Defer breakpoint re-set because we don't want to create new
120 locations for this pspace which we're tearing down. */
121 clear_symtab_users (SYMFILE_DEFER_BP_RESET);
6c95b8df
PA
122}
123
7cac64af
TT
124/* See progspace.h. */
125
fa15972b
SM
126bool
127program_space::multi_objfile_p () const
128{
0fb43ab5
SM
129 return (!m_objfiles_list.empty ()
130 && std::next (m_objfiles_list.begin ()) != m_objfiles_list.end ());
fa15972b
SM
131}
132
133/* See progspace.h. */
134
343cc952
TT
135void
136program_space::free_all_objfiles ()
137{
343cc952 138 /* Any objfile reference would become stale. */
4113c737 139 for (const solib &so : this->solibs ())
8971d278 140 gdb_assert (so.objfile == NULL);
343cc952 141
0fb43ab5
SM
142 while (!m_objfiles_list.empty ())
143 this->remove_objfile (&m_objfiles_list.front ());
343cc952
TT
144}
145
146/* See progspace.h. */
147
7cac64af 148void
e2904e1f 149program_space::add_objfile (std::unique_ptr<objfile> &&objfile,
7d7167ce 150 struct objfile *before)
7cac64af 151{
d0801dd8 152 if (before == nullptr)
0fb43ab5 153 m_objfiles_list.push_back (std::move (objfile));
d0801dd8 154 else
7cac64af 155 {
fa15972b 156 gdb_assert (before->is_linked ());
0fb43ab5
SM
157 m_objfiles_list.insert (m_objfiles_list.iterator_to (*before),
158 std::move (objfile));
7cac64af 159 }
7cac64af
TT
160}
161
23452926
TT
162/* See progspace.h. */
163
164void
165program_space::remove_objfile (struct objfile *objfile)
166{
27c7b875
PA
167 /* Removing an objfile from the objfile list invalidates any frame
168 that was built using frame info found in the objfile. Reinit the
169 frame cache to get rid of any frame that might otherwise
170 reference stale info. */
171 reinit_frame_cache ();
172
d0801dd8
TT
173 if (objfile == symfile_object_file)
174 symfile_object_file = NULL;
fa15972b
SM
175
176 gdb_assert (objfile->is_linked ());
0fb43ab5 177 m_objfiles_list.erase (m_objfiles_list.iterator_to (*objfile));
deeafabb
TT
178}
179
a1fd1ac9
TT
180/* See progspace.h. */
181
27b2eff1
TT
182struct objfile *
183program_space::objfile_for_address (CORE_ADDR address)
184{
185 for (auto iter : objfiles ())
186 {
187 /* Don't check separate debug objfiles. */
188 if (iter->separate_debug_objfile_backlink != nullptr)
189 continue;
190 if (is_addr_in_objfile (address, iter))
191 return iter;
192 }
193 return nullptr;
194}
195
196/* See progspace.h. */
197
8a4f1402
TT
198void
199program_space::exec_close ()
200{
19f6550e 201 if (ebfd != nullptr)
8a4f1402 202 {
8a4f1402 203 /* Removing target sections may close the exec_ops target.
aab91c55
GL
204 Clear ebfd before doing so to prevent recursion. We
205 move it to another ref_ptr instead of saving it to a raw
206 pointer to avoid it looking like possible use-after-free. */
cebc3042 207 gdb_bfd_ref_ptr saved_ebfd = std::move (ebfd);
19f6550e 208 ebfd.reset (nullptr);
8a4f1402
TT
209 ebfd_mtime = 0;
210
aab91c55 211 remove_target_sections (saved_ebfd.get ());
8a4f1402 212
9ad8c583 213 m_exec_filename.reset ();
8a4f1402
TT
214 }
215}
216
6c95b8df
PA
217/* Copies program space SRC to DEST. Copies the main executable file,
218 and the main symbol file. Returns DEST. */
219
220struct program_space *
221clone_program_space (struct program_space *dest, struct program_space *src)
222{
5ed8105e 223 scoped_restore_current_program_space restore_pspace;
6c95b8df
PA
224
225 set_current_program_space (dest);
226
9ad8c583
SM
227 if (src->exec_filename () != nullptr)
228 exec_file_attach (src->exec_filename (), 0);
6c95b8df
PA
229
230 if (src->symfile_object_file != NULL)
b2e586e8
SM
231 symbol_file_add_main (objfile_name (src->symfile_object_file),
232 SYMFILE_DEFER_BP_RESET);
6c95b8df 233
6c95b8df
PA
234 return dest;
235}
236
237/* Sets PSPACE as the current program space. It is the caller's
238 responsibility to make sure that the currently selected
239 inferior/thread matches the selected program space. */
240
241void
242set_current_program_space (struct program_space *pspace)
243{
244 if (current_program_space == pspace)
245 return;
246
247 gdb_assert (pspace != NULL);
248
249 current_program_space = pspace;
250
251 /* Different symbols change our view of the frame chain. */
252 reinit_frame_cache ();
253}
254
6c95b8df
PA
255/* Returns true iff there's no inferior bound to PSPACE. */
256
004eecfd
TT
257bool
258program_space::empty ()
6c95b8df 259{
004eecfd 260 return find_inferior_for_program_space (this) == nullptr;
6c95b8df
PA
261}
262
6c95b8df
PA
263/* Prints the list of program spaces and their details on UIOUT. If
264 REQUESTED is not -1, it's the ID of the pspace that should be
265 printed. Otherwise, all spaces are printed. */
266
267static void
268print_program_space (struct ui_out *uiout, int requested)
269{
6c95b8df 270 int count = 0;
6c95b8df 271
5d80df4a
AB
272 /* Start with a minimum width of 17 for the executable name column. */
273 size_t longest_exec_name = 17;
274
6c95b8df 275 /* Compute number of pspaces we will print. */
94c93c35 276 for (struct program_space *pspace : program_spaces)
6c95b8df
PA
277 {
278 if (requested != -1 && pspace->num != requested)
279 continue;
280
9ad8c583
SM
281 if (pspace->exec_filename () != nullptr)
282 longest_exec_name = std::max (strlen (pspace->exec_filename ()),
5d80df4a
AB
283 longest_exec_name);
284
6c95b8df
PA
285 ++count;
286 }
287
288 /* There should always be at least one. */
289 gdb_assert (count > 0);
290
5d80df4a 291 ui_out_emit_table table_emitter (uiout, 4, count, "pspaces");
112e8700
SM
292 uiout->table_header (1, ui_left, "current", "");
293 uiout->table_header (4, ui_left, "id", "Id");
5d80df4a
AB
294 uiout->table_header (longest_exec_name, ui_left, "exec", "Executable");
295 uiout->table_header (17, ui_left, "core", "Core File");
112e8700 296 uiout->table_body ();
6c95b8df 297
94c93c35 298 for (struct program_space *pspace : program_spaces)
6c95b8df 299 {
6c95b8df
PA
300 int printed_header;
301
302 if (requested != -1 && requested != pspace->num)
303 continue;
304
2e783024 305 ui_out_emit_tuple tuple_emitter (uiout, NULL);
6c95b8df
PA
306
307 if (pspace == current_program_space)
112e8700 308 uiout->field_string ("current", "*");
6c95b8df 309 else
112e8700 310 uiout->field_skip ("current");
6c95b8df 311
381befee 312 uiout->field_signed ("id", pspace->num);
6c95b8df 313
9ad8c583
SM
314 if (pspace->exec_filename () != nullptr)
315 uiout->field_string ("exec", pspace->exec_filename (),
972f7a4b 316 file_name_style.style ());
6c95b8df 317 else
112e8700 318 uiout->field_skip ("exec");
6c95b8df 319
5d80df4a
AB
320 if (pspace->cbfd != nullptr)
321 uiout->field_string ("core", bfd_get_filename (pspace->cbfd.get ()),
322 file_name_style.style ());
323 else
324 uiout->field_skip ("core");
325
6c95b8df
PA
326 /* Print extra info that doesn't really fit in tabular form.
327 Currently, we print the list of inferiors bound to a pspace.
328 There can be more than one inferior bound to the same pspace,
329 e.g., both parent/child inferiors in a vfork, or, on targets
330 that share pspaces between inferiors. */
331 printed_header = 0;
f7c7700d
PA
332
333 /* We're going to switch inferiors. */
334 scoped_restore_current_thread restore_thread;
335
336 for (inferior *inf : all_inferiors ())
6c95b8df
PA
337 if (inf->pspace == pspace)
338 {
f7c7700d
PA
339 /* Switch to inferior in order to call target methods. */
340 switch_to_inferior_no_thread (inf);
341
6c95b8df
PA
342 if (!printed_header)
343 {
344 printed_header = 1;
6cb06a8c
TT
345 gdb_printf ("\n\tBound inferiors: ID %d (%s)",
346 inf->num,
347 target_pid_to_str (ptid_t (inf->pid)).c_str ());
6c95b8df
PA
348 }
349 else
6cb06a8c
TT
350 gdb_printf (", ID %d (%s)",
351 inf->num,
352 target_pid_to_str (ptid_t (inf->pid)).c_str ());
6c95b8df
PA
353 }
354
112e8700 355 uiout->text ("\n");
6c95b8df 356 }
6c95b8df
PA
357}
358
359/* Boolean test for an already-known program space id. */
360
361static int
362valid_program_space_id (int num)
363{
94c93c35 364 for (struct program_space *pspace : program_spaces)
6c95b8df
PA
365 if (pspace->num == num)
366 return 1;
367
368 return 0;
369}
370
371/* If ARGS is NULL or empty, print information about all program
372 spaces. Otherwise, ARGS is a text representation of a LONG
373 indicating which the program space to print information about. */
374
375static void
9c504b5d 376maintenance_info_program_spaces_command (const char *args, int from_tty)
6c95b8df
PA
377{
378 int requested = -1;
379
380 if (args && *args)
381 {
382 requested = parse_and_eval_long (args);
383 if (!valid_program_space_id (requested))
384 error (_("program space ID %d not known."), requested);
385 }
386
79a45e25 387 print_program_space (current_uiout, requested);
6c95b8df
PA
388}
389
6c95b8df
PA
390/* Update all program spaces matching to address spaces. The user may
391 have created several program spaces, and loaded executables into
392 them before connecting to the target interface that will create the
393 inferiors. All that happens before GDB has a chance to know if the
394 inferiors will share an address space or not. Call this after
395 having connected to the target interface and having fetched the
396 target description, to fixup the program/address spaces mappings.
397
398 It is assumed that there are no bound inferiors yet, otherwise,
399 they'd be left with stale referenced to released aspaces. */
400
401void
402update_address_spaces (void)
403{
99d9c3b9
SM
404 int shared_aspace
405 = gdbarch_has_shared_address_space (current_inferior ()->arch ());
6c95b8df
PA
406
407 init_address_spaces ();
408
7e9af34a 409 if (shared_aspace)
6c95b8df 410 {
f9582a22 411 address_space_ref_ptr aspace = new_address_space ();
ad3bbd48 412
94c93c35 413 for (struct program_space *pspace : program_spaces)
7e9af34a 414 pspace->aspace = aspace;
6c95b8df 415 }
7e9af34a 416 else
94c93c35 417 for (struct program_space *pspace : program_spaces)
f9582a22 418 pspace->aspace = new_address_space ();
7e9af34a 419
08bdefb5 420 for (inferior *inf : all_inferiors ())
99d9c3b9 421 if (gdbarch_has_global_solist (current_inferior ()->arch ()))
7e9af34a
DJ
422 inf->aspace = maybe_new_address_space ();
423 else
424 inf->aspace = inf->pspace->aspace;
6c95b8df
PA
425}
426
6c95b8df
PA
427\f
428
edcc5120
TT
429/* See progspace.h. */
430
431void
e39fb971 432program_space::clear_solib_cache ()
edcc5120 433{
e39fb971
TT
434 added_solibs.clear ();
435 deleted_solibs.clear ();
edcc5120
TT
436}
437
7b21ae94 438/* See progspace.h. */
edcc5120 439
6c95b8df 440void
7b21ae94 441initialize_progspace ()
6c95b8df
PA
442{
443 add_cmd ("program-spaces", class_maintenance,
3e43a32a
MS
444 maintenance_info_program_spaces_command,
445 _("Info about currently known program spaces."),
6c95b8df
PA
446 &maintenanceinfolist);
447
448 /* There's always one program space. Note that this function isn't
449 an automatic _initialize_foo function, since other
450 _initialize_foo routines may need to install their per-pspace
451 data keys. We can only allocate a progspace when all those
452 modules have done that. Do this before
7e10abd1
TT
453 initialize_current_architecture, because that accesses the ebfd
454 of current_program_space. */
f9582a22 455 current_program_space = new program_space (new_address_space ());
6c95b8df 456}