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