-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwcm_create_db.py
215 lines (175 loc) · 5.75 KB
/
wcm_create_db.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
import glob
import os
import re
import sqlite3
import sys
CONFIG_DEBUG = False
UNKNOWN_FILE = "__UNK__"
RE_RTL_FUNC = re.compile(r"^;; Function ([\w\.]+)")
RE_REF = re.compile(r"^.*\(symbol_ref:SI.*<(\S+)\s+\S+\s+([^>]+)")
def debug(s):
if CONFIG_DEBUG:
print("debug: %s" % s)
def warning(s):
print("warning: %s" % s)
def get_lines(filename):
encodings = ["utf-8", "iso-8859-1", "ascii"]
for encoding in encodings:
with open(filename, encoding=encoding, mode="r") as f:
try:
return f.readlines()
except UnicodeDecodeError:
# failed to decode, try some other decoding
pass
return []
def parse_rtl(filename):
calls = {}
functions = []
function = None
obj = os.path.basename(filename).split(".")[0]
for line in get_lines(filename):
if len(line) == 1:
continue
line = line.rstrip()
m = RE_RTL_FUNC.match(line)
if m:
function = m.group(1)
functions.append(function)
continue
if not function:
continue
m = RE_REF.match(line)
if m:
if m.group(1) == "function_decl":
if function not in calls:
calls[function] = set()
calls[function].add(m.group(2))
elif m.group(1) == "var_decl":
continue
else:
warning("parse_rtl: unknown decl: %s" % m.group(1))
return obj, functions, calls
def create_tables(cursor):
cursor.execute(
"""
CREATE TABLE files(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
)"""
)
cursor.execute(
"""
CREATE TABLE functions(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
fileid INTEGER NOT NULL,
name TEXT NOT NULL
)"""
)
cursor.execute(
"""
CREATE TABLE calls(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
callerid INTEGER NOT NULL,
calleeid INTEGER NOT NULL
)"""
)
def add_file(cursor, name):
cursor.execute("INSERT INTO files (name) VALUES (?)", (name,))
def add_function(cursor, fileid, name):
cursor.execute(
"""
INSERT INTO functions (
fileid,
name
) VALUES (?, ?)""",
(fileid, name),
)
def add_call(cursor, callerid, calleeid):
cursor.execute(
"""
INSERT INTO calls (
callerid,
calleeid
) VALUES (?, ?)""",
(callerid, calleeid),
)
def get_file_id(cursor, name):
cursor.execute("SELECT id FROM files WHERE name = ?", (name,))
return cursor.fetchone()[0]
def _get_function_id(cursor, fileid, name):
if fileid is None:
cursor.execute("SELECT id FROM functions WHERE name = ?", (name,))
else:
cursor.execute(
"""
SELECT id
FROM functions
WHERE fileid = ? AND name = ?""",
(fileid, name),
)
return cursor.fetchone()[0]
def get_function_id(cursor, fileid, name):
try:
return _get_function_id(cursor, fileid, name)
except TypeError:
pass
unknown_file_id = get_file_id(cursor, UNKNOWN_FILE)
debug("get_function_id: try to find %s in %s" % (name, UNKNOWN_FILE))
try:
return _get_function_id(cursor, unknown_file_id, name)
except TypeError:
pass
debug("get_function_id: add %s to %s" % (name, UNKNOWN_FILE))
add_function(cursor, unknown_file_id, name)
try:
return _get_function_id(cursor, unknown_file_id, name)
except TypeError:
print("fatal: get_function_id: could not find function %s" % name)
sys.exit(-1)
if __name__ == "__main__":
if len(sys.argv) < 3:
print(
"usage:\n\t%s <database.db> <f1.c.expand> [<fn.c.expand> ...]"
% os.path.basename(sys.argv[0])
)
sys.exit(-1)
if os.path.isfile(sys.argv[1]):
os.unlink(sys.argv[1])
connection = sqlite3.connect(sys.argv[1])
cursor = connection.cursor()
create_tables(cursor)
connection.commit()
files = []
for arg in sys.argv[2:]:
for file in glob.glob(arg):
files.append(file)
files = sorted(files)
cache = []
for file in files:
cache.append(parse_rtl(file))
for name, _, _ in cache:
debug("add_file(%s)" % (name))
add_file(cursor, name)
debug("add_file(%s)" % (name))
add_file(cursor, UNKNOWN_FILE)
connection.commit()
for name, functions, _ in cache:
fileid = get_file_id(cursor, name)
for function in functions:
debug("add_function(%s, %s)" % (name, function))
add_function(cursor, fileid, function)
connection.commit()
for name, functions, calls in cache:
for caller, callees in calls.items():
localfileid = get_file_id(cursor, name)
callerid = get_function_id(cursor, localfileid, caller)
for callee in callees:
if callee in functions:
debug("local: %s -> %s" % (caller, callee))
calleeid = get_function_id(cursor, localfileid, callee)
else:
debug("remote: %s -> %s" % (caller, callee))
calleeid = get_function_id(cursor, None, callee)
add_call(cursor, callerid, calleeid)
connection.commit()
connection.close()