memdebug.c 7.4 KB

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