* ========== Copyright Header Begin ==========================================
* Hypervisor Software File: datafile.c
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
* - Do no alter or remove copyright notices
* - Redistribution and use of this software in source and binary forms, with
* or without modification, are permitted provided that the following
* - Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of Sun Microsystems, Inc. or the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* This software is provided "AS IS," without a warranty of any kind.
* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
* MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
* OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
* FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
* DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
* SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* You acknowledge that this software is not designed, licensed or
* intended for use in the design, construction, operation or maintenance of
* ========== Copyright Header End ============================================
* @(#)datafile.c 1.1 02/05/02
* Copyright 2001-2002 Sun Microsystems, Inc. All Rights Reserved
* Copyright Use is subject to license terms.
write_4bytes(int n
, FILE *fd
)
#define LPRINTF(x) printf x
load_symbol_from_file(xref_state_t
*state
, int *sindex
, int n
, FILE *fd
)
LPRINTF(("Loading symbol: %d ", n
));
sym
= state
->xref_cache
[n
];
LPRINTF((" [cached] %x\n", sym
));
LPRINTF((" from file\n"));
new = malloc(sizeof (xref_t
));
memset(new, 0, sizeof (xref_t
));
if (strncmp(tag
, "SYM", 3) != 0) {
printf("Corrupted xref data file\n");
new->id
= read_4bytes(fd
);
new->flags
= read_4bytes(fd
);
new->name
= malloc(bytes
+ 1);
fread(new->name
, 1, bytes
, fd
);
LPRINTF(("....name: %s\n", new->name
));
/* get the file it was defined in */
new->file
= file
= state
->file_cache
[fid
];
new->linenum
= read_4bytes(fd
);
LPRINTF(("....File(%d) : %s:%d\n", fid
, file
->name
, new->linenum
));
xref_add_definition(file
->defs
, new);
/* prepare the data structures for the call refs */
new->calls
.insert
= read_4bytes(fd
);
new->called_by
.insert
= read_4bytes(fd
);
new->calls
.size
= new->calls
.insert
;
new->called_by
.size
= new->called_by
.insert
+ 10;
new->calls
.ptr
= malloc(new->calls
.size
* sizeof (xref_t
*));
LPRINTF(("....Calls: %d\n", new->calls
.insert
));
bytes
= new->calls
.insert
* sizeof (int);
calls_ptr
= malloc(bytes
);
fread(calls_ptr
, 1, bytes
, fd
);
/* prepare the data structures for the called_by refs */
bytes
= new->called_by
.size
* sizeof (xref_t
);
new->called_by
.ptr
= malloc(bytes
);
LPRINTF(("....Called by: %d\n", new->called_by
.insert
));
bytes
= new->called_by
.insert
* sizeof (int);
called_ptr
= malloc(bytes
);
fread(called_ptr
, 1, bytes
, fd
);
state
->xref_cache
[new->id
] = new;
xref_add_definition(state
->all_refs
, new);
for (j
= 0; j
< new->calls
.insert
; j
++) {
new->calls
.ptr
[j
] = load_symbol_from_file(state
,
sindex
, calls_ptr
[j
], fd
);
for (j
= 0; j
< new->called_by
.insert
; j
++) {
new->called_by
.ptr
[j
] = load_symbol_from_file(state
,
sindex
, called_ptr
[j
], fd
);
printf(": %s(%d) ", new->name
, new->id
);
for (j
= 0; j
< new->calls
.insert
; j
++) {
printf("%s(%d) ", new->calls
.ptr
[j
]->name
,
xref_load_file(char *filename
, int verbose
)
int *index
, *findex
, *sindex
, bytes
;
state
= xref_create_state();
fd
= fopen(filename
, "rb");
printf("failed to open Xref data file %s\n", filename
);
rebuild
= read_4bytes(fd
);
printf("loading xref data file %s, %d files, %d refs\n",
bytes
= sizeof (int) * (fid
+ sid
+ 3);
fread(index
, 1, bytes
, fd
);
state
->file_cache
= malloc(sizeof (xref_file_t
*)*fid
);
memset(state
->file_cache
, 0, sizeof (xref_file_t
*) * fid
);
state
->xref_cache
= malloc(sizeof (xref_t
*) * sid
);
memset(state
->xref_cache
, 0, sizeof (xref_t
*) * sid
);
fread(nameptr
, 1, len
, fd
);
newf
= xref_create_file_reference(nameptr
);
printf("File: %d: %s\n", i
, nameptr
);
newf
->next
= state
->sources
;
state
->file_cache
[i
] = newf
;
* Now weve loaded the file names and created the file->sym
* structures fill in the references.
sym
= load_symbol_from_file(state
, sindex
, i
++, fd
);
xref_save_file(char *filename
, xref_state_t
*state
)
xref_file_t
*fptr
= state
->sources
;
int *index
, *findex
, *sindex
, bytes
;
/* allocate a unique file ID */
/* Allocate a unique symbol ID */
for (i
= 0; i
< 128; i
++) {
rptr
= state
->all_refs
[i
];
/* Now write the binary file */
fd
= fopen(filename
, "wb");
printf("failed to open Xref database %s\n", filename
);
bytes
= sizeof (int) * (wid
+ fid
+ 3);
fseek(fd
, bytes
, 0); /* skip the index */
int len
= strlen(fptr
->name
);
findex
[fptr
->id
] = ftell(fd
);
for (i
= 0; i
< 128; i
++) {
rptr
= state
->all_refs
[i
];
printf("Saving: %s (%d)\n", rptr
->name
, rptr
->id
);
len
= strlen(rptr
->name
);
sindex
[rptr
->id
] = ftell(fd
);
write_4bytes(rptr
->id
, fd
);
write_4bytes(rptr
->flags
, fd
);
write_4bytes(rptr
->file
->id
, fd
);
write_4bytes(rptr
->linenum
, fd
);
write_4bytes(rptr
->calls
.insert
, fd
);
write_4bytes(rptr
->called_by
.insert
, fd
);
for (j
= 0; j
< rptr
->calls
.insert
; j
++) {
cptr
= rptr
->calls
.ptr
[j
];
write_4bytes(cptr
->id
, fd
);
for (j
= 0; j
< rptr
->called_by
.insert
; j
++) {
cptr
= rptr
->called_by
.ptr
[j
];
write_4bytes(cptr
->id
, fd
);
fwrite(index
, 1, bytes
, fd
);