]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame_incremental - gdb/progspace.c
[gdb/testsuite] Fix gdb.arch/amd64-disp-step-avx.exp on x86_64-freebsd
[thirdparty/binutils-gdb.git] / gdb / progspace.c
... / ...
CommitLineData
1/* Program and address space management, for GDB, the GNU debugger.
2
3 Copyright (C) 2009-2025 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 "cli/cli-cmds.h"
21#include "objfiles.h"
22#include "gdbcore.h"
23#include "solib.h"
24#include "gdbthread.h"
25#include "inferior.h"
26#include <algorithm>
27#include "cli/cli-style.h"
28#include "observable.h"
29
30/* The last program space number assigned. */
31static int last_program_space_num = 0;
32
33/* The head of the program spaces list. */
34std::vector<struct program_space *> program_spaces;
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
42\f
43
44/* Create a new address space object, and add it to the list. */
45
46address_space::address_space ()
47 : m_num (++highest_address_space_num)
48{
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
55address_space_ref_ptr
56maybe_new_address_space ()
57{
58 int shared_aspace
59 = gdbarch_has_shared_address_space (current_inferior ()->arch ());
60
61 if (shared_aspace)
62 {
63 /* Just return the first in the list. */
64 return program_spaces[0]->aspace;
65 }
66
67 return new_address_space ();
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
80/* Remove a program space from the program spaces list. */
81
82static void
83remove_program_space (program_space *pspace)
84{
85 gdb_assert (pspace != NULL);
86
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);
91}
92
93/* See progspace.h. */
94
95program_space::program_space (address_space_ref_ptr aspace_)
96 : num (++last_program_space_num),
97 aspace (std::move (aspace_))
98{
99 program_spaces.push_back (this);
100 gdb::observers::new_program_space.notify (this);
101}
102
103/* See progspace.h. */
104
105program_space::~program_space ()
106{
107 gdb_assert (this != current_program_space);
108
109 gdb::observers::free_program_space.notify (this);
110 remove_program_space (this);
111
112 scoped_restore_current_program_space restore_pspace;
113
114 set_current_program_space (this);
115
116 breakpoint_program_space_exit (this);
117 no_shared_libraries (this);
118 free_all_objfiles ();
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);
122}
123
124/* See progspace.h. */
125
126bool
127program_space::multi_objfile_p () const
128{
129 return (!m_objfiles_list.empty ()
130 && std::next (m_objfiles_list.begin ()) != m_objfiles_list.end ());
131}
132
133/* See progspace.h. */
134
135void
136program_space::free_all_objfiles ()
137{
138 /* Any objfile reference would become stale. */
139 for (const solib &so : this->solibs ())
140 gdb_assert (so.objfile == NULL);
141
142 while (!m_objfiles_list.empty ())
143 this->remove_objfile (&m_objfiles_list.front ());
144}
145
146/* See progspace.h. */
147
148void
149program_space::add_objfile (std::unique_ptr<objfile> &&objfile,
150 struct objfile *before)
151{
152 if (before == nullptr)
153 m_objfiles_list.push_back (std::move (objfile));
154 else
155 {
156 gdb_assert (before->is_linked ());
157 m_objfiles_list.insert (m_objfiles_list.iterator_to (*before),
158 std::move (objfile));
159 }
160}
161
162/* See progspace.h. */
163
164void
165program_space::remove_objfile (struct objfile *objfile)
166{
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
173 if (objfile == symfile_object_file)
174 symfile_object_file = NULL;
175
176 gdb_assert (objfile->is_linked ());
177 m_objfiles_list.erase (m_objfiles_list.iterator_to (*objfile));
178}
179
180/* See progspace.h. */
181
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
198void
199program_space::exec_close ()
200{
201 if (ebfd != nullptr)
202 {
203 /* Removing target sections may close the exec_ops target.
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. */
207 gdb_bfd_ref_ptr saved_ebfd = std::move (ebfd);
208 ebfd.reset (nullptr);
209 ebfd_mtime = 0;
210
211 remove_target_sections (saved_ebfd.get ());
212
213 m_exec_filename.reset ();
214 }
215}
216
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{
223 scoped_restore_current_program_space restore_pspace;
224
225 set_current_program_space (dest);
226
227 if (src->exec_filename () != nullptr)
228 exec_file_attach (src->exec_filename (), 0);
229
230 if (src->symfile_object_file != NULL)
231 symbol_file_add_main (objfile_name (src->symfile_object_file),
232 SYMFILE_DEFER_BP_RESET);
233
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
255/* Returns true iff there's no inferior bound to PSPACE. */
256
257bool
258program_space::empty ()
259{
260 return find_inferior_for_program_space (this) == nullptr;
261}
262
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{
270 int count = 0;
271
272 /* Start with a minimum width of 17 for the executable name column. */
273 size_t longest_exec_name = 17;
274
275 /* Compute number of pspaces we will print. */
276 for (struct program_space *pspace : program_spaces)
277 {
278 if (requested != -1 && pspace->num != requested)
279 continue;
280
281 if (pspace->exec_filename () != nullptr)
282 longest_exec_name = std::max (strlen (pspace->exec_filename ()),
283 longest_exec_name);
284
285 ++count;
286 }
287
288 /* There should always be at least one. */
289 gdb_assert (count > 0);
290
291 ui_out_emit_table table_emitter (uiout, 4, count, "pspaces");
292 uiout->table_header (1, ui_left, "current", "");
293 uiout->table_header (4, ui_left, "id", "Id");
294 uiout->table_header (longest_exec_name, ui_left, "exec", "Executable");
295 uiout->table_header (17, ui_left, "core", "Core File");
296 uiout->table_body ();
297
298 for (struct program_space *pspace : program_spaces)
299 {
300 int printed_header;
301
302 if (requested != -1 && requested != pspace->num)
303 continue;
304
305 ui_out_emit_tuple tuple_emitter (uiout, NULL);
306
307 if (pspace == current_program_space)
308 uiout->field_string ("current", "*");
309 else
310 uiout->field_skip ("current");
311
312 uiout->field_signed ("id", pspace->num);
313
314 if (pspace->exec_filename () != nullptr)
315 uiout->field_string ("exec", pspace->exec_filename (),
316 file_name_style.style ());
317 else
318 uiout->field_skip ("exec");
319
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
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;
332
333 /* We're going to switch inferiors. */
334 scoped_restore_current_thread restore_thread;
335
336 for (inferior *inf : all_inferiors ())
337 if (inf->pspace == pspace)
338 {
339 /* Switch to inferior in order to call target methods. */
340 switch_to_inferior_no_thread (inf);
341
342 if (!printed_header)
343 {
344 printed_header = 1;
345 gdb_printf ("\n\tBound inferiors: ID %d (%s)",
346 inf->num,
347 target_pid_to_str (ptid_t (inf->pid)).c_str ());
348 }
349 else
350 gdb_printf (", ID %d (%s)",
351 inf->num,
352 target_pid_to_str (ptid_t (inf->pid)).c_str ());
353 }
354
355 uiout->text ("\n");
356 }
357}
358
359/* Boolean test for an already-known program space id. */
360
361static int
362valid_program_space_id (int num)
363{
364 for (struct program_space *pspace : program_spaces)
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
376maintenance_info_program_spaces_command (const char *args, int from_tty)
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
387 print_program_space (current_uiout, requested);
388}
389
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{
404 int shared_aspace
405 = gdbarch_has_shared_address_space (current_inferior ()->arch ());
406
407 init_address_spaces ();
408
409 if (shared_aspace)
410 {
411 address_space_ref_ptr aspace = new_address_space ();
412
413 for (struct program_space *pspace : program_spaces)
414 pspace->aspace = aspace;
415 }
416 else
417 for (struct program_space *pspace : program_spaces)
418 pspace->aspace = new_address_space ();
419
420 for (inferior *inf : all_inferiors ())
421 if (gdbarch_has_global_solist (current_inferior ()->arch ()))
422 inf->aspace = maybe_new_address_space ();
423 else
424 inf->aspace = inf->pspace->aspace;
425}
426
427\f
428
429/* See progspace.h. */
430
431void
432program_space::clear_solib_cache ()
433{
434 added_solibs.clear ();
435 deleted_solibs.clear ();
436}
437
438/* See progspace.h. */
439
440void
441initialize_progspace ()
442{
443 add_cmd ("program-spaces", class_maintenance,
444 maintenance_info_program_spaces_command,
445 _("Info about currently known program spaces."),
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
453 initialize_current_architecture, because that accesses the ebfd
454 of current_program_space. */
455 current_program_space = new program_space (new_address_space ());
456}