Warning, /sdk/kde-dev-scripts/extend_dmalloc is written in an unsupported language. File is not indexed.

0001 #! /usr/bin/env perl
0002 #
0003 # script to run gdb on return-addresses
0004 # Usage: $0 malloc-log-file binary
0005 #
0006 # Copyright 1995 by Gray Watson
0007 #
0008 # This file is part of the dmalloc package.
0009 #
0010 # Permission to use, copy, modify, and distribute this software for
0011 # any purpose and without fee is hereby granted, provided that the
0012 # above copyright notice and this permission notice appear in all
0013 # copies, and that the name of Gray Watson not be used in advertising
0014 # or publicity pertaining to distribution of the document or software
0015 # without specific, written prior permission.
0016 #
0017 # Gray Watson makes no representations about the suitability of the
0018 # software described herein for any purpose.  It is provided "as is"
0019 # without express or implied warranty.
0020 #
0021 # The author may be contacted at gray.watson@letters.com
0022 #
0023 # $Id$
0024 #
0025 
0026 #
0027 # Use this Perl script to run gdb and get information on the return-address
0028 # (ra) addresses from a dmalloc logfile output.  This will search for
0029 # any ra= lines and will examine them and try to get the line number.
0030 #
0031 # NOTE: you may want to direct the output from the script to a file
0032 # else gdb seems to prompt for a return like you are on the end of a
0033 # page.
0034 #
0035 # Be sure to send me mail if there is an easier way to do all this.
0036 #
0037 
0038 ###############################################################################
0039 # usage message
0040 #
0041 if (@ARGV != 2 ) {
0042   die "Usage:  $0  dmalloc-log  binary-that-generated-log\n";
0043 }
0044 
0045 $malloc = @ARGV[0];
0046 $command = @ARGV[1];
0047 
0048 @addresses = ();
0049 
0050 open(malloc, $malloc);
0051 while ( <malloc> ) {
0052   if ($_ =~ m/ra=(0x[0-9a-fA-F]+)/) {
0053     push(@addresses, $1);
0054   }
0055 }
0056 close(malloc);
0057 open(SORT, "|sort -u > $malloc.tmp");
0058 
0059 foreach $address (@addresses) {
0060   print SORT "$address\n";
0061 }
0062 close(SORT);
0063 
0064 @addresses = ();
0065 
0066 open(SORT, "< $malloc.tmp");
0067 while ( <SORT> ) {
0068   chomp $_;
0069   push(@addresses, $_);
0070 }
0071 close(SORT);
0072 unlink $malloc.tmp;
0073 
0074 open (gdb, "|gdb -nx -q $command > $malloc.tmp") || die "Could not run gdb: $!\n";
0075 $| = 1;
0076 
0077 # get rid of the (gdb)
0078 printf (gdb "set prompt\n");
0079 printf (gdb "echo \\n\n");
0080 
0081 # load in the shared libraries
0082 printf (gdb "sharedlibrary\n");
0083 
0084 # run the program to have _definitly_ the information
0085 # we need from the shared libraries. Unfortunately gdb 4.18's
0086 # version of sharedlibrary does nothing ;(
0087 printf (gdb "b main\n");
0088 printf (gdb "run\n");
0089 
0090 foreach $address (@addresses) {
0091 
0092   printf (gdb "echo -----------------------------------------------\\n\n");
0093   # printf (gdb "echo Address = '%s'\n", $address);
0094   printf (gdb "x %s\n", $address);
0095   printf (gdb "info line *(%s)\n", $address);
0096 }
0097 printf (gdb "quit\ny\n");
0098 # $| = 0;
0099 
0100 close(gdb);
0101 
0102 %lines = ();
0103 
0104 open(malloc, "< $malloc.tmp");
0105 
0106 $count = 0;
0107 $address = "";
0108 $line = "";
0109 
0110 while ( <malloc> ) {
0111 
0112   # ignore our own input
0113   if ($_ =~ m/^x 0x/ || $_ =~ m/^echo ------/ || $_ =~ m/^info line/) {
0114     next;
0115   }
0116 
0117   if ($_ =~ m/^--------/) {
0118     if ($line) {
0119       $lines{$address} = "$line";
0120     }
0121     $count = 0;
0122     $address = "";
0123     $line = "";
0124   } else {
0125     $count = $count + 1;
0126   }
0127   
0128   if ($count == 1 && $_ =~ m/(0x[0-9a-fA-F]+)\s*<(.*)>:\s*(\S+)/) {
0129     $address = $1;
0130     $line = "$2<$3>";
0131   }
0132   
0133   if ($count == 2 && $_ =~ m/Line ([0-9]+) of \"([^\"]*)\"/) {
0134     $line = "$2:$1";
0135   }
0136   
0137 }
0138 
0139 if ($line) {
0140   $lines{$address} = "$line";
0141 }
0142 
0143 close(malloc);
0144 
0145 open(malloc, $malloc);
0146 
0147 while ( <malloc> ) {
0148   if ($_ =~ m/ra=(0x[0-9a-fA-F]+)/) {
0149     $address = $1;
0150     if (defined($lines{$address})) {
0151       $_ =~ s/ra=$address/$lines{$address}/;
0152       print STDOUT $_;
0153     } else {
0154       print STDOUT $_;
0155     }
0156   } else {
0157     print STDOUT $_;
0158   }
0159 }