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