GDB (API)
|
00001 /* Program and address space management, for GDB, the GNU debugger. 00002 00003 Copyright (C) 2009-2013 Free Software Foundation, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #include "defs.h" 00021 #include "gdbcmd.h" 00022 #include "objfiles.h" 00023 #include "arch-utils.h" 00024 #include "gdbcore.h" 00025 #include "solib.h" 00026 #include "gdbthread.h" 00027 00028 /* The last program space number assigned. */ 00029 int last_program_space_num = 0; 00030 00031 /* The head of the program spaces list. */ 00032 struct program_space *program_spaces; 00033 00034 /* Pointer to the current program space. */ 00035 struct program_space *current_program_space; 00036 00037 /* The last address space number assigned. */ 00038 static int highest_address_space_num; 00039 00040 00041 00042 /* Keep a registry of per-program_space data-pointers required by other GDB 00043 modules. */ 00044 00045 DEFINE_REGISTRY (program_space, REGISTRY_ACCESS_FIELD) 00046 00047 00048 00049 /* An address space. Currently this is not used for much other than 00050 for comparing if pspaces/inferior/threads see the same address 00051 space. */ 00052 00053 struct address_space 00054 { 00055 int num; 00056 }; 00057 00058 /* Create a new address space object, and add it to the list. */ 00059 00060 struct address_space * 00061 new_address_space (void) 00062 { 00063 struct address_space *aspace; 00064 00065 aspace = XZALLOC (struct address_space); 00066 aspace->num = ++highest_address_space_num; 00067 00068 return aspace; 00069 } 00070 00071 /* Maybe create a new address space object, and add it to the list, or 00072 return a pointer to an existing address space, in case inferiors 00073 share an address space on this target system. */ 00074 00075 struct address_space * 00076 maybe_new_address_space (void) 00077 { 00078 int shared_aspace = gdbarch_has_shared_address_space (target_gdbarch ()); 00079 00080 if (shared_aspace) 00081 { 00082 /* Just return the first in the list. */ 00083 return program_spaces->aspace; 00084 } 00085 00086 return new_address_space (); 00087 } 00088 00089 static void 00090 free_address_space (struct address_space *aspace) 00091 { 00092 xfree (aspace); 00093 } 00094 00095 int 00096 address_space_num (struct address_space *aspace) 00097 { 00098 return aspace->num; 00099 } 00100 00101 /* Start counting over from scratch. */ 00102 00103 static void 00104 init_address_spaces (void) 00105 { 00106 highest_address_space_num = 0; 00107 } 00108 00109 00110 00111 /* Adds a new empty program space to the program space list, and binds 00112 it to ASPACE. Returns the pointer to the new object. */ 00113 00114 struct program_space * 00115 add_program_space (struct address_space *aspace) 00116 { 00117 struct program_space *pspace; 00118 00119 pspace = XZALLOC (struct program_space); 00120 00121 pspace->num = ++last_program_space_num; 00122 pspace->aspace = aspace; 00123 00124 program_space_alloc_data (pspace); 00125 00126 pspace->next = program_spaces; 00127 program_spaces = pspace; 00128 00129 return pspace; 00130 } 00131 00132 /* Releases program space PSPACE, and all its contents (shared 00133 libraries, objfiles, and any other references to the PSPACE in 00134 other modules). It is an internal error to call this when PSPACE 00135 is the current program space, since there should always be a 00136 program space. */ 00137 00138 static void 00139 release_program_space (struct program_space *pspace) 00140 { 00141 struct cleanup *old_chain = save_current_program_space (); 00142 00143 gdb_assert (pspace != current_program_space); 00144 00145 set_current_program_space (pspace); 00146 00147 breakpoint_program_space_exit (pspace); 00148 no_shared_libraries (NULL, 0); 00149 exec_close (); 00150 free_all_objfiles (); 00151 if (!gdbarch_has_shared_address_space (target_gdbarch ())) 00152 free_address_space (pspace->aspace); 00153 resize_section_table (&pspace->target_sections, 00154 -resize_section_table (&pspace->target_sections, 0)); 00155 clear_program_space_solib_cache (pspace); 00156 /* Discard any data modules have associated with the PSPACE. */ 00157 program_space_free_data (pspace); 00158 xfree (pspace); 00159 00160 do_cleanups (old_chain); 00161 } 00162 00163 /* Unlinks PSPACE from the pspace list, and releases it. */ 00164 00165 void 00166 remove_program_space (struct program_space *pspace) 00167 { 00168 struct program_space *ss, **ss_link; 00169 00170 ss = program_spaces; 00171 ss_link = &program_spaces; 00172 while (ss) 00173 { 00174 if (ss != pspace) 00175 { 00176 ss_link = &ss->next; 00177 ss = *ss_link; 00178 continue; 00179 } 00180 00181 *ss_link = ss->next; 00182 release_program_space (ss); 00183 ss = *ss_link; 00184 } 00185 } 00186 00187 /* Copies program space SRC to DEST. Copies the main executable file, 00188 and the main symbol file. Returns DEST. */ 00189 00190 struct program_space * 00191 clone_program_space (struct program_space *dest, struct program_space *src) 00192 { 00193 struct cleanup *old_chain; 00194 00195 old_chain = save_current_program_space (); 00196 00197 set_current_program_space (dest); 00198 00199 if (src->pspace_exec_filename != NULL) 00200 exec_file_attach (src->pspace_exec_filename, 0); 00201 00202 if (src->symfile_object_file != NULL) 00203 symbol_file_add_main (objfile_name (src->symfile_object_file), 0); 00204 00205 do_cleanups (old_chain); 00206 return dest; 00207 } 00208 00209 /* Sets PSPACE as the current program space. It is the caller's 00210 responsibility to make sure that the currently selected 00211 inferior/thread matches the selected program space. */ 00212 00213 void 00214 set_current_program_space (struct program_space *pspace) 00215 { 00216 if (current_program_space == pspace) 00217 return; 00218 00219 gdb_assert (pspace != NULL); 00220 00221 current_program_space = pspace; 00222 00223 /* Different symbols change our view of the frame chain. */ 00224 reinit_frame_cache (); 00225 } 00226 00227 /* A cleanups callback, helper for save_current_program_space 00228 below. */ 00229 00230 static void 00231 restore_program_space (void *arg) 00232 { 00233 struct program_space *saved_pspace = arg; 00234 00235 set_current_program_space (saved_pspace); 00236 } 00237 00238 /* Save the current program space so that it may be restored by a later 00239 call to do_cleanups. Returns the struct cleanup pointer needed for 00240 later doing the cleanup. */ 00241 00242 struct cleanup * 00243 save_current_program_space (void) 00244 { 00245 struct cleanup *old_chain = make_cleanup (restore_program_space, 00246 current_program_space); 00247 00248 return old_chain; 00249 } 00250 00251 /* Returns true iff there's no inferior bound to PSPACE. */ 00252 00253 static int 00254 pspace_empty_p (struct program_space *pspace) 00255 { 00256 if (find_inferior_for_program_space (pspace) != NULL) 00257 return 0; 00258 00259 return 1; 00260 } 00261 00262 /* Prune away automatically added program spaces that aren't required 00263 anymore. */ 00264 00265 void 00266 prune_program_spaces (void) 00267 { 00268 struct program_space *ss, **ss_link; 00269 struct program_space *current = current_program_space; 00270 00271 ss = program_spaces; 00272 ss_link = &program_spaces; 00273 while (ss) 00274 { 00275 if (ss == current || !pspace_empty_p (ss)) 00276 { 00277 ss_link = &ss->next; 00278 ss = *ss_link; 00279 continue; 00280 } 00281 00282 *ss_link = ss->next; 00283 release_program_space (ss); 00284 ss = *ss_link; 00285 } 00286 } 00287 00288 /* Prints the list of program spaces and their details on UIOUT. If 00289 REQUESTED is not -1, it's the ID of the pspace that should be 00290 printed. Otherwise, all spaces are printed. */ 00291 00292 static void 00293 print_program_space (struct ui_out *uiout, int requested) 00294 { 00295 struct program_space *pspace; 00296 int count = 0; 00297 struct cleanup *old_chain; 00298 00299 /* Might as well prune away unneeded ones, so the user doesn't even 00300 seem them. */ 00301 prune_program_spaces (); 00302 00303 /* Compute number of pspaces we will print. */ 00304 ALL_PSPACES (pspace) 00305 { 00306 if (requested != -1 && pspace->num != requested) 00307 continue; 00308 00309 ++count; 00310 } 00311 00312 /* There should always be at least one. */ 00313 gdb_assert (count > 0); 00314 00315 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 3, count, "pspaces"); 00316 ui_out_table_header (uiout, 1, ui_left, "current", ""); 00317 ui_out_table_header (uiout, 4, ui_left, "id", "Id"); 00318 ui_out_table_header (uiout, 17, ui_left, "exec", "Executable"); 00319 ui_out_table_body (uiout); 00320 00321 ALL_PSPACES (pspace) 00322 { 00323 struct cleanup *chain2; 00324 struct inferior *inf; 00325 int printed_header; 00326 00327 if (requested != -1 && requested != pspace->num) 00328 continue; 00329 00330 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); 00331 00332 if (pspace == current_program_space) 00333 ui_out_field_string (uiout, "current", "*"); 00334 else 00335 ui_out_field_skip (uiout, "current"); 00336 00337 ui_out_field_int (uiout, "id", pspace->num); 00338 00339 if (pspace->pspace_exec_filename) 00340 ui_out_field_string (uiout, "exec", pspace->pspace_exec_filename); 00341 else 00342 ui_out_field_skip (uiout, "exec"); 00343 00344 /* Print extra info that doesn't really fit in tabular form. 00345 Currently, we print the list of inferiors bound to a pspace. 00346 There can be more than one inferior bound to the same pspace, 00347 e.g., both parent/child inferiors in a vfork, or, on targets 00348 that share pspaces between inferiors. */ 00349 printed_header = 0; 00350 for (inf = inferior_list; inf; inf = inf->next) 00351 if (inf->pspace == pspace) 00352 { 00353 if (!printed_header) 00354 { 00355 printed_header = 1; 00356 printf_filtered ("\n\tBound inferiors: ID %d (%s)", 00357 inf->num, 00358 target_pid_to_str (pid_to_ptid (inf->pid))); 00359 } 00360 else 00361 printf_filtered (", ID %d (%s)", 00362 inf->num, 00363 target_pid_to_str (pid_to_ptid (inf->pid))); 00364 } 00365 00366 ui_out_text (uiout, "\n"); 00367 do_cleanups (chain2); 00368 } 00369 00370 do_cleanups (old_chain); 00371 } 00372 00373 /* Boolean test for an already-known program space id. */ 00374 00375 static int 00376 valid_program_space_id (int num) 00377 { 00378 struct program_space *pspace; 00379 00380 ALL_PSPACES (pspace) 00381 if (pspace->num == num) 00382 return 1; 00383 00384 return 0; 00385 } 00386 00387 /* If ARGS is NULL or empty, print information about all program 00388 spaces. Otherwise, ARGS is a text representation of a LONG 00389 indicating which the program space to print information about. */ 00390 00391 static void 00392 maintenance_info_program_spaces_command (char *args, int from_tty) 00393 { 00394 int requested = -1; 00395 00396 if (args && *args) 00397 { 00398 requested = parse_and_eval_long (args); 00399 if (!valid_program_space_id (requested)) 00400 error (_("program space ID %d not known."), requested); 00401 } 00402 00403 print_program_space (current_uiout, requested); 00404 } 00405 00406 /* Simply returns the count of program spaces. */ 00407 00408 int 00409 number_of_program_spaces (void) 00410 { 00411 struct program_space *pspace; 00412 int count = 0; 00413 00414 ALL_PSPACES (pspace) 00415 count++; 00416 00417 return count; 00418 } 00419 00420 /* Update all program spaces matching to address spaces. The user may 00421 have created several program spaces, and loaded executables into 00422 them before connecting to the target interface that will create the 00423 inferiors. All that happens before GDB has a chance to know if the 00424 inferiors will share an address space or not. Call this after 00425 having connected to the target interface and having fetched the 00426 target description, to fixup the program/address spaces mappings. 00427 00428 It is assumed that there are no bound inferiors yet, otherwise, 00429 they'd be left with stale referenced to released aspaces. */ 00430 00431 void 00432 update_address_spaces (void) 00433 { 00434 int shared_aspace = gdbarch_has_shared_address_space (target_gdbarch ()); 00435 struct program_space *pspace; 00436 struct inferior *inf; 00437 00438 init_address_spaces (); 00439 00440 if (shared_aspace) 00441 { 00442 struct address_space *aspace = new_address_space (); 00443 00444 free_address_space (current_program_space->aspace); 00445 ALL_PSPACES (pspace) 00446 pspace->aspace = aspace; 00447 } 00448 else 00449 ALL_PSPACES (pspace) 00450 { 00451 free_address_space (pspace->aspace); 00452 pspace->aspace = new_address_space (); 00453 } 00454 00455 for (inf = inferior_list; inf; inf = inf->next) 00456 if (gdbarch_has_global_solist (target_gdbarch ())) 00457 inf->aspace = maybe_new_address_space (); 00458 else 00459 inf->aspace = inf->pspace->aspace; 00460 } 00461 00462 /* Save the current program space so that it may be restored by a later 00463 call to do_cleanups. Returns the struct cleanup pointer needed for 00464 later doing the cleanup. */ 00465 00466 struct cleanup * 00467 save_current_space_and_thread (void) 00468 { 00469 struct cleanup *old_chain; 00470 00471 /* If restoring to null thread, we need to restore the pspace as 00472 well, hence, we need to save the current program space first. */ 00473 old_chain = save_current_program_space (); 00474 /* There's no need to save the current inferior here. 00475 That is handled by make_cleanup_restore_current_thread. */ 00476 make_cleanup_restore_current_thread (); 00477 00478 return old_chain; 00479 } 00480 00481 /* Switches full context to program space PSPACE. Switches to the 00482 first thread found bound to PSPACE. */ 00483 00484 void 00485 switch_to_program_space_and_thread (struct program_space *pspace) 00486 { 00487 struct inferior *inf; 00488 00489 inf = find_inferior_for_program_space (pspace); 00490 if (inf != NULL) 00491 { 00492 struct thread_info *tp; 00493 00494 tp = any_live_thread_of_process (inf->pid); 00495 if (tp != NULL) 00496 { 00497 switch_to_thread (tp->ptid); 00498 /* Switching thread switches pspace implicitly. We're 00499 done. */ 00500 return; 00501 } 00502 } 00503 00504 switch_to_thread (null_ptid); 00505 set_current_program_space (pspace); 00506 } 00507 00508 00509 00510 /* See progspace.h. */ 00511 00512 void 00513 clear_program_space_solib_cache (struct program_space *pspace) 00514 { 00515 VEC_free (so_list_ptr, pspace->added_solibs); 00516 00517 free_char_ptr_vec (pspace->deleted_solibs); 00518 pspace->deleted_solibs = NULL; 00519 } 00520 00521 00522 00523 void 00524 initialize_progspace (void) 00525 { 00526 add_cmd ("program-spaces", class_maintenance, 00527 maintenance_info_program_spaces_command, 00528 _("Info about currently known program spaces."), 00529 &maintenanceinfolist); 00530 00531 /* There's always one program space. Note that this function isn't 00532 an automatic _initialize_foo function, since other 00533 _initialize_foo routines may need to install their per-pspace 00534 data keys. We can only allocate a progspace when all those 00535 modules have done that. Do this before 00536 initialize_current_architecture, because that accesses exec_bfd, 00537 which in turn dereferences current_program_space. */ 00538 current_program_space = add_program_space (new_address_space ()); 00539 }