summaryrefslogtreecommitdiff
path: root/python/ins_stats.py
blob: d9b560243f7f8265668c793a212355f084b1d255 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
import pychrysalide
from pychrysalide.analysis.contents import FileContent
from pychrysalide.format.dex import DexFormat
from pychrysalide.analysis import LoadedBinary
from threading import Event


if len(sys.argv) != 2:
    print('Usage: %s <dex classes>' % sys.argv[0])
    sys.exit(1)


# Load the provided binary

cnt = FileContent(sys.argv[1])

fmt = DexFormat(cnt)

binary = LoadedBinary(fmt)

binary.analyze_and_wait()


# Compute some stats

raw = [ "dn", "db", "dw", "dd", "dq" ]

collected = { }

max_len = 0
ins_count = 0

for ins in binary.processor.instrs:

    if ins.keyword in raw:
        continue

    if len(ins.keyword) > max_len:
        max_len = len(ins.keyword)

    ins_count += 1

    if ins.keyword in collected.keys():
        collected[ins.keyword] += 1
    else:
        collected[ins.keyword] = 1


# Display the results

stats = [ (k, v) for k, v in collected.items() ]

stats = sorted(stats, key = lambda s: s[1], reverse = True)

print('=== Collected %d instructions ===' % ins_count)

for s in stats:
    print(' %s %d (%d%%)' % (s[0].ljust(max_len + 2, '.'), s[1], (s[1] * 100) / ins_count))