1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 from struct import unpack, pack
20
21 from misc import Color
22 from error import warning, error
23
24 global PRETTY_SHOW
25 PRETTY_SHOW = 0
26
27
29 warning("Error : " + msg)
30 raise("oops")
31
32
34 buff = name + " "
35
36 if type(arg).__name__ == 'int' :
37 buff += "0x%x" % arg
38 elif type(arg).__name__ == 'long' :
39 buff += "0x%x" % arg
40 elif type(arg).__name__ == 'str' :
41 buff += "%s" % arg
42 elif isinstance(arg, SV) :
43 buff += "0x%x" % arg.get_value()
44 elif isinstance(arg, SVs) :
45 buff += arg.get_value().__str__()
46
47 print buff
48
52
60
62 paths = []
63 for i in basic_blocks :
64 val = 0
65 if len(i.childs) > 1 :
66 val = 1
67 elif len(i.childs) == 1 :
68 val = 2
69
70 for j in i.childs :
71 paths.append( ( j[0], j[1], val ) )
72 if val == 1 :
73 val = 0
74
75 nb = 0
76 idx = 0
77 for bb in basic_blocks :
78 for ins in bb.ins :
79 p = []
80 for j in paths :
81 way = Color.green
82 if j[2] == 1 :
83 way = Color.red
84 elif j[2] == 2 :
85 way = Color.blue
86
87 m_in = j[0]
88 m_ax = j[1]
89 if j[0] > j[1] :
90 m_in = j[1]
91 m_ax = j[0]
92
93 if idx >= m_in and idx <= m_ax :
94 if idx == j[0] :
95 p.append( j[1] )
96 print "o",
97 if idx == j[1] :
98 print "%s>%s" % (way, Color.normal),
99
100 if idx != j[0] and idx != j[1] :
101 print "%s|%s" % (way, Color.normal),
102 else :
103 print " ",
104
105 print "%s%d%s(%s%x%s)" % (Color.yellow, nb, Color.normal, Color.yellow, idx, Color.normal),
106 ins.show( idx )
107
108 if p != [] :
109 print "%s[" % Color.green, ' '.join("%x" % i for i in p), "]%s" % Color.normal,
110 print
111
112 idx += ( ins.get_length() )
113 nb += 1
114
115
117 idx = 0
118 nb = 0
119 for i in basic_blocks :
120
121 print "%s%s%s : " % (Color.purple, i.name, Color.normal)
122 for ins in i.ins :
123 print "\t%s%d%s(%s%x%s)" % (Color.yellow, nb, Color.normal, Color.yellow, idx, Color.normal),
124 ins.show( idx )
125
126 if ins == i.ins[-1] and i.childs != [] :
127 if len(i.childs) == 2 :
128 print "%s[ %s%s " % (Color.red, i.childs[0][2].name, Color.green),
129 print ' '.join("%s" % c[2].name for c in i.childs[1:]), "]%s" % Color.normal,
130 else :
131 print "%s[" % Color.blue, ' '.join("%s" % c[2].name for c in i.childs), "]%s" % Color.normal,
132
133 idx += ins.get_length()
134 nb += 1
135
136 print
137 print
138
140 idx = 0
141 nb = 0
142 for i in basic_blocks :
143
144 print "%s : " % (i.name)
145 for ins in i.ins :
146 print "\t%d(%x)" % (nb, idx),
147 ins.show( idx )
148
149 if ins == i.ins[-1] and i.childs != [] :
150 print "[", ' '.join("%s" % c[2].name for c in i.childs), "]",
151
152 idx += ins.get_length()
153 nb += 1
154
155 print
156 print
157
158
160 idx = 0
161 nb = 0
162 for i in basic_blocks :
163 if i.bb_tag == 1 :
164 print "%sDIFF%s" % (Color.cyan, Color.normal),
165 elif i.bb_tag == 2 :
166 print "%sNEW%s" %(Color.cyan, Color.normal),
167
168 print "%s%s%s : " % (Color.purple, i.name, Color.normal)
169 for ins in i.ins :
170 print "\t%s%d%s(%s%x%s)" % (Color.yellow, nb, Color.normal, Color.yellow, idx, Color.normal),
171
172 try :
173 tag = getattr(ins, "diff_tag")
174 except AttributeError :
175 tag = 0
176
177 if tag == 1 :
178 print "%s" % Color.green,
179 elif tag == 2 :
180 print "%s" % Color.red,
181
182 ins.show( idx )
183
184 childs = None
185 try :
186 childs = getattr( ins, "childs" )
187 except AttributeError :
188 if ins == i.ins[-1] :
189 if i.childs != [] :
190 childs = i.childs
191
192 if childs != None and childs != [] :
193 if len(childs) == 2 :
194 print "%s[ %s%s " % (Color.red, childs[0][2].name, Color.green),
195 print ' '.join("%s" % c[2].name for c in childs[1:]), "]%s" % Color.normal,
196 else :
197 print "%s[" % Color.blue, ' '.join("%s" % c[2].name for c in childs), "]%s" % Color.normal,
198
199 if tag == 0 :
200 idx += ins.get_length()
201
202 nb += 1
203
204 print
205 print
206
208 """
209
210 """
211 buff = ""
212 for i in mx.basic_blocks.get() :
213 val = "green"
214 if len(i.childs) > 1 :
215 val = "red"
216 elif len(i.childs) == 1 :
217 val = "blue"
218
219 for j in i.childs :
220 buff += "\"%s\" -> \"%s\" [color=\"%s\"];\n" % ( i.get_name(), j[-1].get_name(), val )
221 if val == "red" :
222 val = "green"
223
224 idx = i.start
225 label = ""
226 for ins in i.ins :
227 label += "%x %s\l" % (idx, ins.show_buff(idx))
228 idx += ins.get_length()
229
230 buff += "\"%s\" [color=\"lightgray\", label=\"%s\"]\n" % (i.get_name(), label)
231 return buff
232
256
257 -def method2png( output, mx = None, raw = False ) :
258 """
259
260 """
261 buff = raw
262 if raw == False :
263 buff = method2dot( mx )
264
265 method2format( output, "png", mx, buff )
266
268 """SV is used to handle more easily a value"""
270 self.__size = size
271 self.__value = unpack(self.__size, buff)[0]
272
274 return pack(self.__size, self.__value)
275
277 return "0x%x" % self.__value
278
281
284
287
290
292 """SVs is used to handle more easily a structure of different values"""
293 - def __init__(self, size, ntuple, buff) :
294 self.__size = size
295
296 self.__value = ntuple._make( unpack( self.__size, buff ) )
297
299 l = []
300 for i in self.__value._fields :
301 l.append( getattr( self.__value, i ) )
302 return pack( self.__size, *l)
303
305 return [ x for x in self.__value._fields ]
306
309
312
314 self.__value = self.__value._replace( **attr )
315
318
320 - def show(self, value) :
321 getattr(self, "show_" + value)()
322
325 self.__buff = buff
326 self.__idx = 0
327
329 return self.__buff[ self.__idx : self.__idx + size ]
330
331 - def read(self, size) :
332 if isinstance(size, SV) :
333 size = size.value
334
335 buff = self.__buff[ self.__idx : self.__idx + size ]
336 self.__idx += size
337
338 return buff
339
341 return self.__idx == len(self.__buff)
342
349
352 try :
353 pass
354
355
356 except ImportError :
357 warning("module psyco not found")
358
359 self.__buff = buff
360 self.__idx = 0
361
362
363 - def read(self, size) :
364 if isinstance(size, SV) :
365 size = size.value
366
367 buff = self.__buff[ self.__idx : self.__idx + size ]
368 self.__idx += size
369
370 return buff
371
373 if isinstance(off, SV) :
374 off = off.value
375
376 return self.__buff[ off : ]
377
379 return self.__buff[ self.__idx : self.__idx + size ]
380
382 if isinstance(idx, SV) :
383 self.__idx = idx.value
384 else :
385 self.__idx = idx
386
389
392
393 - def register(self, type_register, fct) :
394 self.__registers[ type_register ].append( fct )
395
398
400 return len( self.__buff )
401
402 - def save(self, filename) :
403 fd = open(filename, "w")
404 buff = self._save()
405 fd.write( buff )
406 fd.close()
407
415
422
429
439
440
442 for _class in vm.get_classes() :
443
444 name = "CLASS_" + FormatClassToPython( _class.get_name() )
445 setattr( vm, name, _class )
446
447
448 m = {}
449 for method in _class.get_methods() :
450 if method.get_name() not in m :
451 m[ method.get_name() ] = []
452 m[ method.get_name() ].append( method )
453
454 for i in m :
455 if len(m[i]) == 1 :
456 j = m[i][0]
457 name = "METHOD_" + FormatNameToPython( j.get_name() )
458 setattr( _class, name, j )
459 else :
460 for j in m[i] :
461 name = "METHOD_" + FormatNameToPython( j.get_name() ) + FormatDescriptorToPython( j.get_descriptor() )
462 setattr( _class, name, j )
463
464
465 f = {}
466 for field in _class.get_fields() :
467 if field.get_name() not in f :
468 f[ field.get_name() ] = []
469 f[ field.get_name() ].append( field )
470
471 for i in f :
472 if len(f[i]) == 1 :
473 j = f[i][0]
474 name = "FIELD_" + FormatNameToPython( j.get_name() )
475 setattr( _class, name, j )
476 else :
477 for j in f[i] :
478 name = "FIELD_" + FormatNameToPython( j.get_name() ) + FormatDescriptorToPython( j.get_descriptor() )
479 setattr( _class, name, j )
480