Fix typo on include guard for term.h
[sxemacs] / src / dumper.c
index 2f6742b..e894834 100644 (file)
@@ -34,6 +34,9 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 #include <sys/mman.h>
 #endif
 
+/* For PATH_EXEC */
+#include <sxe-paths.h>
+
 #ifndef SEPCHAR
 #define SEPCHAR ':'
 #endif
@@ -333,7 +336,7 @@ pdump_get_entry_list(const struct struct_description *sdesc)
        }
        pdump_struct_table.list[pdump_struct_table.count].list.first = 0;
        pdump_struct_table.list[pdump_struct_table.count].list.align =
-           ALIGNOF(max_align_t);
+           ALIGNOF(sxe_max_align_t);
        pdump_struct_table.list[pdump_struct_table.count].list.count = 0;
        pdump_struct_table.list[pdump_struct_table.count].sdesc = sdesc;
 
@@ -587,19 +590,20 @@ pdump_register_struct(const void *data,
                if (me >= 65536) {
                        stderr_out("Backtrace overflow, loop ?\n");
                        abort();
+               } else {
+                       backtrace[me].obj = 0;
+                       backtrace[me].position = 0;
+                       backtrace[me].offset = 0;
+                       
+                       pdump_add_entry(pdump_get_entry_list(sdesc),
+                                       data, sdesc->size, count);
+                       for (i = 0; i < count; i++) {
+                               pdump_register_sub(
+                                       ((const char*)data) + sdesc->size * i,
+                                       sdesc->description, me);
+                       }
+                       --depth;
                }
-               backtrace[me].obj = 0;
-               backtrace[me].position = 0;
-               backtrace[me].offset = 0;
-
-               pdump_add_entry(pdump_get_entry_list(sdesc),
-                               data, sdesc->size, count);
-               for (i = 0; i < count; i++) {
-                       pdump_register_sub(
-                               ((const char*)data) + sdesc->size * i,
-                               sdesc->description, me);
-               }
-               --depth;
        }
 }
 
@@ -827,7 +831,7 @@ pdump_scan_by_alignment(void (*f) (pdump_entry_list_elt *,
 {
        int align;
 
-       for (align = ALIGNOF(max_align_t); align; align >>= 1) {
+       for (align = ALIGNOF(sxe_max_align_t); align; align >>= 1) {
                size_t i;
                pdump_entry_list_elt *elt;
 
@@ -990,11 +994,11 @@ pdump(const char *dumpfile)
        pdump_object_table = xnew_array(pdump_entry_list, lrecord_type_count);
        pdump_alert_undump_object = xnew_array(int, lrecord_type_count);
 
-       assert(ALIGNOF(max_align_t) <= pdump_align_table[0]);
+       assert(ALIGNOF(sxe_max_align_t) <= pdump_align_table[0]);
 
        for (i = 0; i < countof(pdump_align_table); i++)
-               if (pdump_align_table[i] > ALIGNOF(max_align_t))
-                       pdump_align_table[i] = ALIGNOF(max_align_t);
+               if (pdump_align_table[i] > ALIGNOF(sxe_max_align_t))
+                       pdump_align_table[i] = ALIGNOF(sxe_max_align_t);
 
        flush_all_buffer_local_cache();
 
@@ -1016,7 +1020,7 @@ pdump(const char *dumpfile)
 
        for (i = 0; i < lrecord_type_count; i++) {
                pdump_object_table[i].first = 0;
-               pdump_object_table[i].align = ALIGNOF(max_align_t);
+               pdump_object_table[i].align = ALIGNOF(sxe_max_align_t);
                pdump_object_table[i].count = 0;
                pdump_alert_undump_object[i] = 0;
        }
@@ -1024,7 +1028,7 @@ pdump(const char *dumpfile)
        pdump_struct_table.size = -1;
 
        pdump_opaque_data_list.first = 0;
-       pdump_opaque_data_list.align = ALIGNOF(max_align_t);
+       pdump_opaque_data_list.align = ALIGNOF(sxe_max_align_t);
        pdump_opaque_data_list.count = 0;
        depth = 0;
 
@@ -1058,11 +1062,11 @@ pdump(const char *dumpfile)
        header.nb_root_struct_ptrs = Dynarr_length(pdump_root_struct_ptrs);
        header.nb_opaques = Dynarr_length(pdump_opaques);
 
-       cur_offset = ALIGN_SIZE(sizeof(header), ALIGNOF(max_align_t));
+       cur_offset = ALIGN_SIZE(sizeof(header), ALIGNOF(sxe_max_align_t));
        max_size = 0;
 
        pdump_scan_by_alignment(pdump_allocate_offset);
-       cur_offset = ALIGN_SIZE(cur_offset, ALIGNOF(max_align_t));
+       cur_offset = ALIGN_SIZE(cur_offset, ALIGNOF(sxe_max_align_t));
        header.stab_offset = cur_offset;
 
        pdump_buf = xmalloc_atomic(max_size);
@@ -1082,7 +1086,7 @@ pdump(const char *dumpfile)
                } else {
 
                        fwrite(&header, sizeof(header), 1, pdump_out);
-                       PDUMP_ALIGN_OUTPUT(max_align_t);
+                       PDUMP_ALIGN_OUTPUT(sxe_max_align_t);
 
                        pdump_scan_by_alignment(pdump_dump_data);
 
@@ -1265,58 +1269,26 @@ static int pdump_file_get(const char *path)
        return 1;
 }
 
-static int pdump_file_try(char *exe_path, size_t size)
+static int
+pdump_file_try(char *exe_path, size_t size)
 {
-       char *w = exe_path + strlen(exe_path);
-       int sz;
-       size -= strlen(exe_path);
-
-       do {
-
-#ifdef EMACS_PATCH_LEVEL
-               sz = snprintf(w, size, "-%d.%d.%d-%08x.dmp",
-                        EMACS_MAJOR_VERSION, EMACS_MINOR_VERSION,
-                        EMACS_PATCH_LEVEL, dump_id);
-               if (sz >=0 && sz < size && pdump_file_get(exe_path)) {
-                       if (pdump_load_check()) {
-                               return 1;
-                       }
-                       pdump_free();
-               }
-#endif /* EMACS_PATCH_LEVEL */
-#ifdef EMACS_BETA_VERSION
-               sz = snprintf(w, size, "-%d.%d.%d-%08x.dmp",
-                        EMACS_MAJOR_VERSION, EMACS_MINOR_VERSION,
-                        EMACS_BETA_VERSION, dump_id);
-               if (sz >=0 && (size_t)sz < size && pdump_file_get(exe_path)) {
-                       if (pdump_load_check()) {
-                               return 1;
-                       }
-                       pdump_free();
-               }
-#endif /* EMACS_BETA_VERSION */
-
-               sz = snprintf(w, size, "-%08x.dmp", dump_id);
-               if (sz >=0 && (size_t)sz < size && pdump_file_get(exe_path)) {
+       static const char pdump_ext[] = ".dmp";
+       size_t exe_path_len = strlen(exe_path);
+       char *w = exe_path + exe_path_len;
+
+       if (exe_path_len + sizeof(pdump_ext) <= size) {
+               /* just hammer the pdump extension onto w */
+               memcpy(w, pdump_ext, sizeof(pdump_ext));
+               /* exists? */
+               if (pdump_file_get(exe_path)) {
+                       /* works? */
                        if (pdump_load_check()) {
                                return 1;
                        }
+                       /* bugger */
                        pdump_free();
                }
-
-               sz = snprintf(w, size, ".dmp");
-               if (sz >=0 && (size_t)sz < size && pdump_file_get(exe_path)) {
-                       if (pdump_load_check()) {
-                               return 1;
-                       }
-                       pdump_free();
-               }
-
-               do {
-                       w--;
-               } while (w > exe_path && !IS_DIRECTORY_SEP(*w) && (*w != '-')
-                        && (*w != '.'));
-       } while (w > exe_path && !IS_DIRECTORY_SEP(*w));
+       }
        return 0;
 }
 
@@ -1355,6 +1327,7 @@ wipe_out_libtool_stuff(char *path)
 int pdump_load(const char *argv0)
 {
        char exe_path[PATH_MAX], real_exe_path[PATH_MAX];
+       char libarchdir_path[PATH_MAX] = PATH_EXEC "/sxemacs";
        char *w;
        const char *dir, *p;
 
@@ -1374,7 +1347,7 @@ int pdump_load(const char *argv0)
                /* invocation-name includes a directory component -- presumably it
                   is relative to cwd, not $PATH */
                assert(strlen(dir) < sizeof(exe_path));
-               strncpy(exe_path, dir, sizeof(exe_path)-1);
+               xstrncpy(exe_path, dir, sizeof(exe_path));
                exe_path[sizeof(exe_path)-1]='\0';
        } else {
                const char *path = getenv("PATH");
@@ -1406,8 +1379,7 @@ int pdump_load(const char *argv0)
                                remain--;
                        }
                        if (remain > 0) {
-                               strncpy(w, name, remain);
-                               w[remain]='\0';
+                               xstrncpy(w, name, remain);
                        }
 
                        /* Check that exe_path is executable and not a
@@ -1441,10 +1413,11 @@ int pdump_load(const char *argv0)
 
        /* Save exe_path because pdump_file_try() modifies it */
        assert(strlen(exe_path) < sizeof(real_exe_path));
-       strcpy(real_exe_path, exe_path);
+       xstrncpy(real_exe_path, exe_path, sizeof(real_exe_path));
        if (pdump_file_try(exe_path,sizeof(exe_path))
            || (xrealpath(real_exe_path, real_exe_path)
-               && pdump_file_try(real_exe_path,sizeof(real_exe_path)))) {
+               && pdump_file_try(real_exe_path,sizeof(real_exe_path)))
+           || pdump_file_try(libarchdir_path,sizeof(libarchdir_path)) ) {
                pdump_load_finish();
                return 1;
        }