ckcmdb.c 8.5 KB

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