memanalyze.pl 11 KB

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