Created
April 10, 2020 11:31
-
-
Save chen3feng/fb36acbe8be3c3f092b42fcd9b4418f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import sys | |
import re | |
import heapq | |
import subprocess | |
TOP_N=100 | |
MIN_ALLOC_BYTE=10*1024 | |
def push_fixlen_heap(heap1, fixlen, elem1): | |
if len(heap1) < fixlen: | |
heapq.heappush(heap1, elem1) | |
else: | |
heapq.heappushpop(heap1, elem1) | |
def find_top_func(binary1): | |
file1=subprocess.Popen('objdump -C -M intel -d ' + binary1, | |
shell=True, stdout=subprocess.PIPE).stdout | |
func_name=None | |
func_pattern=re.compile('^[0-9a-f]+ <(.*)>:$') | |
sub_pattern=re.compile('sub.*rsp,(0x[0-9]+)') | |
heap1=[] | |
for line1 in file1: | |
match1=func_pattern.search(line1) | |
if match1: | |
func_name=match1.groups(0)[0] | |
continue | |
match1=sub_pattern.search(line1) | |
if not match1: | |
continue | |
if match1 and func_name: | |
alloc_size=int(match1.groups(0)[0], base=16) | |
if alloc_size >= MIN_ALLOC_BYTE: | |
push_fixlen_heap(heap1, TOP_N, (alloc_size, func_name)) | |
func_name=None | |
print 'result size {0}:'.format(len(heap1)) | |
print '(stack_alloc_size,func_name)' | |
heap1 = [heapq.heappop(heap1) for i in range(len(heap1))] | |
for v1 in heap1: | |
print v1 | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
print 'Usage: {0} [/path/to/binary.elf]'.format(sys.argv[0]) | |
sys.exit(1) | |
find_top_func(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment