-
Notifications
You must be signed in to change notification settings - Fork 1
/
machine-code-analyzer.py
executable file
·1460 lines (1207 loc) · 59.1 KB
/
machine-code-analyzer.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
#
# Email: Hagen Paul Pfeifer <[email protected]>
# Machine-Code-Analyzer is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# MachineCodeAnalyzer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MachineCodeAnalyzer. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import optparse
import subprocess
import pprint
import re
import ctypes
import math
import time
# Optional packages
# Arch Linux:
# pacman -S extra/python-pip
# Debian based
# aptitude install python3-pip python3-lxml
# Finally:
# pip3 install pygal
try:
import pygal
except ImportError:
sys.stderr.write("Pygal is not installed! Graph creation will not work\n")
pygal = None
pp = pprint.PrettyPrinter(indent=4)
__programm__ = "machine-code-analyzer"
__author__ = "Hagen Paul Pfeifer"
__version__ = "1"
__license__ = "GPLv3"
# custom exceptions
class ArgumentException(Exception): pass
class InternalSequenceException(Exception): pass
class InternalException(Exception): pass
class SequenceContainerException(InternalException): pass
class NotImplementedException(InternalException): pass
class SkipProcessStepException(Exception): pass
class UnitException(Exception): pass
class FunctionExcluder:
def __init__(self):
self.exclude_files = ['_start', '_fini', '__libc_csu_fini',
'__do_global_dtors_aux', '__libc_csu_init',
'register_tm_clones', 'frame_dummy',
'__init', 'deregister_tm_clones', '_init']
def is_excluded(self, function_name):
if function_name.endswith("@plt"):
return True
if function_name.endswith("@plt-0x10"):
return True
if function_name in self.exclude_files:
return True
return False
class RetObj:
STATIC = 0
DYNAMIC = 1
NO_STACK = 2
def __init__(self, stack_type):
self.stack_type = stack_type
self.register = None
class InstructionCategory:
UNKNOWN = 0
# E.g. http://flint.cs.yale.edu/cs422/doc/24547012.pdf
BINARY_ARITHMETIC = 32
DECIMAL_ARITHMETIC = 33
LOGICAL = 34
SHIFT_ROTATE = 35
BIT_BYTE = 6
CONTROL_TRANSFER = 37
STRING = 38
FLAG_CONTROL = 39
SEGMENT_REGISTER = 40
MISC = 41
DATA_TRANSFER = 42
FLOATING_POINT = 43
SYSTEM = 44
SIMD = 9
MMX = 10
SSE = 11
E3DN = 12
SSE2 = 13
SSE3 = 14
SSSE3 = 15
SSE41 = 16
SSE42 = 17
SSE4A = 18
AES = 19
AVX = 20
FMA = 21
FMA4 = 22
CPUID = 23
MMXEXT = 24
DBn = {
"""mov movl movb movslq movabs cmove xchg bswap xadd push pushq
pop in out cmovne movzbl cmovle cmovge movzwl movsbl cmova cmovbe
cmovg movsq movswl cmovs cmovle cmovb cmovae cmpxchg cmovns
movw cmovl movsbq movsl movsbw""" :
[ DATA_TRANSFER , None ],
"""add addl sub adc imul mul div setle inc neg cmp cmpl cmpq cmpb cmpl
cmpq cmpw idivl dec sbb addq subl addw idiv cmpsb incl decl incq
divl subq subw subb mull divq""" :
[ BINARY_ARITHMETIC , None ],
"""daa das aaa aas aam aad""" :
[ DECIMAL_ARITHMETIC , None ],
"""and or orb orl not xor andb andl orq orw andq andw""" :
[ LOGICAL , None ],
"""sar shr sal shl rol rcr rcl shrd shld ror""" :
[ SHIFT_ROTATE , None ],
"""test bt bts btr btc sete testb cltq setne btsl cltd btrw btrl
testl seta bsr setbe testq""" :
[ BIT_BYTE , None ],
"""jmp je jbe jg jz ja jc jle js loop call callq retq jne jmpq
enter leave leaveq ret iret jl jns jge jae jb""" :
[ CONTROL_TRANSFER , None ],
"""movs movsb rep repz""" :
[ STRING , None ],
"""stc clc sti cli pushf popf stos stosb stosw stosd clac stac""" :
[ FLAG_CONTROL , None ],
"""lds les lgs""" :
[ SEGMENT_REGISTER , None ],
"""lea nop nopl nopw ud2 xlat sfence mfence lfence""" :
[ MISC, None ],
"""cvttss2si""" :
[ FLOATING_POINT, None ],
"""invlpg lgdt lldt ltr str arpl lock hlt rsm sysenter sysleave
rdtsc""" :
[ SYSTEM, None ],
"""emms movd movq packssdw packsswb packuswb paddb paddd paddsb
paddsw paddusb paddusw paddw pand pandn pcmpeqb pcmpeqd pcmpeqw
pcmpgtb pcmpgtd pcmpgtw pmaddwd pmulhw pmullw por pslld psllq psllw
psrad psraw psrld psrlq psrlw psubb psubd psubsb psubsw psubusb
psubusw psubw punpckhbw punpckhdq punpckhwd punpcklbw punpckldq
punpcklwd pxor""" :
[ MMX, None ],
"""addps addss andnps andps cmpeqps cmpeqss cmpleps cmpless cmpltps
cmpltss cmpneqps cmpneqss cmpnleps cmpnless cmpnltps cmpnltss
cmpordps cmpordss cmpps cmpss cmpunordps cmpunordss comiss cvtpi2ps
cvtps2pi cvtsi2ss cvtss2si cvttps2pi cvttss2si divps divss ldmxcsr
maxps maxss minps minss movaps movhlps movhps movlhps movlps
movmskps movntps movss movups mulps mulss orps rcpps rcpss rsqrtps
rsqrtss shufps sqrtps sqrtss stmxcsr subps subss ucomiss unpckhps
unpcklps xorps""" :
[ SSE, None ],
"""maskmovq movntq pavgb pavgw pextrw pinsrw pmaxsw pmaxub pminsw
pminub pmovmskb pmulhuw psadbw pshufw""" :
[ MMXEXT, None ],
"""pf2iw pfnacc pfpnacc pi2fw pswapd""" :
[ E3DN, None ], # 3DNow!
"""addpd addsd andnpd andpd clflush cmpeqpd cmpeqsd cmplepd cmplesd
cmpltpd cmpltsd cmpneqpd cmpneqsd cmpnlepd cmpnlesd cmpnltpd
cmpnltsd cmpordpd cmpordsd cmppd cmpunordpd cmpunordsd comisd
cvtdq2pd cvtdq2ps cvtpd2dq cvtpd2pi cvtpd2ps cvtpi2pd cvtps2dq
cvtps2pd cvtsd2si cvtsd2ss cvtsi2sd cvtss2sd cvttpd2dq cvttpd2pi
cvttps2dq cvttsd2si divpd divsd maskmovdqu maxpd maxsd minpd minsd
movapd movdq2q movdqa movdqu movhpd movlpd movmskpd movntdq movnti
movntpd movq2dq movupd mulpd mulsd orpd paddq pmuludq pshufd
pshufhw pshuflw pslldq psrldq psubq punpckhqdq punpcklqdq shufpd
sqrtpd sqrtsd subpd subsd ucomisd unpckhpd unpcklpd xorpd""" :
[ SSE2, None ],
"""addsubpd addsubps fisttp haddpd haddps hsubpd hsubps lddqu monitor
movddup movshdup movsldup mwait""" :
[ SSE3, None ],
"""pabsb pabsd pabsw palignr phaddd phaddsw phaddw phsubd phsubsw
phsubw pmaddubsw pmulhrsw pshufb psignb psignd psignw""" :
[ SSSE3, None ],
"""blendpd blendps blendvpd blendvps dppd dpps extractps insertps
movntdqa mpsadbw packusdw pblendvb pblendw pcmpeqq pextrb pextrd
pextrq phminposuw pinsrb pinsrd pinsrq pmaxsb pmaxsd pmaxud pmaxuw
pminsb pminsd pminud pminuw pmovsxbd pmovsxbq pmovsxbw pmovsxdq
pmovsxwd pmovsxwq pmovzxbd pmovzxbq pmovzxbw pmovzxdq pmovzxwd
pmovzxwq pmuldq pmulld ptest roundpd roundps roundsd roundss""" :
[ SSE41, None ],
"""crc32 pcmpestri pcmpestrm pcmpgtq pcmpistri pcmpistrm popcnt""" :
[ SSE42, None ],
"""extrq insertq movntsd movntss""" :
[ SSE4A, None ],
"""aesenc aesenclast aesdec aesdeclast aesimc aeskeygenassist""" :
[ AES, None ],
"""pclmulhqhqdq pclmulhqlqdq pclmullqhqdq pclmullqlqdq pclmulqdq
vaddpd vaddps vaddsd vaddss vaddsubpd vaddsubps vaesdec vaesdeclast
vaesenc vaesenclast vaesimc vaeskeygenassist vandnpd vandnps vandpd
vandps vblendpd vblendps vblendvpd vblendvps vbroadcastf128
vbroadcastsd vbroadcastss vcmpeq_ospd vcmpeq_osps vcmpeq_ossd
vcmpeq_osss vcmpeqpd vcmpeqps vcmpeqsd vcmpeqss vcmpeq_uqpd
vcmpeq_uqps vcmpeq_uqsd vcmpeq_uqss vcmpeq_uspd vcmpeq_usps
vcmpeq_ussd vcmpeq_usss vcmpfalse_oqpd vcmpfalse_oqps
vcmpfalse_oqsd vcmpfalse_oqss vcmpfalse_ospd vcmpfalse_osps
vcmpfalse_ossd vcmpfalse_osss vcmpfalsepd vcmpfalseps vcmpfalsesd
vcmpfalsess vcmpge_oqpd vcmpge_oqps vcmpge_oqsd vcmpge_oqss
vcmpge_ospd vcmpge_osps vcmpge_ossd vcmpge_osss vcmpgepd vcmpgeps
vcmpgesd vcmpgess vcmpgt_oqpd vcmpgt_oqps vcmpgt_oqsd vcmpgt_oqss
vcmpgt_ospd vcmpgt_osps vcmpgt_ossd vcmpgt_osss vcmpgtpd vcmpgtps
vcmpgtsd vcmpgtss vcmple_oqpd vcmple_oqps vcmple_oqsd vcmple_oqss
vcmple_ospd vcmple_osps vcmple_ossd vcmple_osss vcmplepd vcmpleps
vcmplesd vcmpless vcmplt_oqpd vcmplt_oqps vcmplt_oqsd vcmplt_oqss
vcmplt_ospd vcmplt_osps vcmplt_ossd vcmplt_osss vcmpltpd vcmpltps
vcmpltsd vcmpltss vcmpneq_oqpd vcmpneq_oqps vcmpneq_oqsd
vcmpneq_oqss vcmpneq_ospd vcmpneq_osps vcmpneq_ossd vcmpneq_osss
vcmpneqpd vcmpneqps vcmpneqsd vcmpneqss vcmpneq_uqpd vcmpneq_uqps
vcmpneq_uqsd vcmpneq_uqss vcmpneq_uspd vcmpneq_usps vcmpneq_ussd
vcmpneq_usss vcmpngepd vcmpngeps vcmpngesd vcmpngess vcmpnge_uqpd
vcmpnge_uqps vcmpnge_uqsd vcmpnge_uqss vcmpnge_uspd vcmpnge_usps
vcmpnge_ussd vcmpnge_usss vcmpngtpd vcmpngtps vcmpngtsd vcmpngtss
vcmpngt_uqpd vcmpngt_uqps vcmpngt_uqsd vcmpngt_uqss vcmpngt_uspd
vcmpngt_usps vcmpngt_ussd vcmpngt_usss vcmpnlepd vcmpnleps
vcmpnlesd vcmpnless vcmpnle_uqpd vcmpnle_uqps vcmpnle_uqsd
vcmpnle_uqss vcmpnle_uspd vcmpnle_usps vcmpnle_ussd vcmpnle_usss
vcmpnltpd vcmpnltps vcmpnltsd vcmpnltss vcmpnlt_uqpd vcmpnlt_uqps
vcmpnlt_uqsd vcmpnlt_uqss vcmpnlt_uspd vcmpnlt_usps vcmpnlt_ussd
vcmpnlt_usss vcmpordpd vcmpordps vcmpord_qpd vcmpord_qps
vcmpord_qsd vcmpord_qss vcmpordsd vcmpord_spd vcmpord_sps vcmpordss
vcmpord_ssd vcmpord_sss vcmppd vcmpps vcmpsd vcmpss vcmptruepd
vcmptrueps vcmptruesd vcmptruess vcmptrue_uqpd vcmptrue_uqps
vcmptrue_uqsd vcmptrue_uqss vcmptrue_uspd vcmptrue_usps
vcmptrue_ussd vcmptrue_usss vcmpunordpd vcmpunordps vcmpunord_qpd
vcmpunord_qps vcmpunord_qsd vcmpunord_qss vcmpunordsd vcmpunord_spd
vcmpunord_sps vcmpunordss vcmpunord_ssd vcmpunord_sss vcomisd
vcomiss vcvtdq2pd vcvtdq2ps vcvtpd2dq vcvtpd2ps vcvtps2dq vcvtps2pd
vcvtsd2si vcvtsd2ss vcvtsi2sd vcvtsi2ss vcvtss2sd vcvtss2si
vcvttpd2dq vcvttps2dq vcvttsd2si vcvttss2si vdivpd vdivps vdivsd
vdivss vdppd vdpps vextractf128 vextractps vhaddpd vhaddps vhsubpd
vhsubps vinsertf128 vinsertps vlddqu vldmxcsr vldqqu vmaskmovdqu
vmaskmovpd vmaskmovps vmaxpd vmaxps vmaxsd vmaxss vminpd vminps
vminsd vminss vmovapd vmovaps vmovd vmovddup vmovdqa vmovdqu
vmovhlps vmovhpd vmovhps vmovlhps vmovlpd vmovlps vmovmskpd
vmovmskps vmovntdq vmovntdqa vmovntpd vmovntps vmovntqq vmovq
vmovqqa vmovqqu vmovsd vmovshdup vmovsldup vmovss vmovupd vmovups
vmpsadbw vmulpd vmulps vmulsd vmulss vorpd vorps vpabsb vpabsd
vpabsw vpackssdw vpacksswb vpackusdw vpackuswb vpaddb vpaddd vpaddq
vpaddsb vpaddsw vpaddusb vpaddusw vpaddw vpalignr vpand vpandn
vpavgb vpavgw vpblendvb vpblendw vpclmulhqhqdq vpclmulhqlqdq
vpclmullqhqdq vpclmullqlqdq vpclmulqdq vpcmpeqb vpcmpeqd vpcmpeqq
vpcmpeqw vpcmpestri vpcmpestrm vpcmpgtb vpcmpgtd vpcmpgtq vpcmpgtw
vpcmpistri vpcmpistrm vperm2f128 vpermilpd vpermilps vpextrb
vpextrd vpextrq vpextrw vphaddd vphaddsw vphaddw vphminposuw
vphsubd vphsubsw vphsubw vpinsrb vpinsrd vpinsrq vpinsrw vpmaddubsw
vpmaddwd vpmaxsb vpmaxsd vpmaxsw vpmaxub vpmaxud vpmaxuw vpminsb
vpminsd vpminsw vpminub vpminud vpminuw vpmovmskb vpmovsxbd
vpmovsxbq vpmovsxbw vpmovsxdq vpmovsxwd vpmovsxwq vpmovzxbd
vpmovzxbq vpmovzxbw vpmovzxdq vpmovzxwd vpmovzxwq vpmuldq vpmulhrsw
vpmulhuw vpmulhw vpmulld vpmullw vpmuludq vpor vpsadbw vpshufb
vpshufd vpshufhw vpshuflw vpsignb vpsignd vpsignw vpslld vpslldq
vpsllq vpsllw vpsrad vpsraw vpsrld vpsrldq vpsrlq vpsrlw vpsubb
vpsubd vpsubq vpsubsb vpsubsw vpsubusb vpsubusw vpsubw vptest
vpunpckhbw vpunpckhdq vpunpckhqdq vpunpckhwd vpunpcklbw vpunpckldq
vpunpcklqdq vpunpcklwd vpxor vrcpps vrcpss vroundpd vroundps
vroundsd vroundss vrsqrtps vrsqrtss vshufpd vshufps vsqrtpd vsqrtps
vsqrtsd vsqrtss vstmxcsr vsubpd vsubps vsubsd vsubss vtestpd
vtestps vucomisd vucomiss vunpckhpd vunpckhps vunpcklpd vunpcklps
vxorpd vxorps vzeroall vzeroupper""" :
[ AVX, None ],
"""vfmadd123pd vfmadd123ps vfmadd123sd vfmadd123ss vfmadd132pd
vfmadd132ps vfmadd132sd vfmadd132ss vfmadd213pd vfmadd213ps
vfmadd213sd vfmadd213ss vfmadd231pd vfmadd231ps vfmadd231sd
vfmadd231ss vfmadd312pd vfmadd312ps vfmadd312sd vfmadd312ss
vfmadd321pd vfmadd321ps vfmadd321sd vfmadd321ss vfmaddsub123pd
vfmaddsub123ps vfmaddsub132pd vfmaddsub132ps vfmaddsub213pd
vfmaddsub213ps vfmaddsub231pd vfmaddsub231ps vfmaddsub312pd
vfmaddsub312ps vfmaddsub321pd vfmaddsub321ps vfmsub123pd
vfmsub123ps vfmsub123sd vfmsub123ss vfmsub132pd vfmsub132ps
vfmsub132sd vfmsub132ss vfmsub213pd vfmsub213ps vfmsub213sd
vfmsub213ss vfmsub231pd vfmsub231ps vfmsub231sd vfmsub231ss
vfmsub312pd vfmsub312ps vfmsub312sd vfmsub312ss vfmsub321pd
vfmsub321ps vfmsub321sd vfmsub321ss vfmsubadd123pd vfmsubadd123ps
vfmsubadd132pd vfmsubadd132ps vfmsubadd213pd vfmsubadd213ps
vfmsubadd231pd vfmsubadd231ps vfmsubadd312pd vfmsubadd312ps
vfmsubadd321pd vfmsubadd321ps vfnmadd123pd vfnmadd123ps
vfnmadd123sd vfnmadd123ss vfnmadd132pd vfnmadd132ps vfnmadd132sd
vfnmadd132ss vfnmadd213pd vfnmadd213ps vfnmadd213sd vfnmadd213ss
vfnmadd231pd vfnmadd231ps vfnmadd231sd vfnmadd231ss vfnmadd312pd
vfnmadd312ps vfnmadd312sd vfnmadd312ss vfnmadd321pd vfnmadd321ps
vfnmadd321sd vfnmadd321ss vfnmsub123pd vfnmsub123ps vfnmsub123sd
vfnmsub123ss vfnmsub132pd vfnmsub132ps vfnmsub132sd vfnmsub132ss
vfnmsub213pd vfnmsub213ps vfnmsub213sd vfnmsub213ss vfnmsub231pd
vfnmsub231ps vfnmsub231sd vfnmsub231ss vfnmsub312pd vfnmsub312ps
vfnmsub312sd vfnmsub312ss vfnmsub321pd vfnmsub321ps vfnmsub321sd
vfnmsub321ss""" :
[ FMA, None ],
"""vfmaddpd vfmaddps vfmaddsd vfmaddss vfmaddsubpd vfmaddsubps
vfmsubaddpd vfmsubaddps vfmsubpd vfmsubps vfmsubsd vfmsubss
vfnmaddpd vfnmaddps vfnmaddsd vfnmaddss vfnmsubpd vfnmsubps
vfnmsubsd vfnmsubss vfrczpd vfrczps vfrczsd vfrczss vpcmov vpcomb
vpcomd vpcomq vpcomub vpcomud vpcomuq vpcomuw vpcomw vphaddbd
vphaddbq vphaddbw vphadddq vphaddubd vphaddubq vphaddubw vphaddudq
vphadduwd vphadduwq vphaddwd vphaddwq vphsubbw vphsubdq vphsubwd
vpmacsdd vpmacsdqh vpmacsdql vpmacssdd vpmacssdqh vpmacssdql
vpmacsswd vpmacssww vpmacswd vpmacsww vpmadcsswd vpmadcswd vpperm
vprotb vprotd vprotq vprotw vpshab vpshad vpshaq vpshaw vpshlb
vpshld vpshlq vpshlw""" :
[ FMA4, None ],
"""cpuid""" :
[ CPUID, None ]
}
# key => instruction
# value => array of category and description
lookup_table = dict()
@staticmethod
def init_instruction_table():
for key, value in InstructionCategory.DBn.items():
for instruction in key.split():
InstructionCategory.lookup_table[instruction] = [value[0], value[1]]
@staticmethod
def guess(instructon):
if instructon in InstructionCategory.lookup_table:
return InstructionCategory.lookup_table[instructon][0]
return InstructionCategory.UNKNOWN
@staticmethod
def str(cat):
if cat == InstructionCategory.UNKNOWN: return "unknown"
if cat == InstructionCategory.DATA_TRANSFER: return "DATA_TRANSFER"
if cat == InstructionCategory.BINARY_ARITHMETIC: return "BINARY_ARITHMETIC"
if cat == InstructionCategory.DECIMAL_ARITHMETIC: return "DECIMAL_ARITHMETIC"
if cat == InstructionCategory.LOGICAL: return "LOGICAL"
if cat == InstructionCategory.SHIFT_ROTATE: return "SHIFT_ROTATE"
if cat == InstructionCategory.BIT_BYTE: return "BIT_BYTE"
if cat == InstructionCategory.CONTROL_TRANSFER: return "CONTROL_TRANSFER"
if cat == InstructionCategory.STRING: return "STRING"
if cat == InstructionCategory.FLAG_CONTROL: return "FLAG_CONTROL"
if cat == InstructionCategory.SEGMENT_REGISTER: return "SEGMENT_REGISTER"
if cat == InstructionCategory.MISC: return "MISC"
if cat == InstructionCategory.DATA_TRANSFER: return "DATA_TRANSFER"
if cat == InstructionCategory.FLOATING_POINT: return "FLOATING_POINT"
if cat == InstructionCategory.SYSTEM: return "SYSTEM"
if cat == InstructionCategory.SIMD: return "SIMD"
if cat == InstructionCategory.MMX: return "MMX"
if cat == InstructionCategory.SSE: return "SSE"
if cat == InstructionCategory.E3DN: return "E3DN"
if cat == InstructionCategory.SSE2: return "SSE2"
if cat == InstructionCategory.SSE3: return "SSE3"
if cat == InstructionCategory.SSSE3: return "SSSE3"
if cat == InstructionCategory.SSE41: return "SSE41"
if cat == InstructionCategory.SSE42: return "SSE42"
if cat == InstructionCategory.SSE4A: return "SSE4A"
if cat == InstructionCategory.AES: return "AES"
if cat == InstructionCategory.AVX: return "AVX"
if cat == InstructionCategory.FMA: return "FMA"
if cat == InstructionCategory.FMA4: return "FMA4"
if cat == InstructionCategory.CPUID: return "CPUID"
if cat == InstructionCategory.MMXEXT: return "MMXEXT"
raise Exception("Programmed error - no string repr defined")
class Context:
def __init__(self):
self.function_name = None
self.function_start_address = None
self.section_name = None
class BinaryAtom:
TYPE_1 = 1
TYPE_2 = 2
TYPE_3 = 3
TYPE_4 = 4
TYPE_5 = 5
TYPE_6 = 6
def __init__(self, b_type, line, addr, opcode, kwargs):
self.line = line
self.addr = addr
self.opcode_str = opcode
self.opcode_len = len(opcode.replace(" ", "")) // 2
self.type = b_type
self.category = InstructionCategory.UNKNOWN
self.src = self.dst = self.jmp_addr = self.jmp_sym = None
self.mnemonic = kwargs['mnemonic']
if b_type == BinaryAtom.TYPE_1:
self.src = kwargs['src']
self.dst = kwargs['dst']
self.jmp_addr = kwargs['jmp-addr']
self.jmp_sym = kwargs['jmp-sym']
elif b_type == BinaryAtom.TYPE_2:
self.src = kwargs['src']
self.dst = kwargs['dst']
elif b_type == BinaryAtom.TYPE_3:
self.src = kwargs['src']
self.jmp_addr = kwargs['jmp-addr']
self.jmp_sym = kwargs['jmp-sym']
elif b_type == BinaryAtom.TYPE_4:
self.src = kwargs['src']
self.jmp_addr = kwargs['jmp-addr']
elif b_type == BinaryAtom.TYPE_5:
self.src = kwargs['src']
elif b_type == BinaryAtom.TYPE_6:
pass
else:
raise Exception("unknown code")
def print(self):
sys.stderr.write("%s\n" % (self.line))
sys.stderr.write("MNEMONIC: %s SRC:%s DST:%s [OPCODE: %s, LEN:%d]\n" %
(self.mnemonic, self.src, self.dst, self.opcode_str, self.opcode_len))
class Common:
def err(self, msg):
sys.stderr.write(msg)
def verbose(self, msg):
if not self.opts.verbose:
return
sys.stderr.write(msg)
def msg(self, msg):
return sys.stdout.write(msg) - 1
def line(self, length, char='-'):
sys.stdout.write(char * length + "\n")
def msg_underline(self, msg, pre_news=0, post_news=0):
str_len = len(msg)
if pre_news:
self.msg("\n" * pre_news)
self.msg(msg)
self.msg("\n" + '=' * str_len)
if post_news:
self.msg("\n" * post_news)
def debug(self, msg):
# remove next line for debuging purpose
return
sys.stderr.write(msg)
class Parser:
def __init__(self, opts):
self.args = opts
self.filename = opts.filename
def try_parse_update_context(self, line, context):
if line == "":
# empty line, do nothing
return
# ./ipproof-client: file format
match = re.search(r'\s*' + self.filename + ':\s+file format', line)
if match:
return
# 0000000000401088 <_init>:
match = re.search(r'^([\da-f]+)\s+<(\S+)>', line)
if match:
context.function_start_address = int(match.group(1).strip(), 16)
context.function_name = match.group(2).strip()
self.caller.debug("Function name: %s, function start address: 0x%x\n" %
(context.function_name, context.function_start_address))
return
# Disassembly of section .plt:
match = re.search(r'Disassembly of section\s+(\S+):', line)
if match:
context.section_name = match.group(1).strip()
self.caller.debug("Section name: %s\n" % (context.section_name))
return
# unknown lines are probably annotated source code
# lines from DWARF info - just skip it
#self.caller.verbose("Unknown line: \"%s\"\n" % (line))
def is_wrapped_line_update(self, line):
match = re.search(r'([\da-f]+):\s+((?:[0-9a-f]{2} )+)', line)
if match:
raise Exception("Line wrapped:\n%s\n" % (line))
def parse_line(self, line, context):
# 404e52: e8 31 c2 ff ff callq 401088 <_init>
ret = dict()
match = re.search(r'([\da-f]+):\s+((?:[0-9a-f]{2} )+)\s+(.*)', line)
if not match:
# Special case overlong wrapped in two lines:
# 4046c7: 48 ba cf f7 53 e3 a5 movabs $0x20c49ba5e353f7cf,%rdx
# 4046ce: 9b c4 20
#self.is_wrapped_line_update(line)
# no instruction, but maybe function information to
# update context data
self.try_parse_update_context(line, context)
return None
addr = match.group(1).strip()
opcode = match.group(2).strip()
instr = match.group(3).strip()
# now unfold asm, following online tools
# can be used to test regular expressions:
# http://www.regexr.com/ or http://rubular.com/
# movsd 0x1e4e(%rip),%xmm1 # 406c28 <symtab.5300+0x48>
m1 = re.search(r'(\w+)\s+(\S+),(\S+)\s+#\s+([\da-f]+)\s+<(.+)>', instr)
# mov 0x18(%rsp),%r12
m2 = re.search(r'(\w+)\s+(\S+),(\S+)', instr)
# jmpq *0x207132(%rip) # 608218 <_GLOBAL_OFFSET_TABLE_+0x28>
m3 = re.search(r'(\w+)\s+(\S+)\s+#\s+([\da-f]+)\s+<(.+)>', instr)
# jne 404e60 <__libc_csu_init+0x50>
m4 = re.search(r'(\w+)\s+(\S+)\s+<(\S+)>', instr)
# jmp 404b20
m5 = re.search(r'(\w+)\s+(\S+)', instr)
# jmp
m6 = re.search(r'(\w+)', instr)
self.caller.debug("%s%s: %s%s\t\t%s%s%s\n" %
(Colors.WARNING, addr, Colors.OKGREEN, opcode, Colors.FAIL, instr, Colors.ENDC))
d = dict()
if m1:
d['mnemonic'] = m1.group(1).strip()
d['src'] = m1.group(2).strip()
d['dst'] = m1.group(3).strip()
d['jmp-addr'] = m1.group(4).strip()
d['jmp-sym'] = m1.group(5).strip()
self.caller.debug("1 movsd 0x1e4e(%rip),%xmm1 # 406c28 <symtab.5300+0x48>\n")
return BinaryAtom(BinaryAtom.TYPE_1, line, addr, opcode, d)
elif m2:
d['mnemonic'] = m2.group(1).strip()
d['src'] = m2.group(2).strip()
d['dst'] = m2.group(3).strip()
self.caller.debug("2 mov 0x18(%rsp),%r12\n")
return BinaryAtom(BinaryAtom.TYPE_2, line, addr, opcode, d)
elif m3:
d['mnemonic'] = m3.group(1).strip()
d['src'] = m3.group(2).strip()
d['jmp-addr'] = m3.group(3).strip()
d['jmp-sym'] = m3.group(4).strip()
self.caller.debug("3 jmpq *0x207132(%rip) # 608218 <_GLOBAL_OFFSET_TABLE_+0x28>\n")
return BinaryAtom(BinaryAtom.TYPE_3, line, addr, opcode, d)
elif m4:
d['mnemonic'] = m4.group(1).strip()
d['src'] = m4.group(2).strip()
d['jmp-addr'] = m4.group(3).strip()
self.caller.debug("4 jne 404e60 <__libc_csu_init+0x50>\n")
return BinaryAtom(BinaryAtom.TYPE_4, line, addr, opcode, d)
elif m5:
d['mnemonic'] = m5.group(1).strip()
d['src'] = m5.group(2).strip()
self.caller.debug("5 jmp 404b20\n")
return BinaryAtom(BinaryAtom.TYPE_5, line, addr, opcode, d)
elif m6:
d['mnemonic'] = m6.group(1).strip()
self.caller.debug("6 ret\n")
return BinaryAtom(BinaryAtom.TYPE_6, line, addr, opcode, d)
else:
err("Something wrong here; %s" % (line))
sys.exit(1)
return None
def process(self, filename):
# maximal instruction length is 15 byte, per
# "Intel® 64 and IA-32 Architectures Software Developer’s Manual"
cmd = 'objdump -d --insn-width=16 %s' % (filename)
context = Context()
self.caller.verbose('pass one: \"%s\"\n' % (cmd))
start_time = time.process_time()
p = subprocess.Popen(cmd.split(), shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
end_time = time.process_time()
self.caller.verbose("objdump took %.2f seconds\n" % (end_time - start_time))
for line in p.stdout.readlines():
atom = self.parse_line(line.decode("utf-8").strip(), context)
if atom is None:
continue
self.caller.process(context, atom)
#if self.args.instruction_analyzer:
# self.module['InstructionAnalyzer'].pass1(context, atom)
# Function Anatomy Analyzer is always processed, because
# the analyzer serves as a ground work for other analyzes
#self.module['FunctionAnalyzer'].pass1(context, atom)
#if self.args.function_branch_jump:
# self.module['FunctionBranchJumpAnalyser'].pass1(context, atom)
#atom.print()
return
def run(self, caller):
self.caller = caller
self.caller.msg("Instruction and Opcode Analyzer - (C) 2014-2021\n")
self.caller.verbose("URL: https://github.com/hgn/instruction-layout-analyzer\n")
self.caller.verbose("Binary to analyze: %s\n" % self.args.filename)
statinfo = os.stat(self.args.filename)
if statinfo.st_size > 1000000:
self.caller.verbose("File larger then 1MByte, analysis may take some time\n")
self.process(self.args.filename)
class FunctionAnalyzer(Common):
def __init__(self):
self.func_excluder = FunctionExcluder()
self.func_excluded = 0
self.parse_local_options()
self.db = dict()
self.db_duplicates = dict()
self.strlen_longest_funcname = 10
self.strlen_largest_size = 4
self.biggest_function = 0
def parse_local_options(self):
parser = optparse.OptionParser()
parser.usage = "InstructionAnalyzer"
parser.add_option( "-v", "--verbose", dest="verbose", default=False,
action="store_true", help="show verbose")
parser.add_option( "-x", "--no-exclude", dest="no_exclude", default=False,
action="store_true", help="do *not* exclude some glibc/gcc helper"
"runtime functions like __init _start or _do_global_dtors_aux")
parser.add_option( "-g", "--graphs", dest="generate_graphs", default=False,
action="store_true", help="generate SVG graphs")
self.opts, args = parser.parse_args(sys.argv[0:])
if len(args) != 3:
self.err("No <binary> argument given, exiting\n")
sys.exit(1)
if self.opts.no_exclude:
# empty list means there will never be a match
self.func_excluder = None
self.verbose("Analyze binary: %s\n" % (sys.argv[-1]))
self.opts.filename = args[-1]
def run(self):
self.parser = Parser(self.opts)
self.parser.run(self)
self.show()
def process_function_pro_epi_logue(self, context, atom, mnemonic_db):
if mnemonic_db['cnt'] < 4:
# we capture the first 10 instructions per functions
mnemonic_db[mnemonic_db['cnt']] = atom.mnemonic
mnemonic_db['captured'] += 1
# this will overwriten each time, at the end this entry will hold
# the last mnemonic for the function
mnemonic_db['-1'] = atom.mnemonic
mnemonic_db['cnt'] += 1
def process_function_duplicate_check(self, context):
if not context.function_name in self.db_duplicates:
self.db_duplicates[context.function_name] = dict()
self.db_duplicates[context.function_name][context.function_start_address] = True
return
self.db_duplicates[context.function_name][context.function_start_address] = True
def process(self, context, atom):
if self.func_excluder and self.func_excluder.is_excluded(context.function_name):
self.func_excluded += 1
return
self.process_function_duplicate_check(context)
self.strlen_longest_funcname = max(len(context.function_name), self.strlen_longest_funcname)
func_id = "%s:%s" % (context.function_name, context.function_start_address)
if not func_id in self.db:
self.db[func_id] = dict()
self.db[func_id]['start'] = \
context.function_start_address
self.db[func_id]['end'] = \
context.function_start_address + atom.opcode_len
self.db[func_id]['size'] = atom.opcode_len
self.db[func_id]['mnemonic'] = dict()
self.db[func_id]['mnemonic']['cnt'] = 0
self.db[func_id]['mnemonic']['captured'] = 0
self.process_function_pro_epi_logue(context, atom, self.db[func_id]['mnemonic'])
self.biggest_function = max(self.db[func_id]['size'], self.biggest_function)
return
self.db[func_id]['end'] += atom.opcode_len
self.db[func_id]['size'] += atom.opcode_len
self.strlen_largest_size = max(len(str(self.db[func_id]['size'])), self.strlen_largest_size)
self.biggest_function = max(self.db[func_id]['size'], self.biggest_function)
# last mnemonic in function
self.process_function_pro_epi_logue(context, atom, self.db[func_id]['mnemonic'])
def show(self, json=False):
self.show_human()
def next_power_of_two(self, n):
return int(math.pow(2, math.ceil(math.log(n, 2))))
def show_function_duplicates(self):
printed_header = False
fmt = "%%%d.%ds: %%3d duplicates\n" % \
(self.strlen_longest_funcname, self.strlen_longest_funcname)
for key in sorted(self.db_duplicates.items(), key=lambda item: len(item[1]), reverse=True):
if len(key[1]) <= 1:
break
if not printed_header:
self.msg_underline("Function Duplicates", pre_news=2, post_news=2)
printed_header = True
self.msg(fmt % (key[0], len(key[1])))
def show_function_size_histogram(self):
if self.opts.generate_graphs:
file_out_name = 'function-size-histogram'
l = pygal.style.LightStyle
l.foreground='black'
l.background='white'
l.plot_background='white'
pie_chart = pygal.Bar(fill=True, style=l, truncate_legend=50,
legend_box_size=10,legend_font_size=14,
margin=20, title_font_size=20,
legend_at_bottom=True)
pie_chart.title = 'Function Size Histogram'
histogram = dict()
histogram[1] = 0 # just for the case
i = 2
while i <= self.next_power_of_two(self.biggest_function):
histogram[i] = 0
i *= 2
for k,v in self.db.items():
power_size = self.next_power_of_two(v['size'])
histogram[power_size] += 1
overall = len(self.db)
i = 2; remain = 0.0
self.msg_underline("Functions Size Histogram", pre_news=2, post_news=2)
while i <= self.next_power_of_two(self.biggest_function):
percent = (float(histogram[i]) / (overall)) * 100.0
self.msg("<= %6d byte: %5d [ %5.2f%% ]\n" % (i, histogram[i], percent))
if self.opts.generate_graphs and percent >= 1.0:
# function buckets with less then 1 percent are not
# plotted by default. They are accounted in the "remain bucket"
pie_chart.add("%d byte [ %4.1f%% ]" % (i, percent), histogram[i])
else:
remain += histogram[i]
i *= 2
if self.opts.generate_graphs and remain > 0:
percent = (remain / (overall)) * 100.0
pie_chart.add("%s [ %4.1f%% ]" % ("Remaining", percent), remain)
if self.opts.generate_graphs:
pie_chart.render_to_file('%s.svg' % (file_out_name))
self.verbose("# created graph file: %s.svg\n" % (file_out_name))
os.system("inkscape --export-png=%s.png %s.svg 1>/dev/null 2>&1" %
(file_out_name, file_out_name))
self.verbose("# created graph file: %s.png\n" % (file_out_name))
def show_common_information(self):
self.msg_underline("Common Information", pre_news=2, post_news=2)
smallest_function = sys.maxsize
for key, value in self.db.items():
smallest_function = min(value['size'], smallest_function)
functions_no = len(self.db)
self.msg("Number of analyzed functions: %d\n" % (functions_no))
if self.func_excluded > 0:
self.msg("Number of excluded functions: %d\n" % (self.func_excluded))
self.msg("Biggest function size: %d byte\n" % (self.biggest_function))
if smallest_function != sys.maxsize:
self.msg("Smallest function size: %d byte\n" % (smallest_function))
def determine_addr_alignment(self, addr_list, address):
for addr_entry in addr_list:
if addr_entry == 0:
return 0
if address % addr_entry == 0:
return addr_entry
def show_function_alignment_info(self):
addr_list = [ 128, 64, 32, 16, 8, 4, 2, 0]
no_functions = 0
self.msg_underline("Function Alignment Info", pre_news=2, post_news=2)
msg = "{:<11} {:>}\n".format("Alignment", 'No Functions [Percent]')
self.msg(msg)
db = dict()
for addr_entry in addr_list:
db[addr_entry] = dict()
db[addr_entry]['functions'] = list()
for key, value in self.db.items():
db[self.determine_addr_alignment(addr_list, value['start'])]['functions'].append(key.split(':')[0])
no_functions += 1
for addr_entry in addr_list:
percent = (float(len(db[addr_entry]['functions'])) / (no_functions))
msg = "{:>8}: {:<} [ {:>6.2%} ]\n".format(addr_entry, len(db[addr_entry]['functions']), percent)
self.msg(msg)
def show_human(self):
# Some overall information about functions
# Number of Functions, average len, min length, max length, etc
self.show_common_information()
self.msg_underline("Functions Size", pre_news=1, post_news=2)
fmt = "%%%d.%ds: %%%dd byte [start: 0x%%x, end: 0x%%x]\n" % \
(self.strlen_longest_funcname, self.strlen_longest_funcname, self.strlen_largest_size)
for key in sorted(self.db.items(), key=lambda item: item[1]['size'], reverse=True):
self.msg(fmt % (key[0].split(':')[0], key[1]['size'], key[1]['start'], key[1]['end']))
self.show_function_size_histogram()
# Function Mnemonic Signature Overview
similar_data = dict()
for key, value in self.db.items():
if self.opts.verbose:
self.msg("%s:\n" % (key.split(':')[0]))
signature = ""
seq = list()
for i in range(self.db[key]['mnemonic']['captured']):
if self.opts.verbose:
self.msg("\t%d %s\n" % (i, self.db[key]['mnemonic'][i]))
signature += self.db[key]['mnemonic'][i]
seq.append(self.db[key]['mnemonic'][i])
if signature in similar_data:
similar_data[signature]['cnt'] += 1
else:
similar_data[signature] = dict()
similar_data[signature]['cnt'] = 1
similar_data[signature]['seq'] = seq
similar_data[signature]['signature'] = signature
self.show_function_duplicates()
self.msg_underline("Functions Prologue Similarity", pre_news=2, post_news=2)
self.msg("No Functions Function Prologue\n")
for key in sorted(similar_data.items(), key=lambda item: item[1]['cnt'], reverse=True):
self.msg("%-6d %s\n" % (key[1]['cnt'], key[1]['seq']))
self.show_function_alignment_info()
def show_json(self):
pass
class InstructionAnalyzer(Common):
def __init__(self):
self.parse_local_options()
self.instructions = dict()
self.sum_opcode_length = 0
self.max_opcode_length = 0
self.db_category = dict()
def parse_local_options(self):
parser = optparse.OptionParser()
parser.usage = "InstructionAnalyzer"
parser.add_option( "-v", "--verbose", dest="verbose", default=False,
action="store_true", help="show verbose")
parser.add_option( "-g", "--graphs", dest="generate_graphs", default=False,
action="store_true", help="generate SVG graphs")
self.opts, args = parser.parse_args(sys.argv[0:])
if len(args) != 3:
self.err("No <binary> argument given, exiting\n")
sys.exit(1)
if self.opts.generate_graphs and not pygal:
self.err("Want graphs but no pygal given!\n")
sys.exit(1)
self.verbose("Analyze binary: %s\n" % (sys.argv[-1]))
self.opts.filename = args[-1]
def run(self):
self.parser = Parser(self.opts)
self.parser.run(self)
self.show()
def add_existing(self, atom):
self.instructions[atom.mnemonic]['count'] += 1
# now account instruction length variability
if atom.opcode_len in self.instructions[atom.mnemonic]['instruction-lengths']:
self.instructions[atom.mnemonic]['instruction-lengths'][atom.opcode_len] += 1
else:
self.instructions[atom.mnemonic]['instruction-lengths'][atom.opcode_len] = 1
def add_new(self, atom):
self.instructions[atom.mnemonic] = dict()
self.instructions[atom.mnemonic]['count'] = 1
self.instructions[atom.mnemonic]['category'] = InstructionCategory.guess(atom.mnemonic)
# ok, this is more complicated but it is suitable for large
# projects with millions of instructions
self.instructions[atom.mnemonic]['instruction-lengths'] = dict()
self.instructions[atom.mnemonic]['instruction-lengths'][atom.opcode_len] = 1
def process_category(self, atom):
cat = InstructionCategory.guess(atom.mnemonic)
if cat in self.db_category:
self.db_category[cat] += 1
else:
self.db_category[cat] = 1
def process(self, context, atom):
self.max_opcode_length = max(self.max_opcode_length, atom.opcode_len)
self.sum_opcode_length += atom.opcode_len
if atom.mnemonic in self.instructions:
self.add_existing(atom)
else:
self.add_new(atom)
self.process_category(atom)
def show_general(self, no_instructions):
self.msg("General Information:\n")
self.msg(" Number Instructions: %d\n" % (no_instructions))
self.msg(" Number different Instructions: %d\n" % (len(self.instructions.keys())))
self.msg(" Overall Opcode length: %d byte\n" % (self.sum_opcode_length))
self.msg(" Maximal Opcode length: %d byte\n" % (self.max_opcode_length))
self.msg("\n")
self.msg_underline("Detailed Analysis", pre_news=2, post_news=3)
self.msg(" Instruction | Count [ %] | Category | Length (avg, min, max)\n")
self.msg("--------------------------------------------------------------------\n")
for k in sorted(self.instructions.items(), key=lambda item: item[1]['count'], reverse=True):
minval = min(k[1]['instruction-lengths'].keys())
maxval = max(k[1]['instruction-lengths'].keys())
sumval = 0.0
for key, value in k[1]['instruction-lengths'].items():
sumval += float(key) * float(value)
sumval /= float(k[1]['count'])
percent = (float(k[1]['count']) / (no_instructions)) * 100.0
self.msg("%15.15s %10d [%5.2f] %13.13s %5.1f,%3.d,%3.d\n" %
(k[0], k[1]['count'], percent, InstructionCategory.str(k[1]['category']), sumval, minval, maxval))