[project @ 2001-09-04 16:33:04 by sewardj]
[ghc-hetmet.git] / ghc / rts / Printer.c
index 600d0a2..e64154d 100644 (file)
@@ -1,12 +1,13 @@
 /* -----------------------------------------------------------------------------
- * $Id: Printer.c,v 1.19 2000/01/13 14:34:04 hwloidl Exp $
+ * $Id: Printer.c,v 1.46 2001/08/21 10:12:02 sewardj Exp $
  *
- * Copyright (c) 1994-1999.
+ * (c) The GHC Team, 1994-2000.
  *
  * Heap printer
  * 
  * ---------------------------------------------------------------------------*/
 
+#include "PosixSource.h"
 #include "Rts.h"
 #include "Printer.h"
 
 
 #include "RtsUtils.h"
 #include "RtsFlags.h"
+#include "MBlock.h"
+#include "Storage.h"
 #include "Bytecodes.h"  /* for InstrPtr */
 #include "Disassembler.h"
 
 #include "Printer.h"
 
+#if defined(GRAN) || defined(PAR)
 // HWL: explicit fixed header size to make debugging easier
 int fixed_hs = FIXED_HS, itbl_sz = sizeofW(StgInfoTable), 
     uf_sz=sizeofW(StgUpdateFrame), sf_sz=sizeofW(StgSeqFrame); 
+#endif
 
 /* --------------------------------------------------------------------------
  * local function decls
@@ -43,33 +48,11 @@ static void    printZcoded   ( const char *raw );
  * Printer
  * ------------------------------------------------------------------------*/
 
-
-#ifdef INTERPRETER
-extern void* itblNames[];
-extern int   nItblNames;
-char* lookupHugsItblName ( void* v )
-{
-   int i;
-   for (i = 0; i < nItblNames; i += 2)
-      if (itblNames[i] == v) return itblNames[i+1];
-   return NULL;
-}
-#endif
-
 void printPtr( StgPtr p )
 {
-#ifdef INTERPRETER
-    char* str;
-#endif
     const char *raw;
     if (lookupGHCName( p, &raw )) {
         printZcoded(raw);
-#ifdef INTERPRETER
-    } else if ((raw = lookupHugsName(p)) != 0) {
-        fprintf(stderr, "%s", raw);
-    } else if ((str = lookupHugsItblName(p)) != 0) {
-        fprintf(stderr, "%p=%s", p, str);
-#endif
     } else {
         fprintf(stderr, "%p", p);
     }
@@ -87,27 +70,38 @@ static void printStdObject( StgClosure *obj, char* tag )
     const StgInfoTable* info = get_itbl(obj);
     fprintf(stderr,"%s(",tag);
     printPtr((StgPtr)obj->header.info);
+#ifdef PROFILING
+    fprintf(stderr,", %s", obj->header.prof.ccs->cc->label);
+#endif
     for (i = 0; i < info->layout.payload.ptrs; ++i) {
         fprintf(stderr,", ");
-        printPtr(payloadPtr(obj,i));
+        printPtr((StgPtr)obj->payload[i]);
     }
     for (j = 0; j < info->layout.payload.nptrs; ++j) {
-        fprintf(stderr,", %xd#",payloadWord(obj,i+j));
+        fprintf(stderr,", %pd#",obj->payload[i+j]);
     }
     fprintf(stderr,")\n");
 }
 
 void printClosure( StgClosure *obj )
 {
-    switch ( get_itbl(obj)->type ) {
+    StgInfoTable *info;
+    
+    info = get_itbl(obj);
+
+    switch ( info->type ) {
     case INVALID_OBJECT:
             barf("Invalid object");
-#ifdef INTERPRETER
     case BCO:
-            fprintf(stderr,"BCO\n");
-            disassemble(stgCast(StgBCO*,obj),"\t");
+            disassemble( (StgBCO*)obj );
             break;
-#endif
+
+    case MUT_VAR:
+        {
+         StgMutVar* mv = (StgMutVar*)obj;
+         fprintf(stderr,"MUT_VAR(var=%p, link=%p)\n", mv->var, mv->mut_link);
+          break;
+        }
 
     case AP_UPD:
         {
@@ -116,7 +110,7 @@ void printClosure( StgClosure *obj )
             fprintf(stderr,"AP_UPD("); printPtr((StgPtr)ap->fun);
             for (i = 0; i < ap->n_args; ++i) {
                 fprintf(stderr,", ");
-                printPtr(payloadPtr(ap,i));
+                printPtr((P_)ap->payload[i]);
             }
             fprintf(stderr,")\n");
             break;
@@ -129,12 +123,18 @@ void printClosure( StgClosure *obj )
             fprintf(stderr,"PAP("); printPtr((StgPtr)pap->fun);
             for (i = 0; i < pap->n_args; ++i) {
                 fprintf(stderr,", ");
-                printPtr(payloadPtr(pap,i));
+                printPtr((StgPtr)pap->payload[i]);
             }
             fprintf(stderr,")\n");
             break;
         }
 
+    case FOREIGN:
+            fprintf(stderr,"FOREIGN("); 
+            printPtr((StgPtr)( ((StgForeignObj*)obj)->data ));
+            fprintf(stderr,")\n"); 
+            break;
+
     case IND:
             fprintf(stderr,"IND("); 
             printPtr((StgPtr)stgCast(StgInd*,obj)->indirectee);
@@ -153,32 +153,6 @@ void printClosure( StgClosure *obj )
             fprintf(stderr,")\n"); 
             break;
 
-    case CAF_UNENTERED:
-        {
-           StgCAF* caf = stgCast(StgCAF*,obj);
-            fprintf(stderr,"CAF_UNENTERED("); 
-            printPtr((StgPtr)caf->body);
-            fprintf(stderr,", ");
-            printPtr((StgPtr)caf->value); /* should be null */
-            fprintf(stderr,", ");
-            printPtr((StgPtr)caf->link);  /* should be null */
-            fprintf(stderr,")\n"); 
-            break;
-        }
-
-    case CAF_ENTERED:
-        {
-           StgCAF* caf = stgCast(StgCAF*,obj);
-            fprintf(stderr,"CAF_ENTERED("); 
-            printPtr((StgPtr)caf->body);
-            fprintf(stderr,", ");
-            printPtr((StgPtr)caf->value);
-            fprintf(stderr,", ");
-            printPtr((StgPtr)caf->link);
-            fprintf(stderr,")\n"); 
-            break;
-        }
-
     case CAF_BLACKHOLE:
             fprintf(stderr,"CAF_BH("); 
             printPtr((StgPtr)stgCast(StgBlockingQueue*,obj)->blocking_queue);
@@ -203,12 +177,48 @@ void printClosure( StgClosure *obj )
             fprintf(stderr,")\n"); 
             break;
 
+    case TSO:
+      fprintf(stderr,"TSO("); 
+      fprintf(stderr,"%d (%p)",((StgTSO*)obj)->id, (StgTSO*)obj);
+      fprintf(stderr,")\n"); 
+      break;
+
+#if defined(PAR)
+    case BLOCKED_FETCH:
+      fprintf(stderr,"BLOCKED_FETCH("); 
+      printGA(&(stgCast(StgBlockedFetch*,obj)->ga));
+      printPtr((StgPtr)(stgCast(StgBlockedFetch*,obj)->node));
+      fprintf(stderr,")\n"); 
+      break;
+
+    case FETCH_ME:
+      fprintf(stderr,"FETCH_ME("); 
+      printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
+      fprintf(stderr,")\n"); 
+      break;
+
+#ifdef DIST      
+    case REMOTE_REF:
+      fprintf(stderr,"REMOTE_REF("); 
+      printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
+      fprintf(stderr,")\n"); 
+      break;
+#endif
+  
+    case FETCH_ME_BQ:
+      fprintf(stderr,"FETCH_ME_BQ("); 
+      // printGA((globalAddr *)stgCast(StgFetchMe*,obj)->ga);
+      printPtr((StgPtr)stgCast(StgFetchMeBlockingQueue*,obj)->blocking_queue);
+      fprintf(stderr,")\n"); 
+      break;
+#endif
 #if defined(GRAN) || defined(PAR)
     case RBH:
       fprintf(stderr,"RBH("); 
       printPtr((StgPtr)stgCast(StgRBH*,obj)->blocking_queue);
       fprintf(stderr,")\n"); 
       break;
+
 #endif
 
     case CONSTR:
@@ -223,21 +233,42 @@ void printClosure( StgClosure *obj )
              * tag as well.
             */
             StgWord i, j;
-            const StgInfoTable* info = get_itbl(obj);
-            fprintf(stderr,"PACK(");
+#ifdef PROFILING
+           fprintf(stderr,"%s(", info->prof.closure_desc);
+           fprintf(stderr,"%s", obj->header.prof.ccs->cc->label);
+#else
+            fprintf(stderr,"CONSTR(");
             printPtr((StgPtr)obj->header.info);
             fprintf(stderr,"(tag=%d)",info->srt_len);
+#endif
             for (i = 0; i < info->layout.payload.ptrs; ++i) {
-                fprintf(stderr,", ");
-                printPtr(payloadPtr(obj,i));
+               fprintf(stderr,", ");
+                printPtr((StgPtr)obj->payload[i]);
             }
             for (j = 0; j < info->layout.payload.nptrs; ++j) {
-                fprintf(stderr,", %x#",payloadWord(obj,i+j));
+                fprintf(stderr,", %p#", obj->payload[i+j]);
             }
             fprintf(stderr,")\n");
             break;
         }
 
+#ifdef XMLAMBDA
+/* rows are mutarrays in xmlambda, maybe we should make a new type: ROW */
+    case MUT_ARR_PTRS_FROZEN:
+          {
+            StgWord i;
+            StgMutArrPtrs* p = stgCast(StgMutArrPtrs*,obj);
+
+            fprintf(stderr,"Row<%i>(",p->ptrs);
+            for (i = 0; i < p->ptrs; ++i) {
+                if (i > 0) fprintf(stderr,", ");
+                printPtr((StgPtr)(p->payload[i]));
+            }
+            fprintf(stderr,")\n");
+            break;
+          }
+#endif  
+
     case FUN:
     case FUN_1_0: case FUN_0_1: 
     case FUN_1_1: case FUN_0_2: case FUN_2_0:
@@ -250,21 +281,31 @@ void printClosure( StgClosure *obj )
     case THUNK_1_1: case THUNK_0_2: case THUNK_2_0:
     case THUNK_STATIC:
             /* ToDo: will this work for THUNK_STATIC too? */
+#ifdef PROFILING
+           printStdObject(obj,info->prof.closure_desc);
+#else
             printStdObject(obj,"THUNK");
+#endif
             break;
-#if 0
+
+    case THUNK_SELECTOR:
+            printStdObject(obj,"THUNK_SELECTOR");
+            break;
+
     case ARR_WORDS:
         {
             StgWord i;
             fprintf(stderr,"ARR_WORDS(\"");
-            /* ToDo: we can't safely assume that this is a string! */
+            /* ToDo: we can't safely assume that this is a string! 
             for (i = 0; arrWordsGetChar(obj,i); ++i) {
                 putchar(arrWordsGetChar(obj,i));
-            }
+               } */
+           for (i=0; i<((StgArrWords *)obj)->words; i++)
+             fprintf(stderr, "%ld", ((StgArrWords *)obj)->payload[i]);
             fprintf(stderr,"\")\n");
             break;
         }
-#endif
+
     case UPDATE_FRAME:
         {
             StgUpdateFrame* u = stgCast(StgUpdateFrame*,obj);
@@ -314,6 +355,7 @@ void printClosure( StgClosure *obj )
             //barf("printClosure %d",get_itbl(obj)->type);
             fprintf(stderr, "*** printClosure: unknown type %d ****\n",
                     get_itbl(obj)->type );
+            barf("printClosure %d",get_itbl(obj)->type);
             return;
     }
 }
@@ -341,16 +383,21 @@ StgPtr printStackObj( StgPtr sp )
     } else {
         StgClosure* c = (StgClosure*)(*sp);
         printPtr((StgPtr)*sp);
-#ifdef INTERPRETER
-        if (c == &ret_bco_info) {
-           fprintf(stderr, "\t\t");
-           fprintf(stderr, "ret_bco_info\n" );
+        if (c == (StgClosure*)&stg_ctoi_ret_R1p_info) {
+           fprintf(stderr, "\t\t\tstg_ctoi_ret_R1p_info\n" );
+       } else
+        if (c == (StgClosure*)&stg_ctoi_ret_R1n_info) {
+           fprintf(stderr, "\t\t\tstg_ctoi_ret_R1n_info\n" );
+       } else
+        if (c == (StgClosure*)&stg_ctoi_ret_F1_info) {
+           fprintf(stderr, "\t\t\tstg_ctoi_ret_F1_info\n" );
+       } else
+        if (c == (StgClosure*)&stg_ctoi_ret_D1_info) {
+           fprintf(stderr, "\t\t\tstg_ctoi_ret_D1_info\n" );
+       } else
+        if (c == (StgClosure*)&stg_ctoi_ret_V_info) {
+           fprintf(stderr, "\t\t\tstg_ctoi_ret_V_info\n" );
        } else
-        if (IS_HUGS_CONSTR_INFO(GET_INFO(c))) {
-           fprintf(stderr, "\t\t\t");
-           fprintf(stderr, "ConstrInfoTable\n" );
-        } else
-#endif
         if (get_itbl(c)->type == BCO) {
            fprintf(stderr, "\t\t\t");
            fprintf(stderr, "BCO(...)\n"); 
@@ -367,7 +414,7 @@ StgPtr printStackObj( StgPtr sp )
 
 void printStackChunk( StgPtr sp, StgPtr spBottom )
 {
-    StgWord32 bitmap;
+    StgWord bitmap;
     const StgInfoTable *info;
 
     ASSERT(sp <= spBottom);
@@ -410,12 +457,12 @@ void printStackChunk( StgPtr sp, StgPtr spBottom )
          sp++;
        small_bitmap:
          while (bitmap != 0) {
-           fprintf(stderr,"Stack[%d] (%p) = ", spBottom-sp, sp);
+           fprintf(stderr,"   stk[%ld] (%p) = ", spBottom-sp, sp);
            if ((bitmap & 1) == 0) {
              printPtr((P_)*sp);
              fprintf(stderr,"\n");
            } else {
-             fprintf(stderr,"Word# %d\n", *sp++);
+             fprintf(stderr,"Word# %ld\n", *sp++);
            }         
            sp++;
            bitmap = bitmap >> 1;
@@ -430,7 +477,7 @@ void printStackChunk( StgPtr sp, StgPtr spBottom )
          break;
        }
       }
-      fprintf(stderr,"Stack[%d] (%p) = ", spBottom-sp, sp);
+      fprintf(stderr,"Stack[%ld] (%p) = ", spBottom-sp, sp);
       sp = printStackObj(sp);
     }
 }
@@ -475,6 +522,94 @@ void printTSO( StgTSO *tso )
     /* printStackChunk( tso->sp, tso->stack+tso->stack_size); */
 }
 
+/* -----------------------------------------------------------------------------
+   Closure types
+   
+   NOTE: must be kept in sync with the closure types in includes/ClosureTypes.h
+   -------------------------------------------------------------------------- */
+
+static char *closure_type_names[] = {
+  "INVALID_OBJECT",            /* 0  */
+  "CONSTR",                    /* 1  */
+  "CONSTR_1_0",                        /* 2  */
+  "CONSTR_0_1",                        /* 3  */
+  "CONSTR_2_0",                        /* 4  */
+  "CONSTR_1_1",                        /* 5  */
+  "CONSTR_0_2",                        /* 6  */
+  "CONSTR_INTLIKE",            /* 7  */
+  "CONSTR_CHARLIKE",           /* 8  */
+  "CONSTR_STATIC",             /* 9  */
+  "CONSTR_NOCAF_STATIC",       /* 10 */
+  "FUN",                       /* 11 */
+  "FUN_1_0",                   /* 12 */
+  "FUN_0_1",                   /* 13 */
+  "FUN_2_0",                   /* 14 */
+  "FUN_1_1",                   /* 15 */
+  "FUN_0_2",                   /* 16 */
+  "FUN_STATIC",                        /* 17 */
+  "THUNK",                     /* 18 */
+  "THUNK_1_0",                 /* 19 */
+  "THUNK_0_1",                 /* 20 */
+  "THUNK_2_0",                 /* 21 */
+  "THUNK_1_1",                 /* 22 */
+  "THUNK_0_2",                 /* 23 */
+  "THUNK_STATIC",              /* 24 */
+  "THUNK_SELECTOR",            /* 25 */
+  "BCO",                       /* 26 */
+  "AP_UPD",                    /* 27 */
+  "PAP",                       /* 28 */
+  "IND",                       /* 29 */
+  "IND_OLDGEN",                        /* 30 */
+  "IND_PERM",                  /* 31 */
+  "IND_OLDGEN_PERM",           /* 32 */
+  "IND_STATIC",                        /* 33 */
+  "CAF_BLACKHOLE",             /* 36 */
+  "RET_BCO",                   /* 37 */
+  "RET_SMALL",                 /* 38 */
+  "RET_VEC_SMALL",             /* 39 */
+  "RET_BIG",                   /* 40 */
+  "RET_VEC_BIG",               /* 41 */
+  "RET_DYN",                   /* 42 */
+  "UPDATE_FRAME",              /* 43 */
+  "CATCH_FRAME",               /* 44 */
+  "STOP_FRAME",                        /* 45 */
+  "SEQ_FRAME",                 /* 46 */
+  "BLACKHOLE",                 /* 47 */
+  "BLACKHOLE_BQ",              /* 48 */
+  "SE_BLACKHOLE",              /* 49 */
+  "SE_CAF_BLACKHOLE",          /* 50 */
+  "MVAR",                      /* 51 */
+  "ARR_WORDS",                 /* 52 */
+  "MUT_ARR_PTRS",              /* 53 */
+  "MUT_ARR_PTRS_FROZEN",       /* 54 */
+  "MUT_VAR",                   /* 55 */
+  "WEAK",                      /* 56 */
+  "FOREIGN",                   /* 57 */
+  "STABLE_NAME",               /* 58 */
+  "TSO",                       /* 59 */
+  "BLOCKED_FETCH",             /* 60 */
+  "FETCH_ME",                   /* 61 */
+  "FETCH_ME_BQ",                /* 62 */
+  "RBH",                        /* 63 */
+  "EVACUATED",                  /* 64 */
+  "REMOTE_REF",                 /* 65 */
+  "N_CLOSURE_TYPES"            /* 66 */
+};
+
+char *
+info_type(StgClosure *closure){ 
+  return closure_type_names[get_itbl(closure)->type];
+}
+
+char *
+info_type_by_ip(StgInfoTable *ip){ 
+  return closure_type_names[ip->type];
+}
+
+void
+info_hdr_type(StgClosure *closure, char *res){ 
+  strcpy(res,closure_type_names[get_itbl(closure)->type]);
+}
 
 /* --------------------------------------------------------------------------
  * Address printing code
@@ -698,7 +833,7 @@ static void printZcoded( const char *raw )
 /* Causing linking trouble on Win32 plats, so I'm
    disabling this for now. 
 */
-#if defined(HAVE_BFD_H) && !defined(_WIN32) && defined(USE_BSD)
+#ifdef USING_LIBBFD
 
 #include <bfd.h>
 
@@ -797,7 +932,7 @@ extern void DEBUG_LoadSymbols( char *name )
 
 #else /* HAVE_BFD_H */
 
-extern void DEBUG_LoadSymbols( char *name )
+extern void DEBUG_LoadSymbols( char *name STG_UNUSED )
 {
   /* nothing, yet */
 }
@@ -806,23 +941,45 @@ extern void DEBUG_LoadSymbols( char *name )
 
 #include "StoragePriv.h"
 
+void findPtr(P_ p, int);               /* keep gcc -Wall happy */
+
 void
-findPtr(P_ p)
+findPtr(P_ p, int follow)
 {
   nat s, g;
-  P_ q;
+  P_ q, r;
   bdescr *bd;
+  const int arr_size = 1024;
+  StgPtr arr[arr_size];
+  int i = 0;
 
   for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
-    for (s = 0; s < generations[g].n_steps; s++) {
-      for (bd = generations[g].steps[s].blocks; bd; bd = bd->link) {
-       for (q = bd->start; q < bd->free; q++) {
-         if (*q == (W_)p) {
-           printf("%p\n", q);
+      for (s = 0; s < generations[g].n_steps; s++) {
+         if (RtsFlags.GcFlags.generations == 1) {
+             bd = generations[g].steps[s].to_blocks;
+         } else {
+             bd = generations[g].steps[s].blocks;
+         }
+         for (; bd; bd = bd->link) {
+             for (q = bd->start; q < bd->free; q++) {
+                 if (*q == (W_)p) {
+                     if (i < arr_size) {
+                         r = q;
+                         while (!LOOKS_LIKE_GHC_INFO(*r)) { r--; };
+                         fprintf(stderr, "%p = ", r);
+                         printClosure((StgClosure *)r);
+                         arr[i++] = r;
+                     } else {
+                         return;
+                     }
+                 }
+             }
          }
-       }
       }
-    }
+  }
+  if (follow && i == 1) {
+      fprintf(stderr, "-->\n");
+      findPtr(arr[0], 1);
   }
 }