1#ifndef HEADER_CURL_MEMDEBUG_H
2#define HEADER_CURL_MEMDEBUG_H
3#ifdef CURLDEBUG
4/***************************************************************************
5 * _ _ ____ _
6 * Project ___| | | | _ \| |
7 * / __| | | | |_) | |
8 * | (__| |_| | _ <| |___
9 * \___|\___/|_| \_\_____|
10 *
11 * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, et al.
12 *
13 * This software is licensed as described in the file COPYING, which
14 * you should have received as part of this distribution. The terms
15 * are also available at https://curl.se/docs/copyright.html.
16 *
17 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
18 * copies of the Software, and permit persons to whom the Software is
19 * furnished to do so, under the terms of the COPYING file.
20 *
21 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
22 * KIND, either express or implied.
23 *
24 * SPDX-License-Identifier: curl
25 *
26 ***************************************************************************/
27
28/*
29 * CAUTION: this header is designed to work when included by the app-side
30 * as well as the library. Do not mix with library internals!
31 */
32
33#if defined(__GNUC__) && __GNUC__ >= 3
34# define ALLOC_FUNC __attribute__((malloc))
35# define ALLOC_SIZE(s) __attribute__((alloc_size(s)))
36# define ALLOC_SIZE2(n, s) __attribute__((alloc_size(n, s)))
37#elif defined(_MSC_VER)
38# define ALLOC_FUNC __declspec(restrict)
39# define ALLOC_SIZE(s)
40# define ALLOC_SIZE2(n, s)
41#else
42# define ALLOC_FUNC
43# define ALLOC_SIZE(s)
44# define ALLOC_SIZE2(n, s)
45#endif
46
47#define CURL_MT_LOGFNAME_BUFSIZE 512
48
49extern FILE *curl_dbg_logfile;
50
51/* memory functions */
52CURL_EXTERN ALLOC_FUNC ALLOC_SIZE(1) void *curl_dbg_malloc(size_t size,
53 int line,
54 const char *source);
55CURL_EXTERN ALLOC_FUNC ALLOC_SIZE2(1, 2) void *curl_dbg_calloc(size_t elements,
56 size_t size, int line, const char *source);
57CURL_EXTERN ALLOC_SIZE(2) void *curl_dbg_realloc(void *ptr,
58 size_t size,
59 int line,
60 const char *source);
61CURL_EXTERN void curl_dbg_free(void *ptr, int line, const char *source);
62CURL_EXTERN ALLOC_FUNC char *curl_dbg_strdup(const char *str, int line,
63 const char *src);
64#if defined(WIN32) && defined(UNICODE)
65CURL_EXTERN ALLOC_FUNC wchar_t *curl_dbg_wcsdup(const wchar_t *str,
66 int line,
67 const char *source);
68#endif
69
70CURL_EXTERN void curl_dbg_memdebug(const char *logname);
71CURL_EXTERN void curl_dbg_memlimit(long limit);
72CURL_EXTERN void curl_dbg_log(const char *format, ...);
73
74/* file descriptor manipulators */
75CURL_EXTERN curl_socket_t curl_dbg_socket(int domain, int type, int protocol,
76 int line, const char *source);
77CURL_EXTERN void curl_dbg_mark_sclose(curl_socket_t sockfd,
78 int line, const char *source);
79CURL_EXTERN int curl_dbg_sclose(curl_socket_t sockfd,
80 int line, const char *source);
81CURL_EXTERN curl_socket_t curl_dbg_accept(curl_socket_t s, void *a, void *alen,
82 int line, const char *source);
83#ifdef HAVE_SOCKETPAIR
84CURL_EXTERN int curl_dbg_socketpair(int domain, int type, int protocol,
85 curl_socket_t socket_vector[2],
86 int line, const char *source);
87#endif
88
89/* send/receive sockets */
90CURL_EXTERN SEND_TYPE_RETV curl_dbg_send(SEND_TYPE_ARG1 sockfd,
91 SEND_QUAL_ARG2 SEND_TYPE_ARG2 buf,
92 SEND_TYPE_ARG3 len,
93 SEND_TYPE_ARG4 flags, int line,
94 const char *source);
95CURL_EXTERN RECV_TYPE_RETV curl_dbg_recv(RECV_TYPE_ARG1 sockfd,
96 RECV_TYPE_ARG2 buf,
97 RECV_TYPE_ARG3 len,
98 RECV_TYPE_ARG4 flags, int line,
99 const char *source);
100
101/* FILE functions */
102CURL_EXTERN ALLOC_FUNC FILE *curl_dbg_fopen(const char *file, const char *mode,
103 int line, const char *source);
104CURL_EXTERN ALLOC_FUNC FILE *curl_dbg_fdopen(int filedes, const char *mode,
105 int line, const char *source);
106
107CURL_EXTERN int curl_dbg_fclose(FILE *file, int line, const char *source);
108
109#ifndef MEMDEBUG_NODEFINES
110
111/* Set this symbol on the command-line, recompile all lib-sources */
112#undef strdup
113#define strdup(ptr) curl_dbg_strdup(ptr, __LINE__, __FILE__)
114#define malloc(size) curl_dbg_malloc(size, __LINE__, __FILE__)
115#define calloc(nbelem,size) curl_dbg_calloc(nbelem, size, __LINE__, __FILE__)
116#define realloc(ptr,size) curl_dbg_realloc(ptr, size, __LINE__, __FILE__)
117#define free(ptr) curl_dbg_free(ptr, __LINE__, __FILE__)
118#define send(a,b,c,d) curl_dbg_send(a,b,c,d, __LINE__, __FILE__)
119#define recv(a,b,c,d) curl_dbg_recv(a,b,c,d, __LINE__, __FILE__)
120
121#ifdef WIN32
122# ifdef UNICODE
123# undef wcsdup
124# define wcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__)
125# undef _wcsdup
126# define _wcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__)
127# undef _tcsdup
128# define _tcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__)
129# else
130# undef _tcsdup
131# define _tcsdup(ptr) curl_dbg_strdup(ptr, __LINE__, __FILE__)
132# endif
133#endif
134
135#undef socket
136#define socket(domain,type,protocol)\
137 curl_dbg_socket(domain, type, protocol, __LINE__, __FILE__)
138#undef accept /* for those with accept as a macro */
139#define accept(sock,addr,len)\
140 curl_dbg_accept(sock, addr, len, __LINE__, __FILE__)
141#ifdef HAVE_SOCKETPAIR
142#define socketpair(domain,type,protocol,socket_vector)\
143 curl_dbg_socketpair(domain, type, protocol, socket_vector, __LINE__, __FILE__)
144#endif
145
146#ifdef HAVE_GETADDRINFO
147#if defined(getaddrinfo) && defined(__osf__)
148/* OSF/1 and Tru64 have getaddrinfo as a define already, so we cannot define
149 our macro as for other platforms. Instead, we redefine the new name they
150 define getaddrinfo to become! */
151#define ogetaddrinfo(host,serv,hint,res) \
152 curl_dbg_getaddrinfo(host, serv, hint, res, __LINE__, __FILE__)
153#else
154#undef getaddrinfo
155#define getaddrinfo(host,serv,hint,res) \
156 curl_dbg_getaddrinfo(host, serv, hint, res, __LINE__, __FILE__)
157#endif
158#endif /* HAVE_GETADDRINFO */
159
160#ifdef HAVE_FREEADDRINFO
161#undef freeaddrinfo
162#define freeaddrinfo(data) \
163 curl_dbg_freeaddrinfo(data, __LINE__, __FILE__)
164#endif /* HAVE_FREEADDRINFO */
165
166/* sclose is probably already defined, redefine it! */
167#undef sclose
168#define sclose(sockfd) curl_dbg_sclose(sockfd,__LINE__,__FILE__)
169
170#define fake_sclose(sockfd) curl_dbg_mark_sclose(sockfd,__LINE__,__FILE__)
171
172#undef fopen
173#define fopen(file,mode) curl_dbg_fopen(file,mode,__LINE__,__FILE__)
174#undef fdopen
175#define fdopen(file,mode) curl_dbg_fdopen(file,mode,__LINE__,__FILE__)
176#define fclose(file) curl_dbg_fclose(file,__LINE__,__FILE__)
177
178#endif /* MEMDEBUG_NODEFINES */
179
180#endif /* CURLDEBUG */
181
182/*
183** Following section applies even when CURLDEBUG is not defined.
184*/
185
186#ifndef fake_sclose
187#define fake_sclose(x) Curl_nop_stmt
188#endif
189
190/*
191 * Curl_safefree defined as a macro to allow MemoryTracking feature
192 * to log free() calls at same location where Curl_safefree is used.
193 * This macro also assigns NULL to given pointer when free'd.
194 */
195
196#define Curl_safefree(ptr) \
197 do { free((ptr)); (ptr) = NULL;} while(0)
198
199#endif /* HEADER_CURL_MEMDEBUG_H */
200