memdebug.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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, <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, ...);
  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. #define malloc(size) curl_dbg_malloc(size, __LINE__, __FILE__)
  105. #define calloc(nbelem,size) curl_dbg_calloc(nbelem, size, __LINE__, __FILE__)
  106. #define realloc(ptr,size) curl_dbg_realloc(ptr, size, __LINE__, __FILE__)
  107. #define free(ptr) curl_dbg_free(ptr, __LINE__, __FILE__)
  108. #define send(a,b,c,d) curl_dbg_send(a,b,c,d, __LINE__, __FILE__)
  109. #define recv(a,b,c,d) curl_dbg_recv(a,b,c,d, __LINE__, __FILE__)
  110. #ifdef WIN32
  111. # ifdef UNICODE
  112. # undef wcsdup
  113. # define wcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__)
  114. # undef _wcsdup
  115. # define _wcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__)
  116. # undef _tcsdup
  117. # define _tcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__)
  118. # else
  119. # undef _tcsdup
  120. # define _tcsdup(ptr) curl_dbg_strdup(ptr, __LINE__, __FILE__)
  121. # endif
  122. #endif
  123. #undef socket
  124. #define socket(domain,type,protocol)\
  125. curl_dbg_socket(domain, type, protocol, __LINE__, __FILE__)
  126. #undef accept /* for those with accept as a macro */
  127. #define accept(sock,addr,len)\
  128. curl_dbg_accept(sock, addr, len, __LINE__, __FILE__)
  129. #ifdef HAVE_SOCKETPAIR
  130. #define socketpair(domain,type,protocol,socket_vector)\
  131. curl_dbg_socketpair(domain, type, protocol, socket_vector, __LINE__, __FILE__)
  132. #endif
  133. #ifdef HAVE_GETADDRINFO
  134. #if defined(getaddrinfo) && defined(__osf__)
  135. /* OSF/1 and Tru64 have getaddrinfo as a define already, so we cannot define
  136. our macro as for other platforms. Instead, we redefine the new name they
  137. define getaddrinfo to become! */
  138. #define ogetaddrinfo(host,serv,hint,res) \
  139. curl_dbg_getaddrinfo(host, serv, hint, res, __LINE__, __FILE__)
  140. #else
  141. #undef getaddrinfo
  142. #define getaddrinfo(host,serv,hint,res) \
  143. curl_dbg_getaddrinfo(host, serv, hint, res, __LINE__, __FILE__)
  144. #endif
  145. #endif /* HAVE_GETADDRINFO */
  146. #ifdef HAVE_FREEADDRINFO
  147. #undef freeaddrinfo
  148. #define freeaddrinfo(data) \
  149. curl_dbg_freeaddrinfo(data, __LINE__, __FILE__)
  150. #endif /* HAVE_FREEADDRINFO */
  151. /* sclose is probably already defined, redefine it! */
  152. #undef sclose
  153. #define sclose(sockfd) curl_dbg_sclose(sockfd,__LINE__,__FILE__)
  154. #define fake_sclose(sockfd) curl_dbg_mark_sclose(sockfd,__LINE__,__FILE__)
  155. #undef fopen
  156. #define fopen(file,mode) curl_dbg_fopen(file,mode,__LINE__,__FILE__)
  157. #undef fdopen
  158. #define fdopen(file,mode) curl_dbg_fdopen(file,mode,__LINE__,__FILE__)
  159. #define fclose(file) curl_dbg_fclose(file,__LINE__,__FILE__)
  160. #endif /* MEMDEBUG_NODEFINES */
  161. #endif /* CURLDEBUG */
  162. /*
  163. ** Following section applies even when CURLDEBUG is not defined.
  164. */
  165. #ifndef fake_sclose
  166. #define fake_sclose(x) Curl_nop_stmt
  167. #endif
  168. /*
  169. * Curl_safefree defined as a macro to allow MemoryTracking feature
  170. * to log free() calls at same location where Curl_safefree is used.
  171. * This macro also assigns NULL to given pointer when free'd.
  172. */
  173. #define Curl_safefree(ptr) \
  174. do { free((ptr)); (ptr) = NULL;} while(0)
  175. #endif /* HEADER_CURL_MEMDEBUG_H */