Chinaunix首页 | 论坛 | 博客
  • 博客访问: 307200
  • 博文数量: 65
  • 博客积分: 185
  • 博客等级: 入伍新兵
  • 技术积分: 609
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-06 21:41
个人简介

好好学习,天天向上

文章分类

全部博文(65)

文章存档

2022年(3)

2021年(25)

2020年(1)

2019年(3)

2016年(2)

2015年(3)

2014年(14)

2013年(7)

2012年(7)

我的朋友

分类: Python/Ruby

2021-04-10 17:23:51


点击(此处)折叠或打开

  1. #!/usr/bin/python3
  2. # -*- coding:UTF-8 -*-
  3. import sys
  4. import os
  5. import glob
  6. import re
  7. import subprocess



  8. if len(sys.argv) < 3:
  9.     print("Usage: ./xx.py tool-chain-gnu- vmlinux-file-path log-file-pach")
  10.     sys.exit()

  11. print("script name:", sys.argv[0])
  12. for i in range(1, len(sys.argv)):
  13.   print("para",i,":", sys.argv[i])

  14. CROSS_COMPILE=sys.argv[1]
  15. VMLINUX=sys.argv[2]
  16. log_f=sys.argv[3]


  17. vmlinux_path=os.path.dirname(os.path.realpath(VMLINUX))
  18. objdump_f=vmlinux_path+"/vmlinux.objdump"
  19. print("vmlinux dump 文件——" + objdump_f)

  20. if os.path.exists(objdump_f):
  21.     print("objdump file already exist, skip objdump")
  22. else:
  23.     dump_cmd=CROSS_COMPILE+"objdump -d "+VMLINUX+ " > "+objdump_f
  24.     print("objdump cmd:",dump_cmd)
  25.     print("objdump...waiting...")
  26.     os.system(dump_cmd)



  27. def cut_section(curr_f, to_find):
  28.     key_str_s="<"+to_find+">:"
  29.     key_str_e="\n"

  30. #    print("start:",key_str_s, "end:",key_str_e)

  31.     file_o=open(curr_f)
  32.     ll=file_o.readlines()

  33.     for c in ll:
  34.         if key_str_s in c:
  35. #            print (c)
  36.             index_s=ll.index(c)
  37. #            print("index_s:",index_s)
  38.             index_e=ll.index(key_str_e, index_s)    #find the first key_str_e from index_s    
  39. #            print("index_e:",index_e)
  40.             
  41. #            for i in range(index_s, index_e):
  42. #                print ll[i].strip("\n")
  43.         
  44.     file_o.close
  45.     return ll[index_s:index_e+2] #index_e+2 get the start line of the next function, also is the end of this function.


  46. def find_str_infile(curr_f):
  47.     key1="+0x"
  48.     key2="/0x"

  49. #    print("key1:",key1, "key2:",key2)

  50.     file_o=open(curr_f)
  51.     ll=file_o.readlines()
  52.     dict_l=[]
  53.     
  54.     for c in ll:
  55.         if key1 in c and key2 in c:
  56. #            print(c)
  57.             #example: [ 6684.448646] [<ffffffff811c71a1>] ? __slab_alloc.isra.64.constprop.67+0x39/0x4c
  58.             #[ 18.185487] [<ffffff90089c5ad4>] __slab_alloc+0x834/0xa48
  59.             #[c0325e80] [c0005264] do_softirq+0x74/0x7c
  60.             #
  61.             mo = re.search(r'([0-9a-fA-F]{8,16})>?]\s+\??\s*([0-9a-zA-Z_.]{3,50})\+0x([0-9a-fA-F]{1,4})/0x([0-9a-fA-F]{1,4})',c)
  62.             #[0-9a-fA-F]{8,16}
  63.             #([0-9a-fA-F]{8,16}).*([0-9a-zA-Z]{3,20})

  64.             dict={}
  65.             dict['addr']=mo.group(1)
  66.             dict['fun']=mo.group(2)
  67.             dict['off']=mo.group(3)
  68.             dict['len']=mo.group(4)
  69. #            print (dict['addr'], "~~~", dict['fun'], dict['off'],dict['len'])
  70.             dict_l.append(dict)
  71.     
  72.     for i in range(0, len(dict_l)):
  73.         dic=dict_l[i]
  74. #        print (dic['addr'], dic['fun'], dic['off'],dic['len'])
  75.     
  76.     file_o.close
  77.     return dict_l


  78. dict_o=find_str_infile(log_f)
  79. for i in range(0, len(dict_o)):
  80. #for i in range(0, 10):
  81.     dic=dict_o[i]
  82. #    print ("outer:",dic['addr'], dic['fun'], dic['off'],dic['len'])
  83.     list_out = cut_section(objdump_f, dic['fun'])
  84.     print("----------------")
  85. #    for i in range(0, len(list_out)):
  86. #        print(list_out[i].strip("\n"))
  87. #    print("1----------------")
  88. #    print(list_out[0])
  89. #    print(list_out[-1])
  90. #    print("2----------------")
  91.     func_s=list_out[0].split(' ')[0]
  92.     func_e=list_out[-1].split(' ')[0]
  93. #    print(func_s)
  94. #    print(func_e)
  95.     func_len=hex(int(func_e,16)-int(func_s,16))
  96. #    print("func_len:",func_len)
  97.     
  98.     if (int(func_len,16) == int(dic['len'], 16)):
  99. #        print("match_or_not ==", "func_name:",dic['fun'])
  100.         func_pc_index=hex(int(func_s,16) + int(dic['off'],16))
  101. #        print("func_pc_index:", func_pc_index)
  102.         func_pc_index_str=(func_pc_index+':').strip("0x")
  103. #        print("func_pc_index_str:", func_pc_index_str)
  104.         
  105.         for i in range(0, len(list_out)-1):
  106.             if (func_pc_index_str in list_out[i]):
  107.                 cmd_str=CROSS_COMPILE+"addr2line -e "+VMLINUX+" -i "+func_pc_index
  108.                 #print (cmd_str)
  109.                 #os.system(cmd_str)
  110.                 addr2line_out = subprocess.getoutput(cmd_str)
  111.                 list_out[i]=list_out[i]+"[---above_pc_from--- ]"+dic['fun']+"+"+"0x"+dic['off']+"/"+"0x"+dic['len'] +"---"+addr2line_out
  112.             print(list_out[i].strip("\n"))
  113.         
  114.         
  115.         
  116.         #print ("xx",list_out[list_out.index(func_pc_index.strip("0x"))] )
  117.     else:
  118.         print("function len not match !=", "func_name:",dic['fun'],"vmlinuxfile func_len:", func_len, "msgfile func_len:", dic['len'])

阅读(354) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~