| 1 | #ifndef HEADER_CURL_MEMDEBUG_H |
| 2 | #define |
| 3 | #ifdef CURLDEBUG |
| 4 | /*************************************************************************** |
| 5 | * _ _ ____ _ |
| 6 | * Project ___| | | | _ \| | |
| 7 | * / __| | | | |_) | | |
| 8 | * | (__| |_| | _ <| |___ |
| 9 | * \___|\___/|_| \_\_____| |
| 10 | * |
| 11 | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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 | #include <curl/curl.h> |
| 34 | #include "functypes.h" |
| 35 | |
| 36 | #if defined(__GNUC__) && __GNUC__ >= 3 |
| 37 | # define ALLOC_FUNC __attribute__((malloc)) |
| 38 | # define ALLOC_SIZE(s) __attribute__((alloc_size(s))) |
| 39 | # define ALLOC_SIZE2(n, s) __attribute__((alloc_size(n, s))) |
| 40 | #elif defined(_MSC_VER) |
| 41 | # define ALLOC_FUNC __declspec(restrict) |
| 42 | # define ALLOC_SIZE(s) |
| 43 | # define ALLOC_SIZE2(n, s) |
| 44 | #else |
| 45 | # define ALLOC_FUNC |
| 46 | # define ALLOC_SIZE(s) |
| 47 | # define ALLOC_SIZE2(n, s) |
| 48 | #endif |
| 49 | |
| 50 | #define CURL_MT_LOGFNAME_BUFSIZE 512 |
| 51 | |
| 52 | extern FILE *curl_dbg_logfile; |
| 53 | |
| 54 | /* memory functions */ |
| 55 | CURL_EXTERN ALLOC_FUNC ALLOC_SIZE(1) void *curl_dbg_malloc(size_t size, |
| 56 | int line, |
| 57 | const char *source); |
| 58 | CURL_EXTERN ALLOC_FUNC ALLOC_SIZE2(1, 2) void *curl_dbg_calloc(size_t elements, |
| 59 | size_t size, int line, const char *source); |
| 60 | CURL_EXTERN ALLOC_SIZE(2) void *curl_dbg_realloc(void *ptr, |
| 61 | size_t size, |
| 62 | int line, |
| 63 | const char *source); |
| 64 | CURL_EXTERN void curl_dbg_free(void *ptr, int line, const char *source); |
| 65 | CURL_EXTERN ALLOC_FUNC char *curl_dbg_strdup(const char *str, int line, |
| 66 | const char *src); |
| 67 | #if defined(WIN32) && defined(UNICODE) |
| 68 | CURL_EXTERN ALLOC_FUNC wchar_t *curl_dbg_wcsdup(const wchar_t *str, |
| 69 | int line, |
| 70 | const char *source); |
| 71 | #endif |
| 72 | |
| 73 | CURL_EXTERN void curl_dbg_memdebug(const char *logname); |
| 74 | CURL_EXTERN void curl_dbg_memlimit(long limit); |
| 75 | CURL_EXTERN void curl_dbg_log(const char *format, ...); |
| 76 | |
| 77 | /* file descriptor manipulators */ |
| 78 | CURL_EXTERN curl_socket_t curl_dbg_socket(int domain, int type, int protocol, |
| 79 | int line, const char *source); |
| 80 | CURL_EXTERN void curl_dbg_mark_sclose(curl_socket_t sockfd, |
| 81 | int line, const char *source); |
| 82 | CURL_EXTERN int curl_dbg_sclose(curl_socket_t sockfd, |
| 83 | int line, const char *source); |
| 84 | CURL_EXTERN curl_socket_t curl_dbg_accept(curl_socket_t s, void *a, void *alen, |
| 85 | int line, const char *source); |
| 86 | #ifdef HAVE_SOCKETPAIR |
| 87 | CURL_EXTERN int curl_dbg_socketpair(int domain, int type, int protocol, |
| 88 | curl_socket_t socket_vector[2], |
| 89 | int line, const char *source); |
| 90 | #endif |
| 91 | |
| 92 | /* send/receive sockets */ |
| 93 | CURL_EXTERN SEND_TYPE_RETV curl_dbg_send(SEND_TYPE_ARG1 sockfd, |
| 94 | SEND_QUAL_ARG2 SEND_TYPE_ARG2 buf, |
| 95 | SEND_TYPE_ARG3 len, |
| 96 | SEND_TYPE_ARG4 flags, int line, |
| 97 | const char *source); |
| 98 | CURL_EXTERN RECV_TYPE_RETV curl_dbg_recv(RECV_TYPE_ARG1 sockfd, |
| 99 | RECV_TYPE_ARG2 buf, |
| 100 | RECV_TYPE_ARG3 len, |
| 101 | RECV_TYPE_ARG4 flags, int line, |
| 102 | const char *source); |
| 103 | |
| 104 | /* FILE functions */ |
| 105 | CURL_EXTERN ALLOC_FUNC FILE *curl_dbg_fopen(const char *file, const char *mode, |
| 106 | int line, const char *source); |
| 107 | CURL_EXTERN ALLOC_FUNC FILE *curl_dbg_fdopen(int filedes, const char *mode, |
| 108 | int line, const char *source); |
| 109 | |
| 110 | CURL_EXTERN int curl_dbg_fclose(FILE *file, int line, const char *source); |
| 111 | |
| 112 | #ifndef MEMDEBUG_NODEFINES |
| 113 | |
| 114 | /* Set this symbol on the command-line, recompile all lib-sources */ |
| 115 | #undef strdup |
| 116 | #define strdup(ptr) curl_dbg_strdup(ptr, __LINE__, __FILE__) |
| 117 | #define malloc(size) curl_dbg_malloc(size, __LINE__, __FILE__) |
| 118 | #define calloc(nbelem,size) curl_dbg_calloc(nbelem, size, __LINE__, __FILE__) |
| 119 | #define realloc(ptr,size) curl_dbg_realloc(ptr, size, __LINE__, __FILE__) |
| 120 | #define free(ptr) curl_dbg_free(ptr, __LINE__, __FILE__) |
| 121 | #define send(a,b,c,d) curl_dbg_send(a,b,c,d, __LINE__, __FILE__) |
| 122 | #define recv(a,b,c,d) curl_dbg_recv(a,b,c,d, __LINE__, __FILE__) |
| 123 | |
| 124 | #ifdef WIN32 |
| 125 | # ifdef UNICODE |
| 126 | # undef wcsdup |
| 127 | # define wcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__) |
| 128 | # undef _wcsdup |
| 129 | # define _wcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__) |
| 130 | # undef _tcsdup |
| 131 | # define _tcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__) |
| 132 | # else |
| 133 | # undef _tcsdup |
| 134 | # define _tcsdup(ptr) curl_dbg_strdup(ptr, __LINE__, __FILE__) |
| 135 | # endif |
| 136 | #endif |
| 137 | |
| 138 | #undef socket |
| 139 | #define socket(domain,type,protocol)\ |
| 140 | curl_dbg_socket(domain, type, protocol, __LINE__, __FILE__) |
| 141 | #undef accept /* for those with accept as a macro */ |
| 142 | #define accept(sock,addr,len)\ |
| 143 | curl_dbg_accept(sock, addr, len, __LINE__, __FILE__) |
| 144 | #ifdef HAVE_SOCKETPAIR |
| 145 | #define socketpair(domain,type,protocol,socket_vector)\ |
| 146 | curl_dbg_socketpair(domain, type, protocol, socket_vector, __LINE__, __FILE__) |
| 147 | #endif |
| 148 | |
| 149 | #ifdef HAVE_GETADDRINFO |
| 150 | #if defined(getaddrinfo) && defined(__osf__) |
| 151 | /* OSF/1 and Tru64 have getaddrinfo as a define already, so we cannot define |
| 152 | our macro as for other platforms. Instead, we redefine the new name they |
| 153 | define getaddrinfo to become! */ |
| 154 | #define ogetaddrinfo(host,serv,hint,res) \ |
| 155 | curl_dbg_getaddrinfo(host, serv, hint, res, __LINE__, __FILE__) |
| 156 | #else |
| 157 | #undef getaddrinfo |
| 158 | #define getaddrinfo(host,serv,hint,res) \ |
| 159 | curl_dbg_getaddrinfo(host, serv, hint, res, __LINE__, __FILE__) |
| 160 | #endif |
| 161 | #endif /* HAVE_GETADDRINFO */ |
| 162 | |
| 163 | #ifdef HAVE_FREEADDRINFO |
| 164 | #undef freeaddrinfo |
| 165 | #define freeaddrinfo(data) \ |
| 166 | curl_dbg_freeaddrinfo(data, __LINE__, __FILE__) |
| 167 | #endif /* HAVE_FREEADDRINFO */ |
| 168 | |
| 169 | /* sclose is probably already defined, redefine it! */ |
| 170 | #undef sclose |
| 171 | #define sclose(sockfd) curl_dbg_sclose(sockfd,__LINE__,__FILE__) |
| 172 | |
| 173 | #define fake_sclose(sockfd) curl_dbg_mark_sclose(sockfd,__LINE__,__FILE__) |
| 174 | |
| 175 | #undef fopen |
| 176 | #define fopen(file,mode) curl_dbg_fopen(file,mode,__LINE__,__FILE__) |
| 177 | #undef fdopen |
| 178 | #define fdopen(file,mode) curl_dbg_fdopen(file,mode,__LINE__,__FILE__) |
| 179 | #define fclose(file) curl_dbg_fclose(file,__LINE__,__FILE__) |
| 180 | |
| 181 | #endif /* MEMDEBUG_NODEFINES */ |
| 182 | |
| 183 | #endif /* CURLDEBUG */ |
| 184 | |
| 185 | /* |
| 186 | ** Following section applies even when CURLDEBUG is not defined. |
| 187 | */ |
| 188 | |
| 189 | #ifndef fake_sclose |
| 190 | #define fake_sclose(x) Curl_nop_stmt |
| 191 | #endif |
| 192 | |
| 193 | /* |
| 194 | * Curl_safefree defined as a macro to allow MemoryTracking feature |
| 195 | * to log free() calls at same location where Curl_safefree is used. |
| 196 | * This macro also assigns NULL to given pointer when free'd. |
| 197 | */ |
| 198 | |
| 199 | #define Curl_safefree(ptr) \ |
| 200 | do { free((ptr)); (ptr) = NULL;} while(0) |
| 201 | |
| 202 | #endif /* HEADER_CURL_MEMDEBUG_H */ |
| 203 | |