home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckc190.zip / ckcmdb.c < prev    next >
C/C++ Source or Header  |  1993-10-24  |  7KB  |  347 lines

  1. /*
  2.   C K C M D B . C  --  malloc debugger.
  3. */
  4.  
  5. /*
  6.   Author: Howie Kaye, Columbia University Center for Computing Activities.
  7.   Copyright (C) 1985, 1992, Trustees of Columbia University in the City of New
  8.   York.  Permission is granted to any individual or institution to use this
  9.   software as long as it is not sold for profit.  This copyright notice must be
  10.   retained.  This software may not be included in commercial products without
  11.   written permission of Columbia University.
  12. */
  13. #include "ckcsym.h"
  14. #include <stdio.h>
  15. #include "ckcdeb.h"
  16.  
  17. #ifdef COHERENT
  18. FILE * fdopen();
  19. #endif /* COHERENT */
  20.  
  21. /*
  22.   memdebug:
  23.   variable to control memory debugging.
  24.   if memdebug ==  1, then action is always taken.
  25.   if memdebug ==  0, then no action is taken.
  26.   if memdebug == -1, then the user is asked (works well with gdb).
  27. */
  28. int memdebug = -1;
  29. int disabled = 0;
  30. int inited = 0;
  31. /*
  32.   To use this package, compile your program with:
  33.   -Dmalloc=dmalloc -Dfree=dfree =Dcalloc=dcalloc ... -DMDEBUG
  34.   and then link it with ckcmdb.c.
  35. */
  36. #ifdef MDEBUG
  37. /* Use the real ones in this module! */
  38. #ifdef malloc
  39. #undef malloc
  40. #endif /* malloc */
  41. #ifdef calloc
  42. #undef calloc
  43. #endif /* calloc */
  44. #ifdef realloc
  45. #undef realloc
  46. #endif /* realloc */
  47. #ifdef free
  48. #undef free
  49. #endif /* free */
  50.  
  51. char *malloc(), *realloc();
  52. char *set_range_check();
  53. char *check_range();
  54. char *maybe_check_range();
  55.  
  56. #define min(x,y) ((x) < (y) ? (x) : (y))
  57. #define RANGE "ABCDEFGHIJKLMNOP"
  58. #define INTSIZE  sizeof(int)
  59. #define LONGSIZE sizeof(long)
  60. #define RSIZE    sizeof(RANGE)
  61. #define RFRONT   min((RSIZE/2),LONGSIZE)
  62. #define RBACK    min((RSIZE-RFRONT),LONGSIZE)
  63.  
  64. char *
  65. dmalloc(size) int size; {
  66.     char *cp;
  67.  
  68.     cp = malloc(size + RSIZE + INTSIZE);
  69.     if (cp) {
  70.     cp = set_range_check(cp, size);
  71.     m_insert(cp);
  72.     }
  73.     return(cp);
  74. }
  75.  
  76. char *
  77. dcalloc(nelem, elsize) int nelem, elsize; {
  78.     char *cp;
  79.  
  80.     cp = dmalloc(nelem * elsize);
  81.     if (cp)
  82.     bzero(cp, nelem * elsize);
  83.     return(cp);
  84. }
  85.  
  86. char *
  87. drealloc(bp,size) char *bp; int size; {
  88.     char *cp;
  89.  
  90.     if (bp == NULL) {
  91.     maybe_quit("Freeing NULL pointer");
  92.     } else {
  93.     m_delete(bp);
  94.     cp = check_range(bp);
  95.     }
  96.     cp = realloc(cp, size + RSIZE + INTSIZE);
  97.     if (cp) {
  98.     cp = set_range_check(cp, size);
  99.     m_insert(cp);
  100.     }
  101.     return(cp);
  102. }
  103.  
  104. dfree(cp) char *cp; {
  105.     if (cp == NULL)
  106.     maybe_quit("Freeing NULL pointer");
  107.     else {
  108.     switch(m_delete(cp)) {
  109.     case 0:
  110.         cp = maybe_check_range(cp);
  111.         break;
  112.     case 1:
  113.         cp = check_range(cp);
  114.         break;
  115.     case 2:
  116.         break;
  117.     }
  118.     }
  119.     return(free(cp));
  120. }
  121.  
  122. char *
  123. set_range_check(cp,size) char *cp; int size; {
  124.     register int i;
  125.     int tmp = size;
  126.  
  127.     for(i = 0; i < INTSIZE; i++) {    /* set the size in the string */
  128.     cp[i] = tmp & 0xff;
  129.     tmp >>= 8;
  130.     }
  131.     cp += INTSIZE;            /* skip the size */
  132.  
  133.     for(i = 0; i < RFRONT; i++)        /* set the front of the range check */
  134.     cp[i] = RANGE[i];        /* string */
  135.  
  136.     cp += RFRONT;            /* skip the front range check */
  137.  
  138.     for(i = 0; i < RBACK; i++)        /* set the back odf the range check */
  139.     cp[i+size] = RANGE[i+RFRONT];
  140.  
  141.     return(cp);
  142. }
  143.  
  144. /*
  145.   Put calls to this routine in your code any place where you want to
  146.   check whether you've copied too many characters into a malloc'd space.
  147. */
  148. char *
  149. check_range(cp) char *cp; {
  150.     register char *bp = cp - RFRONT - INTSIZE;
  151.     char *xp = bp;
  152.     register int i;
  153.     int size = 0;
  154.  
  155.     for(i = 0 ; i < INTSIZE; i++) {    /* get the size out of the string */
  156.     size <<= 8;
  157.     size |= bp[INTSIZE-i-1] & 0xff;
  158.     }
  159.     bp += INTSIZE;
  160.  
  161.     for(i = 0; i < RFRONT; i++)        /* check front range check */
  162.     if (bp[i] != RANGE[i]) {
  163.         maybe_quit("leftside malloc buffer overrun");
  164.         break;
  165.     }
  166.     bp += RFRONT;            /* skip front range check */
  167.  
  168.     for(i = 0; i < RBACK; i++)        /* check back range check */
  169.     if (bp[i+size] != RANGE[i+RFRONT]) {
  170.         maybe_quit("rightside malloc buffer overrun");
  171.         break;
  172.     }
  173.     return(xp);
  174. }
  175.  
  176. static char *
  177. maybe_check_range(cp) char *cp; {
  178.     register char *bp = cp - RFRONT - INTSIZE;
  179.     char *xp = bp;
  180.     register int i;
  181.     int size = 0;
  182.  
  183.     for(i = 0 ; i < INTSIZE; i++) {    /* get the size out of the string */
  184.     size <<= 8;
  185.     size |= bp[INTSIZE-i-1] & 0xff;
  186.     }
  187.     bp += INTSIZE;
  188.  
  189.     for(i = 0; i < RFRONT; i++)        /* check front range check */
  190.     if (bp[i] != RANGE[i]) {
  191.         return(cp);
  192.     }
  193.     bp += RFRONT;            /* skip front range check */
  194.  
  195.     for(i = 0; i < RBACK; i++)        /* check back range check */
  196.     if (bp[i+size] != RANGE[i+RFRONT]) {
  197.         fprintf(stderr,"rightside malloc buffer overrun\n");
  198.         abort();
  199.         break;
  200.     }
  201.     return(xp);
  202. }
  203.  
  204. #define BUCKETS 10000
  205. char *m_used[BUCKETS];
  206. char *m_used2[BUCKETS];
  207.  
  208. VOID
  209. m_insert(cp) register char *cp; {
  210.     register int i;
  211.  
  212.     if (disabled)
  213.     return;
  214.  
  215.     for(i = 0; i < BUCKETS; i++)
  216.     if (m_used[i] == 0) {
  217.         m_used[i] = cp;
  218.         return;
  219.     }
  220.     disabled ++;
  221. }
  222.  
  223. static
  224. m_insert2(cp) register char *cp; {
  225.     register int i;
  226.  
  227.     if (disabled)
  228.     return;
  229.     for(i = 0; i < BUCKETS; i++)
  230.     if (m_used2[i] == 0) {
  231.         m_used2[i] = cp;
  232.         return;
  233.     }
  234.     disabled ++;
  235. }
  236.  
  237. VOID
  238. m_delete(cp) register char *cp; {
  239.     register int i;
  240.  
  241.     for(i = 0; i < BUCKETS; i++)
  242.     if (m_used[i] == cp) {
  243.         m_used[i] = 0;
  244.         return(1);
  245.     }
  246.     for(i = 0; i < BUCKETS; i++)
  247.     if (m_used2[i] == cp) {
  248.         m_used2[i] = 0;
  249.         return(2);
  250.     }
  251.     if (disabled) 
  252.     return(0);
  253.  
  254.     maybe_quit("Freeing unmalloc'ed pointer");
  255.     return(0);
  256. }
  257.  
  258. VOID
  259. m_init() {
  260.     register int i;
  261.  
  262.     inited = 1;
  263.     disabled = 0;
  264.     for(i = 0; i < BUCKETS; i++)
  265.       m_used[i] = 0;
  266. }
  267.  
  268. VOID
  269. m_done() {
  270.     register int i,j=0;
  271.  
  272.     if (disabled) 
  273.     return;
  274.     for(i = 0; i < BUCKETS; i++)
  275.     if (m_used[i] != 0) {
  276.         if (memdebug) {
  277.         if (j == 0)
  278.             fprintf(stderr,"unfree'ed buffers, indices: ");
  279.         fprintf(stderr,"%d, ", i);
  280.         j++;
  281.         }
  282.     }
  283.     if (j)
  284.     fprintf(stderr,"\n");
  285.     for(i = 0; i < BUCKETS; i++)
  286.     if (m_used2[i] != 0) {
  287.         if (memdebug) {
  288.         if (j == 0)
  289.             fprintf(stderr,"unfree'ed registered buffers, indices: ");
  290.         fprintf(stderr,"%d, ", i);
  291.         j++;
  292.         }
  293.     }
  294.     if (j)
  295.     fprintf(stderr,"\n");
  296.     if (j)
  297.     maybe_quit("Unfree'ed malloc buffers");
  298. }
  299.  
  300. VOID
  301. m_checkranges() {
  302.     int i;
  303.  
  304.     for ( i = 0; i < BUCKETS; i++)
  305.     if (m_used[i])
  306.         check_range(m_used[i]);
  307. }
  308.  
  309. static VOID
  310. maybe_quit(str) char *str; {
  311.     debug(F100,"mdebug maybe_quit","",0);
  312.     if (memdebug == 0)
  313.     return;
  314.     fprintf(stderr,"%s\n",str);
  315.     if (memdebug == 1)
  316.     abort();
  317.     if (memdebug == -1)
  318.     if (ask("Quit? "))
  319.         abort();
  320. }
  321.  
  322. static int
  323. ask(str) char *str; {
  324.     char buf[100];
  325.     FILE *in;
  326.     int fd;
  327.     
  328.     fd = dup(fileno(stdin));
  329.     in = fdopen(fd, "r");
  330.     while(1) {
  331.     fprintf(stderr,str);
  332.     fflush(stderr);
  333.     if (fgets(buf, 99, in) == NULL)    /* EOF? */
  334.         return(0);
  335.     if (buf[0] == 'n' || buf[0] == 'N') {
  336.         fclose(in);
  337.         return(0);
  338.     }
  339.     if (buf[0] == 'y' || buf[0] == 'Y') {
  340.         fclose(in);
  341.         return(1);
  342.     }
  343.     fprintf(stderr,"please answer y/n.\n");
  344.     }
  345. }
  346. #endif /* MDEBUG */
  347.