memdebug.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #ifdef CURLDEBUG
  2. /***************************************************************************
  3. * _ _ ____ _
  4. * Project ___| | | | _ \| |
  5. * / __| | | | |_) | |
  6. * | (__| |_| | _ <| |___
  7. * \___|\___/|_| \_\_____|
  8. *
  9. * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
  10. *
  11. * This software is licensed as described in the file COPYING, which
  12. * you should have received as part of this distribution. The terms
  13. * are also available at http://curl.haxx.se/docs/copyright.html.
  14. *
  15. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. * copies of the Software, and permit persons to whom the Software is
  17. * furnished to do so, under the terms of the COPYING file.
  18. *
  19. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. * KIND, either express or implied.
  21. *
  22. * $Id$
  23. ***************************************************************************/
  24. #include "setup.h"
  25. #include <curl/curl.h>
  26. #ifdef HAVE_SYS_SOCKET_H
  27. #include <sys/socket.h>
  28. #endif
  29. #define _MPRINTF_REPLACE
  30. #include <curl/mprintf.h>
  31. #include "urldata.h"
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #ifdef HAVE_UNISTD_H
  36. #include <unistd.h>
  37. #endif
  38. #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
  39. #include "memdebug.h"
  40. struct memdebug {
  41. size_t size;
  42. double mem[1];
  43. /* I'm hoping this is the thing with the strictest alignment
  44. * requirements. That also means we waste some space :-( */
  45. };
  46. /*
  47. * Note that these debug functions are very simple and they are meant to
  48. * remain so. For advanced analysis, record a log file and write perl scripts
  49. * to analyze them!
  50. *
  51. * Don't use these with multithreaded test programs!
  52. */
  53. #define logfile curl_debuglogfile
  54. FILE *curl_debuglogfile;
  55. static bool memlimit; /* enable memory limit */
  56. static long memsize; /* set number of mallocs allowed */
  57. /* this sets the log file name */
  58. void curl_memdebug(const char *logname)
  59. {
  60. if(logname)
  61. logfile = fopen(logname, "w");
  62. else
  63. logfile = stderr;
  64. }
  65. /* This function sets the number of malloc() calls that should return
  66. successfully! */
  67. void curl_memlimit(long limit)
  68. {
  69. memlimit = TRUE;
  70. memsize = limit;
  71. }
  72. /* returns TRUE if this isn't allowed! */
  73. static bool countcheck(const char *func, int line, const char *source)
  74. {
  75. /* if source is NULL, then the call is made internally and this check
  76. should not be made */
  77. if(memlimit && source) {
  78. if(!memsize) {
  79. if(logfile && source)
  80. fprintf(logfile, "LIMIT %s:%d %s reached memlimit\n",
  81. source, line, func);
  82. return TRUE; /* RETURN ERROR! */
  83. }
  84. else
  85. memsize--; /* countdown */
  86. /* log the countdown */
  87. if(logfile && source)
  88. fprintf(logfile, "LIMIT %s:%d %ld ALLOCS left\n",
  89. source, line, memsize);
  90. }
  91. return FALSE; /* allow this */
  92. }
  93. void *curl_domalloc(size_t wantedsize, int line, const char *source)
  94. {
  95. struct memdebug *mem;
  96. size_t size;
  97. if(countcheck("malloc", line, source))
  98. return NULL;
  99. /* alloc at least 64 bytes */
  100. size = sizeof(struct memdebug)+wantedsize;
  101. mem=(struct memdebug *)(malloc)(size);
  102. if(mem) {
  103. /* fill memory with junk */
  104. memset(mem->mem, 0xA5, wantedsize);
  105. mem->size = wantedsize;
  106. }
  107. if(logfile && source)
  108. fprintf(logfile, "MEM %s:%d malloc(%zd) = %p\n",
  109. source, line, wantedsize, mem->mem);
  110. return mem->mem;
  111. }
  112. void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
  113. int line, const char *source)
  114. {
  115. struct memdebug *mem;
  116. size_t size, user_size;
  117. if(countcheck("calloc", line, source))
  118. return NULL;
  119. /* alloc at least 64 bytes */
  120. user_size = wanted_size * wanted_elements;
  121. size = sizeof(struct memdebug) + user_size;
  122. mem = (struct memdebug *)(malloc)(size);
  123. if(mem) {
  124. /* fill memory with zeroes */
  125. memset(mem->mem, 0, user_size);
  126. mem->size = user_size;
  127. }
  128. if(logfile && source)
  129. fprintf(logfile, "MEM %s:%d calloc(%u,%u) = %p\n",
  130. source, line, wanted_elements, wanted_size, mem->mem);
  131. return mem->mem;
  132. }
  133. char *curl_dostrdup(const char *str, int line, const char *source)
  134. {
  135. char *mem;
  136. size_t len;
  137. curlassert(str != NULL);
  138. if(countcheck("strdup", line, source))
  139. return NULL;
  140. len=strlen(str)+1;
  141. mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
  142. memcpy(mem, str, len);
  143. if(logfile)
  144. fprintf(logfile, "MEM %s:%d strdup(%p) (%zd) = %p\n",
  145. source, line, str, len, mem);
  146. return mem;
  147. }
  148. /* We provide a realloc() that accepts a NULL as pointer, which then
  149. performs a malloc(). In order to work with ares. */
  150. void *curl_dorealloc(void *ptr, size_t wantedsize,
  151. int line, const char *source)
  152. {
  153. struct memdebug *mem=NULL;
  154. size_t size = sizeof(struct memdebug)+wantedsize;
  155. if(countcheck("realloc", line, source))
  156. return NULL;
  157. if(ptr)
  158. mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
  159. mem=(struct memdebug *)(realloc)(mem, size);
  160. if(logfile)
  161. fprintf(logfile, "MEM %s:%d realloc(0x%x, %zd) = %p\n",
  162. source, line, ptr, wantedsize, mem?mem->mem:NULL);
  163. if(mem) {
  164. mem->size = wantedsize;
  165. return mem->mem;
  166. }
  167. return NULL;
  168. }
  169. void curl_dofree(void *ptr, int line, const char *source)
  170. {
  171. struct memdebug *mem;
  172. curlassert(ptr != NULL);
  173. mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
  174. /* destroy */
  175. memset(mem->mem, 0x13, mem->size);
  176. /* free for real */
  177. (free)(mem);
  178. if(logfile)
  179. fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
  180. }
  181. int curl_socket(int domain, int type, int protocol, int line,
  182. const char *source)
  183. {
  184. int sockfd=(socket)(domain, type, protocol);
  185. if(logfile && (sockfd!=-1))
  186. fprintf(logfile, "FD %s:%d socket() = %d\n",
  187. source, line, sockfd);
  188. return sockfd;
  189. }
  190. int curl_accept(int s, void *saddr, void *saddrlen,
  191. int line, const char *source)
  192. {
  193. struct sockaddr *addr = (struct sockaddr *)saddr;
  194. socklen_t *addrlen = (socklen_t *)saddrlen;
  195. int sockfd=(accept)(s, addr, addrlen);
  196. if(logfile)
  197. fprintf(logfile, "FD %s:%d accept() = %d\n",
  198. source, line, sockfd);
  199. return sockfd;
  200. }
  201. /* this is our own defined way to close sockets on *ALL* platforms */
  202. int curl_sclose(int sockfd, int line, const char *source)
  203. {
  204. int res=sclose(sockfd);
  205. if(logfile)
  206. fprintf(logfile, "FD %s:%d sclose(%d)\n",
  207. source, line, sockfd);
  208. return res;
  209. }
  210. FILE *curl_fopen(const char *file, const char *mode,
  211. int line, const char *source)
  212. {
  213. FILE *res=(fopen)(file, mode);
  214. if(logfile)
  215. fprintf(logfile, "FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
  216. source, line, file, mode, res);
  217. return res;
  218. }
  219. int curl_fclose(FILE *file, int line, const char *source)
  220. {
  221. int res;
  222. curlassert(file != NULL);
  223. res=(fclose)(file);
  224. if(logfile)
  225. fprintf(logfile, "FILE %s:%d fclose(%p)\n",
  226. source, line, file);
  227. return res;
  228. }
  229. #else
  230. #ifdef VMS
  231. int VOID_VAR_MEMDEBUG;
  232. #else
  233. /* we provide a fake do-nothing function here to avoid compiler warnings */
  234. void curl_memdebug(void) {}
  235. #endif /* VMS */
  236. #endif /* CURLDEBUG */