memdebug.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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 https://curl.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 "curl_setup.h"
  23. #ifdef CURLDEBUG
  24. #include <curl/curl.h>
  25. #include "urldata.h"
  26. #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
  27. /* The last 3 #include files should be in this order */
  28. #include "curl_printf.h"
  29. #include "curl_memory.h"
  30. #include "memdebug.h"
  31. struct memdebug {
  32. size_t size;
  33. union {
  34. curl_off_t o;
  35. double d;
  36. void *p;
  37. } mem[1];
  38. /* I'm hoping this is the thing with the strictest alignment
  39. * requirements. That also means we waste some space :-( */
  40. };
  41. /*
  42. * Note that these debug functions are very simple and they are meant to
  43. * remain so. For advanced analysis, record a log file and write perl scripts
  44. * to analyze them!
  45. *
  46. * Don't use these with multithreaded test programs!
  47. */
  48. FILE *curl_dbg_logfile = NULL;
  49. static bool memlimit = FALSE; /* enable memory limit */
  50. static long memsize = 0; /* set number of mallocs allowed */
  51. /* this sets the log file name */
  52. void curl_dbg_memdebug(const char *logname)
  53. {
  54. if(!curl_dbg_logfile) {
  55. if(logname && *logname)
  56. curl_dbg_logfile = fopen(logname, FOPEN_WRITETEXT);
  57. else
  58. curl_dbg_logfile = stderr;
  59. #ifdef MEMDEBUG_LOG_SYNC
  60. /* Flush the log file after every line so the log isn't lost in a crash */
  61. if(curl_dbg_logfile)
  62. setbuf(curl_dbg_logfile, (char *)NULL);
  63. #endif
  64. }
  65. }
  66. /* This function sets the number of malloc() calls that should return
  67. successfully! */
  68. void curl_dbg_memlimit(long limit)
  69. {
  70. if(!memlimit) {
  71. memlimit = TRUE;
  72. memsize = limit;
  73. }
  74. }
  75. /* returns TRUE if this isn't allowed! */
  76. static bool countcheck(const char *func, int line, const char *source)
  77. {
  78. /* if source is NULL, then the call is made internally and this check
  79. should not be made */
  80. if(memlimit && source) {
  81. if(!memsize) {
  82. if(source) {
  83. /* log to file */
  84. curl_dbg_log("LIMIT %s:%d %s reached memlimit\n",
  85. source, line, func);
  86. /* log to stderr also */
  87. fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
  88. source, line, func);
  89. fflush(curl_dbg_logfile); /* because it might crash now */
  90. }
  91. errno = ENOMEM;
  92. return TRUE; /* RETURN ERROR! */
  93. }
  94. else
  95. memsize--; /* countdown */
  96. }
  97. return FALSE; /* allow this */
  98. }
  99. void *curl_dbg_malloc(size_t wantedsize, int line, const char *source)
  100. {
  101. struct memdebug *mem;
  102. size_t size;
  103. DEBUGASSERT(wantedsize != 0);
  104. if(countcheck("malloc", line, source))
  105. return NULL;
  106. /* alloc at least 64 bytes */
  107. size = sizeof(struct memdebug) + wantedsize;
  108. mem = (Curl_cmalloc)(size);
  109. if(mem) {
  110. mem->size = wantedsize;
  111. }
  112. if(source)
  113. curl_dbg_log("MEM %s:%d malloc(%zu) = %p\n",
  114. source, line, wantedsize,
  115. mem ? (void *)mem->mem : (void *)0);
  116. return (mem ? mem->mem : NULL);
  117. }
  118. void *curl_dbg_calloc(size_t wanted_elements, size_t wanted_size,
  119. int line, const char *source)
  120. {
  121. struct memdebug *mem;
  122. size_t size, user_size;
  123. DEBUGASSERT(wanted_elements != 0);
  124. DEBUGASSERT(wanted_size != 0);
  125. if(countcheck("calloc", line, source))
  126. return NULL;
  127. /* alloc at least 64 bytes */
  128. user_size = wanted_size * wanted_elements;
  129. size = sizeof(struct memdebug) + user_size;
  130. mem = (Curl_ccalloc)(1, size);
  131. if(mem)
  132. mem->size = user_size;
  133. if(source)
  134. curl_dbg_log("MEM %s:%d calloc(%zu,%zu) = %p\n",
  135. source, line, wanted_elements, wanted_size,
  136. mem ? (void *)mem->mem : (void *)0);
  137. return (mem ? mem->mem : NULL);
  138. }
  139. char *curl_dbg_strdup(const char *str, int line, const char *source)
  140. {
  141. char *mem;
  142. size_t len;
  143. DEBUGASSERT(str != NULL);
  144. if(countcheck("strdup", line, source))
  145. return NULL;
  146. len = strlen(str) + 1;
  147. mem = curl_dbg_malloc(len, 0, NULL); /* NULL prevents logging */
  148. if(mem)
  149. memcpy(mem, str, len);
  150. if(source)
  151. curl_dbg_log("MEM %s:%d strdup(%p) (%zu) = %p\n",
  152. source, line, (const void *)str, len, (const void *)mem);
  153. return mem;
  154. }
  155. #if defined(WIN32) && defined(UNICODE)
  156. wchar_t *curl_dbg_wcsdup(const wchar_t *str, int line, const char *source)
  157. {
  158. wchar_t *mem;
  159. size_t wsiz, bsiz;
  160. DEBUGASSERT(str != NULL);
  161. if(countcheck("wcsdup", line, source))
  162. return NULL;
  163. wsiz = wcslen(str) + 1;
  164. bsiz = wsiz * sizeof(wchar_t);
  165. mem = curl_dbg_malloc(bsiz, 0, NULL); /* NULL prevents logging */
  166. if(mem)
  167. memcpy(mem, str, bsiz);
  168. if(source)
  169. curl_dbg_log("MEM %s:%d wcsdup(%p) (%zu) = %p\n",
  170. source, line, (void *)str, bsiz, (void *)mem);
  171. return mem;
  172. }
  173. #endif
  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_dbg_realloc(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. DEBUGASSERT(wantedsize != 0);
  182. if(countcheck("realloc", line, source))
  183. return NULL;
  184. #ifdef __INTEL_COMPILER
  185. # pragma warning(push)
  186. # pragma warning(disable:1684)
  187. /* 1684: conversion from pointer to same-sized integral type */
  188. #endif
  189. if(ptr)
  190. mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
  191. #ifdef __INTEL_COMPILER
  192. # pragma warning(pop)
  193. #endif
  194. mem = (Curl_crealloc)(mem, size);
  195. if(source)
  196. curl_dbg_log("MEM %s:%d realloc(%p, %zu) = %p\n",
  197. source, line, (void *)ptr, wantedsize,
  198. mem ? (void *)mem->mem : (void *)0);
  199. if(mem) {
  200. mem->size = wantedsize;
  201. return mem->mem;
  202. }
  203. return NULL;
  204. }
  205. void curl_dbg_free(void *ptr, int line, const char *source)
  206. {
  207. if(ptr) {
  208. struct memdebug *mem;
  209. #ifdef __INTEL_COMPILER
  210. # pragma warning(push)
  211. # pragma warning(disable:1684)
  212. /* 1684: conversion from pointer to same-sized integral type */
  213. #endif
  214. mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
  215. #ifdef __INTEL_COMPILER
  216. # pragma warning(pop)
  217. #endif
  218. /* free for real */
  219. (Curl_cfree)(mem);
  220. }
  221. if(source && ptr)
  222. curl_dbg_log("MEM %s:%d free(%p)\n", source, line, (void *)ptr);
  223. }
  224. curl_socket_t curl_dbg_socket(int domain, int type, int protocol,
  225. int line, const char *source)
  226. {
  227. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  228. "FD %s:%d socket() = %d\n" :
  229. (sizeof(curl_socket_t) == sizeof(long)) ?
  230. "FD %s:%d socket() = %ld\n" :
  231. "FD %s:%d socket() = %zd\n";
  232. curl_socket_t sockfd;
  233. if(countcheck("socket", line, source))
  234. return CURL_SOCKET_BAD;
  235. sockfd = socket(domain, type, protocol);
  236. if(source && (sockfd != CURL_SOCKET_BAD))
  237. curl_dbg_log(fmt, source, line, sockfd);
  238. return sockfd;
  239. }
  240. SEND_TYPE_RETV curl_dbg_send(SEND_TYPE_ARG1 sockfd,
  241. SEND_QUAL_ARG2 SEND_TYPE_ARG2 buf,
  242. SEND_TYPE_ARG3 len, SEND_TYPE_ARG4 flags, int line,
  243. const char *source)
  244. {
  245. SEND_TYPE_RETV rc;
  246. if(countcheck("send", line, source))
  247. return -1;
  248. rc = send(sockfd, buf, len, flags);
  249. if(source)
  250. curl_dbg_log("SEND %s:%d send(%lu) = %ld\n",
  251. source, line, (unsigned long)len, (long)rc);
  252. return rc;
  253. }
  254. RECV_TYPE_RETV curl_dbg_recv(RECV_TYPE_ARG1 sockfd, RECV_TYPE_ARG2 buf,
  255. RECV_TYPE_ARG3 len, RECV_TYPE_ARG4 flags, int line,
  256. const char *source)
  257. {
  258. RECV_TYPE_RETV rc;
  259. if(countcheck("recv", line, source))
  260. return -1;
  261. rc = recv(sockfd, buf, len, flags);
  262. if(source)
  263. curl_dbg_log("RECV %s:%d recv(%lu) = %ld\n",
  264. source, line, (unsigned long)len, (long)rc);
  265. return rc;
  266. }
  267. #ifdef HAVE_SOCKETPAIR
  268. int curl_dbg_socketpair(int domain, int type, int protocol,
  269. curl_socket_t socket_vector[2],
  270. int line, const char *source)
  271. {
  272. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  273. "FD %s:%d socketpair() = %d %d\n" :
  274. (sizeof(curl_socket_t) == sizeof(long)) ?
  275. "FD %s:%d socketpair() = %ld %ld\n" :
  276. "FD %s:%d socketpair() = %zd %zd\n";
  277. int res = socketpair(domain, type, protocol, socket_vector);
  278. if(source && (0 == res))
  279. curl_dbg_log(fmt, source, line, socket_vector[0], socket_vector[1]);
  280. return res;
  281. }
  282. #endif
  283. curl_socket_t curl_dbg_accept(curl_socket_t s, void *saddr, void *saddrlen,
  284. int line, const char *source)
  285. {
  286. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  287. "FD %s:%d accept() = %d\n" :
  288. (sizeof(curl_socket_t) == sizeof(long)) ?
  289. "FD %s:%d accept() = %ld\n" :
  290. "FD %s:%d accept() = %zd\n";
  291. struct sockaddr *addr = (struct sockaddr *)saddr;
  292. curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
  293. curl_socket_t sockfd = accept(s, addr, addrlen);
  294. if(source && (sockfd != CURL_SOCKET_BAD))
  295. curl_dbg_log(fmt, source, line, sockfd);
  296. return sockfd;
  297. }
  298. /* separate function to allow libcurl to mark a "faked" close */
  299. void curl_dbg_mark_sclose(curl_socket_t sockfd, int line, const char *source)
  300. {
  301. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  302. "FD %s:%d sclose(%d)\n":
  303. (sizeof(curl_socket_t) == sizeof(long)) ?
  304. "FD %s:%d sclose(%ld)\n":
  305. "FD %s:%d sclose(%zd)\n";
  306. if(source)
  307. curl_dbg_log(fmt, source, line, sockfd);
  308. }
  309. /* this is our own defined way to close sockets on *ALL* platforms */
  310. int curl_dbg_sclose(curl_socket_t sockfd, int line, const char *source)
  311. {
  312. int res = sclose(sockfd);
  313. curl_dbg_mark_sclose(sockfd, line, source);
  314. return res;
  315. }
  316. FILE *curl_dbg_fopen(const char *file, const char *mode,
  317. int line, const char *source)
  318. {
  319. FILE *res = fopen(file, mode);
  320. if(source)
  321. curl_dbg_log("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
  322. source, line, file, mode, (void *)res);
  323. return res;
  324. }
  325. FILE *curl_dbg_fdopen(int filedes, const char *mode,
  326. int line, const char *source)
  327. {
  328. FILE *res = fdopen(filedes, mode);
  329. if(source)
  330. curl_dbg_log("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
  331. source, line, filedes, mode, (void *)res);
  332. return res;
  333. }
  334. int curl_dbg_fclose(FILE *file, int line, const char *source)
  335. {
  336. int res;
  337. DEBUGASSERT(file != NULL);
  338. if(source)
  339. curl_dbg_log("FILE %s:%d fclose(%p)\n",
  340. source, line, (void *)file);
  341. res = fclose(file);
  342. return res;
  343. }
  344. #define LOGLINE_BUFSIZE 1024
  345. /* this does the writing to the memory tracking log file */
  346. void curl_dbg_log(const char *format, ...)
  347. {
  348. char *buf;
  349. int nchars;
  350. va_list ap;
  351. if(!curl_dbg_logfile)
  352. return;
  353. buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
  354. if(!buf)
  355. return;
  356. va_start(ap, format);
  357. nchars = mvsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
  358. va_end(ap);
  359. if(nchars > LOGLINE_BUFSIZE - 1)
  360. nchars = LOGLINE_BUFSIZE - 1;
  361. if(nchars > 0)
  362. fwrite(buf, 1, (size_t)nchars, curl_dbg_logfile);
  363. (Curl_cfree)(buf);
  364. }
  365. #endif /* CURLDEBUG */