memdebug.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #ifdef MALLOCDEBUG
  2. /***************************************************************************
  3. * _ _ ____ _
  4. * Project ___| | | | _ \| |
  5. * / __| | | | |_) | |
  6. * | (__| |_| | _ <| |___
  7. * \___|\___/|_| \_\_____|
  8. *
  9. * Copyright (C) 1998 - 2002, Daniel Stenberg, <[email protected]>, et al.
  10. *
  11. * This software is licensed as described in the file COPYING, which
  12. * you should have received as part of this distribution. The terms
  13. * are also available at http://curl.haxx.se/docs/copyright.html.
  14. *
  15. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. * copies of the Software, and permit persons to whom the Software is
  17. * furnished to do so, under the terms of the COPYING file.
  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. double mem[1];
  46. /* I'm hoping this is the thing with the strictest alignment
  47. * requirements. That also means we waste some space :-( */
  48. };
  49. /*
  50. * Note that these debug functions are very simple and they are meant to
  51. * remain so. For advanced analysis, record a log file and write perl scripts
  52. * to analyze them!
  53. *
  54. * Don't use these with multithreaded test programs!
  55. */
  56. FILE *logfile;
  57. /* this sets the log file name */
  58. void curl_memdebug(const char *logname)
  59. {
  60. if(logname)
  61. logfile = fopen(logname, "w");
  62. else
  63. logfile = stderr;
  64. }
  65. void *curl_domalloc(size_t wantedsize, int line, const char *source)
  66. {
  67. struct memdebug *mem;
  68. size_t size;
  69. /* alloc at least 64 bytes */
  70. size = sizeof(struct memdebug)+wantedsize;
  71. mem=(struct memdebug *)(malloc)(size);
  72. if(mem) {
  73. /* fill memory with junk */
  74. memset(mem->mem, 0xA5, wantedsize);
  75. mem->size = wantedsize;
  76. }
  77. if(logfile && source)
  78. fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n",
  79. source, line, wantedsize, mem->mem);
  80. return mem->mem;
  81. }
  82. char *curl_dostrdup(const char *str, int line, const char *source)
  83. {
  84. char *mem;
  85. size_t len;
  86. if(NULL ==str) {
  87. fprintf(stderr, "ILLEGAL strdup() on NULL at %s:%d\n",
  88. source, line);
  89. exit(2);
  90. }
  91. len=strlen(str)+1;
  92. mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
  93. memcpy(mem, str, len);
  94. if(logfile)
  95. fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n",
  96. source, line, str, len, mem);
  97. return mem;
  98. }
  99. void *curl_dorealloc(void *ptr, size_t wantedsize,
  100. int line, const char *source)
  101. {
  102. struct memdebug *mem;
  103. size_t size = sizeof(struct memdebug)+wantedsize;
  104. mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
  105. mem=(struct memdebug *)(realloc)(mem, size);
  106. if(logfile)
  107. fprintf(logfile, "MEM %s:%d realloc(%p, %d) = %p\n",
  108. source, line, ptr, wantedsize, mem?mem->mem:NULL);
  109. if(mem) {
  110. mem->size = wantedsize;
  111. return mem->mem;
  112. }
  113. return NULL;
  114. }
  115. void curl_dofree(void *ptr, int line, const char *source)
  116. {
  117. struct memdebug *mem;
  118. if(NULL == ptr) {
  119. fprintf(stderr, "ILLEGAL free() on NULL at %s:%d\n",
  120. source, line);
  121. exit(2);
  122. }
  123. mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
  124. /* destroy */
  125. memset(mem->mem, 0x13, mem->size);
  126. /* free for real */
  127. (free)(mem);
  128. if(logfile)
  129. fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
  130. }
  131. int curl_socket(int domain, int type, int protocol, int line, char *source)
  132. {
  133. int sockfd=(socket)(domain, type, protocol);
  134. if(logfile)
  135. fprintf(logfile, "FD %s:%d socket() = %d\n",
  136. source, line, sockfd);
  137. return sockfd;
  138. }
  139. int curl_accept(int s, struct sockaddr *addr, socklen_t *addrlen,
  140. int line, const char *source)
  141. {
  142. int sockfd=(accept)(s, addr, addrlen);
  143. if(logfile)
  144. fprintf(logfile, "FD %s:%d accept() = %d\n",
  145. source, line, sockfd);
  146. return sockfd;
  147. }
  148. /* this is our own defined way to close sockets on *ALL* platforms */
  149. int curl_sclose(int sockfd, int line, char *source)
  150. {
  151. int res=sclose(sockfd);
  152. if(logfile)
  153. fprintf(logfile, "FD %s:%d sclose(%d)\n",
  154. source, line, sockfd);
  155. return res;
  156. }
  157. FILE *curl_fopen(const char *file, const char *mode,
  158. int line, const char *source)
  159. {
  160. FILE *res=(fopen)(file, mode);
  161. if(logfile)
  162. fprintf(logfile, "FILE %s:%d fopen(\"%s\") = %p\n",
  163. source, line, file, res);
  164. return res;
  165. }
  166. int curl_fclose(FILE *file, int line, const char *source)
  167. {
  168. int res;
  169. if(NULL == file) {
  170. fprintf(stderr, "ILLEGAL flose() on NULL at %s:%d\n",
  171. source, line);
  172. exit(2);
  173. }
  174. res=(fclose)(file);
  175. if(logfile)
  176. fprintf(logfile, "FILE %s:%d fclose(%p)\n",
  177. source, line, file);
  178. return res;
  179. }
  180. #else
  181. #ifdef VMS
  182. int VOID_VAR_MEMDEBUG;
  183. #endif
  184. #endif /* MALLOCDEBUG */
  185. /*
  186. * local variables:
  187. * eval: (load-file "../curl-mode.el")
  188. * end:
  189. * vim600: fdm=marker
  190. * vim: et sw=2 ts=2 sts=2 tw=78
  191. */