memdebug.h 8.0 KB

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