memdebug.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #ifdef CURLDEBUG
  2. /***************************************************************************
  3. * _ _ ____ _
  4. * Project ___| | | | _ \| |
  5. * / __| | | | |_) | |
  6. * | (__| |_| | _ <| |___
  7. * \___|\___/|_| \_\_____|
  8. *
  9. * Copyright (C) 1998 - 2005, Daniel Stenberg, <[email protected]>, 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 "memory.h"
  40. #include "memdebug.h"
  41. struct memdebug {
  42. size_t size;
  43. double mem[1];
  44. /* I'm hoping this is the thing with the strictest alignment
  45. * requirements. That also means we waste some space :-( */
  46. };
  47. /*
  48. * Note that these debug functions are very simple and they are meant to
  49. * remain so. For advanced analysis, record a log file and write perl scripts
  50. * to analyze them!
  51. *
  52. * Don't use these with multithreaded test programs!
  53. */
  54. #define logfile curl_debuglogfile
  55. FILE *curl_debuglogfile = NULL;
  56. static bool memlimit = FALSE; /* enable memory limit */
  57. static long memsize = 0; /* set number of mallocs allowed */
  58. /* this sets the log file name */
  59. void curl_memdebug(const char *logname)
  60. {
  61. if (!logfile) {
  62. if(logname)
  63. logfile = fopen(logname, "w");
  64. else
  65. logfile = stderr;
  66. }
  67. }
  68. /* This function sets the number of malloc() calls that should return
  69. successfully! */
  70. void curl_memlimit(long limit)
  71. {
  72. if (!memlimit) {
  73. memlimit = TRUE;
  74. memsize = limit;
  75. }
  76. }
  77. /* returns TRUE if this isn't allowed! */
  78. static bool countcheck(const char *func, int line, const char *source)
  79. {
  80. /* if source is NULL, then the call is made internally and this check
  81. should not be made */
  82. if(memlimit && source) {
  83. if(!memsize) {
  84. if(logfile && source)
  85. fprintf(logfile, "LIMIT %s:%d %s reached memlimit\n",
  86. source, line, func);
  87. if(source)
  88. fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
  89. source, line, func);
  90. errno = ENOMEM;
  91. return TRUE; /* RETURN ERROR! */
  92. }
  93. else
  94. memsize--; /* countdown */
  95. /* log the countdown */
  96. if(logfile && source)
  97. fprintf(logfile, "LIMIT %s:%d %ld ALLOCS left\n",
  98. source, line, memsize);
  99. }
  100. return FALSE; /* allow this */
  101. }
  102. void *curl_domalloc(size_t wantedsize, int line, const char *source)
  103. {
  104. struct memdebug *mem;
  105. size_t size;
  106. if(countcheck("malloc", line, source))
  107. return NULL;
  108. /* alloc at least 64 bytes */
  109. size = sizeof(struct memdebug)+wantedsize;
  110. mem=(struct memdebug *)(Curl_cmalloc)(size);
  111. if(mem) {
  112. /* fill memory with junk */
  113. memset(mem->mem, 0xA5, wantedsize);
  114. mem->size = wantedsize;
  115. }
  116. if(logfile && source)
  117. fprintf(logfile, "MEM %s:%d malloc(%zd) = %p\n",
  118. source, line, wantedsize, mem ? mem->mem : 0);
  119. return (mem ? mem->mem : NULL);
  120. }
  121. void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
  122. int line, const char *source)
  123. {
  124. struct memdebug *mem;
  125. size_t size, user_size;
  126. if(countcheck("calloc", line, source))
  127. return NULL;
  128. /* alloc at least 64 bytes */
  129. user_size = wanted_size * wanted_elements;
  130. size = sizeof(struct memdebug) + user_size;
  131. mem = (struct memdebug *)(Curl_cmalloc)(size);
  132. if(mem) {
  133. /* fill memory with zeroes */
  134. memset(mem->mem, 0, user_size);
  135. mem->size = user_size;
  136. }
  137. if(logfile && source)
  138. fprintf(logfile, "MEM %s:%d calloc(%u,%u) = %p\n",
  139. source, line, wanted_elements, wanted_size, mem ? mem->mem : 0);
  140. return (mem ? mem->mem : NULL);
  141. }
  142. char *curl_dostrdup(const char *str, int line, const char *source)
  143. {
  144. char *mem;
  145. size_t len;
  146. curlassert(str != NULL);
  147. if(countcheck("strdup", line, source))
  148. return NULL;
  149. len=strlen(str)+1;
  150. mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
  151. if (mem)
  152. memcpy(mem, str, len);
  153. if(logfile)
  154. fprintf(logfile, "MEM %s:%d strdup(%p) (%zd) = %p\n",
  155. source, line, str, len, mem);
  156. return mem;
  157. }
  158. /* We provide a realloc() that accepts a NULL as pointer, which then
  159. performs a malloc(). In order to work with ares. */
  160. void *curl_dorealloc(void *ptr, size_t wantedsize,
  161. int line, const char *source)
  162. {
  163. struct memdebug *mem=NULL;
  164. size_t size = sizeof(struct memdebug)+wantedsize;
  165. if(countcheck("realloc", line, source))
  166. return NULL;
  167. if(ptr)
  168. mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
  169. mem=(struct memdebug *)(Curl_crealloc)(mem, size);
  170. if(logfile)
  171. fprintf(logfile, "MEM %s:%d realloc(%p, %zd) = %p\n",
  172. source, line, ptr, wantedsize, mem?mem->mem:NULL);
  173. if(mem) {
  174. mem->size = wantedsize;
  175. return mem->mem;
  176. }
  177. return NULL;
  178. }
  179. void curl_dofree(void *ptr, int line, const char *source)
  180. {
  181. struct memdebug *mem;
  182. curlassert(ptr != NULL);
  183. mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
  184. /* destroy */
  185. memset(mem->mem, 0x13, mem->size);
  186. /* free for real */
  187. (Curl_cfree)(mem);
  188. if(logfile)
  189. fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
  190. }
  191. int curl_socket(int domain, int type, int protocol, int line,
  192. const char *source)
  193. {
  194. int sockfd=(socket)(domain, type, protocol);
  195. if(logfile && (sockfd!=-1))
  196. fprintf(logfile, "FD %s:%d socket() = %d\n",
  197. source, line, sockfd);
  198. return sockfd;
  199. }
  200. int curl_accept(int s, void *saddr, void *saddrlen,
  201. int line, const char *source)
  202. {
  203. struct sockaddr *addr = (struct sockaddr *)saddr;
  204. socklen_t *addrlen = (socklen_t *)saddrlen;
  205. int sockfd=(accept)(s, addr, addrlen);
  206. if(logfile)
  207. fprintf(logfile, "FD %s:%d accept() = %d\n",
  208. source, line, sockfd);
  209. return sockfd;
  210. }
  211. /* this is our own defined way to close sockets on *ALL* platforms */
  212. int curl_sclose(int sockfd, int line, const char *source)
  213. {
  214. int res=sclose(sockfd);
  215. if(logfile)
  216. fprintf(logfile, "FD %s:%d sclose(%d)\n",
  217. source, line, sockfd);
  218. return res;
  219. }
  220. FILE *curl_fopen(const char *file, const char *mode,
  221. int line, const char *source)
  222. {
  223. FILE *res=(fopen)(file, mode);
  224. if(logfile)
  225. fprintf(logfile, "FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
  226. source, line, file, mode, res);
  227. return res;
  228. }
  229. int curl_fclose(FILE *file, int line, const char *source)
  230. {
  231. int res;
  232. curlassert(file != NULL);
  233. res=(fclose)(file);
  234. if(logfile)
  235. fprintf(logfile, "FILE %s:%d fclose(%p)\n",
  236. source, line, file);
  237. return res;
  238. }
  239. #else
  240. #ifdef VMS
  241. int VOID_VAR_MEMDEBUG;
  242. #else
  243. /* we provide a fake do-nothing function here to avoid compiler warnings */
  244. void curl_memdebug(void) {}
  245. #endif /* VMS */
  246. #endif /* CURLDEBUG */