442
|
1 /* Portable data dumper for XEmacs.
|
|
2 Copyright (C) 1999-2000 Olivier Galibert
|
458
|
3 Copyright (C) 2001 Martin Buchholz
|
826
|
4 Copyright (C) 2001, 2002 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
|
800
|
25 /* !!#### Not yet Mule-ized */
|
|
26
|
442
|
27 #include <config.h>
|
|
28 #include "lisp.h"
|
|
29
|
|
30 #include "specifier.h"
|
771
|
31 #include "file-coding.h"
|
442
|
32 #include "elhash.h"
|
|
33 #include "sysfile.h"
|
|
34 #include "console-stream.h"
|
|
35 #include "dumper.h"
|
|
36
|
|
37 #ifdef WIN32_NATIVE
|
771
|
38 #include "syswindows.h"
|
442
|
39 #else
|
|
40 #ifdef HAVE_MMAP
|
|
41 #include <sys/mman.h>
|
|
42 #endif
|
|
43 #endif
|
|
44
|
|
45 typedef struct
|
|
46 {
|
545
|
47 const void *varaddress;
|
665
|
48 Bytecount size;
|
452
|
49 } pdump_opaque;
|
|
50
|
|
51 typedef struct
|
|
52 {
|
|
53 Dynarr_declare (pdump_opaque);
|
|
54 } pdump_opaque_dynarr;
|
|
55
|
|
56 typedef struct
|
|
57 {
|
|
58 void **ptraddress;
|
|
59 const struct struct_description *desc;
|
|
60 } pdump_root_struct_ptr;
|
|
61
|
|
62 typedef struct
|
|
63 {
|
|
64 Dynarr_declare (pdump_root_struct_ptr);
|
|
65 } pdump_root_struct_ptr_dynarr;
|
|
66
|
458
|
67 typedef struct
|
|
68 {
|
|
69 Lisp_Object *address;
|
|
70 Lisp_Object value;
|
|
71 } pdump_static_Lisp_Object;
|
|
72
|
|
73 typedef struct
|
|
74 {
|
|
75 char **address; /* char * for ease of doing relocation */
|
|
76 char * value;
|
|
77 } pdump_static_pointer;
|
|
78
|
452
|
79 static pdump_opaque_dynarr *pdump_opaques;
|
|
80 static pdump_root_struct_ptr_dynarr *pdump_root_struct_ptrs;
|
|
81 static Lisp_Object_ptr_dynarr *pdump_root_objects;
|
|
82 static Lisp_Object_ptr_dynarr *pdump_weak_object_chains;
|
|
83
|
|
84 /* Mark SIZE bytes at non-heap address VARADDRESS for dumping as is,
|
|
85 without any bit-twiddling. */
|
|
86 void
|
665
|
87 dump_add_opaque (const void *varaddress, Bytecount size)
|
452
|
88 {
|
|
89 pdump_opaque info;
|
|
90 info.varaddress = varaddress;
|
|
91 info.size = size;
|
|
92 if (pdump_opaques == NULL)
|
|
93 pdump_opaques = Dynarr_new (pdump_opaque);
|
|
94 Dynarr_add (pdump_opaques, info);
|
|
95 }
|
|
96
|
|
97 /* Mark the struct described by DESC and pointed to by the pointer at
|
|
98 non-heap address VARADDRESS for dumping.
|
|
99 All the objects reachable from this pointer will also be dumped. */
|
|
100 void
|
771
|
101 dump_add_root_struct_ptr (void *ptraddress,
|
|
102 const struct struct_description *desc)
|
452
|
103 {
|
|
104 pdump_root_struct_ptr info;
|
|
105 info.ptraddress = (void **) ptraddress;
|
|
106 info.desc = desc;
|
|
107 if (pdump_root_struct_ptrs == NULL)
|
|
108 pdump_root_struct_ptrs = Dynarr_new (pdump_root_struct_ptr);
|
|
109 Dynarr_add (pdump_root_struct_ptrs, info);
|
|
110 }
|
|
111
|
|
112 /* Mark the Lisp_Object at non-heap address VARADDRESS for dumping.
|
|
113 All the objects reachable from this var will also be dumped. */
|
|
114 void
|
|
115 dump_add_root_object (Lisp_Object *varaddress)
|
|
116 {
|
|
117 if (pdump_root_objects == NULL)
|
|
118 pdump_root_objects = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
|
|
119 Dynarr_add (pdump_root_objects, varaddress);
|
|
120 }
|
|
121
|
|
122 /* Mark the list pointed to by the Lisp_Object at VARADDRESS for dumping. */
|
|
123 void
|
|
124 dump_add_weak_object_chain (Lisp_Object *varaddress)
|
|
125 {
|
|
126 if (pdump_weak_object_chains == NULL)
|
|
127 pdump_weak_object_chains = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
|
|
128 Dynarr_add (pdump_weak_object_chains, varaddress);
|
|
129 }
|
|
130
|
|
131
|
458
|
132 inline static void
|
665
|
133 pdump_align_stream (FILE *stream, Bytecount alignment)
|
458
|
134 {
|
|
135 long offset = ftell (stream);
|
|
136 long adjustment = ALIGN_SIZE (offset, alignment) - offset;
|
|
137 if (adjustment)
|
|
138 fseek (stream, adjustment, SEEK_CUR);
|
|
139 }
|
|
140
|
|
141 #define PDUMP_ALIGN_OUTPUT(type) pdump_align_stream (pdump_out, ALIGNOF (type))
|
|
142
|
|
143 #define PDUMP_WRITE(type, object) \
|
771
|
144 retry_fwrite (&object, sizeof (object), 1, pdump_out);
|
458
|
145
|
|
146 #define PDUMP_WRITE_ALIGNED(type, object) do { \
|
|
147 PDUMP_ALIGN_OUTPUT (type); \
|
|
148 PDUMP_WRITE (type, object); \
|
|
149 } while (0)
|
|
150
|
|
151 #define PDUMP_READ(ptr, type) \
|
|
152 (((type *) (ptr = (char*) (((type *) ptr) + 1)))[-1])
|
|
153
|
|
154 #define PDUMP_READ_ALIGNED(ptr, type) \
|
826
|
155 ((ptr = (char *) ALIGN_PTR (ptr, type)), PDUMP_READ (ptr, type))
|
458
|
156
|
|
157
|
|
158
|
452
|
159 typedef struct
|
|
160 {
|
442
|
161 const struct lrecord_description *desc;
|
|
162 int count;
|
|
163 } pdump_reloc_table;
|
|
164
|
|
165 static char *pdump_rt_list = 0;
|
|
166
|
|
167 void
|
|
168 pdump_objects_unmark (void)
|
|
169 {
|
|
170 int i;
|
|
171 char *p = pdump_rt_list;
|
|
172 if (p)
|
|
173 for (;;)
|
|
174 {
|
|
175 pdump_reloc_table *rt = (pdump_reloc_table *)p;
|
|
176 p += sizeof (pdump_reloc_table);
|
|
177 if (rt->desc)
|
|
178 {
|
|
179 for (i=0; i<rt->count; i++)
|
|
180 {
|
|
181 struct lrecord_header *lh = * (struct lrecord_header **) p;
|
|
182 if (! C_READONLY_RECORD_HEADER_P (lh))
|
|
183 UNMARK_RECORD_HEADER (lh);
|
|
184 p += sizeof (EMACS_INT);
|
|
185 }
|
|
186 } else
|
|
187 break;
|
|
188 }
|
|
189 }
|
|
190
|
|
191
|
|
192 /* The structure of the file
|
458
|
193 0 - header
|
|
194 - dumped objects
|
|
195 stab_offset - nb_root_struct_ptrs*pair(void *, adr)
|
|
196 for pointers to structures
|
|
197 - nb_opaques*pair(void *, size) for raw bits to restore
|
|
198 - relocation table
|
|
199 - root lisp object address/value couples with the count
|
|
200 preceding the list
|
442
|
201 */
|
|
202
|
|
203
|
452
|
204 #define PDUMP_SIGNATURE "XEmacsDP"
|
|
205 #define PDUMP_SIGNATURE_LEN (sizeof (PDUMP_SIGNATURE) - 1)
|
442
|
206
|
|
207 typedef struct
|
|
208 {
|
452
|
209 char signature[PDUMP_SIGNATURE_LEN];
|
442
|
210 unsigned int id;
|
|
211 EMACS_UINT stab_offset;
|
|
212 EMACS_UINT reloc_address;
|
452
|
213 int nb_root_struct_ptrs;
|
|
214 int nb_opaques;
|
|
215 } pdump_header;
|
442
|
216
|
458
|
217 char *pdump_start;
|
|
218 char *pdump_end;
|
665
|
219 static Bytecount pdump_length;
|
442
|
220
|
|
221 #ifdef WIN32_NATIVE
|
452
|
222 /* Handle for the dump file */
|
458
|
223 static HANDLE pdump_hFile = INVALID_HANDLE_VALUE;
|
452
|
224 /* Handle for the file mapping object for the dump file */
|
458
|
225 static HANDLE pdump_hMap = INVALID_HANDLE_VALUE;
|
442
|
226 #endif
|
|
227
|
458
|
228 static void (*pdump_free) (void);
|
442
|
229
|
460
|
230 static unsigned char pdump_align_table[] =
|
442
|
231 {
|
460
|
232 64, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1,
|
|
233 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1,
|
|
234 32, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1,
|
|
235 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1
|
442
|
236 };
|
|
237
|
647
|
238 static inline int
|
665
|
239 pdump_size_to_align (Bytecount size)
|
442
|
240 {
|
460
|
241 return pdump_align_table[size % countof (pdump_align_table)];
|
|
242 }
|
|
243
|
|
244 typedef struct pdump_entry_list_elt
|
|
245 {
|
|
246 struct pdump_entry_list_elt *next;
|
442
|
247 const void *obj;
|
665
|
248 Bytecount size;
|
442
|
249 int count;
|
|
250 EMACS_INT save_offset;
|
460
|
251 } pdump_entry_list_elt;
|
442
|
252
|
|
253 typedef struct
|
|
254 {
|
460
|
255 pdump_entry_list_elt *first;
|
442
|
256 int align;
|
|
257 int count;
|
|
258 } pdump_entry_list;
|
|
259
|
460
|
260 typedef struct pdump_struct_list_elt
|
442
|
261 {
|
|
262 pdump_entry_list list;
|
|
263 const struct struct_description *sdesc;
|
460
|
264 } pdump_struct_list_elt;
|
442
|
265
|
|
266 typedef struct
|
|
267 {
|
460
|
268 pdump_struct_list_elt *list;
|
442
|
269 int count;
|
|
270 int size;
|
|
271 } pdump_struct_list;
|
|
272
|
460
|
273 static pdump_entry_list *pdump_object_table;
|
442
|
274 static pdump_entry_list pdump_opaque_data_list;
|
|
275 static pdump_struct_list pdump_struct_table;
|
|
276
|
460
|
277 static int *pdump_alert_undump_object;
|
442
|
278
|
|
279 static unsigned long cur_offset;
|
665
|
280 static Bytecount max_size;
|
442
|
281 static int pdump_fd;
|
|
282 static void *pdump_buf;
|
458
|
283 static FILE *pdump_out;
|
442
|
284
|
|
285 #define PDUMP_HASHSIZE 200001
|
|
286
|
460
|
287 static pdump_entry_list_elt **pdump_hash;
|
442
|
288
|
|
289 /* Since most pointers are eight bytes aligned, the >>3 allows for a better hash */
|
|
290 static int
|
|
291 pdump_make_hash (const void *obj)
|
|
292 {
|
|
293 return ((unsigned long)(obj)>>3) % PDUMP_HASHSIZE;
|
|
294 }
|
|
295
|
460
|
296 static pdump_entry_list_elt *
|
442
|
297 pdump_get_entry (const void *obj)
|
|
298 {
|
|
299 int pos = pdump_make_hash (obj);
|
460
|
300 pdump_entry_list_elt *e;
|
442
|
301
|
|
302 assert (obj != 0);
|
|
303
|
|
304 while ((e = pdump_hash[pos]) != 0)
|
|
305 {
|
|
306 if (e->obj == obj)
|
|
307 return e;
|
|
308
|
|
309 pos++;
|
|
310 if (pos == PDUMP_HASHSIZE)
|
|
311 pos = 0;
|
|
312 }
|
|
313 return 0;
|
|
314 }
|
|
315
|
|
316 static void
|
665
|
317 pdump_add_entry (pdump_entry_list *list, const void *obj, Bytecount size,
|
458
|
318 int count)
|
442
|
319 {
|
460
|
320 pdump_entry_list_elt *e;
|
442
|
321 int pos = pdump_make_hash (obj);
|
|
322
|
|
323 while ((e = pdump_hash[pos]) != 0)
|
|
324 {
|
|
325 if (e->obj == obj)
|
|
326 return;
|
|
327
|
|
328 pos++;
|
|
329 if (pos == PDUMP_HASHSIZE)
|
|
330 pos = 0;
|
|
331 }
|
|
332
|
460
|
333 e = xnew (pdump_entry_list_elt);
|
442
|
334
|
|
335 e->next = list->first;
|
|
336 e->obj = obj;
|
|
337 e->size = size;
|
|
338 e->count = count;
|
|
339 list->first = e;
|
|
340
|
|
341 list->count += count;
|
|
342 pdump_hash[pos] = e;
|
|
343
|
460
|
344 {
|
|
345 int align = pdump_size_to_align (size);
|
442
|
346
|
460
|
347 if (align < list->align)
|
|
348 list->align = align;
|
|
349 }
|
442
|
350 }
|
|
351
|
|
352 static pdump_entry_list *
|
|
353 pdump_get_entry_list (const struct struct_description *sdesc)
|
|
354 {
|
|
355 int i;
|
|
356 for (i=0; i<pdump_struct_table.count; i++)
|
|
357 if (pdump_struct_table.list[i].sdesc == sdesc)
|
|
358 return &pdump_struct_table.list[i].list;
|
|
359
|
|
360 if (pdump_struct_table.size <= pdump_struct_table.count)
|
|
361 {
|
|
362 if (pdump_struct_table.size == -1)
|
|
363 pdump_struct_table.size = 10;
|
|
364 else
|
|
365 pdump_struct_table.size = pdump_struct_table.size * 2;
|
460
|
366 pdump_struct_table.list = (pdump_struct_list_elt *)
|
442
|
367 xrealloc (pdump_struct_table.list,
|
460
|
368 pdump_struct_table.size * sizeof (pdump_struct_list_elt));
|
442
|
369 }
|
|
370 pdump_struct_table.list[pdump_struct_table.count].list.first = 0;
|
460
|
371 pdump_struct_table.list[pdump_struct_table.count].list.align = ALIGNOF (max_align_t);
|
442
|
372 pdump_struct_table.list[pdump_struct_table.count].list.count = 0;
|
|
373 pdump_struct_table.list[pdump_struct_table.count].sdesc = sdesc;
|
|
374
|
|
375 return &pdump_struct_table.list[pdump_struct_table.count++].list;
|
|
376 }
|
|
377
|
|
378 static struct
|
|
379 {
|
|
380 struct lrecord_header *obj;
|
|
381 int position;
|
|
382 int offset;
|
|
383 } backtrace[65536];
|
|
384
|
|
385 static int depth;
|
|
386
|
452
|
387 static void
|
|
388 pdump_backtrace (void)
|
442
|
389 {
|
|
390 int i;
|
|
391 stderr_out ("pdump backtrace :\n");
|
771
|
392 for (i = 0; i < depth; i++)
|
442
|
393 {
|
|
394 if (!backtrace[i].obj)
|
458
|
395 stderr_out (" - ind. (%d, %d)\n",
|
|
396 backtrace[i].position,
|
|
397 backtrace[i].offset);
|
442
|
398 else
|
|
399 {
|
|
400 stderr_out (" - %s (%d, %d)\n",
|
462
|
401 LHEADER_IMPLEMENTATION (backtrace[i].obj)->name,
|
|
402 backtrace[i].position,
|
|
403 backtrace[i].offset);
|
442
|
404 }
|
|
405 }
|
|
406 }
|
|
407
|
|
408 static void pdump_register_object (Lisp_Object obj);
|
771
|
409 static void pdump_register_struct_contents (const void *data,
|
|
410 const struct struct_description *
|
|
411 sdesc,
|
|
412 int count);
|
458
|
413 static void pdump_register_struct (const void *data,
|
|
414 const struct struct_description *sdesc,
|
|
415 int count);
|
442
|
416
|
|
417 static EMACS_INT
|
458
|
418 pdump_get_indirect_count (EMACS_INT code,
|
|
419 const struct lrecord_description *idesc,
|
|
420 const void *idata)
|
442
|
421 {
|
|
422 EMACS_INT count;
|
|
423 const void *irdata;
|
|
424
|
|
425 int line = XD_INDIRECT_VAL (code);
|
|
426 int delta = XD_INDIRECT_DELTA (code);
|
|
427
|
|
428 irdata = ((char *)idata) + idesc[line].offset;
|
|
429 switch (idesc[line].type)
|
|
430 {
|
665
|
431 case XD_BYTECOUNT:
|
|
432 count = *(Bytecount *)irdata;
|
647
|
433 break;
|
665
|
434 case XD_ELEMCOUNT:
|
|
435 count = *(Elemcount *)irdata;
|
647
|
436 break;
|
665
|
437 case XD_HASHCODE:
|
|
438 count = *(Hashcode *)irdata;
|
442
|
439 break;
|
|
440 case XD_INT:
|
|
441 count = *(int *)irdata;
|
|
442 break;
|
|
443 case XD_LONG:
|
|
444 count = *(long *)irdata;
|
|
445 break;
|
|
446 default:
|
458
|
447 stderr_out ("Unsupported count type : %d (line = %d, code=%ld)\n",
|
|
448 idesc[line].type, line, (long)code);
|
442
|
449 pdump_backtrace ();
|
647
|
450 count = 0; /* warning suppression */
|
442
|
451 abort ();
|
|
452 }
|
|
453 count += delta;
|
|
454 return count;
|
|
455 }
|
|
456
|
|
457 static void
|
|
458 pdump_register_sub (const void *data, const struct lrecord_description *desc, int me)
|
|
459 {
|
|
460 int pos;
|
|
461
|
|
462 restart:
|
|
463 for (pos = 0; desc[pos].type != XD_END; pos++)
|
|
464 {
|
|
465 const void *rdata = (const char *)data + desc[pos].offset;
|
|
466
|
|
467 backtrace[me].position = pos;
|
|
468 backtrace[me].offset = desc[pos].offset;
|
|
469
|
|
470 switch (desc[pos].type)
|
|
471 {
|
|
472 case XD_SPECIFIER_END:
|
|
473 pos = 0;
|
|
474 desc = ((const Lisp_Specifier *)data)->methods->extra_description;
|
|
475 goto restart;
|
771
|
476 case XD_CODING_SYSTEM_END:
|
|
477 pos = 0;
|
|
478 desc =
|
|
479 ((const Lisp_Coding_System *)data)->methods->extra_description;
|
|
480 goto restart;
|
665
|
481 case XD_BYTECOUNT:
|
|
482 case XD_ELEMCOUNT:
|
|
483 case XD_HASHCODE:
|
442
|
484 case XD_INT:
|
|
485 case XD_LONG:
|
|
486 case XD_INT_RESET:
|
|
487 case XD_LO_LINK:
|
|
488 break;
|
|
489 case XD_OPAQUE_DATA_PTR:
|
|
490 {
|
|
491 EMACS_INT count = desc[pos].data1;
|
|
492 if (XD_IS_INDIRECT (count))
|
|
493 count = pdump_get_indirect_count (count, desc, data);
|
|
494
|
|
495 pdump_add_entry (&pdump_opaque_data_list,
|
458
|
496 *(void **)rdata, count, 1);
|
442
|
497 break;
|
|
498 }
|
|
499 case XD_C_STRING:
|
|
500 {
|
|
501 const char *str = *(const char **)rdata;
|
|
502 if (str)
|
458
|
503 pdump_add_entry (&pdump_opaque_data_list, str, strlen (str)+1, 1);
|
442
|
504 break;
|
|
505 }
|
|
506 case XD_DOC_STRING:
|
|
507 {
|
|
508 const char *str = *(const char **)rdata;
|
|
509 if ((EMACS_INT)str > 0)
|
458
|
510 pdump_add_entry (&pdump_opaque_data_list, str, strlen (str)+1, 1);
|
442
|
511 break;
|
|
512 }
|
|
513 case XD_LISP_OBJECT:
|
|
514 {
|
|
515 const Lisp_Object *pobj = (const Lisp_Object *)rdata;
|
|
516
|
|
517 assert (desc[pos].data1 == 0);
|
|
518
|
|
519 backtrace[me].offset = (const char *)pobj - (const char *)data;
|
|
520 pdump_register_object (*pobj);
|
|
521 break;
|
|
522 }
|
|
523 case XD_LISP_OBJECT_ARRAY:
|
|
524 {
|
|
525 int i;
|
|
526 EMACS_INT count = desc[pos].data1;
|
|
527 if (XD_IS_INDIRECT (count))
|
|
528 count = pdump_get_indirect_count (count, desc, data);
|
|
529
|
|
530 for (i = 0; i < count; i++)
|
|
531 {
|
|
532 const Lisp_Object *pobj = ((const Lisp_Object *)rdata) + i;
|
|
533 Lisp_Object dobj = *pobj;
|
|
534
|
|
535 backtrace[me].offset = (const char *)pobj - (const char *)data;
|
|
536 pdump_register_object (dobj);
|
|
537 }
|
|
538 break;
|
|
539 }
|
|
540 case XD_STRUCT_PTR:
|
|
541 {
|
|
542 EMACS_INT count = desc[pos].data1;
|
|
543 const struct struct_description *sdesc = desc[pos].data2;
|
|
544 const char *dobj = *(const char **)rdata;
|
|
545 if (dobj)
|
|
546 {
|
|
547 if (XD_IS_INDIRECT (count))
|
|
548 count = pdump_get_indirect_count (count, desc, data);
|
|
549
|
|
550 pdump_register_struct (dobj, sdesc, count);
|
|
551 }
|
|
552 break;
|
|
553 }
|
771
|
554 case XD_STRUCT_ARRAY:
|
|
555 {
|
|
556 EMACS_INT count = desc[pos].data1;
|
|
557 const struct struct_description *sdesc = desc[pos].data2;
|
|
558
|
|
559 if (XD_IS_INDIRECT (count))
|
|
560 count = pdump_get_indirect_count (count, desc, data);
|
|
561
|
|
562 pdump_register_struct_contents (rdata, sdesc, count);
|
|
563 break;
|
|
564 }
|
|
565 case XD_UNION:
|
|
566 abort (); /* #### IMPLEMENT ME! NEEDED FOR UNICODE SUPPORT */
|
|
567
|
442
|
568 default:
|
|
569 stderr_out ("Unsupported dump type : %d\n", desc[pos].type);
|
|
570 pdump_backtrace ();
|
|
571 abort ();
|
|
572 };
|
|
573 }
|
|
574 }
|
|
575
|
|
576 static void
|
|
577 pdump_register_object (Lisp_Object obj)
|
|
578 {
|
|
579 struct lrecord_header *objh;
|
458
|
580 const struct lrecord_implementation *imp;
|
442
|
581
|
|
582 if (!POINTER_TYPE_P (XTYPE (obj)))
|
|
583 return;
|
|
584
|
|
585 objh = XRECORD_LHEADER (obj);
|
|
586 if (!objh)
|
|
587 return;
|
|
588
|
|
589 if (pdump_get_entry (objh))
|
|
590 return;
|
|
591
|
458
|
592 imp = LHEADER_IMPLEMENTATION (objh);
|
|
593
|
|
594 if (imp->description)
|
442
|
595 {
|
|
596 int me = depth++;
|
771
|
597 if (me > 65536)
|
442
|
598 {
|
|
599 stderr_out ("Backtrace overflow, loop ?\n");
|
|
600 abort ();
|
|
601 }
|
|
602 backtrace[me].obj = objh;
|
|
603 backtrace[me].position = 0;
|
|
604 backtrace[me].offset = 0;
|
|
605
|
|
606 pdump_add_entry (pdump_object_table + objh->type,
|
|
607 objh,
|
458
|
608 imp->static_size ?
|
|
609 imp->static_size :
|
|
610 imp->size_in_bytes_method (objh),
|
442
|
611 1);
|
458
|
612 pdump_register_sub (objh, imp->description, me);
|
442
|
613 --depth;
|
|
614 }
|
|
615 else
|
|
616 {
|
|
617 pdump_alert_undump_object[objh->type]++;
|
458
|
618 stderr_out ("Undumpable object type : %s\n", imp->name);
|
442
|
619 pdump_backtrace ();
|
|
620 }
|
|
621 }
|
|
622
|
771
|
623 /* Return the size of the memory block (NOT necessarily a structure!)
|
|
624 described by SDESC and pointed to by OBJ. If SDESC records an
|
|
625 explicit size (i.e. non-zero), it is simply returned; otherwise,
|
|
626 the size is calculated by the maximum offset and the size of the
|
|
627 object at that offset, rounded up to the maximum alignment. In
|
|
628 this case, we may need the object, for example when retrieving an
|
|
629 "indirect count" of an inlined array (the count is not constant,
|
|
630 but is specified by one of the elements of the memory block). (It
|
|
631 is generally not a problem if we return an overly large size -- we
|
|
632 will simply end up reserving more space than necessary; but if the
|
|
633 size is too small we could be in serious trouble, in particular
|
|
634 with nested inlined structures, where there may be alignment
|
|
635 padding in the middle of a block. #### In fact there is an (at
|
|
636 least theoretical) problem with an overly large size -- we may
|
|
637 trigger a protection fault when reading from invalid memory. We
|
|
638 need to handle this -- perhaps in a stupid but dependable way,
|
|
639 i.e. by trapping SIGSEGV and SIGBUS.) */
|
|
640
|
|
641 static Bytecount
|
|
642 pdump_structure_size (const void *obj, const struct struct_description *sdesc)
|
|
643 {
|
|
644 int max_offset = -1;
|
|
645 int max_offset_pos = -1;
|
|
646 int size_at_max = 0;
|
|
647 int pos;
|
|
648 const struct lrecord_description *desc;
|
|
649 void *rdata;
|
|
650
|
|
651 if (sdesc->size)
|
|
652 return sdesc->size;
|
|
653
|
|
654 desc = sdesc->description;
|
|
655
|
|
656 for (pos = 0; desc[pos].type != XD_END; pos++)
|
|
657 {
|
|
658 if (desc[pos].offset == max_offset)
|
|
659 {
|
|
660 stderr_out ("Two relocatable elements at same offset?\n");
|
|
661 abort ();
|
|
662 }
|
|
663 else if (desc[pos].offset > max_offset)
|
|
664 {
|
|
665 max_offset = desc[pos].offset;
|
|
666 max_offset_pos = pos;
|
|
667 }
|
|
668 }
|
|
669
|
|
670 if (max_offset_pos < 0)
|
|
671 return 0;
|
|
672
|
|
673 pos = max_offset_pos;
|
|
674 rdata = (char *) obj + desc[pos].offset;
|
|
675
|
|
676 switch (desc[pos].type)
|
|
677 {
|
|
678 case XD_LISP_OBJECT_ARRAY:
|
|
679 {
|
|
680 EMACS_INT val = desc[pos].data1;
|
|
681 if (XD_IS_INDIRECT (val))
|
|
682 val = pdump_get_indirect_count (val, desc, obj);
|
|
683 size_at_max = val * sizeof (Lisp_Object);
|
|
684 break;
|
|
685 }
|
|
686 case XD_LISP_OBJECT:
|
|
687 case XD_LO_LINK:
|
|
688 size_at_max = sizeof (Lisp_Object);
|
|
689 break;
|
|
690 case XD_OPAQUE_PTR:
|
|
691 size_at_max = sizeof (void *);
|
|
692 break;
|
|
693 case XD_STRUCT_PTR:
|
|
694 {
|
|
695 EMACS_INT val = desc[pos].data1;
|
|
696 if (XD_IS_INDIRECT (val))
|
|
697 val = pdump_get_indirect_count (val, desc, obj);
|
|
698 size_at_max = val * sizeof (void *);
|
|
699 break;
|
|
700 }
|
|
701 break;
|
|
702 case XD_STRUCT_ARRAY:
|
|
703 {
|
|
704 EMACS_INT val = desc[pos].data1;
|
|
705
|
|
706 if (XD_IS_INDIRECT (val))
|
|
707 val = pdump_get_indirect_count (val, desc, obj);
|
|
708
|
|
709 size_at_max = val * pdump_structure_size (rdata, desc[pos].data2);
|
|
710 break;
|
|
711 }
|
|
712 break;
|
|
713 case XD_OPAQUE_DATA_PTR:
|
|
714 size_at_max = sizeof (void *);
|
|
715 break;
|
|
716 case XD_UNION:
|
|
717 abort (); /* #### IMPLEMENT ME! NEEDED FOR UNICODE
|
|
718 SUPPORT */
|
|
719 break;
|
|
720 case XD_C_STRING:
|
|
721 size_at_max = sizeof (void *);
|
|
722 break;
|
|
723 case XD_DOC_STRING:
|
|
724 size_at_max = sizeof (void *);
|
|
725 break;
|
|
726 case XD_INT_RESET:
|
|
727 size_at_max = sizeof (int);
|
|
728 break;
|
|
729 case XD_BYTECOUNT:
|
|
730 size_at_max = sizeof (Bytecount);
|
|
731 break;
|
|
732 case XD_ELEMCOUNT:
|
|
733 size_at_max = sizeof (Elemcount);
|
|
734 break;
|
|
735 case XD_HASHCODE:
|
|
736 size_at_max = sizeof (Hashcode);
|
|
737 break;
|
|
738 case XD_INT:
|
|
739 size_at_max = sizeof (int);
|
|
740 break;
|
|
741 case XD_LONG:
|
|
742 size_at_max = sizeof (long);
|
|
743 break;
|
|
744 case XD_SPECIFIER_END:
|
|
745 case XD_CODING_SYSTEM_END:
|
|
746 stderr_out
|
|
747 ("Should not be seeing XD_SPECIFIER_END or\n"
|
|
748 "XD_CODING_SYSTEM_END outside of struct Lisp_Specifier\n"
|
|
749 "and struct Lisp_Coding_System.\n");
|
|
750 abort ();
|
|
751 default:
|
|
752 stderr_out ("Unsupported dump type : %d\n", desc[pos].type);
|
|
753 abort ();
|
|
754 }
|
|
755
|
|
756 /* We have no way of knowing the required alignment for this structure,
|
|
757 so just max it maximally aligned. */
|
826
|
758 return MAX_ALIGN_SIZE (max_offset + size_at_max);
|
771
|
759 }
|
|
760
|
|
761 /* Register the referenced objects in the array of COUNT objects of
|
|
762 located at DATA; each object is described by SDESC. "Object" here
|
|
763 simply means any block of memory; it need not actually be a C
|
|
764 "struct". It could be a single integer or Lisp_Object, for
|
|
765 example, as long as the description is accurate.
|
|
766
|
|
767 This does not register the block of memory itself; it may, for
|
|
768 example, be an array of structures inlined in another memory block
|
|
769 and thus should not be registered. See pdump_register_struct(),
|
|
770 which does register the memory block. */
|
|
771
|
|
772 static void
|
|
773 pdump_register_struct_contents (const void *data,
|
|
774 const struct struct_description *sdesc,
|
|
775 int count)
|
|
776
|
|
777 {
|
|
778 int me = depth++;
|
|
779 int i;
|
|
780 Bytecount elsize;
|
|
781
|
|
782 if (me>65536)
|
|
783 {
|
|
784 stderr_out ("Backtrace overflow, loop ?\n");
|
|
785 abort ();
|
|
786 }
|
|
787 backtrace[me].obj = 0;
|
|
788 backtrace[me].position = 0;
|
|
789 backtrace[me].offset = 0;
|
|
790
|
|
791 elsize = pdump_structure_size (data, sdesc);
|
|
792
|
|
793 for (i = 0; i < count; i++)
|
|
794 {
|
|
795 pdump_register_sub (((char *) data) + elsize * i,
|
|
796 sdesc->description,
|
|
797 me);
|
|
798 }
|
|
799 --depth;
|
|
800 }
|
|
801
|
|
802 /* Register the array of COUNT objects of located at DATA; each object is
|
|
803 described by SDESC. "Object" here simply means any block of memory;
|
|
804 it need not actually be a C "struct". It could be a single integer
|
|
805 or Lisp_Object, for example, as long as the description is accurate.
|
|
806
|
|
807 This is like pdump_register_struct_contents() but also registers
|
|
808 the memory block itself. */
|
|
809
|
442
|
810 static void
|
458
|
811 pdump_register_struct (const void *data,
|
|
812 const struct struct_description *sdesc,
|
|
813 int count)
|
442
|
814 {
|
|
815 if (data && !pdump_get_entry (data))
|
|
816 {
|
771
|
817 pdump_add_entry (pdump_get_entry_list (sdesc), data,
|
|
818 pdump_structure_size (data, sdesc), count);
|
442
|
819
|
771
|
820 pdump_register_struct_contents (data, sdesc, count);
|
442
|
821 }
|
|
822 }
|
|
823
|
771
|
824 /* Store the already-calculated new pointer offsets for all pointers
|
|
825 in the COUNT contiguous blocks of memory, each described by DESC
|
|
826 and of size SIZE, whose original is located at ORIG_DATA and the
|
|
827 modifiable copy at DATA.
|
|
828
|
|
829 This is done just before writing the modified block of memory to
|
|
830 the dump file. The new pointer offsets have been carefully
|
|
831 calculated so that the data being pointed gets written at that
|
|
832 offset in the dump file. That way, the dump file is a correct
|
|
833 memory image except perhaps for a constant that needs to be added
|
|
834 to all pointers. (#### In fact, we SHOULD be starting up a dumped
|
|
835 XEmacs, seeing where the dumped file gets loaded into memory, and
|
|
836 then rewriting the dumped file after relocating all the pointers
|
|
837 relative to this memory location. That way, if the file gets
|
|
838 loaded again at the same location, which will be common, we don't
|
|
839 have to do any relocating, which is both faster at startup and
|
|
840 allows the read-only part of the dumped data to be shared read-only
|
|
841 between different invocations of XEmacs.)
|
|
842
|
|
843 #### Do we distinguish between read-only and writable dumped data?
|
|
844 Should we? It's tricky because the dumped data, once loaded again,
|
|
845 cannot really be free()d or garbage collected since it's all stored
|
|
846 in one contiguous block of data with no malloc() headers, and we
|
|
847 don't keep track of the pointers used internally in malloc() and
|
|
848 the Lisp allocator to track allocated blocks of memory. */
|
|
849
|
|
850 static void
|
|
851 pdump_store_new_pointer_offsets (int count, void *data, const void *orig_data,
|
|
852 const struct lrecord_description *desc,
|
|
853 int size)
|
|
854 {
|
|
855 int pos, i;
|
|
856 /* Process each block one by one */
|
|
857 for (i = 0; i < count; i++)
|
|
858 {
|
|
859 /* CUR points to the beginning of each block in the new data. */
|
|
860 char *cur = ((char *)data) + i*size;
|
|
861 restart:
|
|
862 /* Scan each line of the description for relocatable pointers */
|
|
863 for (pos = 0; desc[pos].type != XD_END; pos++)
|
|
864 {
|
|
865 /* RDATA points to the beginning of each element in the new data. */
|
|
866 void *rdata = cur + desc[pos].offset;
|
|
867 switch (desc[pos].type)
|
|
868 {
|
|
869 case XD_SPECIFIER_END:
|
|
870 desc = ((const Lisp_Specifier *)(orig_data))->
|
|
871 methods->extra_description;
|
|
872 goto restart;
|
|
873 case XD_CODING_SYSTEM_END:
|
|
874 desc = ((const Lisp_Coding_System *)(orig_data))->
|
|
875 methods->extra_description;
|
|
876 goto restart;
|
|
877 case XD_BYTECOUNT:
|
|
878 case XD_ELEMCOUNT:
|
|
879 case XD_HASHCODE:
|
|
880 case XD_INT:
|
|
881 case XD_LONG:
|
|
882 break;
|
|
883 case XD_INT_RESET:
|
|
884 {
|
|
885 EMACS_INT val = desc[pos].data1;
|
|
886 if (XD_IS_INDIRECT (val))
|
|
887 val = pdump_get_indirect_count (val, desc, orig_data);
|
|
888 * (int *) rdata = val;
|
|
889 break;
|
|
890 }
|
|
891 case XD_OPAQUE_DATA_PTR:
|
|
892 case XD_C_STRING:
|
|
893 case XD_STRUCT_PTR:
|
|
894 {
|
|
895 void *ptr = * (void **) rdata;
|
|
896 if (ptr)
|
|
897 * (EMACS_INT *) rdata = pdump_get_entry (ptr)->save_offset;
|
|
898 break;
|
|
899 }
|
|
900 case XD_LO_LINK:
|
|
901 {
|
|
902 /* As described in lrecord.h, this is a weak link.
|
|
903 Thus, we need to link this object not (necessarily)
|
|
904 to the object directly pointed to, but to the next
|
|
905 referenced object in the chain. None of the
|
|
906 intermediate objects will be written out, so we
|
|
907 traverse down the chain of objects until we find a
|
|
908 referenced one. (The Qnil or Qunbound that ends the
|
|
909 chain will always be a referenced object.) */
|
|
910 Lisp_Object obj = * (Lisp_Object *) rdata;
|
|
911 pdump_entry_list_elt *elt1;
|
|
912 for (;;)
|
|
913 {
|
|
914 elt1 = pdump_get_entry (XRECORD_LHEADER (obj));
|
|
915 if (elt1)
|
|
916 break;
|
|
917 obj = * (Lisp_Object *) (desc[pos].offset +
|
|
918 (char *)(XRECORD_LHEADER (obj)));
|
|
919 }
|
|
920 * (EMACS_INT *) rdata = elt1->save_offset;
|
|
921 break;
|
|
922 }
|
|
923 case XD_LISP_OBJECT:
|
|
924 {
|
|
925 Lisp_Object *pobj = (Lisp_Object *) rdata;
|
|
926
|
|
927 assert (desc[pos].data1 == 0);
|
|
928
|
|
929 if (POINTER_TYPE_P (XTYPE (*pobj)) && XRECORD_LHEADER (*pobj))
|
|
930 * (EMACS_INT *) pobj =
|
|
931 pdump_get_entry (XRECORD_LHEADER (*pobj))->save_offset;
|
|
932 break;
|
|
933 }
|
|
934 case XD_LISP_OBJECT_ARRAY:
|
|
935 {
|
|
936 EMACS_INT num = desc[pos].data1;
|
|
937 int j;
|
|
938 if (XD_IS_INDIRECT (num))
|
|
939 num = pdump_get_indirect_count (num, desc, orig_data);
|
|
940
|
|
941 for (j = 0; j < num; j++)
|
|
942 {
|
|
943 Lisp_Object *pobj = ((Lisp_Object *) rdata) + j;
|
|
944 if (POINTER_TYPE_P (XTYPE (*pobj)) &&
|
|
945 XRECORD_LHEADER (*pobj))
|
|
946 * (EMACS_INT *) pobj =
|
|
947 pdump_get_entry (XRECORD_LHEADER (*pobj))->save_offset;
|
|
948 }
|
|
949 break;
|
|
950 }
|
|
951 case XD_DOC_STRING:
|
|
952 {
|
|
953 EMACS_INT str = *(EMACS_INT *)rdata;
|
|
954 if (str > 0)
|
|
955 * (EMACS_INT *) rdata =
|
|
956 pdump_get_entry ((void *)str)->save_offset;
|
|
957 break;
|
|
958 }
|
|
959 case XD_STRUCT_ARRAY:
|
|
960 {
|
|
961 EMACS_INT num = desc[pos].data1;
|
|
962 if (XD_IS_INDIRECT (num))
|
|
963 num = pdump_get_indirect_count (num, desc, orig_data);
|
|
964
|
|
965 pdump_store_new_pointer_offsets
|
|
966 (num, rdata,
|
|
967 ((char *) rdata - (char *) data) + (char *) orig_data,
|
|
968 desc[pos].data2->description,
|
|
969 pdump_structure_size
|
|
970 (((char *) rdata - (char *) data) + (char *) orig_data,
|
|
971 desc[pos].data2));
|
|
972 break;
|
|
973 }
|
|
974 case XD_UNION:
|
|
975 abort (); /* #### IMPLEMENT ME! NEEDED FOR UNICODE
|
|
976 SUPPORT */
|
|
977
|
|
978 default:
|
|
979 stderr_out ("Unsupported dump type : %d\n", desc[pos].type);
|
|
980 abort ();
|
|
981 }
|
|
982 }
|
|
983 }
|
|
984 }
|
|
985
|
|
986 /* Write out to global file descriptor PDUMP_OUT the element (one or
|
|
987 more contiguous blocks of identical size/description) recorded in
|
|
988 ELT and described by DESC. The element is first copied to a buffer
|
|
989 and then all pointers (this includes Lisp_Objects other than
|
|
990 integer/character) are relocated to the (pre-computed) offset in
|
|
991 the dump file. */
|
|
992
|
442
|
993 static void
|
460
|
994 pdump_dump_data (pdump_entry_list_elt *elt,
|
458
|
995 const struct lrecord_description *desc)
|
442
|
996 {
|
665
|
997 Bytecount size = elt->size;
|
460
|
998 int count = elt->count;
|
442
|
999 if (desc)
|
|
1000 {
|
771
|
1001 /* Copy to temporary buffer */
|
460
|
1002 memcpy (pdump_buf, elt->obj, size*count);
|
442
|
1003
|
771
|
1004 /* Store new offsets into all pointers in block */
|
|
1005 pdump_store_new_pointer_offsets (count, pdump_buf, elt->obj, desc, size);
|
|
1006 }
|
|
1007 retry_fwrite (desc ? pdump_buf : elt->obj, size, count, pdump_out);
|
|
1008 }
|
442
|
1009
|
771
|
1010 /* Relocate a single memory block at DATA, described by DESC, from its
|
|
1011 assumed load location to its actual one by adding DELTA to all
|
|
1012 pointers in the block. Does not recursively relocate any other
|
|
1013 memory blocks pointed to. (We already have a list of all memory
|
|
1014 blocks in the dump file.) */
|
442
|
1015
|
|
1016 static void
|
458
|
1017 pdump_reloc_one (void *data, EMACS_INT delta,
|
|
1018 const struct lrecord_description *desc)
|
442
|
1019 {
|
|
1020 int pos;
|
|
1021
|
|
1022 restart:
|
|
1023 for (pos = 0; desc[pos].type != XD_END; pos++)
|
|
1024 {
|
|
1025 void *rdata = (char *)data + desc[pos].offset;
|
|
1026 switch (desc[pos].type)
|
|
1027 {
|
|
1028 case XD_SPECIFIER_END:
|
|
1029 pos = 0;
|
|
1030 desc = ((const Lisp_Specifier *)data)->methods->extra_description;
|
|
1031 goto restart;
|
771
|
1032 case XD_CODING_SYSTEM_END:
|
|
1033 pos = 0;
|
|
1034 desc =
|
|
1035 ((const Lisp_Coding_System *)data)->methods->extra_description;
|
|
1036 goto restart;
|
665
|
1037 case XD_BYTECOUNT:
|
|
1038 case XD_ELEMCOUNT:
|
|
1039 case XD_HASHCODE:
|
442
|
1040 case XD_INT:
|
|
1041 case XD_LONG:
|
|
1042 case XD_INT_RESET:
|
|
1043 break;
|
|
1044 case XD_OPAQUE_DATA_PTR:
|
|
1045 case XD_C_STRING:
|
|
1046 case XD_STRUCT_PTR:
|
|
1047 case XD_LO_LINK:
|
|
1048 {
|
|
1049 EMACS_INT ptr = *(EMACS_INT *)rdata;
|
|
1050 if (ptr)
|
|
1051 *(EMACS_INT *)rdata = ptr+delta;
|
|
1052 break;
|
|
1053 }
|
|
1054 case XD_LISP_OBJECT:
|
|
1055 {
|
|
1056 Lisp_Object *pobj = (Lisp_Object *) rdata;
|
|
1057
|
|
1058 assert (desc[pos].data1 == 0);
|
|
1059
|
|
1060 if (POINTER_TYPE_P (XTYPE (*pobj))
|
|
1061 && ! EQ (*pobj, Qnull_pointer))
|
793
|
1062 *pobj = wrap_pointer_1 ((char *) XPNTR (*pobj) + delta);
|
442
|
1063
|
|
1064 break;
|
|
1065 }
|
|
1066 case XD_LISP_OBJECT_ARRAY:
|
|
1067 {
|
|
1068 EMACS_INT num = desc[pos].data1;
|
|
1069 int j;
|
|
1070 if (XD_IS_INDIRECT (num))
|
|
1071 num = pdump_get_indirect_count (num, desc, data);
|
|
1072
|
|
1073 for (j=0; j<num; j++)
|
|
1074 {
|
|
1075 Lisp_Object *pobj = (Lisp_Object *) rdata + j;
|
|
1076
|
|
1077 if (POINTER_TYPE_P (XTYPE (*pobj))
|
|
1078 && ! EQ (*pobj, Qnull_pointer))
|
793
|
1079 *pobj = wrap_pointer_1 ((char *) XPNTR (*pobj) + delta);
|
442
|
1080 }
|
|
1081 break;
|
|
1082 }
|
|
1083 case XD_DOC_STRING:
|
|
1084 {
|
|
1085 EMACS_INT str = *(EMACS_INT *)rdata;
|
|
1086 if (str > 0)
|
|
1087 *(EMACS_INT *)rdata = str + delta;
|
|
1088 break;
|
|
1089 }
|
771
|
1090 case XD_STRUCT_ARRAY:
|
|
1091 {
|
|
1092 EMACS_INT num = desc[pos].data1;
|
|
1093 int j;
|
|
1094 const struct struct_description *sdesc = desc[pos].data2;
|
|
1095 Bytecount size = pdump_structure_size (rdata, sdesc);
|
|
1096
|
|
1097 if (XD_IS_INDIRECT (num))
|
|
1098 num = pdump_get_indirect_count (num, desc, data);
|
|
1099 /* Note: We are recursing over data in the block itself */
|
|
1100 for (j = 0; j < num; j++)
|
|
1101 pdump_reloc_one ((char *) rdata + j * size, delta,
|
|
1102 sdesc->description);
|
|
1103
|
|
1104 break;
|
|
1105 }
|
|
1106
|
|
1107 case XD_UNION:
|
|
1108 abort (); /* #### IMPLEMENT ME! NEEDED FOR UNICODE SUPPORT */
|
442
|
1109 default:
|
|
1110 stderr_out ("Unsupported dump type : %d\n", desc[pos].type);
|
|
1111 abort ();
|
|
1112 };
|
|
1113 }
|
|
1114 }
|
|
1115
|
|
1116 static void
|
460
|
1117 pdump_allocate_offset (pdump_entry_list_elt *elt,
|
458
|
1118 const struct lrecord_description *desc)
|
442
|
1119 {
|
665
|
1120 Bytecount size = elt->count * elt->size;
|
460
|
1121 elt->save_offset = cur_offset;
|
442
|
1122 if (size>max_size)
|
|
1123 max_size = size;
|
|
1124 cur_offset += size;
|
|
1125 }
|
|
1126
|
|
1127 static void
|
460
|
1128 pdump_scan_by_alignment (void (*f)(pdump_entry_list_elt *,
|
458
|
1129 const struct lrecord_description *))
|
442
|
1130 {
|
460
|
1131 int align;
|
|
1132
|
|
1133 for (align = ALIGNOF (max_align_t); align; align>>=1)
|
442
|
1134 {
|
460
|
1135 int i;
|
|
1136 pdump_entry_list_elt *elt;
|
|
1137
|
442
|
1138 for (i=0; i<lrecord_type_count; i++)
|
|
1139 if (pdump_object_table[i].align == align)
|
460
|
1140 for (elt = pdump_object_table[i].first; elt; elt = elt->next)
|
|
1141 f (elt, lrecord_implementations_table[i]->description);
|
442
|
1142
|
|
1143 for (i=0; i<pdump_struct_table.count; i++)
|
460
|
1144 {
|
|
1145 pdump_struct_list_elt list = pdump_struct_table.list[i];
|
|
1146 if (list.list.align == align)
|
|
1147 for (elt = list.list.first; elt; elt = elt->next)
|
|
1148 f (elt, list.sdesc->description);
|
|
1149 }
|
442
|
1150
|
460
|
1151 for (elt = pdump_opaque_data_list.first; elt; elt = elt->next)
|
|
1152 if (pdump_size_to_align (elt->size) == align)
|
|
1153 f (elt, 0);
|
442
|
1154 }
|
|
1155 }
|
|
1156
|
|
1157 static void
|
458
|
1158 pdump_dump_root_struct_ptrs (void)
|
442
|
1159 {
|
|
1160 int i;
|
665
|
1161 Elemcount count = Dynarr_length (pdump_root_struct_ptrs);
|
458
|
1162 pdump_static_pointer *data = alloca_array (pdump_static_pointer, count);
|
|
1163 for (i = 0; i < count; i++)
|
442
|
1164 {
|
458
|
1165 data[i].address = (char **) Dynarr_atp (pdump_root_struct_ptrs, i)->ptraddress;
|
|
1166 data[i].value = (char *) pdump_get_entry (* data[i].address)->save_offset;
|
442
|
1167 }
|
458
|
1168 PDUMP_ALIGN_OUTPUT (pdump_static_pointer);
|
771
|
1169 retry_fwrite (data, sizeof (pdump_static_pointer), count, pdump_out);
|
442
|
1170 }
|
|
1171
|
|
1172 static void
|
452
|
1173 pdump_dump_opaques (void)
|
442
|
1174 {
|
|
1175 int i;
|
452
|
1176 for (i = 0; i < Dynarr_length (pdump_opaques); i++)
|
442
|
1177 {
|
452
|
1178 pdump_opaque *info = Dynarr_atp (pdump_opaques, i);
|
458
|
1179 PDUMP_WRITE_ALIGNED (pdump_opaque, *info);
|
771
|
1180 retry_fwrite (info->varaddress, info->size, 1, pdump_out);
|
442
|
1181 }
|
|
1182 }
|
|
1183
|
|
1184 static void
|
|
1185 pdump_dump_rtables (void)
|
|
1186 {
|
452
|
1187 int i;
|
460
|
1188 pdump_entry_list_elt *elt;
|
442
|
1189 pdump_reloc_table rt;
|
|
1190
|
|
1191 for (i=0; i<lrecord_type_count; i++)
|
|
1192 {
|
460
|
1193 elt = pdump_object_table[i].first;
|
|
1194 if (!elt)
|
442
|
1195 continue;
|
|
1196 rt.desc = lrecord_implementations_table[i]->description;
|
|
1197 rt.count = pdump_object_table[i].count;
|
458
|
1198 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
460
|
1199 while (elt)
|
442
|
1200 {
|
460
|
1201 EMACS_INT rdata = pdump_get_entry (elt->obj)->save_offset;
|
458
|
1202 PDUMP_WRITE_ALIGNED (EMACS_INT, rdata);
|
460
|
1203 elt = elt->next;
|
442
|
1204 }
|
|
1205 }
|
|
1206
|
|
1207 rt.desc = 0;
|
|
1208 rt.count = 0;
|
458
|
1209 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
442
|
1210
|
|
1211 for (i=0; i<pdump_struct_table.count; i++)
|
|
1212 {
|
460
|
1213 elt = pdump_struct_table.list[i].list.first;
|
442
|
1214 rt.desc = pdump_struct_table.list[i].sdesc->description;
|
|
1215 rt.count = pdump_struct_table.list[i].list.count;
|
458
|
1216 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
460
|
1217 while (elt)
|
442
|
1218 {
|
460
|
1219 EMACS_INT rdata = pdump_get_entry (elt->obj)->save_offset;
|
452
|
1220 int j;
|
460
|
1221 for (j=0; j<elt->count; j++)
|
442
|
1222 {
|
458
|
1223 PDUMP_WRITE_ALIGNED (EMACS_INT, rdata);
|
460
|
1224 rdata += elt->size;
|
442
|
1225 }
|
460
|
1226 elt = elt->next;
|
442
|
1227 }
|
|
1228 }
|
|
1229 rt.desc = 0;
|
|
1230 rt.count = 0;
|
458
|
1231 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
442
|
1232 }
|
|
1233
|
|
1234 static void
|
458
|
1235 pdump_dump_root_objects (void)
|
442
|
1236 {
|
665
|
1237 Elemcount count = (Dynarr_length (pdump_root_objects) +
|
647
|
1238 Dynarr_length (pdump_weak_object_chains));
|
665
|
1239 Elemcount i;
|
442
|
1240
|
665
|
1241 PDUMP_WRITE_ALIGNED (Elemcount, count);
|
458
|
1242 PDUMP_ALIGN_OUTPUT (pdump_static_Lisp_Object);
|
442
|
1243
|
647
|
1244 for (i = 0; i < Dynarr_length (pdump_root_objects); i++)
|
442
|
1245 {
|
458
|
1246 pdump_static_Lisp_Object obj;
|
|
1247 obj.address = Dynarr_at (pdump_root_objects, i);
|
|
1248 obj.value = * obj.address;
|
460
|
1249
|
458
|
1250 if (POINTER_TYPE_P (XTYPE (obj.value)))
|
619
|
1251 obj.value =
|
|
1252 wrap_pointer_1 ((void *) pdump_get_entry (XRECORD_LHEADER
|
617
|
1253 (obj.value))->save_offset);
|
460
|
1254
|
458
|
1255 PDUMP_WRITE (pdump_static_Lisp_Object, obj);
|
442
|
1256 }
|
|
1257
|
452
|
1258 for (i=0; i<Dynarr_length (pdump_weak_object_chains); i++)
|
442
|
1259 {
|
460
|
1260 pdump_entry_list_elt *elt;
|
458
|
1261 pdump_static_Lisp_Object obj;
|
442
|
1262
|
458
|
1263 obj.address = Dynarr_at (pdump_weak_object_chains, i);
|
|
1264 obj.value = * obj.address;
|
460
|
1265
|
442
|
1266 for (;;)
|
|
1267 {
|
|
1268 const struct lrecord_description *desc;
|
|
1269 int pos;
|
460
|
1270 elt = pdump_get_entry (XRECORD_LHEADER (obj.value));
|
|
1271 if (elt)
|
442
|
1272 break;
|
458
|
1273 desc = XRECORD_LHEADER_IMPLEMENTATION (obj.value)->description;
|
442
|
1274 for (pos = 0; desc[pos].type != XD_LO_LINK; pos++)
|
|
1275 assert (desc[pos].type != XD_END);
|
|
1276
|
458
|
1277 obj.value = *(Lisp_Object *)(desc[pos].offset + (char *)(XRECORD_LHEADER (obj.value)));
|
442
|
1278 }
|
619
|
1279 obj.value = wrap_pointer_1 ((void *) elt->save_offset);
|
442
|
1280
|
458
|
1281 PDUMP_WRITE (pdump_static_Lisp_Object, obj);
|
442
|
1282 }
|
|
1283 }
|
|
1284
|
|
1285 void
|
|
1286 pdump (void)
|
|
1287 {
|
|
1288 int i;
|
|
1289 Lisp_Object t_console, t_device, t_frame;
|
|
1290 int none;
|
458
|
1291 pdump_header header;
|
442
|
1292
|
460
|
1293 pdump_object_table = xnew_array (pdump_entry_list, lrecord_type_count);
|
|
1294 pdump_alert_undump_object = xnew_array (int, lrecord_type_count);
|
|
1295
|
|
1296 assert (ALIGNOF (max_align_t) <= pdump_align_table[0]);
|
|
1297
|
|
1298 for (i = 0; i < countof (pdump_align_table); i++)
|
|
1299 if (pdump_align_table[i] > ALIGNOF (max_align_t))
|
|
1300 pdump_align_table[i] = ALIGNOF (max_align_t);
|
|
1301
|
446
|
1302 flush_all_buffer_local_cache ();
|
|
1303
|
442
|
1304 /* These appear in a DEFVAR_LISP, which does a staticpro() */
|
452
|
1305 t_console = Vterminal_console; Vterminal_console = Qnil;
|
|
1306 t_frame = Vterminal_frame; Vterminal_frame = Qnil;
|
|
1307 t_device = Vterminal_device; Vterminal_device = Qnil;
|
442
|
1308
|
452
|
1309 dump_add_opaque (&lrecord_implementations_table,
|
|
1310 lrecord_type_count * sizeof (lrecord_implementations_table[0]));
|
|
1311 dump_add_opaque (&lrecord_markers,
|
|
1312 lrecord_type_count * sizeof (lrecord_markers[0]));
|
442
|
1313
|
460
|
1314 pdump_hash = xnew_array_and_zero (pdump_entry_list_elt *, PDUMP_HASHSIZE);
|
442
|
1315
|
|
1316 for (i=0; i<lrecord_type_count; i++)
|
|
1317 {
|
|
1318 pdump_object_table[i].first = 0;
|
460
|
1319 pdump_object_table[i].align = ALIGNOF (max_align_t);
|
442
|
1320 pdump_object_table[i].count = 0;
|
|
1321 pdump_alert_undump_object[i] = 0;
|
|
1322 }
|
|
1323 pdump_struct_table.count = 0;
|
|
1324 pdump_struct_table.size = -1;
|
|
1325
|
|
1326 pdump_opaque_data_list.first = 0;
|
460
|
1327 pdump_opaque_data_list.align = ALIGNOF (max_align_t);
|
442
|
1328 pdump_opaque_data_list.count = 0;
|
|
1329 depth = 0;
|
|
1330
|
452
|
1331 for (i=0; i<Dynarr_length (pdump_root_objects); i++)
|
|
1332 pdump_register_object (* Dynarr_at (pdump_root_objects, i));
|
442
|
1333
|
|
1334 none = 1;
|
|
1335 for (i=0; i<lrecord_type_count; i++)
|
|
1336 if (pdump_alert_undump_object[i])
|
|
1337 {
|
|
1338 if (none)
|
|
1339 printf ("Undumpable types list :\n");
|
|
1340 none = 0;
|
|
1341 printf (" - %s (%d)\n", lrecord_implementations_table[i]->name, pdump_alert_undump_object[i]);
|
|
1342 }
|
|
1343 if (!none)
|
|
1344 return;
|
|
1345
|
452
|
1346 for (i=0; i<Dynarr_length (pdump_root_struct_ptrs); i++)
|
|
1347 {
|
|
1348 pdump_root_struct_ptr info = Dynarr_at (pdump_root_struct_ptrs, i);
|
|
1349 pdump_register_struct (*(info.ptraddress), info.desc, 1);
|
|
1350 }
|
442
|
1351
|
458
|
1352 memcpy (header.signature, PDUMP_SIGNATURE, PDUMP_SIGNATURE_LEN);
|
|
1353 header.id = dump_id;
|
|
1354 header.reloc_address = 0;
|
|
1355 header.nb_root_struct_ptrs = Dynarr_length (pdump_root_struct_ptrs);
|
|
1356 header.nb_opaques = Dynarr_length (pdump_opaques);
|
442
|
1357
|
826
|
1358 cur_offset = MAX_ALIGN_SIZE (sizeof (header));
|
442
|
1359 max_size = 0;
|
|
1360
|
|
1361 pdump_scan_by_alignment (pdump_allocate_offset);
|
826
|
1362 cur_offset = MAX_ALIGN_SIZE (cur_offset);
|
458
|
1363 header.stab_offset = cur_offset;
|
442
|
1364
|
|
1365 pdump_buf = xmalloc (max_size);
|
|
1366 pdump_fd = open (EMACS_PROGNAME ".dmp",
|
|
1367 O_WRONLY | O_CREAT | O_TRUNC | OPEN_BINARY, 0666);
|
771
|
1368 if (pdump_fd < 0)
|
|
1369 report_file_error ("Unable to open dump file",
|
|
1370 build_string (EMACS_PROGNAME ".dmp"));
|
458
|
1371 pdump_out = fdopen (pdump_fd, "w");
|
771
|
1372 if (pdump_out < 0)
|
|
1373 report_file_error ("Unable to open dump file for writing",
|
|
1374 build_string (EMACS_PROGNAME ".dmp"));
|
442
|
1375
|
771
|
1376 retry_fwrite (&header, sizeof (header), 1, pdump_out);
|
458
|
1377 PDUMP_ALIGN_OUTPUT (max_align_t);
|
442
|
1378
|
|
1379 pdump_scan_by_alignment (pdump_dump_data);
|
|
1380
|
458
|
1381 fseek (pdump_out, header.stab_offset, SEEK_SET);
|
442
|
1382
|
458
|
1383 pdump_dump_root_struct_ptrs ();
|
452
|
1384 pdump_dump_opaques ();
|
442
|
1385 pdump_dump_rtables ();
|
458
|
1386 pdump_dump_root_objects ();
|
442
|
1387
|
771
|
1388 retry_fclose (pdump_out);
|
|
1389 retry_close (pdump_fd);
|
458
|
1390
|
442
|
1391 free (pdump_buf);
|
|
1392
|
|
1393 free (pdump_hash);
|
|
1394
|
|
1395 Vterminal_console = t_console;
|
|
1396 Vterminal_frame = t_frame;
|
|
1397 Vterminal_device = t_device;
|
|
1398 }
|
|
1399
|
452
|
1400 static int
|
|
1401 pdump_load_check (void)
|
442
|
1402 {
|
452
|
1403 return (!memcmp (((pdump_header *)pdump_start)->signature,
|
|
1404 PDUMP_SIGNATURE, PDUMP_SIGNATURE_LEN)
|
|
1405 && ((pdump_header *)pdump_start)->id == dump_id);
|
442
|
1406 }
|
|
1407
|
458
|
1408 /*----------------------------------------------------------------------*/
|
|
1409 /* Reading the dump file */
|
|
1410 /*----------------------------------------------------------------------*/
|
452
|
1411 static int
|
|
1412 pdump_load_finish (void)
|
442
|
1413 {
|
|
1414 int i;
|
|
1415 char *p;
|
|
1416 EMACS_INT delta;
|
|
1417 EMACS_INT count;
|
458
|
1418 pdump_header *header = (pdump_header *)pdump_start;
|
442
|
1419
|
|
1420 pdump_end = pdump_start + pdump_length;
|
|
1421
|
458
|
1422 delta = ((EMACS_INT)pdump_start) - header->reloc_address;
|
|
1423 p = pdump_start + header->stab_offset;
|
442
|
1424
|
452
|
1425 /* Put back the pdump_root_struct_ptrs */
|
826
|
1426 p = (char *) ALIGN_PTR (p, pdump_static_pointer);
|
458
|
1427 for (i=0; i<header->nb_root_struct_ptrs; i++)
|
442
|
1428 {
|
458
|
1429 pdump_static_pointer ptr = PDUMP_READ (p, pdump_static_pointer);
|
|
1430 (* ptr.address) = ptr.value + delta;
|
442
|
1431 }
|
|
1432
|
452
|
1433 /* Put back the pdump_opaques */
|
458
|
1434 for (i=0; i<header->nb_opaques; i++)
|
442
|
1435 {
|
458
|
1436 pdump_opaque info = PDUMP_READ_ALIGNED (p, pdump_opaque);
|
545
|
1437 memcpy ((void*)info.varaddress, p, info.size);
|
452
|
1438 p += info.size;
|
442
|
1439 }
|
|
1440
|
|
1441 /* Do the relocations */
|
|
1442 pdump_rt_list = p;
|
|
1443 count = 2;
|
|
1444 for (;;)
|
|
1445 {
|
458
|
1446 pdump_reloc_table rt = PDUMP_READ_ALIGNED (p, pdump_reloc_table);
|
826
|
1447 p = (char *) ALIGN_PTR (p, char *);
|
442
|
1448 if (rt.desc)
|
|
1449 {
|
458
|
1450 char **reloc = (char **)p;
|
442
|
1451 for (i=0; i < rt.count; i++)
|
|
1452 {
|
458
|
1453 reloc[i] += delta;
|
|
1454 pdump_reloc_one (reloc[i], delta, rt.desc);
|
442
|
1455 }
|
458
|
1456 p += rt.count * sizeof (char *);
|
442
|
1457 } else
|
|
1458 if (!(--count))
|
|
1459 break;
|
|
1460 }
|
|
1461
|
452
|
1462 /* Put the pdump_root_objects variables in place */
|
665
|
1463 i = PDUMP_READ_ALIGNED (p, Elemcount);
|
826
|
1464 p = (char *) ALIGN_PTR (p, pdump_static_Lisp_Object);
|
458
|
1465 while (i--)
|
442
|
1466 {
|
458
|
1467 pdump_static_Lisp_Object obj = PDUMP_READ (p, pdump_static_Lisp_Object);
|
442
|
1468
|
458
|
1469 if (POINTER_TYPE_P (XTYPE (obj.value)))
|
619
|
1470 obj.value = wrap_pointer_1 ((char *) XPNTR (obj.value) + delta);
|
442
|
1471
|
458
|
1472 (* obj.address) = obj.value;
|
442
|
1473 }
|
|
1474
|
|
1475 /* Final cleanups */
|
|
1476 /* reorganize hash tables */
|
|
1477 p = pdump_rt_list;
|
|
1478 for (;;)
|
|
1479 {
|
458
|
1480 pdump_reloc_table rt = PDUMP_READ_ALIGNED (p, pdump_reloc_table);
|
826
|
1481 p = (char *) ALIGN_PTR (p, Lisp_Object);
|
442
|
1482 if (!rt.desc)
|
|
1483 break;
|
|
1484 if (rt.desc == hash_table_description)
|
|
1485 {
|
|
1486 for (i=0; i < rt.count; i++)
|
|
1487 pdump_reorganize_hash_table (PDUMP_READ (p, Lisp_Object));
|
|
1488 break;
|
|
1489 } else
|
|
1490 p += sizeof (Lisp_Object) * rt.count;
|
|
1491 }
|
|
1492
|
|
1493 return 1;
|
|
1494 }
|
|
1495
|
|
1496 #ifdef WIN32_NATIVE
|
|
1497 /* Free the mapped file if we decide we don't want it after all */
|
452
|
1498 static void
|
|
1499 pdump_file_unmap (void)
|
442
|
1500 {
|
|
1501 UnmapViewOfFile (pdump_start);
|
|
1502 CloseHandle (pdump_hFile);
|
|
1503 CloseHandle (pdump_hMap);
|
|
1504 }
|
|
1505
|
452
|
1506 static int
|
|
1507 pdump_file_get (const char *path)
|
442
|
1508 {
|
|
1509
|
800
|
1510 pdump_hFile = CreateFileA (path,
|
442
|
1511 GENERIC_READ + GENERIC_WRITE, /* Required for copy on write */
|
|
1512 0, /* Not shared */
|
|
1513 NULL, /* Not inheritable */
|
|
1514 OPEN_EXISTING,
|
|
1515 FILE_ATTRIBUTE_NORMAL,
|
|
1516 NULL); /* No template file */
|
|
1517 if (pdump_hFile == INVALID_HANDLE_VALUE)
|
|
1518 return 0;
|
|
1519
|
|
1520 pdump_length = GetFileSize (pdump_hFile, NULL);
|
800
|
1521 pdump_hMap = CreateFileMappingA (pdump_hFile,
|
442
|
1522 NULL, /* No security attributes */
|
|
1523 PAGE_WRITECOPY, /* Copy on write */
|
|
1524 0, /* Max size, high half */
|
|
1525 0, /* Max size, low half */
|
|
1526 NULL); /* Unnamed */
|
|
1527 if (pdump_hMap == INVALID_HANDLE_VALUE)
|
|
1528 return 0;
|
|
1529
|
|
1530 pdump_start = MapViewOfFile (pdump_hMap,
|
452
|
1531 FILE_MAP_COPY, /* Copy on write */
|
442
|
1532 0, /* Start at zero */
|
|
1533 0,
|
|
1534 0); /* Map all of it */
|
|
1535 pdump_free = pdump_file_unmap;
|
|
1536 return 1;
|
|
1537 }
|
|
1538
|
|
1539 /* pdump_resource_free is called (via the pdump_free pointer) to release
|
|
1540 any resources allocated by pdump_resource_get. Since the Windows API
|
|
1541 specs specifically state that you don't need to (and shouldn't) free the
|
|
1542 resources allocated by FindResource, LoadResource, and LockResource this
|
|
1543 routine does nothing. */
|
452
|
1544 static void
|
|
1545 pdump_resource_free (void)
|
442
|
1546 {
|
|
1547 }
|
|
1548
|
452
|
1549 static int
|
|
1550 pdump_resource_get (void)
|
442
|
1551 {
|
452
|
1552 HRSRC hRes; /* Handle to dump resource */
|
|
1553 HRSRC hResLoad; /* Handle to loaded dump resource */
|
442
|
1554
|
|
1555 /* See Q126630 which describes how Windows NT and 95 trap writes to
|
|
1556 resource sections and duplicate the page to allow the write to proceed.
|
|
1557 It also describes how to make the resource section read/write (and hence
|
|
1558 private to each process). Doing this avoids the exceptions and related
|
|
1559 overhead, but causes the resource section to be private to each process
|
|
1560 that is running XEmacs. Since the resource section contains little
|
|
1561 other than the dumped data, which should be private to each process, we
|
|
1562 make the whole resource section read/write so we don't have to copy it. */
|
|
1563
|
800
|
1564 hRes = FindResourceA (NULL, MAKEINTRESOURCE (101), "DUMP");
|
442
|
1565 if (hRes == NULL)
|
|
1566 return 0;
|
|
1567
|
|
1568 /* Found it, use the data in the resource */
|
|
1569 hResLoad = LoadResource (NULL, hRes);
|
|
1570 if (hResLoad == NULL)
|
|
1571 return 0;
|
|
1572
|
|
1573 pdump_start = LockResource (hResLoad);
|
|
1574 if (pdump_start == NULL)
|
|
1575 return 0;
|
|
1576
|
|
1577 pdump_free = pdump_resource_free;
|
|
1578 pdump_length = SizeofResource (NULL, hRes);
|
665
|
1579 if (pdump_length <= (Bytecount) sizeof (pdump_header))
|
442
|
1580 {
|
|
1581 pdump_start = 0;
|
|
1582 return 0;
|
|
1583 }
|
|
1584
|
|
1585 return 1;
|
|
1586 }
|
|
1587
|
|
1588 #else /* !WIN32_NATIVE */
|
|
1589
|
452
|
1590 static void
|
|
1591 pdump_file_free (void)
|
442
|
1592 {
|
460
|
1593 xfree (pdump_start);
|
442
|
1594 }
|
|
1595
|
|
1596 #ifdef HAVE_MMAP
|
452
|
1597 static void
|
|
1598 pdump_file_unmap (void)
|
442
|
1599 {
|
|
1600 munmap (pdump_start, pdump_length);
|
|
1601 }
|
|
1602 #endif
|
|
1603
|
452
|
1604 static int
|
|
1605 pdump_file_get (const char *path)
|
442
|
1606 {
|
|
1607 int fd = open (path, O_RDONLY | OPEN_BINARY);
|
|
1608 if (fd<0)
|
|
1609 return 0;
|
|
1610
|
|
1611 pdump_length = lseek (fd, 0, SEEK_END);
|
665
|
1612 if (pdump_length < (Bytecount) sizeof (pdump_header))
|
442
|
1613 {
|
771
|
1614 retry_close (fd);
|
442
|
1615 return 0;
|
|
1616 }
|
|
1617
|
|
1618 lseek (fd, 0, SEEK_SET);
|
|
1619
|
|
1620 #ifdef HAVE_MMAP
|
456
|
1621 /* Unix 98 requires that sys/mman.h define MAP_FAILED,
|
|
1622 but many earlier implementations don't. */
|
|
1623 # ifndef MAP_FAILED
|
|
1624 # define MAP_FAILED ((void *) -1L)
|
|
1625 # endif
|
442
|
1626 pdump_start = (char *) mmap (0, pdump_length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
|
452
|
1627 if (pdump_start != (char *) MAP_FAILED)
|
442
|
1628 {
|
|
1629 pdump_free = pdump_file_unmap;
|
771
|
1630 retry_close (fd);
|
442
|
1631 return 1;
|
|
1632 }
|
456
|
1633 #endif /* HAVE_MMAP */
|
442
|
1634
|
460
|
1635 pdump_start = xnew_array (char, pdump_length);
|
442
|
1636 pdump_free = pdump_file_free;
|
771
|
1637 retry_read (fd, pdump_start, pdump_length);
|
442
|
1638
|
771
|
1639 retry_close (fd);
|
442
|
1640 return 1;
|
|
1641 }
|
|
1642 #endif /* !WIN32_NATIVE */
|
|
1643
|
|
1644
|
452
|
1645 static int
|
|
1646 pdump_file_try (char *exe_path)
|
442
|
1647 {
|
460
|
1648 char *w = exe_path + strlen (exe_path);
|
442
|
1649
|
|
1650 do
|
|
1651 {
|
|
1652 sprintf (w, "-%s-%08x.dmp", EMACS_VERSION, dump_id);
|
|
1653 if (pdump_file_get (exe_path))
|
|
1654 {
|
|
1655 if (pdump_load_check ())
|
|
1656 return 1;
|
452
|
1657 pdump_free ();
|
442
|
1658 }
|
|
1659
|
|
1660 sprintf (w, "-%08x.dmp", dump_id);
|
|
1661 if (pdump_file_get (exe_path))
|
|
1662 {
|
|
1663 if (pdump_load_check ())
|
|
1664 return 1;
|
452
|
1665 pdump_free ();
|
442
|
1666 }
|
|
1667
|
|
1668 sprintf (w, ".dmp");
|
|
1669 if (pdump_file_get (exe_path))
|
|
1670 {
|
|
1671 if (pdump_load_check ())
|
|
1672 return 1;
|
452
|
1673 pdump_free ();
|
442
|
1674 }
|
|
1675
|
|
1676 do
|
|
1677 w--;
|
|
1678 while (w>exe_path && !IS_DIRECTORY_SEP (*w) && (*w != '-') && (*w != '.'));
|
|
1679 }
|
|
1680 while (w>exe_path && !IS_DIRECTORY_SEP (*w));
|
|
1681 return 0;
|
|
1682 }
|
|
1683
|
452
|
1684 int
|
771
|
1685 pdump_load (const Extbyte *argv0)
|
442
|
1686 {
|
771
|
1687 Extbyte exe_path[PATH_MAX];
|
442
|
1688 #ifdef WIN32_NATIVE
|
800
|
1689 GetModuleFileNameA (NULL, exe_path, PATH_MAX);
|
442
|
1690 #else /* !WIN32_NATIVE */
|
771
|
1691 Extbyte *w;
|
|
1692 const Extbyte *dir, *p;
|
442
|
1693
|
|
1694 dir = argv0;
|
|
1695 if (dir[0] == '-')
|
|
1696 {
|
|
1697 /* XEmacs as a login shell, oh goody! */
|
771
|
1698 dir = getenv ("SHELL"); /* not egetenv -- not yet initialized */
|
442
|
1699 }
|
|
1700
|
452
|
1701 p = dir + strlen (dir);
|
442
|
1702 while (p != dir && !IS_ANY_SEP (p[-1])) p--;
|
|
1703
|
|
1704 if (p != dir)
|
|
1705 {
|
|
1706 /* invocation-name includes a directory component -- presumably it
|
|
1707 is relative to cwd, not $PATH */
|
|
1708 strcpy (exe_path, dir);
|
|
1709 }
|
|
1710 else
|
|
1711 {
|
771
|
1712 const Extbyte *path = getenv ("PATH"); /* not egetenv -- not yet init. */
|
|
1713 const Extbyte *name = p;
|
442
|
1714 for (;;)
|
|
1715 {
|
|
1716 p = path;
|
|
1717 while (*p && *p != SEPCHAR)
|
|
1718 p++;
|
|
1719 if (p == path)
|
|
1720 {
|
|
1721 exe_path[0] = '.';
|
|
1722 w = exe_path + 1;
|
|
1723 }
|
|
1724 else
|
|
1725 {
|
|
1726 memcpy (exe_path, path, p - path);
|
|
1727 w = exe_path + (p - path);
|
|
1728 }
|
|
1729 if (!IS_DIRECTORY_SEP (w[-1]))
|
|
1730 {
|
|
1731 *w++ = '/';
|
|
1732 }
|
452
|
1733 strcpy (w, name);
|
442
|
1734
|
|
1735 if (!access (exe_path, X_OK))
|
|
1736 break;
|
|
1737 if (!*p)
|
|
1738 {
|
|
1739 /* Oh well, let's have some kind of default */
|
|
1740 sprintf (exe_path, "./%s", name);
|
|
1741 break;
|
|
1742 }
|
|
1743 path = p+1;
|
|
1744 }
|
|
1745 }
|
|
1746 #endif /* WIN32_NATIVE */
|
|
1747
|
|
1748 if (pdump_file_try (exe_path))
|
|
1749 {
|
|
1750 pdump_load_finish ();
|
|
1751 return 1;
|
|
1752 }
|
|
1753
|
|
1754 #ifdef WIN32_NATIVE
|
|
1755 if (pdump_resource_get ())
|
|
1756 {
|
|
1757 if (pdump_load_check ())
|
|
1758 {
|
|
1759 pdump_load_finish ();
|
|
1760 return 1;
|
|
1761 }
|
|
1762 pdump_free ();
|
|
1763 }
|
|
1764 #endif
|
|
1765
|
|
1766 return 0;
|
|
1767 }
|