[Python]根據地址從maps檔案中找相應的庫名

來源:互聯網
上載者:User

/proc/PID/maps提供了進程的memory layout,下面指令碼根據給定地址找出相應的庫名:

#!/usr/bin/pythonfrom __future__ import print_functionimport sysimport numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport bisectimport reimport lp_utilVERBOSE = False def process(line, mapset):pattern = r'([0-9a-f]+)-([0-9a-f]+) ([rwxps-]+) ([0-9a-f]+) ([0-9a-f:]+) ([0-9]+)\s+(\S*)'match_obj = re.match(pattern, line)if match_obj is not None:one_mapping = match_obj.group(0)addr_start = int(match_obj.group(1), 16)addr_end = int(match_obj.group(2), 16)permission = match_obj.group(3)offset = match_obj.group(4)device = match_obj.group(5)inode = match_obj.group(6)pathname = match_obj.group(7)print(hex(addr_start), hex(addr_end), permission, offset, device, inode, pathname)mapset.append({'addr_start':addr_start, 'addr_end':addr_end, 'permission':permission, 'offset':offset, 'device':device, 'inode':inode, 'pathname':pathname})else:print("Invalid maps")def usage():print("usage:", sys.argv[0], "maps_file")def find_map(mapset, keys, addr):addr = int(addr, 16)index = bisect.bisect_right(keys, addr)if VERBOSE:print("addr = ", addr)print("index = ", index)print(keys)if index == 0:return Noneif addr < mapset[index - 1]['addr_end']:return mapset[index - 1]else:return Nonedef main():argc = len(sys.argv)if argc != 2:usage()sys.exit()try:in_file = open(sys.argv[1], "r")except:sys.exit("Error opening file ", sys.argv[1])mapset = []while True:line = in_file.readline()if not line:breakprocess(line, mapset)in_file.close()print("Parsing done, item num = ", len(mapset))mapset.sort(key = lambda r:r['addr_start'])keys = [r['addr_start'] for r in mapset]while True:addr = raw_input("Please enter an address:")if not lp_util.addr_is_valid(addr):print("Invalid input")continuemap_item = find_map(mapset, keys, addr)if map_item is not None:print("lib name = ", map_item['pathname'])else:print("Not found")if __name__ == "__main__":    main()

lp_util.py中為一個簡單的地址格式檢查函數,保證不要錯得太離譜

import redef addr_is_valid(addr):match_obj = re.match(r'^(0x)?[0-9a-f]{1,}$', addr)return match_obj is not None

如maps檔案為:

00400000-00409000 r-xp 00000000 08:01 1835099                            /usr/bin/bluetooth-applet00608000-00609000 r--p 00008000 08:01 1835099                            /usr/bin/bluetooth-applet00609000-0060a000 rw-p 00009000 08:01 1835099                            /usr/bin/bluetooth-applet00861000-00af8000 rw-p 00000000 00:00 0                                  [heap]7f7164000000-7f7164022000 rw-p 00000000 00:00 07f7164022000-7f7168000000 ---p 00000000 00:00 07f7168000000-7f7168022000 rw-p 00000000 00:00 07f7168022000-7f716c000000 ---p 00000000 00:00 07f716d1c3000-7f716d314000 r-xp 00000000 08:01 1836558                    /usr/lib/x86_64-linux-gnu/libxml2.so.2.7.87f716d314000-7f716d513000 ---p 00151000 08:01 1836558                    /usr/lib/x86_64-linux-gnu/libxml2.so.2.7.87f716d513000-7f716d51b000 r--p 00150000 08:01 1836558                    /usr/lib/x86_64-linux-gnu/libxml2.so.2.7.87f716d51b000-7f716d51d000 rw-p 00158000 08:01 1836558                    /usr/lib/x86_64-linux-gnu/libxml2.so.2.7.87f716d51d000-7f716d51e000 rw-p 00000000 00:00 07f716d51e000-7f716e713000 r--p 00000000 08:01 2917167                    /usr/share/icons/hicolor/icon-theme.cache7f716e713000-7f716effe000 r--p 00000000 08:01 2752557                    /usr/share/icons/gnome/icon-theme.cache...

輸入0x7f716d513123,輸出相應的庫名/usr/lib/x86_64-linux-gnu/libxml2.so.2.7.8

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.