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
;
201 fnp
= (struct formnode
*) xmalloc (sizeof (struct formnode
));
202 (void) memset (fnp
, 0, sizeof (struct formnode
));
203 fnp
-> sibling
= formtree
;
208 /* Recursively walk a form-tree from the specified node, freeing
209 nodes from the bottom up. The concept is pretty simple, just free
210 all the child nodes, then all the sibling nodes, then the node
215 struct formnode
*fnp
;
219 free_subtree (fnp
-> child
);
220 free_subtree (fnp
-> sibling
);
221 if (fnp
-> nodedata
!= NULL
)
223 free (fnp
-> nodedata
);
229 /* Recursively walk a form-tree from the specified node, computing the
230 size of each subtree from the bottom up.
232 At each node, the file space that will be consumed by the subtree
233 rooted in that node is the sum of all the subtrees rooted in each
234 child node plus the size of the node itself.
236 Thus for each node, we size the child subtrees, add to that our
237 size, contribute this size towards the size of any parent node, and
238 then ask any of our siblings to do the same.
240 Also, once we know the size of any subtree rooted at this node, we
241 can initialize the offset to the sibling node (if any).
243 Since every form-tree node must have valid nodedata at this point,
244 we detect and report a warning for any node that doesn't. */
248 struct formnode
*fnp
;
254 if (fnp
-> nodedata
== NULL
)
256 warning ("internal error -- empty form node");
260 size_subtree (fnp
-> child
);
261 fnp
-> treesize
+= *(long *) fnp
-> nodedata
;
262 if (fnp
-> parent
!= NULL
)
264 fnp
-> parent
-> treesize
+= fnp
-> treesize
;
268 size_subtree (fnp
-> sibling
);
269 lp
= (long *) (fnp
-> nodedata
+ 2 * sizeof (long));
270 *lp
= fnp
-> treesize
;
276 /* Recursively walk a form-tree from the specified node, writing
277 nodes from the top down. */
280 write_subtree (fnp
, asfd
)
281 struct formnode
*fnp
;
286 if (fnp
-> nodedata
!= NULL
)
288 fwrite (fnp
-> nodedata
, *(long *) fnp
-> nodedata
, 1, asfd
-> fp
);
290 write_subtree (fnp
-> child
, asfd
);
291 write_subtree (fnp
-> sibling
, asfd
);
295 /* Free the entire current formtree. Called via do_cleanups, regardless
296 of whether there is an error or not. */
301 free_subtree (formtree
);
305 /* Write out the file header. Generally this is done last, even though
306 it is located at the start of the file, since we need to have file
307 offset to where the annotated form tree was written, and it's size. */
313 fseek (asfd
-> fp
, 0L, SEEK_SET
);
314 fwrite ((char *) &asfd
-> hdr
, sizeof (asfd
-> hdr
), 1, asfd
-> fp
);
317 /* Write out the annotated form tree. We should already have written out
318 the state data, and noted the file offsets and sizes in each node of
319 the form tree that references part of the state data.
321 The form tree can be written anywhere in the file where there is room
322 for it. Since there is always room at the end of the file, we write
323 it there. We also need to record the file offset to the start of the
324 form tree, and it's size, for future use when writing the file header.
326 In order to compute the sibling references, we need to know, at
327 each node, how much space will be consumed when all of that node's
328 children nodes have been written. Thus we walk the tree, computing
329 the sizes of the subtrees from the bottom up. At any node, the
330 offset from the start of that node to the start of the sibling node
331 is simply the size of the node plus the size of the subtree rooted
335 write_formtree (asfd
)
338 size_subtree (formtree
);
339 fseek (asfd
-> fp
, 0L, SEEK_END
);
340 asfd
-> hdr
.sf_ftoff
= ftell (asfd
-> fp
);
341 write_subtree (formtree
, asfd
);
342 asfd
-> hdr
.sf_ftsize
= ftell (asfd
-> fp
) - asfd
-> hdr
.sf_ftoff
;
345 /* Note that we currently only support having one objfile with dumpable
349 write_objfile_state (asfd
)
352 struct objfile
*objfile
;
353 struct formnode
*fnp
;
357 unsigned int ftesize
;
361 /* First walk through the objfile list looking for the first objfile
364 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
366 if (objfile
-> flags
& OBJF_DUMPABLE
)
374 warning ("no dumpable objfile was found");
378 fnp
= alloc_formnode ();
381 lp
++; /* Skip FTE size slot, filled in at the end. */
382 *lp
++ = TAG_objfile
; /* This is an objfile FTE */
383 *lp
++ = 0; /* Zero the sibling reference slot. */
385 /* Build an AT_name attribute for the objfile's name, and write
386 the name into the state data. */
389 *lp
++ = (long) ftell (asfd
-> fp
);
390 fwrite (objfile
-> name
, strlen (objfile
-> name
) + 1, 1, asfd
-> fp
);
392 /* Build an AT_addr attribute for the virtual address to which the
393 objfile data is mapped (and needs to be remapped when read in). */
399 /* Build an AT_aux_addr attribute for the address of the objfile
400 structure itself, within the dumpable data. When we read the objfile
401 back in, we use this address as the pointer the "struct objfile". */
404 *lp
++ = (long) objfile
;
406 /* Reposition in state file to next paging boundry so we can mmap the
407 dumpable objfile data when we reload it. */
409 foffset
= (long) mmap_page_align ((PTR
) ftell (asfd
-> fp
));
410 fseek (asfd
-> fp
, foffset
, SEEK_SET
);
412 /* Build an AT_offset attribute for the offset in the state file to
413 the start of the dumped objfile data. */
416 *lp
++ = (long) ftell (asfd
-> fp
);
418 /* Build an AT_size attribute for the size of the dumped objfile data. */
420 breakval
= mmap_sbrk (0);
422 *lp
++ = breakval
- base
;
424 /* Write the dumpable data. */
426 fwrite ((char *) base
, breakval
- base
, 1, asfd
-> fp
);
428 /* Now finish up the FTE by filling in the size slot based on
429 how much of the ftebuf we have used, allocate some memory for
430 it hung off the form tree node, and copy it there. */
432 ftebuf
[0] = (lp
- ftebuf
) * sizeof (ftebuf
[0]);
433 fnp
-> nodedata
= (char *) xmalloc (ftebuf
[0]);
434 memcpy (fnp
-> nodedata
, ftebuf
, ftebuf
[0]);
439 load_state_command (arg_string
, from_tty
)
446 struct cleanup
*cleanups
;
450 if (arg_string
== NULL
)
452 error ("load-state takes a file name and optional state specifiers");
454 else if ((argv
= buildargv (arg_string
)) == NULL
)
456 fatal ("virtual memory exhausted.", 0);
458 cleanups
= make_cleanup (freeargv
, argv
);
460 filename
= tilde_expand (*argv
);
461 make_cleanup (free
, filename
);
463 if ((fp
= fopen (filename
, FOPEN_RB
)) == NULL
)
465 perror_with_name (filename
);
467 make_cleanup (fclose
, fp
);
470 while (*++argv
!= NULL
)
472 if (STREQ (*argv
, "symbols"))
475 && !query ("load symbol table state from file \"%s\"? ",
478 error ("Not confirmed.");
484 error ("unknown state specifier '%s'", *argv
);
488 do_cleanups (cleanups
);
493 dump_state_command (arg_string
, from_tty
)
500 struct cleanup
*cleanups
;
504 if (arg_string
== NULL
)
506 error ("dump-state takes a file name and state specifiers");
508 else if ((argv
= buildargv (arg_string
)) == NULL
)
510 fatal ("virtual memory exhausted.", 0);
512 cleanups
= make_cleanup (freeargv
, argv
);
514 filename
= tilde_expand (*argv
);
515 make_cleanup (free
, filename
);
517 /* Now attempt to create a fresh state file. */
519 if ((asfd
= sfd_fopen (filename
, FOPEN_WB
)) == NULL
)
521 perror_with_name (filename
);
523 make_cleanup (sfd_fclose
, asfd
);
524 make_cleanup (free_formtree
, NULL
);
527 /* Now that we have an open and initialized state file, seek to the
528 proper offset to start writing state data and the process the
529 arguments. For each argument, write the state data and initialize
530 a form-tree node for each piece of state data. */
532 fseek (asfd
-> fp
, sizeof (sf_hdr
), SEEK_SET
);
533 while (*++argv
!= NULL
)
535 if (STREQ (*argv
, "objfile"))
537 write_objfile_state (asfd
);
541 error ("unknown state specifier '%s'", *argv
);
546 /* We have written any state data. All that is left to do now is
547 write the form-tree and the file header. */
549 write_formtree (asfd
);
553 do_cleanups (cleanups
);
557 find_fte_by_walk (thisfte
, endfte
, tag
)
568 while (thisfte
< endfte
)
570 if ((thistag
= *(long *)(thisfte
+ sizeof (long))) == tag
)
577 thissize
= *(long *)(thisfte
);
578 siboffset
= *(long *)(thisfte
+ (2 * sizeof (long)));
579 nextfte
= thisfte
+ (siboffset
!= 0 ? siboffset
: thissize
);
580 found
= find_fte_by_walk (thisfte
+ thissize
, nextfte
, tag
);
587 /* Walk the form-tree looking for a specific FTE type. Returns the first
588 one found that matches the specified tag. */
600 if (fseek (asfd
-> fp
, asfd
-> hdr
.sf_ftoff
, SEEK_SET
) == 0)
602 ftbase
= xmalloc (asfd
-> hdr
.sf_ftsize
);
603 ftend
= ftbase
+ asfd
-> hdr
.sf_ftsize
;
604 if (fread (ftbase
, asfd
-> hdr
.sf_ftsize
, 1, asfd
-> fp
) == 1)
606 ftep
= find_fte_by_walk (ftbase
, ftend
, tag
);
609 found
= xmalloc (*(long *)ftep
);
610 memcpy (found
, ftep
, (int) *(long *)ftep
);
619 objfile_from_statefile (asfd
)
622 struct objfile
*objfile
= NULL
;
630 ftep
= find_fte (asfd
, TAG_objfile
);
631 thisattr
= (long *) (ftep
+ 3 * sizeof (long));
632 endattr
= (long *) (ftep
+ *(long *)ftep
);
633 while (thisattr
< endattr
)
642 base
= (PTR
) *thisattr
++;
645 objfile
= (struct objfile
*) *thisattr
++;
648 foffset
= *thisattr
++;
651 mapsize
= *thisattr
++;
655 if (mmap_remap (base
, mapsize
, (int) fileno (asfd
-> fp
), foffset
) != base
)
657 print_sys_errmsg (asfd
-> filename
, errno
);
658 error ("mapping failed");
667 objfile_from_statefile (asfd
)
670 error ("this version of gdb doesn't support reloading symtabs from state files");
673 #endif /* HAVE_MMAP */
675 /* Close a state file, freeing all memory that was used by the state
676 file descriptor, closing the raw file pointer, etc. */
684 if (asfd
-> fp
!= NULL
)
688 if (asfd
-> filename
!= NULL
)
690 free (asfd
-> filename
);
696 /* Given the name of a possible statefile, and flags to use to open it,
697 try to open the file and prepare it for use.
699 If the flags contain 'r', then we want to read an existing state
700 file, so attempt to read in the state file header and determine if this
701 is a valid state file. If not, return NULL.
703 Returns a pointer to a properly initialized state file descriptor if
707 sfd_fopen (name
, flags
)
714 asfd
= (sfd
*) xmalloc (sizeof (sfd
));
715 (void) memset (asfd
, 0, sizeof (sfd
));
716 asfd
-> filename
= xmalloc (strlen (name
) + 1);
717 (void) strcpy (asfd
-> filename
, name
);
719 if ((asfd
-> fp
= fopen (asfd
-> filename
, flags
)) != NULL
)
721 /* We have the file, now see if we are reading an existing file
722 or writing to a new file. We don't currently support "rw". */
723 if (strchr (flags
, 'r') != NULL
)
725 if (fread ((char *) &asfd
-> hdr
, sizeof (asfd
-> hdr
), 1,
728 if (SF_GOOD_MAGIC (asfd
))
736 /* This is a new state file. Initialize various things. */
737 asfd
-> hdr
.sf_mag0
= SF_MAG0
;
738 asfd
-> hdr
.sf_mag1
= SF_MAG1
;
739 asfd
-> hdr
.sf_mag2
= SF_MAG2
;
740 asfd
-> hdr
.sf_mag3
= SF_MAG3
;
761 add_com ("load-state", class_support
, load_state_command
,
762 "Load some saved gdb state from FILE.\n\
763 Select and load some portion of gdb's saved state from the specified file.\n\
764 The dump-state command may be used to save various portions of gdb's\n\
767 add_com ("dump-state", class_support
, dump_state_command
,
768 "Dump some of gdb's state to FILE.\n\
769 Select and dump some portion of gdb's internal state to the specified file.\n\
770 The load-state command may be used to reload various portions of gdb's\n\
771 internal state from the file.");
773 #endif /* HAVE_MMAP */
This page took 0.048216 seconds and 4 git commands to generate.