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
|
934
|
594 #ifdef USE_KKCC
|
|
595 if (imp->description
|
|
596 && RECORD_DUMPABLE(objh))
|
|
597 #else /* not USE_KKCC */
|
458
|
598 if (imp->description)
|
934
|
599 #endif /* not USE_KKCC */
|
442
|
600 {
|
|
601 int me = depth++;
|
771
|
602 if (me > 65536)
|
442
|
603 {
|
|
604 stderr_out ("Backtrace overflow, loop ?\n");
|
|
605 abort ();
|
|
606 }
|
|
607 backtrace[me].obj = objh;
|
|
608 backtrace[me].position = 0;
|
|
609 backtrace[me].offset = 0;
|
|
610
|
|
611 pdump_add_entry (pdump_object_table + objh->type,
|
|
612 objh,
|
458
|
613 imp->static_size ?
|
|
614 imp->static_size :
|
|
615 imp->size_in_bytes_method (objh),
|
442
|
616 1);
|
458
|
617 pdump_register_sub (objh, imp->description, me);
|
442
|
618 --depth;
|
|
619 }
|
|
620 else
|
|
621 {
|
|
622 pdump_alert_undump_object[objh->type]++;
|
458
|
623 stderr_out ("Undumpable object type : %s\n", imp->name);
|
442
|
624 pdump_backtrace ();
|
|
625 }
|
|
626 }
|
|
627
|
771
|
628 /* Return the size of the memory block (NOT necessarily a structure!)
|
|
629 described by SDESC and pointed to by OBJ. If SDESC records an
|
|
630 explicit size (i.e. non-zero), it is simply returned; otherwise,
|
|
631 the size is calculated by the maximum offset and the size of the
|
|
632 object at that offset, rounded up to the maximum alignment. In
|
|
633 this case, we may need the object, for example when retrieving an
|
|
634 "indirect count" of an inlined array (the count is not constant,
|
|
635 but is specified by one of the elements of the memory block). (It
|
|
636 is generally not a problem if we return an overly large size -- we
|
|
637 will simply end up reserving more space than necessary; but if the
|
|
638 size is too small we could be in serious trouble, in particular
|
|
639 with nested inlined structures, where there may be alignment
|
|
640 padding in the middle of a block. #### In fact there is an (at
|
|
641 least theoretical) problem with an overly large size -- we may
|
|
642 trigger a protection fault when reading from invalid memory. We
|
|
643 need to handle this -- perhaps in a stupid but dependable way,
|
|
644 i.e. by trapping SIGSEGV and SIGBUS.) */
|
|
645
|
|
646 static Bytecount
|
|
647 pdump_structure_size (const void *obj, const struct struct_description *sdesc)
|
|
648 {
|
|
649 int max_offset = -1;
|
|
650 int max_offset_pos = -1;
|
|
651 int size_at_max = 0;
|
|
652 int pos;
|
|
653 const struct lrecord_description *desc;
|
|
654 void *rdata;
|
|
655
|
|
656 if (sdesc->size)
|
|
657 return sdesc->size;
|
|
658
|
|
659 desc = sdesc->description;
|
|
660
|
|
661 for (pos = 0; desc[pos].type != XD_END; pos++)
|
|
662 {
|
|
663 if (desc[pos].offset == max_offset)
|
|
664 {
|
|
665 stderr_out ("Two relocatable elements at same offset?\n");
|
|
666 abort ();
|
|
667 }
|
|
668 else if (desc[pos].offset > max_offset)
|
|
669 {
|
|
670 max_offset = desc[pos].offset;
|
|
671 max_offset_pos = pos;
|
|
672 }
|
|
673 }
|
|
674
|
|
675 if (max_offset_pos < 0)
|
|
676 return 0;
|
|
677
|
|
678 pos = max_offset_pos;
|
|
679 rdata = (char *) obj + desc[pos].offset;
|
|
680
|
|
681 switch (desc[pos].type)
|
|
682 {
|
|
683 case XD_LISP_OBJECT_ARRAY:
|
|
684 {
|
|
685 EMACS_INT val = desc[pos].data1;
|
|
686 if (XD_IS_INDIRECT (val))
|
|
687 val = pdump_get_indirect_count (val, desc, obj);
|
|
688 size_at_max = val * sizeof (Lisp_Object);
|
|
689 break;
|
|
690 }
|
|
691 case XD_LISP_OBJECT:
|
|
692 case XD_LO_LINK:
|
|
693 size_at_max = sizeof (Lisp_Object);
|
|
694 break;
|
|
695 case XD_OPAQUE_PTR:
|
|
696 size_at_max = sizeof (void *);
|
|
697 break;
|
|
698 case XD_STRUCT_PTR:
|
|
699 {
|
|
700 EMACS_INT val = desc[pos].data1;
|
|
701 if (XD_IS_INDIRECT (val))
|
|
702 val = pdump_get_indirect_count (val, desc, obj);
|
|
703 size_at_max = val * sizeof (void *);
|
|
704 break;
|
|
705 }
|
|
706 break;
|
|
707 case XD_STRUCT_ARRAY:
|
|
708 {
|
|
709 EMACS_INT val = desc[pos].data1;
|
|
710
|
|
711 if (XD_IS_INDIRECT (val))
|
|
712 val = pdump_get_indirect_count (val, desc, obj);
|
|
713
|
|
714 size_at_max = val * pdump_structure_size (rdata, desc[pos].data2);
|
|
715 break;
|
|
716 }
|
|
717 break;
|
|
718 case XD_OPAQUE_DATA_PTR:
|
|
719 size_at_max = sizeof (void *);
|
|
720 break;
|
|
721 case XD_UNION:
|
|
722 abort (); /* #### IMPLEMENT ME! NEEDED FOR UNICODE
|
|
723 SUPPORT */
|
|
724 break;
|
|
725 case XD_C_STRING:
|
|
726 size_at_max = sizeof (void *);
|
|
727 break;
|
|
728 case XD_DOC_STRING:
|
|
729 size_at_max = sizeof (void *);
|
|
730 break;
|
|
731 case XD_INT_RESET:
|
|
732 size_at_max = sizeof (int);
|
|
733 break;
|
|
734 case XD_BYTECOUNT:
|
|
735 size_at_max = sizeof (Bytecount);
|
|
736 break;
|
|
737 case XD_ELEMCOUNT:
|
|
738 size_at_max = sizeof (Elemcount);
|
|
739 break;
|
|
740 case XD_HASHCODE:
|
|
741 size_at_max = sizeof (Hashcode);
|
|
742 break;
|
|
743 case XD_INT:
|
|
744 size_at_max = sizeof (int);
|
|
745 break;
|
|
746 case XD_LONG:
|
|
747 size_at_max = sizeof (long);
|
|
748 break;
|
|
749 case XD_SPECIFIER_END:
|
|
750 case XD_CODING_SYSTEM_END:
|
|
751 stderr_out
|
|
752 ("Should not be seeing XD_SPECIFIER_END or\n"
|
|
753 "XD_CODING_SYSTEM_END outside of struct Lisp_Specifier\n"
|
|
754 "and struct Lisp_Coding_System.\n");
|
|
755 abort ();
|
|
756 default:
|
|
757 stderr_out ("Unsupported dump type : %d\n", desc[pos].type);
|
|
758 abort ();
|
|
759 }
|
|
760
|
|
761 /* We have no way of knowing the required alignment for this structure,
|
|
762 so just max it maximally aligned. */
|
826
|
763 return MAX_ALIGN_SIZE (max_offset + size_at_max);
|
771
|
764 }
|
|
765
|
|
766 /* Register the referenced objects in the array of COUNT objects of
|
|
767 located at DATA; each object is described by SDESC. "Object" here
|
|
768 simply means any block of memory; it need not actually be a C
|
|
769 "struct". It could be a single integer or Lisp_Object, for
|
|
770 example, as long as the description is accurate.
|
|
771
|
|
772 This does not register the block of memory itself; it may, for
|
|
773 example, be an array of structures inlined in another memory block
|
|
774 and thus should not be registered. See pdump_register_struct(),
|
|
775 which does register the memory block. */
|
|
776
|
|
777 static void
|
|
778 pdump_register_struct_contents (const void *data,
|
|
779 const struct struct_description *sdesc,
|
|
780 int count)
|
|
781
|
|
782 {
|
|
783 int me = depth++;
|
|
784 int i;
|
|
785 Bytecount elsize;
|
|
786
|
|
787 if (me>65536)
|
|
788 {
|
|
789 stderr_out ("Backtrace overflow, loop ?\n");
|
|
790 abort ();
|
|
791 }
|
|
792 backtrace[me].obj = 0;
|
|
793 backtrace[me].position = 0;
|
|
794 backtrace[me].offset = 0;
|
|
795
|
|
796 elsize = pdump_structure_size (data, sdesc);
|
|
797
|
|
798 for (i = 0; i < count; i++)
|
|
799 {
|
|
800 pdump_register_sub (((char *) data) + elsize * i,
|
|
801 sdesc->description,
|
|
802 me);
|
|
803 }
|
|
804 --depth;
|
|
805 }
|
|
806
|
|
807 /* Register the array of COUNT objects of located at DATA; each object is
|
|
808 described by SDESC. "Object" here simply means any block of memory;
|
|
809 it need not actually be a C "struct". It could be a single integer
|
|
810 or Lisp_Object, for example, as long as the description is accurate.
|
|
811
|
|
812 This is like pdump_register_struct_contents() but also registers
|
|
813 the memory block itself. */
|
|
814
|
442
|
815 static void
|
458
|
816 pdump_register_struct (const void *data,
|
|
817 const struct struct_description *sdesc,
|
|
818 int count)
|
442
|
819 {
|
|
820 if (data && !pdump_get_entry (data))
|
|
821 {
|
771
|
822 pdump_add_entry (pdump_get_entry_list (sdesc), data,
|
|
823 pdump_structure_size (data, sdesc), count);
|
442
|
824
|
771
|
825 pdump_register_struct_contents (data, sdesc, count);
|
442
|
826 }
|
|
827 }
|
|
828
|
771
|
829 /* Store the already-calculated new pointer offsets for all pointers
|
|
830 in the COUNT contiguous blocks of memory, each described by DESC
|
|
831 and of size SIZE, whose original is located at ORIG_DATA and the
|
|
832 modifiable copy at DATA.
|
|
833
|
|
834 This is done just before writing the modified block of memory to
|
|
835 the dump file. The new pointer offsets have been carefully
|
|
836 calculated so that the data being pointed gets written at that
|
|
837 offset in the dump file. That way, the dump file is a correct
|
|
838 memory image except perhaps for a constant that needs to be added
|
|
839 to all pointers. (#### In fact, we SHOULD be starting up a dumped
|
|
840 XEmacs, seeing where the dumped file gets loaded into memory, and
|
|
841 then rewriting the dumped file after relocating all the pointers
|
|
842 relative to this memory location. That way, if the file gets
|
|
843 loaded again at the same location, which will be common, we don't
|
|
844 have to do any relocating, which is both faster at startup and
|
|
845 allows the read-only part of the dumped data to be shared read-only
|
|
846 between different invocations of XEmacs.)
|
|
847
|
|
848 #### Do we distinguish between read-only and writable dumped data?
|
|
849 Should we? It's tricky because the dumped data, once loaded again,
|
|
850 cannot really be free()d or garbage collected since it's all stored
|
|
851 in one contiguous block of data with no malloc() headers, and we
|
|
852 don't keep track of the pointers used internally in malloc() and
|
|
853 the Lisp allocator to track allocated blocks of memory. */
|
|
854
|
|
855 static void
|
|
856 pdump_store_new_pointer_offsets (int count, void *data, const void *orig_data,
|
|
857 const struct lrecord_description *desc,
|
|
858 int size)
|
|
859 {
|
|
860 int pos, i;
|
|
861 /* Process each block one by one */
|
|
862 for (i = 0; i < count; i++)
|
|
863 {
|
|
864 /* CUR points to the beginning of each block in the new data. */
|
|
865 char *cur = ((char *)data) + i*size;
|
|
866 restart:
|
|
867 /* Scan each line of the description for relocatable pointers */
|
|
868 for (pos = 0; desc[pos].type != XD_END; pos++)
|
|
869 {
|
|
870 /* RDATA points to the beginning of each element in the new data. */
|
|
871 void *rdata = cur + desc[pos].offset;
|
|
872 switch (desc[pos].type)
|
|
873 {
|
|
874 case XD_SPECIFIER_END:
|
|
875 desc = ((const Lisp_Specifier *)(orig_data))->
|
|
876 methods->extra_description;
|
|
877 goto restart;
|
|
878 case XD_CODING_SYSTEM_END:
|
|
879 desc = ((const Lisp_Coding_System *)(orig_data))->
|
|
880 methods->extra_description;
|
|
881 goto restart;
|
|
882 case XD_BYTECOUNT:
|
|
883 case XD_ELEMCOUNT:
|
|
884 case XD_HASHCODE:
|
|
885 case XD_INT:
|
|
886 case XD_LONG:
|
|
887 break;
|
|
888 case XD_INT_RESET:
|
|
889 {
|
|
890 EMACS_INT val = desc[pos].data1;
|
|
891 if (XD_IS_INDIRECT (val))
|
|
892 val = pdump_get_indirect_count (val, desc, orig_data);
|
|
893 * (int *) rdata = val;
|
|
894 break;
|
|
895 }
|
|
896 case XD_OPAQUE_DATA_PTR:
|
|
897 case XD_C_STRING:
|
|
898 case XD_STRUCT_PTR:
|
|
899 {
|
|
900 void *ptr = * (void **) rdata;
|
|
901 if (ptr)
|
|
902 * (EMACS_INT *) rdata = pdump_get_entry (ptr)->save_offset;
|
|
903 break;
|
|
904 }
|
|
905 case XD_LO_LINK:
|
|
906 {
|
|
907 /* As described in lrecord.h, this is a weak link.
|
|
908 Thus, we need to link this object not (necessarily)
|
|
909 to the object directly pointed to, but to the next
|
|
910 referenced object in the chain. None of the
|
|
911 intermediate objects will be written out, so we
|
|
912 traverse down the chain of objects until we find a
|
|
913 referenced one. (The Qnil or Qunbound that ends the
|
|
914 chain will always be a referenced object.) */
|
|
915 Lisp_Object obj = * (Lisp_Object *) rdata;
|
|
916 pdump_entry_list_elt *elt1;
|
|
917 for (;;)
|
|
918 {
|
|
919 elt1 = pdump_get_entry (XRECORD_LHEADER (obj));
|
|
920 if (elt1)
|
|
921 break;
|
|
922 obj = * (Lisp_Object *) (desc[pos].offset +
|
|
923 (char *)(XRECORD_LHEADER (obj)));
|
|
924 }
|
|
925 * (EMACS_INT *) rdata = elt1->save_offset;
|
|
926 break;
|
|
927 }
|
|
928 case XD_LISP_OBJECT:
|
|
929 {
|
|
930 Lisp_Object *pobj = (Lisp_Object *) rdata;
|
|
931
|
|
932 assert (desc[pos].data1 == 0);
|
|
933
|
|
934 if (POINTER_TYPE_P (XTYPE (*pobj)) && XRECORD_LHEADER (*pobj))
|
|
935 * (EMACS_INT *) pobj =
|
|
936 pdump_get_entry (XRECORD_LHEADER (*pobj))->save_offset;
|
|
937 break;
|
|
938 }
|
|
939 case XD_LISP_OBJECT_ARRAY:
|
|
940 {
|
|
941 EMACS_INT num = desc[pos].data1;
|
|
942 int j;
|
|
943 if (XD_IS_INDIRECT (num))
|
|
944 num = pdump_get_indirect_count (num, desc, orig_data);
|
|
945
|
|
946 for (j = 0; j < num; j++)
|
|
947 {
|
|
948 Lisp_Object *pobj = ((Lisp_Object *) rdata) + j;
|
|
949 if (POINTER_TYPE_P (XTYPE (*pobj)) &&
|
|
950 XRECORD_LHEADER (*pobj))
|
|
951 * (EMACS_INT *) pobj =
|
|
952 pdump_get_entry (XRECORD_LHEADER (*pobj))->save_offset;
|
|
953 }
|
|
954 break;
|
|
955 }
|
|
956 case XD_DOC_STRING:
|
|
957 {
|
|
958 EMACS_INT str = *(EMACS_INT *)rdata;
|
|
959 if (str > 0)
|
|
960 * (EMACS_INT *) rdata =
|
|
961 pdump_get_entry ((void *)str)->save_offset;
|
|
962 break;
|
|
963 }
|
|
964 case XD_STRUCT_ARRAY:
|
|
965 {
|
|
966 EMACS_INT num = desc[pos].data1;
|
|
967 if (XD_IS_INDIRECT (num))
|
|
968 num = pdump_get_indirect_count (num, desc, orig_data);
|
|
969
|
|
970 pdump_store_new_pointer_offsets
|
|
971 (num, rdata,
|
|
972 ((char *) rdata - (char *) data) + (char *) orig_data,
|
|
973 desc[pos].data2->description,
|
|
974 pdump_structure_size
|
|
975 (((char *) rdata - (char *) data) + (char *) orig_data,
|
|
976 desc[pos].data2));
|
|
977 break;
|
|
978 }
|
|
979 case XD_UNION:
|
|
980 abort (); /* #### IMPLEMENT ME! NEEDED FOR UNICODE
|
|
981 SUPPORT */
|
|
982
|
|
983 default:
|
|
984 stderr_out ("Unsupported dump type : %d\n", desc[pos].type);
|
|
985 abort ();
|
|
986 }
|
|
987 }
|
|
988 }
|
|
989 }
|
|
990
|
|
991 /* Write out to global file descriptor PDUMP_OUT the element (one or
|
|
992 more contiguous blocks of identical size/description) recorded in
|
|
993 ELT and described by DESC. The element is first copied to a buffer
|
|
994 and then all pointers (this includes Lisp_Objects other than
|
|
995 integer/character) are relocated to the (pre-computed) offset in
|
|
996 the dump file. */
|
|
997
|
442
|
998 static void
|
460
|
999 pdump_dump_data (pdump_entry_list_elt *elt,
|
458
|
1000 const struct lrecord_description *desc)
|
442
|
1001 {
|
665
|
1002 Bytecount size = elt->size;
|
460
|
1003 int count = elt->count;
|
442
|
1004 if (desc)
|
|
1005 {
|
771
|
1006 /* Copy to temporary buffer */
|
460
|
1007 memcpy (pdump_buf, elt->obj, size*count);
|
442
|
1008
|
771
|
1009 /* Store new offsets into all pointers in block */
|
|
1010 pdump_store_new_pointer_offsets (count, pdump_buf, elt->obj, desc, size);
|
|
1011 }
|
|
1012 retry_fwrite (desc ? pdump_buf : elt->obj, size, count, pdump_out);
|
|
1013 }
|
442
|
1014
|
771
|
1015 /* Relocate a single memory block at DATA, described by DESC, from its
|
|
1016 assumed load location to its actual one by adding DELTA to all
|
|
1017 pointers in the block. Does not recursively relocate any other
|
|
1018 memory blocks pointed to. (We already have a list of all memory
|
|
1019 blocks in the dump file.) */
|
442
|
1020
|
|
1021 static void
|
458
|
1022 pdump_reloc_one (void *data, EMACS_INT delta,
|
|
1023 const struct lrecord_description *desc)
|
442
|
1024 {
|
|
1025 int pos;
|
|
1026
|
|
1027 restart:
|
|
1028 for (pos = 0; desc[pos].type != XD_END; pos++)
|
|
1029 {
|
|
1030 void *rdata = (char *)data + desc[pos].offset;
|
|
1031 switch (desc[pos].type)
|
|
1032 {
|
|
1033 case XD_SPECIFIER_END:
|
|
1034 pos = 0;
|
|
1035 desc = ((const Lisp_Specifier *)data)->methods->extra_description;
|
|
1036 goto restart;
|
771
|
1037 case XD_CODING_SYSTEM_END:
|
|
1038 pos = 0;
|
|
1039 desc =
|
|
1040 ((const Lisp_Coding_System *)data)->methods->extra_description;
|
|
1041 goto restart;
|
665
|
1042 case XD_BYTECOUNT:
|
|
1043 case XD_ELEMCOUNT:
|
|
1044 case XD_HASHCODE:
|
442
|
1045 case XD_INT:
|
|
1046 case XD_LONG:
|
|
1047 case XD_INT_RESET:
|
|
1048 break;
|
|
1049 case XD_OPAQUE_DATA_PTR:
|
|
1050 case XD_C_STRING:
|
|
1051 case XD_STRUCT_PTR:
|
|
1052 case XD_LO_LINK:
|
|
1053 {
|
|
1054 EMACS_INT ptr = *(EMACS_INT *)rdata;
|
|
1055 if (ptr)
|
|
1056 *(EMACS_INT *)rdata = ptr+delta;
|
|
1057 break;
|
|
1058 }
|
|
1059 case XD_LISP_OBJECT:
|
|
1060 {
|
|
1061 Lisp_Object *pobj = (Lisp_Object *) rdata;
|
|
1062
|
|
1063 assert (desc[pos].data1 == 0);
|
|
1064
|
|
1065 if (POINTER_TYPE_P (XTYPE (*pobj))
|
|
1066 && ! EQ (*pobj, Qnull_pointer))
|
793
|
1067 *pobj = wrap_pointer_1 ((char *) XPNTR (*pobj) + delta);
|
442
|
1068
|
|
1069 break;
|
|
1070 }
|
|
1071 case XD_LISP_OBJECT_ARRAY:
|
|
1072 {
|
|
1073 EMACS_INT num = desc[pos].data1;
|
|
1074 int j;
|
|
1075 if (XD_IS_INDIRECT (num))
|
|
1076 num = pdump_get_indirect_count (num, desc, data);
|
|
1077
|
|
1078 for (j=0; j<num; j++)
|
|
1079 {
|
|
1080 Lisp_Object *pobj = (Lisp_Object *) rdata + j;
|
|
1081
|
|
1082 if (POINTER_TYPE_P (XTYPE (*pobj))
|
|
1083 && ! EQ (*pobj, Qnull_pointer))
|
793
|
1084 *pobj = wrap_pointer_1 ((char *) XPNTR (*pobj) + delta);
|
442
|
1085 }
|
|
1086 break;
|
|
1087 }
|
|
1088 case XD_DOC_STRING:
|
|
1089 {
|
|
1090 EMACS_INT str = *(EMACS_INT *)rdata;
|
|
1091 if (str > 0)
|
|
1092 *(EMACS_INT *)rdata = str + delta;
|
|
1093 break;
|
|
1094 }
|
771
|
1095 case XD_STRUCT_ARRAY:
|
|
1096 {
|
|
1097 EMACS_INT num = desc[pos].data1;
|
|
1098 int j;
|
|
1099 const struct struct_description *sdesc = desc[pos].data2;
|
|
1100 Bytecount size = pdump_structure_size (rdata, sdesc);
|
|
1101
|
|
1102 if (XD_IS_INDIRECT (num))
|
|
1103 num = pdump_get_indirect_count (num, desc, data);
|
|
1104 /* Note: We are recursing over data in the block itself */
|
|
1105 for (j = 0; j < num; j++)
|
|
1106 pdump_reloc_one ((char *) rdata + j * size, delta,
|
|
1107 sdesc->description);
|
|
1108
|
|
1109 break;
|
|
1110 }
|
|
1111
|
|
1112 case XD_UNION:
|
|
1113 abort (); /* #### IMPLEMENT ME! NEEDED FOR UNICODE SUPPORT */
|
442
|
1114 default:
|
|
1115 stderr_out ("Unsupported dump type : %d\n", desc[pos].type);
|
|
1116 abort ();
|
|
1117 };
|
|
1118 }
|
|
1119 }
|
|
1120
|
|
1121 static void
|
460
|
1122 pdump_allocate_offset (pdump_entry_list_elt *elt,
|
458
|
1123 const struct lrecord_description *desc)
|
442
|
1124 {
|
665
|
1125 Bytecount size = elt->count * elt->size;
|
460
|
1126 elt->save_offset = cur_offset;
|
442
|
1127 if (size>max_size)
|
|
1128 max_size = size;
|
|
1129 cur_offset += size;
|
|
1130 }
|
|
1131
|
|
1132 static void
|
460
|
1133 pdump_scan_by_alignment (void (*f)(pdump_entry_list_elt *,
|
458
|
1134 const struct lrecord_description *))
|
442
|
1135 {
|
460
|
1136 int align;
|
|
1137
|
|
1138 for (align = ALIGNOF (max_align_t); align; align>>=1)
|
442
|
1139 {
|
460
|
1140 int i;
|
|
1141 pdump_entry_list_elt *elt;
|
|
1142
|
442
|
1143 for (i=0; i<lrecord_type_count; i++)
|
|
1144 if (pdump_object_table[i].align == align)
|
460
|
1145 for (elt = pdump_object_table[i].first; elt; elt = elt->next)
|
|
1146 f (elt, lrecord_implementations_table[i]->description);
|
442
|
1147
|
|
1148 for (i=0; i<pdump_struct_table.count; i++)
|
460
|
1149 {
|
|
1150 pdump_struct_list_elt list = pdump_struct_table.list[i];
|
|
1151 if (list.list.align == align)
|
|
1152 for (elt = list.list.first; elt; elt = elt->next)
|
|
1153 f (elt, list.sdesc->description);
|
|
1154 }
|
442
|
1155
|
460
|
1156 for (elt = pdump_opaque_data_list.first; elt; elt = elt->next)
|
|
1157 if (pdump_size_to_align (elt->size) == align)
|
|
1158 f (elt, 0);
|
442
|
1159 }
|
|
1160 }
|
|
1161
|
|
1162 static void
|
458
|
1163 pdump_dump_root_struct_ptrs (void)
|
442
|
1164 {
|
|
1165 int i;
|
665
|
1166 Elemcount count = Dynarr_length (pdump_root_struct_ptrs);
|
458
|
1167 pdump_static_pointer *data = alloca_array (pdump_static_pointer, count);
|
|
1168 for (i = 0; i < count; i++)
|
442
|
1169 {
|
458
|
1170 data[i].address = (char **) Dynarr_atp (pdump_root_struct_ptrs, i)->ptraddress;
|
|
1171 data[i].value = (char *) pdump_get_entry (* data[i].address)->save_offset;
|
442
|
1172 }
|
458
|
1173 PDUMP_ALIGN_OUTPUT (pdump_static_pointer);
|
771
|
1174 retry_fwrite (data, sizeof (pdump_static_pointer), count, pdump_out);
|
442
|
1175 }
|
|
1176
|
|
1177 static void
|
452
|
1178 pdump_dump_opaques (void)
|
442
|
1179 {
|
|
1180 int i;
|
452
|
1181 for (i = 0; i < Dynarr_length (pdump_opaques); i++)
|
442
|
1182 {
|
452
|
1183 pdump_opaque *info = Dynarr_atp (pdump_opaques, i);
|
458
|
1184 PDUMP_WRITE_ALIGNED (pdump_opaque, *info);
|
771
|
1185 retry_fwrite (info->varaddress, info->size, 1, pdump_out);
|
442
|
1186 }
|
|
1187 }
|
|
1188
|
|
1189 static void
|
|
1190 pdump_dump_rtables (void)
|
|
1191 {
|
452
|
1192 int i;
|
460
|
1193 pdump_entry_list_elt *elt;
|
442
|
1194 pdump_reloc_table rt;
|
|
1195
|
|
1196 for (i=0; i<lrecord_type_count; i++)
|
|
1197 {
|
460
|
1198 elt = pdump_object_table[i].first;
|
|
1199 if (!elt)
|
442
|
1200 continue;
|
|
1201 rt.desc = lrecord_implementations_table[i]->description;
|
|
1202 rt.count = pdump_object_table[i].count;
|
458
|
1203 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
460
|
1204 while (elt)
|
442
|
1205 {
|
460
|
1206 EMACS_INT rdata = pdump_get_entry (elt->obj)->save_offset;
|
458
|
1207 PDUMP_WRITE_ALIGNED (EMACS_INT, rdata);
|
460
|
1208 elt = elt->next;
|
442
|
1209 }
|
|
1210 }
|
|
1211
|
|
1212 rt.desc = 0;
|
|
1213 rt.count = 0;
|
458
|
1214 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
442
|
1215
|
|
1216 for (i=0; i<pdump_struct_table.count; i++)
|
|
1217 {
|
460
|
1218 elt = pdump_struct_table.list[i].list.first;
|
442
|
1219 rt.desc = pdump_struct_table.list[i].sdesc->description;
|
|
1220 rt.count = pdump_struct_table.list[i].list.count;
|
458
|
1221 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
460
|
1222 while (elt)
|
442
|
1223 {
|
460
|
1224 EMACS_INT rdata = pdump_get_entry (elt->obj)->save_offset;
|
452
|
1225 int j;
|
460
|
1226 for (j=0; j<elt->count; j++)
|
442
|
1227 {
|
458
|
1228 PDUMP_WRITE_ALIGNED (EMACS_INT, rdata);
|
460
|
1229 rdata += elt->size;
|
442
|
1230 }
|
460
|
1231 elt = elt->next;
|
442
|
1232 }
|
|
1233 }
|
|
1234 rt.desc = 0;
|
|
1235 rt.count = 0;
|
458
|
1236 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
442
|
1237 }
|
|
1238
|
|
1239 static void
|
458
|
1240 pdump_dump_root_objects (void)
|
442
|
1241 {
|
665
|
1242 Elemcount count = (Dynarr_length (pdump_root_objects) +
|
647
|
1243 Dynarr_length (pdump_weak_object_chains));
|
665
|
1244 Elemcount i;
|
442
|
1245
|
665
|
1246 PDUMP_WRITE_ALIGNED (Elemcount, count);
|
458
|
1247 PDUMP_ALIGN_OUTPUT (pdump_static_Lisp_Object);
|
442
|
1248
|
647
|
1249 for (i = 0; i < Dynarr_length (pdump_root_objects); i++)
|
442
|
1250 {
|
458
|
1251 pdump_static_Lisp_Object obj;
|
|
1252 obj.address = Dynarr_at (pdump_root_objects, i);
|
|
1253 obj.value = * obj.address;
|
460
|
1254
|
458
|
1255 if (POINTER_TYPE_P (XTYPE (obj.value)))
|
619
|
1256 obj.value =
|
|
1257 wrap_pointer_1 ((void *) pdump_get_entry (XRECORD_LHEADER
|
617
|
1258 (obj.value))->save_offset);
|
460
|
1259
|
458
|
1260 PDUMP_WRITE (pdump_static_Lisp_Object, obj);
|
442
|
1261 }
|
|
1262
|
452
|
1263 for (i=0; i<Dynarr_length (pdump_weak_object_chains); i++)
|
442
|
1264 {
|
460
|
1265 pdump_entry_list_elt *elt;
|
458
|
1266 pdump_static_Lisp_Object obj;
|
442
|
1267
|
458
|
1268 obj.address = Dynarr_at (pdump_weak_object_chains, i);
|
|
1269 obj.value = * obj.address;
|
460
|
1270
|
442
|
1271 for (;;)
|
|
1272 {
|
|
1273 const struct lrecord_description *desc;
|
|
1274 int pos;
|
460
|
1275 elt = pdump_get_entry (XRECORD_LHEADER (obj.value));
|
|
1276 if (elt)
|
442
|
1277 break;
|
458
|
1278 desc = XRECORD_LHEADER_IMPLEMENTATION (obj.value)->description;
|
442
|
1279 for (pos = 0; desc[pos].type != XD_LO_LINK; pos++)
|
|
1280 assert (desc[pos].type != XD_END);
|
|
1281
|
458
|
1282 obj.value = *(Lisp_Object *)(desc[pos].offset + (char *)(XRECORD_LHEADER (obj.value)));
|
442
|
1283 }
|
619
|
1284 obj.value = wrap_pointer_1 ((void *) elt->save_offset);
|
442
|
1285
|
458
|
1286 PDUMP_WRITE (pdump_static_Lisp_Object, obj);
|
442
|
1287 }
|
|
1288 }
|
|
1289
|
|
1290 void
|
|
1291 pdump (void)
|
|
1292 {
|
|
1293 int i;
|
|
1294 Lisp_Object t_console, t_device, t_frame;
|
|
1295 int none;
|
458
|
1296 pdump_header header;
|
442
|
1297
|
460
|
1298 pdump_object_table = xnew_array (pdump_entry_list, lrecord_type_count);
|
|
1299 pdump_alert_undump_object = xnew_array (int, lrecord_type_count);
|
|
1300
|
|
1301 assert (ALIGNOF (max_align_t) <= pdump_align_table[0]);
|
|
1302
|
|
1303 for (i = 0; i < countof (pdump_align_table); i++)
|
|
1304 if (pdump_align_table[i] > ALIGNOF (max_align_t))
|
|
1305 pdump_align_table[i] = ALIGNOF (max_align_t);
|
|
1306
|
446
|
1307 flush_all_buffer_local_cache ();
|
|
1308
|
442
|
1309 /* These appear in a DEFVAR_LISP, which does a staticpro() */
|
452
|
1310 t_console = Vterminal_console; Vterminal_console = Qnil;
|
|
1311 t_frame = Vterminal_frame; Vterminal_frame = Qnil;
|
|
1312 t_device = Vterminal_device; Vterminal_device = Qnil;
|
442
|
1313
|
452
|
1314 dump_add_opaque (&lrecord_implementations_table,
|
|
1315 lrecord_type_count * sizeof (lrecord_implementations_table[0]));
|
|
1316 dump_add_opaque (&lrecord_markers,
|
|
1317 lrecord_type_count * sizeof (lrecord_markers[0]));
|
442
|
1318
|
460
|
1319 pdump_hash = xnew_array_and_zero (pdump_entry_list_elt *, PDUMP_HASHSIZE);
|
442
|
1320
|
|
1321 for (i=0; i<lrecord_type_count; i++)
|
|
1322 {
|
|
1323 pdump_object_table[i].first = 0;
|
460
|
1324 pdump_object_table[i].align = ALIGNOF (max_align_t);
|
442
|
1325 pdump_object_table[i].count = 0;
|
|
1326 pdump_alert_undump_object[i] = 0;
|
|
1327 }
|
|
1328 pdump_struct_table.count = 0;
|
|
1329 pdump_struct_table.size = -1;
|
|
1330
|
|
1331 pdump_opaque_data_list.first = 0;
|
460
|
1332 pdump_opaque_data_list.align = ALIGNOF (max_align_t);
|
442
|
1333 pdump_opaque_data_list.count = 0;
|
|
1334 depth = 0;
|
|
1335
|
452
|
1336 for (i=0; i<Dynarr_length (pdump_root_objects); i++)
|
|
1337 pdump_register_object (* Dynarr_at (pdump_root_objects, i));
|
442
|
1338
|
|
1339 none = 1;
|
|
1340 for (i=0; i<lrecord_type_count; i++)
|
|
1341 if (pdump_alert_undump_object[i])
|
|
1342 {
|
|
1343 if (none)
|
|
1344 printf ("Undumpable types list :\n");
|
|
1345 none = 0;
|
|
1346 printf (" - %s (%d)\n", lrecord_implementations_table[i]->name, pdump_alert_undump_object[i]);
|
|
1347 }
|
|
1348 if (!none)
|
|
1349 return;
|
|
1350
|
452
|
1351 for (i=0; i<Dynarr_length (pdump_root_struct_ptrs); i++)
|
|
1352 {
|
|
1353 pdump_root_struct_ptr info = Dynarr_at (pdump_root_struct_ptrs, i);
|
|
1354 pdump_register_struct (*(info.ptraddress), info.desc, 1);
|
|
1355 }
|
442
|
1356
|
458
|
1357 memcpy (header.signature, PDUMP_SIGNATURE, PDUMP_SIGNATURE_LEN);
|
|
1358 header.id = dump_id;
|
|
1359 header.reloc_address = 0;
|
|
1360 header.nb_root_struct_ptrs = Dynarr_length (pdump_root_struct_ptrs);
|
|
1361 header.nb_opaques = Dynarr_length (pdump_opaques);
|
442
|
1362
|
826
|
1363 cur_offset = MAX_ALIGN_SIZE (sizeof (header));
|
442
|
1364 max_size = 0;
|
|
1365
|
|
1366 pdump_scan_by_alignment (pdump_allocate_offset);
|
826
|
1367 cur_offset = MAX_ALIGN_SIZE (cur_offset);
|
458
|
1368 header.stab_offset = cur_offset;
|
442
|
1369
|
|
1370 pdump_buf = xmalloc (max_size);
|
|
1371 pdump_fd = open (EMACS_PROGNAME ".dmp",
|
|
1372 O_WRONLY | O_CREAT | O_TRUNC | OPEN_BINARY, 0666);
|
771
|
1373 if (pdump_fd < 0)
|
|
1374 report_file_error ("Unable to open dump file",
|
|
1375 build_string (EMACS_PROGNAME ".dmp"));
|
458
|
1376 pdump_out = fdopen (pdump_fd, "w");
|
771
|
1377 if (pdump_out < 0)
|
|
1378 report_file_error ("Unable to open dump file for writing",
|
|
1379 build_string (EMACS_PROGNAME ".dmp"));
|
442
|
1380
|
771
|
1381 retry_fwrite (&header, sizeof (header), 1, pdump_out);
|
458
|
1382 PDUMP_ALIGN_OUTPUT (max_align_t);
|
442
|
1383
|
|
1384 pdump_scan_by_alignment (pdump_dump_data);
|
|
1385
|
458
|
1386 fseek (pdump_out, header.stab_offset, SEEK_SET);
|
442
|
1387
|
458
|
1388 pdump_dump_root_struct_ptrs ();
|
452
|
1389 pdump_dump_opaques ();
|
442
|
1390 pdump_dump_rtables ();
|
458
|
1391 pdump_dump_root_objects ();
|
442
|
1392
|
771
|
1393 retry_fclose (pdump_out);
|
|
1394 retry_close (pdump_fd);
|
458
|
1395
|
442
|
1396 free (pdump_buf);
|
|
1397
|
|
1398 free (pdump_hash);
|
|
1399
|
|
1400 Vterminal_console = t_console;
|
|
1401 Vterminal_frame = t_frame;
|
|
1402 Vterminal_device = t_device;
|
|
1403 }
|
|
1404
|
452
|
1405 static int
|
|
1406 pdump_load_check (void)
|
442
|
1407 {
|
452
|
1408 return (!memcmp (((pdump_header *)pdump_start)->signature,
|
|
1409 PDUMP_SIGNATURE, PDUMP_SIGNATURE_LEN)
|
|
1410 && ((pdump_header *)pdump_start)->id == dump_id);
|
442
|
1411 }
|
|
1412
|
458
|
1413 /*----------------------------------------------------------------------*/
|
|
1414 /* Reading the dump file */
|
|
1415 /*----------------------------------------------------------------------*/
|
452
|
1416 static int
|
|
1417 pdump_load_finish (void)
|
442
|
1418 {
|
|
1419 int i;
|
|
1420 char *p;
|
|
1421 EMACS_INT delta;
|
|
1422 EMACS_INT count;
|
458
|
1423 pdump_header *header = (pdump_header *)pdump_start;
|
442
|
1424
|
|
1425 pdump_end = pdump_start + pdump_length;
|
|
1426
|
458
|
1427 delta = ((EMACS_INT)pdump_start) - header->reloc_address;
|
|
1428 p = pdump_start + header->stab_offset;
|
442
|
1429
|
452
|
1430 /* Put back the pdump_root_struct_ptrs */
|
826
|
1431 p = (char *) ALIGN_PTR (p, pdump_static_pointer);
|
458
|
1432 for (i=0; i<header->nb_root_struct_ptrs; i++)
|
442
|
1433 {
|
458
|
1434 pdump_static_pointer ptr = PDUMP_READ (p, pdump_static_pointer);
|
|
1435 (* ptr.address) = ptr.value + delta;
|
442
|
1436 }
|
|
1437
|
452
|
1438 /* Put back the pdump_opaques */
|
458
|
1439 for (i=0; i<header->nb_opaques; i++)
|
442
|
1440 {
|
458
|
1441 pdump_opaque info = PDUMP_READ_ALIGNED (p, pdump_opaque);
|
545
|
1442 memcpy ((void*)info.varaddress, p, info.size);
|
452
|
1443 p += info.size;
|
442
|
1444 }
|
|
1445
|
|
1446 /* Do the relocations */
|
|
1447 pdump_rt_list = p;
|
|
1448 count = 2;
|
|
1449 for (;;)
|
|
1450 {
|
458
|
1451 pdump_reloc_table rt = PDUMP_READ_ALIGNED (p, pdump_reloc_table);
|
826
|
1452 p = (char *) ALIGN_PTR (p, char *);
|
442
|
1453 if (rt.desc)
|
|
1454 {
|
458
|
1455 char **reloc = (char **)p;
|
442
|
1456 for (i=0; i < rt.count; i++)
|
|
1457 {
|
458
|
1458 reloc[i] += delta;
|
|
1459 pdump_reloc_one (reloc[i], delta, rt.desc);
|
442
|
1460 }
|
458
|
1461 p += rt.count * sizeof (char *);
|
442
|
1462 } else
|
|
1463 if (!(--count))
|
|
1464 break;
|
|
1465 }
|
|
1466
|
452
|
1467 /* Put the pdump_root_objects variables in place */
|
665
|
1468 i = PDUMP_READ_ALIGNED (p, Elemcount);
|
826
|
1469 p = (char *) ALIGN_PTR (p, pdump_static_Lisp_Object);
|
458
|
1470 while (i--)
|
442
|
1471 {
|
458
|
1472 pdump_static_Lisp_Object obj = PDUMP_READ (p, pdump_static_Lisp_Object);
|
442
|
1473
|
458
|
1474 if (POINTER_TYPE_P (XTYPE (obj.value)))
|
619
|
1475 obj.value = wrap_pointer_1 ((char *) XPNTR (obj.value) + delta);
|
442
|
1476
|
458
|
1477 (* obj.address) = obj.value;
|
442
|
1478 }
|
|
1479
|
|
1480 /* Final cleanups */
|
|
1481 /* reorganize hash tables */
|
|
1482 p = pdump_rt_list;
|
|
1483 for (;;)
|
|
1484 {
|
458
|
1485 pdump_reloc_table rt = PDUMP_READ_ALIGNED (p, pdump_reloc_table);
|
826
|
1486 p = (char *) ALIGN_PTR (p, Lisp_Object);
|
442
|
1487 if (!rt.desc)
|
|
1488 break;
|
|
1489 if (rt.desc == hash_table_description)
|
|
1490 {
|
|
1491 for (i=0; i < rt.count; i++)
|
|
1492 pdump_reorganize_hash_table (PDUMP_READ (p, Lisp_Object));
|
|
1493 break;
|
|
1494 } else
|
|
1495 p += sizeof (Lisp_Object) * rt.count;
|
|
1496 }
|
|
1497
|
|
1498 return 1;
|
|
1499 }
|
|
1500
|
|
1501 #ifdef WIN32_NATIVE
|
|
1502 /* Free the mapped file if we decide we don't want it after all */
|
452
|
1503 static void
|
|
1504 pdump_file_unmap (void)
|
442
|
1505 {
|
|
1506 UnmapViewOfFile (pdump_start);
|
|
1507 CloseHandle (pdump_hFile);
|
|
1508 CloseHandle (pdump_hMap);
|
|
1509 }
|
|
1510
|
452
|
1511 static int
|
|
1512 pdump_file_get (const char *path)
|
442
|
1513 {
|
|
1514
|
800
|
1515 pdump_hFile = CreateFileA (path,
|
442
|
1516 GENERIC_READ + GENERIC_WRITE, /* Required for copy on write */
|
|
1517 0, /* Not shared */
|
|
1518 NULL, /* Not inheritable */
|
|
1519 OPEN_EXISTING,
|
|
1520 FILE_ATTRIBUTE_NORMAL,
|
|
1521 NULL); /* No template file */
|
|
1522 if (pdump_hFile == INVALID_HANDLE_VALUE)
|
|
1523 return 0;
|
|
1524
|
|
1525 pdump_length = GetFileSize (pdump_hFile, NULL);
|
800
|
1526 pdump_hMap = CreateFileMappingA (pdump_hFile,
|
442
|
1527 NULL, /* No security attributes */
|
|
1528 PAGE_WRITECOPY, /* Copy on write */
|
|
1529 0, /* Max size, high half */
|
|
1530 0, /* Max size, low half */
|
|
1531 NULL); /* Unnamed */
|
|
1532 if (pdump_hMap == INVALID_HANDLE_VALUE)
|
|
1533 return 0;
|
|
1534
|
|
1535 pdump_start = MapViewOfFile (pdump_hMap,
|
452
|
1536 FILE_MAP_COPY, /* Copy on write */
|
442
|
1537 0, /* Start at zero */
|
|
1538 0,
|
|
1539 0); /* Map all of it */
|
|
1540 pdump_free = pdump_file_unmap;
|
|
1541 return 1;
|
|
1542 }
|
|
1543
|
|
1544 /* pdump_resource_free is called (via the pdump_free pointer) to release
|
|
1545 any resources allocated by pdump_resource_get. Since the Windows API
|
|
1546 specs specifically state that you don't need to (and shouldn't) free the
|
|
1547 resources allocated by FindResource, LoadResource, and LockResource this
|
|
1548 routine does nothing. */
|
452
|
1549 static void
|
|
1550 pdump_resource_free (void)
|
442
|
1551 {
|
|
1552 }
|
|
1553
|
452
|
1554 static int
|
|
1555 pdump_resource_get (void)
|
442
|
1556 {
|
452
|
1557 HRSRC hRes; /* Handle to dump resource */
|
|
1558 HRSRC hResLoad; /* Handle to loaded dump resource */
|
442
|
1559
|
|
1560 /* See Q126630 which describes how Windows NT and 95 trap writes to
|
|
1561 resource sections and duplicate the page to allow the write to proceed.
|
|
1562 It also describes how to make the resource section read/write (and hence
|
|
1563 private to each process). Doing this avoids the exceptions and related
|
|
1564 overhead, but causes the resource section to be private to each process
|
|
1565 that is running XEmacs. Since the resource section contains little
|
|
1566 other than the dumped data, which should be private to each process, we
|
|
1567 make the whole resource section read/write so we don't have to copy it. */
|
|
1568
|
800
|
1569 hRes = FindResourceA (NULL, MAKEINTRESOURCE (101), "DUMP");
|
442
|
1570 if (hRes == NULL)
|
|
1571 return 0;
|
|
1572
|
|
1573 /* Found it, use the data in the resource */
|
|
1574 hResLoad = LoadResource (NULL, hRes);
|
|
1575 if (hResLoad == NULL)
|
|
1576 return 0;
|
|
1577
|
|
1578 pdump_start = LockResource (hResLoad);
|
|
1579 if (pdump_start == NULL)
|
|
1580 return 0;
|
|
1581
|
|
1582 pdump_free = pdump_resource_free;
|
|
1583 pdump_length = SizeofResource (NULL, hRes);
|
665
|
1584 if (pdump_length <= (Bytecount) sizeof (pdump_header))
|
442
|
1585 {
|
|
1586 pdump_start = 0;
|
|
1587 return 0;
|
|
1588 }
|
|
1589
|
|
1590 return 1;
|
|
1591 }
|
|
1592
|
|
1593 #else /* !WIN32_NATIVE */
|
|
1594
|
452
|
1595 static void
|
|
1596 pdump_file_free (void)
|
442
|
1597 {
|
460
|
1598 xfree (pdump_start);
|
442
|
1599 }
|
|
1600
|
|
1601 #ifdef HAVE_MMAP
|
452
|
1602 static void
|
|
1603 pdump_file_unmap (void)
|
442
|
1604 {
|
|
1605 munmap (pdump_start, pdump_length);
|
|
1606 }
|
|
1607 #endif
|
|
1608
|
452
|
1609 static int
|
|
1610 pdump_file_get (const char *path)
|
442
|
1611 {
|
|
1612 int fd = open (path, O_RDONLY | OPEN_BINARY);
|
|
1613 if (fd<0)
|
|
1614 return 0;
|
|
1615
|
|
1616 pdump_length = lseek (fd, 0, SEEK_END);
|
665
|
1617 if (pdump_length < (Bytecount) sizeof (pdump_header))
|
442
|
1618 {
|
771
|
1619 retry_close (fd);
|
442
|
1620 return 0;
|
|
1621 }
|
|
1622
|
|
1623 lseek (fd, 0, SEEK_SET);
|
|
1624
|
|
1625 #ifdef HAVE_MMAP
|
456
|
1626 /* Unix 98 requires that sys/mman.h define MAP_FAILED,
|
|
1627 but many earlier implementations don't. */
|
|
1628 # ifndef MAP_FAILED
|
|
1629 # define MAP_FAILED ((void *) -1L)
|
|
1630 # endif
|
442
|
1631 pdump_start = (char *) mmap (0, pdump_length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
|
452
|
1632 if (pdump_start != (char *) MAP_FAILED)
|
442
|
1633 {
|
|
1634 pdump_free = pdump_file_unmap;
|
771
|
1635 retry_close (fd);
|
442
|
1636 return 1;
|
|
1637 }
|
456
|
1638 #endif /* HAVE_MMAP */
|
442
|
1639
|
460
|
1640 pdump_start = xnew_array (char, pdump_length);
|
442
|
1641 pdump_free = pdump_file_free;
|
771
|
1642 retry_read (fd, pdump_start, pdump_length);
|
442
|
1643
|
771
|
1644 retry_close (fd);
|
442
|
1645 return 1;
|
|
1646 }
|
|
1647 #endif /* !WIN32_NATIVE */
|
|
1648
|
|
1649
|
452
|
1650 static int
|
|
1651 pdump_file_try (char *exe_path)
|
442
|
1652 {
|
460
|
1653 char *w = exe_path + strlen (exe_path);
|
442
|
1654
|
|
1655 do
|
|
1656 {
|
|
1657 sprintf (w, "-%s-%08x.dmp", EMACS_VERSION, dump_id);
|
|
1658 if (pdump_file_get (exe_path))
|
|
1659 {
|
|
1660 if (pdump_load_check ())
|
|
1661 return 1;
|
452
|
1662 pdump_free ();
|
442
|
1663 }
|
|
1664
|
|
1665 sprintf (w, "-%08x.dmp", dump_id);
|
|
1666 if (pdump_file_get (exe_path))
|
|
1667 {
|
|
1668 if (pdump_load_check ())
|
|
1669 return 1;
|
452
|
1670 pdump_free ();
|
442
|
1671 }
|
|
1672
|
|
1673 sprintf (w, ".dmp");
|
|
1674 if (pdump_file_get (exe_path))
|
|
1675 {
|
|
1676 if (pdump_load_check ())
|
|
1677 return 1;
|
452
|
1678 pdump_free ();
|
442
|
1679 }
|
|
1680
|
|
1681 do
|
|
1682 w--;
|
|
1683 while (w>exe_path && !IS_DIRECTORY_SEP (*w) && (*w != '-') && (*w != '.'));
|
|
1684 }
|
|
1685 while (w>exe_path && !IS_DIRECTORY_SEP (*w));
|
|
1686 return 0;
|
|
1687 }
|
|
1688
|
452
|
1689 int
|
771
|
1690 pdump_load (const Extbyte *argv0)
|
442
|
1691 {
|
771
|
1692 Extbyte exe_path[PATH_MAX];
|
442
|
1693 #ifdef WIN32_NATIVE
|
800
|
1694 GetModuleFileNameA (NULL, exe_path, PATH_MAX);
|
442
|
1695 #else /* !WIN32_NATIVE */
|
771
|
1696 Extbyte *w;
|
|
1697 const Extbyte *dir, *p;
|
442
|
1698
|
|
1699 dir = argv0;
|
|
1700 if (dir[0] == '-')
|
|
1701 {
|
|
1702 /* XEmacs as a login shell, oh goody! */
|
771
|
1703 dir = getenv ("SHELL"); /* not egetenv -- not yet initialized */
|
442
|
1704 }
|
|
1705
|
452
|
1706 p = dir + strlen (dir);
|
442
|
1707 while (p != dir && !IS_ANY_SEP (p[-1])) p--;
|
|
1708
|
|
1709 if (p != dir)
|
|
1710 {
|
|
1711 /* invocation-name includes a directory component -- presumably it
|
|
1712 is relative to cwd, not $PATH */
|
|
1713 strcpy (exe_path, dir);
|
|
1714 }
|
|
1715 else
|
|
1716 {
|
771
|
1717 const Extbyte *path = getenv ("PATH"); /* not egetenv -- not yet init. */
|
|
1718 const Extbyte *name = p;
|
442
|
1719 for (;;)
|
|
1720 {
|
|
1721 p = path;
|
|
1722 while (*p && *p != SEPCHAR)
|
|
1723 p++;
|
|
1724 if (p == path)
|
|
1725 {
|
|
1726 exe_path[0] = '.';
|
|
1727 w = exe_path + 1;
|
|
1728 }
|
|
1729 else
|
|
1730 {
|
|
1731 memcpy (exe_path, path, p - path);
|
|
1732 w = exe_path + (p - path);
|
|
1733 }
|
|
1734 if (!IS_DIRECTORY_SEP (w[-1]))
|
|
1735 {
|
|
1736 *w++ = '/';
|
|
1737 }
|
452
|
1738 strcpy (w, name);
|
442
|
1739
|
|
1740 if (!access (exe_path, X_OK))
|
|
1741 break;
|
|
1742 if (!*p)
|
|
1743 {
|
|
1744 /* Oh well, let's have some kind of default */
|
|
1745 sprintf (exe_path, "./%s", name);
|
|
1746 break;
|
|
1747 }
|
|
1748 path = p+1;
|
|
1749 }
|
|
1750 }
|
|
1751 #endif /* WIN32_NATIVE */
|
|
1752
|
|
1753 if (pdump_file_try (exe_path))
|
|
1754 {
|
|
1755 pdump_load_finish ();
|
|
1756 return 1;
|
|
1757 }
|
|
1758
|
|
1759 #ifdef WIN32_NATIVE
|
|
1760 if (pdump_resource_get ())
|
|
1761 {
|
|
1762 if (pdump_load_check ())
|
|
1763 {
|
|
1764 pdump_load_finish ();
|
|
1765 return 1;
|
|
1766 }
|
|
1767 pdump_free ();
|
|
1768 }
|
|
1769 #endif
|
|
1770
|
|
1771 return 0;
|
|
1772 }
|