memanalyze.pl 12 KB

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