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