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