[Python] Find the corresponding library name from the maps file based on the address

Source: Internet
Author: User

/Proc/Pid/maps provides the memory layout of the process. The following script finds the corresponding database name based on the given address:

#!/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 checks the function for a simple address format to ensure that the error is not too outrageous.

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

For example, the maps file is:

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...

Input 0x7f716d513123, output the corresponding library name/usr/lib/x86_64-linux-gnu/libxml2.so. 2.7.8

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.