* objfiles.h (gdb_bfd_close_or_warn): Declare.
[platform/upstream/binutils.git] / gdb / progspace.c
1 /* Program and address space management, for GDB, the GNU debugger.
2
3    Copyright (C) 2009, 2010 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 "gdbthread.h"
27
28 /* The last program space number assigned.  */
29 int last_program_space_num = 0;
30
31 /* The head of the program spaces list.  */
32 struct program_space *program_spaces;
33
34 /* Pointer to the current program space.  */
35 struct program_space *current_program_space;
36
37 /* The last address space number assigned.  */
38 static int highest_address_space_num;
39
40 /* Prototypes for local functions */
41
42 static void program_space_alloc_data (struct program_space *);
43 static void program_space_free_data (struct program_space *);
44 \f
45
46 /* An address space.  Currently this is not used for much other than
47    for comparing if pspaces/inferior/threads see the same address
48    space.  */
49
50 struct address_space
51 {
52   int num;
53 };
54
55 /* Create a new address space object, and add it to the list.  */
56
57 struct address_space *
58 new_address_space (void)
59 {
60   struct address_space *aspace;
61
62   aspace = XZALLOC (struct address_space);
63   aspace->num = ++highest_address_space_num;
64
65   return aspace;
66 }
67
68 /* Maybe create a new address space object, and add it to the list, or
69    return a pointer to an existing address space, in case inferiors
70    share an address space on this target system.  */
71
72 struct address_space *
73 maybe_new_address_space (void)
74 {
75   int shared_aspace = gdbarch_has_shared_address_space (target_gdbarch);
76
77   if (shared_aspace)
78     {
79       /* Just return the first in the list.  */
80       return program_spaces->aspace;
81     }
82
83   return new_address_space ();
84 }
85
86 static void
87 free_address_space (struct address_space *aspace)
88 {
89   xfree (aspace);
90 }
91
92 int
93 address_space_num (struct address_space *aspace)
94 {
95   return aspace->num;
96 }
97
98 /* Start counting over from scratch.  */
99
100 static void
101 init_address_spaces (void)
102 {
103   highest_address_space_num = 0;
104 }
105
106 \f
107
108 /* Adds a new empty program space to the program space list, and binds
109    it to ASPACE.  Returns the pointer to the new object.  */
110
111 struct program_space *
112 add_program_space (struct address_space *aspace)
113 {
114   struct program_space *pspace;
115
116   pspace = XZALLOC (struct program_space);
117
118   pspace->num = ++last_program_space_num;
119   pspace->aspace = aspace;
120
121   program_space_alloc_data (pspace);
122
123   pspace->next = program_spaces;
124   program_spaces = pspace;
125
126   return pspace;
127 }
128
129 /* Releases program space PSPACE, and all its contents (shared
130    libraries, objfiles, and any other references to the PSPACE in
131    other modules).  It is an internal error to call this when PSPACE
132    is the current program space, since there should always be a
133    program space.  */
134
135 static void
136 release_program_space (struct program_space *pspace)
137 {
138   struct cleanup *old_chain = save_current_program_space ();
139
140   gdb_assert (pspace != current_program_space);
141
142   set_current_program_space (pspace);
143
144   breakpoint_program_space_exit (pspace);
145   no_shared_libraries (NULL, 0);
146   exec_close ();
147   free_all_objfiles ();
148   if (!gdbarch_has_shared_address_space (target_gdbarch))
149     free_address_space (pspace->aspace);
150   resize_section_table (&pspace->target_sections,
151                         -resize_section_table (&pspace->target_sections, 0));
152     /* Discard any data modules have associated with the PSPACE.  */
153   program_space_free_data (pspace);
154   xfree (pspace);
155
156   do_cleanups (old_chain);
157 }
158
159 /* Unlinks PSPACE from the pspace list, and releases it.  */
160
161 void
162 remove_program_space (struct program_space *pspace)
163 {
164   struct program_space *ss, **ss_link;
165
166   ss = program_spaces;
167   ss_link = &program_spaces;
168   while (ss)
169     {
170       if (ss != pspace)
171         {
172           ss_link = &ss->next;
173           ss = *ss_link;
174           continue;
175         }
176
177       *ss_link = ss->next;
178       release_program_space (ss);
179       ss = *ss_link;
180     }
181 }
182
183 /* Copies program space SRC to DEST.  Copies the main executable file,
184    and the main symbol file.  Returns DEST.  */
185
186 struct program_space *
187 clone_program_space (struct program_space *dest, struct program_space *src)
188 {
189   struct program_space *new_pspace;
190   struct cleanup *old_chain;
191
192   old_chain = save_current_program_space ();
193
194   set_current_program_space (dest);
195
196   if (src->ebfd != NULL)
197     exec_file_attach (bfd_get_filename (src->ebfd), 0);
198
199   if (src->symfile_object_file != NULL)
200     symbol_file_add_main (src->symfile_object_file->name, 0);
201
202   do_cleanups (old_chain);
203   return dest;
204 }
205
206 /* Sets PSPACE as the current program space.  It is the caller's
207    responsibility to make sure that the currently selected
208    inferior/thread matches the selected program space.  */
209
210 void
211 set_current_program_space (struct program_space *pspace)
212 {
213   if (current_program_space == pspace)
214     return;
215
216   gdb_assert (pspace != NULL);
217
218   current_program_space = pspace;
219
220   /* Different symbols change our view of the frame chain.  */
221   reinit_frame_cache ();
222 }
223
224 /* A cleanups callback, helper for save_current_program_space
225    below.  */
226
227 static void
228 restore_program_space (void *arg)
229 {
230   struct program_space *saved_pspace = arg;
231   set_current_program_space (saved_pspace);
232 }
233
234 /* Save the current program space so that it may be restored by a later
235    call to do_cleanups.  Returns the struct cleanup pointer needed for
236    later doing the cleanup.  */
237
238 struct cleanup *
239 save_current_program_space (void)
240 {
241   struct cleanup *old_chain = make_cleanup (restore_program_space,
242                                             current_program_space);
243   return old_chain;
244 }
245
246 /* Returns true iff there's no inferior bound to PSPACE.  */
247
248 static int
249 pspace_empty_p (struct program_space *pspace)
250 {
251   struct inferior *inf;
252
253   if (find_inferior_for_program_space (pspace) != NULL)
254       return 0;
255
256   return 1;
257 }
258
259 /* Prune away automatically added program spaces that aren't required
260    anymore.  */
261
262 void
263 prune_program_spaces (void)
264 {
265   struct program_space *ss, **ss_link;
266   struct program_space *current = current_program_space;
267
268   ss = program_spaces;
269   ss_link = &program_spaces;
270   while (ss)
271     {
272       if (ss == current || !pspace_empty_p (ss))
273         {
274           ss_link = &ss->next;
275           ss = *ss_link;
276           continue;
277         }
278
279       *ss_link = ss->next;
280       release_program_space (ss);
281       ss = *ss_link;
282     }
283 }
284
285 /* Prints the list of program spaces and their details on UIOUT.  If
286    REQUESTED is not -1, it's the ID of the pspace that should be
287    printed.  Otherwise, all spaces are printed.  */
288
289 static void
290 print_program_space (struct ui_out *uiout, int requested)
291 {
292   struct program_space *pspace;
293   int count = 0;
294   struct cleanup *old_chain;
295
296   /* Might as well prune away unneeded ones, so the user doesn't even
297      seem them.  */
298   prune_program_spaces ();
299
300   /* Compute number of pspaces we will print.  */
301   ALL_PSPACES (pspace)
302     {
303       if (requested != -1 && pspace->num != requested)
304         continue;
305
306       ++count;
307     }
308
309   /* There should always be at least one.  */
310   gdb_assert (count > 0);
311
312   old_chain = make_cleanup_ui_out_table_begin_end (uiout, 3, count, "pspaces");
313   ui_out_table_header (uiout, 1, ui_left, "current", "");
314   ui_out_table_header (uiout, 4, ui_left, "id", "Id");
315   ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
316   ui_out_table_body (uiout);
317
318   ALL_PSPACES (pspace)
319     {
320       struct cleanup *chain2;
321       struct inferior *inf;
322       int printed_header;
323
324       if (requested != -1 && requested != pspace->num)
325         continue;
326
327       chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
328
329       if (pspace == current_program_space)
330         ui_out_field_string (uiout, "current", "*");
331       else
332         ui_out_field_skip (uiout, "current");
333
334       ui_out_field_int (uiout, "id", pspace->num);
335
336       if (pspace->ebfd)
337         ui_out_field_string (uiout, "exec",
338                              bfd_get_filename (pspace->ebfd));
339       else
340         ui_out_field_skip (uiout, "exec");
341
342       /* Print extra info that doesn't really fit in tabular form.
343          Currently, we print the list of inferiors bound to a pspace.
344          There can be more than one inferior bound to the same pspace,
345          e.g., both parent/child inferiors in a vfork, or, on targets
346          that share pspaces between inferiors.  */
347       printed_header = 0;
348       for (inf = inferior_list; inf; inf = inf->next)
349         if (inf->pspace == pspace)
350           {
351             if (!printed_header)
352               {
353                 printed_header = 1;
354                 printf_filtered ("\n\tBound inferiors: ID %d (%s)",
355                                  inf->num,
356                                  target_pid_to_str (pid_to_ptid (inf->pid)));
357               }
358             else
359               printf_filtered (", ID %d (%s)",
360                                inf->num,
361                                target_pid_to_str (pid_to_ptid (inf->pid)));
362           }
363
364       ui_out_text (uiout, "\n");
365       do_cleanups (chain2);
366     }
367
368   do_cleanups (old_chain);
369 }
370
371 /* Boolean test for an already-known program space id.  */
372
373 static int
374 valid_program_space_id (int num)
375 {
376   struct program_space *pspace;
377
378   ALL_PSPACES (pspace)
379     if (pspace->num == num)
380       return 1;
381
382   return 0;
383 }
384
385 /* If ARGS is NULL or empty, print information about all program
386    spaces.  Otherwise, ARGS is a text representation of a LONG
387    indicating which the program space to print information about.  */
388
389 static void
390 maintenance_info_program_spaces_command (char *args, int from_tty)
391 {
392   int requested = -1;
393
394   if (args && *args)
395     {
396       requested = parse_and_eval_long (args);
397       if (!valid_program_space_id (requested))
398         error (_("program space ID %d not known."), requested);
399     }
400
401   print_program_space (uiout, requested);
402 }
403
404 /* Simply returns the count of program spaces.  */
405
406 int
407 number_of_program_spaces (void)
408 {
409   struct program_space *pspace;
410   int count = 0;
411
412   ALL_PSPACES (pspace)
413     count++;
414
415   return count;
416 }
417
418 /* Update all program spaces matching to address spaces.  The user may
419    have created several program spaces, and loaded executables into
420    them before connecting to the target interface that will create the
421    inferiors.  All that happens before GDB has a chance to know if the
422    inferiors will share an address space or not.  Call this after
423    having connected to the target interface and having fetched the
424    target description, to fixup the program/address spaces mappings.
425
426    It is assumed that there are no bound inferiors yet, otherwise,
427    they'd be left with stale referenced to released aspaces.  */
428
429 void
430 update_address_spaces (void)
431 {
432   int shared_aspace = gdbarch_has_shared_address_space (target_gdbarch);
433   struct program_space *pspace;
434   struct inferior *inf;
435
436   init_address_spaces ();
437
438   if (shared_aspace)
439     {
440       struct address_space *aspace = new_address_space ();
441       free_address_space (current_program_space->aspace);
442       ALL_PSPACES (pspace)
443         pspace->aspace = aspace;
444     }
445   else
446     ALL_PSPACES (pspace)
447       {
448         free_address_space (pspace->aspace);
449         pspace->aspace = new_address_space ();
450       }
451
452   for (inf = inferior_list; inf; inf = inf->next)
453     if (gdbarch_has_global_solist (target_gdbarch))
454       inf->aspace = maybe_new_address_space ();
455     else
456       inf->aspace = inf->pspace->aspace;
457 }
458
459 /* Save the current program space so that it may be restored by a later
460    call to do_cleanups.  Returns the struct cleanup pointer needed for
461    later doing the cleanup.  */
462
463 struct cleanup *
464 save_current_space_and_thread (void)
465 {
466   struct cleanup *old_chain;
467
468   /* If restoring to null thread, we need to restore the pspace as
469      well, hence, we need to save the current program space first.  */
470   old_chain = save_current_program_space ();
471   save_current_inferior ();
472   make_cleanup_restore_current_thread ();
473
474   return old_chain;
475 }
476
477 /* Switches full context to program space PSPACE.  Switches to the
478    first thread found bound to PSPACE.  */
479
480 void
481 switch_to_program_space_and_thread (struct program_space *pspace)
482 {
483   struct inferior *inf;
484
485   inf = find_inferior_for_program_space (pspace);
486   if (inf != NULL)
487     {
488       struct thread_info *tp;
489
490       tp = any_live_thread_of_process (inf->pid);
491       if (tp != NULL)
492         {
493           switch_to_thread (tp->ptid);
494           /* Switching thread switches pspace implicitly.  We're
495              done.  */
496           return;
497         }
498     }
499
500   switch_to_thread (null_ptid);
501   set_current_program_space (pspace);
502 }
503
504 \f
505
506 /* Keep a registry of per-program_space data-pointers required by other GDB
507    modules.  */
508
509 struct program_space_data
510 {
511   unsigned index;
512   void (*cleanup) (struct program_space *, void *);
513 };
514
515 struct program_space_data_registration
516 {
517   struct program_space_data *data;
518   struct program_space_data_registration *next;
519 };
520
521 struct program_space_data_registry
522 {
523   struct program_space_data_registration *registrations;
524   unsigned num_registrations;
525 };
526
527 static struct program_space_data_registry program_space_data_registry
528   = { NULL, 0 };
529
530 const struct program_space_data *
531 register_program_space_data_with_cleanup
532   (void (*cleanup) (struct program_space *, void *))
533 {
534   struct program_space_data_registration **curr;
535
536   /* Append new registration.  */
537   for (curr = &program_space_data_registry.registrations;
538        *curr != NULL; curr = &(*curr)->next);
539
540   *curr = XMALLOC (struct program_space_data_registration);
541   (*curr)->next = NULL;
542   (*curr)->data = XMALLOC (struct program_space_data);
543   (*curr)->data->index = program_space_data_registry.num_registrations++;
544   (*curr)->data->cleanup = cleanup;
545
546   return (*curr)->data;
547 }
548
549 const struct program_space_data *
550 register_program_space_data (void)
551 {
552   return register_program_space_data_with_cleanup (NULL);
553 }
554
555 static void
556 program_space_alloc_data (struct program_space *pspace)
557 {
558   gdb_assert (pspace->data == NULL);
559   pspace->num_data = program_space_data_registry.num_registrations;
560   pspace->data = XCALLOC (pspace->num_data, void *);
561 }
562
563 static void
564 program_space_free_data (struct program_space *pspace)
565 {
566   gdb_assert (pspace->data != NULL);
567   clear_program_space_data (pspace);
568   xfree (pspace->data);
569   pspace->data = NULL;
570 }
571
572 void
573 clear_program_space_data (struct program_space *pspace)
574 {
575   struct program_space_data_registration *registration;
576   int i;
577
578   gdb_assert (pspace->data != NULL);
579
580   for (registration = program_space_data_registry.registrations, i = 0;
581        i < pspace->num_data;
582        registration = registration->next, i++)
583     if (pspace->data[i] != NULL && registration->data->cleanup)
584       registration->data->cleanup (pspace, pspace->data[i]);
585
586   memset (pspace->data, 0, pspace->num_data * sizeof (void *));
587 }
588
589 void
590 set_program_space_data (struct program_space *pspace,
591                        const struct program_space_data *data,
592                        void *value)
593 {
594   gdb_assert (data->index < pspace->num_data);
595   pspace->data[data->index] = value;
596 }
597
598 void *
599 program_space_data (struct program_space *pspace, const struct program_space_data *data)
600 {
601   gdb_assert (data->index < pspace->num_data);
602   return pspace->data[data->index];
603 }
604
605 \f
606
607 void
608 initialize_progspace (void)
609 {
610   add_cmd ("program-spaces", class_maintenance,
611            maintenance_info_program_spaces_command, _("\
612 Info about currently known program spaces."),
613            &maintenanceinfolist);
614
615   /* There's always one program space.  Note that this function isn't
616      an automatic _initialize_foo function, since other
617      _initialize_foo routines may need to install their per-pspace
618      data keys.  We can only allocate a progspace when all those
619      modules have done that.  Do this before
620      initialize_current_architecture, because that accesses exec_bfd,
621      which in turn dereferences current_program_space.  */
622   current_program_space = add_program_space (new_address_space ());
623 }