Index: M2/Macaulay2/d/M2mem.c
===================================================================
--- M2/Macaulay2/d/M2mem.c	(revision 15728)
+++ M2/Macaulay2/d/M2mem.c	(working copy)
@@ -42,8 +42,35 @@
      exit(1);
 }
 
+#define framepointer					\
+     ({ /* x86_64 only */				\
+        void *p;					\
+        asm("movq %%rbp,%0" : "=mr" (p): : "rax");	\
+	p;						\
+	  })
+
+void pbacktrace(int n) {
+     void *p;
+     for (p=framepointer; p && n--; p = *(void **)p) {
+	  printf("frame at %p\n",p);
+	  }
+     fflush(stdout);
+     }
+
+void framecheck() {
+     #if 0
+     void *p=framepointer;
+     while (p) {
+	  p=*(void **)p; /* if a framepointer is corrupted, we will crash here */
+	  }
+     #endif
+     }
+
 char *getmem(size_t n)
 {
+#ifdef DEBUG
+  framecheck();
+#endif
   char *p;
   enter_getmem();
   p = GC_MALLOC(n);		/* GC_MALLOC clears its memory, but getmem doesn't guarntee to */
@@ -58,6 +85,7 @@
 
 void freememlen(void *s, size_t old) {
 #    ifdef DEBUG
+     framecheck();
      trapchk(s);
 #    endif
      GC_FREE(s);
@@ -65,6 +93,7 @@
 
 void freemem(void *s) {
 #    ifdef DEBUG
+     framecheck();
      trapchk(s);
 #    endif
      GC_FREE(s);
@@ -72,6 +101,9 @@
 
 char *getmem_clear(size_t n)
 {
+#ifdef DEBUG
+  framecheck();
+#endif
   char *p;
   enter_getmem();
   p = GC_MALLOC(n);
@@ -92,6 +124,9 @@
 
 char *getmem_atomic(size_t n)
 {
+#ifdef DEBUG
+  framecheck();
+#endif
   char *p;
   enter_getmem();
   p = GC_MALLOC_ATOMIC(n);
@@ -106,6 +141,9 @@
 
 char *getmem_malloc(size_t n)
 {
+#ifdef DEBUG
+  framecheck();
+#endif
   char *p;
   enter_getmem();
   p = malloc(n);
@@ -120,6 +158,9 @@
 
 char *getmem_atomic_clear(size_t n)
 {
+#ifdef DEBUG
+  framecheck();
+#endif
   char *p;
   enter_getmem();
   p = GC_MALLOC_ATOMIC(n);
@@ -133,6 +174,9 @@
 }
 
 char *getmoremem (char *s, size_t old, size_t new) {
+#ifdef DEBUG
+  framecheck();
+#endif
      void *p;
      enter_getmem();
      p = GC_REALLOC(s,new);
@@ -145,6 +189,9 @@
      }
 
 char *getmoremem1 (char *s, size_t new) {
+#ifdef DEBUG
+  framecheck();
+#endif
      void *p;
      enter_getmem();
      p = GC_REALLOC(s,new);
@@ -157,6 +204,9 @@
      }
 
 char *getmoremem_atomic (char *s, size_t old, size_t new) {
+#ifdef DEBUG
+  framecheck();
+#endif
      void *p;
      enter_getmem();
      p = GC_MALLOC_ATOMIC(new);
