442
|
1 /* Portable data dumper for XEmacs.
|
|
2 Copyright (C) 1999-2000 Olivier Galibert
|
458
|
3 Copyright (C) 2001 Martin Buchholz
|
442
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Not in FSF. */
|
|
23
|
|
24 #include <config.h>
|
|
25 #include "lisp.h"
|
|
26
|
|
27 #include "specifier.h"
|
|
28 #include "elhash.h"
|
|
29 #include "sysfile.h"
|
|
30 #include "console-stream.h"
|
|
31 #include "dumper.h"
|
|
32
|
|
33 #ifdef WIN32_NATIVE
|
|
34 #include "nt.h"
|
|
35 #else
|
|
36 #ifdef HAVE_MMAP
|
|
37 #include <sys/mman.h>
|
|
38 #endif
|
|
39 #endif
|
|
40
|
|
41 #ifndef SEPCHAR
|
|
42 #define SEPCHAR ':'
|
|
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
|
|
101 dump_add_root_struct_ptr (void *ptraddress, const struct struct_description *desc)
|
|
102 {
|
|
103 pdump_root_struct_ptr info;
|
|
104 info.ptraddress = (void **) ptraddress;
|
|
105 info.desc = desc;
|
|
106 if (pdump_root_struct_ptrs == NULL)
|
|
107 pdump_root_struct_ptrs = Dynarr_new (pdump_root_struct_ptr);
|
|
108 Dynarr_add (pdump_root_struct_ptrs, info);
|
|
109 }
|
|
110
|
|
111 /* Mark the Lisp_Object at non-heap address VARADDRESS for dumping.
|
|
112 All the objects reachable from this var will also be dumped. */
|
|
113 void
|
|
114 dump_add_root_object (Lisp_Object *varaddress)
|
|
115 {
|
|
116 if (pdump_root_objects == NULL)
|
|
117 pdump_root_objects = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
|
|
118 Dynarr_add (pdump_root_objects, varaddress);
|
|
119 }
|
|
120
|
|
121 /* Mark the list pointed to by the Lisp_Object at VARADDRESS for dumping. */
|
|
122 void
|
|
123 dump_add_weak_object_chain (Lisp_Object *varaddress)
|
|
124 {
|
|
125 if (pdump_weak_object_chains == NULL)
|
|
126 pdump_weak_object_chains = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
|
|
127 Dynarr_add (pdump_weak_object_chains, varaddress);
|
|
128 }
|
|
129
|
|
130
|
458
|
131 inline static void
|
665
|
132 pdump_align_stream (FILE *stream, Bytecount alignment)
|
458
|
133 {
|
|
134 long offset = ftell (stream);
|
|
135 long adjustment = ALIGN_SIZE (offset, alignment) - offset;
|
|
136 if (adjustment)
|
|
137 fseek (stream, adjustment, SEEK_CUR);
|
|
138 }
|
|
139
|
|
140 #define PDUMP_ALIGN_OUTPUT(type) pdump_align_stream (pdump_out, ALIGNOF (type))
|
|
141
|
|
142 #define PDUMP_WRITE(type, object) \
|
|
143 fwrite (&object, sizeof (object), 1, pdump_out);
|
|
144
|
|
145 #define PDUMP_WRITE_ALIGNED(type, object) do { \
|
|
146 PDUMP_ALIGN_OUTPUT (type); \
|
|
147 PDUMP_WRITE (type, object); \
|
|
148 } while (0)
|
|
149
|
|
150 #define PDUMP_READ(ptr, type) \
|
|
151 (((type *) (ptr = (char*) (((type *) ptr) + 1)))[-1])
|
|
152
|
|
153 #define PDUMP_READ_ALIGNED(ptr, type) \
|
|
154 ((ptr = (char *) ALIGN_PTR (ptr, ALIGNOF (type))), PDUMP_READ (ptr, type))
|
|
155
|
|
156
|
|
157
|
452
|
158 typedef struct
|
|
159 {
|
442
|
160 const struct lrecord_description *desc;
|
|
161 int count;
|
|
162 } pdump_reloc_table;
|
|
163
|
|
164 static char *pdump_rt_list = 0;
|
|
165
|
|
166 void
|
|
167 pdump_objects_unmark (void)
|
|
168 {
|
|
169 int i;
|
|
170 char *p = pdump_rt_list;
|
|
171 if (p)
|
|
172 for (;;)
|
|
173 {
|
|
174 pdump_reloc_table *rt = (pdump_reloc_table *)p;
|
|
175 p += sizeof (pdump_reloc_table);
|
|
176 if (rt->desc)
|
|
177 {
|
|
178 for (i=0; i<rt->count; i++)
|
|
179 {
|
|
180 struct lrecord_header *lh = * (struct lrecord_header **) p;
|
|
181 if (! C_READONLY_RECORD_HEADER_P (lh))
|
|
182 UNMARK_RECORD_HEADER (lh);
|
|
183 p += sizeof (EMACS_INT);
|
|
184 }
|
|
185 } else
|
|
186 break;
|
|
187 }
|
|
188 }
|
|
189
|
|
190
|
|
191 /* The structure of the file
|
458
|
192 0 - header
|
|
193 - dumped objects
|
|
194 stab_offset - nb_root_struct_ptrs*pair(void *, adr)
|
|
195 for pointers to structures
|
|
196 - nb_opaques*pair(void *, size) for raw bits to restore
|
|
197 - relocation table
|
|
198 - root lisp object address/value couples with the count
|
|
199 preceding the list
|
442
|
200 */
|
|
201
|
|
202
|
452
|
203 #define PDUMP_SIGNATURE "XEmacsDP"
|
|
204 #define PDUMP_SIGNATURE_LEN (sizeof (PDUMP_SIGNATURE) - 1)
|
442
|
205
|
|
206 typedef struct
|
|
207 {
|
452
|
208 char signature[PDUMP_SIGNATURE_LEN];
|
442
|
209 unsigned int id;
|
|
210 EMACS_UINT stab_offset;
|
|
211 EMACS_UINT reloc_address;
|
452
|
212 int nb_root_struct_ptrs;
|
|
213 int nb_opaques;
|
|
214 } pdump_header;
|
442
|
215
|
458
|
216 char *pdump_start;
|
|
217 char *pdump_end;
|
665
|
218 static Bytecount pdump_length;
|
442
|
219
|
|
220 #ifdef WIN32_NATIVE
|
452
|
221 /* Handle for the dump file */
|
458
|
222 static HANDLE pdump_hFile = INVALID_HANDLE_VALUE;
|
452
|
223 /* Handle for the file mapping object for the dump file */
|
458
|
224 static HANDLE pdump_hMap = INVALID_HANDLE_VALUE;
|
442
|
225 #endif
|
|
226
|
458
|
227 static void (*pdump_free) (void);
|
442
|
228
|
460
|
229 static unsigned char pdump_align_table[] =
|
442
|
230 {
|
460
|
231 64, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1,
|
|
232 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1,
|
|
233 32, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1,
|
|
234 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1
|
442
|
235 };
|
|
236
|
647
|
237 static inline int
|
665
|
238 pdump_size_to_align (Bytecount size)
|
442
|
239 {
|
460
|
240 return pdump_align_table[size % countof (pdump_align_table)];
|
|
241 }
|
|
242
|
|
243 typedef struct pdump_entry_list_elt
|
|
244 {
|
|
245 struct pdump_entry_list_elt *next;
|
442
|
246 const void *obj;
|
665
|
247 Bytecount size;
|
442
|
248 int count;
|
|
249 EMACS_INT save_offset;
|
460
|
250 } pdump_entry_list_elt;
|
442
|
251
|
|
252 typedef struct
|
|
253 {
|
460
|
254 pdump_entry_list_elt *first;
|
442
|
255 int align;
|
|
256 int count;
|
|
257 } pdump_entry_list;
|
|
258
|
460
|
259 typedef struct pdump_struct_list_elt
|
442
|
260 {
|
|
261 pdump_entry_list list;
|
|
262 const struct struct_description *sdesc;
|
460
|
263 } pdump_struct_list_elt;
|
442
|
264
|
|
265 typedef struct
|
|
266 {
|
460
|
267 pdump_struct_list_elt *list;
|
442
|
268 int count;
|
|
269 int size;
|
|
270 } pdump_struct_list;
|
|
271
|
460
|
272 static pdump_entry_list *pdump_object_table;
|
442
|
273 static pdump_entry_list pdump_opaque_data_list;
|
|
274 static pdump_struct_list pdump_struct_table;
|
|
275
|
460
|
276 static int *pdump_alert_undump_object;
|
442
|
277
|
|
278 static unsigned long cur_offset;
|
665
|
279 static Bytecount max_size;
|
442
|
280 static int pdump_fd;
|
|
281 static void *pdump_buf;
|
458
|
282 static FILE *pdump_out;
|
442
|
283
|
|
284 #define PDUMP_HASHSIZE 200001
|
|
285
|
460
|
286 static pdump_entry_list_elt **pdump_hash;
|
442
|
287
|
|
288 /* Since most pointers are eight bytes aligned, the >>3 allows for a better hash */
|
|
289 static int
|
|
290 pdump_make_hash (const void *obj)
|
|
291 {
|
|
292 return ((unsigned long)(obj)>>3) % PDUMP_HASHSIZE;
|
|
293 }
|
|
294
|
460
|
295 static pdump_entry_list_elt *
|
442
|
296 pdump_get_entry (const void *obj)
|
|
297 {
|
|
298 int pos = pdump_make_hash (obj);
|
460
|
299 pdump_entry_list_elt *e;
|
442
|
300
|
|
301 assert (obj != 0);
|
|
302
|
|
303 while ((e = pdump_hash[pos]) != 0)
|
|
304 {
|
|
305 if (e->obj == obj)
|
|
306 return e;
|
|
307
|
|
308 pos++;
|
|
309 if (pos == PDUMP_HASHSIZE)
|
|
310 pos = 0;
|
|
311 }
|
|
312 return 0;
|
|
313 }
|
|
314
|
|
315 static void
|
665
|
316 pdump_add_entry (pdump_entry_list *list, const void *obj, Bytecount size,
|
458
|
317 int count)
|
442
|
318 {
|
460
|
319 pdump_entry_list_elt *e;
|
442
|
320 int pos = pdump_make_hash (obj);
|
|
321
|
|
322 while ((e = pdump_hash[pos]) != 0)
|
|
323 {
|
|
324 if (e->obj == obj)
|
|
325 return;
|
|
326
|
|
327 pos++;
|
|
328 if (pos == PDUMP_HASHSIZE)
|
|
329 pos = 0;
|
|
330 }
|
|
331
|
460
|
332 e = xnew (pdump_entry_list_elt);
|
442
|
333
|
|
334 e->next = list->first;
|
|
335 e->obj = obj;
|
|
336 e->size = size;
|
|
337 e->count = count;
|
|
338 list->first = e;
|
|
339
|
|
340 list->count += count;
|
|
341 pdump_hash[pos] = e;
|
|
342
|
460
|
343 {
|
|
344 int align = pdump_size_to_align (size);
|
442
|
345
|
460
|
346 if (align < list->align)
|
|
347 list->align = align;
|
|
348 }
|
442
|
349 }
|
|
350
|
|
351 static pdump_entry_list *
|
|
352 pdump_get_entry_list (const struct struct_description *sdesc)
|
|
353 {
|
|
354 int i;
|
|
355 for (i=0; i<pdump_struct_table.count; i++)
|
|
356 if (pdump_struct_table.list[i].sdesc == sdesc)
|
|
357 return &pdump_struct_table.list[i].list;
|
|
358
|
|
359 if (pdump_struct_table.size <= pdump_struct_table.count)
|
|
360 {
|
|
361 if (pdump_struct_table.size == -1)
|
|
362 pdump_struct_table.size = 10;
|
|
363 else
|
|
364 pdump_struct_table.size = pdump_struct_table.size * 2;
|
460
|
365 pdump_struct_table.list = (pdump_struct_list_elt *)
|
442
|
366 xrealloc (pdump_struct_table.list,
|
460
|
367 pdump_struct_table.size * sizeof (pdump_struct_list_elt));
|
442
|
368 }
|
|
369 pdump_struct_table.list[pdump_struct_table.count].list.first = 0;
|
460
|
370 pdump_struct_table.list[pdump_struct_table.count].list.align = ALIGNOF (max_align_t);
|
442
|
371 pdump_struct_table.list[pdump_struct_table.count].list.count = 0;
|
|
372 pdump_struct_table.list[pdump_struct_table.count].sdesc = sdesc;
|
|
373
|
|
374 return &pdump_struct_table.list[pdump_struct_table.count++].list;
|
|
375 }
|
|
376
|
|
377 static struct
|
|
378 {
|
|
379 struct lrecord_header *obj;
|
|
380 int position;
|
|
381 int offset;
|
|
382 } backtrace[65536];
|
|
383
|
|
384 static int depth;
|
|
385
|
452
|
386 static void
|
|
387 pdump_backtrace (void)
|
442
|
388 {
|
|
389 int i;
|
|
390 stderr_out ("pdump backtrace :\n");
|
|
391 for (i=0;i<depth;i++)
|
|
392 {
|
|
393 if (!backtrace[i].obj)
|
458
|
394 stderr_out (" - ind. (%d, %d)\n",
|
|
395 backtrace[i].position,
|
|
396 backtrace[i].offset);
|
442
|
397 else
|
|
398 {
|
|
399 stderr_out (" - %s (%d, %d)\n",
|
462
|
400 LHEADER_IMPLEMENTATION (backtrace[i].obj)->name,
|
|
401 backtrace[i].position,
|
|
402 backtrace[i].offset);
|
442
|
403 }
|
|
404 }
|
|
405 }
|
|
406
|
|
407 static void pdump_register_object (Lisp_Object obj);
|
458
|
408 static void pdump_register_struct (const void *data,
|
|
409 const struct struct_description *sdesc,
|
|
410 int count);
|
442
|
411
|
|
412 static EMACS_INT
|
458
|
413 pdump_get_indirect_count (EMACS_INT code,
|
|
414 const struct lrecord_description *idesc,
|
|
415 const void *idata)
|
442
|
416 {
|
|
417 EMACS_INT count;
|
|
418 const void *irdata;
|
|
419
|
|
420 int line = XD_INDIRECT_VAL (code);
|
|
421 int delta = XD_INDIRECT_DELTA (code);
|
|
422
|
|
423 irdata = ((char *)idata) + idesc[line].offset;
|
|
424 switch (idesc[line].type)
|
|
425 {
|
665
|
426 case XD_BYTECOUNT:
|
|
427 count = *(Bytecount *)irdata;
|
647
|
428 break;
|
665
|
429 case XD_ELEMCOUNT:
|
|
430 count = *(Elemcount *)irdata;
|
647
|
431 break;
|
665
|
432 case XD_HASHCODE:
|
|
433 count = *(Hashcode *)irdata;
|
442
|
434 break;
|
|
435 case XD_INT:
|
|
436 count = *(int *)irdata;
|
|
437 break;
|
|
438 case XD_LONG:
|
|
439 count = *(long *)irdata;
|
|
440 break;
|
|
441 default:
|
458
|
442 stderr_out ("Unsupported count type : %d (line = %d, code=%ld)\n",
|
|
443 idesc[line].type, line, (long)code);
|
442
|
444 pdump_backtrace ();
|
647
|
445 count = 0; /* warning suppression */
|
442
|
446 abort ();
|
|
447 }
|
|
448 count += delta;
|
|
449 return count;
|
|
450 }
|
|
451
|
|
452 static void
|
|
453 pdump_register_sub (const void *data, const struct lrecord_description *desc, int me)
|
|
454 {
|
|
455 int pos;
|
|
456
|
|
457 restart:
|
|
458 for (pos = 0; desc[pos].type != XD_END; pos++)
|
|
459 {
|
|
460 const void *rdata = (const char *)data + desc[pos].offset;
|
|
461
|
|
462 backtrace[me].position = pos;
|
|
463 backtrace[me].offset = desc[pos].offset;
|
|
464
|
|
465 switch (desc[pos].type)
|
|
466 {
|
|
467 case XD_SPECIFIER_END:
|
|
468 pos = 0;
|
|
469 desc = ((const Lisp_Specifier *)data)->methods->extra_description;
|
|
470 goto restart;
|
665
|
471 case XD_BYTECOUNT:
|
|
472 case XD_ELEMCOUNT:
|
|
473 case XD_HASHCODE:
|
442
|
474 case XD_INT:
|
|
475 case XD_LONG:
|
|
476 case XD_INT_RESET:
|
|
477 case XD_LO_LINK:
|
|
478 break;
|
|
479 case XD_OPAQUE_DATA_PTR:
|
|
480 {
|
|
481 EMACS_INT count = desc[pos].data1;
|
|
482 if (XD_IS_INDIRECT (count))
|
|
483 count = pdump_get_indirect_count (count, desc, data);
|
|
484
|
|
485 pdump_add_entry (&pdump_opaque_data_list,
|
458
|
486 *(void **)rdata, count, 1);
|
442
|
487 break;
|
|
488 }
|
|
489 case XD_C_STRING:
|
|
490 {
|
|
491 const char *str = *(const char **)rdata;
|
|
492 if (str)
|
458
|
493 pdump_add_entry (&pdump_opaque_data_list, str, strlen (str)+1, 1);
|
442
|
494 break;
|
|
495 }
|
|
496 case XD_DOC_STRING:
|
|
497 {
|
|
498 const char *str = *(const char **)rdata;
|
|
499 if ((EMACS_INT)str > 0)
|
458
|
500 pdump_add_entry (&pdump_opaque_data_list, str, strlen (str)+1, 1);
|
442
|
501 break;
|
|
502 }
|
|
503 case XD_LISP_OBJECT:
|
|
504 {
|
|
505 const Lisp_Object *pobj = (const Lisp_Object *)rdata;
|
|
506
|
|
507 assert (desc[pos].data1 == 0);
|
|
508
|
|
509 backtrace[me].offset = (const char *)pobj - (const char *)data;
|
|
510 pdump_register_object (*pobj);
|
|
511 break;
|
|
512 }
|
|
513 case XD_LISP_OBJECT_ARRAY:
|
|
514 {
|
|
515 int i;
|
|
516 EMACS_INT count = desc[pos].data1;
|
|
517 if (XD_IS_INDIRECT (count))
|
|
518 count = pdump_get_indirect_count (count, desc, data);
|
|
519
|
|
520 for (i = 0; i < count; i++)
|
|
521 {
|
|
522 const Lisp_Object *pobj = ((const Lisp_Object *)rdata) + i;
|
|
523 Lisp_Object dobj = *pobj;
|
|
524
|
|
525 backtrace[me].offset = (const char *)pobj - (const char *)data;
|
|
526 pdump_register_object (dobj);
|
|
527 }
|
|
528 break;
|
|
529 }
|
|
530 case XD_STRUCT_PTR:
|
|
531 {
|
|
532 EMACS_INT count = desc[pos].data1;
|
|
533 const struct struct_description *sdesc = desc[pos].data2;
|
|
534 const char *dobj = *(const char **)rdata;
|
|
535 if (dobj)
|
|
536 {
|
|
537 if (XD_IS_INDIRECT (count))
|
|
538 count = pdump_get_indirect_count (count, desc, data);
|
|
539
|
|
540 pdump_register_struct (dobj, sdesc, count);
|
|
541 }
|
|
542 break;
|
|
543 }
|
|
544 default:
|
|
545 stderr_out ("Unsupported dump type : %d\n", desc[pos].type);
|
|
546 pdump_backtrace ();
|
|
547 abort ();
|
|
548 };
|
|
549 }
|
|
550 }
|
|
551
|
|
552 static void
|
|
553 pdump_register_object (Lisp_Object obj)
|
|
554 {
|
|
555 struct lrecord_header *objh;
|
458
|
556 const struct lrecord_implementation *imp;
|
442
|
557
|
|
558 if (!POINTER_TYPE_P (XTYPE (obj)))
|
|
559 return;
|
|
560
|
|
561 objh = XRECORD_LHEADER (obj);
|
|
562 if (!objh)
|
|
563 return;
|
|
564
|
|
565 if (pdump_get_entry (objh))
|
|
566 return;
|
|
567
|
458
|
568 imp = LHEADER_IMPLEMENTATION (objh);
|
|
569
|
|
570 if (imp->description)
|
442
|
571 {
|
|
572 int me = depth++;
|
|
573 if (me>65536)
|
|
574 {
|
|
575 stderr_out ("Backtrace overflow, loop ?\n");
|
|
576 abort ();
|
|
577 }
|
|
578 backtrace[me].obj = objh;
|
|
579 backtrace[me].position = 0;
|
|
580 backtrace[me].offset = 0;
|
|
581
|
|
582 pdump_add_entry (pdump_object_table + objh->type,
|
|
583 objh,
|
458
|
584 imp->static_size ?
|
|
585 imp->static_size :
|
|
586 imp->size_in_bytes_method (objh),
|
442
|
587 1);
|
458
|
588 pdump_register_sub (objh, imp->description, me);
|
442
|
589 --depth;
|
|
590 }
|
|
591 else
|
|
592 {
|
|
593 pdump_alert_undump_object[objh->type]++;
|
458
|
594 stderr_out ("Undumpable object type : %s\n", imp->name);
|
442
|
595 pdump_backtrace ();
|
|
596 }
|
|
597 }
|
|
598
|
|
599 static void
|
458
|
600 pdump_register_struct (const void *data,
|
|
601 const struct struct_description *sdesc,
|
|
602 int count)
|
442
|
603 {
|
|
604 if (data && !pdump_get_entry (data))
|
|
605 {
|
|
606 int me = depth++;
|
|
607 int i;
|
|
608 if (me>65536)
|
|
609 {
|
|
610 stderr_out ("Backtrace overflow, loop ?\n");
|
|
611 abort ();
|
|
612 }
|
|
613 backtrace[me].obj = 0;
|
|
614 backtrace[me].position = 0;
|
|
615 backtrace[me].offset = 0;
|
|
616
|
|
617 pdump_add_entry (pdump_get_entry_list (sdesc),
|
458
|
618 data, sdesc->size, count);
|
442
|
619 for (i=0; i<count; i++)
|
|
620 {
|
|
621 pdump_register_sub (((char *)data) + sdesc->size*i,
|
|
622 sdesc->description,
|
|
623 me);
|
|
624 }
|
|
625 --depth;
|
|
626 }
|
|
627 }
|
|
628
|
|
629 static void
|
460
|
630 pdump_dump_data (pdump_entry_list_elt *elt,
|
458
|
631 const struct lrecord_description *desc)
|
442
|
632 {
|
665
|
633 Bytecount size = elt->size;
|
460
|
634 int count = elt->count;
|
442
|
635 if (desc)
|
|
636 {
|
|
637 int pos, i;
|
460
|
638 memcpy (pdump_buf, elt->obj, size*count);
|
442
|
639
|
|
640 for (i=0; i<count; i++)
|
|
641 {
|
|
642 char *cur = ((char *)pdump_buf) + i*size;
|
|
643 restart:
|
|
644 for (pos = 0; desc[pos].type != XD_END; pos++)
|
|
645 {
|
|
646 void *rdata = cur + desc[pos].offset;
|
|
647 switch (desc[pos].type)
|
|
648 {
|
|
649 case XD_SPECIFIER_END:
|
460
|
650 desc = ((const Lisp_Specifier *)(elt->obj))->methods->extra_description;
|
442
|
651 goto restart;
|
665
|
652 case XD_BYTECOUNT:
|
|
653 case XD_ELEMCOUNT:
|
|
654 case XD_HASHCODE:
|
442
|
655 case XD_INT:
|
|
656 case XD_LONG:
|
|
657 break;
|
|
658 case XD_INT_RESET:
|
|
659 {
|
|
660 EMACS_INT val = desc[pos].data1;
|
|
661 if (XD_IS_INDIRECT (val))
|
460
|
662 val = pdump_get_indirect_count (val, desc, elt->obj);
|
442
|
663 *(int *)rdata = val;
|
|
664 break;
|
|
665 }
|
|
666 case XD_OPAQUE_DATA_PTR:
|
|
667 case XD_C_STRING:
|
|
668 case XD_STRUCT_PTR:
|
|
669 {
|
|
670 void *ptr = *(void **)rdata;
|
|
671 if (ptr)
|
|
672 *(EMACS_INT *)rdata = pdump_get_entry (ptr)->save_offset;
|
|
673 break;
|
|
674 }
|
|
675 case XD_LO_LINK:
|
|
676 {
|
|
677 Lisp_Object obj = *(Lisp_Object *)rdata;
|
460
|
678 pdump_entry_list_elt *elt1;
|
442
|
679 for (;;)
|
|
680 {
|
460
|
681 elt1 = pdump_get_entry (XRECORD_LHEADER (obj));
|
|
682 if (elt1)
|
442
|
683 break;
|
|
684 obj = *(Lisp_Object *)(desc[pos].offset + (char *)(XRECORD_LHEADER (obj)));
|
|
685 }
|
460
|
686 *(EMACS_INT *)rdata = elt1->save_offset;
|
442
|
687 break;
|
|
688 }
|
|
689 case XD_LISP_OBJECT:
|
|
690 {
|
|
691 Lisp_Object *pobj = (Lisp_Object *) rdata;
|
|
692
|
|
693 assert (desc[pos].data1 == 0);
|
|
694
|
|
695 if (POINTER_TYPE_P (XTYPE (*pobj)) && XRECORD_LHEADER (*pobj))
|
|
696 *(EMACS_INT *)pobj =
|
|
697 pdump_get_entry (XRECORD_LHEADER (*pobj))->save_offset;
|
|
698 break;
|
|
699 }
|
|
700 case XD_LISP_OBJECT_ARRAY:
|
|
701 {
|
|
702 EMACS_INT num = desc[pos].data1;
|
|
703 int j;
|
|
704 if (XD_IS_INDIRECT (num))
|
460
|
705 num = pdump_get_indirect_count (num, desc, elt->obj);
|
442
|
706
|
|
707 for (j=0; j<num; j++)
|
|
708 {
|
|
709 Lisp_Object *pobj = ((Lisp_Object *)rdata) + j;
|
|
710 if (POINTER_TYPE_P (XTYPE (*pobj)) && XRECORD_LHEADER (*pobj))
|
|
711 *(EMACS_INT *)pobj =
|
|
712 pdump_get_entry (XRECORD_LHEADER (*pobj))->save_offset;
|
|
713 }
|
|
714 break;
|
|
715 }
|
|
716 case XD_DOC_STRING:
|
|
717 {
|
|
718 EMACS_INT str = *(EMACS_INT *)rdata;
|
|
719 if (str > 0)
|
|
720 *(EMACS_INT *)rdata = pdump_get_entry ((void *)str)->save_offset;
|
|
721 break;
|
|
722 }
|
|
723 default:
|
|
724 stderr_out ("Unsupported dump type : %d\n", desc[pos].type);
|
|
725 abort ();
|
458
|
726 }
|
442
|
727 }
|
|
728 }
|
|
729 }
|
460
|
730 fwrite (desc ? pdump_buf : elt->obj, size, count, pdump_out);
|
442
|
731 }
|
|
732
|
|
733 static void
|
458
|
734 pdump_reloc_one (void *data, EMACS_INT delta,
|
|
735 const struct lrecord_description *desc)
|
442
|
736 {
|
|
737 int pos;
|
|
738
|
|
739 restart:
|
|
740 for (pos = 0; desc[pos].type != XD_END; pos++)
|
|
741 {
|
|
742 void *rdata = (char *)data + desc[pos].offset;
|
|
743 switch (desc[pos].type)
|
|
744 {
|
|
745 case XD_SPECIFIER_END:
|
|
746 pos = 0;
|
|
747 desc = ((const Lisp_Specifier *)data)->methods->extra_description;
|
|
748 goto restart;
|
665
|
749 case XD_BYTECOUNT:
|
|
750 case XD_ELEMCOUNT:
|
|
751 case XD_HASHCODE:
|
442
|
752 case XD_INT:
|
|
753 case XD_LONG:
|
|
754 case XD_INT_RESET:
|
|
755 break;
|
|
756 case XD_OPAQUE_DATA_PTR:
|
|
757 case XD_C_STRING:
|
|
758 case XD_STRUCT_PTR:
|
|
759 case XD_LO_LINK:
|
|
760 {
|
|
761 EMACS_INT ptr = *(EMACS_INT *)rdata;
|
|
762 if (ptr)
|
|
763 *(EMACS_INT *)rdata = ptr+delta;
|
|
764 break;
|
|
765 }
|
|
766 case XD_LISP_OBJECT:
|
|
767 {
|
|
768 Lisp_Object *pobj = (Lisp_Object *) rdata;
|
|
769
|
|
770 assert (desc[pos].data1 == 0);
|
|
771
|
|
772 if (POINTER_TYPE_P (XTYPE (*pobj))
|
|
773 && ! EQ (*pobj, Qnull_pointer))
|
|
774 XSETOBJ (*pobj, (char *) XPNTR (*pobj) + delta);
|
|
775
|
|
776 break;
|
|
777 }
|
|
778 case XD_LISP_OBJECT_ARRAY:
|
|
779 {
|
|
780 EMACS_INT num = desc[pos].data1;
|
|
781 int j;
|
|
782 if (XD_IS_INDIRECT (num))
|
|
783 num = pdump_get_indirect_count (num, desc, data);
|
|
784
|
|
785 for (j=0; j<num; j++)
|
|
786 {
|
|
787 Lisp_Object *pobj = (Lisp_Object *) rdata + j;
|
|
788
|
|
789 if (POINTER_TYPE_P (XTYPE (*pobj))
|
|
790 && ! EQ (*pobj, Qnull_pointer))
|
|
791 XSETOBJ (*pobj, (char *) XPNTR (*pobj) + delta);
|
|
792 }
|
|
793 break;
|
|
794 }
|
|
795 case XD_DOC_STRING:
|
|
796 {
|
|
797 EMACS_INT str = *(EMACS_INT *)rdata;
|
|
798 if (str > 0)
|
|
799 *(EMACS_INT *)rdata = str + delta;
|
|
800 break;
|
|
801 }
|
|
802 default:
|
|
803 stderr_out ("Unsupported dump type : %d\n", desc[pos].type);
|
|
804 abort ();
|
|
805 };
|
|
806 }
|
|
807 }
|
|
808
|
|
809 static void
|
460
|
810 pdump_allocate_offset (pdump_entry_list_elt *elt,
|
458
|
811 const struct lrecord_description *desc)
|
442
|
812 {
|
665
|
813 Bytecount size = elt->count * elt->size;
|
460
|
814 elt->save_offset = cur_offset;
|
442
|
815 if (size>max_size)
|
|
816 max_size = size;
|
|
817 cur_offset += size;
|
|
818 }
|
|
819
|
|
820 static void
|
460
|
821 pdump_scan_by_alignment (void (*f)(pdump_entry_list_elt *,
|
458
|
822 const struct lrecord_description *))
|
442
|
823 {
|
460
|
824 int align;
|
|
825
|
|
826 for (align = ALIGNOF (max_align_t); align; align>>=1)
|
442
|
827 {
|
460
|
828 int i;
|
|
829 pdump_entry_list_elt *elt;
|
|
830
|
442
|
831 for (i=0; i<lrecord_type_count; i++)
|
|
832 if (pdump_object_table[i].align == align)
|
460
|
833 for (elt = pdump_object_table[i].first; elt; elt = elt->next)
|
|
834 f (elt, lrecord_implementations_table[i]->description);
|
442
|
835
|
|
836 for (i=0; i<pdump_struct_table.count; i++)
|
460
|
837 {
|
|
838 pdump_struct_list_elt list = pdump_struct_table.list[i];
|
|
839 if (list.list.align == align)
|
|
840 for (elt = list.list.first; elt; elt = elt->next)
|
|
841 f (elt, list.sdesc->description);
|
|
842 }
|
442
|
843
|
460
|
844 for (elt = pdump_opaque_data_list.first; elt; elt = elt->next)
|
|
845 if (pdump_size_to_align (elt->size) == align)
|
|
846 f (elt, 0);
|
442
|
847 }
|
|
848 }
|
|
849
|
|
850 static void
|
458
|
851 pdump_dump_root_struct_ptrs (void)
|
442
|
852 {
|
|
853 int i;
|
665
|
854 Elemcount count = Dynarr_length (pdump_root_struct_ptrs);
|
458
|
855 pdump_static_pointer *data = alloca_array (pdump_static_pointer, count);
|
|
856 for (i = 0; i < count; i++)
|
442
|
857 {
|
458
|
858 data[i].address = (char **) Dynarr_atp (pdump_root_struct_ptrs, i)->ptraddress;
|
|
859 data[i].value = (char *) pdump_get_entry (* data[i].address)->save_offset;
|
442
|
860 }
|
458
|
861 PDUMP_ALIGN_OUTPUT (pdump_static_pointer);
|
|
862 fwrite (data, sizeof (pdump_static_pointer), count, pdump_out);
|
442
|
863 }
|
|
864
|
|
865 static void
|
452
|
866 pdump_dump_opaques (void)
|
442
|
867 {
|
|
868 int i;
|
452
|
869 for (i = 0; i < Dynarr_length (pdump_opaques); i++)
|
442
|
870 {
|
452
|
871 pdump_opaque *info = Dynarr_atp (pdump_opaques, i);
|
458
|
872 PDUMP_WRITE_ALIGNED (pdump_opaque, *info);
|
|
873 fwrite (info->varaddress, info->size, 1, pdump_out);
|
442
|
874 }
|
|
875 }
|
|
876
|
|
877 static void
|
|
878 pdump_dump_rtables (void)
|
|
879 {
|
452
|
880 int i;
|
460
|
881 pdump_entry_list_elt *elt;
|
442
|
882 pdump_reloc_table rt;
|
|
883
|
|
884 for (i=0; i<lrecord_type_count; i++)
|
|
885 {
|
460
|
886 elt = pdump_object_table[i].first;
|
|
887 if (!elt)
|
442
|
888 continue;
|
|
889 rt.desc = lrecord_implementations_table[i]->description;
|
|
890 rt.count = pdump_object_table[i].count;
|
458
|
891 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
460
|
892 while (elt)
|
442
|
893 {
|
460
|
894 EMACS_INT rdata = pdump_get_entry (elt->obj)->save_offset;
|
458
|
895 PDUMP_WRITE_ALIGNED (EMACS_INT, rdata);
|
460
|
896 elt = elt->next;
|
442
|
897 }
|
|
898 }
|
|
899
|
|
900 rt.desc = 0;
|
|
901 rt.count = 0;
|
458
|
902 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
442
|
903
|
|
904 for (i=0; i<pdump_struct_table.count; i++)
|
|
905 {
|
460
|
906 elt = pdump_struct_table.list[i].list.first;
|
442
|
907 rt.desc = pdump_struct_table.list[i].sdesc->description;
|
|
908 rt.count = pdump_struct_table.list[i].list.count;
|
458
|
909 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
460
|
910 while (elt)
|
442
|
911 {
|
460
|
912 EMACS_INT rdata = pdump_get_entry (elt->obj)->save_offset;
|
452
|
913 int j;
|
460
|
914 for (j=0; j<elt->count; j++)
|
442
|
915 {
|
458
|
916 PDUMP_WRITE_ALIGNED (EMACS_INT, rdata);
|
460
|
917 rdata += elt->size;
|
442
|
918 }
|
460
|
919 elt = elt->next;
|
442
|
920 }
|
|
921 }
|
|
922 rt.desc = 0;
|
|
923 rt.count = 0;
|
458
|
924 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
442
|
925 }
|
|
926
|
|
927 static void
|
458
|
928 pdump_dump_root_objects (void)
|
442
|
929 {
|
665
|
930 Elemcount count = (Dynarr_length (pdump_root_objects) +
|
647
|
931 Dynarr_length (pdump_weak_object_chains));
|
665
|
932 Elemcount i;
|
442
|
933
|
665
|
934 PDUMP_WRITE_ALIGNED (Elemcount, count);
|
458
|
935 PDUMP_ALIGN_OUTPUT (pdump_static_Lisp_Object);
|
442
|
936
|
647
|
937 for (i = 0; i < Dynarr_length (pdump_root_objects); i++)
|
442
|
938 {
|
458
|
939 pdump_static_Lisp_Object obj;
|
|
940 obj.address = Dynarr_at (pdump_root_objects, i);
|
|
941 obj.value = * obj.address;
|
460
|
942
|
458
|
943 if (POINTER_TYPE_P (XTYPE (obj.value)))
|
619
|
944 obj.value =
|
|
945 wrap_pointer_1 ((void *) pdump_get_entry (XRECORD_LHEADER
|
617
|
946 (obj.value))->save_offset);
|
460
|
947
|
458
|
948 PDUMP_WRITE (pdump_static_Lisp_Object, obj);
|
442
|
949 }
|
|
950
|
452
|
951 for (i=0; i<Dynarr_length (pdump_weak_object_chains); i++)
|
442
|
952 {
|
460
|
953 pdump_entry_list_elt *elt;
|
458
|
954 pdump_static_Lisp_Object obj;
|
442
|
955
|
458
|
956 obj.address = Dynarr_at (pdump_weak_object_chains, i);
|
|
957 obj.value = * obj.address;
|
460
|
958
|
442
|
959 for (;;)
|
|
960 {
|
|
961 const struct lrecord_description *desc;
|
|
962 int pos;
|
460
|
963 elt = pdump_get_entry (XRECORD_LHEADER (obj.value));
|
|
964 if (elt)
|
442
|
965 break;
|
458
|
966 desc = XRECORD_LHEADER_IMPLEMENTATION (obj.value)->description;
|
442
|
967 for (pos = 0; desc[pos].type != XD_LO_LINK; pos++)
|
|
968 assert (desc[pos].type != XD_END);
|
|
969
|
458
|
970 obj.value = *(Lisp_Object *)(desc[pos].offset + (char *)(XRECORD_LHEADER (obj.value)));
|
442
|
971 }
|
619
|
972 obj.value = wrap_pointer_1 ((void *) elt->save_offset);
|
442
|
973
|
458
|
974 PDUMP_WRITE (pdump_static_Lisp_Object, obj);
|
442
|
975 }
|
|
976 }
|
|
977
|
|
978 void
|
|
979 pdump (void)
|
|
980 {
|
|
981 int i;
|
|
982 Lisp_Object t_console, t_device, t_frame;
|
|
983 int none;
|
458
|
984 pdump_header header;
|
442
|
985
|
460
|
986 pdump_object_table = xnew_array (pdump_entry_list, lrecord_type_count);
|
|
987 pdump_alert_undump_object = xnew_array (int, lrecord_type_count);
|
|
988
|
|
989 assert (ALIGNOF (max_align_t) <= pdump_align_table[0]);
|
|
990
|
|
991 for (i = 0; i < countof (pdump_align_table); i++)
|
|
992 if (pdump_align_table[i] > ALIGNOF (max_align_t))
|
|
993 pdump_align_table[i] = ALIGNOF (max_align_t);
|
|
994
|
446
|
995 flush_all_buffer_local_cache ();
|
|
996
|
442
|
997 /* These appear in a DEFVAR_LISP, which does a staticpro() */
|
452
|
998 t_console = Vterminal_console; Vterminal_console = Qnil;
|
|
999 t_frame = Vterminal_frame; Vterminal_frame = Qnil;
|
|
1000 t_device = Vterminal_device; Vterminal_device = Qnil;
|
442
|
1001
|
452
|
1002 dump_add_opaque (&lrecord_implementations_table,
|
|
1003 lrecord_type_count * sizeof (lrecord_implementations_table[0]));
|
|
1004 dump_add_opaque (&lrecord_markers,
|
|
1005 lrecord_type_count * sizeof (lrecord_markers[0]));
|
442
|
1006
|
460
|
1007 pdump_hash = xnew_array_and_zero (pdump_entry_list_elt *, PDUMP_HASHSIZE);
|
442
|
1008
|
|
1009 for (i=0; i<lrecord_type_count; i++)
|
|
1010 {
|
|
1011 pdump_object_table[i].first = 0;
|
460
|
1012 pdump_object_table[i].align = ALIGNOF (max_align_t);
|
442
|
1013 pdump_object_table[i].count = 0;
|
|
1014 pdump_alert_undump_object[i] = 0;
|
|
1015 }
|
|
1016 pdump_struct_table.count = 0;
|
|
1017 pdump_struct_table.size = -1;
|
|
1018
|
|
1019 pdump_opaque_data_list.first = 0;
|
460
|
1020 pdump_opaque_data_list.align = ALIGNOF (max_align_t);
|
442
|
1021 pdump_opaque_data_list.count = 0;
|
|
1022 depth = 0;
|
|
1023
|
452
|
1024 for (i=0; i<Dynarr_length (pdump_root_objects); i++)
|
|
1025 pdump_register_object (* Dynarr_at (pdump_root_objects, i));
|
442
|
1026
|
|
1027 none = 1;
|
|
1028 for (i=0; i<lrecord_type_count; i++)
|
|
1029 if (pdump_alert_undump_object[i])
|
|
1030 {
|
|
1031 if (none)
|
|
1032 printf ("Undumpable types list :\n");
|
|
1033 none = 0;
|
|
1034 printf (" - %s (%d)\n", lrecord_implementations_table[i]->name, pdump_alert_undump_object[i]);
|
|
1035 }
|
|
1036 if (!none)
|
|
1037 return;
|
|
1038
|
452
|
1039 for (i=0; i<Dynarr_length (pdump_root_struct_ptrs); i++)
|
|
1040 {
|
|
1041 pdump_root_struct_ptr info = Dynarr_at (pdump_root_struct_ptrs, i);
|
|
1042 pdump_register_struct (*(info.ptraddress), info.desc, 1);
|
|
1043 }
|
442
|
1044
|
458
|
1045 memcpy (header.signature, PDUMP_SIGNATURE, PDUMP_SIGNATURE_LEN);
|
|
1046 header.id = dump_id;
|
|
1047 header.reloc_address = 0;
|
|
1048 header.nb_root_struct_ptrs = Dynarr_length (pdump_root_struct_ptrs);
|
|
1049 header.nb_opaques = Dynarr_length (pdump_opaques);
|
442
|
1050
|
458
|
1051 cur_offset = ALIGN_SIZE (sizeof (header), ALIGNOF (max_align_t));
|
442
|
1052 max_size = 0;
|
|
1053
|
|
1054 pdump_scan_by_alignment (pdump_allocate_offset);
|
458
|
1055 cur_offset = ALIGN_SIZE (cur_offset, ALIGNOF (max_align_t));
|
|
1056 header.stab_offset = cur_offset;
|
442
|
1057
|
|
1058 pdump_buf = xmalloc (max_size);
|
|
1059 /* Avoid use of the `open' macro. We want the real function. */
|
|
1060 #undef open
|
|
1061 pdump_fd = open (EMACS_PROGNAME ".dmp",
|
|
1062 O_WRONLY | O_CREAT | O_TRUNC | OPEN_BINARY, 0666);
|
458
|
1063 pdump_out = fdopen (pdump_fd, "w");
|
442
|
1064
|
458
|
1065 fwrite (&header, sizeof (header), 1, pdump_out);
|
|
1066 PDUMP_ALIGN_OUTPUT (max_align_t);
|
442
|
1067
|
|
1068 pdump_scan_by_alignment (pdump_dump_data);
|
|
1069
|
458
|
1070 fseek (pdump_out, header.stab_offset, SEEK_SET);
|
442
|
1071
|
458
|
1072 pdump_dump_root_struct_ptrs ();
|
452
|
1073 pdump_dump_opaques ();
|
442
|
1074 pdump_dump_rtables ();
|
458
|
1075 pdump_dump_root_objects ();
|
442
|
1076
|
458
|
1077 fclose (pdump_out);
|
442
|
1078 close (pdump_fd);
|
458
|
1079
|
442
|
1080 free (pdump_buf);
|
|
1081
|
|
1082 free (pdump_hash);
|
|
1083
|
|
1084 Vterminal_console = t_console;
|
|
1085 Vterminal_frame = t_frame;
|
|
1086 Vterminal_device = t_device;
|
|
1087 }
|
|
1088
|
452
|
1089 static int
|
|
1090 pdump_load_check (void)
|
442
|
1091 {
|
452
|
1092 return (!memcmp (((pdump_header *)pdump_start)->signature,
|
|
1093 PDUMP_SIGNATURE, PDUMP_SIGNATURE_LEN)
|
|
1094 && ((pdump_header *)pdump_start)->id == dump_id);
|
442
|
1095 }
|
|
1096
|
458
|
1097 /*----------------------------------------------------------------------*/
|
|
1098 /* Reading the dump file */
|
|
1099 /*----------------------------------------------------------------------*/
|
452
|
1100 static int
|
|
1101 pdump_load_finish (void)
|
442
|
1102 {
|
|
1103 int i;
|
|
1104 char *p;
|
|
1105 EMACS_INT delta;
|
|
1106 EMACS_INT count;
|
458
|
1107 pdump_header *header = (pdump_header *)pdump_start;
|
442
|
1108
|
|
1109 pdump_end = pdump_start + pdump_length;
|
|
1110
|
458
|
1111 delta = ((EMACS_INT)pdump_start) - header->reloc_address;
|
|
1112 p = pdump_start + header->stab_offset;
|
442
|
1113
|
452
|
1114 /* Put back the pdump_root_struct_ptrs */
|
458
|
1115 p = (char *) ALIGN_PTR (p, ALIGNOF (pdump_static_pointer));
|
|
1116 for (i=0; i<header->nb_root_struct_ptrs; i++)
|
442
|
1117 {
|
458
|
1118 pdump_static_pointer ptr = PDUMP_READ (p, pdump_static_pointer);
|
|
1119 (* ptr.address) = ptr.value + delta;
|
442
|
1120 }
|
|
1121
|
452
|
1122 /* Put back the pdump_opaques */
|
458
|
1123 for (i=0; i<header->nb_opaques; i++)
|
442
|
1124 {
|
458
|
1125 pdump_opaque info = PDUMP_READ_ALIGNED (p, pdump_opaque);
|
545
|
1126 memcpy ((void*)info.varaddress, p, info.size);
|
452
|
1127 p += info.size;
|
442
|
1128 }
|
|
1129
|
|
1130 /* Do the relocations */
|
|
1131 pdump_rt_list = p;
|
|
1132 count = 2;
|
|
1133 for (;;)
|
|
1134 {
|
458
|
1135 pdump_reloc_table rt = PDUMP_READ_ALIGNED (p, pdump_reloc_table);
|
|
1136 p = (char *) ALIGN_PTR (p, ALIGNOF (char *));
|
442
|
1137 if (rt.desc)
|
|
1138 {
|
458
|
1139 char **reloc = (char **)p;
|
442
|
1140 for (i=0; i < rt.count; i++)
|
|
1141 {
|
458
|
1142 reloc[i] += delta;
|
|
1143 pdump_reloc_one (reloc[i], delta, rt.desc);
|
442
|
1144 }
|
458
|
1145 p += rt.count * sizeof (char *);
|
442
|
1146 } else
|
|
1147 if (!(--count))
|
|
1148 break;
|
|
1149 }
|
|
1150
|
452
|
1151 /* Put the pdump_root_objects variables in place */
|
665
|
1152 i = PDUMP_READ_ALIGNED (p, Elemcount);
|
458
|
1153 p = (char *) ALIGN_PTR (p, ALIGNOF (pdump_static_Lisp_Object));
|
|
1154 while (i--)
|
442
|
1155 {
|
458
|
1156 pdump_static_Lisp_Object obj = PDUMP_READ (p, pdump_static_Lisp_Object);
|
442
|
1157
|
458
|
1158 if (POINTER_TYPE_P (XTYPE (obj.value)))
|
619
|
1159 obj.value = wrap_pointer_1 ((char *) XPNTR (obj.value) + delta);
|
442
|
1160
|
458
|
1161 (* obj.address) = obj.value;
|
442
|
1162 }
|
|
1163
|
|
1164 /* Final cleanups */
|
|
1165 /* reorganize hash tables */
|
|
1166 p = pdump_rt_list;
|
|
1167 for (;;)
|
|
1168 {
|
458
|
1169 pdump_reloc_table rt = PDUMP_READ_ALIGNED (p, pdump_reloc_table);
|
|
1170 p = (char *) ALIGN_PTR (p, ALIGNOF (Lisp_Object));
|
442
|
1171 if (!rt.desc)
|
|
1172 break;
|
|
1173 if (rt.desc == hash_table_description)
|
|
1174 {
|
|
1175 for (i=0; i < rt.count; i++)
|
|
1176 pdump_reorganize_hash_table (PDUMP_READ (p, Lisp_Object));
|
|
1177 break;
|
|
1178 } else
|
|
1179 p += sizeof (Lisp_Object) * rt.count;
|
|
1180 }
|
|
1181
|
|
1182 return 1;
|
|
1183 }
|
|
1184
|
|
1185 #ifdef WIN32_NATIVE
|
|
1186 /* Free the mapped file if we decide we don't want it after all */
|
452
|
1187 static void
|
|
1188 pdump_file_unmap (void)
|
442
|
1189 {
|
|
1190 UnmapViewOfFile (pdump_start);
|
|
1191 CloseHandle (pdump_hFile);
|
|
1192 CloseHandle (pdump_hMap);
|
|
1193 }
|
|
1194
|
452
|
1195 static int
|
|
1196 pdump_file_get (const char *path)
|
442
|
1197 {
|
|
1198
|
|
1199 pdump_hFile = CreateFile (path,
|
|
1200 GENERIC_READ + GENERIC_WRITE, /* Required for copy on write */
|
|
1201 0, /* Not shared */
|
|
1202 NULL, /* Not inheritable */
|
|
1203 OPEN_EXISTING,
|
|
1204 FILE_ATTRIBUTE_NORMAL,
|
|
1205 NULL); /* No template file */
|
|
1206 if (pdump_hFile == INVALID_HANDLE_VALUE)
|
|
1207 return 0;
|
|
1208
|
|
1209 pdump_length = GetFileSize (pdump_hFile, NULL);
|
|
1210 pdump_hMap = CreateFileMapping (pdump_hFile,
|
|
1211 NULL, /* No security attributes */
|
|
1212 PAGE_WRITECOPY, /* Copy on write */
|
|
1213 0, /* Max size, high half */
|
|
1214 0, /* Max size, low half */
|
|
1215 NULL); /* Unnamed */
|
|
1216 if (pdump_hMap == INVALID_HANDLE_VALUE)
|
|
1217 return 0;
|
|
1218
|
|
1219 pdump_start = MapViewOfFile (pdump_hMap,
|
452
|
1220 FILE_MAP_COPY, /* Copy on write */
|
442
|
1221 0, /* Start at zero */
|
|
1222 0,
|
|
1223 0); /* Map all of it */
|
|
1224 pdump_free = pdump_file_unmap;
|
|
1225 return 1;
|
|
1226 }
|
|
1227
|
|
1228 /* pdump_resource_free is called (via the pdump_free pointer) to release
|
|
1229 any resources allocated by pdump_resource_get. Since the Windows API
|
|
1230 specs specifically state that you don't need to (and shouldn't) free the
|
|
1231 resources allocated by FindResource, LoadResource, and LockResource this
|
|
1232 routine does nothing. */
|
452
|
1233 static void
|
|
1234 pdump_resource_free (void)
|
442
|
1235 {
|
|
1236 }
|
|
1237
|
452
|
1238 static int
|
|
1239 pdump_resource_get (void)
|
442
|
1240 {
|
452
|
1241 HRSRC hRes; /* Handle to dump resource */
|
|
1242 HRSRC hResLoad; /* Handle to loaded dump resource */
|
442
|
1243
|
|
1244 /* See Q126630 which describes how Windows NT and 95 trap writes to
|
|
1245 resource sections and duplicate the page to allow the write to proceed.
|
|
1246 It also describes how to make the resource section read/write (and hence
|
|
1247 private to each process). Doing this avoids the exceptions and related
|
|
1248 overhead, but causes the resource section to be private to each process
|
|
1249 that is running XEmacs. Since the resource section contains little
|
|
1250 other than the dumped data, which should be private to each process, we
|
|
1251 make the whole resource section read/write so we don't have to copy it. */
|
|
1252
|
|
1253 hRes = FindResource (NULL, MAKEINTRESOURCE(101), "DUMP");
|
|
1254 if (hRes == NULL)
|
|
1255 return 0;
|
|
1256
|
|
1257 /* Found it, use the data in the resource */
|
|
1258 hResLoad = LoadResource (NULL, hRes);
|
|
1259 if (hResLoad == NULL)
|
|
1260 return 0;
|
|
1261
|
|
1262 pdump_start = LockResource (hResLoad);
|
|
1263 if (pdump_start == NULL)
|
|
1264 return 0;
|
|
1265
|
|
1266 pdump_free = pdump_resource_free;
|
|
1267 pdump_length = SizeofResource (NULL, hRes);
|
665
|
1268 if (pdump_length <= (Bytecount) sizeof (pdump_header))
|
442
|
1269 {
|
|
1270 pdump_start = 0;
|
|
1271 return 0;
|
|
1272 }
|
|
1273
|
|
1274 return 1;
|
|
1275 }
|
|
1276
|
|
1277 #else /* !WIN32_NATIVE */
|
|
1278
|
452
|
1279 static void
|
|
1280 pdump_file_free (void)
|
442
|
1281 {
|
460
|
1282 xfree (pdump_start);
|
442
|
1283 }
|
|
1284
|
|
1285 #ifdef HAVE_MMAP
|
452
|
1286 static void
|
|
1287 pdump_file_unmap (void)
|
442
|
1288 {
|
|
1289 munmap (pdump_start, pdump_length);
|
|
1290 }
|
|
1291 #endif
|
|
1292
|
452
|
1293 static int
|
|
1294 pdump_file_get (const char *path)
|
442
|
1295 {
|
|
1296 int fd = open (path, O_RDONLY | OPEN_BINARY);
|
|
1297 if (fd<0)
|
|
1298 return 0;
|
|
1299
|
|
1300 pdump_length = lseek (fd, 0, SEEK_END);
|
665
|
1301 if (pdump_length < (Bytecount) sizeof (pdump_header))
|
442
|
1302 {
|
|
1303 close (fd);
|
|
1304 return 0;
|
|
1305 }
|
|
1306
|
|
1307 lseek (fd, 0, SEEK_SET);
|
|
1308
|
|
1309 #ifdef HAVE_MMAP
|
456
|
1310 /* Unix 98 requires that sys/mman.h define MAP_FAILED,
|
|
1311 but many earlier implementations don't. */
|
|
1312 # ifndef MAP_FAILED
|
|
1313 # define MAP_FAILED ((void *) -1L)
|
|
1314 # endif
|
442
|
1315 pdump_start = (char *) mmap (0, pdump_length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
|
452
|
1316 if (pdump_start != (char *) MAP_FAILED)
|
442
|
1317 {
|
|
1318 pdump_free = pdump_file_unmap;
|
|
1319 close (fd);
|
|
1320 return 1;
|
|
1321 }
|
456
|
1322 #endif /* HAVE_MMAP */
|
442
|
1323
|
460
|
1324 pdump_start = xnew_array (char, pdump_length);
|
442
|
1325 pdump_free = pdump_file_free;
|
446
|
1326 read (fd, pdump_start, pdump_length);
|
442
|
1327
|
446
|
1328 close (fd);
|
442
|
1329 return 1;
|
|
1330 }
|
|
1331 #endif /* !WIN32_NATIVE */
|
|
1332
|
|
1333
|
452
|
1334 static int
|
|
1335 pdump_file_try (char *exe_path)
|
442
|
1336 {
|
460
|
1337 char *w = exe_path + strlen (exe_path);
|
442
|
1338
|
|
1339 do
|
|
1340 {
|
|
1341 sprintf (w, "-%s-%08x.dmp", EMACS_VERSION, dump_id);
|
|
1342 if (pdump_file_get (exe_path))
|
|
1343 {
|
|
1344 if (pdump_load_check ())
|
|
1345 return 1;
|
452
|
1346 pdump_free ();
|
442
|
1347 }
|
|
1348
|
|
1349 sprintf (w, "-%08x.dmp", dump_id);
|
|
1350 if (pdump_file_get (exe_path))
|
|
1351 {
|
|
1352 if (pdump_load_check ())
|
|
1353 return 1;
|
452
|
1354 pdump_free ();
|
442
|
1355 }
|
|
1356
|
|
1357 sprintf (w, ".dmp");
|
|
1358 if (pdump_file_get (exe_path))
|
|
1359 {
|
|
1360 if (pdump_load_check ())
|
|
1361 return 1;
|
452
|
1362 pdump_free ();
|
442
|
1363 }
|
|
1364
|
|
1365 do
|
|
1366 w--;
|
|
1367 while (w>exe_path && !IS_DIRECTORY_SEP (*w) && (*w != '-') && (*w != '.'));
|
|
1368 }
|
|
1369 while (w>exe_path && !IS_DIRECTORY_SEP (*w));
|
|
1370 return 0;
|
|
1371 }
|
|
1372
|
452
|
1373 int
|
|
1374 pdump_load (const char *argv0)
|
442
|
1375 {
|
|
1376 char exe_path[PATH_MAX];
|
|
1377 #ifdef WIN32_NATIVE
|
|
1378 GetModuleFileName (NULL, exe_path, PATH_MAX);
|
|
1379 #else /* !WIN32_NATIVE */
|
|
1380 char *w;
|
|
1381 const char *dir, *p;
|
|
1382
|
|
1383 dir = argv0;
|
|
1384 if (dir[0] == '-')
|
|
1385 {
|
|
1386 /* XEmacs as a login shell, oh goody! */
|
452
|
1387 dir = getenv ("SHELL");
|
442
|
1388 }
|
|
1389
|
452
|
1390 p = dir + strlen (dir);
|
442
|
1391 while (p != dir && !IS_ANY_SEP (p[-1])) p--;
|
|
1392
|
|
1393 if (p != dir)
|
|
1394 {
|
|
1395 /* invocation-name includes a directory component -- presumably it
|
|
1396 is relative to cwd, not $PATH */
|
|
1397 strcpy (exe_path, dir);
|
|
1398 }
|
|
1399 else
|
|
1400 {
|
|
1401 const char *path = getenv ("PATH");
|
|
1402 const char *name = p;
|
|
1403 for (;;)
|
|
1404 {
|
|
1405 p = path;
|
|
1406 while (*p && *p != SEPCHAR)
|
|
1407 p++;
|
|
1408 if (p == path)
|
|
1409 {
|
|
1410 exe_path[0] = '.';
|
|
1411 w = exe_path + 1;
|
|
1412 }
|
|
1413 else
|
|
1414 {
|
|
1415 memcpy (exe_path, path, p - path);
|
|
1416 w = exe_path + (p - path);
|
|
1417 }
|
|
1418 if (!IS_DIRECTORY_SEP (w[-1]))
|
|
1419 {
|
|
1420 *w++ = '/';
|
|
1421 }
|
452
|
1422 strcpy (w, name);
|
442
|
1423
|
|
1424 /* ### #$%$#^$^@%$^#%@$ ! */
|
|
1425 #ifdef access
|
|
1426 #undef access
|
|
1427 #endif
|
|
1428
|
|
1429 if (!access (exe_path, X_OK))
|
|
1430 break;
|
|
1431 if (!*p)
|
|
1432 {
|
|
1433 /* Oh well, let's have some kind of default */
|
|
1434 sprintf (exe_path, "./%s", name);
|
|
1435 break;
|
|
1436 }
|
|
1437 path = p+1;
|
|
1438 }
|
|
1439 }
|
|
1440 #endif /* WIN32_NATIVE */
|
|
1441
|
|
1442 if (pdump_file_try (exe_path))
|
|
1443 {
|
|
1444 pdump_load_finish ();
|
|
1445 return 1;
|
|
1446 }
|
|
1447
|
|
1448 #ifdef WIN32_NATIVE
|
|
1449 if (pdump_resource_get ())
|
|
1450 {
|
|
1451 if (pdump_load_check ())
|
|
1452 {
|
|
1453 pdump_load_finish ();
|
|
1454 return 1;
|
|
1455 }
|
|
1456 pdump_free ();
|
|
1457 }
|
|
1458 #endif
|
|
1459
|
|
1460 return 0;
|
|
1461 }
|