memdebug.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, 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. #include <stdarg.h>
  36. #ifdef HAVE_UNISTD_H
  37. #include <unistd.h>
  38. #endif
  39. #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
  40. #include "curl_memory.h"
  41. #include "memdebug.h"
  42. #ifndef HAVE_ASSERT_H
  43. # define assert(x) do { } while (0)
  44. #endif
  45. struct memdebug {
  46. size_t size;
  47. union {
  48. double d;
  49. void * p;
  50. } mem[1];
  51. /* I'm hoping this is the thing with the strictest alignment
  52. * requirements. That also means we waste some space :-( */
  53. };
  54. /*
  55. * Note that these debug functions are very simple and they are meant to
  56. * remain so. For advanced analysis, record a log file and write perl scripts
  57. * to analyze them!
  58. *
  59. * Don't use these with multithreaded test programs!
  60. */
  61. #define logfile curl_debuglogfile
  62. FILE *curl_debuglogfile = NULL;
  63. static bool memlimit = FALSE; /* enable memory limit */
  64. static long memsize = 0; /* set number of mallocs allowed */
  65. /* this sets the log file name */
  66. void curl_memdebug(const char *logname)
  67. {
  68. if(!logfile) {
  69. if(logname)
  70. logfile = fopen(logname, "w");
  71. else
  72. logfile = stderr;
  73. #ifdef MEMDEBUG_LOG_SYNC
  74. /* Flush the log file after every line so the log isn't lost in a crash */
  75. setvbuf(logfile, (char *)NULL, _IOLBF, 0);
  76. #endif
  77. }
  78. }
  79. /* This function sets the number of malloc() calls that should return
  80. successfully! */
  81. void curl_memlimit(long limit)
  82. {
  83. if(!memlimit) {
  84. memlimit = TRUE;
  85. memsize = limit;
  86. }
  87. }
  88. /* returns TRUE if this isn't allowed! */
  89. static bool countcheck(const char *func, int line, const char *source)
  90. {
  91. /* if source is NULL, then the call is made internally and this check
  92. should not be made */
  93. if(memlimit && source) {
  94. if(!memsize) {
  95. if(source) {
  96. /* log to file */
  97. curl_memlog("LIMIT %s:%d %s reached memlimit\n",
  98. source, line, func);
  99. /* log to stderr also */
  100. fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
  101. source, line, func);
  102. }
  103. SET_ERRNO(ENOMEM);
  104. return TRUE; /* RETURN ERROR! */
  105. }
  106. else
  107. memsize--; /* countdown */
  108. /* log the countdown */
  109. if(source)
  110. curl_memlog("LIMIT %s:%d %ld ALLOCS left\n",
  111. source, line, memsize);
  112. }
  113. return FALSE; /* allow this */
  114. }
  115. void *curl_domalloc(size_t wantedsize, int line, const char *source)
  116. {
  117. struct memdebug *mem;
  118. size_t size;
  119. assert(wantedsize != 0);
  120. if(countcheck("malloc", line, source))
  121. return NULL;
  122. /* alloc at least 64 bytes */
  123. size = sizeof(struct memdebug)+wantedsize;
  124. mem = (Curl_cmalloc)(size);
  125. if(mem) {
  126. /* fill memory with junk */
  127. memset(mem->mem, 0xA5, wantedsize);
  128. mem->size = wantedsize;
  129. }
  130. if(source)
  131. curl_memlog("MEM %s:%d malloc(%zd) = %p\n",
  132. source, line, wantedsize, mem ? mem->mem : 0);
  133. return (mem ? mem->mem : NULL);
  134. }
  135. void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
  136. int line, const char *source)
  137. {
  138. struct memdebug *mem;
  139. size_t size, user_size;
  140. assert(wanted_elements != 0);
  141. assert(wanted_size != 0);
  142. if(countcheck("calloc", line, source))
  143. return NULL;
  144. /* alloc at least 64 bytes */
  145. user_size = wanted_size * wanted_elements;
  146. size = sizeof(struct memdebug) + user_size;
  147. mem = (Curl_cmalloc)(size);
  148. if(mem) {
  149. /* fill memory with zeroes */
  150. memset(mem->mem, 0, user_size);
  151. mem->size = user_size;
  152. }
  153. if(source)
  154. curl_memlog("MEM %s:%d calloc(%zu,%zu) = %p\n",
  155. source, line, wanted_elements, wanted_size, mem?mem->mem:0);
  156. return (mem ? mem->mem : NULL);
  157. }
  158. char *curl_dostrdup(const char *str, int line, const char *source)
  159. {
  160. char *mem;
  161. size_t len;
  162. assert(str != NULL);
  163. if(countcheck("strdup", line, source))
  164. return NULL;
  165. len=strlen(str)+1;
  166. mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
  167. if(mem)
  168. memcpy(mem, str, len);
  169. if(source)
  170. curl_memlog("MEM %s:%d strdup(%p) (%zu) = %p\n",
  171. source, line, str, len, mem);
  172. return mem;
  173. }
  174. /* We provide a realloc() that accepts a NULL as pointer, which then
  175. performs a malloc(). In order to work with ares. */
  176. void *curl_dorealloc(void *ptr, size_t wantedsize,
  177. int line, const char *source)
  178. {
  179. struct memdebug *mem=NULL;
  180. size_t size = sizeof(struct memdebug)+wantedsize;
  181. assert(wantedsize != 0);
  182. if(countcheck("realloc", line, source))
  183. return NULL;
  184. if(ptr)
  185. mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
  186. mem = (Curl_crealloc)(mem, size);
  187. if(source)
  188. curl_memlog("MEM %s:%d realloc(%p, %zu) = %p\n",
  189. source, line, ptr, wantedsize, mem?mem->mem:NULL);
  190. if(mem) {
  191. mem->size = wantedsize;
  192. return mem->mem;
  193. }
  194. return NULL;
  195. }
  196. void curl_dofree(void *ptr, int line, const char *source)
  197. {
  198. struct memdebug *mem;
  199. assert(ptr != NULL);
  200. mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
  201. /* destroy */
  202. memset(mem->mem, 0x13, mem->size);
  203. /* free for real */
  204. (Curl_cfree)(mem);
  205. if(source)
  206. curl_memlog("MEM %s:%d free(%p)\n", source, line, ptr);
  207. }
  208. int curl_socket(int domain, int type, int protocol, int line,
  209. const char *source)
  210. {
  211. int sockfd=socket(domain, type, protocol);
  212. if(source && (sockfd!=-1))
  213. curl_memlog("FD %s:%d socket() = %d\n",
  214. source, line, sockfd);
  215. return sockfd;
  216. }
  217. int curl_accept(int s, void *saddr, void *saddrlen,
  218. int line, const char *source)
  219. {
  220. struct sockaddr *addr = (struct sockaddr *)saddr;
  221. curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
  222. int sockfd=accept(s, addr, addrlen);
  223. if(source)
  224. curl_memlog("FD %s:%d accept() = %d\n",
  225. source, line, sockfd);
  226. return sockfd;
  227. }
  228. /* separate function to allow libcurl to mark a "faked" close */
  229. void curl_mark_sclose(int sockfd, int line, const char *source)
  230. {
  231. if(source)
  232. curl_memlog("FD %s:%d sclose(%d)\n",
  233. source, line, sockfd);
  234. }
  235. /* this is our own defined way to close sockets on *ALL* platforms */
  236. int curl_sclose(int sockfd, int line, const char *source)
  237. {
  238. int res=sclose(sockfd);
  239. curl_mark_sclose(sockfd, line, source);
  240. return res;
  241. }
  242. FILE *curl_fopen(const char *file, const char *mode,
  243. int line, const char *source)
  244. {
  245. FILE *res=fopen(file, mode);
  246. if(source)
  247. curl_memlog("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
  248. source, line, file, mode, res);
  249. return res;
  250. }
  251. #ifdef HAVE_FDOPEN
  252. FILE *curl_fdopen(int filedes, const char *mode,
  253. int line, const char *source)
  254. {
  255. FILE *res=fdopen(filedes, mode);
  256. if(source)
  257. curl_memlog("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
  258. source, line, filedes, mode, res);
  259. return res;
  260. }
  261. #endif
  262. int curl_fclose(FILE *file, int line, const char *source)
  263. {
  264. int res;
  265. assert(file != NULL);
  266. res=fclose(file);
  267. if(source)
  268. curl_memlog("FILE %s:%d fclose(%p)\n",
  269. source, line, file);
  270. return res;
  271. }
  272. #define LOGLINE_BUFSIZE 1024
  273. /* this does the writting to the memory tracking log file */
  274. void curl_memlog(const char *format, ...)
  275. {
  276. char *buf;
  277. int nchars;
  278. va_list ap;
  279. if(!logfile)
  280. return;
  281. buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
  282. if(!buf)
  283. return;
  284. va_start(ap, format);
  285. nchars = vsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
  286. va_end(ap);
  287. if(nchars > LOGLINE_BUFSIZE - 1)
  288. nchars = LOGLINE_BUFSIZE - 1;
  289. if(nchars > 0)
  290. fwrite(buf, 1, nchars, logfile);
  291. (Curl_cfree)(buf);
  292. }
  293. #endif /* CURLDEBUG */