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