home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dbmalloc.zip / dump.c < prev    next >
C/C++ Source or Header  |  1993-01-04  |  11KB  |  448 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.  
  285.             for(i=0; (i < FILE_LEN) && (ptr->file[i] != EOS); i++)
  286.             {
  287.                 buffer[detail+9+i] = ptr->file[i];
  288.             }
  289.  
  290.             VOIDCAST tostring(buffer+detail+30,
  291.                         (ULONG)ptr->line,7,B_DEC, ' ');
  292.         }
  293.         else
  294.         {
  295.             for(i=0; i < (sizeof("unknown")-1); i++)
  296.             {
  297.                 buffer[detail+9+i] = "unknown"[i];
  298.             }
  299.         }
  300.             
  301.         func = MallocFuncName(ptr);
  302.         /*
  303.          * copy the function name into the string.
  304.          */
  305.         for( i=0; func[i] != EOS; i++)
  306.         {
  307.             buffer[detail+38+i] = func[i];
  308.         }
  309.  
  310.         /*
  311.          * add the call number
  312.          */
  313.         buffer[detail+38+ i++] = '(';
  314.         i += tostring(buffer+detail+38+i,(ULONG)ptr->id,0,B_DEC,' ');
  315.         buffer[detail+38+i] = ')';
  316.     
  317.         /*
  318.          * display the length of the segment
  319.          */
  320.         VOIDCAST tostring(buffer+detail+53,
  321.                 (ULONG)ptr->r_size,7,B_DEC,' ');
  322.  
  323.         /* 
  324.          * display the first seven bytes of data
  325.          */
  326.         for( i=0; (i < NUM_BYTES) && (i < ptr->r_size); i++)
  327.         {
  328.             VOIDCAST tostring(buffer+detail + 61 + (i * 2),
  329.                     (ULONG)ptr->data[i], 2, B_HEX, '0');
  330.         }
  331.  
  332.         buffer[detail + sizeof(NORMAL_HDR_3)] = '\n';
  333.         WRITEOUT(fd,buffer,detail+sizeof(NORMAL_HDR_3)+1);
  334.  
  335.         /*
  336.          * and dump any stack info (if it was specified by the user)
  337.          */
  338.         StackDump(fd,(char *) NULL,ptr->stack);
  339.  
  340.     }
  341.  
  342.     if( detail != DETAIL_NONE )
  343.     {
  344.         WRITEOUT(fd,"Malloc start:      ",19);
  345.         VOIDCAST tostring(buffer, (ULONG)&malloc_start, 10, B_HEX, '0');
  346.         buffer[10] = '\n';
  347.         WRITEOUT(fd,buffer,11);
  348.  
  349.         WRITEOUT(fd,"Malloc end:        ", 19);
  350.         VOIDCAST tostring(buffer, (ULONG) malloc_end, 10, B_HEX, '0');
  351.         buffer[10] = '\n';
  352.         WRITEOUT(fd,buffer,11);
  353.  
  354.         WRITEOUT(fd,"Malloc data start: ", 19);
  355.         VOIDCAST tostring(buffer,(ULONG)malloc_data_start,10,B_HEX,'0');
  356.         buffer[10] = '\n';
  357.         WRITEOUT(fd,buffer,11);
  358.  
  359.         WRITEOUT(fd,"Malloc data end:   ", 19);
  360.         VOIDCAST tostring(buffer,(ULONG)malloc_data_end, 10,B_HEX, '0');
  361.         buffer[10] = '\n';
  362.         WRITEOUT(fd,buffer,11);
  363.  
  364.         WRITEOUT(fd,"Malloc free list:  ", 19);
  365.         VOIDCAST tostring(buffer, (ULONG)malloc_freelist, 10,B_HEX,'0');
  366.         buffer[10] = '\n';
  367.         WRITEOUT(fd,buffer,11);
  368.  
  369.         for(ptr=malloc_freelist->freenext; ptr!=NULL; ptr=ptr->freenext)
  370.         {
  371.             WRITEOUT(fd,"                -> ", 19);
  372.             VOIDCAST tostring(buffer, (ULONG) ptr, 10, B_HEX, '0');
  373.             buffer[10] = '\n';
  374.             WRITEOUT(fd,buffer,11);
  375.         }
  376.  
  377.     }
  378.  
  379.     WRITEOUT(fd,"\n",1);
  380.     
  381. } /* malloc_dump(... */
  382.  
  383.  
  384. /*
  385.  * $Log: dump.c,v $
  386.  * Revision 1.20  1992/08/22  16:27:13  cpcahil
  387.  * final changes for pl14
  388.  *
  389.  * Revision 1.19  1992/06/30  13:06:39  cpcahil
  390.  * added support for aligned allocations
  391.  *
  392.  * Revision 1.18  1992/06/22  23:40:10  cpcahil
  393.  * many fixes for working on small int systems
  394.  *
  395.  * Revision 1.17  1992/05/08  02:30:35  cpcahil
  396.  * minor cleanups from minix/atari port
  397.  *
  398.  * Revision 1.16  1992/04/24  12:09:13  cpcahil
  399.  * (hopefully) final cleanup for patch 10
  400.  *
  401.  * Revision 1.15  1992/04/22  18:17:32  cpcahil
  402.  * added support for Xt Alloc functions, linted code
  403.  *
  404.  * Revision 1.14  1992/04/15  12:51:06  cpcahil
  405.  * fixes per testing of patch 8
  406.  *
  407.  * Revision 1.13  1992/04/14  02:27:30  cpcahil
  408.  * adjusted output of pointes so that values that had the high bit
  409.  * set would print correctly.
  410.  *
  411.  * Revision 1.12  1992/04/13  19:57:15  cpcahil
  412.  * more patch 8 fixes
  413.  *
  414.  * Revision 1.11  1992/04/13  03:06:33  cpcahil
  415.  * Added Stack support, marking of non-leaks, auto-config, auto-testing
  416.  *
  417.  * Revision 1.10  1992/01/30  12:23:06  cpcahil
  418.  * renamed mallocint.h -> mallocin.h
  419.  *
  420.  * Revision 1.9  1992/01/10  17:28:03  cpcahil
  421.  * Added support for overriding void datatype
  422.  *
  423.  * Revision 1.8  1991/12/04  09:23:36  cpcahil
  424.  * several performance enhancements including addition of free list
  425.  *
  426.  * Revision 1.7  91/11/25  14:41:52  cpcahil
  427.  * Final changes in preparation for patch 4 release
  428.  * 
  429.  * Revision 1.6  91/11/24  00:49:25  cpcahil
  430.  * first cut at patch 4
  431.  * 
  432.  * Revision 1.5  90/08/29  21:22:37  cpcahil
  433.  * miscellaneous lint fixes
  434.  * 
  435.  * Revision 1.4  90/05/11  00:13:08  cpcahil
  436.  * added copyright statment
  437.  * 
  438.  * Revision 1.3  90/02/24  21:50:07  cpcahil
  439.  * lots of lint fixes
  440.  * 
  441.  * Revision 1.2  90/02/24  17:27:48  cpcahil
  442.  * changed $header to $Id to remove full path from rcs id string
  443.  * 
  444.  * Revision 1.1  90/02/22  23:17:43  cpcahil
  445.  * Initial revision
  446.  * 
  447.  */
  448.