memdebug.c 9.8 KB

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