memdebug.c 7.8 KB

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