1
0
mirror of https://github.com/moparisthebest/curl synced 2024-11-13 21:15:08 -05:00
curl/lib/memdebug.c

463 lines
12 KiB
C
Raw Normal View History

2002-09-03 07:52:59 -04:00
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
2000-10-09 07:11:43 -04:00
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
2000-10-09 07:11:43 -04:00
*
2002-09-03 07:52:59 -04:00
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
2020-11-04 08:02:01 -05:00
* are also available at https://curl.se/docs/copyright.html.
*
2001-01-03 04:29:33 -05:00
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
2002-09-03 07:52:59 -04:00
* furnished to do so, under the terms of the COPYING file.
2000-10-09 07:11:43 -04:00
*
2001-01-03 04:29:33 -05:00
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
2000-10-09 07:11:43 -04:00
*
2002-09-03 07:52:59 -04:00
***************************************************************************/
2000-10-09 07:11:43 -04:00
build: fix circular header inclusion with other packages This commit renames lib/setup.h to lib/curl_setup.h and renames lib/setup_once.h to lib/curl_setup_once.h. Removes the need and usage of a header inclusion guard foreign to libcurl. [1] Removes the need and presence of an alarming notice we carried in old setup_once.h [2] ---------------------------------------- 1 - lib/setup_once.h used __SETUP_ONCE_H macro as header inclusion guard up to commit ec691ca3 which changed this to HEADER_CURL_SETUP_ONCE_H, this single inclusion guard is enough to ensure that inclusion of lib/setup_once.h done from lib/setup.h is only done once. Additionally lib/setup.h has always used __SETUP_ONCE_H macro to protect inclusion of setup_once.h even after commit ec691ca3, this was to avoid a circular header inclusion triggered when building a c-ares enabled version with c-ares sources available which also has a setup_once.h header. Commit ec691ca3 exposes the real nature of __SETUP_ONCE_H usage in lib/setup.h, it is a header inclusion guard foreign to libcurl belonging to c-ares's setup_once.h The renaming this commit does, fixes the circular header inclusion, and as such removes the need and usage of a header inclusion guard foreign to libcurl. Macro __SETUP_ONCE_H no longer used in libcurl. 2 - Due to the circular interdependency of old lib/setup_once.h and the c-ares setup_once.h header, old file lib/setup_once.h has carried back from 2006 up to now days an alarming and prominent notice about the need of keeping libcurl's and c-ares's setup_once.h in sync. Given that this commit fixes the circular interdependency, the need and presence of mentioned notice is removed. All mentioned interdependencies come back from now old days when the c-ares project lived inside a curl subdirectory. This commit removes last traces of such fact.
2013-01-06 13:06:49 -05:00
#include "curl_setup.h"
2000-10-09 07:11:43 -04:00
#ifdef CURLDEBUG
#include <curl/curl.h>
#include "urldata.h"
2000-10-09 07:11:43 -04:00
#define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
#include "curl_memory.h"
#include "memdebug.h"
struct memdebug {
size_t size;
union {
curl_off_t o;
double d;
void *p;
} mem[1];
/* I'm hoping this is the thing with the strictest alignment
* requirements. That also means we waste some space :-( */
};
2000-10-09 07:11:43 -04:00
/*
* Note that these debug functions are very simple and they are meant to
* remain so. For advanced analysis, record a log file and write perl scripts
* to analyze them!
*
* Don't use these with multithreaded test programs!
*/
FILE *curl_dbg_logfile = NULL;
static bool memlimit = FALSE; /* enable memory limit */
static long memsize = 0; /* set number of mallocs allowed */
2000-10-09 07:11:43 -04:00
/* this sets the log file name */
void curl_dbg_memdebug(const char *logname)
2000-10-09 07:11:43 -04:00
{
if(!curl_dbg_logfile) {
if(logname && *logname)
curl_dbg_logfile = fopen(logname, FOPEN_WRITETEXT);
else
curl_dbg_logfile = stderr;
#ifdef MEMDEBUG_LOG_SYNC
/* Flush the log file after every line so the log isn't lost in a crash */
if(curl_dbg_logfile)
setbuf(curl_dbg_logfile, (char *)NULL);
#endif
}
2000-10-09 07:11:43 -04:00
}
/* This function sets the number of malloc() calls that should return
successfully! */
void curl_dbg_memlimit(long limit)
{
if(!memlimit) {
memlimit = TRUE;
memsize = limit;
}
}
/* returns TRUE if this isn't allowed! */
static bool countcheck(const char *func, int line, const char *source)
{
/* if source is NULL, then the call is made internally and this check
should not be made */
if(memlimit && source) {
if(!memsize) {
if(source) {
/* log to file */
curl_dbg_log("LIMIT %s:%d %s reached memlimit\n",
source, line, func);
/* log to stderr also */
fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
source, line, func);
fflush(curl_dbg_logfile); /* because it might crash now */
}
errno = ENOMEM;
return TRUE; /* RETURN ERROR! */
}
else
memsize--; /* countdown */
}
return FALSE; /* allow this */
}
2000-10-09 07:11:43 -04:00
void *curl_dbg_malloc(size_t wantedsize, int line, const char *source)
2000-10-09 07:11:43 -04:00
{
struct memdebug *mem;
size_t size;
DEBUGASSERT(wantedsize != 0);
if(countcheck("malloc", line, source))
return NULL;
/* alloc at least 64 bytes */
2017-09-09 17:55:08 -04:00
size = sizeof(struct memdebug) + wantedsize;
2009-04-21 05:03:15 -04:00
mem = (Curl_cmalloc)(size);
if(mem) {
mem->size = wantedsize;
}
if(source)
curl_dbg_log("MEM %s:%d malloc(%zu) = %p\n",
source, line, wantedsize,
mem ? (void *)mem->mem : (void *)0);
return (mem ? mem->mem : NULL);
2000-10-09 07:11:43 -04:00
}
void *curl_dbg_calloc(size_t wanted_elements, size_t wanted_size,
int line, const char *source)
{
struct memdebug *mem;
size_t size, user_size;
DEBUGASSERT(wanted_elements != 0);
DEBUGASSERT(wanted_size != 0);
if(countcheck("calloc", line, source))
return NULL;
/* alloc at least 64 bytes */
user_size = wanted_size * wanted_elements;
size = sizeof(struct memdebug) + user_size;
mem = (Curl_ccalloc)(1, size);
if(mem)
mem->size = user_size;
if(source)
curl_dbg_log("MEM %s:%d calloc(%zu,%zu) = %p\n",
source, line, wanted_elements, wanted_size,
mem ? (void *)mem->mem : (void *)0);
return (mem ? mem->mem : NULL);
}
char *curl_dbg_strdup(const char *str, int line, const char *source)
2000-10-09 07:11:43 -04:00
{
char *mem;
size_t len;
DEBUGASSERT(str != NULL);
if(countcheck("strdup", line, source))
return NULL;
2017-09-09 17:55:08 -04:00
len = strlen(str) + 1;
mem = curl_dbg_malloc(len, 0, NULL); /* NULL prevents logging */
if(mem)
memcpy(mem, str, len);
if(source)
curl_dbg_log("MEM %s:%d strdup(%p) (%zu) = %p\n",
source, line, (const void *)str, len, (const void *)mem);
2000-10-09 07:11:43 -04:00
return mem;
}
#if defined(WIN32) && defined(UNICODE)
wchar_t *curl_dbg_wcsdup(const wchar_t *str, int line, const char *source)
{
wchar_t *mem;
size_t wsiz, bsiz;
DEBUGASSERT(str != NULL);
if(countcheck("wcsdup", line, source))
return NULL;
wsiz = wcslen(str) + 1;
bsiz = wsiz * sizeof(wchar_t);
mem = curl_dbg_malloc(bsiz, 0, NULL); /* NULL prevents logging */
if(mem)
memcpy(mem, str, bsiz);
if(source)
curl_dbg_log("MEM %s:%d wcsdup(%p) (%zu) = %p\n",
source, line, (void *)str, bsiz, (void *)mem);
return mem;
}
#endif
/* We provide a realloc() that accepts a NULL as pointer, which then
performs a malloc(). In order to work with ares. */
void *curl_dbg_realloc(void *ptr, size_t wantedsize,
int line, const char *source)
2000-10-09 07:11:43 -04:00
{
struct memdebug *mem = NULL;
2017-09-09 17:55:08 -04:00
size_t size = sizeof(struct memdebug) + wantedsize;
DEBUGASSERT(wantedsize != 0);
if(countcheck("realloc", line, source))
return NULL;
2010-02-21 14:59:09 -05:00
#ifdef __INTEL_COMPILER
# pragma warning(push)
# pragma warning(disable:1684)
/* 1684: conversion from pointer to same-sized integral type */
#endif
if(ptr)
2010-02-20 06:58:26 -05:00
mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
2010-02-21 14:59:09 -05:00
#ifdef __INTEL_COMPILER
# pragma warning(pop)
#endif
2009-04-21 05:03:15 -04:00
mem = (Curl_crealloc)(mem, size);
if(source)
curl_dbg_log("MEM %s:%d realloc(%p, %zu) = %p\n",
source, line, (void *)ptr, wantedsize,
mem ? (void *)mem->mem : (void *)0);
if(mem) {
mem->size = wantedsize;
return mem->mem;
}
return NULL;
2000-10-09 07:11:43 -04:00
}
void curl_dbg_free(void *ptr, int line, const char *source)
2000-10-09 07:11:43 -04:00
{
if(ptr) {
struct memdebug *mem;
2010-02-21 14:59:09 -05:00
#ifdef __INTEL_COMPILER
# pragma warning(push)
# pragma warning(disable:1684)
/* 1684: conversion from pointer to same-sized integral type */
#endif
mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
2010-02-21 14:59:09 -05:00
#ifdef __INTEL_COMPILER
# pragma warning(pop)
#endif
/* free for real */
(Curl_cfree)(mem);
}
if(source && ptr)
curl_dbg_log("MEM %s:%d free(%p)\n", source, line, (void *)ptr);
2000-10-09 07:11:43 -04:00
}
curl_socket_t curl_dbg_socket(int domain, int type, int protocol,
int line, const char *source)
{
const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
"FD %s:%d socket() = %d\n" :
(sizeof(curl_socket_t) == sizeof(long)) ?
"FD %s:%d socket() = %ld\n" :
"FD %s:%d socket() = %zd\n";
curl_socket_t sockfd;
if(countcheck("socket", line, source))
return CURL_SOCKET_BAD;
sockfd = socket(domain, type, protocol);
if(source && (sockfd != CURL_SOCKET_BAD))
curl_dbg_log(fmt, source, line, sockfd);
return sockfd;
}
SEND_TYPE_RETV curl_dbg_send(SEND_TYPE_ARG1 sockfd,
SEND_QUAL_ARG2 SEND_TYPE_ARG2 buf,
SEND_TYPE_ARG3 len, SEND_TYPE_ARG4 flags, int line,
const char *source)
{
SEND_TYPE_RETV rc;
if(countcheck("send", line, source))
return -1;
rc = send(sockfd, buf, len, flags);
if(source)
curl_dbg_log("SEND %s:%d send(%lu) = %ld\n",
source, line, (unsigned long)len, (long)rc);
return rc;
}
RECV_TYPE_RETV curl_dbg_recv(RECV_TYPE_ARG1 sockfd, RECV_TYPE_ARG2 buf,
RECV_TYPE_ARG3 len, RECV_TYPE_ARG4 flags, int line,
const char *source)
{
RECV_TYPE_RETV rc;
if(countcheck("recv", line, source))
return -1;
rc = recv(sockfd, buf, len, flags);
if(source)
curl_dbg_log("RECV %s:%d recv(%lu) = %ld\n",
source, line, (unsigned long)len, (long)rc);
return rc;
}
#ifdef HAVE_SOCKETPAIR
int curl_dbg_socketpair(int domain, int type, int protocol,
curl_socket_t socket_vector[2],
int line, const char *source)
{
const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
"FD %s:%d socketpair() = %d %d\n" :
(sizeof(curl_socket_t) == sizeof(long)) ?
"FD %s:%d socketpair() = %ld %ld\n" :
"FD %s:%d socketpair() = %zd %zd\n";
int res = socketpair(domain, type, protocol, socket_vector);
if(source && (0 == res))
curl_dbg_log(fmt, source, line, socket_vector[0], socket_vector[1]);
return res;
}
#endif
curl_socket_t curl_dbg_accept(curl_socket_t s, void *saddr, void *saddrlen,
int line, const char *source)
2000-12-18 11:13:37 -05:00
{
const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
"FD %s:%d accept() = %d\n" :
(sizeof(curl_socket_t) == sizeof(long)) ?
"FD %s:%d accept() = %ld\n" :
"FD %s:%d accept() = %zd\n";
struct sockaddr *addr = (struct sockaddr *)saddr;
curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
curl_socket_t sockfd = accept(s, addr, addrlen);
if(source && (sockfd != CURL_SOCKET_BAD))
curl_dbg_log(fmt, source, line, sockfd);
2000-12-18 11:13:37 -05:00
return sockfd;
}
/* separate function to allow libcurl to mark a "faked" close */
void curl_dbg_mark_sclose(curl_socket_t sockfd, int line, const char *source)
{
const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
"FD %s:%d sclose(%d)\n":
(sizeof(curl_socket_t) == sizeof(long)) ?
"FD %s:%d sclose(%ld)\n":
"FD %s:%d sclose(%zd)\n";
if(source)
curl_dbg_log(fmt, source, line, sockfd);
}
/* this is our own defined way to close sockets on *ALL* platforms */
int curl_dbg_sclose(curl_socket_t sockfd, int line, const char *source)
{
int res = sclose(sockfd);
curl_dbg_mark_sclose(sockfd, line, source);
return res;
}
FILE *curl_dbg_fopen(const char *file, const char *mode,
int line, const char *source)
{
FILE *res = fopen(file, mode);
if(source)
curl_dbg_log("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
source, line, file, mode, (void *)res);
return res;
}
FILE *curl_dbg_fdopen(int filedes, const char *mode,
int line, const char *source)
{
FILE *res = fdopen(filedes, mode);
if(source)
curl_dbg_log("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
source, line, filedes, mode, (void *)res);
return res;
}
int curl_dbg_fclose(FILE *file, int line, const char *source)
{
2002-03-08 10:31:44 -05:00
int res;
DEBUGASSERT(file != NULL);
2002-03-08 10:31:44 -05:00
if(source)
curl_dbg_log("FILE %s:%d fclose(%p)\n",
source, line, (void *)file);
res = fclose(file);
return res;
}
#define LOGLINE_BUFSIZE 1024
2017-03-26 11:02:22 -04:00
/* this does the writing to the memory tracking log file */
void curl_dbg_log(const char *format, ...)
{
char *buf;
int nchars;
va_list ap;
if(!curl_dbg_logfile)
return;
buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
if(!buf)
return;
va_start(ap, format);
nchars = mvsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
va_end(ap);
if(nchars > LOGLINE_BUFSIZE - 1)
nchars = LOGLINE_BUFSIZE - 1;
if(nchars > 0)
fwrite(buf, 1, (size_t)nchars, curl_dbg_logfile);
(Curl_cfree)(buf);
}
#endif /* CURLDEBUG */