the malloc debug system only logs data if the logfile FILE * is set, which
[platform/upstream/curl.git] / lib / memdebug.c
1 #ifdef MALLOCDEBUG
2 /*****************************************************************************
3  *                                  _   _ ____  _     
4  *  Project                     ___| | | |  _ \| |    
5  *                             / __| | | | |_) | |    
6  *                            | (__| |_| |  _ <| |___ 
7  *                             \___|\___/|_| \_\_____|
8  *
9  * Copyright (C) 2000, Daniel Stenberg, <daniel@haxx.se>, et al.
10  *
11  * In order to be useful for every potential user, curl and libcurl are
12  * dual-licensed under the MPL and the MIT/X-derivate licenses.
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 MPL or the MIT/X-derivate
17  * licenses. You may pick one of these licenses.
18  *
19  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20  * KIND, either express or implied.
21  *
22  * $Id$
23  *****************************************************************************/
24
25 #include "setup.h"
26
27 #include <curl/curl.h>
28
29 #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
30 #include <winsock.h>
31 #else /* some kind of unix */
32 #ifdef HAVE_SYS_SOCKET_H
33 #include <sys/socket.h>
34 #endif
35 #endif
36
37 #define _MPRINTF_REPLACE
38 #include <curl/mprintf.h>
39 #include "urldata.h"
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47
48 /* DONT include memdebug.h here! */
49
50 /*
51  * Note that these debug functions are very simple and they are meant to
52  * remain so. For advanced analysis, record a log file and write perl scripts
53  * to analyze them!
54  *
55  * Don't use these with multithreaded test programs!
56  */
57
58 FILE *logfile;
59
60 /* this sets the log file name */
61 void curl_memdebug(const char *logname)
62 {
63   if(logname)
64     logfile = fopen(logname, "w");
65   else
66     logfile = stderr;
67 }
68
69
70 void *curl_domalloc(size_t size, int line, const char *source)
71 {
72   void *mem=(malloc)(size);
73   if(logfile)
74     fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n",
75             source, line, size, mem);
76   return mem;
77 }
78
79 char *curl_dostrdup(const char *str, int line, const char *source)
80 {
81   char *mem;
82   size_t len;
83   
84   if(NULL ==str) {
85     fprintf(stderr, "ILLEGAL strdup() on NULL at %s:%d\n",
86             source, line);
87     exit(2);
88   }
89
90   mem=(strdup)(str);
91   len=strlen(str)+1;
92   if(logfile)
93     fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n",
94             source, line, str, len, mem);
95   return mem;
96 }
97
98 void *curl_dorealloc(void *ptr, size_t size, int line, const char *source)
99 {
100   void *mem=(realloc)(ptr, size);
101   if(logfile)
102     fprintf(logfile, "MEM %s:%d realloc(%p, %d) = %p\n",
103             source, line, ptr, size, mem);
104   return mem;
105 }
106
107 void curl_dofree(void *ptr, int line, const char *source)
108 {
109   if(NULL == ptr) {
110     fprintf(stderr, "ILLEGAL free() on NULL at %s:%d\n",
111             source, line);
112     exit(2);
113   }
114
115   (free)(ptr);
116
117   if(logfile)
118     fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
119 }
120
121 int curl_socket(int domain, int type, int protocol, int line, char *source)
122 {
123   int sockfd=(socket)(domain, type, protocol);
124   if(logfile)
125     fprintf(logfile, "FD %s:%d socket() = %d\n",
126             source, line, sockfd);
127   return sockfd;
128 }
129
130 int curl_accept(int s, struct sockaddr *addr, socklen_t *addrlen,
131                 int line, const char *source)
132 {
133   int sockfd=(accept)(s, addr, addrlen);
134   if(logfile)
135     fprintf(logfile, "FD %s:%d accept() = %d\n",
136             source, line, sockfd);
137   return sockfd;
138 }
139
140 /* this is our own defined way to close sockets on *ALL* platforms */
141 int curl_sclose(int sockfd, int line, char *source)
142 {
143   int res=sclose(sockfd);
144   if(logfile)
145     fprintf(logfile, "FD %s:%d sclose(%d)\n",
146             source, line, sockfd);
147   return res;
148 }
149
150 FILE *curl_fopen(const char *file, const char *mode,
151                  int line, const char *source)
152 {
153   FILE *res=(fopen)(file, mode);
154   if(logfile)
155     fprintf(logfile, "FILE %s:%d fopen(\"%s\") = %p\n",
156             source, line, file, res);
157   return res;
158 }
159
160 int curl_fclose(FILE *file, int line, const char *source)
161 {
162   int res=(fclose)(file);
163   if(logfile)
164     fprintf(logfile, "FILE %s:%d fclose(%p)\n",
165             source, line, file);
166   return res;
167 }
168
169 #endif /* MALLOCDEBUG */
170
171 /*
172  * local variables:
173  * eval: (load-file "../curl-mode.el")
174  * end:
175  * vim600: fdm=marker
176  * vim: et sw=2 ts=2 sts=2 tw=78
177  */