-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpyc_dbg_data.py
More file actions
99 lines (78 loc) · 2.26 KB
/
Copy pathpyc_dbg_data.py
File metadata and controls
99 lines (78 loc) · 2.26 KB
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
# Copyright 2013 anthony cantor
# This file is part of pyc.
#
# pyc 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.
#
# pyc 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 pyc. If not, see <http://www.gnu.org/licenses/>.
import pyc_lineage
import pyc_asm_nodes
import pickle
import struct
def get_vars(live_set):
result = set([])
for n in live_set:
if isinstance(n, pyc_asm_nodes.Register): continue
result.add(n.name)
return result
def bloc_table(blocs):
d = {}
for bloc in blocs:
real_insns = []
for ins in bloc.insns:
if isinstance(ins, pyc_asm_nodes.PseudoInst):
continue
real_insns.append({
'sir_lineno': ins.origin.lineno,
'src_lineno': pyc_lineage.src_lineno(ins.origin),
'live': get_vars(ins.live),
'dummy': False
})
bloc_src_lineno = pyc_lineage.src_lineno(bloc.origin)
dummy_prefix_insns = [{
'src_lineno': bloc_src_lineno,
'sir_lineno': bloc.origin.lineno,
'live': set([]),
'dummy': True
}]*bloc.preamble_size()
dummy_suffix_insns = [{
'src_lineno': bloc_src_lineno,
'sir_lineno': bloc.origin.lineno,
'live': set([]),
'dummy': True
}]*bloc.postamble_size()
d[bloc.name] = {
'insns': dummy_prefix_insns + real_insns + dummy_suffix_insns,
'src_lineno': bloc_src_lineno,
'sir_lineno': bloc.origin.lineno,
'mem_map': bloc.symtbl.mem_map
}
return d
def encode_as_byte_list(str):
return ", ".join([
hex(ord(x)) for x in str
])
def headers(src, sir_src, blocs, name_map):
data = dump(src, sir_src, blocs, name_map)
headers = [
".section .pyc_dbg",
".byte %s" % encode_as_byte_list(data)
]
return headers
def dump(src, sir_src, blocs, name_map):
b_table = bloc_table(blocs)
dbg_table = {
'src': src,
'sir_src': sir_src,
'blocs': b_table,
'name_map': name_map
}
return pickle.dumps(dbg_table)