db: replace database info scripts with smdb.py
[smatch.git] / smatch_data / db / smdb.py
blobe0e307044bbd27af9f8b750357260307996cc662
1 #!/usr/bin/python
3 # Copyright (C) 2013 Oracle.
5 # Licensed under the Open Software License version 1.1
7 import sqlite3
8 import sys
9 import re
11 try:
12 con = sqlite3.connect('smatch_db.sqlite')
13 except sqlite3.Error, e:
14 print "Error %s:" % e.args[0]
15 sys.exit(1)
17 def usage():
18 print "%s <function> [table] [type] [parameter]" %(sys.argv[0])
19 sys.exit(1)
21 function_ptrs = []
22 searched_ptrs = []
23 def get_function_pointers_helper(func):
24 cur = con.cursor()
25 cur.execute("select distinct ptr from function_ptr where function = '%s';" %(func))
26 for row in cur:
27 ptr = row[0]
28 if ptr in function_ptrs:
29 continue
30 function_ptrs.append(ptr)
31 if not ptr in searched_ptrs:
32 searched_ptrs.append(ptr)
33 get_function_pointers_helper(ptr)
35 def get_function_pointers(func):
36 global function_ptrs
37 global searched_ptrs
38 function_ptrs = [func]
39 searched_ptrs = [func]
40 get_function_pointers_helper(func)
41 return function_ptrs
43 db_types = [ "INTERNAL", "PARAM_VALUE", "BUF_SIZE", "USER_DATA", "CAPPED_DATA",
44 "RETURN_VALUE", "DEREFERENCE", "RANGE_CAP", "LOCK_HELD",
45 "LOCK_RELEASED", "ABSOLUTE_LIMITS", "LIMITED_VALUE",
46 "ADDED_VALUE", "FILTER_VALUE", "PARAM_CLEARED",
47 "UPPER_CONSTRAINT" ]
49 def type_to_str(type_int):
51 t = int(type_int)
52 if t < len(db_types):
53 return db_types[t]
54 return type_int
56 def type_to_int(type_string):
57 for i in range(len(db_types)):
58 if db_types[i] == type_string:
59 return i
60 return -1
62 def display_caller_info(printed, cur):
63 for txt in cur:
64 if not printed:
65 print "file | caller | function | type | parameter | key | value |"
66 printed = 1
67 print "%20s | %20s | %20s |" %(txt[0], txt[1], txt[2]),
68 print " %10s |" %(type_to_str(txt[5])),
69 print " %d | %s | %s" %(txt[6], txt[7], txt[8])
70 return printed
72 def get_caller_info(ptrs, my_type):
73 cur = con.cursor()
74 printed = 0
75 type_filter = ""
76 if my_type != "":
77 type_filter = "and type = %d" %(type_to_int(my_type))
78 for ptr in ptrs:
79 cur.execute("select * from caller_info where function = '%s' %s;" %(ptr, type_filter))
80 printed = display_caller_info(printed, cur)
82 def print_caller_info(func, my_type = ""):
83 ptrs = get_function_pointers(func)
84 get_caller_info(ptrs, my_type)
86 def print_return_states(func):
87 cur = con.cursor()
88 cur.execute("select * from return_states where function = '%s';" %(func))
89 count = 0
90 for txt in cur:
91 printed = 1
92 if count == 0:
93 print "file | function | return_id | return_value | type | param | key | value |"
94 count += 1
95 print "%s | %s | %2s | %13s" %(txt[0], txt[1], txt[3], txt[4]),
96 print "| %13s |" %(type_to_str(txt[6])),
97 print " %2d | %20s | %20s |" %(txt[7], txt[8], txt[9])
99 def print_return_values(func):
100 cur = con.cursor()
101 cur.execute("select * from return_values where function = '%s';" %(func))
102 count = 0
103 for txt in cur:
104 printed = 1
105 if count == 0:
106 print "file | function | value |"
107 count += 1
108 print "%s | %s | %s |" %(txt[0], txt[1], txt[4]),
110 def print_call_implies(func):
111 cur = con.cursor()
112 cur.execute("select * from call_implies where function = '%s';" %(func))
113 count = 0
114 for txt in cur:
115 if not count:
116 print "file | function | type | param | key | value |"
117 count += 1
118 print "%15s | %15s" %(txt[0], txt[1]),
119 print "| %15s" %(type_to_str(txt[4])),
120 print "| %3d | %15s |" %(txt[5], txt[6])
122 def print_type_size(var):
123 cur = con.cursor()
124 if not re.search("^\(struct ", var):
125 m = re.search('(?<=->)\w+', var)
126 print "searched"
127 if not m:
128 print "Can't determine type for %s" %(var)
129 return
130 var = "%->" + m.group(0)
131 cur.execute("select * from type_size where type like '%s';" %(var))
132 print "file | type | size"
133 for txt in cur:
134 printed = 1
135 print "%15s | %15s | %d" %(txt[0], txt[1], txt[2])
137 def print_fn_ptrs(func):
138 ptrs = get_function_pointers(func)
139 if not ptrs:
140 return
141 print "%s = " %(func),
142 i = 0
143 for p in ptrs:
144 if i > 0:
145 print ",",
146 i = i + 1
147 print "'%s'" %(p),
148 print ""
150 if len(sys.argv) < 2:
151 usage()
153 if len(sys.argv) == 2:
154 func = sys.argv[1]
155 print_caller_info(func)
156 elif sys.argv[1] == "user_data":
157 func = sys.argv[2]
158 print_caller_info(func, "USER_DATA")
159 elif sys.argv[1] == "param_value":
160 func = sys.argv[2]
161 print_caller_info(func, "PARAM_VALUE")
162 elif sys.argv[1] == "function_ptr" or sys.argv[1] == "fn_ptr":
163 func = sys.argv[2]
164 print_fn_ptrs(func)
165 elif sys.argv[1] == "return_states":
166 func = sys.argv[2]
167 print_return_states(func)
168 elif sys.argv[1] == "return_values":
169 func = sys.argv[2]
170 print_return_values(func)
171 elif sys.argv[1] == "call_implies":
172 func = sys.argv[2]
173 print_call_implies(func)
174 elif sys.argv[1] == "type_size":
175 var = sys.argv[2]
176 print_type_size(var)
177 else:
178 usage()