"Fossies" - the Fresh Open Source Software Archive

Member "mpr-2.8/mprmap" (3 Nov 2002, 3454 Bytes) of package /linux/misc/old/mpr-2.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/bin/sh
    2 
    3 TMP=${MPRTMP-/tmp}
    4 MPRAWK=${MPRAWK-awk}
    5 
    6 nmfile=$TMP/mprmap.nm.$$
    7 logfile=$TMP/mprmap.log.$$
    8 flfile=$TMP/mprmap.fl.$$
    9 
   10 popt=
   11 fopt=
   12 fofiles=
   13 lopt=
   14 Iopt=,
   15 iopt=
   16 mprso=
   17 
   18 for arg in "$@"; do
   19     case "$1" in
   20     -p) popt=-p;;
   21     -f) fopt=true;;
   22     -F )    fopt=true; fofiles=$fofiles${fofiles:+,}"$2"; shift;;
   23     -F*)    fopt=true; fofiles=$fofiles${fofiles:+,}`echo "$1" | sed 's/-F//'`;;
   24     -l) lopt=true; fopt=true;;
   25     -i )    iopt=$iopt${iopt:+$Iopt}"$2"; shift;;
   26     -i*)    iopt=$iopt${iopt:+$Iopt}`echo "$1" | sed 's/-i//'`;;
   27     -I )    Iopt="$2"; shift;;
   28     -I*)    Iopt=`echo "$1" | sed 's/-I//'`;;
   29     --) shift; break;;
   30     -*) echo "mprmap: warning: ignoring unknown option $1" >&2;;
   31     *)  break;;
   32     esac
   33     shift
   34 done
   35 
   36 case "$#" in
   37 1|2)    ;;
   38 *)  echo "usage: mprmap [-f|-F a.c,b.c,...] [-l] [-Ix] [-i foo,bar,...] [-p] a.out [log]" >&2
   39     exit 1;;
   40 esac
   41 
   42 aout=`mprwhich "$1"` || exit 1
   43 shift
   44 
   45 status=1
   46 set -e
   47 trap 'set +e; rm -f $nmfile $logfile $flfile; trap 0; exit $status' 0 1 2 13 15
   48 
   49 MPRFILTER=`mprfilter mpr "$@"`
   50 MPRSO=`mprso "$aout"`; eval "$MPRSO"
   51 
   52 case "$#" in
   53 0)  case "$fopt$mprso" in
   54     ?*) gzip -1 >$logfile
   55         MPRFILTER="gzip -dc $logfile";;
   56     esac;;
   57 esac
   58 
   59 case "$mprso" in
   60 "") mprnm $popt "$aout" >$nmfile
   61     case "$fopt" in
   62     ?*) $MPRFILTER | mprfl ${fofiles:+-F"$fofiles"} "$aout" >$flfile;;
   63     esac;;
   64 *)  $MPRFILTER | mprfl-so $popt ${fofiles:+-F"$fofiles"} -n $nmfile -f $flfile "$aout";;
   65 esac
   66 
   67 MAPOPT="-v nmfile=$nmfile"
   68 case "$mprso" in
   69 ?*) MAPOPT=$MAPOPT" -v nmdirect=true";;
   70 esac
   71 case "$fopt" in
   72 ?*) MAPOPT=$MAPOPT" -v flfile=$flfile";;
   73 esac
   74 
   75 $MPRFILTER |
   76 
   77 $MPRAWK -v aout="$aout" $MAPOPT '
   78     BEGIN {
   79         FS="    "   # tab
   80         i=1
   81         while (getline <nmfile) {
   82             if (nmdirect != "") {
   83                 memoize[$1]=$2
   84                 i++
   85             }
   86             else {
   87                 addr[i] = $1
   88                 fn[i++] = $2
   89             }
   90         }
   91         addr[i] = addr[i-1] # sentinel
   92         naddr = i-1
   93         if (flfile)
   94             while (getline <flfile)
   95                 fl[$1] = "("$2","$3")"
   96         FS=OFS=":"
   97     }
   98     function bt(n, i) {
   99         printf "%s", $1
  100         if (flfile)
  101             for (i=2; i<n; i++)
  102                 printf ":%s%s", mapfn($i), mapfl($i)
  103         else
  104             for (i=2; i<n; i++)
  105                 printf ":%s", mapfn($i)
  106         for (i=n; i<=NF; i++)
  107             printf ":%s", $i
  108         printf "\n"
  109     }
  110     function mapfn(pc, l,u,m) {
  111         if (pc in memoize)
  112             return memoize[pc]
  113         if (nmdirect == "") {
  114             l=1; u=naddr
  115             while (l <= u) {    # binary search
  116                 m = int((l+u)/2)
  117                 if (addr[m] > pc)
  118                     u=m-1
  119                 else if (addr[m+1] <= pc)
  120                     l=m+1
  121                 else
  122                     return memoize[pc] = fn[m]
  123             }
  124         }
  125         printf "mprmap: warning: cannot map pc to name (pc=%d; NR=%d)\n", pc, NR |"cat 1>&2"
  126         return memoize[pc] = pc
  127     }
  128     function mapfl(pc) {
  129         if (pc in fl)
  130             return fl[pc]
  131         else
  132             return "()"
  133     }
  134     /^m/ { bt(NF-1) }
  135     /^f/ { bt(NF) }
  136     /^r/ { bt(NF-2) }' |
  137 
  138 $MPRAWK -v iopt="$iopt" -v Iopt="$Iopt" -v fopt=$fopt -v lopt=$lopt '
  139     BEGIN {
  140         # escape special regex chars /^[]|()*+ (for C++)
  141         gsub("\\/", "\\/", iopt)
  142         gsub("\\^", "\\^", iopt)
  143         gsub("\\[", "\\[", iopt)
  144         gsub("\\]", "\\]", iopt)
  145         gsub("\\|", "\\|", iopt)
  146         gsub("\\(", "\\(", iopt)
  147         gsub("\\)", "\\)", iopt)
  148         gsub("\\*", "\\*", iopt)
  149         gsub("\\+", "\\+", iopt)
  150         split(iopt, iarr, Iopt)
  151         if (fopt != "" && lopt == "")
  152             delline=1
  153     }
  154     {
  155         sub(/^m.*:malloc(\([^:]*\))?:/, "m:")
  156         sub(/^m.*:realloc(\([^:]*\))?:/, "m:")
  157         sub(/^r.*:realloc(\([^:]*\))?:/, "r:")
  158         sub(/^f.*:free(\([^:]*\))?:/, "f:")
  159         sub(/^f.*:realloc(\([^:]*\))?:/, "f:")
  160         for (i in iarr) {
  161             gsub(":"iarr[i]":", ":")
  162             gsub(":"iarr[i]"\([^:]*\):", ":")
  163         }
  164         if (delline)
  165             gsub(/,[0-9]+\)/, ")")
  166         print
  167     }'
  168 
  169 status=0