memanalyze.pl 12 KB

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