0.9.2.42:
[sbcl/lichteblau.git] / src / runtime / coreparse.c
blobdd74c749959bbcc8c0965d4fe758dec2d03cd849
1 /*
2 * A saved SBCL system is a .core file; the code here helps us accept
3 * such a file as input.
4 */
6 /*
7 * This software is part of the SBCL system. See the README file for
8 * more information.
10 * This software is derived from the CMU CL system, which was
11 * written at Carnegie Mellon University and released into the
12 * public domain. The software is in the public domain and is
13 * provided with absolutely no warranty. See the COPYING and CREDITS
14 * files for more information.
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/file.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
26 #ifdef irix
27 #include <fcntl.h>
28 #endif
30 #include "sbcl.h"
31 #include "os.h"
32 #include "runtime.h"
33 #include "globals.h"
34 #include "core.h"
35 #include "arch.h"
36 #include "interr.h"
37 #include "thread.h"
39 unsigned char build_id[] =
40 #include "../../output/build-id.tmp"
43 static void
44 process_directory(int fd, u32 *ptr, int count)
46 struct ndir_entry *entry;
48 FSHOW((stderr, "/process_directory(..), count=%d\n", count));
50 for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
52 long id = entry->identifier;
53 long offset = os_vm_page_size * (1 + entry->data_page);
54 os_vm_address_t addr =
55 (os_vm_address_t) (os_vm_page_size * entry->address);
56 lispobj *free_pointer = (lispobj *) addr + entry->nwords;
57 long len = os_vm_page_size * entry->page_count;
59 if (len != 0) {
60 os_vm_address_t real_addr;
61 FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
62 (long)len, (long)len, addr));
63 real_addr = os_map(fd, offset, addr, len);
64 if (real_addr != addr) {
65 lose("file mapped in wrong place! "
66 "(0x%08x != 0x%08lx)",
67 real_addr,
68 addr);
72 FSHOW((stderr, "/space id = %d, free pointer = 0x%08x\n",
73 id, (long)free_pointer));
75 switch (id) {
76 case DYNAMIC_CORE_SPACE_ID:
77 #ifdef LISP_FEATURE_GENCGC
78 if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
79 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
80 (long)addr, (long)DYNAMIC_SPACE_START);
81 lose("core/runtime address mismatch: DYNAMIC_SPACE_START");
83 #else
84 if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
85 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
86 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
87 (long)addr,
88 (long)DYNAMIC_0_SPACE_START,
89 (long)DYNAMIC_1_SPACE_START);
90 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START");
92 #endif
93 #if defined(ALLOCATION_POINTER)
94 SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
95 #else
96 dynamic_space_free_pointer = free_pointer;
97 #endif
98 /* For stop-and-copy GC, this will be whatever the GC was
99 * using at the time. With GENCGC, this will always be
100 * space 0. (We checked above that for GENCGC,
101 * addr==DYNAMIC_SPACE_START.) */
102 current_dynamic_space = (lispobj *)addr;
103 break;
104 case STATIC_CORE_SPACE_ID:
105 if (addr != (os_vm_address_t)STATIC_SPACE_START) {
106 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
107 (long)addr, (long)STATIC_SPACE_START);
108 lose("core/runtime address mismatch: STATIC_SPACE_START");
110 break;
111 case READ_ONLY_CORE_SPACE_ID:
112 if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
113 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
114 (long)addr, (long)READ_ONLY_SPACE_START);
115 lose("core/runtime address mismatch: READ_ONLY_SPACE_START");
117 break;
118 default:
119 lose("unknown space ID %ld addr 0x%p", id);
124 lispobj
125 load_core_file(char *file)
127 lispobj *header, val, len, *ptr, remaining_len;
128 int fd = open(file, O_RDONLY), count;
130 lispobj initial_function = NIL;
131 FSHOW((stderr, "/entering load_core_file(%s)\n", file));
132 if (fd < 0) {
133 fprintf(stderr, "could not open file \"%s\"\n", file);
134 perror("open");
135 exit(1);
138 header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
140 count = read(fd, header, os_vm_page_size);
141 if (count < os_vm_page_size) {
142 lose("premature end of core file");
144 SHOW("successfully read first page of core");
146 ptr = header;
147 val = *ptr++;
149 if (val != CORE_MAGIC) {
150 lose("invalid magic number in core: 0x%lx should have been 0x%x.",
151 val,
152 CORE_MAGIC);
154 SHOW("found CORE_MAGIC");
156 while (val != END_CORE_ENTRY_TYPE_CODE) {
157 val = *ptr++;
158 len = *ptr++;
159 remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
160 FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
161 (long)val, (long)remaining_len));
163 switch (val) {
165 case END_CORE_ENTRY_TYPE_CODE:
166 SHOW("END_CORE_ENTRY_TYPE_CODE case");
167 break;
169 case VERSION_CORE_ENTRY_TYPE_CODE:
170 SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
171 if (*ptr != SBCL_CORE_VERSION_INTEGER) {
172 lose("core file version (%d) != runtime library version (%d)",
173 *ptr,
174 SBCL_CORE_VERSION_INTEGER);
176 break;
178 case BUILD_ID_CORE_ENTRY_TYPE_CODE:
179 SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
181 int i;
183 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
184 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
185 if (remaining_len != strlen(build_id))
186 goto losing_build_id;
187 for (i = 0; i < remaining_len; ++i) {
188 FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
189 i, ptr[i], build_id[i]));
190 if (ptr[i] != build_id[i])
191 goto losing_build_id;
193 break;
194 losing_build_id:
195 /* .core files are not binary-compatible between
196 * builds because we can't easily detect whether the
197 * sources were patched between the time the
198 * dumping-the-.core runtime was built and the time
199 * that the loading-the-.core runtime was built.
201 * (We could easily detect whether version.lisp-expr
202 * was changed, but people experimenting with patches
203 * don't necessarily update version.lisp-expr.) */
205 lose("can't load .core for different runtime, sorry");
208 case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
209 SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
210 process_directory(fd,
211 ptr,
212 #ifndef LISP_FEATURE_ALPHA
213 remaining_len / (sizeof(struct ndir_entry) /
214 sizeof(long))
215 #else
216 remaining_len / (sizeof(struct ndir_entry) /
217 sizeof(u32))
218 #endif
220 break;
222 case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
223 SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
224 initial_function = (lispobj)*ptr;
225 break;
227 default:
228 lose("unknown core file entry: %ld", (long)val);
231 ptr += remaining_len;
232 FSHOW((stderr, "/new ptr=%x\n", ptr));
234 SHOW("about to free(header)");
235 free(header);
236 SHOW("returning from load_core_file(..)");
237 return initial_function;