442
|
1 /* Portable data dumper for XEmacs.
|
|
2 Copyright (C) 1999-2000 Olivier Galibert
|
458
|
3 Copyright (C) 2001 Martin Buchholz
|
2367
|
4 Copyright (C) 2001, 2002, 2003, 2004 Ben Wing.
|
442
|
5
|
|
6 This file is part of XEmacs.
|
|
7
|
|
8 XEmacs is free software; you can redistribute it and/or modify it
|
|
9 under the terms of the GNU General Public License as published by the
|
|
10 Free Software Foundation; either version 2, or (at your option) any
|
|
11 later version.
|
|
12
|
|
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
16 for more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License
|
|
19 along with XEmacs; see the file COPYING. If not, write to
|
|
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
21 Boston, MA 02111-1307, USA. */
|
|
22
|
|
23 /* Synched up with: Not in FSF. */
|
|
24
|
2367
|
25 /* This file has been Mule-ized, Ben Wing, 10-10-04. */
|
|
26
|
|
27 /* #### Put in much more assertions. Whenever we store fixups in the
|
|
28 process or writing out data, make sure the fixups (offsets) point to the
|
|
29 beginning of an object, i.e. are registered. Same whenever we read in
|
|
30 -- verify offsets as registered, and when compute a fixup, verify the
|
|
31 pointer is pointing within the pdump area. registered and check within
|
|
32 pdump area. For specific types of pointers (e.g. to Lisp_Objects),
|
|
33 check if they're pointing to the right kinds of types. It should be
|
|
34 possible to check that a putative Lisp_Object is really a Lisp_Object
|
|
35 since it will follow a strict format in its header. */
|
800
|
36
|
442
|
37 #include <config.h>
|
|
38 #include "lisp.h"
|
|
39
|
|
40 #include "specifier.h"
|
771
|
41 #include "file-coding.h"
|
442
|
42 #include "elhash.h"
|
1204
|
43 #include "lstream.h"
|
442
|
44 #include "sysfile.h"
|
|
45 #include "console-stream.h"
|
|
46
|
|
47 #ifdef WIN32_NATIVE
|
771
|
48 #include "syswindows.h"
|
442
|
49 #else
|
|
50 #ifdef HAVE_MMAP
|
|
51 #include <sys/mman.h>
|
|
52 #endif
|
2015
|
53 #include "dump-data.h"
|
442
|
54 #endif
|
|
55
|
|
56 typedef struct
|
|
57 {
|
2367
|
58 const void *blockaddr;
|
665
|
59 Bytecount size;
|
1204
|
60 const struct memory_description *desc;
|
|
61 } pdump_root_block;
|
452
|
62
|
|
63 typedef struct
|
|
64 {
|
1204
|
65 Dynarr_declare (pdump_root_block);
|
|
66 } pdump_root_block_dynarr;
|
452
|
67
|
|
68 typedef struct
|
|
69 {
|
|
70 void **ptraddress;
|
1204
|
71 const struct sized_memory_description *desc;
|
2367
|
72 } pdump_root_block_ptr;
|
452
|
73
|
|
74 typedef struct
|
|
75 {
|
2367
|
76 Dynarr_declare (pdump_root_block_ptr);
|
|
77 } pdump_root_block_ptr_dynarr;
|
452
|
78
|
458
|
79 typedef struct
|
|
80 {
|
|
81 Lisp_Object *address;
|
|
82 Lisp_Object value;
|
|
83 } pdump_static_Lisp_Object;
|
|
84
|
|
85 typedef struct
|
|
86 {
|
2367
|
87 Rawbyte **address; /* Rawbyte * for ease of doing relocation */
|
|
88 Rawbyte * value;
|
458
|
89 } pdump_static_pointer;
|
|
90
|
1204
|
91 static pdump_root_block_dynarr *pdump_root_blocks;
|
2367
|
92 static pdump_root_block_ptr_dynarr *pdump_root_block_ptrs;
|
1204
|
93 static Lisp_Object_ptr_dynarr *pdump_root_lisp_objects;
|
452
|
94 static Lisp_Object_ptr_dynarr *pdump_weak_object_chains;
|
|
95
|
2367
|
96 /* Mark SIZE bytes at non-heap address BLOCKADDR for dumping, described
|
|
97 by DESC. Called by outside callers during XEmacs initialization. */
|
|
98
|
452
|
99 void
|
2367
|
100 dump_add_root_block (const void *blockaddr, Bytecount size,
|
1204
|
101 const struct memory_description *desc)
|
452
|
102 {
|
1204
|
103 pdump_root_block info;
|
2367
|
104 info.blockaddr = blockaddr;
|
452
|
105 info.size = size;
|
1204
|
106 info.desc = desc;
|
|
107 if (pdump_root_blocks == NULL)
|
|
108 pdump_root_blocks = Dynarr_new (pdump_root_block);
|
|
109 Dynarr_add (pdump_root_blocks, info);
|
452
|
110 }
|
|
111
|
2367
|
112 /* Mark the block described by DESC and pointed to by the pointer at
|
|
113 non-heap address PTRADDRESS for dumping.
|
|
114 All the objects reachable from this pointer will also be dumped.
|
|
115 Called by outside callers during XEmacs initialization. */
|
452
|
116 void
|
2367
|
117 dump_add_root_block_ptr (void *ptraddress,
|
|
118 const struct sized_memory_description *desc)
|
452
|
119 {
|
2367
|
120 pdump_root_block_ptr info;
|
452
|
121 info.ptraddress = (void **) ptraddress;
|
|
122 info.desc = desc;
|
2367
|
123 if (pdump_root_block_ptrs == NULL)
|
|
124 pdump_root_block_ptrs = Dynarr_new (pdump_root_block_ptr);
|
|
125 Dynarr_add (pdump_root_block_ptrs, info);
|
452
|
126 }
|
|
127
|
|
128 /* Mark the Lisp_Object at non-heap address VARADDRESS for dumping.
|
2367
|
129 All the objects reachable from this var will also be dumped.
|
|
130 Called by outside callers during XEmacs initialization. */
|
452
|
131 void
|
1204
|
132 dump_add_root_lisp_object (Lisp_Object *varaddress)
|
452
|
133 {
|
1204
|
134 if (pdump_root_lisp_objects == NULL)
|
|
135 pdump_root_lisp_objects = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
|
|
136 Dynarr_add (pdump_root_lisp_objects, varaddress);
|
452
|
137 }
|
|
138
|
2367
|
139 /* Mark the list pointed to by the Lisp_Object at VARADDRESS for dumping.
|
|
140 Called by outside callers during XEmacs initialization. */
|
452
|
141 void
|
|
142 dump_add_weak_object_chain (Lisp_Object *varaddress)
|
|
143 {
|
|
144 if (pdump_weak_object_chains == NULL)
|
|
145 pdump_weak_object_chains = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
|
|
146 Dynarr_add (pdump_weak_object_chains, varaddress);
|
|
147 }
|
|
148
|
|
149
|
458
|
150 inline static void
|
665
|
151 pdump_align_stream (FILE *stream, Bytecount alignment)
|
458
|
152 {
|
|
153 long offset = ftell (stream);
|
|
154 long adjustment = ALIGN_SIZE (offset, alignment) - offset;
|
|
155 if (adjustment)
|
|
156 fseek (stream, adjustment, SEEK_CUR);
|
|
157 }
|
|
158
|
|
159 #define PDUMP_ALIGN_OUTPUT(type) pdump_align_stream (pdump_out, ALIGNOF (type))
|
|
160
|
|
161 #define PDUMP_WRITE(type, object) \
|
771
|
162 retry_fwrite (&object, sizeof (object), 1, pdump_out);
|
458
|
163
|
|
164 #define PDUMP_WRITE_ALIGNED(type, object) do { \
|
|
165 PDUMP_ALIGN_OUTPUT (type); \
|
|
166 PDUMP_WRITE (type, object); \
|
|
167 } while (0)
|
|
168
|
|
169 #define PDUMP_READ(ptr, type) \
|
2367
|
170 (((type *) (ptr = (Rawbyte *) (((type *) ptr) + 1)))[-1])
|
458
|
171
|
|
172 #define PDUMP_READ_ALIGNED(ptr, type) \
|
2367
|
173 ((ptr = (Rawbyte *) ALIGN_PTR (ptr, type)), PDUMP_READ (ptr, type))
|
458
|
174
|
|
175
|
|
176
|
452
|
177 typedef struct
|
|
178 {
|
1204
|
179 const struct memory_description *desc;
|
442
|
180 int count;
|
|
181 } pdump_reloc_table;
|
|
182
|
2367
|
183 static Rawbyte *pdump_rt_list = 0;
|
442
|
184
|
|
185 void
|
|
186 pdump_objects_unmark (void)
|
|
187 {
|
|
188 int i;
|
2367
|
189 Rawbyte *p = pdump_rt_list;
|
442
|
190 if (p)
|
|
191 for (;;)
|
|
192 {
|
|
193 pdump_reloc_table *rt = (pdump_reloc_table *)p;
|
|
194 p += sizeof (pdump_reloc_table);
|
|
195 if (rt->desc)
|
|
196 {
|
|
197 for (i=0; i<rt->count; i++)
|
|
198 {
|
|
199 struct lrecord_header *lh = * (struct lrecord_header **) p;
|
|
200 if (! C_READONLY_RECORD_HEADER_P (lh))
|
|
201 UNMARK_RECORD_HEADER (lh);
|
|
202 p += sizeof (EMACS_INT);
|
|
203 }
|
|
204 } else
|
|
205 break;
|
|
206 }
|
|
207 }
|
|
208
|
|
209
|
1204
|
210 /* The structure of the dump file looks like this:
|
458
|
211 0 - header
|
|
212 - dumped objects
|
2367
|
213 stab_offset - nb_root_block_ptrs*struct(void *, adr)
|
|
214 for global pointers to heap blocks
|
1204
|
215 - nb_root_blocks*struct(void *, size, info) for global
|
2367
|
216 data-segment blocks to restore
|
458
|
217 - relocation table
|
|
218 - root lisp object address/value couples with the count
|
|
219 preceding the list
|
442
|
220 */
|
|
221
|
|
222
|
452
|
223 #define PDUMP_SIGNATURE "XEmacsDP"
|
|
224 #define PDUMP_SIGNATURE_LEN (sizeof (PDUMP_SIGNATURE) - 1)
|
442
|
225
|
|
226 typedef struct
|
|
227 {
|
452
|
228 char signature[PDUMP_SIGNATURE_LEN];
|
442
|
229 unsigned int id;
|
|
230 EMACS_UINT stab_offset;
|
|
231 EMACS_UINT reloc_address;
|
2367
|
232 int nb_root_block_ptrs;
|
1204
|
233 int nb_root_blocks;
|
452
|
234 } pdump_header;
|
442
|
235
|
2367
|
236 Rawbyte *pdump_start;
|
|
237 Rawbyte *pdump_end;
|
665
|
238 static Bytecount pdump_length;
|
442
|
239
|
|
240 #ifdef WIN32_NATIVE
|
452
|
241 /* Handle for the dump file */
|
458
|
242 static HANDLE pdump_hFile = INVALID_HANDLE_VALUE;
|
452
|
243 /* Handle for the file mapping object for the dump file */
|
458
|
244 static HANDLE pdump_hMap = INVALID_HANDLE_VALUE;
|
442
|
245 #endif
|
|
246
|
458
|
247 static void (*pdump_free) (void);
|
442
|
248
|
460
|
249 static unsigned char pdump_align_table[] =
|
442
|
250 {
|
460
|
251 64, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1,
|
|
252 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1,
|
|
253 32, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1,
|
|
254 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1
|
442
|
255 };
|
|
256
|
647
|
257 static inline int
|
665
|
258 pdump_size_to_align (Bytecount size)
|
442
|
259 {
|
460
|
260 return pdump_align_table[size % countof (pdump_align_table)];
|
|
261 }
|
|
262
|
2367
|
263 /************************************************************************/
|
|
264 /* Registering memory blocks */
|
|
265 /************************************************************************/
|
|
266
|
|
267 /* "Registering" or recording a heap memory block (which will need to be
|
|
268 written out, reloaded and relocated, and to which there may be pointers
|
|
269 from other heap blocks or from the data segment) happens both in a list
|
|
270 and in a hash table. There is a single hash table covering all
|
|
271 registered blocks, but different lists for different kinds of blocks.
|
|
272 There is one list for "opaque data" (stuff identified as
|
|
273 XD_OPAQUE_DATA_PTR, XD_ASCII_STRING, XD_DOC_STRING), one list for each
|
|
274 type of Lisp object, and one list for each different memory descriptor.
|
|
275 This lets similar-sized and aligned objects be grouped together when
|
|
276 they are written out, to save space.
|
|
277
|
|
278 pdump_block_list is a list keeping track of registered memory blocks.
|
|
279 pdump_block_list_elt is a single entry through the list, and the list is
|
|
280 threaded through the NEXT pointer. The information in this list
|
|
281 associated with a particular block of memory is
|
|
282
|
|
283 -- address of the beginning
|
|
284 -- number of elements at that address
|
|
285 -- size of each element
|
|
286 -- offset to this block in the dumped data
|
|
287
|
|
288 pdump_desc_list is a list keeping track of the various descriptions
|
|
289 that we've seen. The primary purpose of this is so that memory blocks
|
|
290 can be grouped depending on the particular memory description
|
|
291 appropriate for them. The format of the list is different from
|
|
292 pdump_block_list -- a single array is used. (#### Dynarr should have
|
|
293 been used!!!). The information in this list associated with a
|
|
294 description is
|
|
295
|
|
296 -- pointer to the description
|
|
297 -- a pdump_block_list of blocks using that description
|
|
298
|
|
299 Functions for working with lists of memory blocks:
|
|
300
|
|
301 -- Add a memory block to a list using pdump_add_block()
|
|
302
|
|
303 -- Get a memory block from a pointer to its beginning using
|
|
304 pdump_get_block(). This uses the hash table, which lists everything.
|
|
305
|
|
306 -- Return the memory-block list (pdump_block_list) associated with a
|
|
307 descriptor, using pdump_get_block_list(). If no entry found in the
|
|
308 pdump_desc_list, add a new one.
|
|
309
|
|
310 */
|
|
311
|
|
312 typedef struct pdump_block_list_elt
|
460
|
313 {
|
2367
|
314 struct pdump_block_list_elt *next;
|
442
|
315 const void *obj;
|
665
|
316 Bytecount size;
|
442
|
317 int count;
|
|
318 EMACS_INT save_offset;
|
2367
|
319 } pdump_block_list_elt;
|
442
|
320
|
|
321 typedef struct
|
|
322 {
|
2367
|
323 pdump_block_list_elt *first;
|
442
|
324 int align;
|
|
325 int count;
|
2367
|
326 } pdump_block_list;
|
442
|
327
|
2367
|
328 typedef struct pdump_desc_list_elt
|
442
|
329 {
|
2367
|
330 pdump_block_list list;
|
1204
|
331 const struct memory_description *desc;
|
2367
|
332 } pdump_desc_list_elt;
|
442
|
333
|
|
334 typedef struct
|
|
335 {
|
2367
|
336 pdump_desc_list_elt *list;
|
442
|
337 int count;
|
|
338 int size;
|
2367
|
339 } pdump_desc_list;
|
442
|
340
|
2367
|
341 static pdump_block_list *pdump_object_table;
|
|
342 static pdump_block_list pdump_opaque_data_list;
|
|
343 static pdump_desc_list pdump_desc_table;
|
442
|
344
|
460
|
345 static int *pdump_alert_undump_object;
|
442
|
346
|
|
347 static unsigned long cur_offset;
|
665
|
348 static Bytecount max_size;
|
442
|
349 static int pdump_fd;
|
|
350 static void *pdump_buf;
|
458
|
351 static FILE *pdump_out;
|
442
|
352
|
|
353 #define PDUMP_HASHSIZE 200001
|
|
354
|
2367
|
355 static pdump_block_list_elt **pdump_hash;
|
442
|
356
|
|
357 /* Since most pointers are eight bytes aligned, the >>3 allows for a better hash */
|
|
358 static int
|
|
359 pdump_make_hash (const void *obj)
|
|
360 {
|
|
361 return ((unsigned long)(obj)>>3) % PDUMP_HASHSIZE;
|
|
362 }
|
|
363
|
2367
|
364 /* Return the entry for an already-registered memory block at OBJ,
|
|
365 or NULL if none. */
|
|
366
|
|
367 static pdump_block_list_elt *
|
|
368 pdump_get_block (const void *obj)
|
442
|
369 {
|
|
370 int pos = pdump_make_hash (obj);
|
2367
|
371 pdump_block_list_elt *e;
|
442
|
372
|
|
373 assert (obj != 0);
|
|
374
|
|
375 while ((e = pdump_hash[pos]) != 0)
|
|
376 {
|
|
377 if (e->obj == obj)
|
|
378 return e;
|
|
379
|
|
380 pos++;
|
|
381 if (pos == PDUMP_HASHSIZE)
|
|
382 pos = 0;
|
|
383 }
|
|
384 return 0;
|
|
385 }
|
|
386
|
2367
|
387 /* Register a new memory block on Return the entry for an already-registered heap (?) memory block at OBJ,
|
|
388 or NULL if none. */
|
|
389
|
442
|
390 static void
|
2367
|
391 pdump_add_block (pdump_block_list *list, const void *obj, Bytecount size,
|
458
|
392 int count)
|
442
|
393 {
|
2367
|
394 pdump_block_list_elt *e;
|
442
|
395 int pos = pdump_make_hash (obj);
|
|
396
|
|
397 while ((e = pdump_hash[pos]) != 0)
|
|
398 {
|
|
399 if (e->obj == obj)
|
|
400 return;
|
|
401
|
|
402 pos++;
|
|
403 if (pos == PDUMP_HASHSIZE)
|
|
404 pos = 0;
|
|
405 }
|
|
406
|
2367
|
407 e = xnew (pdump_block_list_elt);
|
442
|
408
|
|
409 e->next = list->first;
|
|
410 e->obj = obj;
|
|
411 e->size = size;
|
|
412 e->count = count;
|
|
413 list->first = e;
|
|
414
|
|
415 list->count += count;
|
|
416 pdump_hash[pos] = e;
|
|
417
|
460
|
418 {
|
|
419 int align = pdump_size_to_align (size);
|
442
|
420
|
460
|
421 if (align < list->align)
|
|
422 list->align = align;
|
|
423 }
|
442
|
424 }
|
|
425
|
2367
|
426 static pdump_block_list *
|
|
427 pdump_get_block_list (const struct memory_description *desc)
|
442
|
428 {
|
|
429 int i;
|
2367
|
430 for (i=0; i<pdump_desc_table.count; i++)
|
|
431 if (pdump_desc_table.list[i].desc == desc)
|
|
432 return &pdump_desc_table.list[i].list;
|
442
|
433
|
2367
|
434 if (pdump_desc_table.size <= pdump_desc_table.count)
|
442
|
435 {
|
2367
|
436 if (pdump_desc_table.size == -1)
|
|
437 pdump_desc_table.size = 10;
|
442
|
438 else
|
2367
|
439 pdump_desc_table.size = pdump_desc_table.size * 2;
|
|
440 pdump_desc_table.list = (pdump_desc_list_elt *)
|
|
441 xrealloc (pdump_desc_table.list,
|
|
442 pdump_desc_table.size * sizeof (pdump_desc_list_elt));
|
442
|
443 }
|
2367
|
444 pdump_desc_table.list[pdump_desc_table.count].list.first = 0;
|
|
445 pdump_desc_table.list[pdump_desc_table.count].list.align = ALIGNOF (max_align_t);
|
|
446 pdump_desc_table.list[pdump_desc_table.count].list.count = 0;
|
|
447 pdump_desc_table.list[pdump_desc_table.count].desc = desc;
|
442
|
448
|
2367
|
449 return &pdump_desc_table.list[pdump_desc_table.count++].list;
|
442
|
450 }
|
|
451
|
|
452 static struct
|
|
453 {
|
|
454 struct lrecord_header *obj;
|
|
455 int position;
|
|
456 int offset;
|
|
457 } backtrace[65536];
|
|
458
|
1204
|
459 static int pdump_depth;
|
442
|
460
|
1204
|
461 void
|
452
|
462 pdump_backtrace (void)
|
442
|
463 {
|
|
464 int i;
|
|
465 stderr_out ("pdump backtrace :\n");
|
1204
|
466 for (i = 0; i < pdump_depth; i++)
|
442
|
467 {
|
|
468 if (!backtrace[i].obj)
|
458
|
469 stderr_out (" - ind. (%d, %d)\n",
|
|
470 backtrace[i].position,
|
|
471 backtrace[i].offset);
|
442
|
472 else
|
|
473 {
|
|
474 stderr_out (" - %s (%d, %d)\n",
|
1204
|
475 LHEADER_IMPLEMENTATION (backtrace[i].obj)->name,
|
|
476 backtrace[i].position,
|
|
477 backtrace[i].offset);
|
442
|
478 }
|
|
479 }
|
|
480 }
|
|
481
|
1204
|
482 static void
|
1333
|
483 pdump_unsupported_dump_type (enum memory_description_type type,
|
|
484 int do_backtrace)
|
|
485 {
|
|
486 stderr_out ("Unsupported dump type : %d\n", type);
|
|
487 #ifdef WIN32_NATIVE
|
|
488 stderr_out ("Are you compiling with SUPPORT_EDIT_AND_CONTINUE?\n");
|
|
489 stderr_out ("See the PROBLEMS file.\n");
|
|
490 #endif
|
|
491 if (do_backtrace)
|
|
492 pdump_backtrace ();
|
|
493 abort ();
|
|
494 }
|
|
495
|
|
496 static void
|
1204
|
497 pdump_bump_depth (void)
|
|
498 {
|
|
499 int me = pdump_depth++;
|
|
500 if (me > 65536)
|
|
501 {
|
|
502 stderr_out ("Backtrace overflow, loop ?\n");
|
|
503 abort ();
|
|
504 }
|
|
505 backtrace[me].obj = 0;
|
|
506 backtrace[me].position = 0;
|
|
507 backtrace[me].offset = 0;
|
|
508 }
|
|
509
|
442
|
510 static void pdump_register_object (Lisp_Object obj);
|
2367
|
511 static void pdump_register_block_contents (const void *data,
|
|
512 Bytecount size,
|
|
513 const struct memory_description *
|
|
514 desc,
|
|
515 int count);
|
|
516 static void pdump_register_block (const void *data,
|
|
517 Bytecount size,
|
|
518 const struct memory_description *desc,
|
|
519 int count);
|
442
|
520
|
|
521 static void
|
1204
|
522 pdump_register_sub (const void *data, const struct memory_description *desc)
|
442
|
523 {
|
|
524 int pos;
|
1204
|
525 int me = pdump_depth - 1;
|
442
|
526
|
|
527 for (pos = 0; desc[pos].type != XD_END; pos++)
|
|
528 {
|
1204
|
529 const struct memory_description *desc1 = &desc[pos];
|
|
530 EMACS_INT offset = lispdesc_indirect_count (desc1->offset, desc,
|
|
531 data);
|
2367
|
532 const void *rdata = (const Rawbyte *) data + offset;
|
442
|
533
|
|
534 backtrace[me].position = pos;
|
1204
|
535 backtrace[me].offset = offset;
|
|
536
|
|
537 union_switcheroo:
|
442
|
538
|
1204
|
539 /* If the flag says don't dump, then don't dump. */
|
|
540 if ((desc1->flags) & XD_FLAG_NO_PDUMP)
|
|
541 continue;
|
|
542
|
|
543 switch (desc1->type)
|
442
|
544 {
|
665
|
545 case XD_BYTECOUNT:
|
|
546 case XD_ELEMCOUNT:
|
|
547 case XD_HASHCODE:
|
442
|
548 case XD_INT:
|
|
549 case XD_LONG:
|
|
550 case XD_INT_RESET:
|
|
551 case XD_LO_LINK:
|
|
552 break;
|
|
553 case XD_OPAQUE_DATA_PTR:
|
|
554 {
|
1204
|
555 EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc,
|
|
556 data);
|
442
|
557
|
2367
|
558 pdump_add_block (&pdump_opaque_data_list,
|
458
|
559 *(void **)rdata, count, 1);
|
442
|
560 break;
|
|
561 }
|
2367
|
562 case XD_ASCII_STRING:
|
442
|
563 {
|
2367
|
564 const Ascbyte *str = * (const Ascbyte **) rdata;
|
442
|
565 if (str)
|
2367
|
566 pdump_add_block (&pdump_opaque_data_list, str, strlen (str) + 1,
|
1204
|
567 1);
|
442
|
568 break;
|
|
569 }
|
|
570 case XD_DOC_STRING:
|
|
571 {
|
2367
|
572 const Ascbyte *str = * (const Ascbyte **) rdata;
|
1204
|
573 if ((EMACS_INT) str > 0)
|
2367
|
574 pdump_add_block (&pdump_opaque_data_list, str, strlen (str) + 1,
|
1204
|
575 1);
|
442
|
576 break;
|
|
577 }
|
|
578 case XD_LISP_OBJECT:
|
|
579 {
|
1204
|
580 const Lisp_Object *pobj = (const Lisp_Object *) rdata;
|
442
|
581
|
1204
|
582 assert (desc1->data1 == 0);
|
442
|
583
|
2367
|
584 backtrace[me].offset =
|
|
585 (const Rawbyte *) pobj - (const Rawbyte *) data;
|
442
|
586 pdump_register_object (*pobj);
|
|
587 break;
|
|
588 }
|
|
589 case XD_LISP_OBJECT_ARRAY:
|
|
590 {
|
|
591 int i;
|
1204
|
592 EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc,
|
|
593 data);
|
442
|
594
|
|
595 for (i = 0; i < count; i++)
|
|
596 {
|
1204
|
597 const Lisp_Object *pobj = ((const Lisp_Object *) rdata) + i;
|
442
|
598 Lisp_Object dobj = *pobj;
|
|
599
|
1204
|
600 backtrace[me].offset =
|
2367
|
601 (const Rawbyte *) pobj - (const Rawbyte *) data;
|
442
|
602 pdump_register_object (dobj);
|
|
603 }
|
|
604 break;
|
|
605 }
|
2367
|
606 case XD_BLOCK_PTR:
|
442
|
607 {
|
1204
|
608 EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc,
|
|
609 data);
|
|
610 const struct sized_memory_description *sdesc =
|
|
611 lispdesc_indirect_description (data, desc1->data2);
|
2367
|
612 const Rawbyte *dobj = *(const Rawbyte **)rdata;
|
442
|
613 if (dobj)
|
2367
|
614 pdump_register_block (dobj, sdesc->size, sdesc->description,
|
|
615 count);
|
442
|
616 break;
|
|
617 }
|
2367
|
618 case XD_BLOCK_ARRAY:
|
771
|
619 {
|
1204
|
620 EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc,
|
|
621 data);
|
|
622 const struct sized_memory_description *sdesc =
|
|
623 lispdesc_indirect_description (data, desc1->data2);
|
771
|
624
|
2367
|
625 pdump_register_block_contents (rdata, sdesc->size,
|
|
626 sdesc->description, count);
|
771
|
627 break;
|
|
628 }
|
|
629 case XD_UNION:
|
1204
|
630 case XD_UNION_DYNAMIC_SIZE:
|
|
631 desc1 = lispdesc_process_xd_union (desc1, desc, data);
|
|
632 if (desc1)
|
|
633 goto union_switcheroo;
|
|
634 break;
|
771
|
635
|
442
|
636 default:
|
1333
|
637 pdump_unsupported_dump_type (desc1->type, 1);
|
1204
|
638 }
|
442
|
639 }
|
|
640 }
|
|
641
|
|
642 static void
|
|
643 pdump_register_object (Lisp_Object obj)
|
|
644 {
|
|
645 struct lrecord_header *objh;
|
458
|
646 const struct lrecord_implementation *imp;
|
442
|
647
|
|
648 if (!POINTER_TYPE_P (XTYPE (obj)))
|
|
649 return;
|
|
650
|
|
651 objh = XRECORD_LHEADER (obj);
|
|
652 if (!objh)
|
|
653 return;
|
|
654
|
2367
|
655 if (pdump_get_block (objh))
|
442
|
656 return;
|
|
657
|
458
|
658 imp = LHEADER_IMPLEMENTATION (objh);
|
|
659
|
934
|
660 if (imp->description
|
1204
|
661 && RECORD_DUMPABLE (objh))
|
442
|
662 {
|
1204
|
663 pdump_bump_depth ();
|
|
664 backtrace[pdump_depth - 1].obj = objh;
|
2367
|
665 pdump_add_block (pdump_object_table + objh->type,
|
1204
|
666 objh, detagged_lisp_object_size (objh), 1);
|
|
667 pdump_register_sub (objh, imp->description);
|
|
668 --pdump_depth;
|
442
|
669 }
|
|
670 else
|
|
671 {
|
|
672 pdump_alert_undump_object[objh->type]++;
|
458
|
673 stderr_out ("Undumpable object type : %s\n", imp->name);
|
442
|
674 pdump_backtrace ();
|
|
675 }
|
|
676 }
|
|
677
|
2367
|
678 /* Register the referenced objects in the array of COUNT blocks located at
|
|
679 DATA; each block is described by SIZE and DESC. "Block" here simply
|
|
680 means any block of memory.
|
771
|
681
|
|
682 This does not register the block of memory itself; it may, for
|
|
683 example, be an array of structures inlined in another memory block
|
2367
|
684 and thus should not be registered. See pdump_register_block(),
|
771
|
685 which does register the memory block. */
|
|
686
|
|
687 static void
|
2367
|
688 pdump_register_block_contents (const void *data,
|
|
689 Bytecount size,
|
|
690 const struct memory_description *desc,
|
|
691 int count)
|
771
|
692 {
|
|
693 int i;
|
|
694 Bytecount elsize;
|
|
695
|
1204
|
696 pdump_bump_depth ();
|
2367
|
697 elsize = lispdesc_block_size_1 (data, size, desc);
|
771
|
698 for (i = 0; i < count; i++)
|
|
699 {
|
2367
|
700 pdump_register_sub (((Rawbyte *) data) + elsize * i, desc);
|
771
|
701 }
|
1204
|
702 --pdump_depth;
|
771
|
703 }
|
|
704
|
2367
|
705 /* Register the array of COUNT blocks located at DATA; each block is
|
|
706 described by SDESC. "Block" here simply means any block of memory,
|
|
707 which is more accurate and less confusing than terms like `struct' and
|
|
708 `object'. A `block' need not actually be a C "struct". It could be a
|
|
709 single integer or Lisp_Object, for example, as long as the description
|
|
710 is accurate.
|
771
|
711
|
2367
|
712 This is like pdump_register_block_contents() but also registers
|
771
|
713 the memory block itself. */
|
|
714
|
442
|
715 static void
|
2367
|
716 pdump_register_block (const void *data,
|
|
717 Bytecount size,
|
|
718 const struct memory_description *desc,
|
|
719 int count)
|
442
|
720 {
|
2367
|
721 if (data && !pdump_get_block (data))
|
442
|
722 {
|
2367
|
723 pdump_add_block (pdump_get_block_list (desc), data,
|
|
724 lispdesc_block_size_1 (data, size, desc), count);
|
|
725 pdump_register_block_contents (data, size, desc, count);
|
442
|
726 }
|
|
727 }
|
|
728
|
1204
|
729 /* Store the already-calculated new pointer offsets for all pointers in the
|
|
730 COUNT contiguous blocks of memory, each described by DESC and of size
|
|
731 SIZE, whose original is located at ORIG_DATA and the modifiable copy at
|
|
732 DATA. We examine the description to figure out where the pointers are,
|
2367
|
733 and then look up the replacement values using pdump_get_block().
|
771
|
734
|
1204
|
735 This is done just before writing the modified block of memory to the
|
|
736 dump file. The new pointer offsets have been carefully calculated so
|
|
737 that the data being pointed gets written at that offset in the dump
|
|
738 file. That way, the dump file is a correct memory image except perhaps
|
|
739 for a constant that needs to be added to all pointers. (#### In fact, we
|
|
740 SHOULD be starting up a dumped XEmacs, seeing where the dumped file gets
|
|
741 loaded into memory, and then rewriting the dumped file after relocating
|
|
742 all the pointers relative to this memory location. That way, if the
|
|
743 file gets loaded again at the same location, which will be common, we
|
|
744 don't have to do any relocating, which is both faster at startup and
|
771
|
745 allows the read-only part of the dumped data to be shared read-only
|
|
746 between different invocations of XEmacs.)
|
|
747
|
|
748 #### Do we distinguish between read-only and writable dumped data?
|
|
749 Should we? It's tricky because the dumped data, once loaded again,
|
1204
|
750 cannot really be free()d or garbage collected since it's all stored in
|
|
751 one contiguous block of data with no malloc() headers, and we don't keep
|
|
752 track of the pointers used internally in malloc() and the Lisp allocator
|
|
753 to track allocated blocks of memory. */
|
771
|
754
|
|
755 static void
|
|
756 pdump_store_new_pointer_offsets (int count, void *data, const void *orig_data,
|
1204
|
757 const struct memory_description *desc,
|
771
|
758 int size)
|
|
759 {
|
|
760 int pos, i;
|
|
761 /* Process each block one by one */
|
|
762 for (i = 0; i < count; i++)
|
|
763 {
|
|
764 /* CUR points to the beginning of each block in the new data. */
|
2367
|
765 Rawbyte *cur = ((Rawbyte *)data) + i * size;
|
771
|
766 /* Scan each line of the description for relocatable pointers */
|
|
767 for (pos = 0; desc[pos].type != XD_END; pos++)
|
|
768 {
|
|
769 /* RDATA points to the beginning of each element in the new data. */
|
1204
|
770 const struct memory_description *desc1 = &desc[pos];
|
|
771 /* #### Change ORIG_DATA to DATA. See below. */
|
|
772 void *rdata = cur + lispdesc_indirect_count (desc1->offset, desc,
|
|
773 orig_data);
|
|
774 union_switcheroo:
|
|
775
|
|
776 /* If the flag says don't dump, then don't dump. */
|
|
777 if ((desc1->flags) & XD_FLAG_NO_PDUMP)
|
|
778 continue;
|
|
779
|
|
780 switch (desc1->type)
|
771
|
781 {
|
|
782 case XD_BYTECOUNT:
|
|
783 case XD_ELEMCOUNT:
|
|
784 case XD_HASHCODE:
|
|
785 case XD_INT:
|
|
786 case XD_LONG:
|
|
787 break;
|
|
788 case XD_INT_RESET:
|
|
789 {
|
1204
|
790 EMACS_INT val = lispdesc_indirect_count (desc1->data1, desc,
|
|
791 orig_data);
|
771
|
792 * (int *) rdata = val;
|
|
793 break;
|
|
794 }
|
|
795 case XD_OPAQUE_DATA_PTR:
|
2367
|
796 case XD_ASCII_STRING:
|
|
797 case XD_BLOCK_PTR:
|
771
|
798 {
|
|
799 void *ptr = * (void **) rdata;
|
|
800 if (ptr)
|
2367
|
801 * (EMACS_INT *) rdata = pdump_get_block (ptr)->save_offset;
|
771
|
802 break;
|
|
803 }
|
|
804 case XD_LO_LINK:
|
|
805 {
|
|
806 /* As described in lrecord.h, this is a weak link.
|
|
807 Thus, we need to link this object not (necessarily)
|
|
808 to the object directly pointed to, but to the next
|
|
809 referenced object in the chain. None of the
|
|
810 intermediate objects will be written out, so we
|
|
811 traverse down the chain of objects until we find a
|
|
812 referenced one. (The Qnil or Qunbound that ends the
|
|
813 chain will always be a referenced object.) */
|
|
814 Lisp_Object obj = * (Lisp_Object *) rdata;
|
2367
|
815 pdump_block_list_elt *elt1;
|
1204
|
816 /* #### Figure out how to handle indirect offsets here.
|
|
817 #### In general, when computing indirect counts, do we
|
|
818 really need to use the orig_data pointer? Why not just
|
|
819 use the new stuff?
|
|
820
|
|
821 No, we don't usually need orig_data. We only need it
|
|
822 when fetching pointers out of the data, not integers.
|
|
823 This currently occurs only with description maps. We
|
|
824 should change the other places to DATA to emphasize
|
|
825 this. */
|
|
826 assert (!XD_IS_INDIRECT (desc1->offset));
|
771
|
827 for (;;)
|
|
828 {
|
2367
|
829 elt1 = pdump_get_block (XRECORD_LHEADER (obj));
|
771
|
830 if (elt1)
|
|
831 break;
|
1204
|
832 obj = * (Lisp_Object *) (desc1->offset +
|
2367
|
833 (Rawbyte *)
|
|
834 (XRECORD_LHEADER (obj)));
|
771
|
835 }
|
|
836 * (EMACS_INT *) rdata = elt1->save_offset;
|
|
837 break;
|
|
838 }
|
|
839 case XD_LISP_OBJECT:
|
|
840 {
|
|
841 Lisp_Object *pobj = (Lisp_Object *) rdata;
|
|
842
|
1204
|
843 assert (desc1->data1 == 0);
|
771
|
844
|
|
845 if (POINTER_TYPE_P (XTYPE (*pobj)) && XRECORD_LHEADER (*pobj))
|
|
846 * (EMACS_INT *) pobj =
|
2367
|
847 pdump_get_block (XRECORD_LHEADER (*pobj))->save_offset;
|
771
|
848 break;
|
|
849 }
|
|
850 case XD_LISP_OBJECT_ARRAY:
|
|
851 {
|
1204
|
852 EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc,
|
|
853 orig_data);
|
771
|
854 int j;
|
|
855
|
|
856 for (j = 0; j < num; j++)
|
|
857 {
|
|
858 Lisp_Object *pobj = ((Lisp_Object *) rdata) + j;
|
|
859 if (POINTER_TYPE_P (XTYPE (*pobj)) &&
|
|
860 XRECORD_LHEADER (*pobj))
|
|
861 * (EMACS_INT *) pobj =
|
2367
|
862 pdump_get_block (XRECORD_LHEADER (*pobj))->save_offset;
|
771
|
863 }
|
|
864 break;
|
|
865 }
|
|
866 case XD_DOC_STRING:
|
|
867 {
|
|
868 EMACS_INT str = *(EMACS_INT *)rdata;
|
|
869 if (str > 0)
|
|
870 * (EMACS_INT *) rdata =
|
2367
|
871 pdump_get_block ((void *)str)->save_offset;
|
771
|
872 break;
|
|
873 }
|
2367
|
874 case XD_BLOCK_ARRAY:
|
771
|
875 {
|
1204
|
876 EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc,
|
|
877 orig_data);
|
|
878 const struct sized_memory_description *sdesc =
|
|
879 lispdesc_indirect_description (orig_data, desc1->data2);
|
771
|
880
|
|
881 pdump_store_new_pointer_offsets
|
|
882 (num, rdata,
|
2367
|
883 ((Rawbyte *) rdata - (Rawbyte *) data) +
|
|
884 (Rawbyte *) orig_data,
|
1204
|
885 sdesc->description,
|
2367
|
886 lispdesc_block_size
|
|
887 (((Rawbyte *) rdata - (Rawbyte *) data) +
|
|
888 (Rawbyte *) orig_data, sdesc));
|
771
|
889 break;
|
|
890 }
|
|
891 case XD_UNION:
|
1204
|
892 case XD_UNION_DYNAMIC_SIZE:
|
|
893 desc1 = lispdesc_process_xd_union (desc1, desc, orig_data);
|
|
894 if (desc1)
|
|
895 goto union_switcheroo;
|
|
896 break;
|
771
|
897
|
|
898 default:
|
1333
|
899 pdump_unsupported_dump_type (desc1->type, 0);
|
771
|
900 }
|
|
901 }
|
|
902 }
|
|
903 }
|
|
904
|
|
905 /* Write out to global file descriptor PDUMP_OUT the element (one or
|
|
906 more contiguous blocks of identical size/description) recorded in
|
|
907 ELT and described by DESC. The element is first copied to a buffer
|
|
908 and then all pointers (this includes Lisp_Objects other than
|
|
909 integer/character) are relocated to the (pre-computed) offset in
|
|
910 the dump file. */
|
|
911
|
442
|
912 static void
|
2367
|
913 pdump_dump_data (pdump_block_list_elt *elt,
|
1204
|
914 const struct memory_description *desc)
|
442
|
915 {
|
665
|
916 Bytecount size = elt->size;
|
460
|
917 int count = elt->count;
|
442
|
918 if (desc)
|
|
919 {
|
771
|
920 /* Copy to temporary buffer */
|
460
|
921 memcpy (pdump_buf, elt->obj, size*count);
|
442
|
922
|
771
|
923 /* Store new offsets into all pointers in block */
|
|
924 pdump_store_new_pointer_offsets (count, pdump_buf, elt->obj, desc, size);
|
|
925 }
|
|
926 retry_fwrite (desc ? pdump_buf : elt->obj, size, count, pdump_out);
|
|
927 }
|
442
|
928
|
771
|
929 /* Relocate a single memory block at DATA, described by DESC, from its
|
1204
|
930 assumed load location to its actual one by adding DELTA to all pointers
|
|
931 in the block. Does not recursively relocate any other memory blocks
|
|
932 pointed to. (We already have a list of all memory blocks in the dump
|
|
933 file.) This is used once the dump data has been loaded back in, both
|
2367
|
934 for blocks sitting in the dumped data (former heap blocks) and in global
|
|
935 data-sgment blocks whose contents have been restored from the dumped
|
|
936 data. */
|
442
|
937
|
|
938 static void
|
458
|
939 pdump_reloc_one (void *data, EMACS_INT delta,
|
1204
|
940 const struct memory_description *desc)
|
442
|
941 {
|
|
942 int pos;
|
|
943
|
|
944 for (pos = 0; desc[pos].type != XD_END; pos++)
|
|
945 {
|
1204
|
946 const struct memory_description *desc1 = &desc[pos];
|
2367
|
947 void *rdata =
|
|
948 (Rawbyte *) data + lispdesc_indirect_count (desc1->offset,
|
|
949 desc, data);
|
1204
|
950
|
|
951 union_switcheroo:
|
|
952
|
|
953 /* If the flag says don't dump, then don't dump. */
|
|
954 if ((desc1->flags) & XD_FLAG_NO_PDUMP)
|
|
955 continue;
|
|
956
|
|
957 switch (desc1->type)
|
442
|
958 {
|
665
|
959 case XD_BYTECOUNT:
|
|
960 case XD_ELEMCOUNT:
|
|
961 case XD_HASHCODE:
|
442
|
962 case XD_INT:
|
|
963 case XD_LONG:
|
|
964 case XD_INT_RESET:
|
|
965 break;
|
|
966 case XD_OPAQUE_DATA_PTR:
|
2367
|
967 case XD_ASCII_STRING:
|
|
968 case XD_BLOCK_PTR:
|
442
|
969 case XD_LO_LINK:
|
|
970 {
|
|
971 EMACS_INT ptr = *(EMACS_INT *)rdata;
|
|
972 if (ptr)
|
|
973 *(EMACS_INT *)rdata = ptr+delta;
|
|
974 break;
|
|
975 }
|
|
976 case XD_LISP_OBJECT:
|
|
977 {
|
|
978 Lisp_Object *pobj = (Lisp_Object *) rdata;
|
|
979
|
1204
|
980 assert (desc1->data1 == 0);
|
442
|
981
|
|
982 if (POINTER_TYPE_P (XTYPE (*pobj))
|
|
983 && ! EQ (*pobj, Qnull_pointer))
|
2367
|
984 *pobj = wrap_pointer_1 ((Rawbyte *) XPNTR (*pobj) + delta);
|
442
|
985
|
|
986 break;
|
|
987 }
|
|
988 case XD_LISP_OBJECT_ARRAY:
|
|
989 {
|
1204
|
990 EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc,
|
|
991 data);
|
442
|
992 int j;
|
|
993
|
|
994 for (j=0; j<num; j++)
|
|
995 {
|
|
996 Lisp_Object *pobj = (Lisp_Object *) rdata + j;
|
|
997
|
|
998 if (POINTER_TYPE_P (XTYPE (*pobj))
|
|
999 && ! EQ (*pobj, Qnull_pointer))
|
2367
|
1000 *pobj = wrap_pointer_1 ((Rawbyte *) XPNTR (*pobj) +
|
|
1001 delta);
|
442
|
1002 }
|
|
1003 break;
|
|
1004 }
|
|
1005 case XD_DOC_STRING:
|
|
1006 {
|
|
1007 EMACS_INT str = *(EMACS_INT *)rdata;
|
|
1008 if (str > 0)
|
|
1009 *(EMACS_INT *)rdata = str + delta;
|
|
1010 break;
|
|
1011 }
|
2367
|
1012 case XD_BLOCK_ARRAY:
|
771
|
1013 {
|
1204
|
1014 EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc,
|
|
1015 data);
|
771
|
1016 int j;
|
1204
|
1017 const struct sized_memory_description *sdesc =
|
|
1018 lispdesc_indirect_description (data, desc1->data2);
|
2367
|
1019 Bytecount size = lispdesc_block_size (rdata, sdesc);
|
771
|
1020
|
|
1021 /* Note: We are recursing over data in the block itself */
|
|
1022 for (j = 0; j < num; j++)
|
2367
|
1023 pdump_reloc_one ((Rawbyte *) rdata + j * size, delta,
|
771
|
1024 sdesc->description);
|
|
1025
|
|
1026 break;
|
|
1027 }
|
1204
|
1028 case XD_UNION:
|
|
1029 case XD_UNION_DYNAMIC_SIZE:
|
|
1030 desc1 = lispdesc_process_xd_union (desc1, desc, data);
|
|
1031 if (desc1)
|
|
1032 goto union_switcheroo;
|
|
1033 break;
|
771
|
1034
|
442
|
1035 default:
|
1333
|
1036 pdump_unsupported_dump_type (desc1->type, 0);
|
1204
|
1037 }
|
442
|
1038 }
|
|
1039 }
|
|
1040
|
|
1041 static void
|
2367
|
1042 pdump_allocate_offset (pdump_block_list_elt *elt,
|
2286
|
1043 const struct memory_description *UNUSED (desc))
|
442
|
1044 {
|
665
|
1045 Bytecount size = elt->count * elt->size;
|
460
|
1046 elt->save_offset = cur_offset;
|
2367
|
1047 if (size > max_size)
|
442
|
1048 max_size = size;
|
|
1049 cur_offset += size;
|
|
1050 }
|
|
1051
|
2367
|
1052 /* Traverse through all the heap blocks, once the "register" stage of
|
|
1053 dumping has finished. To compress space as much as possible, we
|
|
1054 logically sort all blocks by alignment, hitting all blocks with
|
|
1055 alignment == the maximum (which may be 8 bytes, for doubles), then
|
|
1056 all blocks with the next lower alignment (4 bytes), etc.
|
|
1057
|
|
1058 Within each alignment we hit
|
|
1059
|
|
1060 -- first the Lisp objects, type-by-type
|
|
1061
|
|
1062 -- then the heap memory blocks that are not Lisp objects, description-by-
|
|
1063 description -- i.e. all blocks with the same description will be
|
|
1064 placed together
|
|
1065
|
|
1066 -- then the "opaque" data objects declared as XD_OPAQUE_DATA_PTR,
|
|
1067 XD_ASCII_STRING and XD_DOC_STRING.
|
|
1068
|
|
1069 The idea is to have as little blank space as possible in the laid-out
|
|
1070 data.
|
|
1071
|
|
1072 For each item that we have hit, we process it by calling F, the function
|
|
1073 passed it. In dumper.c, pdump_scan_by_alignment() is called twice with
|
|
1074 two different functions -- pdump_allocate_offset() in stage 2 to compute
|
|
1075 the offset to each block, and pdump_dump_data() in stage 3 to
|
|
1076 successively write each block to disk.
|
|
1077
|
|
1078 It's extremely important that the SAME traversal order gets invoked
|
|
1079 in both stage 2 and 3.
|
|
1080 */
|
|
1081
|
442
|
1082 static void
|
2367
|
1083 pdump_scan_by_alignment (void (*f)(pdump_block_list_elt *,
|
1204
|
1084 const struct memory_description *))
|
442
|
1085 {
|
460
|
1086 int align;
|
|
1087
|
|
1088 for (align = ALIGNOF (max_align_t); align; align>>=1)
|
442
|
1089 {
|
460
|
1090 int i;
|
2367
|
1091 pdump_block_list_elt *elt;
|
460
|
1092
|
442
|
1093 for (i=0; i<lrecord_type_count; i++)
|
|
1094 if (pdump_object_table[i].align == align)
|
460
|
1095 for (elt = pdump_object_table[i].first; elt; elt = elt->next)
|
|
1096 f (elt, lrecord_implementations_table[i]->description);
|
442
|
1097
|
2367
|
1098 for (i=0; i<pdump_desc_table.count; i++)
|
460
|
1099 {
|
2367
|
1100 pdump_desc_list_elt list = pdump_desc_table.list[i];
|
460
|
1101 if (list.list.align == align)
|
|
1102 for (elt = list.list.first; elt; elt = elt->next)
|
1204
|
1103 f (elt, list.desc);
|
460
|
1104 }
|
442
|
1105
|
460
|
1106 for (elt = pdump_opaque_data_list.first; elt; elt = elt->next)
|
|
1107 if (pdump_size_to_align (elt->size) == align)
|
|
1108 f (elt, 0);
|
442
|
1109 }
|
|
1110 }
|
|
1111
|
2367
|
1112 /* Dump out the root block pointers, part of stage 3 (the "WRITE" stage) of
|
|
1113 dumping. For each pointer we dump out a structure containing the
|
|
1114 location of the pointer and its value, replaced by the appropriate
|
|
1115 offset into the dumped data. */
|
|
1116
|
442
|
1117 static void
|
2367
|
1118 pdump_dump_root_block_ptrs (void)
|
442
|
1119 {
|
|
1120 int i;
|
2367
|
1121 Elemcount count = Dynarr_length (pdump_root_block_ptrs);
|
458
|
1122 pdump_static_pointer *data = alloca_array (pdump_static_pointer, count);
|
|
1123 for (i = 0; i < count; i++)
|
442
|
1124 {
|
1333
|
1125 data[i].address =
|
2367
|
1126 (Rawbyte **) Dynarr_atp (pdump_root_block_ptrs, i)->ptraddress;
|
1333
|
1127 data[i].value =
|
2367
|
1128 (Rawbyte *) pdump_get_block (* data[i].address)->save_offset;
|
442
|
1129 }
|
458
|
1130 PDUMP_ALIGN_OUTPUT (pdump_static_pointer);
|
771
|
1131 retry_fwrite (data, sizeof (pdump_static_pointer), count, pdump_out);
|
442
|
1132 }
|
|
1133
|
2367
|
1134 /* Dump out the root blocks, part of stage 3 (the "WRITE" stage) of
|
|
1135 dumping. For each block we dump a structure containing info about the
|
|
1136 block (its location, size and description) and then the block itself,
|
|
1137 with its pointers replaced with offsets into the dump data. */
|
|
1138
|
442
|
1139 static void
|
1204
|
1140 pdump_dump_root_blocks (void)
|
442
|
1141 {
|
|
1142 int i;
|
1204
|
1143 for (i = 0; i < Dynarr_length (pdump_root_blocks); i++)
|
442
|
1144 {
|
2367
|
1145 pdump_root_block info = Dynarr_at (pdump_root_blocks, i);
|
|
1146 PDUMP_WRITE_ALIGNED (pdump_root_block, info);
|
|
1147
|
|
1148 if (info.desc)
|
|
1149 {
|
|
1150 /* Copy to temporary buffer */
|
|
1151 memcpy (pdump_buf, info.blockaddr, info.size);
|
|
1152
|
|
1153 /* Store new offsets into all pointers in block */
|
|
1154 pdump_store_new_pointer_offsets (1, pdump_buf, info.blockaddr,
|
|
1155 info.desc, info.size);
|
|
1156 }
|
|
1157 retry_fwrite (info.desc ? pdump_buf : info.blockaddr,
|
|
1158 info.size, 1, pdump_out);
|
442
|
1159 }
|
|
1160 }
|
|
1161
|
|
1162 static void
|
|
1163 pdump_dump_rtables (void)
|
|
1164 {
|
452
|
1165 int i;
|
2367
|
1166 pdump_block_list_elt *elt;
|
442
|
1167 pdump_reloc_table rt;
|
|
1168
|
|
1169 for (i=0; i<lrecord_type_count; i++)
|
|
1170 {
|
460
|
1171 elt = pdump_object_table[i].first;
|
|
1172 if (!elt)
|
442
|
1173 continue;
|
|
1174 rt.desc = lrecord_implementations_table[i]->description;
|
|
1175 rt.count = pdump_object_table[i].count;
|
458
|
1176 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
460
|
1177 while (elt)
|
442
|
1178 {
|
2367
|
1179 EMACS_INT rdata = pdump_get_block (elt->obj)->save_offset;
|
458
|
1180 PDUMP_WRITE_ALIGNED (EMACS_INT, rdata);
|
460
|
1181 elt = elt->next;
|
442
|
1182 }
|
|
1183 }
|
|
1184
|
|
1185 rt.desc = 0;
|
|
1186 rt.count = 0;
|
458
|
1187 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
442
|
1188
|
2367
|
1189 for (i=0; i<pdump_desc_table.count; i++)
|
442
|
1190 {
|
2367
|
1191 elt = pdump_desc_table.list[i].list.first;
|
|
1192 rt.desc = pdump_desc_table.list[i].desc;
|
|
1193 rt.count = pdump_desc_table.list[i].list.count;
|
458
|
1194 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
460
|
1195 while (elt)
|
442
|
1196 {
|
2367
|
1197 EMACS_INT rdata = pdump_get_block (elt->obj)->save_offset;
|
452
|
1198 int j;
|
460
|
1199 for (j=0; j<elt->count; j++)
|
442
|
1200 {
|
458
|
1201 PDUMP_WRITE_ALIGNED (EMACS_INT, rdata);
|
460
|
1202 rdata += elt->size;
|
442
|
1203 }
|
460
|
1204 elt = elt->next;
|
442
|
1205 }
|
|
1206 }
|
|
1207 rt.desc = 0;
|
|
1208 rt.count = 0;
|
458
|
1209 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
442
|
1210 }
|
|
1211
|
|
1212 static void
|
1204
|
1213 pdump_dump_root_lisp_objects (void)
|
442
|
1214 {
|
1204
|
1215 Elemcount count = (Dynarr_length (pdump_root_lisp_objects) +
|
647
|
1216 Dynarr_length (pdump_weak_object_chains));
|
665
|
1217 Elemcount i;
|
442
|
1218
|
665
|
1219 PDUMP_WRITE_ALIGNED (Elemcount, count);
|
458
|
1220 PDUMP_ALIGN_OUTPUT (pdump_static_Lisp_Object);
|
442
|
1221
|
1204
|
1222 for (i = 0; i < Dynarr_length (pdump_root_lisp_objects); i++)
|
442
|
1223 {
|
458
|
1224 pdump_static_Lisp_Object obj;
|
1204
|
1225 obj.address = Dynarr_at (pdump_root_lisp_objects, i);
|
458
|
1226 obj.value = * obj.address;
|
460
|
1227
|
458
|
1228 if (POINTER_TYPE_P (XTYPE (obj.value)))
|
619
|
1229 obj.value =
|
2367
|
1230 wrap_pointer_1 ((void *) pdump_get_block (XRECORD_LHEADER
|
617
|
1231 (obj.value))->save_offset);
|
460
|
1232
|
458
|
1233 PDUMP_WRITE (pdump_static_Lisp_Object, obj);
|
442
|
1234 }
|
|
1235
|
2367
|
1236 for (i = 0; i < Dynarr_length (pdump_weak_object_chains); i++)
|
442
|
1237 {
|
2367
|
1238 pdump_block_list_elt *elt;
|
458
|
1239 pdump_static_Lisp_Object obj;
|
442
|
1240
|
458
|
1241 obj.address = Dynarr_at (pdump_weak_object_chains, i);
|
|
1242 obj.value = * obj.address;
|
460
|
1243
|
442
|
1244 for (;;)
|
|
1245 {
|
1204
|
1246 const struct memory_description *desc;
|
442
|
1247 int pos;
|
2367
|
1248 elt = pdump_get_block (XRECORD_LHEADER (obj.value));
|
460
|
1249 if (elt)
|
442
|
1250 break;
|
458
|
1251 desc = XRECORD_LHEADER_IMPLEMENTATION (obj.value)->description;
|
442
|
1252 for (pos = 0; desc[pos].type != XD_LO_LINK; pos++)
|
|
1253 assert (desc[pos].type != XD_END);
|
|
1254
|
1204
|
1255 /* #### Figure out how to handle indirect offsets here. */
|
|
1256 assert (!XD_IS_INDIRECT (desc[pos].offset));
|
|
1257 obj.value =
|
|
1258 * (Lisp_Object *) (desc[pos].offset +
|
2367
|
1259 (Rawbyte *) (XRECORD_LHEADER (obj.value)));
|
442
|
1260 }
|
619
|
1261 obj.value = wrap_pointer_1 ((void *) elt->save_offset);
|
442
|
1262
|
458
|
1263 PDUMP_WRITE (pdump_static_Lisp_Object, obj);
|
442
|
1264 }
|
|
1265 }
|
|
1266
|
2367
|
1267
|
|
1268 /*########################################################################
|
|
1269 # Pdump #
|
|
1270 ########################################################################
|
|
1271
|
|
1272 [ben]
|
|
1273
|
|
1274 DISCUSSION OF DUMPING:
|
|
1275
|
|
1276 The idea of dumping is to record the state of XEmacs in a file, so that
|
|
1277 it can be reloaded later. This avoids having to reload all of the basic
|
|
1278 Lisp code each time XEmacs is run, which is a rather time-consuming
|
|
1279 process. (Less so on new machines, but still noticeable. As an example
|
|
1280 of a program with similar issues but which does not have a dumping
|
|
1281 process and as a result has a slow startup time, consider Adobe Photoshop
|
|
1282 5.0 or Adobe Photoshop Elements 2.0.)
|
|
1283
|
|
1284 We don't actually record ALL the state of XEmacs (some of it, for example,
|
|
1285 is dependent on the run-time environment and needs to be initialized
|
|
1286 whenever XEmacs is run), but whatever state we don't record needs to be
|
|
1287 reinitialized every time XEmacs is run.
|
|
1288
|
|
1289 The old way of dumping was to make a new executable file with the data
|
|
1290 segment expanded to contain the heap and written out from memory. This
|
|
1291 is what the unex* files do. Unfortunately this process is extremely
|
|
1292 system-specific and breaks easily with OS changes.
|
|
1293
|
|
1294 Another simple, more portable trick, the "static heap" method, involves
|
|
1295 replacing the allocator with our own allocator which allocates all space
|
|
1296 out of a very large array declared in our data segment until we run out,
|
|
1297 then uses the underlying malloc() to start allocating on the heap. If we
|
|
1298 ensure that the large array is big enough to hold all data allocated
|
|
1299 during the dump stage, then all of the data we need to save is in the
|
|
1300 data segment, and it's easy to calculate the location and size of the
|
|
1301 data segment we want to save (we don't want to record and reinitialize
|
|
1302 the data segment of library functions) by using appropriately declared
|
|
1303 variables in the first and last file linked. This method is known as the
|
|
1304 "static heap" method, and is used by the non-pdump version of the dumper
|
|
1305 under Cygwin, and was also used under VMS and in Win-Emacs.
|
|
1306
|
|
1307 The "static heap" method works well in practice. Nonetheless, a more
|
|
1308 complex method of dumping was written by Olivier Galibert, which requires
|
|
1309 that structural descriptions of all data allocated in the heap be provided
|
|
1310 and the roots of all pointers into the heap be noted through function calls
|
|
1311 to the pdump API. This way, all the heap data can be traversed and written
|
|
1312 out to a file, and then reloaded at run-time and the pointers relocated to
|
|
1313 point at the new location of the loaded data. This is the "pdump" method
|
|
1314 used in this file.
|
|
1315
|
|
1316 There are two potential advantages of "pdump" over the "static heap":
|
|
1317
|
|
1318 (1) It doesn't require any tricks to calculate the beginning and end of
|
|
1319 the data segment, or even that the XEmacs section of the data segment
|
|
1320 be contiguous. (It's not clear whether this is an issue in practice.)
|
|
1321 (2) Potentially, it could handle an OS that does not always load the
|
|
1322 static data segment at a predictable location. The "static heap"
|
|
1323 method by its nature needs the data segment to stay in the same place
|
|
1324 from invocation to invocation, since it simply dumps out memory and
|
|
1325 reloads it, without any pointer relocation. I say "potentially"
|
|
1326 because as it is currently written pdump does assume that the data
|
|
1327 segment is never relocated. However, changing pdump to remove this
|
|
1328 assumption is probably not difficult, as all the mechanism to handle
|
|
1329 pointer relocation is already present.
|
|
1330
|
|
1331
|
|
1332 DISCUSSION OF PDUMP WORKINGS:
|
|
1333
|
|
1334 See man/internals/internals.texi for more information.
|
|
1335
|
|
1336 NOTE that we have two kinds of memory to handle: memory on the heap
|
|
1337 (i.e. allocated through malloc()) or the like, and static memory in the
|
|
1338 data segment of the program, i.e. stuff declared as global or static.
|
|
1339 All heap memory needs to be written out to the dump file and reproduced
|
|
1340 (i.e. reloaded and any necessary relocations performed). Data-segment
|
|
1341 memory that is not statically initialized (i.e. through declarations in
|
|
1342 the C code) needs either to be written out and reloaded, or
|
|
1343 reinitialized. In addition, any pointers in data-segment memory to heap
|
|
1344 memory must be written out, reloaded and relocated.
|
|
1345
|
|
1346 NOTE that we currently don't handle relocation of pointers into data-
|
|
1347 segment memory. (See overview discussion above.) These are treated in
|
|
1348 the descriptions as opaque data not needing relocation. If this becomes a
|
|
1349 problem, it can be fixed through new kinds of types in
|
|
1350 enum memory_description_type.
|
|
1351
|
|
1352 Three basic steps to dumping out:
|
|
1353
|
|
1354 (1) "REGISTER":
|
|
1355 Starting with all sources of relocatable memory (currently this means
|
|
1356 all data-segment pointers to heap memory -- see above about pointers
|
|
1357 to data-segment memory), recursively traverse the tree of pointers
|
|
1358 and "register" (make a note of) every memory block seen.
|
|
1359
|
|
1360 (2) "LAYOUT":
|
|
1361 Go through all of the registered blocks and compute the location of
|
|
1362 each one in the dump data (i.e. the "offset" that will be added to
|
|
1363 the address corresponding to start of the loaded-in data to get the
|
|
1364 new pointer referring to this block). The blocks will be laid out
|
|
1365 sequentially according to the order we traverse them. Also note the
|
|
1366 maximum-sized block for use in step 3.
|
|
1367
|
|
1368 (3) "WRITE":
|
|
1369 After writing some header stuff, go through all of the registered
|
|
1370 blocks and write out each one to the dump file. Note that we are
|
|
1371 simply writing out the blocks sequentially as we see them, and our
|
|
1372 traversal path is identical to that in step 2, so blocks will end up
|
|
1373 at the locations computed for them. In order to write out a block,
|
|
1374 first copy it to a temporary location (hence the maximum-block-size
|
|
1375 computation in the previous step), then for each relocatable pointer
|
|
1376 in the block, write in its place the offset to the heap block in the
|
|
1377 dump data. When the dump data is loaded, the address of the
|
|
1378 beginning of the dump data will be added to the offset in each
|
|
1379 pointer, and thence become accurate.
|
|
1380
|
|
1381 --ben
|
|
1382 */
|
|
1383
|
442
|
1384 void
|
|
1385 pdump (void)
|
|
1386 {
|
|
1387 int i;
|
|
1388 Lisp_Object t_console, t_device, t_frame;
|
|
1389 int none;
|
458
|
1390 pdump_header header;
|
442
|
1391
|
1204
|
1392 in_pdump = 1;
|
|
1393
|
2367
|
1394 pdump_object_table = xnew_array (pdump_block_list, lrecord_type_count);
|
460
|
1395 pdump_alert_undump_object = xnew_array (int, lrecord_type_count);
|
|
1396
|
|
1397 assert (ALIGNOF (max_align_t) <= pdump_align_table[0]);
|
|
1398
|
|
1399 for (i = 0; i < countof (pdump_align_table); i++)
|
|
1400 if (pdump_align_table[i] > ALIGNOF (max_align_t))
|
|
1401 pdump_align_table[i] = ALIGNOF (max_align_t);
|
|
1402
|
446
|
1403 flush_all_buffer_local_cache ();
|
|
1404
|
442
|
1405 /* These appear in a DEFVAR_LISP, which does a staticpro() */
|
452
|
1406 t_console = Vterminal_console; Vterminal_console = Qnil;
|
|
1407 t_frame = Vterminal_frame; Vterminal_frame = Qnil;
|
|
1408 t_device = Vterminal_device; Vterminal_device = Qnil;
|
442
|
1409
|
452
|
1410 dump_add_opaque (&lrecord_implementations_table,
|
1204
|
1411 lrecord_type_count *
|
|
1412 sizeof (lrecord_implementations_table[0]));
|
1676
|
1413 #ifdef USE_KKCC
|
|
1414 dump_add_opaque (&lrecord_memory_descriptions,
|
|
1415 lrecord_type_count
|
|
1416 * sizeof (lrecord_memory_descriptions[0]));
|
|
1417 #else /* not USE_KKCC */
|
452
|
1418 dump_add_opaque (&lrecord_markers,
|
|
1419 lrecord_type_count * sizeof (lrecord_markers[0]));
|
1676
|
1420 #endif /* not USE_KKCC */
|
442
|
1421
|
2367
|
1422 pdump_hash = xnew_array_and_zero (pdump_block_list_elt *, PDUMP_HASHSIZE);
|
442
|
1423
|
2367
|
1424 for (i = 0; i<lrecord_type_count; i++)
|
442
|
1425 {
|
|
1426 pdump_object_table[i].first = 0;
|
460
|
1427 pdump_object_table[i].align = ALIGNOF (max_align_t);
|
442
|
1428 pdump_object_table[i].count = 0;
|
|
1429 pdump_alert_undump_object[i] = 0;
|
|
1430 }
|
2367
|
1431 pdump_desc_table.count = 0;
|
|
1432 pdump_desc_table.size = -1;
|
442
|
1433
|
|
1434 pdump_opaque_data_list.first = 0;
|
460
|
1435 pdump_opaque_data_list.align = ALIGNOF (max_align_t);
|
442
|
1436 pdump_opaque_data_list.count = 0;
|
1204
|
1437 pdump_depth = 0;
|
442
|
1438
|
2367
|
1439 /* (I) The "register" stage: Note all heap memory blocks to be relocated
|
|
1440 */
|
|
1441
|
|
1442 /* Try various roots of accessibility: */
|
|
1443
|
|
1444 /* (1) Lisp objects, both those declared using DEFVAR_LISP*() and those
|
|
1445 staticpro()d. */
|
1204
|
1446 for (i = 0; i < Dynarr_length (pdump_root_lisp_objects); i++)
|
|
1447 pdump_register_object (* Dynarr_at (pdump_root_lisp_objects, i));
|
442
|
1448
|
|
1449 none = 1;
|
2367
|
1450 for (i = 0; i < lrecord_type_count; i++)
|
442
|
1451 if (pdump_alert_undump_object[i])
|
|
1452 {
|
|
1453 if (none)
|
2367
|
1454 stderr_out ("Undumpable types list :\n");
|
442
|
1455 none = 0;
|
2367
|
1456 stderr_out (" - %s (%d)\n", lrecord_implementations_table[i]->name,
|
|
1457 pdump_alert_undump_object[i]);
|
442
|
1458 }
|
|
1459 if (!none)
|
1204
|
1460 {
|
|
1461 in_pdump = 0;
|
|
1462 return;
|
|
1463 }
|
442
|
1464
|
2367
|
1465 /* (2) Register out the data-segment pointer variables to heap blocks */
|
|
1466 for (i = 0; i < Dynarr_length (pdump_root_block_ptrs); i++)
|
452
|
1467 {
|
2367
|
1468 pdump_root_block_ptr info = Dynarr_at (pdump_root_block_ptrs, i);
|
|
1469 pdump_register_block (*(info.ptraddress), info.desc->size,
|
|
1470 info.desc->description, 1);
|
452
|
1471 }
|
442
|
1472
|
2367
|
1473 /* (3) Register out the data-segment blocks, maybe with pointers to heap
|
|
1474 blocks */
|
|
1475 for (i = 0; i < Dynarr_length (pdump_root_blocks); i++)
|
|
1476 {
|
|
1477 pdump_root_block *info = Dynarr_atp (pdump_root_blocks, i);
|
|
1478 if (info->desc)
|
|
1479 {
|
|
1480 /* Size may have been given as 0 meaning "compute later".
|
|
1481 Compute now and update. If no DESC, size must always be
|
|
1482 correct as there is no other way of computing it. */
|
|
1483 info->size = lispdesc_block_size_1 (info->blockaddr, info->size,
|
|
1484 info->desc);
|
|
1485 pdump_register_block_contents (info->blockaddr, info->size,
|
|
1486 info->desc, 1);
|
|
1487 }
|
|
1488 }
|
|
1489
|
|
1490 /* (II) The "layout" stage: Compute the offsets and max-size */
|
|
1491
|
|
1492 /* (1) Determine header size */
|
458
|
1493 memcpy (header.signature, PDUMP_SIGNATURE, PDUMP_SIGNATURE_LEN);
|
|
1494 header.id = dump_id;
|
|
1495 header.reloc_address = 0;
|
2367
|
1496 header.nb_root_block_ptrs = Dynarr_length (pdump_root_block_ptrs);
|
1204
|
1497 header.nb_root_blocks = Dynarr_length (pdump_root_blocks);
|
442
|
1498
|
826
|
1499 cur_offset = MAX_ALIGN_SIZE (sizeof (header));
|
442
|
1500 max_size = 0;
|
|
1501
|
2367
|
1502 /* (2) Traverse all heap blocks and compute their offsets; keep track
|
|
1503 of maximum block size seen */
|
442
|
1504 pdump_scan_by_alignment (pdump_allocate_offset);
|
826
|
1505 cur_offset = MAX_ALIGN_SIZE (cur_offset);
|
458
|
1506 header.stab_offset = cur_offset;
|
442
|
1507
|
2367
|
1508 /* (3) Update maximum size based on root (data-segment) blocks */
|
|
1509 for (i = 0; i < Dynarr_length (pdump_root_blocks); i++)
|
|
1510 {
|
|
1511 pdump_root_block info = Dynarr_at (pdump_root_blocks, i);
|
|
1512
|
|
1513 /* If no DESC, no relocation needed and we copy directly instead of
|
|
1514 into a temp buffer. */
|
|
1515 if (info.desc)
|
|
1516 {
|
|
1517 if (info.size > max_size)
|
|
1518 max_size = info.size;
|
|
1519 }
|
|
1520 }
|
|
1521
|
|
1522 /* (III) The "write "stage: Dump out the data, storing the offsets in
|
|
1523 place of pointers whenever we write out memory blocks */
|
|
1524
|
442
|
1525 pdump_buf = xmalloc (max_size);
|
2367
|
1526 /* EMACS_PROGNAME is entirely ASCII so this should be Mule-safe */
|
442
|
1527 pdump_fd = open (EMACS_PROGNAME ".dmp",
|
|
1528 O_WRONLY | O_CREAT | O_TRUNC | OPEN_BINARY, 0666);
|
771
|
1529 if (pdump_fd < 0)
|
|
1530 report_file_error ("Unable to open dump file",
|
|
1531 build_string (EMACS_PROGNAME ".dmp"));
|
458
|
1532 pdump_out = fdopen (pdump_fd, "w");
|
771
|
1533 if (pdump_out < 0)
|
|
1534 report_file_error ("Unable to open dump file for writing",
|
|
1535 build_string (EMACS_PROGNAME ".dmp"));
|
442
|
1536
|
771
|
1537 retry_fwrite (&header, sizeof (header), 1, pdump_out);
|
458
|
1538 PDUMP_ALIGN_OUTPUT (max_align_t);
|
442
|
1539
|
|
1540 pdump_scan_by_alignment (pdump_dump_data);
|
|
1541
|
458
|
1542 fseek (pdump_out, header.stab_offset, SEEK_SET);
|
442
|
1543
|
2367
|
1544 pdump_dump_root_block_ptrs ();
|
1204
|
1545 pdump_dump_root_blocks ();
|
442
|
1546 pdump_dump_rtables ();
|
1204
|
1547 pdump_dump_root_lisp_objects ();
|
442
|
1548
|
771
|
1549 retry_fclose (pdump_out);
|
|
1550 retry_close (pdump_fd);
|
458
|
1551
|
442
|
1552 free (pdump_buf);
|
|
1553
|
|
1554 free (pdump_hash);
|
|
1555
|
|
1556 Vterminal_console = t_console;
|
|
1557 Vterminal_frame = t_frame;
|
|
1558 Vterminal_device = t_device;
|
1204
|
1559 in_pdump = 0;
|
442
|
1560 }
|
|
1561
|
452
|
1562 static int
|
|
1563 pdump_load_check (void)
|
442
|
1564 {
|
2367
|
1565 return (!memcmp (((pdump_header *) pdump_start)->signature,
|
452
|
1566 PDUMP_SIGNATURE, PDUMP_SIGNATURE_LEN)
|
|
1567 && ((pdump_header *)pdump_start)->id == dump_id);
|
442
|
1568 }
|
|
1569
|
458
|
1570 /*----------------------------------------------------------------------*/
|
|
1571 /* Reading the dump file */
|
|
1572 /*----------------------------------------------------------------------*/
|
452
|
1573 static int
|
|
1574 pdump_load_finish (void)
|
442
|
1575 {
|
|
1576 int i;
|
2367
|
1577 Rawbyte *p;
|
442
|
1578 EMACS_INT delta;
|
|
1579 EMACS_INT count;
|
1204
|
1580 pdump_header *header = (pdump_header *) pdump_start;
|
442
|
1581
|
|
1582 pdump_end = pdump_start + pdump_length;
|
|
1583
|
1204
|
1584 delta = ((EMACS_INT) pdump_start) - header->reloc_address;
|
458
|
1585 p = pdump_start + header->stab_offset;
|
442
|
1586
|
2367
|
1587 /* Put back the pdump_root_block_ptrs */
|
|
1588 p = (Rawbyte *) ALIGN_PTR (p, pdump_static_pointer);
|
|
1589 for (i = 0; i < header->nb_root_block_ptrs; i++)
|
442
|
1590 {
|
458
|
1591 pdump_static_pointer ptr = PDUMP_READ (p, pdump_static_pointer);
|
|
1592 (* ptr.address) = ptr.value + delta;
|
442
|
1593 }
|
|
1594
|
1204
|
1595 /* Put back the pdump_root_blocks and relocate */
|
|
1596 for (i = 0; i < header->nb_root_blocks; i++)
|
442
|
1597 {
|
1204
|
1598 pdump_root_block info = PDUMP_READ_ALIGNED (p, pdump_root_block);
|
2367
|
1599 memcpy ((void *) info.blockaddr, p, info.size);
|
1204
|
1600 if (info.desc)
|
2367
|
1601 pdump_reloc_one ((void *) info.blockaddr, delta, info.desc);
|
452
|
1602 p += info.size;
|
442
|
1603 }
|
|
1604
|
1204
|
1605 /* Relocate the heap objects */
|
442
|
1606 pdump_rt_list = p;
|
|
1607 count = 2;
|
|
1608 for (;;)
|
|
1609 {
|
458
|
1610 pdump_reloc_table rt = PDUMP_READ_ALIGNED (p, pdump_reloc_table);
|
2367
|
1611 p = (Rawbyte *) ALIGN_PTR (p, Rawbyte *);
|
442
|
1612 if (rt.desc)
|
|
1613 {
|
2367
|
1614 Rawbyte **reloc = (Rawbyte **) p;
|
1204
|
1615 for (i = 0; i < rt.count; i++)
|
442
|
1616 {
|
458
|
1617 reloc[i] += delta;
|
|
1618 pdump_reloc_one (reloc[i], delta, rt.desc);
|
442
|
1619 }
|
2367
|
1620 p += rt.count * sizeof (Rawbyte *);
|
1204
|
1621 }
|
|
1622 else if (!(--count))
|
|
1623 break;
|
442
|
1624 }
|
|
1625
|
1204
|
1626 /* Put the pdump_root_lisp_objects variables in place */
|
665
|
1627 i = PDUMP_READ_ALIGNED (p, Elemcount);
|
2367
|
1628 p = (Rawbyte *) ALIGN_PTR (p, pdump_static_Lisp_Object);
|
458
|
1629 while (i--)
|
442
|
1630 {
|
458
|
1631 pdump_static_Lisp_Object obj = PDUMP_READ (p, pdump_static_Lisp_Object);
|
442
|
1632
|
458
|
1633 if (POINTER_TYPE_P (XTYPE (obj.value)))
|
2367
|
1634 obj.value = wrap_pointer_1 ((Rawbyte *) XPNTR (obj.value) + delta);
|
442
|
1635
|
458
|
1636 (* obj.address) = obj.value;
|
442
|
1637 }
|
|
1638
|
|
1639 /* Final cleanups */
|
|
1640 /* reorganize hash tables */
|
|
1641 p = pdump_rt_list;
|
|
1642 for (;;)
|
|
1643 {
|
458
|
1644 pdump_reloc_table rt = PDUMP_READ_ALIGNED (p, pdump_reloc_table);
|
2367
|
1645 p = (Rawbyte *) ALIGN_PTR (p, Lisp_Object);
|
442
|
1646 if (!rt.desc)
|
|
1647 break;
|
|
1648 if (rt.desc == hash_table_description)
|
|
1649 {
|
1204
|
1650 for (i = 0; i < rt.count; i++)
|
442
|
1651 pdump_reorganize_hash_table (PDUMP_READ (p, Lisp_Object));
|
|
1652 break;
|
1204
|
1653 }
|
|
1654 else
|
|
1655 p += sizeof (Lisp_Object) * rt.count;
|
442
|
1656 }
|
|
1657
|
|
1658 return 1;
|
|
1659 }
|
|
1660
|
|
1661 #ifdef WIN32_NATIVE
|
|
1662 /* Free the mapped file if we decide we don't want it after all */
|
452
|
1663 static void
|
|
1664 pdump_file_unmap (void)
|
442
|
1665 {
|
|
1666 UnmapViewOfFile (pdump_start);
|
|
1667 CloseHandle (pdump_hFile);
|
|
1668 CloseHandle (pdump_hMap);
|
|
1669 }
|
|
1670
|
452
|
1671 static int
|
2367
|
1672 pdump_file_get (const Wexttext *wpath)
|
442
|
1673 {
|
2367
|
1674 Extbyte *path;
|
|
1675 if (XEUNICODE_P)
|
|
1676 path = (Extbyte *) wpath;
|
|
1677 else
|
|
1678 path = WEXTTEXT_TO_MULTIBYTE (wpath);
|
442
|
1679
|
2367
|
1680 pdump_hFile =
|
|
1681 qxeCreateFile (path,
|
|
1682 GENERIC_READ + GENERIC_WRITE, /* Required for copy on
|
|
1683 write */
|
|
1684 0, /* Not shared */
|
|
1685 NULL, /* Not inheritable */
|
|
1686 OPEN_EXISTING,
|
|
1687 FILE_ATTRIBUTE_NORMAL,
|
|
1688 NULL); /* No template file */
|
442
|
1689 if (pdump_hFile == INVALID_HANDLE_VALUE)
|
|
1690 return 0;
|
|
1691
|
|
1692 pdump_length = GetFileSize (pdump_hFile, NULL);
|
2367
|
1693 pdump_hMap =
|
|
1694 qxeCreateFileMapping (pdump_hFile,
|
|
1695 NULL, /* No security attributes */
|
|
1696 PAGE_WRITECOPY, /* Copy on write */
|
|
1697 0, /* Max size, high half */
|
|
1698 0, /* Max size, low half */
|
|
1699 NULL); /* Unnamed */
|
442
|
1700 if (pdump_hMap == INVALID_HANDLE_VALUE)
|
|
1701 return 0;
|
|
1702
|
2367
|
1703 pdump_start =
|
|
1704 (Rawbyte *) MapViewOfFile (pdump_hMap,
|
|
1705 FILE_MAP_COPY, /* Copy on write */
|
|
1706 0, /* Start at zero */
|
|
1707 0,
|
|
1708 0); /* Map all of it */
|
442
|
1709 pdump_free = pdump_file_unmap;
|
|
1710 return 1;
|
|
1711 }
|
|
1712
|
|
1713 /* pdump_resource_free is called (via the pdump_free pointer) to release
|
|
1714 any resources allocated by pdump_resource_get. Since the Windows API
|
|
1715 specs specifically state that you don't need to (and shouldn't) free the
|
|
1716 resources allocated by FindResource, LoadResource, and LockResource this
|
|
1717 routine does nothing. */
|
452
|
1718 static void
|
|
1719 pdump_resource_free (void)
|
442
|
1720 {
|
|
1721 }
|
|
1722
|
452
|
1723 static int
|
|
1724 pdump_resource_get (void)
|
442
|
1725 {
|
452
|
1726 HRSRC hRes; /* Handle to dump resource */
|
|
1727 HRSRC hResLoad; /* Handle to loaded dump resource */
|
442
|
1728
|
|
1729 /* See Q126630 which describes how Windows NT and 95 trap writes to
|
|
1730 resource sections and duplicate the page to allow the write to proceed.
|
|
1731 It also describes how to make the resource section read/write (and hence
|
|
1732 private to each process). Doing this avoids the exceptions and related
|
|
1733 overhead, but causes the resource section to be private to each process
|
|
1734 that is running XEmacs. Since the resource section contains little
|
|
1735 other than the dumped data, which should be private to each process, we
|
|
1736 make the whole resource section read/write so we don't have to copy it. */
|
|
1737
|
800
|
1738 hRes = FindResourceA (NULL, MAKEINTRESOURCE (101), "DUMP");
|
442
|
1739 if (hRes == NULL)
|
|
1740 return 0;
|
|
1741
|
|
1742 /* Found it, use the data in the resource */
|
1204
|
1743 hResLoad = (HRSRC) LoadResource (NULL, hRes);
|
442
|
1744 if (hResLoad == NULL)
|
|
1745 return 0;
|
|
1746
|
2367
|
1747 pdump_start = (Rawbyte *) LockResource (hResLoad);
|
442
|
1748 if (pdump_start == NULL)
|
|
1749 return 0;
|
|
1750
|
|
1751 pdump_free = pdump_resource_free;
|
|
1752 pdump_length = SizeofResource (NULL, hRes);
|
665
|
1753 if (pdump_length <= (Bytecount) sizeof (pdump_header))
|
442
|
1754 {
|
|
1755 pdump_start = 0;
|
|
1756 return 0;
|
|
1757 }
|
|
1758
|
|
1759 return 1;
|
|
1760 }
|
|
1761
|
|
1762 #else /* !WIN32_NATIVE */
|
|
1763
|
452
|
1764 static void
|
|
1765 pdump_file_free (void)
|
442
|
1766 {
|
2367
|
1767 xfree (pdump_start, Rawbyte *);
|
442
|
1768 }
|
|
1769
|
|
1770 #ifdef HAVE_MMAP
|
452
|
1771 static void
|
|
1772 pdump_file_unmap (void)
|
442
|
1773 {
|
|
1774 munmap (pdump_start, pdump_length);
|
|
1775 }
|
|
1776 #endif
|
|
1777
|
452
|
1778 static int
|
2367
|
1779 pdump_file_get (const Wexttext *path)
|
442
|
1780 {
|
2367
|
1781 int fd = wext_retry_open (path, O_RDONLY | OPEN_BINARY);
|
|
1782 if (fd < 0)
|
442
|
1783 return 0;
|
|
1784
|
|
1785 pdump_length = lseek (fd, 0, SEEK_END);
|
665
|
1786 if (pdump_length < (Bytecount) sizeof (pdump_header))
|
442
|
1787 {
|
771
|
1788 retry_close (fd);
|
442
|
1789 return 0;
|
|
1790 }
|
|
1791
|
|
1792 lseek (fd, 0, SEEK_SET);
|
|
1793
|
|
1794 #ifdef HAVE_MMAP
|
456
|
1795 /* Unix 98 requires that sys/mman.h define MAP_FAILED,
|
|
1796 but many earlier implementations don't. */
|
|
1797 # ifndef MAP_FAILED
|
|
1798 # define MAP_FAILED ((void *) -1L)
|
|
1799 # endif
|
2367
|
1800 pdump_start =
|
|
1801 (Rawbyte *) mmap (0, pdump_length, PROT_READ|PROT_WRITE, MAP_PRIVATE,
|
|
1802 fd, 0);
|
|
1803 if (pdump_start != (Rawbyte *) MAP_FAILED)
|
442
|
1804 {
|
|
1805 pdump_free = pdump_file_unmap;
|
771
|
1806 retry_close (fd);
|
442
|
1807 return 1;
|
|
1808 }
|
456
|
1809 #endif /* HAVE_MMAP */
|
442
|
1810
|
2367
|
1811 pdump_start = xnew_array (Rawbyte, pdump_length);
|
442
|
1812 pdump_free = pdump_file_free;
|
771
|
1813 retry_read (fd, pdump_start, pdump_length);
|
442
|
1814
|
771
|
1815 retry_close (fd);
|
442
|
1816 return 1;
|
|
1817 }
|
2015
|
1818
|
|
1819 static int
|
|
1820 pdump_ram_try (void)
|
|
1821 {
|
2367
|
1822 pdump_start = dumped_data_get ();
|
|
1823 pdump_length = dumped_data_size ();
|
2015
|
1824
|
2367
|
1825 return pdump_load_check ();
|
2015
|
1826 }
|
|
1827
|
442
|
1828 #endif /* !WIN32_NATIVE */
|
|
1829
|
|
1830
|
452
|
1831 static int
|
2367
|
1832 pdump_file_try (Wexttext *exe_path)
|
442
|
1833 {
|
2367
|
1834 Wexttext *w = exe_path + wext_strlen (exe_path);
|
442
|
1835
|
|
1836 do
|
|
1837 {
|
2367
|
1838 wext_sprintf (w, WEXTSTRING ("-%s-%08x.dmp"), WEXTSTRING (EMACS_VERSION),
|
|
1839 dump_id);
|
442
|
1840 if (pdump_file_get (exe_path))
|
|
1841 {
|
|
1842 if (pdump_load_check ())
|
|
1843 return 1;
|
452
|
1844 pdump_free ();
|
442
|
1845 }
|
|
1846
|
2367
|
1847 wext_sprintf (w, WEXTSTRING ("-%08x.dmp"), dump_id);
|
442
|
1848 if (pdump_file_get (exe_path))
|
|
1849 {
|
|
1850 if (pdump_load_check ())
|
|
1851 return 1;
|
452
|
1852 pdump_free ();
|
442
|
1853 }
|
|
1854
|
2367
|
1855 wext_sprintf (w, WEXTSTRING (".dmp"));
|
442
|
1856 if (pdump_file_get (exe_path))
|
|
1857 {
|
|
1858 if (pdump_load_check ())
|
|
1859 return 1;
|
452
|
1860 pdump_free ();
|
442
|
1861 }
|
|
1862
|
|
1863 do
|
|
1864 w--;
|
2367
|
1865 /* !!#### See comment below about how this is unsafe. */
|
|
1866 while (w > exe_path && !IS_DIRECTORY_SEP (*w) && (*w != '-') &&
|
|
1867 (*w != '.'));
|
442
|
1868 }
|
2367
|
1869 while (w > exe_path && !IS_DIRECTORY_SEP (*w));
|
442
|
1870 return 0;
|
|
1871 }
|
|
1872
|
452
|
1873 int
|
2367
|
1874 pdump_load (const Wexttext *argv0)
|
442
|
1875 {
|
2367
|
1876 Wexttext exe_path[PATH_MAX];
|
442
|
1877 #ifdef WIN32_NATIVE
|
2367
|
1878 qxeGetModuleFileName (NULL, (Extbyte *) exe_path, PATH_MAX);
|
|
1879 if (!XEUNICODE_P)
|
|
1880 {
|
|
1881 Wexttext *wexe = MULTIBYTE_TO_WEXTTEXT ((Extbyte *) exe_path);
|
|
1882 wext_strcpy (exe_path, wexe);
|
|
1883 }
|
442
|
1884 #else /* !WIN32_NATIVE */
|
2367
|
1885 Wexttext *w;
|
|
1886 const Wexttext *dir, *p;
|
442
|
1887
|
2367
|
1888 if (pdump_ram_try ())
|
|
1889 {
|
|
1890 pdump_load_finish ();
|
|
1891 in_pdump = 0;
|
|
1892 return 1;
|
|
1893 }
|
2015
|
1894
|
1204
|
1895 in_pdump = 1;
|
442
|
1896 dir = argv0;
|
|
1897 if (dir[0] == '-')
|
|
1898 {
|
|
1899 /* XEmacs as a login shell, oh goody! */
|
2367
|
1900 dir = wext_getenv ("SHELL"); /* not egetenv -- not yet initialized and we
|
|
1901 want external-format data */
|
442
|
1902 }
|
|
1903
|
2367
|
1904 p = dir + wext_strlen (dir);
|
|
1905 /* !!#### This is bad as it may fail with certain non-ASCII-compatible
|
|
1906 external formats such as JIS. Maybe we should be using the mb*()
|
|
1907 routines in libc? But can we reliably trust them on all Unix
|
|
1908 platforms? (We can't convert to internal since those conversion
|
|
1909 routines aren't yet initialized) */
|
|
1910 while (p != dir && !IS_ANY_SEP (p[-1]))
|
|
1911 p--;
|
442
|
1912
|
|
1913 if (p != dir)
|
|
1914 {
|
|
1915 /* invocation-name includes a directory component -- presumably it
|
|
1916 is relative to cwd, not $PATH */
|
2367
|
1917 wext_strcpy (exe_path, dir);
|
442
|
1918 }
|
|
1919 else
|
|
1920 {
|
2367
|
1921 const Wexttext *path = wext_getenv ("PATH"); /* not egetenv --
|
|
1922 not yet init. */
|
|
1923 const Wexttext *name = p;
|
442
|
1924 for (;;)
|
|
1925 {
|
|
1926 p = path;
|
|
1927 while (*p && *p != SEPCHAR)
|
|
1928 p++;
|
|
1929 if (p == path)
|
|
1930 {
|
|
1931 exe_path[0] = '.';
|
|
1932 w = exe_path + 1;
|
|
1933 }
|
|
1934 else
|
|
1935 {
|
2367
|
1936 memcpy (exe_path, path, (p - path) * sizeof (Wexttext));
|
442
|
1937 w = exe_path + (p - path);
|
|
1938 }
|
|
1939 if (!IS_DIRECTORY_SEP (w[-1]))
|
2367
|
1940 *w++ = '/';
|
|
1941 wext_strcpy (w, name);
|
1466
|
1942
|
|
1943 {
|
|
1944 struct stat statbuf;
|
2367
|
1945 if (wext_access (exe_path, X_OK) == 0
|
|
1946 && wext_stat (exe_path, &statbuf) == 0
|
1466
|
1947 && ! S_ISDIR (statbuf.st_mode))
|
|
1948 break;
|
|
1949 }
|
|
1950
|
442
|
1951 if (!*p)
|
|
1952 {
|
|
1953 /* Oh well, let's have some kind of default */
|
2367
|
1954 wext_sprintf (exe_path, "./%s", name);
|
442
|
1955 break;
|
|
1956 }
|
|
1957 path = p+1;
|
|
1958 }
|
|
1959 }
|
|
1960 #endif /* WIN32_NATIVE */
|
|
1961
|
|
1962 if (pdump_file_try (exe_path))
|
|
1963 {
|
|
1964 pdump_load_finish ();
|
1204
|
1965 in_pdump = 0;
|
442
|
1966 return 1;
|
|
1967 }
|
|
1968
|
|
1969 #ifdef WIN32_NATIVE
|
|
1970 if (pdump_resource_get ())
|
|
1971 {
|
|
1972 if (pdump_load_check ())
|
|
1973 {
|
|
1974 pdump_load_finish ();
|
1204
|
1975 in_pdump = 0;
|
442
|
1976 return 1;
|
|
1977 }
|
|
1978 pdump_free ();
|
|
1979 }
|
|
1980 #endif
|
|
1981
|
1204
|
1982 in_pdump = 0;
|
442
|
1983 return 0;
|
|
1984 }
|