memanalyze.pl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, 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. ###########################################################################
  23. #
  24. # Example input:
  25. #
  26. # MEM mprintf.c:1094 malloc(32) = e5718
  27. # MEM mprintf.c:1103 realloc(e5718, 64) = e6118
  28. # MEM sendf.c:232 free(f6520)
  29. my $mallocs=0;
  30. my $callocs=0;
  31. my $reallocs=0;
  32. my $strdups=0;
  33. my $showlimit;
  34. while(1) {
  35. if($ARGV[0] eq "-v") {
  36. $verbose=1;
  37. shift @ARGV;
  38. }
  39. elsif($ARGV[0] eq "-t") {
  40. $trace=1;
  41. shift @ARGV;
  42. }
  43. elsif($ARGV[0] eq "-l") {
  44. # only show what alloc that caused a memlimit failure
  45. $showlimit=1;
  46. shift @ARGV;
  47. }
  48. else {
  49. last;
  50. }
  51. }
  52. my $maxmem;
  53. sub newtotal {
  54. my ($newtot)=@_;
  55. # count a max here
  56. if($newtot > $maxmem) {
  57. $maxmem= $newtot;
  58. }
  59. }
  60. my $file = $ARGV[0];
  61. if(! -f $file) {
  62. print "Usage: memanalyze.pl [options] <dump file>\n",
  63. "Options:\n",
  64. " -l memlimit failure displayed\n",
  65. " -v Verbose\n",
  66. " -t Trace\n";
  67. exit;
  68. }
  69. open(FILE, "<$file");
  70. if($showlimit) {
  71. while(<FILE>) {
  72. if(/^LIMIT.*memlimit$/) {
  73. print $_;
  74. last;
  75. }
  76. }
  77. close(FILE);
  78. exit;
  79. }
  80. my $lnum=0;
  81. while(<FILE>) {
  82. chomp $_;
  83. $line = $_;
  84. $lnum++;
  85. if($line =~ /^LIMIT ([^ ]*):(\d*) (.*)/) {
  86. # new memory limit test prefix
  87. my $i = $3;
  88. my ($source, $linenum) = ($1, $2);
  89. if($trace && ($i =~ /([^ ]*) reached memlimit/)) {
  90. print "LIMIT: $1 returned error at $source:$linenum\n";
  91. }
  92. }
  93. elsif($line =~ /^MEM ([^ ]*):(\d*) (.*)/) {
  94. # generic match for the filename+linenumber
  95. $source = $1;
  96. $linenum = $2;
  97. $function = $3;
  98. if($function =~ /free\(0x([0-9a-f]*)/) {
  99. $addr = $1;
  100. if(!exists $sizeataddr{$addr}) {
  101. print "FREE ERROR: No memory allocated: $line\n";
  102. }
  103. elsif(-1 == $sizeataddr{$addr}) {
  104. print "FREE ERROR: Memory freed twice: $line\n";
  105. print "FREE ERROR: Previously freed at: ".$getmem{$addr}."\n";
  106. }
  107. else {
  108. $totalmem -= $sizeataddr{$addr};
  109. if($trace) {
  110. print "FREE: malloc at ".$getmem{$addr}." is freed again at $source:$linenum\n";
  111. printf("FREE: %d bytes freed, left allocated: $totalmem bytes\n", $sizeataddr{$addr});
  112. }
  113. newtotal($totalmem);
  114. $frees++;
  115. $sizeataddr{$addr}=-1; # set -1 to mark as freed
  116. $getmem{$addr}="$source:$linenum";
  117. }
  118. }
  119. elsif($function =~ /malloc\((\d*)\) = 0x([0-9a-f]*)/) {
  120. $size = $1;
  121. $addr = $2;
  122. if($sizeataddr{$addr}>0) {
  123. # this means weeeeeirdo
  124. print "Mixed debug compile ($source:$linenum at line $lnum), rebuild curl now\n";
  125. print "We think $sizeataddr{$addr} bytes are already allocated at that memory address: $addr!\n";
  126. }
  127. $sizeataddr{$addr}=$size;
  128. $totalmem += $size;
  129. if($trace) {
  130. print "MALLOC: malloc($size) at $source:$linenum",
  131. " makes totally $totalmem bytes\n";
  132. }
  133. newtotal($totalmem);
  134. $mallocs++;
  135. $getmem{$addr}="$source:$linenum";
  136. }
  137. elsif($function =~ /calloc\((\d*),(\d*)\) = 0x([0-9a-f]*)/) {
  138. $size = $1*$2;
  139. $addr = $3;
  140. $arg1 = $1;
  141. $arg2 = $2;
  142. if($sizeataddr{$addr}>0) {
  143. # this means weeeeeirdo
  144. print "Mixed debug compile, rebuild curl now\n";
  145. }
  146. $sizeataddr{$addr}=$size;
  147. $totalmem += $size;
  148. if($trace) {
  149. print "CALLOC: calloc($arg1,$arg2) at $source:$linenum",
  150. " makes totally $totalmem bytes\n";
  151. }
  152. newtotal($totalmem);
  153. $callocs++;
  154. $getmem{$addr}="$source:$linenum";
  155. }
  156. elsif($function =~ /realloc\((\(nil\)|0x([0-9a-f]*)), (\d*)\) = 0x([0-9a-f]*)/) {
  157. my ($oldaddr, $newsize, $newaddr) = ($2, $3, $4);
  158. $totalmem -= $sizeataddr{$oldaddr};
  159. if($trace) {
  160. printf("REALLOC: %d less bytes and ", $sizeataddr{$oldaddr});
  161. }
  162. $sizeataddr{$oldaddr}=0;
  163. $totalmem += $newsize;
  164. $sizeataddr{$newaddr}=$newsize;
  165. if($trace) {
  166. printf("%d more bytes ($source:$linenum)\n", $newsize);
  167. }
  168. newtotal($totalmem);
  169. $reallocs++;
  170. $getmem{$oldaddr}="";
  171. $getmem{$newaddr}="$source:$linenum";
  172. }
  173. elsif($function =~ /strdup\(0x([0-9a-f]*)\) \((\d*)\) = 0x([0-9a-f]*)/) {
  174. # strdup(a5b50) (8) = df7c0
  175. $dup = $1;
  176. $size = $2;
  177. $addr = $3;
  178. $getmem{$addr}="$source:$linenum";
  179. $sizeataddr{$addr}=$size;
  180. $totalmem += $size;
  181. if($trace) {
  182. printf("STRDUP: $size bytes at %s, makes totally: %d bytes\n",
  183. $getmem{$addr}, $totalmem);
  184. }
  185. newtotal($totalmem);
  186. $strdups++;
  187. }
  188. else {
  189. print "Not recognized input line: $function\n";
  190. }
  191. }
  192. # FD url.c:1282 socket() = 5
  193. elsif($_ =~ /^FD ([^ ]*):(\d*) (.*)/) {
  194. # generic match for the filename+linenumber
  195. $source = $1;
  196. $linenum = $2;
  197. $function = $3;
  198. if($function =~ /socket\(\) = (\d*)/) {
  199. $filedes{$1}=1;
  200. $getfile{$1}="$source:$linenum";
  201. $openfile++;
  202. }
  203. elsif($function =~ /socketpair\(\) = (\d*) (\d*)/) {
  204. $filedes{$1}=1;
  205. $getfile{$1}="$source:$linenum";
  206. $openfile++;
  207. $filedes{$2}=1;
  208. $getfile{$2}="$source:$linenum";
  209. $openfile++;
  210. }
  211. elsif($function =~ /accept\(\) = (\d*)/) {
  212. $filedes{$1}=1;
  213. $getfile{$1}="$source:$linenum";
  214. $openfile++;
  215. }
  216. elsif($function =~ /sclose\((\d*)\)/) {
  217. if($filedes{$1} != 1) {
  218. print "Close without open: $line\n";
  219. }
  220. else {
  221. $filedes{$1}=0; # closed now
  222. $openfile--;
  223. }
  224. }
  225. }
  226. # FILE url.c:1282 fopen("blabla") = 0x5ddd
  227. elsif($_ =~ /^FILE ([^ ]*):(\d*) (.*)/) {
  228. # generic match for the filename+linenumber
  229. $source = $1;
  230. $linenum = $2;
  231. $function = $3;
  232. if($function =~ /f[d]*open\(\"([^\"]*)\",\"([^\"]*)\"\) = (\(nil\)|0x([0-9a-f]*))/) {
  233. if($3 eq "(nil)") {
  234. ;
  235. }
  236. else {
  237. $fopen{$4}=1;
  238. $fopenfile{$4}="$source:$linenum";
  239. $fopens++;
  240. }
  241. }
  242. # fclose(0x1026c8)
  243. elsif($function =~ /fclose\(0x([0-9a-f]*)\)/) {
  244. if(!$fopen{$1}) {
  245. print "fclose() without fopen(): $line\n";
  246. }
  247. else {
  248. $fopen{$1}=0;
  249. $fopens--;
  250. }
  251. }
  252. }
  253. # GETNAME url.c:1901 getnameinfo()
  254. elsif($_ =~ /^GETNAME ([^ ]*):(\d*) (.*)/) {
  255. # not much to do
  256. }
  257. # ADDR url.c:1282 getaddrinfo() = 0x5ddd
  258. elsif($_ =~ /^ADDR ([^ ]*):(\d*) (.*)/) {
  259. # generic match for the filename+linenumber
  260. $source = $1;
  261. $linenum = $2;
  262. $function = $3;
  263. if($function =~ /getaddrinfo\(\) = (\(nil\)|0x([0-9a-f]*))/) {
  264. my $add = $2;
  265. if($add eq "(nil)") {
  266. ;
  267. }
  268. else {
  269. $addrinfo{$add}=1;
  270. $addrinfofile{$add}="$source:$linenum";
  271. $addrinfos++;
  272. }
  273. if($trace) {
  274. printf("GETADDRINFO ($source:$linenum)\n");
  275. }
  276. }
  277. # fclose(0x1026c8)
  278. elsif($function =~ /freeaddrinfo\(0x([0-9a-f]*)\)/) {
  279. if(!$addrinfo{$1}) {
  280. print "freeaddrinfo() without getaddrinfo(): $line\n";
  281. }
  282. else {
  283. $addrinfo{$1}=0;
  284. $addrinfos--;
  285. }
  286. if($trace) {
  287. printf("FREEADDRINFO ($source:$linenum)\n");
  288. }
  289. }
  290. }
  291. else {
  292. print "Not recognized prefix line: $line\n";
  293. }
  294. }
  295. close(FILE);
  296. if($totalmem) {
  297. print "Leak detected: memory still allocated: $totalmem bytes\n";
  298. for(keys %sizeataddr) {
  299. $addr = $_;
  300. $size = $sizeataddr{$addr};
  301. if($size > 0) {
  302. print "At $addr, there's $size bytes.\n";
  303. print " allocated by ".$getmem{$addr}."\n";
  304. }
  305. }
  306. }
  307. if($openfile) {
  308. for(keys %filedes) {
  309. if($filedes{$_} == 1) {
  310. print "Open file descriptor created at ".$getfile{$_}."\n";
  311. }
  312. }
  313. }
  314. if($fopens) {
  315. print "Open FILE handles left at:\n";
  316. for(keys %fopen) {
  317. if($fopen{$_} == 1) {
  318. print "fopen() called at ".$fopenfile{$_}."\n";
  319. }
  320. }
  321. }
  322. if($addrinfos) {
  323. print "IPv6-style name resolve data left at:\n";
  324. for(keys %addrinfofile) {
  325. if($addrinfo{$_} == 1) {
  326. print "getaddrinfo() called at ".$addrinfofile{$_}."\n";
  327. }
  328. }
  329. }
  330. if($verbose) {
  331. print "Mallocs: $mallocs\n",
  332. "Reallocs: $reallocs\n",
  333. "Callocs: $callocs\n",
  334. "Strdups: $strdups\n",
  335. "Frees: $frees\n",
  336. "Allocations: ".($mallocs + $callocs + $reallocs + $strdups)."\n";
  337. print "Maximum allocated: $maxmem\n";
  338. }