#!/bin/bash
#利用本工具查看段错误函数的反汇编,定位到出错的行号; 工具入参前两个必填,第3个可选, <二进制可执行文件> <发生段错误的函数> [出错位置的偏移]
function print_help() {
echo "use: this_tool <bin> <func | addr> 0x[offset]"
echo " ex 1. core_dump.sh a.out main 1c ---> number 1c means 0x1c"
echo " ex 2. core_dump.sh bin 400e0 9 ---> number 400e0 means func addr"
#echo "ex. : this_tool.sh <bin> <func>"
exit 1
}
if [ $1 = "--help" ] ; then
print_help
fi
#if [ $ -ne 2 ] ; then
if [ $# -lt 2 ] ; then
print_help
fi
bin=$1
func=$2
_strip=`file $bin | grep "not stripped"`
if [ "$_strip" = "" ] ; then
echo "is stripped."
else
echo "not stripped."
fi
debug=`readelf -S $bin | grep debug_ | wc -l`
if [ $debug = "0" ] ; then
echo "have not debug_info."
else
echo "have $debug debug."
fi
#result_start=`nm -n $bin | grep $func | grep -e "[T|B] " ` #临时测试结束end
result_start=`nm -n $bin | grep $func | grep "T " `
num=`nm -n $bin | grep $func | grep "T " | wc -l `
#number=`$result_start | wc -l`
#echo "$number"
#if [ "$result_start" = "" ] ; then
if [ $((num)) -eq 0 ] ; then
echo "$func not found."
exit 1
elif [ $((num)) -gt 1 ] ; then
echo "***Here have more than once match.***"
echo "$result_start" | c++filt
start_addr=`echo -n $result_start | awk '{print $1}' `
echo "***please choose one, such as first line: $0 $1 $start_addr"
exit 1
fi
echo "***start: $result_start" | c++filt
start_addr=`echo -n $result_start | awk '{print $1}' `
#func_name=`echo -n $result_start | awk '{print $3}' `
#echo $start_addr
let "left = 0x$start_addr "
end_addr=`nm -n $bin | grep $start_addr -A1 | sed "1d" | awk '{print $1}' `
if [ "$end_addr" = "" ] ; then
echo " have not end_addr."
#((tmp=16#$start_addr))
#end_addr=`echo "ibase=10 ; obase=16 ; $tmp + 1" | bc `
end_addr=`echo "obase=16 ; $left + 1 " | bc `
fi
echo "***end : $end_addr"
#m=`expr $left + 1` ; echo "m:$m"
#let "m=0x11" ; echo $m ; let "n=0x10" ; echo "ibase=16 ; obase=16 ; $m + $n" | bc #结果02 01即就是0x21
if [ $3 ] ; then
#((tmp2=16#$start_addr))
#offset=`echo "ibase=16 ; obase=16 ; (($tmp2 + 0x$3))" | bc `
let "off = 0x$3" #; echo $offs
offset=`echo "obase=16 ; $left + $off " | bc `
fi
echo "***offset: ${offset,,}"
outfile=dump_func.log
echo -e "\n*****begin*****$start_addr**" >> $outfile
date >> $outfile
objdump -lS $bin --start-address=0x$start_addr --stop-address=0x$end_addr >> $outfile
echo -e "******end******$end_addr**\n" >> $outfile
#cat $outfile
echo -e "***output file: $outfile\ndone."
利用脚本-一键导出段错误的函数反汇编
于 2020-12-28 10:31:56 首次发布