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