home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / dbmalloc-1.14-src.tgz / tar.out / contrib / dbmalloc / dump.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  11KB  |  450 lines

  1.  
  2. /*
  3.  * (c) Copyright 1990, 1991, 1992 Conor P. Cahill (cpcahil@virtech.vti.com)
  4.  *
  5.  * This software may be distributed freely as long as the following conditions
  6.  * are met:
  7.  *         * the distribution, or any derivative thereof, may not be
  8.  *          included as part of a commercial product
  9.  *        * full source code is provided including this copyright
  10.  *        * there is no charge for the software itself (there may be
  11.  *          a minimal charge for the copying or distribution effort)
  12.  *        * this copyright notice is not modified or removed from any
  13.  *          source file
  14.  */
  15. #include <stdio.h>
  16. #include "mallocin.h"
  17. #include "tostring.h"
  18.  
  19. #ifndef lint
  20. static
  21. char rcs_hdr[] = "$Id: dump.c,v 1.20 1992/08/22 16:27:13 cpcahil Exp $";
  22. #endif
  23.  
  24. /*
  25.  * various macro definitions used within this module.
  26.  */
  27.  
  28. #define WRITEOUT(fd,str,len)    if( write(fd,str,(WRTSIZE)(len)) != (len) ) \
  29.                 { \
  30.                     VOIDCAST write(2,ERRSTR,\
  31.                              (WRTSIZE)strlen(ERRSTR));\
  32.                     exit(120); \
  33.                 }
  34.  
  35. #define DETAIL_NONE         0
  36. #define DETAIL_NOT_SET_YET    -1
  37. #define DETAIL_ST_COL        (sizeof(DETAIL_HDR_3)-1)
  38. #define ERRSTR    "I/O Error on malloc dump file descriptor\n"
  39. #define FILE_LEN        20
  40. #define LIST_ALL        1
  41. #define LIST_SOME        2
  42. #define NUM_BYTES        7
  43. #define TITLE            " Dump of Malloc Chain "
  44.  
  45. #define DETAIL_HDR_1 \
  46.      "                             FREE     FREE                  ACTUAL SIZE    "
  47. #define DETAIL_HDR_2 \
  48.      "  PTR      NEXT     PREV     NEXT     PREV      FLAGS       INT    HEX     "
  49. #define DETAIL_HDR_3 \
  50.      "-------- -------- -------- -------- -------- ---------- -------- --------- "
  51.  
  52. #define NORMAL_HDR_1 \
  53.  "POINTER     FILE  WHERE         LINE      ALLOC        DATA     HEX DUMP   \n"
  54. #define NORMAL_HDR_2 \
  55.  "TO DATA      ALLOCATED         NUMBER     FUNCT       LENGTH  OF BYTES 1-7 \n"
  56. #define NORMAL_HDR_3 \
  57.  "-------- -------------------- ------- -------------- ------- --------------\n"
  58.  
  59. #define THISBUFSIZE (sizeof(NORMAL_HDR_3)+sizeof(DETAIL_HDR_3))
  60.  
  61.  
  62. /*
  63.  * Function:    malloc_dump()
  64.  *
  65.  * Purpose:    to dump a printed copy of the malloc chain and
  66.  *        associated data elements
  67.  *
  68.  * Arguments:    fd    - file descriptor to write data to
  69.  *
  70.  * Returns:    nothing of any use
  71.  *
  72.  * Narrative:    Just print out all the junk
  73.  *
  74.  */
  75. VOIDTYPE
  76. malloc_dump(fd)
  77.     int        fd;
  78. {
  79.     malloc_list_items(fd,LIST_ALL,0L,0L);
  80. }
  81.  
  82. /*
  83.  * Function:    malloc_list()
  84.  *
  85.  * Purpose:    to dump a printed copy of the malloc chain and
  86.  *        associated data elements
  87.  *
  88.  * Arguments:    fd    - file descriptor to write data to
  89.  *        histid1 - id of the first record to display
  90.  *        histid2 - id one above the last record to display
  91.  *
  92.  * Returns:    nothing of any use
  93.  *
  94.  * Narrative:    Just call malloc_list_items to display the junk
  95.  *
  96.  */
  97. VOIDTYPE
  98. malloc_list(fd,histid1,histid2)
  99.     int        fd;
  100.     IDTYPE        histid1;
  101.     IDTYPE        histid2;
  102. {
  103.     malloc_list_items(fd, LIST_SOME, histid1, histid2);
  104. }
  105.  
  106. /*
  107.  * Function:    malloc_list_items()
  108.  *
  109.  * Purpose:    to dump a printed copy of the malloc chain and
  110.  *        associated data elements
  111.  *
  112.  * Arguments:    fd      - file descriptor to write data to
  113.  *        list_type - type of list (all records, or a selected list)
  114.  *        histid1      - first id to list (if type is some)
  115.  *        histid2   - one above last id to list
  116.  *
  117.  * Returns:    nothing of any use
  118.  *
  119.  * Narrative:    Just print out all the junk
  120.  *
  121.  * Notes:    This function is implemented using low level calls because
  122.  *         of the likelyhood that the malloc tree is damaged when it
  123.  *        is called.  (Lots of things in the c library use malloc and
  124.  *        we don't want to get into a catch-22).
  125.  *
  126.  */
  127. VOIDTYPE
  128. malloc_list_items(fd,list_type,histid1,histid2)
  129.     int              fd;
  130.     int              list_type;
  131.     IDTYPE              histid1;
  132.     IDTYPE              histid2;
  133. {
  134.     char              buffer[THISBUFSIZE];
  135.     int              detail;
  136.     int              first_time = 1;
  137.     CONST char        * func;
  138.     int              i;
  139.     int              loc;
  140.     struct mlist         * ptr;
  141.  
  142.     MALLOC_INIT();
  143.  
  144.     if( (malloc_opts & MOPT_DETAIL) != 0 )
  145.     {
  146.         detail = DETAIL_ST_COL;
  147.     }
  148.     else
  149.     {
  150.         detail = DETAIL_NONE;
  151.     }
  152.  
  153.     /*
  154.      * for each element in the trace
  155.      */
  156.     for(ptr = &malloc_start; ptr; ptr = ptr->next)
  157.     {
  158.         /*
  159.          * if this item is not in use or it is a stack element
  160.          *     and we are not in detail mode or list-all mode.
  161.          */
  162.         if(  ( ((ptr->flag & M_INUSE)==0) || (GETTYPE(ptr)==M_T_STACK) )
  163.             && ((detail == DETAIL_NONE) || (list_type != LIST_ALL)) )
  164.         {
  165.             continue;
  166.         }
  167.         /*
  168.          * else if we are only listing a range of items, check to see
  169.          * if this item is in the correct range and is not marked. 
  170.          * if not, skip it
  171.           */
  172.         else if(   (list_type == LIST_SOME)
  173.             && (    (ptr->hist_id < histid1)
  174.                  || (ptr->hist_id >= histid2)
  175.                  || ((ptr->flag & M_MARKED) != 0) ) )
  176.         {
  177.             continue;
  178.         }
  179.  
  180.         /*
  181.          * if this is the first time, put out the headers.
  182.          */
  183.         if( first_time )
  184.         {
  185.             /*
  186.              * fill the title line with asterisks
  187.              */
  188.             for(i=0; i < (detail + sizeof(NORMAL_HDR_3)-1); i++)
  189.             {
  190.                 buffer[i] = '*';
  191.             }
  192.             buffer[i] = '\n';
  193.             buffer[i+1] = EOS;
  194.  
  195.             /*
  196.              * add in the title  (centered, of course)
  197.              */
  198.             loc = (i - sizeof(TITLE)) / 2;
  199.             for(i=0; i < (sizeof(TITLE)-1); i++)
  200.             {
  201.                 buffer[loc+i] = TITLE[i];
  202.             }
  203.  
  204.             /*
  205.              * and write it out
  206.              */
  207.             WRITEOUT(fd,buffer,strlen(buffer));
  208.  
  209.             /*
  210.              * write out the column headers
  211.              */
  212.             if( detail != DETAIL_NONE )
  213.             {
  214.                 WRITEOUT(fd,DETAIL_HDR_1,
  215.                         sizeof(DETAIL_HDR_1)-1);
  216.                 WRITEOUT(fd,NORMAL_HDR_1,
  217.                         sizeof(NORMAL_HDR_1)-1);
  218.                 WRITEOUT(fd,DETAIL_HDR_2,
  219.                         sizeof(DETAIL_HDR_2)-1);
  220.                 WRITEOUT(fd,NORMAL_HDR_2,
  221.                         sizeof(NORMAL_HDR_2)-1);
  222.                 WRITEOUT(fd,DETAIL_HDR_3,
  223.                         sizeof(DETAIL_HDR_3)-1);
  224.                 WRITEOUT(fd,NORMAL_HDR_3,
  225.                         sizeof(NORMAL_HDR_3)-1);
  226.             }
  227.             else
  228.             {
  229.                 WRITEOUT(fd,NORMAL_HDR_1,
  230.                         sizeof(NORMAL_HDR_1)-1);
  231.                 WRITEOUT(fd,NORMAL_HDR_2,
  232.                         sizeof(NORMAL_HDR_2)-1);
  233.                 WRITEOUT(fd,NORMAL_HDR_3,
  234.                         sizeof(NORMAL_HDR_3)-1);
  235.             }
  236.  
  237.             first_time = 0;
  238.         }
  239.  
  240.         /*
  241.          * fill in the string with blanks
  242.          */
  243.         for(i=0; i < (sizeof(DETAIL_HDR_3)+sizeof(NORMAL_HDR_3)); i++)
  244.         {
  245.             buffer[i] = ' ';
  246.         }
  247.  
  248.         /*
  249.           * handle detail output
  250.          */
  251.         if( detail != DETAIL_NONE )
  252.         {
  253.             VOIDCAST tostring(buffer,
  254.                     (ULONG)ptr,8,B_HEX,'0');
  255.             VOIDCAST tostring(buffer+9,
  256.                     (ULONG)ptr->next,8,B_HEX,'0');
  257.             VOIDCAST tostring(buffer+18,
  258.                     (ULONG)ptr->prev,8,B_HEX,'0');
  259.             VOIDCAST tostring(buffer+27,
  260.                     (ULONG)ptr->freenext,8,B_HEX,'0');
  261.             VOIDCAST tostring(buffer+36,
  262.                     (ULONG)ptr->freeprev,8,B_HEX,'0');
  263.             VOIDCAST tostring(buffer+45,
  264.                     (ULONG)ptr->flag,10,B_HEX,'0');
  265.             VOIDCAST tostring(buffer+56,
  266.                     (ULONG)ptr->s.size,8,B_DEC,' ');
  267.             VOIDCAST tostring(buffer+65,
  268.                     (ULONG)ptr->s.size,8,B_HEX,'0');
  269.             buffer[64] = '(';
  270.             buffer[74] = ')';
  271.         }
  272.  
  273.         /*
  274.          * and now add in the normal stuff
  275.          */
  276.          VOIDCAST tostring(buffer+detail,
  277.                 (ULONG) ptr->data, 8, B_HEX, '0');
  278.  
  279.         /*
  280.          * if a file has been specified
  281.          */
  282.         if( (ptr->file != NULL)     && (ptr->file[0] != EOS) )
  283.         {
  284.             const char *filename;
  285.             filename = strrchr (ptr->file, '/');
  286.             filename = filename ? filename + 1 : ptr->file;
  287.             for(i=0; (i < FILE_LEN) && (filename[i] != EOS); i++)
  288.             {
  289.                 buffer[detail+9+i] = filename[i];
  290.             }
  291.  
  292.             VOIDCAST tostring(buffer+detail+30,
  293.                         (ULONG)ptr->line,7,B_DEC, ' ');
  294.         }
  295.         else
  296.         {
  297.             for(i=0; i < (sizeof("unknown")-1); i++)
  298.             {
  299.                 buffer[detail+9+i] = "unknown"[i];
  300.             }
  301.         }
  302.             
  303.         func = MallocFuncName(ptr);
  304.         /*
  305.          * copy the function name into the string.
  306.          */
  307.         for( i=0; func[i] != EOS; i++)
  308.         {
  309.             buffer[detail+38+i] = func[i];
  310.         }
  311.  
  312.         /*
  313.          * add the call number
  314.          */
  315.         buffer[detail+38+ i++] = '(';
  316.         i += tostring(buffer+detail+38+i,(ULONG)ptr->id,0,B_DEC,' ');
  317.         buffer[detail+38+i] = ')';
  318.     
  319.         /*
  320.          * display the length of the segment
  321.          */
  322.         VOIDCAST tostring(buffer+detail+53,
  323.                 (ULONG)ptr->r_size,7,B_DEC,' ');
  324.  
  325.         /* 
  326.          * display the first seven bytes of data
  327.          */
  328.         for( i=0; (i < NUM_BYTES) && (i < ptr->r_size); i++)
  329.         {
  330.             VOIDCAST tostring(buffer+detail + 61 + (i * 2),
  331.                     (ULONG)ptr->data[i], 2, B_HEX, '0');
  332.         }
  333.  
  334.         buffer[detail + sizeof(NORMAL_HDR_3)] = '\n';
  335.         WRITEOUT(fd,buffer,detail+sizeof(NORMAL_HDR_3)+1);
  336.  
  337.         /*
  338.          * and dump any stack info (if it was specified by the user)
  339.          */
  340.         StackDump(fd,(char *) NULL,ptr->stack);
  341.  
  342.     }
  343.  
  344.     if( detail != DETAIL_NONE )
  345.     {
  346.         WRITEOUT(fd,"Malloc start:      ",19);
  347.         VOIDCAST tostring(buffer, (ULONG)&malloc_start, 10, B_HEX, '0');
  348.         buffer[10] = '\n';
  349.         WRITEOUT(fd,buffer,11);
  350.  
  351.         WRITEOUT(fd,"Malloc end:        ", 19);
  352.         VOIDCAST tostring(buffer, (ULONG) malloc_end, 10, B_HEX, '0');
  353.         buffer[10] = '\n';
  354.         WRITEOUT(fd,buffer,11);
  355.  
  356.         WRITEOUT(fd,"Malloc data start: ", 19);
  357.         VOIDCAST tostring(buffer,(ULONG)malloc_data_start,10,B_HEX,'0');
  358.         buffer[10] = '\n';
  359.         WRITEOUT(fd,buffer,11);
  360.  
  361.         WRITEOUT(fd,"Malloc data end:   ", 19);
  362.         VOIDCAST tostring(buffer,(ULONG)malloc_data_end, 10,B_HEX, '0');
  363.         buffer[10] = '\n';
  364.         WRITEOUT(fd,buffer,11);
  365.  
  366.         WRITEOUT(fd,"Malloc free list:  ", 19);
  367.         VOIDCAST tostring(buffer, (ULONG)malloc_freelist, 10,B_HEX,'0');
  368.         buffer[10] = '\n';
  369.         WRITEOUT(fd,buffer,11);
  370.  
  371.         for(ptr=malloc_freelist->freenext; ptr!=NULL; ptr=ptr->freenext)
  372.         {
  373.             WRITEOUT(fd,"                -> ", 19);
  374.             VOIDCAST tostring(buffer, (ULONG) ptr, 10, B_HEX, '0');
  375.             buffer[10] = '\n';
  376.             WRITEOUT(fd,buffer,11);
  377.         }
  378.  
  379.     }
  380.  
  381.     WRITEOUT(fd,"\n",1);
  382.     
  383. } /* malloc_dump(... */
  384.  
  385.  
  386. /*
  387.  * $Log: dump.c,v $
  388.  * Revision 1.20  1992/08/22  16:27:13  cpcahil
  389.  * final changes for pl14
  390.  *
  391.  * Revision 1.19  1992/06/30  13:06:39  cpcahil
  392.  * added support for aligned allocations
  393.  *
  394.  * Revision 1.18  1992/06/22  23:40:10  cpcahil
  395.  * many fixes for working on small int systems
  396.  *
  397.  * Revision 1.17  1992/05/08  02:30:35  cpcahil
  398.  * minor cleanups from minix/atari port
  399.  *
  400.  * Revision 1.16  1992/04/24  12:09:13  cpcahil
  401.  * (hopefully) final cleanup for patch 10
  402.  *
  403.  * Revision 1.15  1992/04/22  18:17:32  cpcahil
  404.  * added support for Xt Alloc functions, linted code
  405.  *
  406.  * Revision 1.14  1992/04/15  12:51:06  cpcahil
  407.  * fixes per testing of patch 8
  408.  *
  409.  * Revision 1.13  1992/04/14  02:27:30  cpcahil
  410.  * adjusted output of pointes so that values that had the high bit
  411.  * set would print correctly.
  412.  *
  413.  * Revision 1.12  1992/04/13  19:57:15  cpcahil
  414.  * more patch 8 fixes
  415.  *
  416.  * Revision 1.11  1992/04/13  03:06:33  cpcahil
  417.  * Added Stack support, marking of non-leaks, auto-config, auto-testing
  418.  *
  419.  * Revision 1.10  1992/01/30  12:23:06  cpcahil
  420.  * renamed mallocint.h -> mallocin.h
  421.  *
  422.  * Revision 1.9  1992/01/10  17:28:03  cpcahil
  423.  * Added support for overriding void datatype
  424.  *
  425.  * Revision 1.8  1991/12/04  09:23:36  cpcahil
  426.  * several performance enhancements including addition of free list
  427.  *
  428.  * Revision 1.7  91/11/25  14:41:52  cpcahil
  429.  * Final changes in preparation for patch 4 release
  430.  * 
  431.  * Revision 1.6  91/11/24  00:49:25  cpcahil
  432.  * first cut at patch 4
  433.  * 
  434.  * Revision 1.5  90/08/29  21:22:37  cpcahil
  435.  * miscellaneous lint fixes
  436.  * 
  437.  * Revision 1.4  90/05/11  00:13:08  cpcahil
  438.  * added copyright statment
  439.  * 
  440.  * Revision 1.3  90/02/24  21:50:07  cpcahil
  441.  * lots of lint fixes
  442.  * 
  443.  * Revision 1.2  90/02/24  17:27:48  cpcahil
  444.  * changed $header to $Id to remove full path from rcs id string
  445.  * 
  446.  * Revision 1.1  90/02/22  23:17:43  cpcahil
  447.  * Initial revision
  448.  * 
  449.  */
  450.