memdebug.h 6.9 KB

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