113498266Sopenharmony_ci#ifndef HEADER_CURL_MEMDEBUG_H 213498266Sopenharmony_ci#define HEADER_CURL_MEMDEBUG_H 313498266Sopenharmony_ci#ifdef CURLDEBUG 413498266Sopenharmony_ci/*************************************************************************** 513498266Sopenharmony_ci * _ _ ____ _ 613498266Sopenharmony_ci * Project ___| | | | _ \| | 713498266Sopenharmony_ci * / __| | | | |_) | | 813498266Sopenharmony_ci * | (__| |_| | _ <| |___ 913498266Sopenharmony_ci * \___|\___/|_| \_\_____| 1013498266Sopenharmony_ci * 1113498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 1213498266Sopenharmony_ci * 1313498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1413498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1513498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1613498266Sopenharmony_ci * 1713498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1813498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1913498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 2213498266Sopenharmony_ci * KIND, either express or implied. 2313498266Sopenharmony_ci * 2413498266Sopenharmony_ci * SPDX-License-Identifier: curl 2513498266Sopenharmony_ci * 2613498266Sopenharmony_ci ***************************************************************************/ 2713498266Sopenharmony_ci 2813498266Sopenharmony_ci/* 2913498266Sopenharmony_ci * CAUTION: this header is designed to work when included by the app-side 3013498266Sopenharmony_ci * as well as the library. Do not mix with library internals! 3113498266Sopenharmony_ci */ 3213498266Sopenharmony_ci 3313498266Sopenharmony_ci#include <curl/curl.h> 3413498266Sopenharmony_ci#include "functypes.h" 3513498266Sopenharmony_ci 3613498266Sopenharmony_ci#if defined(__GNUC__) && __GNUC__ >= 3 3713498266Sopenharmony_ci# define ALLOC_FUNC __attribute__((malloc)) 3813498266Sopenharmony_ci# define ALLOC_SIZE(s) __attribute__((alloc_size(s))) 3913498266Sopenharmony_ci# define ALLOC_SIZE2(n, s) __attribute__((alloc_size(n, s))) 4013498266Sopenharmony_ci#elif defined(_MSC_VER) 4113498266Sopenharmony_ci# define ALLOC_FUNC __declspec(restrict) 4213498266Sopenharmony_ci# define ALLOC_SIZE(s) 4313498266Sopenharmony_ci# define ALLOC_SIZE2(n, s) 4413498266Sopenharmony_ci#else 4513498266Sopenharmony_ci# define ALLOC_FUNC 4613498266Sopenharmony_ci# define ALLOC_SIZE(s) 4713498266Sopenharmony_ci# define ALLOC_SIZE2(n, s) 4813498266Sopenharmony_ci#endif 4913498266Sopenharmony_ci 5013498266Sopenharmony_ci#define CURL_MT_LOGFNAME_BUFSIZE 512 5113498266Sopenharmony_ci 5213498266Sopenharmony_ciextern FILE *curl_dbg_logfile; 5313498266Sopenharmony_ci 5413498266Sopenharmony_ci/* memory functions */ 5513498266Sopenharmony_ciCURL_EXTERN ALLOC_FUNC ALLOC_SIZE(1) void *curl_dbg_malloc(size_t size, 5613498266Sopenharmony_ci int line, 5713498266Sopenharmony_ci const char *source); 5813498266Sopenharmony_ciCURL_EXTERN ALLOC_FUNC ALLOC_SIZE2(1, 2) void *curl_dbg_calloc(size_t elements, 5913498266Sopenharmony_ci size_t size, int line, const char *source); 6013498266Sopenharmony_ciCURL_EXTERN ALLOC_SIZE(2) void *curl_dbg_realloc(void *ptr, 6113498266Sopenharmony_ci size_t size, 6213498266Sopenharmony_ci int line, 6313498266Sopenharmony_ci const char *source); 6413498266Sopenharmony_ciCURL_EXTERN void curl_dbg_free(void *ptr, int line, const char *source); 6513498266Sopenharmony_ciCURL_EXTERN ALLOC_FUNC char *curl_dbg_strdup(const char *str, int line, 6613498266Sopenharmony_ci const char *src); 6713498266Sopenharmony_ci#if defined(_WIN32) && defined(UNICODE) 6813498266Sopenharmony_ciCURL_EXTERN ALLOC_FUNC wchar_t *curl_dbg_wcsdup(const wchar_t *str, 6913498266Sopenharmony_ci int line, 7013498266Sopenharmony_ci const char *source); 7113498266Sopenharmony_ci#endif 7213498266Sopenharmony_ci 7313498266Sopenharmony_ciCURL_EXTERN void curl_dbg_memdebug(const char *logname); 7413498266Sopenharmony_ciCURL_EXTERN void curl_dbg_memlimit(long limit); 7513498266Sopenharmony_ciCURL_EXTERN void curl_dbg_log(const char *format, ...) CURL_PRINTF(1, 2); 7613498266Sopenharmony_ci 7713498266Sopenharmony_ci/* file descriptor manipulators */ 7813498266Sopenharmony_ciCURL_EXTERN curl_socket_t curl_dbg_socket(int domain, int type, int protocol, 7913498266Sopenharmony_ci int line, const char *source); 8013498266Sopenharmony_ciCURL_EXTERN void curl_dbg_mark_sclose(curl_socket_t sockfd, 8113498266Sopenharmony_ci int line, const char *source); 8213498266Sopenharmony_ciCURL_EXTERN int curl_dbg_sclose(curl_socket_t sockfd, 8313498266Sopenharmony_ci int line, const char *source); 8413498266Sopenharmony_ciCURL_EXTERN curl_socket_t curl_dbg_accept(curl_socket_t s, void *a, void *alen, 8513498266Sopenharmony_ci int line, const char *source); 8613498266Sopenharmony_ci#ifdef HAVE_SOCKETPAIR 8713498266Sopenharmony_ciCURL_EXTERN int curl_dbg_socketpair(int domain, int type, int protocol, 8813498266Sopenharmony_ci curl_socket_t socket_vector[2], 8913498266Sopenharmony_ci int line, const char *source); 9013498266Sopenharmony_ci#endif 9113498266Sopenharmony_ci 9213498266Sopenharmony_ci/* send/receive sockets */ 9313498266Sopenharmony_ciCURL_EXTERN SEND_TYPE_RETV curl_dbg_send(SEND_TYPE_ARG1 sockfd, 9413498266Sopenharmony_ci SEND_QUAL_ARG2 SEND_TYPE_ARG2 buf, 9513498266Sopenharmony_ci SEND_TYPE_ARG3 len, 9613498266Sopenharmony_ci SEND_TYPE_ARG4 flags, int line, 9713498266Sopenharmony_ci const char *source); 9813498266Sopenharmony_ciCURL_EXTERN RECV_TYPE_RETV curl_dbg_recv(RECV_TYPE_ARG1 sockfd, 9913498266Sopenharmony_ci RECV_TYPE_ARG2 buf, 10013498266Sopenharmony_ci RECV_TYPE_ARG3 len, 10113498266Sopenharmony_ci RECV_TYPE_ARG4 flags, int line, 10213498266Sopenharmony_ci const char *source); 10313498266Sopenharmony_ci 10413498266Sopenharmony_ci/* FILE functions */ 10513498266Sopenharmony_ciCURL_EXTERN ALLOC_FUNC FILE *curl_dbg_fopen(const char *file, const char *mode, 10613498266Sopenharmony_ci int line, const char *source); 10713498266Sopenharmony_ciCURL_EXTERN ALLOC_FUNC FILE *curl_dbg_fdopen(int filedes, const char *mode, 10813498266Sopenharmony_ci int line, const char *source); 10913498266Sopenharmony_ci 11013498266Sopenharmony_ciCURL_EXTERN int curl_dbg_fclose(FILE *file, int line, const char *source); 11113498266Sopenharmony_ci 11213498266Sopenharmony_ci#ifndef MEMDEBUG_NODEFINES 11313498266Sopenharmony_ci 11413498266Sopenharmony_ci/* Set this symbol on the command-line, recompile all lib-sources */ 11513498266Sopenharmony_ci#undef strdup 11613498266Sopenharmony_ci#define strdup(ptr) curl_dbg_strdup(ptr, __LINE__, __FILE__) 11713498266Sopenharmony_ci#define malloc(size) curl_dbg_malloc(size, __LINE__, __FILE__) 11813498266Sopenharmony_ci#define calloc(nbelem,size) curl_dbg_calloc(nbelem, size, __LINE__, __FILE__) 11913498266Sopenharmony_ci#define realloc(ptr,size) curl_dbg_realloc(ptr, size, __LINE__, __FILE__) 12013498266Sopenharmony_ci#define free(ptr) curl_dbg_free(ptr, __LINE__, __FILE__) 12113498266Sopenharmony_ci#define send(a,b,c,d) curl_dbg_send(a,b,c,d, __LINE__, __FILE__) 12213498266Sopenharmony_ci#define recv(a,b,c,d) curl_dbg_recv(a,b,c,d, __LINE__, __FILE__) 12313498266Sopenharmony_ci 12413498266Sopenharmony_ci#ifdef _WIN32 12513498266Sopenharmony_ci# ifdef UNICODE 12613498266Sopenharmony_ci# undef wcsdup 12713498266Sopenharmony_ci# define wcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__) 12813498266Sopenharmony_ci# undef _wcsdup 12913498266Sopenharmony_ci# define _wcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__) 13013498266Sopenharmony_ci# undef _tcsdup 13113498266Sopenharmony_ci# define _tcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__) 13213498266Sopenharmony_ci# else 13313498266Sopenharmony_ci# undef _tcsdup 13413498266Sopenharmony_ci# define _tcsdup(ptr) curl_dbg_strdup(ptr, __LINE__, __FILE__) 13513498266Sopenharmony_ci# endif 13613498266Sopenharmony_ci#endif 13713498266Sopenharmony_ci 13813498266Sopenharmony_ci#undef socket 13913498266Sopenharmony_ci#define socket(domain,type,protocol)\ 14013498266Sopenharmony_ci curl_dbg_socket(domain, type, protocol, __LINE__, __FILE__) 14113498266Sopenharmony_ci#undef accept /* for those with accept as a macro */ 14213498266Sopenharmony_ci#define accept(sock,addr,len)\ 14313498266Sopenharmony_ci curl_dbg_accept(sock, addr, len, __LINE__, __FILE__) 14413498266Sopenharmony_ci#ifdef HAVE_SOCKETPAIR 14513498266Sopenharmony_ci#define socketpair(domain,type,protocol,socket_vector)\ 14613498266Sopenharmony_ci curl_dbg_socketpair(domain, type, protocol, socket_vector, __LINE__, __FILE__) 14713498266Sopenharmony_ci#endif 14813498266Sopenharmony_ci 14913498266Sopenharmony_ci#ifdef HAVE_GETADDRINFO 15013498266Sopenharmony_ci#if defined(getaddrinfo) && defined(__osf__) 15113498266Sopenharmony_ci/* OSF/1 and Tru64 have getaddrinfo as a define already, so we cannot define 15213498266Sopenharmony_ci our macro as for other platforms. Instead, we redefine the new name they 15313498266Sopenharmony_ci define getaddrinfo to become! */ 15413498266Sopenharmony_ci#define ogetaddrinfo(host,serv,hint,res) \ 15513498266Sopenharmony_ci curl_dbg_getaddrinfo(host, serv, hint, res, __LINE__, __FILE__) 15613498266Sopenharmony_ci#else 15713498266Sopenharmony_ci#undef getaddrinfo 15813498266Sopenharmony_ci#define getaddrinfo(host,serv,hint,res) \ 15913498266Sopenharmony_ci curl_dbg_getaddrinfo(host, serv, hint, res, __LINE__, __FILE__) 16013498266Sopenharmony_ci#endif 16113498266Sopenharmony_ci#endif /* HAVE_GETADDRINFO */ 16213498266Sopenharmony_ci 16313498266Sopenharmony_ci#ifdef HAVE_FREEADDRINFO 16413498266Sopenharmony_ci#undef freeaddrinfo 16513498266Sopenharmony_ci#define freeaddrinfo(data) \ 16613498266Sopenharmony_ci curl_dbg_freeaddrinfo(data, __LINE__, __FILE__) 16713498266Sopenharmony_ci#endif /* HAVE_FREEADDRINFO */ 16813498266Sopenharmony_ci 16913498266Sopenharmony_ci/* sclose is probably already defined, redefine it! */ 17013498266Sopenharmony_ci#undef sclose 17113498266Sopenharmony_ci#define sclose(sockfd) curl_dbg_sclose(sockfd,__LINE__,__FILE__) 17213498266Sopenharmony_ci 17313498266Sopenharmony_ci#define fake_sclose(sockfd) curl_dbg_mark_sclose(sockfd,__LINE__,__FILE__) 17413498266Sopenharmony_ci 17513498266Sopenharmony_ci#undef fopen 17613498266Sopenharmony_ci#define fopen(file,mode) curl_dbg_fopen(file,mode,__LINE__,__FILE__) 17713498266Sopenharmony_ci#undef fdopen 17813498266Sopenharmony_ci#define fdopen(file,mode) curl_dbg_fdopen(file,mode,__LINE__,__FILE__) 17913498266Sopenharmony_ci#define fclose(file) curl_dbg_fclose(file,__LINE__,__FILE__) 18013498266Sopenharmony_ci 18113498266Sopenharmony_ci#endif /* MEMDEBUG_NODEFINES */ 18213498266Sopenharmony_ci 18313498266Sopenharmony_ci#endif /* CURLDEBUG */ 18413498266Sopenharmony_ci 18513498266Sopenharmony_ci/* 18613498266Sopenharmony_ci** Following section applies even when CURLDEBUG is not defined. 18713498266Sopenharmony_ci*/ 18813498266Sopenharmony_ci 18913498266Sopenharmony_ci#ifndef fake_sclose 19013498266Sopenharmony_ci#define fake_sclose(x) Curl_nop_stmt 19113498266Sopenharmony_ci#endif 19213498266Sopenharmony_ci 19313498266Sopenharmony_ci/* 19413498266Sopenharmony_ci * Curl_safefree defined as a macro to allow MemoryTracking feature 19513498266Sopenharmony_ci * to log free() calls at same location where Curl_safefree is used. 19613498266Sopenharmony_ci * This macro also assigns NULL to given pointer when free'd. 19713498266Sopenharmony_ci */ 19813498266Sopenharmony_ci 19913498266Sopenharmony_ci#define Curl_safefree(ptr) \ 20013498266Sopenharmony_ci do { free((ptr)); (ptr) = NULL;} while(0) 20113498266Sopenharmony_ci 20213498266Sopenharmony_ci#endif /* HEADER_CURL_MEMDEBUG_H */ 203