1 /* Support for dumping and reloading various pieces of GDB's internal state.
2 Copyright 1992 Free Software Foundation, Inc.
3 Contributed by Cygnus Support, using pieces from other GDB modules.
5 This file is part of GDB.
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This file provides support for dumping and then later reloading various
22 portions of gdb's internal state. It was originally implemented to
23 support a need for mapping in an image of gdb's symbol table from an
24 external file, where this image was created by an external program, such
25 as an incremental linker. However, it was generalized to enable future
26 support for dumping and reloading various other useful pieces of gdb's
29 State files have a fairly simple form which is intended to be easily
30 extensible. The basic format is:
32 <file-header> <state-data> <form-tree>
36 file-header A simple file-header containing a magic number
37 so that gdb (and other readers) can quickly
38 determine what kind of file this is, and a file
39 offset to the root of the form-tree.
41 state-data The "raw" state-data that is referenced by nodes
44 form-tree A tree of arbitrarily sized nodes containing
45 information about gdb's internal state, and
46 possibly referencing data in the state-data section
47 of the file. Resembles DWARF in some respects.
49 When writing a state file, a hole is left for the file-header at the
50 beginning of the file, the state data is written immediately after the
51 file header (while storing the file offsets and sizes back into the
52 internal form-tree along the way), the form-tree itself is written
53 at the end of the file, and then the file header is written by seeking
54 back to the beginning of the file. This order is required because
55 the form tree contains file offsets and sizes in the state data portion
56 of the file, and the file header contains the file offset to the start
59 Readers simply open the file, validate the magic number, seek to the
60 root of the form-tree, and walk the tree looking for the information that
61 they are interested in (and ignoring things that they aren't, or don't
81 /* Inside the state file, the form-tree consists of a series of
82 form-tree entries (FTE's). The parent/child/sibling relationships
83 are implied by the ordering and by an explicit sibling reference
84 in FTE's that have siblings.
86 Specifically, given two sequential FTE's, say A and B, if B immediately
87 follows A, and A does not have a sibling reference to B, then B is
88 the first child of A. Otherwise B must be a sibling of A and A must
89 have a sibling reference for it.
91 Each FTE is simply an array of long integers, with at least three
92 members. This form was chosen over a packed data form for simplicity
93 in access, not having to worry about the relative sizes of the different
94 integers (short, int, long), and not having to worry about alignment
95 constraints. Also in the name of simplicity, every FTE has a sibling
96 reference slot reserved for it, even if there are no siblings.
98 The first value in an FTE is the size of the FTE in bytes, including
99 the size value itself. The second entry contains a tag which indicates
100 the type of the FTE. The third entry is a sibling reference, which either
101 refers to a valid sibling node or is zero. Following is zero or more
102 attributes, each of which consists of one or more long values. */
104 /* Tag names and codes. */
106 #define TAG_padding 0x0000 /* Padding */
107 #define TAG_objfile 0x0001 /* Dumped objfile */
109 /* Form names, codes, and macros. */
111 #define FORM_ABSREF 0x01 /* Next long is absolute file offset */
112 #define FORM_RELREF 0x02 /* Next long is relative file offset */
113 #define FORM_IVAL 0x03 /* Next long is int value */
114 #define FORM_ADDR 0x04 /* Next long is mem addr */
116 #define FORM_MASK 0xFF
117 #define FORM_X(atr) ((atr) & FORM_MASK)
119 /* Attribute names and codes. */
121 #define AT_sibling (0x0100 | FORM_RELREF) /* Reference to sibling node */
122 #define AT_name (0x0200 | FORM_ABSREF) /* Reference to a string */
123 #define AT_offset (0x0300 | FORM_ABSREF) /* Reference to generic data */
124 #define AT_size (0x0400 | FORM_IVAL)
125 #define AT_addr (0x0500 | FORM_ADDR)
126 #define AT_aux_addr (0x0600 | FORM_ADDR)
131 load_symbols PARAMS ((FILE *));
134 dump_state_command PARAMS ((char *, int));
137 load_state_command PARAMS ((char *, int));
142 write_header PARAMS ((sfd *));
145 write_formtree PARAMS ((sfd *));
148 write_objfile_state PARAMS ((sfd *));
151 free_subtree PARAMS ((struct formnode *));
154 size_subtree PARAMS ((struct formnode *));
158 struct formnode *formtree = NULL;
162 load_symbols (statefile)
167 /* Discard old symbols. FIXME: This is essentially symbol_file_command's
168 body when there is no name. Make it a common function that is
169 called from each place. */
173 free_objfile (symfile_objfile);
175 symfile_objfile = NULL;
178 #if 0 && defined (HAVE_MMAP)
181 warning ("internal error: mbase (%08x) != mtop (%08x)",
183 munmap (mbase, mtop - mbase);
185 #endif /* HAVE_MMAP */
187 /* Getting new symbols may change our opinion about what is frameless. */
189 reinit_frame_cache ();
195 /* Allocate a form node */
197 static struct formnode *
200 struct formnode *fnp;
202 fnp = (struct formnode *) xmalloc (sizeof (struct formnode));
203 (void) memset (fnp, 0, sizeof (struct formnode));
204 fnp -> sibling = formtree;
209 /* Recursively walk a form-tree from the specified node, freeing
210 nodes from the bottom up. The concept is pretty simple, just free
211 all the child nodes, then all the sibling nodes, then the node
216 struct formnode *fnp;
220 free_subtree (fnp -> child);
221 free_subtree (fnp -> sibling);
222 if (fnp -> nodedata != NULL)
224 free (fnp -> nodedata);
230 /* Recursively walk a form-tree from the specified node, computing the
231 size of each subtree from the bottom up.
233 At each node, the file space that will be consumed by the subtree
234 rooted in that node is the sum of all the subtrees rooted in each
235 child node plus the size of the node itself.
237 Thus for each node, we size the child subtrees, add to that our
238 size, contribute this size towards the size of any parent node, and
239 then ask any of our siblings to do the same.
241 Also, once we know the size of any subtree rooted at this node, we
242 can initialize the offset to the sibling node (if any).
244 Since every form-tree node must have valid nodedata at this point,
245 we detect and report a warning for any node that doesn't. */
249 struct formnode *fnp;
255 if (fnp -> nodedata == NULL)
257 warning ("internal error -- empty form node");
261 size_subtree (fnp -> child);
262 fnp -> treesize += *(long *) fnp -> nodedata;
263 if (fnp -> parent != NULL)
265 fnp -> parent -> treesize += fnp -> treesize;
269 size_subtree (fnp -> sibling);
270 lp = (long *) (fnp -> nodedata + 2 * sizeof (long));
271 *lp = fnp -> treesize;
277 /* Recursively walk a form-tree from the specified node, writing
278 nodes from the top down. */
281 write_subtree (fnp, asfd)
282 struct formnode *fnp;
287 if (fnp -> nodedata != NULL)
289 fwrite (fnp -> nodedata, *(long *) fnp -> nodedata, 1, asfd -> fp);
291 write_subtree (fnp -> child, asfd);
292 write_subtree (fnp -> sibling, asfd);
296 /* Free the entire current formtree. Called via do_cleanups, regardless
297 of whether there is an error or not. */
302 free_subtree (formtree);
306 /* Write out the file header. Generally this is done last, even though
307 it is located at the start of the file, since we need to have file
308 offset to where the annotated form tree was written, and it's size. */
314 fseek (asfd -> fp, 0L, SEEK_SET);
315 fwrite ((char *) &asfd -> hdr, sizeof (asfd -> hdr), 1, asfd -> fp);
318 /* Write out the annotated form tree. We should already have written out
319 the state data, and noted the file offsets and sizes in each node of
320 the form tree that references part of the state data.
322 The form tree can be written anywhere in the file where there is room
323 for it. Since there is always room at the end of the file, we write
324 it there. We also need to record the file offset to the start of the
325 form tree, and it's size, for future use when writing the file header.
327 In order to compute the sibling references, we need to know, at
328 each node, how much space will be consumed when all of that node's
329 children nodes have been written. Thus we walk the tree, computing
330 the sizes of the subtrees from the bottom up. At any node, the
331 offset from the start of that node to the start of the sibling node
332 is simply the size of the node plus the size of the subtree rooted
336 write_formtree (asfd)
339 size_subtree (formtree);
340 fseek (asfd -> fp, 0L, SEEK_END);
341 asfd -> hdr.sf_ftoff = ftell (asfd -> fp);
342 write_subtree (formtree, asfd);
343 asfd -> hdr.sf_ftsize = ftell (asfd -> fp) - asfd -> hdr.sf_ftoff;
346 /* Note that we currently only support having one objfile with dumpable
350 write_objfile_state (asfd)
353 struct objfile *objfile;
354 struct formnode *fnp;
358 unsigned int ftesize;
362 /* First walk through the objfile list looking for the first objfile
365 for (objfile = object_files; objfile != NULL; objfile = objfile -> next)
367 if (objfile -> flags & OBJF_DUMPABLE)
375 warning ("no dumpable objfile was found");
379 fnp = alloc_formnode ();
382 lp++; /* Skip FTE size slot, filled in at the end. */
383 *lp++ = TAG_objfile; /* This is an objfile FTE */
384 *lp++ = 0; /* Zero the sibling reference slot. */
386 /* Build an AT_name attribute for the objfile's name, and write
387 the name into the state data. */
390 *lp++ = (long) ftell (asfd -> fp);
391 fwrite (objfile -> name, strlen (objfile -> name) + 1, 1, asfd -> fp);
393 /* Build an AT_addr attribute for the virtual address to which the
394 objfile data is mapped (and needs to be remapped when read in). */
400 /* Build an AT_aux_addr attribute for the address of the objfile
401 structure itself, within the dumpable data. When we read the objfile
402 back in, we use this address as the pointer the "struct objfile". */
405 *lp++ = (long) objfile;
407 /* Reposition in state file to next paging boundry so we can mmap the
408 dumpable objfile data when we reload it. */
410 foffset = (long) mmap_page_align ((PTR) ftell (asfd -> fp));
411 fseek (asfd -> fp, foffset, SEEK_SET);
413 /* Build an AT_offset attribute for the offset in the state file to
414 the start of the dumped objfile data. */
417 *lp++ = (long) ftell (asfd -> fp);
419 /* Build an AT_size attribute for the size of the dumped objfile data. */
421 breakval = mmap_sbrk (0);
423 *lp++ = breakval - base;
425 /* Write the dumpable data. */
427 fwrite ((char *) base, breakval - base, 1, asfd -> fp);
429 /* Now finish up the FTE by filling in the size slot based on
430 how much of the ftebuf we have used, allocate some memory for
431 it hung off the form tree node, and copy it there. */
433 ftebuf[0] = (lp - ftebuf) * sizeof (ftebuf[0]);
434 fnp -> nodedata = (char *) xmalloc (ftebuf[0]);
435 memcpy (fnp -> nodedata, ftebuf, ftebuf[0]);
440 load_state_command (arg_string, from_tty)
447 struct cleanup *cleanups;
451 if (arg_string == NULL)
453 error ("load-state takes a file name and optional state specifiers");
455 else if ((argv = buildargv (arg_string)) == NULL)
457 fatal ("virtual memory exhausted.", 0);
459 cleanups = make_cleanup (freeargv, argv);
461 filename = tilde_expand (*argv);
462 make_cleanup (free, filename);
464 if ((fp = fopen (filename, "r")) == NULL)
466 perror_with_name (filename);
468 make_cleanup (fclose, fp);
471 while (*++argv != NULL)
473 if (strcmp (*argv, "symbols") == 0)
476 && !query ("load symbol table state from file \"%s\"? ",
479 error ("Not confirmed.");
485 error ("unknown state specifier '%s'", *argv);
489 do_cleanups (cleanups);
494 dump_state_command (arg_string, from_tty)
501 struct cleanup *cleanups;
505 if (arg_string == NULL)
507 error ("dump-state takes a file name and state specifiers");
509 else if ((argv = buildargv (arg_string)) == NULL)
511 fatal ("virtual memory exhausted.", 0);
513 cleanups = make_cleanup (freeargv, argv);
515 filename = tilde_expand (*argv);
516 make_cleanup (free, filename);
518 /* Now attempt to create a fresh state file. */
520 if ((asfd = sfd_fopen (filename, "w")) == NULL)
522 perror_with_name (filename);
524 make_cleanup (sfd_fclose, asfd);
525 make_cleanup (free_formtree, NULL);
528 /* Now that we have an open and initialized state file, seek to the
529 proper offset to start writing state data and the process the
530 arguments. For each argument, write the state data and initialize
531 a form-tree node for each piece of state data. */
533 fseek (asfd -> fp, sizeof (sf_hdr), SEEK_SET);
534 while (*++argv != NULL)
536 if (strcmp (*argv, "objfile") == 0)
538 write_objfile_state (asfd);
542 error ("unknown state specifier '%s'", *argv);
547 /* We have written any state data. All that is left to do now is
548 write the form-tree and the file header. */
550 write_formtree (asfd);
554 do_cleanups (cleanups);
558 find_fte_by_walk (thisfte, endfte, tag)
569 while (thisfte < endfte)
571 if ((thistag = *(long *)(thisfte + sizeof (long))) == tag)
578 thissize = *(long *)(thisfte);
579 siboffset = *(long *)(thisfte + (2 * sizeof (long)));
580 nextfte = thisfte + (siboffset != 0 ? siboffset : thissize);
581 found = find_fte_by_walk (thisfte + thissize, nextfte, tag);
588 /* Walk the form-tree looking for a specific FTE type. Returns the first
589 one found that matches the specified tag. */
601 if (fseek (asfd -> fp, asfd -> hdr.sf_ftoff, SEEK_SET) == 0)
603 ftbase = xmalloc (asfd -> hdr.sf_ftsize);
604 ftend = ftbase + asfd -> hdr.sf_ftsize;
605 if (fread (ftbase, asfd -> hdr.sf_ftsize, 1, asfd -> fp) == 1)
607 ftep = find_fte_by_walk (ftbase, ftend, tag);
610 found = xmalloc (*(long *)ftep);
611 memcpy (found, ftep, (int) *(long *)ftep);
620 objfile_from_statefile (asfd)
623 struct objfile *objfile = NULL;
631 ftep = find_fte (asfd, TAG_objfile);
632 thisattr = (long *) (ftep + 3 * sizeof (long));
633 endattr = (long *) (ftep + *(long *)ftep);
634 while (thisattr < endattr)
643 base = (PTR) *thisattr++;
646 objfile = (struct objfile *) *thisattr++;
649 foffset = *thisattr++;
652 mapsize = *thisattr++;
656 if (mmap_remap (base, mapsize, (int) fileno (asfd -> fp), foffset) != base)
658 print_sys_errmsg (asfd -> filename, errno);
659 error ("mapping failed");
668 objfile_from_statefile (asfd)
671 error ("this version of gdb doesn't support reloading symtabs from state files");
674 #endif /* HAVE_MMAP */
676 /* Close a state file, freeing all memory that was used by the state
677 file descriptor, closing the raw file pointer, etc. */
685 if (asfd -> fp != NULL)
689 if (asfd -> filename != NULL)
691 free (asfd -> filename);
697 /* Given the name of a possible statefile, and flags to use to open it,
698 try to open the file and prepare it for use.
700 If the flags contain 'r', then we want to read an existing state
701 file, so attempt to read in the state file header and determine if this
702 is a valid state file. If not, return NULL.
704 Returns a pointer to a properly initialized state file descriptor if
708 sfd_fopen (name, flags)
715 asfd = (sfd *) xmalloc (sizeof (sfd));
716 (void) memset (asfd, 0, sizeof (sfd));
717 asfd -> filename = xmalloc (strlen (name) + 1);
718 (void) strcpy (asfd -> filename, name);
720 if ((asfd -> fp = fopen (asfd -> filename, flags)) != NULL)
722 /* We have the file, now see if we are reading an existing file
723 or writing to a new file. We don't currently support "rw". */
724 if (strchr (flags, 'r') != NULL)
726 if (fread ((char *) &asfd -> hdr, sizeof (asfd -> hdr), 1,
729 if (SF_GOOD_MAGIC (asfd))
737 /* This is a new state file. Initialize various things. */
738 asfd -> hdr.sf_mag0 = SF_MAG0;
739 asfd -> hdr.sf_mag1 = SF_MAG1;
740 asfd -> hdr.sf_mag2 = SF_MAG2;
741 asfd -> hdr.sf_mag3 = SF_MAG3;
762 add_com ("load-state", class_support, load_state_command,
763 "Load some saved gdb state from FILE.\n\
764 Select and load some portion of gdb's saved state from the specified file.\n\
765 The dump-state command may be used to save various portions of gdb's\n\
768 add_com ("dump-state", class_support, dump_state_command,
769 "Dump some of gdb's state to FILE.\n\
770 Select and dump some portion of gdb's internal state to the specified file.\n\
771 The load-state command may be used to reload various portions of gdb's\n\
772 internal state from the file.");
774 #endif /* HAVE_MMAP */