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;
|
647
|
48 Memory_Count 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
|
647
|
87 dump_add_opaque (const void *varaddress, Memory_Count 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
|
647
|
132 pdump_align_stream (FILE *stream, Memory_Count 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;
|
647
|
218 static Memory_Count 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
|
|
238 pdump_size_to_align (Memory_Count 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;
|
647
|
247 Memory_Count 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;
|
647
|
279 static Memory_Count 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
|
647
|
316 pdump_add_entry (pdump_entry_list *list, const void *obj, Memory_Count 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 {
|
647
|
426 case XD_MEMORY_COUNT:
|
|
427 count = *(Memory_Count *)irdata;
|
|
428 break;
|
|
429 case XD_ELEMENT_COUNT:
|
|
430 count = *(Element_Count *)irdata;
|
|
431 break;
|
|
432 case XD_HASH_CODE:
|
|
433 count = *(Hash_Code *)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 case XD_BYTECOUNT:
|
|
442 count = *(Bytecount *)irdata;
|
|
443 break;
|
|
444 default:
|
458
|
445 stderr_out ("Unsupported count type : %d (line = %d, code=%ld)\n",
|
|
446 idesc[line].type, line, (long)code);
|
442
|
447 pdump_backtrace ();
|
647
|
448 count = 0; /* warning suppression */
|
442
|
449 abort ();
|
|
450 }
|
|
451 count += delta;
|
|
452 return count;
|
|
453 }
|
|
454
|
|
455 static void
|
|
456 pdump_register_sub (const void *data, const struct lrecord_description *desc, int me)
|
|
457 {
|
|
458 int pos;
|
|
459
|
|
460 restart:
|
|
461 for (pos = 0; desc[pos].type != XD_END; pos++)
|
|
462 {
|
|
463 const void *rdata = (const char *)data + desc[pos].offset;
|
|
464
|
|
465 backtrace[me].position = pos;
|
|
466 backtrace[me].offset = desc[pos].offset;
|
|
467
|
|
468 switch (desc[pos].type)
|
|
469 {
|
|
470 case XD_SPECIFIER_END:
|
|
471 pos = 0;
|
|
472 desc = ((const Lisp_Specifier *)data)->methods->extra_description;
|
|
473 goto restart;
|
647
|
474 case XD_MEMORY_COUNT:
|
|
475 case XD_ELEMENT_COUNT:
|
|
476 case XD_HASH_CODE:
|
442
|
477 case XD_INT:
|
|
478 case XD_LONG:
|
|
479 case XD_BYTECOUNT:
|
|
480 case XD_INT_RESET:
|
|
481 case XD_LO_LINK:
|
|
482 break;
|
|
483 case XD_OPAQUE_DATA_PTR:
|
|
484 {
|
|
485 EMACS_INT count = desc[pos].data1;
|
|
486 if (XD_IS_INDIRECT (count))
|
|
487 count = pdump_get_indirect_count (count, desc, data);
|
|
488
|
|
489 pdump_add_entry (&pdump_opaque_data_list,
|
458
|
490 *(void **)rdata, count, 1);
|
442
|
491 break;
|
|
492 }
|
|
493 case XD_C_STRING:
|
|
494 {
|
|
495 const char *str = *(const char **)rdata;
|
|
496 if (str)
|
458
|
497 pdump_add_entry (&pdump_opaque_data_list, str, strlen (str)+1, 1);
|
442
|
498 break;
|
|
499 }
|
|
500 case XD_DOC_STRING:
|
|
501 {
|
|
502 const char *str = *(const char **)rdata;
|
|
503 if ((EMACS_INT)str > 0)
|
458
|
504 pdump_add_entry (&pdump_opaque_data_list, str, strlen (str)+1, 1);
|
442
|
505 break;
|
|
506 }
|
|
507 case XD_LISP_OBJECT:
|
|
508 {
|
|
509 const Lisp_Object *pobj = (const Lisp_Object *)rdata;
|
|
510
|
|
511 assert (desc[pos].data1 == 0);
|
|
512
|
|
513 backtrace[me].offset = (const char *)pobj - (const char *)data;
|
|
514 pdump_register_object (*pobj);
|
|
515 break;
|
|
516 }
|
|
517 case XD_LISP_OBJECT_ARRAY:
|
|
518 {
|
|
519 int i;
|
|
520 EMACS_INT count = desc[pos].data1;
|
|
521 if (XD_IS_INDIRECT (count))
|
|
522 count = pdump_get_indirect_count (count, desc, data);
|
|
523
|
|
524 for (i = 0; i < count; i++)
|
|
525 {
|
|
526 const Lisp_Object *pobj = ((const Lisp_Object *)rdata) + i;
|
|
527 Lisp_Object dobj = *pobj;
|
|
528
|
|
529 backtrace[me].offset = (const char *)pobj - (const char *)data;
|
|
530 pdump_register_object (dobj);
|
|
531 }
|
|
532 break;
|
|
533 }
|
|
534 case XD_STRUCT_PTR:
|
|
535 {
|
|
536 EMACS_INT count = desc[pos].data1;
|
|
537 const struct struct_description *sdesc = desc[pos].data2;
|
|
538 const char *dobj = *(const char **)rdata;
|
|
539 if (dobj)
|
|
540 {
|
|
541 if (XD_IS_INDIRECT (count))
|
|
542 count = pdump_get_indirect_count (count, desc, data);
|
|
543
|
|
544 pdump_register_struct (dobj, sdesc, count);
|
|
545 }
|
|
546 break;
|
|
547 }
|
|
548 default:
|
|
549 stderr_out ("Unsupported dump type : %d\n", desc[pos].type);
|
|
550 pdump_backtrace ();
|
|
551 abort ();
|
|
552 };
|
|
553 }
|
|
554 }
|
|
555
|
|
556 static void
|
|
557 pdump_register_object (Lisp_Object obj)
|
|
558 {
|
|
559 struct lrecord_header *objh;
|
458
|
560 const struct lrecord_implementation *imp;
|
442
|
561
|
|
562 if (!POINTER_TYPE_P (XTYPE (obj)))
|
|
563 return;
|
|
564
|
|
565 objh = XRECORD_LHEADER (obj);
|
|
566 if (!objh)
|
|
567 return;
|
|
568
|
|
569 if (pdump_get_entry (objh))
|
|
570 return;
|
|
571
|
458
|
572 imp = LHEADER_IMPLEMENTATION (objh);
|
|
573
|
|
574 if (imp->description)
|
442
|
575 {
|
|
576 int me = depth++;
|
|
577 if (me>65536)
|
|
578 {
|
|
579 stderr_out ("Backtrace overflow, loop ?\n");
|
|
580 abort ();
|
|
581 }
|
|
582 backtrace[me].obj = objh;
|
|
583 backtrace[me].position = 0;
|
|
584 backtrace[me].offset = 0;
|
|
585
|
|
586 pdump_add_entry (pdump_object_table + objh->type,
|
|
587 objh,
|
458
|
588 imp->static_size ?
|
|
589 imp->static_size :
|
|
590 imp->size_in_bytes_method (objh),
|
442
|
591 1);
|
458
|
592 pdump_register_sub (objh, imp->description, me);
|
442
|
593 --depth;
|
|
594 }
|
|
595 else
|
|
596 {
|
|
597 pdump_alert_undump_object[objh->type]++;
|
458
|
598 stderr_out ("Undumpable object type : %s\n", imp->name);
|
442
|
599 pdump_backtrace ();
|
|
600 }
|
|
601 }
|
|
602
|
|
603 static void
|
458
|
604 pdump_register_struct (const void *data,
|
|
605 const struct struct_description *sdesc,
|
|
606 int count)
|
442
|
607 {
|
|
608 if (data && !pdump_get_entry (data))
|
|
609 {
|
|
610 int me = depth++;
|
|
611 int i;
|
|
612 if (me>65536)
|
|
613 {
|
|
614 stderr_out ("Backtrace overflow, loop ?\n");
|
|
615 abort ();
|
|
616 }
|
|
617 backtrace[me].obj = 0;
|
|
618 backtrace[me].position = 0;
|
|
619 backtrace[me].offset = 0;
|
|
620
|
|
621 pdump_add_entry (pdump_get_entry_list (sdesc),
|
458
|
622 data, sdesc->size, count);
|
442
|
623 for (i=0; i<count; i++)
|
|
624 {
|
|
625 pdump_register_sub (((char *)data) + sdesc->size*i,
|
|
626 sdesc->description,
|
|
627 me);
|
|
628 }
|
|
629 --depth;
|
|
630 }
|
|
631 }
|
|
632
|
|
633 static void
|
460
|
634 pdump_dump_data (pdump_entry_list_elt *elt,
|
458
|
635 const struct lrecord_description *desc)
|
442
|
636 {
|
647
|
637 Memory_Count size = elt->size;
|
460
|
638 int count = elt->count;
|
442
|
639 if (desc)
|
|
640 {
|
|
641 int pos, i;
|
460
|
642 memcpy (pdump_buf, elt->obj, size*count);
|
442
|
643
|
|
644 for (i=0; i<count; i++)
|
|
645 {
|
|
646 char *cur = ((char *)pdump_buf) + i*size;
|
|
647 restart:
|
|
648 for (pos = 0; desc[pos].type != XD_END; pos++)
|
|
649 {
|
|
650 void *rdata = cur + desc[pos].offset;
|
|
651 switch (desc[pos].type)
|
|
652 {
|
|
653 case XD_SPECIFIER_END:
|
460
|
654 desc = ((const Lisp_Specifier *)(elt->obj))->methods->extra_description;
|
442
|
655 goto restart;
|
647
|
656 case XD_MEMORY_COUNT:
|
|
657 case XD_ELEMENT_COUNT:
|
|
658 case XD_HASH_CODE:
|
442
|
659 case XD_INT:
|
|
660 case XD_LONG:
|
|
661 case XD_BYTECOUNT:
|
|
662 break;
|
|
663 case XD_INT_RESET:
|
|
664 {
|
|
665 EMACS_INT val = desc[pos].data1;
|
|
666 if (XD_IS_INDIRECT (val))
|
460
|
667 val = pdump_get_indirect_count (val, desc, elt->obj);
|
442
|
668 *(int *)rdata = val;
|
|
669 break;
|
|
670 }
|
|
671 case XD_OPAQUE_DATA_PTR:
|
|
672 case XD_C_STRING:
|
|
673 case XD_STRUCT_PTR:
|
|
674 {
|
|
675 void *ptr = *(void **)rdata;
|
|
676 if (ptr)
|
|
677 *(EMACS_INT *)rdata = pdump_get_entry (ptr)->save_offset;
|
|
678 break;
|
|
679 }
|
|
680 case XD_LO_LINK:
|
|
681 {
|
|
682 Lisp_Object obj = *(Lisp_Object *)rdata;
|
460
|
683 pdump_entry_list_elt *elt1;
|
442
|
684 for (;;)
|
|
685 {
|
460
|
686 elt1 = pdump_get_entry (XRECORD_LHEADER (obj));
|
|
687 if (elt1)
|
442
|
688 break;
|
|
689 obj = *(Lisp_Object *)(desc[pos].offset + (char *)(XRECORD_LHEADER (obj)));
|
|
690 }
|
460
|
691 *(EMACS_INT *)rdata = elt1->save_offset;
|
442
|
692 break;
|
|
693 }
|
|
694 case XD_LISP_OBJECT:
|
|
695 {
|
|
696 Lisp_Object *pobj = (Lisp_Object *) rdata;
|
|
697
|
|
698 assert (desc[pos].data1 == 0);
|
|
699
|
|
700 if (POINTER_TYPE_P (XTYPE (*pobj)) && XRECORD_LHEADER (*pobj))
|
|
701 *(EMACS_INT *)pobj =
|
|
702 pdump_get_entry (XRECORD_LHEADER (*pobj))->save_offset;
|
|
703 break;
|
|
704 }
|
|
705 case XD_LISP_OBJECT_ARRAY:
|
|
706 {
|
|
707 EMACS_INT num = desc[pos].data1;
|
|
708 int j;
|
|
709 if (XD_IS_INDIRECT (num))
|
460
|
710 num = pdump_get_indirect_count (num, desc, elt->obj);
|
442
|
711
|
|
712 for (j=0; j<num; j++)
|
|
713 {
|
|
714 Lisp_Object *pobj = ((Lisp_Object *)rdata) + j;
|
|
715 if (POINTER_TYPE_P (XTYPE (*pobj)) && XRECORD_LHEADER (*pobj))
|
|
716 *(EMACS_INT *)pobj =
|
|
717 pdump_get_entry (XRECORD_LHEADER (*pobj))->save_offset;
|
|
718 }
|
|
719 break;
|
|
720 }
|
|
721 case XD_DOC_STRING:
|
|
722 {
|
|
723 EMACS_INT str = *(EMACS_INT *)rdata;
|
|
724 if (str > 0)
|
|
725 *(EMACS_INT *)rdata = pdump_get_entry ((void *)str)->save_offset;
|
|
726 break;
|
|
727 }
|
|
728 default:
|
|
729 stderr_out ("Unsupported dump type : %d\n", desc[pos].type);
|
|
730 abort ();
|
458
|
731 }
|
442
|
732 }
|
|
733 }
|
|
734 }
|
460
|
735 fwrite (desc ? pdump_buf : elt->obj, size, count, pdump_out);
|
442
|
736 }
|
|
737
|
|
738 static void
|
458
|
739 pdump_reloc_one (void *data, EMACS_INT delta,
|
|
740 const struct lrecord_description *desc)
|
442
|
741 {
|
|
742 int pos;
|
|
743
|
|
744 restart:
|
|
745 for (pos = 0; desc[pos].type != XD_END; pos++)
|
|
746 {
|
|
747 void *rdata = (char *)data + desc[pos].offset;
|
|
748 switch (desc[pos].type)
|
|
749 {
|
|
750 case XD_SPECIFIER_END:
|
|
751 pos = 0;
|
|
752 desc = ((const Lisp_Specifier *)data)->methods->extra_description;
|
|
753 goto restart;
|
647
|
754 case XD_MEMORY_COUNT:
|
|
755 case XD_ELEMENT_COUNT:
|
|
756 case XD_HASH_CODE:
|
442
|
757 case XD_INT:
|
|
758 case XD_LONG:
|
|
759 case XD_BYTECOUNT:
|
|
760 case XD_INT_RESET:
|
|
761 break;
|
|
762 case XD_OPAQUE_DATA_PTR:
|
|
763 case XD_C_STRING:
|
|
764 case XD_STRUCT_PTR:
|
|
765 case XD_LO_LINK:
|
|
766 {
|
|
767 EMACS_INT ptr = *(EMACS_INT *)rdata;
|
|
768 if (ptr)
|
|
769 *(EMACS_INT *)rdata = ptr+delta;
|
|
770 break;
|
|
771 }
|
|
772 case XD_LISP_OBJECT:
|
|
773 {
|
|
774 Lisp_Object *pobj = (Lisp_Object *) rdata;
|
|
775
|
|
776 assert (desc[pos].data1 == 0);
|
|
777
|
|
778 if (POINTER_TYPE_P (XTYPE (*pobj))
|
|
779 && ! EQ (*pobj, Qnull_pointer))
|
|
780 XSETOBJ (*pobj, (char *) XPNTR (*pobj) + delta);
|
|
781
|
|
782 break;
|
|
783 }
|
|
784 case XD_LISP_OBJECT_ARRAY:
|
|
785 {
|
|
786 EMACS_INT num = desc[pos].data1;
|
|
787 int j;
|
|
788 if (XD_IS_INDIRECT (num))
|
|
789 num = pdump_get_indirect_count (num, desc, data);
|
|
790
|
|
791 for (j=0; j<num; j++)
|
|
792 {
|
|
793 Lisp_Object *pobj = (Lisp_Object *) rdata + j;
|
|
794
|
|
795 if (POINTER_TYPE_P (XTYPE (*pobj))
|
|
796 && ! EQ (*pobj, Qnull_pointer))
|
|
797 XSETOBJ (*pobj, (char *) XPNTR (*pobj) + delta);
|
|
798 }
|
|
799 break;
|
|
800 }
|
|
801 case XD_DOC_STRING:
|
|
802 {
|
|
803 EMACS_INT str = *(EMACS_INT *)rdata;
|
|
804 if (str > 0)
|
|
805 *(EMACS_INT *)rdata = str + delta;
|
|
806 break;
|
|
807 }
|
|
808 default:
|
|
809 stderr_out ("Unsupported dump type : %d\n", desc[pos].type);
|
|
810 abort ();
|
|
811 };
|
|
812 }
|
|
813 }
|
|
814
|
|
815 static void
|
460
|
816 pdump_allocate_offset (pdump_entry_list_elt *elt,
|
458
|
817 const struct lrecord_description *desc)
|
442
|
818 {
|
647
|
819 Memory_Count size = elt->count * elt->size;
|
460
|
820 elt->save_offset = cur_offset;
|
442
|
821 if (size>max_size)
|
|
822 max_size = size;
|
|
823 cur_offset += size;
|
|
824 }
|
|
825
|
|
826 static void
|
460
|
827 pdump_scan_by_alignment (void (*f)(pdump_entry_list_elt *,
|
458
|
828 const struct lrecord_description *))
|
442
|
829 {
|
460
|
830 int align;
|
|
831
|
|
832 for (align = ALIGNOF (max_align_t); align; align>>=1)
|
442
|
833 {
|
460
|
834 int i;
|
|
835 pdump_entry_list_elt *elt;
|
|
836
|
442
|
837 for (i=0; i<lrecord_type_count; i++)
|
|
838 if (pdump_object_table[i].align == align)
|
460
|
839 for (elt = pdump_object_table[i].first; elt; elt = elt->next)
|
|
840 f (elt, lrecord_implementations_table[i]->description);
|
442
|
841
|
|
842 for (i=0; i<pdump_struct_table.count; i++)
|
460
|
843 {
|
|
844 pdump_struct_list_elt list = pdump_struct_table.list[i];
|
|
845 if (list.list.align == align)
|
|
846 for (elt = list.list.first; elt; elt = elt->next)
|
|
847 f (elt, list.sdesc->description);
|
|
848 }
|
442
|
849
|
460
|
850 for (elt = pdump_opaque_data_list.first; elt; elt = elt->next)
|
|
851 if (pdump_size_to_align (elt->size) == align)
|
|
852 f (elt, 0);
|
442
|
853 }
|
|
854 }
|
|
855
|
|
856 static void
|
458
|
857 pdump_dump_root_struct_ptrs (void)
|
442
|
858 {
|
|
859 int i;
|
647
|
860 Element_Count count = Dynarr_length (pdump_root_struct_ptrs);
|
458
|
861 pdump_static_pointer *data = alloca_array (pdump_static_pointer, count);
|
|
862 for (i = 0; i < count; i++)
|
442
|
863 {
|
458
|
864 data[i].address = (char **) Dynarr_atp (pdump_root_struct_ptrs, i)->ptraddress;
|
|
865 data[i].value = (char *) pdump_get_entry (* data[i].address)->save_offset;
|
442
|
866 }
|
458
|
867 PDUMP_ALIGN_OUTPUT (pdump_static_pointer);
|
|
868 fwrite (data, sizeof (pdump_static_pointer), count, pdump_out);
|
442
|
869 }
|
|
870
|
|
871 static void
|
452
|
872 pdump_dump_opaques (void)
|
442
|
873 {
|
|
874 int i;
|
452
|
875 for (i = 0; i < Dynarr_length (pdump_opaques); i++)
|
442
|
876 {
|
452
|
877 pdump_opaque *info = Dynarr_atp (pdump_opaques, i);
|
458
|
878 PDUMP_WRITE_ALIGNED (pdump_opaque, *info);
|
|
879 fwrite (info->varaddress, info->size, 1, pdump_out);
|
442
|
880 }
|
|
881 }
|
|
882
|
|
883 static void
|
|
884 pdump_dump_rtables (void)
|
|
885 {
|
452
|
886 int i;
|
460
|
887 pdump_entry_list_elt *elt;
|
442
|
888 pdump_reloc_table rt;
|
|
889
|
|
890 for (i=0; i<lrecord_type_count; i++)
|
|
891 {
|
460
|
892 elt = pdump_object_table[i].first;
|
|
893 if (!elt)
|
442
|
894 continue;
|
|
895 rt.desc = lrecord_implementations_table[i]->description;
|
|
896 rt.count = pdump_object_table[i].count;
|
458
|
897 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
460
|
898 while (elt)
|
442
|
899 {
|
460
|
900 EMACS_INT rdata = pdump_get_entry (elt->obj)->save_offset;
|
458
|
901 PDUMP_WRITE_ALIGNED (EMACS_INT, rdata);
|
460
|
902 elt = elt->next;
|
442
|
903 }
|
|
904 }
|
|
905
|
|
906 rt.desc = 0;
|
|
907 rt.count = 0;
|
458
|
908 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
442
|
909
|
|
910 for (i=0; i<pdump_struct_table.count; i++)
|
|
911 {
|
460
|
912 elt = pdump_struct_table.list[i].list.first;
|
442
|
913 rt.desc = pdump_struct_table.list[i].sdesc->description;
|
|
914 rt.count = pdump_struct_table.list[i].list.count;
|
458
|
915 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
460
|
916 while (elt)
|
442
|
917 {
|
460
|
918 EMACS_INT rdata = pdump_get_entry (elt->obj)->save_offset;
|
452
|
919 int j;
|
460
|
920 for (j=0; j<elt->count; j++)
|
442
|
921 {
|
458
|
922 PDUMP_WRITE_ALIGNED (EMACS_INT, rdata);
|
460
|
923 rdata += elt->size;
|
442
|
924 }
|
460
|
925 elt = elt->next;
|
442
|
926 }
|
|
927 }
|
|
928 rt.desc = 0;
|
|
929 rt.count = 0;
|
458
|
930 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
|
442
|
931 }
|
|
932
|
|
933 static void
|
458
|
934 pdump_dump_root_objects (void)
|
442
|
935 {
|
647
|
936 Element_Count count = (Dynarr_length (pdump_root_objects) +
|
|
937 Dynarr_length (pdump_weak_object_chains));
|
|
938 Element_Count i;
|
442
|
939
|
647
|
940 PDUMP_WRITE_ALIGNED (Element_Count, count);
|
458
|
941 PDUMP_ALIGN_OUTPUT (pdump_static_Lisp_Object);
|
442
|
942
|
647
|
943 for (i = 0; i < Dynarr_length (pdump_root_objects); i++)
|
442
|
944 {
|
458
|
945 pdump_static_Lisp_Object obj;
|
|
946 obj.address = Dynarr_at (pdump_root_objects, i);
|
|
947 obj.value = * obj.address;
|
460
|
948
|
458
|
949 if (POINTER_TYPE_P (XTYPE (obj.value)))
|
619
|
950 obj.value =
|
|
951 wrap_pointer_1 ((void *) pdump_get_entry (XRECORD_LHEADER
|
617
|
952 (obj.value))->save_offset);
|
460
|
953
|
458
|
954 PDUMP_WRITE (pdump_static_Lisp_Object, obj);
|
442
|
955 }
|
|
956
|
452
|
957 for (i=0; i<Dynarr_length (pdump_weak_object_chains); i++)
|
442
|
958 {
|
460
|
959 pdump_entry_list_elt *elt;
|
458
|
960 pdump_static_Lisp_Object obj;
|
442
|
961
|
458
|
962 obj.address = Dynarr_at (pdump_weak_object_chains, i);
|
|
963 obj.value = * obj.address;
|
460
|
964
|
442
|
965 for (;;)
|
|
966 {
|
|
967 const struct lrecord_description *desc;
|
|
968 int pos;
|
460
|
969 elt = pdump_get_entry (XRECORD_LHEADER (obj.value));
|
|
970 if (elt)
|
442
|
971 break;
|
458
|
972 desc = XRECORD_LHEADER_IMPLEMENTATION (obj.value)->description;
|
442
|
973 for (pos = 0; desc[pos].type != XD_LO_LINK; pos++)
|
|
974 assert (desc[pos].type != XD_END);
|
|
975
|
458
|
976 obj.value = *(Lisp_Object *)(desc[pos].offset + (char *)(XRECORD_LHEADER (obj.value)));
|
442
|
977 }
|
619
|
978 obj.value = wrap_pointer_1 ((void *) elt->save_offset);
|
442
|
979
|
458
|
980 PDUMP_WRITE (pdump_static_Lisp_Object, obj);
|
442
|
981 }
|
|
982 }
|
|
983
|
|
984 void
|
|
985 pdump (void)
|
|
986 {
|
|
987 int i;
|
|
988 Lisp_Object t_console, t_device, t_frame;
|
|
989 int none;
|
458
|
990 pdump_header header;
|
442
|
991
|
460
|
992 pdump_object_table = xnew_array (pdump_entry_list, lrecord_type_count);
|
|
993 pdump_alert_undump_object = xnew_array (int, lrecord_type_count);
|
|
994
|
|
995 assert (ALIGNOF (max_align_t) <= pdump_align_table[0]);
|
|
996
|
|
997 for (i = 0; i < countof (pdump_align_table); i++)
|
|
998 if (pdump_align_table[i] > ALIGNOF (max_align_t))
|
|
999 pdump_align_table[i] = ALIGNOF (max_align_t);
|
|
1000
|
446
|
1001 flush_all_buffer_local_cache ();
|
|
1002
|
442
|
1003 /* These appear in a DEFVAR_LISP, which does a staticpro() */
|
452
|
1004 t_console = Vterminal_console; Vterminal_console = Qnil;
|
|
1005 t_frame = Vterminal_frame; Vterminal_frame = Qnil;
|
|
1006 t_device = Vterminal_device; Vterminal_device = Qnil;
|
442
|
1007
|
452
|
1008 dump_add_opaque (&lrecord_implementations_table,
|
|
1009 lrecord_type_count * sizeof (lrecord_implementations_table[0]));
|
|
1010 dump_add_opaque (&lrecord_markers,
|
|
1011 lrecord_type_count * sizeof (lrecord_markers[0]));
|
442
|
1012
|
460
|
1013 pdump_hash = xnew_array_and_zero (pdump_entry_list_elt *, PDUMP_HASHSIZE);
|
442
|
1014
|
|
1015 for (i=0; i<lrecord_type_count; i++)
|
|
1016 {
|
|
1017 pdump_object_table[i].first = 0;
|
460
|
1018 pdump_object_table[i].align = ALIGNOF (max_align_t);
|
442
|
1019 pdump_object_table[i].count = 0;
|
|
1020 pdump_alert_undump_object[i] = 0;
|
|
1021 }
|
|
1022 pdump_struct_table.count = 0;
|
|
1023 pdump_struct_table.size = -1;
|
|
1024
|
|
1025 pdump_opaque_data_list.first = 0;
|
460
|
1026 pdump_opaque_data_list.align = ALIGNOF (max_align_t);
|
442
|
1027 pdump_opaque_data_list.count = 0;
|
|
1028 depth = 0;
|
|
1029
|
452
|
1030 for (i=0; i<Dynarr_length (pdump_root_objects); i++)
|
|
1031 pdump_register_object (* Dynarr_at (pdump_root_objects, i));
|
442
|
1032
|
|
1033 none = 1;
|
|
1034 for (i=0; i<lrecord_type_count; i++)
|
|
1035 if (pdump_alert_undump_object[i])
|
|
1036 {
|
|
1037 if (none)
|
|
1038 printf ("Undumpable types list :\n");
|
|
1039 none = 0;
|
|
1040 printf (" - %s (%d)\n", lrecord_implementations_table[i]->name, pdump_alert_undump_object[i]);
|
|
1041 }
|
|
1042 if (!none)
|
|
1043 return;
|
|
1044
|
452
|
1045 for (i=0; i<Dynarr_length (pdump_root_struct_ptrs); i++)
|
|
1046 {
|
|
1047 pdump_root_struct_ptr info = Dynarr_at (pdump_root_struct_ptrs, i);
|
|
1048 pdump_register_struct (*(info.ptraddress), info.desc, 1);
|
|
1049 }
|
442
|
1050
|
458
|
1051 memcpy (header.signature, PDUMP_SIGNATURE, PDUMP_SIGNATURE_LEN);
|
|
1052 header.id = dump_id;
|
|
1053 header.reloc_address = 0;
|
|
1054 header.nb_root_struct_ptrs = Dynarr_length (pdump_root_struct_ptrs);
|
|
1055 header.nb_opaques = Dynarr_length (pdump_opaques);
|
442
|
1056
|
458
|
1057 cur_offset = ALIGN_SIZE (sizeof (header), ALIGNOF (max_align_t));
|
442
|
1058 max_size = 0;
|
|
1059
|
|
1060 pdump_scan_by_alignment (pdump_allocate_offset);
|
458
|
1061 cur_offset = ALIGN_SIZE (cur_offset, ALIGNOF (max_align_t));
|
|
1062 header.stab_offset = cur_offset;
|
442
|
1063
|
|
1064 pdump_buf = xmalloc (max_size);
|
|
1065 /* Avoid use of the `open' macro. We want the real function. */
|
|
1066 #undef open
|
|
1067 pdump_fd = open (EMACS_PROGNAME ".dmp",
|
|
1068 O_WRONLY | O_CREAT | O_TRUNC | OPEN_BINARY, 0666);
|
458
|
1069 pdump_out = fdopen (pdump_fd, "w");
|
442
|
1070
|
458
|
1071 fwrite (&header, sizeof (header), 1, pdump_out);
|
|
1072 PDUMP_ALIGN_OUTPUT (max_align_t);
|
442
|
1073
|
|
1074 pdump_scan_by_alignment (pdump_dump_data);
|
|
1075
|
458
|
1076 fseek (pdump_out, header.stab_offset, SEEK_SET);
|
442
|
1077
|
458
|
1078 pdump_dump_root_struct_ptrs ();
|
452
|
1079 pdump_dump_opaques ();
|
442
|
1080 pdump_dump_rtables ();
|
458
|
1081 pdump_dump_root_objects ();
|
442
|
1082
|
458
|
1083 fclose (pdump_out);
|
442
|
1084 close (pdump_fd);
|
458
|
1085
|
442
|
1086 free (pdump_buf);
|
|
1087
|
|
1088 free (pdump_hash);
|
|
1089
|
|
1090 Vterminal_console = t_console;
|
|
1091 Vterminal_frame = t_frame;
|
|
1092 Vterminal_device = t_device;
|
|
1093 }
|
|
1094
|
452
|
1095 static int
|
|
1096 pdump_load_check (void)
|
442
|
1097 {
|
452
|
1098 return (!memcmp (((pdump_header *)pdump_start)->signature,
|
|
1099 PDUMP_SIGNATURE, PDUMP_SIGNATURE_LEN)
|
|
1100 && ((pdump_header *)pdump_start)->id == dump_id);
|
442
|
1101 }
|
|
1102
|
458
|
1103 /*----------------------------------------------------------------------*/
|
|
1104 /* Reading the dump file */
|
|
1105 /*----------------------------------------------------------------------*/
|
452
|
1106 static int
|
|
1107 pdump_load_finish (void)
|
442
|
1108 {
|
|
1109 int i;
|
|
1110 char *p;
|
|
1111 EMACS_INT delta;
|
|
1112 EMACS_INT count;
|
458
|
1113 pdump_header *header = (pdump_header *)pdump_start;
|
442
|
1114
|
|
1115 pdump_end = pdump_start + pdump_length;
|
|
1116
|
458
|
1117 delta = ((EMACS_INT)pdump_start) - header->reloc_address;
|
|
1118 p = pdump_start + header->stab_offset;
|
442
|
1119
|
452
|
1120 /* Put back the pdump_root_struct_ptrs */
|
458
|
1121 p = (char *) ALIGN_PTR (p, ALIGNOF (pdump_static_pointer));
|
|
1122 for (i=0; i<header->nb_root_struct_ptrs; i++)
|
442
|
1123 {
|
458
|
1124 pdump_static_pointer ptr = PDUMP_READ (p, pdump_static_pointer);
|
|
1125 (* ptr.address) = ptr.value + delta;
|
442
|
1126 }
|
|
1127
|
452
|
1128 /* Put back the pdump_opaques */
|
458
|
1129 for (i=0; i<header->nb_opaques; i++)
|
442
|
1130 {
|
458
|
1131 pdump_opaque info = PDUMP_READ_ALIGNED (p, pdump_opaque);
|
545
|
1132 memcpy ((void*)info.varaddress, p, info.size);
|
452
|
1133 p += info.size;
|
442
|
1134 }
|
|
1135
|
|
1136 /* Do the relocations */
|
|
1137 pdump_rt_list = p;
|
|
1138 count = 2;
|
|
1139 for (;;)
|
|
1140 {
|
458
|
1141 pdump_reloc_table rt = PDUMP_READ_ALIGNED (p, pdump_reloc_table);
|
|
1142 p = (char *) ALIGN_PTR (p, ALIGNOF (char *));
|
442
|
1143 if (rt.desc)
|
|
1144 {
|
458
|
1145 char **reloc = (char **)p;
|
442
|
1146 for (i=0; i < rt.count; i++)
|
|
1147 {
|
458
|
1148 reloc[i] += delta;
|
|
1149 pdump_reloc_one (reloc[i], delta, rt.desc);
|
442
|
1150 }
|
458
|
1151 p += rt.count * sizeof (char *);
|
442
|
1152 } else
|
|
1153 if (!(--count))
|
|
1154 break;
|
|
1155 }
|
|
1156
|
452
|
1157 /* Put the pdump_root_objects variables in place */
|
647
|
1158 i = PDUMP_READ_ALIGNED (p, Element_Count);
|
458
|
1159 p = (char *) ALIGN_PTR (p, ALIGNOF (pdump_static_Lisp_Object));
|
|
1160 while (i--)
|
442
|
1161 {
|
458
|
1162 pdump_static_Lisp_Object obj = PDUMP_READ (p, pdump_static_Lisp_Object);
|
442
|
1163
|
458
|
1164 if (POINTER_TYPE_P (XTYPE (obj.value)))
|
619
|
1165 obj.value = wrap_pointer_1 ((char *) XPNTR (obj.value) + delta);
|
442
|
1166
|
458
|
1167 (* obj.address) = obj.value;
|
442
|
1168 }
|
|
1169
|
|
1170 /* Final cleanups */
|
|
1171 /* reorganize hash tables */
|
|
1172 p = pdump_rt_list;
|
|
1173 for (;;)
|
|
1174 {
|
458
|
1175 pdump_reloc_table rt = PDUMP_READ_ALIGNED (p, pdump_reloc_table);
|
|
1176 p = (char *) ALIGN_PTR (p, ALIGNOF (Lisp_Object));
|
442
|
1177 if (!rt.desc)
|
|
1178 break;
|
|
1179 if (rt.desc == hash_table_description)
|
|
1180 {
|
|
1181 for (i=0; i < rt.count; i++)
|
|
1182 pdump_reorganize_hash_table (PDUMP_READ (p, Lisp_Object));
|
|
1183 break;
|
|
1184 } else
|
|
1185 p += sizeof (Lisp_Object) * rt.count;
|
|
1186 }
|
|
1187
|
|
1188 return 1;
|
|
1189 }
|
|
1190
|
|
1191 #ifdef WIN32_NATIVE
|
|
1192 /* Free the mapped file if we decide we don't want it after all */
|
452
|
1193 static void
|
|
1194 pdump_file_unmap (void)
|
442
|
1195 {
|
|
1196 UnmapViewOfFile (pdump_start);
|
|
1197 CloseHandle (pdump_hFile);
|
|
1198 CloseHandle (pdump_hMap);
|
|
1199 }
|
|
1200
|
452
|
1201 static int
|
|
1202 pdump_file_get (const char *path)
|
442
|
1203 {
|
|
1204
|
|
1205 pdump_hFile = CreateFile (path,
|
|
1206 GENERIC_READ + GENERIC_WRITE, /* Required for copy on write */
|
|
1207 0, /* Not shared */
|
|
1208 NULL, /* Not inheritable */
|
|
1209 OPEN_EXISTING,
|
|
1210 FILE_ATTRIBUTE_NORMAL,
|
|
1211 NULL); /* No template file */
|
|
1212 if (pdump_hFile == INVALID_HANDLE_VALUE)
|
|
1213 return 0;
|
|
1214
|
|
1215 pdump_length = GetFileSize (pdump_hFile, NULL);
|
|
1216 pdump_hMap = CreateFileMapping (pdump_hFile,
|
|
1217 NULL, /* No security attributes */
|
|
1218 PAGE_WRITECOPY, /* Copy on write */
|
|
1219 0, /* Max size, high half */
|
|
1220 0, /* Max size, low half */
|
|
1221 NULL); /* Unnamed */
|
|
1222 if (pdump_hMap == INVALID_HANDLE_VALUE)
|
|
1223 return 0;
|
|
1224
|
|
1225 pdump_start = MapViewOfFile (pdump_hMap,
|
452
|
1226 FILE_MAP_COPY, /* Copy on write */
|
442
|
1227 0, /* Start at zero */
|
|
1228 0,
|
|
1229 0); /* Map all of it */
|
|
1230 pdump_free = pdump_file_unmap;
|
|
1231 return 1;
|
|
1232 }
|
|
1233
|
|
1234 /* pdump_resource_free is called (via the pdump_free pointer) to release
|
|
1235 any resources allocated by pdump_resource_get. Since the Windows API
|
|
1236 specs specifically state that you don't need to (and shouldn't) free the
|
|
1237 resources allocated by FindResource, LoadResource, and LockResource this
|
|
1238 routine does nothing. */
|
452
|
1239 static void
|
|
1240 pdump_resource_free (void)
|
442
|
1241 {
|
|
1242 }
|
|
1243
|
452
|
1244 static int
|
|
1245 pdump_resource_get (void)
|
442
|
1246 {
|
452
|
1247 HRSRC hRes; /* Handle to dump resource */
|
|
1248 HRSRC hResLoad; /* Handle to loaded dump resource */
|
442
|
1249
|
|
1250 /* See Q126630 which describes how Windows NT and 95 trap writes to
|
|
1251 resource sections and duplicate the page to allow the write to proceed.
|
|
1252 It also describes how to make the resource section read/write (and hence
|
|
1253 private to each process). Doing this avoids the exceptions and related
|
|
1254 overhead, but causes the resource section to be private to each process
|
|
1255 that is running XEmacs. Since the resource section contains little
|
|
1256 other than the dumped data, which should be private to each process, we
|
|
1257 make the whole resource section read/write so we don't have to copy it. */
|
|
1258
|
|
1259 hRes = FindResource (NULL, MAKEINTRESOURCE(101), "DUMP");
|
|
1260 if (hRes == NULL)
|
|
1261 return 0;
|
|
1262
|
|
1263 /* Found it, use the data in the resource */
|
|
1264 hResLoad = LoadResource (NULL, hRes);
|
|
1265 if (hResLoad == NULL)
|
|
1266 return 0;
|
|
1267
|
|
1268 pdump_start = LockResource (hResLoad);
|
|
1269 if (pdump_start == NULL)
|
|
1270 return 0;
|
|
1271
|
|
1272 pdump_free = pdump_resource_free;
|
|
1273 pdump_length = SizeofResource (NULL, hRes);
|
647
|
1274 if (pdump_length <= (Memory_Count) sizeof (pdump_header))
|
442
|
1275 {
|
|
1276 pdump_start = 0;
|
|
1277 return 0;
|
|
1278 }
|
|
1279
|
|
1280 return 1;
|
|
1281 }
|
|
1282
|
|
1283 #else /* !WIN32_NATIVE */
|
|
1284
|
452
|
1285 static void
|
|
1286 pdump_file_free (void)
|
442
|
1287 {
|
460
|
1288 xfree (pdump_start);
|
442
|
1289 }
|
|
1290
|
|
1291 #ifdef HAVE_MMAP
|
452
|
1292 static void
|
|
1293 pdump_file_unmap (void)
|
442
|
1294 {
|
|
1295 munmap (pdump_start, pdump_length);
|
|
1296 }
|
|
1297 #endif
|
|
1298
|
452
|
1299 static int
|
|
1300 pdump_file_get (const char *path)
|
442
|
1301 {
|
|
1302 int fd = open (path, O_RDONLY | OPEN_BINARY);
|
|
1303 if (fd<0)
|
|
1304 return 0;
|
|
1305
|
|
1306 pdump_length = lseek (fd, 0, SEEK_END);
|
647
|
1307 if (pdump_length < (Memory_Count) sizeof (pdump_header))
|
442
|
1308 {
|
|
1309 close (fd);
|
|
1310 return 0;
|
|
1311 }
|
|
1312
|
|
1313 lseek (fd, 0, SEEK_SET);
|
|
1314
|
|
1315 #ifdef HAVE_MMAP
|
456
|
1316 /* Unix 98 requires that sys/mman.h define MAP_FAILED,
|
|
1317 but many earlier implementations don't. */
|
|
1318 # ifndef MAP_FAILED
|
|
1319 # define MAP_FAILED ((void *) -1L)
|
|
1320 # endif
|
442
|
1321 pdump_start = (char *) mmap (0, pdump_length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
|
452
|
1322 if (pdump_start != (char *) MAP_FAILED)
|
442
|
1323 {
|
|
1324 pdump_free = pdump_file_unmap;
|
|
1325 close (fd);
|
|
1326 return 1;
|
|
1327 }
|
456
|
1328 #endif /* HAVE_MMAP */
|
442
|
1329
|
460
|
1330 pdump_start = xnew_array (char, pdump_length);
|
442
|
1331 pdump_free = pdump_file_free;
|
446
|
1332 read (fd, pdump_start, pdump_length);
|
442
|
1333
|
446
|
1334 close (fd);
|
442
|
1335 return 1;
|
|
1336 }
|
|
1337 #endif /* !WIN32_NATIVE */
|
|
1338
|
|
1339
|
452
|
1340 static int
|
|
1341 pdump_file_try (char *exe_path)
|
442
|
1342 {
|
460
|
1343 char *w = exe_path + strlen (exe_path);
|
442
|
1344
|
|
1345 do
|
|
1346 {
|
|
1347 sprintf (w, "-%s-%08x.dmp", EMACS_VERSION, dump_id);
|
|
1348 if (pdump_file_get (exe_path))
|
|
1349 {
|
|
1350 if (pdump_load_check ())
|
|
1351 return 1;
|
452
|
1352 pdump_free ();
|
442
|
1353 }
|
|
1354
|
|
1355 sprintf (w, "-%08x.dmp", dump_id);
|
|
1356 if (pdump_file_get (exe_path))
|
|
1357 {
|
|
1358 if (pdump_load_check ())
|
|
1359 return 1;
|
452
|
1360 pdump_free ();
|
442
|
1361 }
|
|
1362
|
|
1363 sprintf (w, ".dmp");
|
|
1364 if (pdump_file_get (exe_path))
|
|
1365 {
|
|
1366 if (pdump_load_check ())
|
|
1367 return 1;
|
452
|
1368 pdump_free ();
|
442
|
1369 }
|
|
1370
|
|
1371 do
|
|
1372 w--;
|
|
1373 while (w>exe_path && !IS_DIRECTORY_SEP (*w) && (*w != '-') && (*w != '.'));
|
|
1374 }
|
|
1375 while (w>exe_path && !IS_DIRECTORY_SEP (*w));
|
|
1376 return 0;
|
|
1377 }
|
|
1378
|
452
|
1379 int
|
|
1380 pdump_load (const char *argv0)
|
442
|
1381 {
|
|
1382 char exe_path[PATH_MAX];
|
|
1383 #ifdef WIN32_NATIVE
|
|
1384 GetModuleFileName (NULL, exe_path, PATH_MAX);
|
|
1385 #else /* !WIN32_NATIVE */
|
|
1386 char *w;
|
|
1387 const char *dir, *p;
|
|
1388
|
|
1389 dir = argv0;
|
|
1390 if (dir[0] == '-')
|
|
1391 {
|
|
1392 /* XEmacs as a login shell, oh goody! */
|
452
|
1393 dir = getenv ("SHELL");
|
442
|
1394 }
|
|
1395
|
452
|
1396 p = dir + strlen (dir);
|
442
|
1397 while (p != dir && !IS_ANY_SEP (p[-1])) p--;
|
|
1398
|
|
1399 if (p != dir)
|
|
1400 {
|
|
1401 /* invocation-name includes a directory component -- presumably it
|
|
1402 is relative to cwd, not $PATH */
|
|
1403 strcpy (exe_path, dir);
|
|
1404 }
|
|
1405 else
|
|
1406 {
|
|
1407 const char *path = getenv ("PATH");
|
|
1408 const char *name = p;
|
|
1409 for (;;)
|
|
1410 {
|
|
1411 p = path;
|
|
1412 while (*p && *p != SEPCHAR)
|
|
1413 p++;
|
|
1414 if (p == path)
|
|
1415 {
|
|
1416 exe_path[0] = '.';
|
|
1417 w = exe_path + 1;
|
|
1418 }
|
|
1419 else
|
|
1420 {
|
|
1421 memcpy (exe_path, path, p - path);
|
|
1422 w = exe_path + (p - path);
|
|
1423 }
|
|
1424 if (!IS_DIRECTORY_SEP (w[-1]))
|
|
1425 {
|
|
1426 *w++ = '/';
|
|
1427 }
|
452
|
1428 strcpy (w, name);
|
442
|
1429
|
|
1430 /* ### #$%$#^$^@%$^#%@$ ! */
|
|
1431 #ifdef access
|
|
1432 #undef access
|
|
1433 #endif
|
|
1434
|
|
1435 if (!access (exe_path, X_OK))
|
|
1436 break;
|
|
1437 if (!*p)
|
|
1438 {
|
|
1439 /* Oh well, let's have some kind of default */
|
|
1440 sprintf (exe_path, "./%s", name);
|
|
1441 break;
|
|
1442 }
|
|
1443 path = p+1;
|
|
1444 }
|
|
1445 }
|
|
1446 #endif /* WIN32_NATIVE */
|
|
1447
|
|
1448 if (pdump_file_try (exe_path))
|
|
1449 {
|
|
1450 pdump_load_finish ();
|
|
1451 return 1;
|
|
1452 }
|
|
1453
|
|
1454 #ifdef WIN32_NATIVE
|
|
1455 if (pdump_resource_get ())
|
|
1456 {
|
|
1457 if (pdump_load_check ())
|
|
1458 {
|
|
1459 pdump_load_finish ();
|
|
1460 return 1;
|
|
1461 }
|
|
1462 pdump_free ();
|
|
1463 }
|
|
1464 #endif
|
|
1465
|
|
1466 return 0;
|
|
1467 }
|