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