利用脚本-一键导出段错误的函数反汇编

博客围绕 Linux 服务器运维展开,虽未给出具体内容,但可知涉及 Linux 系统在服务器运维方面的相关知识,属于信息技术中基础运维领域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/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."

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值