memanalyze.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 https://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\((\(nil\)|0x([0-9a-f]*))/) {
  100. $addr = $2;
  101. if($1 eq "(nil)") {
  102. ; # do nothing when free(NULL)
  103. }
  104. elsif(!exists $sizeataddr{$addr}) {
  105. print "FREE ERROR: No memory allocated: $line\n";
  106. }
  107. elsif(-1 == $sizeataddr{$addr}) {
  108. print "FREE ERROR: Memory freed twice: $line\n";
  109. print "FREE ERROR: Previously freed at: ".$getmem{$addr}."\n";
  110. }
  111. else {
  112. $totalmem -= $sizeataddr{$addr};
  113. if($trace) {
  114. print "FREE: malloc at ".$getmem{$addr}." is freed again at $source:$linenum\n";
  115. printf("FREE: %d bytes freed, left allocated: $totalmem bytes\n", $sizeataddr{$addr});
  116. }
  117. newtotal($totalmem);
  118. $frees++;
  119. $sizeataddr{$addr}=-1; # set -1 to mark as freed
  120. $getmem{$addr}="$source:$linenum";
  121. }
  122. }
  123. elsif($function =~ /malloc\((\d*)\) = 0x([0-9a-f]*)/) {
  124. $size = $1;
  125. $addr = $2;
  126. if($sizeataddr{$addr}>0) {
  127. # this means weeeeeirdo
  128. print "Mixed debug compile ($source:$linenum at line $lnum), rebuild curl now\n";
  129. print "We think $sizeataddr{$addr} bytes are already allocated at that memory address: $addr!\n";
  130. }
  131. $sizeataddr{$addr}=$size;
  132. $totalmem += $size;
  133. if($trace) {
  134. print "MALLOC: malloc($size) at $source:$linenum",
  135. " makes totally $totalmem bytes\n";
  136. }
  137. newtotal($totalmem);
  138. $mallocs++;
  139. $getmem{$addr}="$source:$linenum";
  140. }
  141. elsif($function =~ /calloc\((\d*),(\d*)\) = 0x([0-9a-f]*)/) {
  142. $size = $1*$2;
  143. $addr = $3;
  144. $arg1 = $1;
  145. $arg2 = $2;
  146. if($sizeataddr{$addr}>0) {
  147. # this means weeeeeirdo
  148. print "Mixed debug compile, rebuild curl now\n";
  149. }
  150. $sizeataddr{$addr}=$size;
  151. $totalmem += $size;
  152. if($trace) {
  153. print "CALLOC: calloc($arg1,$arg2) at $source:$linenum",
  154. " makes totally $totalmem bytes\n";
  155. }
  156. newtotal($totalmem);
  157. $callocs++;
  158. $getmem{$addr}="$source:$linenum";
  159. }
  160. elsif($function =~ /realloc\((\(nil\)|0x([0-9a-f]*)), (\d*)\) = 0x([0-9a-f]*)/) {
  161. my ($oldaddr, $newsize, $newaddr) = ($2, $3, $4);
  162. $totalmem -= $sizeataddr{$oldaddr};
  163. if($trace) {
  164. printf("REALLOC: %d less bytes and ", $sizeataddr{$oldaddr});
  165. }
  166. $sizeataddr{$oldaddr}=0;
  167. $totalmem += $newsize;
  168. $sizeataddr{$newaddr}=$newsize;
  169. if($trace) {
  170. printf("%d more bytes ($source:$linenum)\n", $newsize);
  171. }
  172. newtotal($totalmem);
  173. $reallocs++;
  174. $getmem{$oldaddr}="";
  175. $getmem{$newaddr}="$source:$linenum";
  176. }
  177. elsif($function =~ /strdup\(0x([0-9a-f]*)\) \((\d*)\) = 0x([0-9a-f]*)/) {
  178. # strdup(a5b50) (8) = df7c0
  179. $dup = $1;
  180. $size = $2;
  181. $addr = $3;
  182. $getmem{$addr}="$source:$linenum";
  183. $sizeataddr{$addr}=$size;
  184. $totalmem += $size;
  185. if($trace) {
  186. printf("STRDUP: $size bytes at %s, makes totally: %d bytes\n",
  187. $getmem{$addr}, $totalmem);
  188. }
  189. newtotal($totalmem);
  190. $strdups++;
  191. }
  192. elsif($function =~ /wcsdup\(0x([0-9a-f]*)\) \((\d*)\) = 0x([0-9a-f]*)/) {
  193. # wcsdup(a5b50) (8) = df7c0
  194. $dup = $1;
  195. $size = $2;
  196. $addr = $3;
  197. $getmem{$addr}="$source:$linenum";
  198. $sizeataddr{$addr}=$size;
  199. $totalmem += $size;
  200. if($trace) {
  201. printf("WCSDUP: $size bytes at %s, makes totally: %d bytes\n",
  202. $getmem{$addr}, $totalmem);
  203. }
  204. newtotal($totalmem);
  205. $wcsdups++;
  206. }
  207. else {
  208. print "Not recognized input line: $function\n";
  209. }
  210. }
  211. # FD url.c:1282 socket() = 5
  212. elsif($_ =~ /^FD ([^ ]*):(\d*) (.*)/) {
  213. # generic match for the filename+linenumber
  214. $source = $1;
  215. $linenum = $2;
  216. $function = $3;
  217. if($function =~ /socket\(\) = (\d*)/) {
  218. $filedes{$1}=1;
  219. $getfile{$1}="$source:$linenum";
  220. $openfile++;
  221. }
  222. elsif($function =~ /socketpair\(\) = (\d*) (\d*)/) {
  223. $filedes{$1}=1;
  224. $getfile{$1}="$source:$linenum";
  225. $openfile++;
  226. $filedes{$2}=1;
  227. $getfile{$2}="$source:$linenum";
  228. $openfile++;
  229. }
  230. elsif($function =~ /accept\(\) = (\d*)/) {
  231. $filedes{$1}=1;
  232. $getfile{$1}="$source:$linenum";
  233. $openfile++;
  234. }
  235. elsif($function =~ /sclose\((\d*)\)/) {
  236. if($filedes{$1} != 1) {
  237. print "Close without open: $line\n";
  238. }
  239. else {
  240. $filedes{$1}=0; # closed now
  241. $openfile--;
  242. }
  243. }
  244. }
  245. # FILE url.c:1282 fopen("blabla") = 0x5ddd
  246. elsif($_ =~ /^FILE ([^ ]*):(\d*) (.*)/) {
  247. # generic match for the filename+linenumber
  248. $source = $1;
  249. $linenum = $2;
  250. $function = $3;
  251. if($function =~ /f[d]*open\(\"(.*)\",\"([^\"]*)\"\) = (\(nil\)|0x([0-9a-f]*))/) {
  252. if($3 eq "(nil)") {
  253. ;
  254. }
  255. else {
  256. $fopen{$4}=1;
  257. $fopenfile{$4}="$source:$linenum";
  258. $fopens++;
  259. }
  260. }
  261. # fclose(0x1026c8)
  262. elsif($function =~ /fclose\(0x([0-9a-f]*)\)/) {
  263. if(!$fopen{$1}) {
  264. print "fclose() without fopen(): $line\n";
  265. }
  266. else {
  267. $fopen{$1}=0;
  268. $fopens--;
  269. }
  270. }
  271. }
  272. # GETNAME url.c:1901 getnameinfo()
  273. elsif($_ =~ /^GETNAME ([^ ]*):(\d*) (.*)/) {
  274. # not much to do
  275. }
  276. # ADDR url.c:1282 getaddrinfo() = 0x5ddd
  277. elsif($_ =~ /^ADDR ([^ ]*):(\d*) (.*)/) {
  278. # generic match for the filename+linenumber
  279. $source = $1;
  280. $linenum = $2;
  281. $function = $3;
  282. if($function =~ /getaddrinfo\(\) = (\(nil\)|0x([0-9a-f]*))/) {
  283. my $add = $2;
  284. if($add eq "(nil)") {
  285. ;
  286. }
  287. else {
  288. $addrinfo{$add}=1;
  289. $addrinfofile{$add}="$source:$linenum";
  290. $addrinfos++;
  291. }
  292. if($trace) {
  293. printf("GETADDRINFO ($source:$linenum)\n");
  294. }
  295. }
  296. # fclose(0x1026c8)
  297. elsif($function =~ /freeaddrinfo\(0x([0-9a-f]*)\)/) {
  298. if(!$addrinfo{$1}) {
  299. print "freeaddrinfo() without getaddrinfo(): $line\n";
  300. }
  301. else {
  302. $addrinfo{$1}=0;
  303. $addrinfos--;
  304. }
  305. if($trace) {
  306. printf("FREEADDRINFO ($source:$linenum)\n");
  307. }
  308. }
  309. }
  310. else {
  311. print "Not recognized prefix line: $line\n";
  312. }
  313. }
  314. close(FILE);
  315. if($totalmem) {
  316. print "Leak detected: memory still allocated: $totalmem bytes\n";
  317. for(keys %sizeataddr) {
  318. $addr = $_;
  319. $size = $sizeataddr{$addr};
  320. if($size > 0) {
  321. print "At $addr, there's $size bytes.\n";
  322. print " allocated by ".$getmem{$addr}."\n";
  323. }
  324. }
  325. }
  326. if($openfile) {
  327. for(keys %filedes) {
  328. if($filedes{$_} == 1) {
  329. print "Open file descriptor created at ".$getfile{$_}."\n";
  330. }
  331. }
  332. }
  333. if($fopens) {
  334. print "Open FILE handles left at:\n";
  335. for(keys %fopen) {
  336. if($fopen{$_} == 1) {
  337. print "fopen() called at ".$fopenfile{$_}."\n";
  338. }
  339. }
  340. }
  341. if($addrinfos) {
  342. print "IPv6-style name resolve data left at:\n";
  343. for(keys %addrinfofile) {
  344. if($addrinfo{$_} == 1) {
  345. print "getaddrinfo() called at ".$addrinfofile{$_}."\n";
  346. }
  347. }
  348. }
  349. if($verbose) {
  350. print "Mallocs: $mallocs\n",
  351. "Reallocs: $reallocs\n",
  352. "Callocs: $callocs\n",
  353. "Strdups: $strdups\n",
  354. "Wcsdups: $wcsdups\n",
  355. "Frees: $frees\n",
  356. "Allocations: ".($mallocs + $callocs + $reallocs + $strdups + $wcsdups)."\n";
  357. print "Maximum allocated: $maxmem\n";
  358. }