1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * SPDX-License-Identifier: curl
23 ***************************************************************************/
25 #include "curl_setup.h"
29 #include <curl/curl.h>
33 #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
35 /* The last 3 #include files should be in this order */
36 #include "curl_printf.h"
37 #include "curl_memory.h"
47 /* I'm hoping this is the thing with the strictest alignment
48 * requirements. That also means we waste some space :-( */
52 * Note that these debug functions are very simple and they are meant to
53 * remain so. For advanced analysis, record a log file and write perl scripts
56 * Don't use these with multithreaded test programs!
59 FILE *curl_dbg_logfile = NULL;
60 static bool registered_cleanup = FALSE; /* atexit registered cleanup */
61 static bool memlimit = FALSE; /* enable memory limit */
62 static long memsize = 0; /* set number of mallocs allowed */
64 /* LeakSantizier (LSAN) calls _exit() instead of exit() when a leak is detected
65 on exit so the logfile must be closed explicitly or data could be lost.
66 Though _exit() does not call atexit handlers such as this, LSAN's call to
67 _exit() comes after the atexit handlers are called. curl/curl#6620 */
68 static void curl_dbg_cleanup(void)
70 if(curl_dbg_logfile &&
71 curl_dbg_logfile != stderr &&
72 curl_dbg_logfile != stdout) {
73 fclose(curl_dbg_logfile);
75 curl_dbg_logfile = NULL;
78 /* this sets the log file name */
79 void curl_dbg_memdebug(const char *logname)
81 if(!curl_dbg_logfile) {
82 if(logname && *logname)
83 curl_dbg_logfile = fopen(logname, FOPEN_WRITETEXT);
85 curl_dbg_logfile = stderr;
86 #ifdef MEMDEBUG_LOG_SYNC
87 /* Flush the log file after every line so the log isn't lost in a crash */
89 setbuf(curl_dbg_logfile, (char *)NULL);
92 if(!registered_cleanup)
93 registered_cleanup = !atexit(curl_dbg_cleanup);
96 /* This function sets the number of malloc() calls that should return
98 void curl_dbg_memlimit(long limit)
106 /* returns TRUE if this isn't allowed! */
107 static bool countcheck(const char *func, int line, const char *source)
109 /* if source is NULL, then the call is made internally and this check
110 should not be made */
111 if(memlimit && source) {
114 curl_dbg_log("LIMIT %s:%d %s reached memlimit\n",
116 /* log to stderr also */
117 fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
119 fflush(curl_dbg_logfile); /* because it might crash now */
121 return TRUE; /* RETURN ERROR! */
124 memsize--; /* countdown */
129 return FALSE; /* allow this */
132 ALLOC_FUNC void *curl_dbg_malloc(size_t wantedsize,
133 int line, const char *source)
135 struct memdebug *mem;
138 DEBUGASSERT(wantedsize != 0);
140 if(countcheck("malloc", line, source))
143 /* alloc at least 64 bytes */
144 size = sizeof(struct memdebug) + wantedsize;
146 mem = (Curl_cmalloc)(size);
148 mem->size = wantedsize;
152 curl_dbg_log("MEM %s:%d malloc(%zu) = %p\n",
153 source, line, wantedsize,
154 mem ? (void *)mem->mem : (void *)0);
156 return (mem ? mem->mem : NULL);
159 ALLOC_FUNC void *curl_dbg_calloc(size_t wanted_elements, size_t wanted_size,
160 int line, const char *source)
162 struct memdebug *mem;
163 size_t size, user_size;
165 DEBUGASSERT(wanted_elements != 0);
166 DEBUGASSERT(wanted_size != 0);
168 if(countcheck("calloc", line, source))
171 /* alloc at least 64 bytes */
172 user_size = wanted_size * wanted_elements;
173 size = sizeof(struct memdebug) + user_size;
175 mem = (Curl_ccalloc)(1, size);
177 mem->size = user_size;
180 curl_dbg_log("MEM %s:%d calloc(%zu,%zu) = %p\n",
181 source, line, wanted_elements, wanted_size,
182 mem ? (void *)mem->mem : (void *)0);
184 return (mem ? mem->mem : NULL);
187 ALLOC_FUNC char *curl_dbg_strdup(const char *str,
188 int line, const char *source)
193 DEBUGASSERT(str != NULL);
195 if(countcheck("strdup", line, source))
198 len = strlen(str) + 1;
200 mem = curl_dbg_malloc(len, 0, NULL); /* NULL prevents logging */
202 memcpy(mem, str, len);
205 curl_dbg_log("MEM %s:%d strdup(%p) (%zu) = %p\n",
206 source, line, (const void *)str, len, (const void *)mem);
211 #if defined(WIN32) && defined(UNICODE)
212 ALLOC_FUNC wchar_t *curl_dbg_wcsdup(const wchar_t *str,
213 int line, const char *source)
218 DEBUGASSERT(str != NULL);
220 if(countcheck("wcsdup", line, source))
223 wsiz = wcslen(str) + 1;
224 bsiz = wsiz * sizeof(wchar_t);
226 mem = curl_dbg_malloc(bsiz, 0, NULL); /* NULL prevents logging */
228 memcpy(mem, str, bsiz);
231 curl_dbg_log("MEM %s:%d wcsdup(%p) (%zu) = %p\n",
232 source, line, (void *)str, bsiz, (void *)mem);
238 /* We provide a realloc() that accepts a NULL as pointer, which then
239 performs a malloc(). In order to work with ares. */
240 void *curl_dbg_realloc(void *ptr, size_t wantedsize,
241 int line, const char *source)
243 struct memdebug *mem = NULL;
245 size_t size = sizeof(struct memdebug) + wantedsize;
247 DEBUGASSERT(wantedsize != 0);
249 if(countcheck("realloc", line, source))
252 #ifdef __INTEL_COMPILER
253 # pragma warning(push)
254 # pragma warning(disable:1684)
255 /* 1684: conversion from pointer to same-sized integral type */
259 mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
261 #ifdef __INTEL_COMPILER
262 # pragma warning(pop)
265 mem = (Curl_crealloc)(mem, size);
267 curl_dbg_log("MEM %s:%d realloc(%p, %zu) = %p\n",
268 source, line, (void *)ptr, wantedsize,
269 mem ? (void *)mem->mem : (void *)0);
272 mem->size = wantedsize;
279 void curl_dbg_free(void *ptr, int line, const char *source)
282 struct memdebug *mem;
284 #ifdef __INTEL_COMPILER
285 # pragma warning(push)
286 # pragma warning(disable:1684)
287 /* 1684: conversion from pointer to same-sized integral type */
290 mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
292 #ifdef __INTEL_COMPILER
293 # pragma warning(pop)
301 curl_dbg_log("MEM %s:%d free(%p)\n", source, line, (void *)ptr);
304 curl_socket_t curl_dbg_socket(int domain, int type, int protocol,
305 int line, const char *source)
307 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
308 "FD %s:%d socket() = %d\n" :
309 (sizeof(curl_socket_t) == sizeof(long)) ?
310 "FD %s:%d socket() = %ld\n" :
311 "FD %s:%d socket() = %zd\n";
313 curl_socket_t sockfd;
315 if(countcheck("socket", line, source))
316 return CURL_SOCKET_BAD;
318 sockfd = socket(domain, type, protocol);
320 if(source && (sockfd != CURL_SOCKET_BAD))
321 curl_dbg_log(fmt, source, line, sockfd);
326 SEND_TYPE_RETV curl_dbg_send(SEND_TYPE_ARG1 sockfd,
327 SEND_QUAL_ARG2 SEND_TYPE_ARG2 buf,
328 SEND_TYPE_ARG3 len, SEND_TYPE_ARG4 flags, int line,
332 if(countcheck("send", line, source))
334 rc = send(sockfd, buf, len, flags);
336 curl_dbg_log("SEND %s:%d send(%lu) = %ld\n",
337 source, line, (unsigned long)len, (long)rc);
341 RECV_TYPE_RETV curl_dbg_recv(RECV_TYPE_ARG1 sockfd, RECV_TYPE_ARG2 buf,
342 RECV_TYPE_ARG3 len, RECV_TYPE_ARG4 flags, int line,
346 if(countcheck("recv", line, source))
348 rc = recv(sockfd, buf, len, flags);
350 curl_dbg_log("RECV %s:%d recv(%lu) = %ld\n",
351 source, line, (unsigned long)len, (long)rc);
355 #ifdef HAVE_SOCKETPAIR
356 int curl_dbg_socketpair(int domain, int type, int protocol,
357 curl_socket_t socket_vector[2],
358 int line, const char *source)
360 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
361 "FD %s:%d socketpair() = %d %d\n" :
362 (sizeof(curl_socket_t) == sizeof(long)) ?
363 "FD %s:%d socketpair() = %ld %ld\n" :
364 "FD %s:%d socketpair() = %zd %zd\n";
366 int res = socketpair(domain, type, protocol, socket_vector);
368 if(source && (0 == res))
369 curl_dbg_log(fmt, source, line, socket_vector[0], socket_vector[1]);
375 curl_socket_t curl_dbg_accept(curl_socket_t s, void *saddr, void *saddrlen,
376 int line, const char *source)
378 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
379 "FD %s:%d accept() = %d\n" :
380 (sizeof(curl_socket_t) == sizeof(long)) ?
381 "FD %s:%d accept() = %ld\n" :
382 "FD %s:%d accept() = %zd\n";
384 struct sockaddr *addr = (struct sockaddr *)saddr;
385 curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
387 curl_socket_t sockfd = accept(s, addr, addrlen);
389 if(source && (sockfd != CURL_SOCKET_BAD))
390 curl_dbg_log(fmt, source, line, sockfd);
395 /* separate function to allow libcurl to mark a "faked" close */
396 void curl_dbg_mark_sclose(curl_socket_t sockfd, int line, const char *source)
398 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
399 "FD %s:%d sclose(%d)\n":
400 (sizeof(curl_socket_t) == sizeof(long)) ?
401 "FD %s:%d sclose(%ld)\n":
402 "FD %s:%d sclose(%zd)\n";
405 curl_dbg_log(fmt, source, line, sockfd);
408 /* this is our own defined way to close sockets on *ALL* platforms */
409 int curl_dbg_sclose(curl_socket_t sockfd, int line, const char *source)
411 int res = sclose(sockfd);
412 curl_dbg_mark_sclose(sockfd, line, source);
416 ALLOC_FUNC FILE *curl_dbg_fopen(const char *file, const char *mode,
417 int line, const char *source)
419 FILE *res = fopen(file, mode);
422 curl_dbg_log("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
423 source, line, file, mode, (void *)res);
428 ALLOC_FUNC FILE *curl_dbg_fdopen(int filedes, const char *mode,
429 int line, const char *source)
431 FILE *res = fdopen(filedes, mode);
433 curl_dbg_log("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
434 source, line, filedes, mode, (void *)res);
438 int curl_dbg_fclose(FILE *file, int line, const char *source)
442 DEBUGASSERT(file != NULL);
445 curl_dbg_log("FILE %s:%d fclose(%p)\n",
446 source, line, (void *)file);
453 #define LOGLINE_BUFSIZE 1024
455 /* this does the writing to the memory tracking log file */
456 void curl_dbg_log(const char *format, ...)
462 if(!curl_dbg_logfile)
465 buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
469 va_start(ap, format);
470 nchars = mvsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
473 if(nchars > LOGLINE_BUFSIZE - 1)
474 nchars = LOGLINE_BUFSIZE - 1;
477 fwrite(buf, 1, (size_t)nchars, curl_dbg_logfile);
482 #endif /* CURLDEBUG */