2 /***************************************************************************
4 * Project ___| | | | _ \| |
6 * | (__| |_| | _ <| |___
7 * \___|\___/|_| \_\_____|
9 * Copyright (C) 1998 - 2005, Daniel Stenberg, <daniel@haxx.se>, et al.
11 * This software is licensed as described in the file COPYING, which
12 * you should have received as part of this distribution. The terms
13 * are also available at http://curl.haxx.se/docs/copyright.html.
15 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
16 * copies of the Software, and permit persons to whom the Software is
17 * furnished to do so, under the terms of the COPYING file.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 * $Id: memdebug.c,v 1.2 2007-03-15 19:22:13 andy Exp $
23 ***************************************************************************/
27 #include <curl/curl.h>
29 #ifdef HAVE_SYS_SOCKET_H
30 #include <sys/socket.h>
33 #define _MPRINTF_REPLACE
34 #include <curl/mprintf.h>
44 #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
51 /* I'm hoping this is the thing with the strictest alignment
52 * requirements. That also means we waste some space :-( */
56 * Note that these debug functions are very simple and they are meant to
57 * remain so. For advanced analysis, record a log file and write perl scripts
60 * Don't use these with multithreaded test programs!
63 #define logfile curl_debuglogfile
64 FILE *curl_debuglogfile
= NULL
;
65 static bool memlimit
= FALSE
; /* enable memory limit */
66 static long memsize
= 0; /* set number of mallocs allowed */
68 /* this sets the log file name */
69 void curl_memdebug(const char *logname
)
73 logfile
= fopen(logname
, "w");
79 /* This function sets the number of malloc() calls that should return
81 void curl_memlimit(long limit
)
89 /* returns TRUE if this isn't allowed! */
90 static bool countcheck(const char *func
, int line
, const char *source
)
92 /* if source is NULL, then the call is made internally and this check
94 if(memlimit
&& source
) {
97 fprintf(logfile
, "LIMIT %s:%d %s reached memlimit\n",
100 fprintf(stderr
, "LIMIT %s:%d %s reached memlimit\n",
103 return TRUE
; /* RETURN ERROR! */
106 memsize
--; /* countdown */
108 /* log the countdown */
109 if(logfile
&& source
)
110 fprintf(logfile
, "LIMIT %s:%d %ld ALLOCS left\n",
111 source
, line
, memsize
);
115 return FALSE
; /* allow this */
118 void *curl_domalloc(size_t wantedsize
, int line
, const char *source
)
120 struct memdebug
*mem
;
123 if(countcheck("malloc", line
, source
))
126 /* alloc at least 64 bytes */
127 size
= sizeof(struct memdebug
)+wantedsize
;
129 mem
=(struct memdebug
*)(Curl_cmalloc
)(size
);
131 /* fill memory with junk */
132 memset(mem
->mem
, 0xA5, wantedsize
);
133 mem
->size
= wantedsize
;
136 if(logfile
&& source
)
137 fprintf(logfile
, "MEM %s:%d malloc(%zd) = %p\n",
138 source
, line
, wantedsize
, mem
? mem
->mem
: 0);
139 return (mem
? mem
->mem
: NULL
);
142 void *curl_docalloc(size_t wanted_elements
, size_t wanted_size
,
143 int line
, const char *source
)
145 struct memdebug
*mem
;
146 size_t size
, user_size
;
148 if(countcheck("calloc", line
, source
))
151 /* alloc at least 64 bytes */
152 user_size
= wanted_size
* wanted_elements
;
153 size
= sizeof(struct memdebug
) + user_size
;
155 mem
= (struct memdebug
*)(Curl_cmalloc
)(size
);
157 /* fill memory with zeroes */
158 memset(mem
->mem
, 0, user_size
);
159 mem
->size
= user_size
;
162 if(logfile
&& source
)
163 fprintf(logfile
, "MEM %s:%d calloc(%u,%u) = %p\n",
164 source
, line
, wanted_elements
, wanted_size
, mem
? mem
->mem
: 0);
165 return (mem
? mem
->mem
: NULL
);
168 char *curl_dostrdup(const char *str
, int line
, const char *source
)
173 curlassert(str
!= NULL
);
175 if(countcheck("strdup", line
, source
))
180 mem
=curl_domalloc(len
, 0, NULL
); /* NULL prevents logging */
182 memcpy(mem
, str
, len
);
185 fprintf(logfile
, "MEM %s:%d strdup(%p) (%zd) = %p\n",
186 source
, line
, str
, len
, mem
);
191 /* We provide a realloc() that accepts a NULL as pointer, which then
192 performs a malloc(). In order to work with ares. */
193 void *curl_dorealloc(void *ptr
, size_t wantedsize
,
194 int line
, const char *source
)
196 struct memdebug
*mem
=NULL
;
198 size_t size
= sizeof(struct memdebug
)+wantedsize
;
200 if(countcheck("realloc", line
, source
))
204 mem
= (struct memdebug
*)((char *)ptr
- offsetof(struct memdebug
, mem
));
206 mem
=(struct memdebug
*)(Curl_crealloc
)(mem
, size
);
208 fprintf(logfile
, "MEM %s:%d realloc(%p, %zd) = %p\n",
209 source
, line
, ptr
, wantedsize
, mem
?mem
->mem
:NULL
);
212 mem
->size
= wantedsize
;
219 void curl_dofree(void *ptr
, int line
, const char *source
)
221 struct memdebug
*mem
;
223 curlassert(ptr
!= NULL
);
225 mem
= (struct memdebug
*)((char *)ptr
- offsetof(struct memdebug
, mem
));
228 memset(mem
->mem
, 0x13, mem
->size
);
234 fprintf(logfile
, "MEM %s:%d free(%p)\n", source
, line
, ptr
);
237 int curl_socket(int domain
, int type
, int protocol
, int line
,
240 int sockfd
=(socket
)(domain
, type
, protocol
);
241 if(logfile
&& (sockfd
!=-1))
242 fprintf(logfile
, "FD %s:%d socket() = %d\n",
243 source
, line
, sockfd
);
247 int curl_accept(int s
, void *saddr
, void *saddrlen
,
248 int line
, const char *source
)
250 struct sockaddr
*addr
= (struct sockaddr
*)saddr
;
251 socklen_t
*addrlen
= (socklen_t
*)saddrlen
;
252 int sockfd
=(accept
)(s
, addr
, addrlen
);
254 fprintf(logfile
, "FD %s:%d accept() = %d\n",
255 source
, line
, sockfd
);
259 /* this is our own defined way to close sockets on *ALL* platforms */
260 int curl_sclose(int sockfd
, int line
, const char *source
)
262 int res
=sclose(sockfd
);
264 fprintf(logfile
, "FD %s:%d sclose(%d)\n",
265 source
, line
, sockfd
);
269 FILE *curl_fopen(const char *file
, const char *mode
,
270 int line
, const char *source
)
272 FILE *res
=(fopen
)(file
, mode
);
274 fprintf(logfile
, "FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
275 source
, line
, file
, mode
, res
);
279 int curl_fclose(FILE *file
, int line
, const char *source
)
283 curlassert(file
!= NULL
);
287 fprintf(logfile
, "FILE %s:%d fclose(%p)\n",
293 int VOID_VAR_MEMDEBUG
;
295 /* we provide a fake do-nothing function here to avoid compiler warnings */
296 void curl_memdebug(void) {}
298 #endif /* CURLDEBUG */