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