memdebug.h (7695B)
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) 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 #ifdef __clang__ 37 # define ALLOC_FUNC __attribute__((__malloc__)) 38 # if __clang_major__ >= 4 39 # define ALLOC_SIZE(s) __attribute__((__alloc_size__(s))) 40 # define ALLOC_SIZE2(n, s) __attribute__((__alloc_size__(n, s))) 41 # else 42 # define ALLOC_SIZE(s) 43 # define ALLOC_SIZE2(n, s) 44 # endif 45 #elif defined(__GNUC__) && __GNUC__ >= 3 46 # define ALLOC_FUNC __attribute__((__malloc__)) 47 # define ALLOC_SIZE(s) __attribute__((__alloc_size__(s))) 48 # define ALLOC_SIZE2(n, s) __attribute__((__alloc_size__(n, s))) 49 #elif defined(_MSC_VER) 50 # define ALLOC_FUNC __declspec(restrict) 51 # define ALLOC_SIZE(s) 52 # define ALLOC_SIZE2(n, s) 53 #else 54 # define ALLOC_FUNC 55 # define ALLOC_SIZE(s) 56 # define ALLOC_SIZE2(n, s) 57 #endif 58 59 /* Avoid redundant redeclaration warnings with modern compilers, when including 60 this header multiple times. */ 61 #ifndef HEADER_CURL_MEMDEBUG_H_EXTERNS 62 #define HEADER_CURL_MEMDEBUG_H_EXTERNS 63 extern FILE *curl_dbg_logfile; 64 65 /* memory functions */ 66 CURL_EXTERN void curl_dbg_free(void *ptr, int line, const char *source); 67 CURL_EXTERN ALLOC_FUNC ALLOC_SIZE(1) 68 void *curl_dbg_malloc(size_t size, int line, const char *source); 69 CURL_EXTERN ALLOC_FUNC ALLOC_SIZE2(1, 2) 70 void *curl_dbg_calloc(size_t n, size_t size, int line, const char *source); 71 CURL_EXTERN ALLOC_SIZE(2) 72 void *curl_dbg_realloc(void *ptr, size_t size, int line, const char *source); 73 CURL_EXTERN ALLOC_FUNC 74 char *curl_dbg_strdup(const char *str, int line, const char *src); 75 #if defined(_WIN32) && defined(UNICODE) 76 CURL_EXTERN ALLOC_FUNC 77 wchar_t *curl_dbg_wcsdup(const wchar_t *str, int line, const char *source); 78 #endif 79 80 CURL_EXTERN void curl_dbg_memdebug(const char *logname); 81 CURL_EXTERN void curl_dbg_memlimit(long limit); 82 CURL_EXTERN void curl_dbg_log(const char *format, ...) CURL_PRINTF(1, 2); 83 84 /* file descriptor manipulators */ 85 CURL_EXTERN curl_socket_t curl_dbg_socket(int domain, int type, int protocol, 86 int line, const char *source); 87 CURL_EXTERN void curl_dbg_mark_sclose(curl_socket_t sockfd, 88 int line, const char *source); 89 CURL_EXTERN int curl_dbg_sclose(curl_socket_t sockfd, 90 int line, const char *source); 91 CURL_EXTERN curl_socket_t curl_dbg_accept(curl_socket_t s, void *a, void *alen, 92 int line, const char *source); 93 #ifdef HAVE_ACCEPT4 94 CURL_EXTERN curl_socket_t curl_dbg_accept4(curl_socket_t s, void *saddr, 95 void *saddrlen, int flags, 96 int line, const char *source); 97 #endif 98 #ifdef HAVE_SOCKETPAIR 99 CURL_EXTERN int curl_dbg_socketpair(int domain, int type, int protocol, 100 curl_socket_t socket_vector[2], 101 int line, const char *source); 102 #endif 103 104 /* send/receive sockets */ 105 CURL_EXTERN SEND_TYPE_RETV curl_dbg_send(SEND_TYPE_ARG1 sockfd, 106 SEND_QUAL_ARG2 SEND_TYPE_ARG2 buf, 107 SEND_TYPE_ARG3 len, 108 SEND_TYPE_ARG4 flags, int line, 109 const char *source); 110 CURL_EXTERN RECV_TYPE_RETV curl_dbg_recv(RECV_TYPE_ARG1 sockfd, 111 RECV_TYPE_ARG2 buf, 112 RECV_TYPE_ARG3 len, 113 RECV_TYPE_ARG4 flags, int line, 114 const char *source); 115 116 /* FILE functions */ 117 CURL_EXTERN int curl_dbg_fclose(FILE *file, int line, const char *source); 118 CURL_EXTERN ALLOC_FUNC 119 FILE *curl_dbg_fopen(const char *file, const char *mode, 120 int line, const char *source); 121 CURL_EXTERN ALLOC_FUNC 122 FILE *curl_dbg_fdopen(int filedes, const char *mode, 123 int line, const char *source); 124 125 #endif /* HEADER_CURL_MEMDEBUG_H_EXTERNS */ 126 127 /* Set this symbol on the command-line, recompile all lib-sources */ 128 #undef strdup 129 #define strdup(ptr) curl_dbg_strdup(ptr, __LINE__, __FILE__) 130 #undef malloc 131 #define malloc(size) curl_dbg_malloc(size, __LINE__, __FILE__) 132 #undef calloc 133 #define calloc(nbelem,size) curl_dbg_calloc(nbelem, size, __LINE__, __FILE__) 134 #undef realloc 135 #define realloc(ptr,size) curl_dbg_realloc(ptr, size, __LINE__, __FILE__) 136 #undef free 137 #define free(ptr) curl_dbg_free(ptr, __LINE__, __FILE__) 138 #undef send 139 #define send(a,b,c,d) curl_dbg_send(a,b,c,d, __LINE__, __FILE__) 140 #undef recv 141 #define recv(a,b,c,d) curl_dbg_recv(a,b,c,d, __LINE__, __FILE__) 142 143 #ifdef _WIN32 144 #undef _tcsdup 145 #ifdef UNICODE 146 #define _tcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__) 147 #else 148 #define _tcsdup(ptr) curl_dbg_strdup(ptr, __LINE__, __FILE__) 149 #endif 150 #endif /* _WIN32 */ 151 152 #undef socket 153 #define socket(domain,type,protocol) \ 154 curl_dbg_socket((int)domain, type, protocol, __LINE__, __FILE__) 155 #undef accept /* for those with accept as a macro */ 156 #define accept(sock,addr,len) \ 157 curl_dbg_accept(sock, addr, len, __LINE__, __FILE__) 158 #ifdef HAVE_ACCEPT4 159 #undef accept4 /* for those with accept4 as a macro */ 160 #define accept4(sock,addr,len,flags) \ 161 curl_dbg_accept4(sock, addr, len, flags, __LINE__, __FILE__) 162 #endif 163 #ifdef HAVE_SOCKETPAIR 164 #define socketpair(domain,type,protocol,socket_vector) \ 165 curl_dbg_socketpair((int)domain, type, protocol, socket_vector, \ 166 __LINE__, __FILE__) 167 #endif 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 #undef fclose 180 #define fclose(file) curl_dbg_fclose(file,__LINE__,__FILE__) 181 182 #endif /* CURLDEBUG */ 183 184 /* 185 ** Following section applies even when CURLDEBUG is not defined. 186 */ 187 188 #ifndef fake_sclose 189 #define fake_sclose(x) Curl_nop_stmt 190 #endif 191 192 /* 193 * Curl_safefree defined as a macro to allow MemoryTracking feature 194 * to log free() calls at same location where Curl_safefree is used. 195 * This macro also assigns NULL to given pointer when free'd. 196 */ 197 198 #define Curl_safefree(ptr) \ 199 do { free((ptr)); (ptr) = NULL;} while(0) 200 201 #endif /* HEADER_CURL_MEMDEBUG_H */