memdebug.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #ifdef MALLOCDEBUG
  2. /*****************************************************************************
  3. * _ _ ____ _
  4. * Project ___| | | | _ \| |
  5. * / __| | | | |_) | |
  6. * | (__| |_| | _ <| |___
  7. * \___|\___/|_| \_\_____|
  8. *
  9. * Copyright (C) 2000, Daniel Stenberg, <[email protected]>, et al.
  10. *
  11. * In order to be useful for every potential user, curl and libcurl are
  12. * dual-licensed under the MPL and the MIT/X-derivate licenses.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the MPL or the MIT/X-derivate
  17. * licenses. You may pick one of these licenses.
  18. *
  19. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. * KIND, either express or implied.
  21. *
  22. * $Id$
  23. *****************************************************************************/
  24. #include "setup.h"
  25. #include <curl/curl.h>
  26. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  27. #include <winsock.h>
  28. #else /* some kind of unix */
  29. #ifdef HAVE_SYS_SOCKET_H
  30. #include <sys/socket.h>
  31. #endif
  32. #endif
  33. #define _MPRINTF_REPLACE
  34. #include <curl/mprintf.h>
  35. #include "urldata.h"
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #ifdef HAVE_UNISTD_H
  40. #include <unistd.h>
  41. #endif
  42. /* DONT include memdebug.h here! */
  43. struct memdebug {
  44. int size;
  45. char mem[1];
  46. };
  47. /*
  48. * Note that these debug functions are very simple and they are meant to
  49. * remain so. For advanced analysis, record a log file and write perl scripts
  50. * to analyze them!
  51. *
  52. * Don't use these with multithreaded test programs!
  53. */
  54. FILE *logfile;
  55. /* this sets the log file name */
  56. void curl_memdebug(const char *logname)
  57. {
  58. if(logname)
  59. logfile = fopen(logname, "w");
  60. else
  61. logfile = stderr;
  62. }
  63. void *curl_domalloc(size_t wantedsize, int line, const char *source)
  64. {
  65. struct memdebug *mem;
  66. size_t size;
  67. /* alloc at least 64 bytes */
  68. size = sizeof(struct memdebug)+wantedsize;
  69. mem=(struct memdebug *)(malloc)(size);
  70. if(mem) {
  71. /* fill memory with junk */
  72. memset(mem->mem, 0xA5, wantedsize);
  73. mem->size = wantedsize;
  74. }
  75. if(logfile && source)
  76. fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n",
  77. source, line, wantedsize, mem->mem);
  78. return mem->mem;
  79. }
  80. char *curl_dostrdup(const char *str, int line, const char *source)
  81. {
  82. char *mem;
  83. size_t len;
  84. if(NULL ==str) {
  85. fprintf(stderr, "ILLEGAL strdup() on NULL at %s:%d\n",
  86. source, line);
  87. exit(2);
  88. }
  89. len=strlen(str)+1;
  90. mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
  91. memcpy(mem, str, len);
  92. if(logfile)
  93. fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n",
  94. source, line, str, len, mem);
  95. return mem;
  96. }
  97. void *curl_dorealloc(void *ptr, size_t wantedsize,
  98. int line, const char *source)
  99. {
  100. struct memdebug *mem;
  101. size_t size = sizeof(struct memdebug)+wantedsize;
  102. mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
  103. mem=(struct memdebug *)(realloc)(mem, size);
  104. if(logfile)
  105. fprintf(logfile, "MEM %s:%d realloc(%p, %d) = %p\n",
  106. source, line, ptr, wantedsize, mem?mem->mem:NULL);
  107. if(mem) {
  108. mem->size = wantedsize;
  109. return mem->mem;
  110. }
  111. return NULL;
  112. }
  113. void curl_dofree(void *ptr, int line, const char *source)
  114. {
  115. struct memdebug *mem;
  116. if(NULL == ptr) {
  117. fprintf(stderr, "ILLEGAL free() on NULL at %s:%d\n",
  118. source, line);
  119. exit(2);
  120. }
  121. mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
  122. /* destroy */
  123. memset(mem->mem, 0x13, mem->size);
  124. /* free for real */
  125. (free)(mem);
  126. if(logfile)
  127. fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
  128. }
  129. int curl_socket(int domain, int type, int protocol, int line, char *source)
  130. {
  131. int sockfd=(socket)(domain, type, protocol);
  132. if(logfile)
  133. fprintf(logfile, "FD %s:%d socket() = %d\n",
  134. source, line, sockfd);
  135. return sockfd;
  136. }
  137. int curl_accept(int s, struct sockaddr *addr, socklen_t *addrlen,
  138. int line, const char *source)
  139. {
  140. int sockfd=(accept)(s, addr, addrlen);
  141. if(logfile)
  142. fprintf(logfile, "FD %s:%d accept() = %d\n",
  143. source, line, sockfd);
  144. return sockfd;
  145. }
  146. /* this is our own defined way to close sockets on *ALL* platforms */
  147. int curl_sclose(int sockfd, int line, char *source)
  148. {
  149. int res=sclose(sockfd);
  150. if(logfile)
  151. fprintf(logfile, "FD %s:%d sclose(%d)\n",
  152. source, line, sockfd);
  153. return res;
  154. }
  155. FILE *curl_fopen(const char *file, const char *mode,
  156. int line, const char *source)
  157. {
  158. FILE *res=(fopen)(file, mode);
  159. if(logfile)
  160. fprintf(logfile, "FILE %s:%d fopen(\"%s\") = %p\n",
  161. source, line, file, res);
  162. return res;
  163. }
  164. int curl_fclose(FILE *file, int line, const char *source)
  165. {
  166. int res=(fclose)(file);
  167. if(logfile)
  168. fprintf(logfile, "FILE %s:%d fclose(%p)\n",
  169. source, line, file);
  170. return res;
  171. }
  172. #else
  173. #ifdef VMS
  174. int VOID_VAR_MEMDEBUG;
  175. #endif
  176. #endif /* MALLOCDEBUG */
  177. /*
  178. * local variables:
  179. * eval: (load-file "../curl-mode.el")
  180. * end:
  181. * vim600: fdm=marker
  182. * vim: et sw=2 ts=2 sts=2 tw=78
  183. */