3 # Copyright (C) 2013 Oracle.
5 # Licensed under the Open Software License version 1.1
12 con
= sqlite3
.connect('smatch_db.sqlite')
13 except sqlite3
.Error
, e
:
14 print "Error %s:" % e
.args
[0]
18 print "%s <function> [table] [type] [parameter]" %(sys
.argv
[0])
23 def get_function_pointers_helper(func
):
25 cur
.execute("select distinct ptr from function_ptr where function = '%s';" %(func))
28 if ptr
in function_ptrs
:
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
):
38 function_ptrs
= [func
]
39 searched_ptrs
= [func
]
40 get_function_pointers_helper(func
)
43 db_types
= { 0: "INTERNAL",
54 1009: "LOCK_RELEASED",
55 1010: "ABSOLUTE_LIMITS",
56 1011: "LIMITED_VALUE",
64 def type_to_str(type_int
):
67 if db_types
.has_key(t
):
71 def type_to_int(type_string
):
72 for k
in db_types
.keys():
73 if db_types
[k
] == type_string
:
77 def display_caller_info(printed
, cur
):
80 print "file | caller | function | type | parameter | key | value |"
82 print "%20s | %20s | %20s |" %(txt
[0], txt
[1], txt
[2]),
83 print " %10s |" %(type_to_str(txt
[5])),
84 print " %d | %s | %s" %(txt
[6], txt
[7], txt
[8])
87 def get_caller_info(ptrs
, my_type
):
92 type_filter
= "and type = %d" %(type_to_int(my_type
))
94 cur
.execute("select * from caller_info where function = '%s' %s;" %(ptr
, type_filter
))
95 printed
= display_caller_info(printed
, cur
)
97 def print_caller_info(func
, my_type
= ""):
98 ptrs
= get_function_pointers(func
)
99 get_caller_info(ptrs
, my_type
)
101 def print_return_states(func
):
103 cur
.execute("select * from return_states where function = '%s';" %(func))
108 print "file | function | return_id | return_value | type | param | key | value |"
110 print "%s | %s | %2s | %13s" %(txt
[0], txt
[1], txt
[3], txt
[4]),
111 print "| %13s |" %(type_to_str(txt
[6])),
112 print " %2d | %20s | %20s |" %(txt
[7], txt
[8], txt
[9])
114 def print_call_implies(func
):
116 cur
.execute("select * from call_implies where function = '%s';" %(func))
120 print "file | function | type | param | key | value |"
122 print "%15s | %15s" %(txt
[0], txt
[1]),
123 print "| %15s" %(type_to_str(txt
[4])),
124 print "| %3d | %15s |" %(txt
[5], txt
[6])
126 def print_type_size(struct_type
, member
):
128 cur
.execute("select * from type_size where type like '(struct %s)->%s';" %(struct_type
, member
))
131 print "%-15s | %s" %(txt
[0], txt
[1])
133 cur
.execute("select * from function_type_size where type like '(struct %s)->%s';" %(struct_type
, member
))
134 print "file | function | type | size"
136 print "%-15s | %-15s | %-15s | %s" %(txt
[0], txt
[1], txt
[2], txt
[3])
138 def print_fn_ptrs(func
):
139 ptrs
= get_function_pointers(func
)
142 print "%s = " %(func),
151 def get_callers(func
):
154 ptrs
= get_function_pointers(func
)
156 cur
.execute("select distinct caller from caller_info where function = '%s';" %(ptr))
162 def call_tree_helper(func
, indent
= 0):
164 if func
in printed_funcs
:
166 print "%s%s()" %(" " * indent
, func
)
167 if func
== "too common":
171 printed_funcs
.append(func
)
172 callers
= get_callers(func
)
173 if len(callers
) >= 20:
174 print "Over 20 callers for %s()" %(func)
176 for caller
in callers
:
177 call_tree_helper(caller
, indent
+ 2)
179 def print_call_tree(func
):
182 call_tree_helper(func
)
184 def function_type_value(struct_type
, member
):
186 cur
.execute("select * from function_type_value where type like '(struct %s)->%s';" %(struct_type
, member
))
188 print "%-30s | %-30s | %s | %s" %(txt
[0], txt
[1], txt
[2], txt
[3])
190 if len(sys
.argv
) < 2:
193 if len(sys
.argv
) == 2:
195 print_caller_info(func
)
196 elif sys
.argv
[1] == "user_data":
198 print_caller_info(func
, "USER_DATA")
199 elif sys
.argv
[1] == "param_value":
201 print_caller_info(func
, "PARAM_VALUE")
202 elif sys
.argv
[1] == "function_ptr" or sys
.argv
[1] == "fn_ptr":
205 elif sys
.argv
[1] == "return_states":
207 print_return_states(func
)
208 elif sys
.argv
[1] == "call_implies":
210 print_call_implies(func
)
211 elif sys
.argv
[1] == "type_size" or sys
.argv
[1] == "buf_size":
212 struct_type
= sys
.argv
[2]
214 print_type_size(struct_type
, member
)
215 elif sys
.argv
[1] == "call_tree":
217 print_call_tree(func
)
218 elif sys
.argv
[1] == "where":
219 if len(sys
.argv
) == 3:
222 elif len(sys
.argv
) == 4:
223 struct_type
= sys
.argv
[2]
225 function_type_value(struct_type
, member
)
226 elif sys
.argv
[1] == "local":
227 filename
= sys
.argv
[2]
229 if len(sys
.argv
) == 4:
230 variable
= sys
.argv
[3]
231 local_values(filename
, variable
)