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" %(sys
.argv
[0])
19 print "<function> - how a function is called"
20 print "return_states <function> - what a function returns"
21 print "call_tree <function> - show the call tree"
22 print "where <struct_type> <member> - where a struct member is set"
23 print "type_size <struct_type> <member> - how a struct member is allocated"
24 print "data_info <struct_type> <member> - information about a given data type"
25 print "function_ptr <function> - which function pointers point to this"
26 print "trace_param <function> <param> - trace where a parameter came from"
31 def get_function_pointers_helper(func
):
33 cur
.execute("select distinct ptr from function_ptr where function = '%s';" %(func))
36 if ptr
in function_ptrs
:
38 function_ptrs
.append(ptr
)
39 if not ptr
in searched_ptrs
:
40 searched_ptrs
.append(ptr
)
41 get_function_pointers_helper(ptr
)
43 def get_function_pointers(func
):
46 function_ptrs
= [func
]
47 searched_ptrs
= [func
]
48 get_function_pointers_helper(func
)
51 db_types
= { 0: "INTERNAL",
63 1009: "LOCK_RELEASED",
64 1010: "ABSOLUTE_LIMITS",
74 1023: "UNTRACKED_PARAM",
79 1028: "COMPARE_LIMIT",
80 1029: "PARAM_COMPARE",
83 8019: "NO_OVERFLOW_SIMPLE",
90 def add_range(rl
, min_val
, max_val
):
97 return [[min_val
, max_val
]]
99 for idx
in range(len(rl
)):
103 # we already merged the new range but we might need to change later
104 # ranges if they over lap with more than one
106 # join with added range
107 if max_val
+ 1 == cur_min
:
108 ret
[len(ret
) - 1][1] = cur_max
112 if max_val
< cur_min
:
113 ret
.append([cur_min
, cur_max
])
117 if max_val
< cur_max
:
118 ret
[len(ret
) - 1][1] = cur_max
124 # join 2 ranges into one
125 if max_val
+ 1 == cur_min
:
126 ret
.append([min_val
, cur_max
])
129 # range is entirely below
130 if max_val
< cur_min
:
131 ret
.append([min_val
, max_val
])
132 ret
.append([cur_min
, cur_max
])
135 # range is partially below
136 if min_val
< cur_min
:
137 if max_val
<= cur_max
:
138 ret
.append([min_val
, cur_max
])
142 ret
.append([min_val
, max_val
])
145 # range already included
146 if max_val
<= cur_max
:
147 ret
.append([cur_min
, cur_max
])
150 # range partially above
151 if min_val
<= cur_max
:
152 ret
.append([cur_min
, max_val
])
155 # join 2 ranges on the other side
156 if min_val
- 1 == cur_max
:
157 ret
.append([cur_min
, max_val
])
161 ret
.append([cur_min
, cur_max
])
163 if idx
+ 1 < len(rl
): # we hit a break statement
164 ret
= ret
+ rl
[idx
+ 1:]
165 elif done
: # we hit a break on the last iteration
167 elif not check_next
: # it's past the end of the rl
168 ret
.append([min_val
, max_val
])
172 def rl_union(rl1
, rl2
):
175 ret
= add_range(ret
, r
[0], r
[1])
177 ret
= add_range(ret
, r
[0], r
[1])
179 if (rl1
or rl2
) and not ret
:
180 print "bug: merging %s + %s gives empty" %(rl1
, rl2
)
187 elif txt
== "s32min":
189 elif txt
== "s16min":
191 elif txt
== "s64max":
193 elif txt
== "s32max":
195 elif txt
== "s16max":
197 elif txt
== "u64max":
199 elif txt
== "u32max":
201 elif txt
== "u16max":
212 elif val
== -(2**31):
214 elif val
== -(2**15):
216 elif val
== 2**63 - 1:
218 elif val
== 2**31 - 1:
220 elif val
== 2**15 - 1:
222 elif val
== 2**64 - 1:
224 elif val
== 2**32 - 1:
226 elif val
== 2**16 - 1:
233 def get_next_str(txt
):
245 elif txt
[0] == 's' or txt
[0] == 'u':
251 for char
in txt
[parsed
:]:
263 pairs
= txt
.split(",")
265 cnt
, min_str
= get_next_str(pair
)
269 cnt
, max_str
= get_next_str(pair
[cnt
+ 1:])
270 min_val
= txt_to_val(min_str
)
271 max_val
= txt_to_val(max_str
)
272 ret
.append([min_val
, max_val
])
274 # Hm... Smatch won't call INT_MAX s32max if the variable is unsigned.
275 # if txt != rl_to_txt(ret):
276 # print "bug: converting: text = %s rl = %s internal = %s" %(txt, rl_to_txt(ret), ret)
282 for idx
in range(len(rl
)):
289 if cur_min
== cur_max
:
290 ret
+= val_to_txt(cur_min
)
292 ret
+= val_to_txt(cur_min
)
294 ret
+= val_to_txt(cur_max
)
297 def type_to_str(type_int
):
300 if db_types
.has_key(t
):
304 def type_to_int(type_string
):
305 for k
in db_types
.keys():
306 if db_types
[k
] == type_string
:
310 def display_caller_info(printed
, cur
, param_names
):
313 print "file | caller | function | type | parameter | key | value |"
316 parameter
= int(txt
[6])
318 if len(param_names
) and parameter
in param_names
:
319 key
= key
.replace("$", param_names
[parameter
])
321 print "%20s | %20s | %20s |" %(txt
[0], txt
[1], txt
[2]),
322 print " %10s |" %(type_to_str(txt
[5])),
323 print " %d | %s | %s" %(parameter
, key
, txt
[8])
326 def get_caller_info(filename
, ptrs
, my_type
):
328 param_names
= get_param_names(filename
, func
)
332 type_filter
= "and type = %d" %(type_to_int(my_type
))
334 cur
.execute("select * from caller_info where function = '%s' %s;" %(ptr
, type_filter
))
335 printed
= display_caller_info(printed
, cur
, param_names
)
337 def print_caller_info(filename
, func
, my_type
= ""):
338 ptrs
= get_function_pointers(func
)
339 get_caller_info(filename
, ptrs
, my_type
)
341 def merge_values(param_names
, vals
, cur
):
343 parameter
= int(txt
[0])
345 rl
= txt_to_rl(txt
[2])
346 if parameter
in param_names
:
347 name
= name
.replace("$", param_names
[parameter
])
349 if not parameter
in vals
:
352 # the first item on the list is the number of rows. it's incremented
353 # every time we call merge_values().
354 if name
in vals
[parameter
]:
355 vals
[parameter
][name
] = [vals
[parameter
][name
][0] + 1, rl_union(vals
[parameter
][name
][1], rl
)]
357 vals
[parameter
][name
] = [1, rl
]
359 def get_param_names(filename
, func
):
362 cur
.execute("select parameter, value from parameter_name where file = '%s' and function = '%s';" %(filename
, func
))
364 parameter
= int(txt
[0])
366 param_names
[parameter
] = name
370 cur
.execute("select parameter, value from parameter_name where function = '%s';" %(func))
372 parameter
= int(txt
[0])
374 param_names
[parameter
] = name
377 def get_caller_count(ptrs
):
381 cur
.execute("select count(distinct(call_id)) from caller_info where function = '%s';" %(ptr))
386 def print_merged_caller_values(filename
, func
, ptrs
, param_names
, call_cnt
):
390 cur
.execute("select parameter, key, value from caller_info where function = '%s' and type = %d;" %(ptr
, type_to_int("PARAM_VALUE")))
391 merge_values(param_names
, vals
, cur
);
393 for param
in sorted(vals
):
394 for name
in sorted(vals
[param
]):
395 if vals
[param
][name
][0] != call_cnt
:
397 print "%d %s -> %s" %(param
, name
, rl_to_txt(vals
[param
][name
][1]))
400 def print_unmerged_caller_values(filename
, func
, ptrs
, param_names
):
404 cur
.execute("select file, caller, call_id, parameter, key, value from caller_info where function = '%s' and type = %d;" %(ptr
, type_to_int("PARAM_VALUE")))
405 for filename
, caller
, call_id
, parameter
, name
, value
in cur
:
406 if prev
!= int(call_id
):
409 parameter
= int(parameter
)
410 if parameter
< len(param_names
):
411 name
= name
.replace("$", param_names
[parameter
])
413 name
= name
.replace("$", "$%d" %(parameter))
415 print "%s | %s | %s | %s" %(filename
, caller
, name
, value
)
416 print "=========================="
418 def print_caller_values(filename
, func
, ptrs
):
419 param_names
= get_param_names(filename
, func
)
420 call_cnt
= get_caller_count(ptrs
)
422 print_merged_caller_values(filename
, func
, ptrs
, param_names
, call_cnt
)
423 print "=========================="
424 print_unmerged_caller_values(filename
, func
, ptrs
, param_names
)
426 def caller_info_values(filename
, func
):
427 ptrs
= get_function_pointers(func
)
428 print_caller_values(filename
, func
, ptrs
)
430 def print_return_states(func
):
432 cur
.execute("select * from return_states where function = '%s';" %(func))
437 print "file | function | return_id | return_value | type | param | key | value |"
439 print "%s | %s | %2s | %13s" %(txt
[0], txt
[1], txt
[3], txt
[4]),
440 print "| %13s |" %(type_to_str(txt
[6])),
441 print " %2d | %20s | %20s |" %(txt
[7], txt
[8], txt
[9])
443 def print_return_implies(func
):
445 cur
.execute("select * from return_implies where function = '%s';" %(func))
449 print "file | function | type | param | key | value |"
451 print "%15s | %15s" %(txt
[0], txt
[1]),
452 print "| %15s" %(type_to_str(txt
[4])),
453 print "| %3d | %s | %15s |" %(txt
[5], txt
[6], txt
[7])
455 def print_type_size(struct_type
, member
):
457 cur
.execute("select * from type_size where type like '(struct %s)->%s';" %(struct_type
, member
))
460 print "%-15s | %s" %(txt
[0], txt
[1])
462 cur
.execute("select * from function_type_size where type like '(struct %s)->%s';" %(struct_type
, member
))
463 print "file | function | type | size"
465 print "%-15s | %-15s | %-15s | %s" %(txt
[0], txt
[1], txt
[2], txt
[3])
467 def print_data_info(struct_type
, member
):
469 cur
.execute("select * from data_info where data like '(struct %s)->%s';" %(struct_type
, member
))
470 print "file | data | type | value"
472 print "%-15s | %-15s | %-15s | %s" %(txt
[0], txt
[1], type_to_str(txt
[2]), txt
[3])
474 def print_fn_ptrs(func
):
475 ptrs
= get_function_pointers(func
)
478 print "%s = " %(func),
487 def print_functions(member
):
489 cur
.execute("select * from function_ptr where ptr like '%%->%s';" %(member))
490 print "File | Pointer | Function | Static"
492 print "%-15s | %-15s | %-15s | %s" %(txt
[0], txt
[2], txt
[1], txt
[3])
494 def get_callers(func
):
497 ptrs
= get_function_pointers(func
)
499 cur
.execute("select distinct caller from caller_info where function = '%s';" %(ptr))
505 def call_tree_helper(func
, indent
= 0):
507 if func
in printed_funcs
:
509 print "%s%s()" %(" " * indent
, func
)
510 if func
== "too common":
514 printed_funcs
.append(func
)
515 callers
= get_callers(func
)
516 if len(callers
) >= 20:
517 print "Over 20 callers for %s()" %(func)
519 for caller
in callers
:
520 call_tree_helper(caller
, indent
+ 2)
522 def print_call_tree(func
):
525 call_tree_helper(func
)
527 def function_type_value(struct_type
, member
):
529 cur
.execute("select * from function_type_value where type like '(struct %s)->%s';" %(struct_type
, member
))
531 print "%-30s | %-30s | %s | %s" %(txt
[0], txt
[1], txt
[2], txt
[3])
533 def trace_callers(func
, param
):
538 ptrs
= get_function_pointers(func
)
540 cur
.execute("select type, caller, value from caller_info where function = '%s' and (type = 0 or type = 1014 or type = 1028) and (parameter = -1 or parameter = %d);" %(ptr
, param
))
542 data_type
= int(row
[0])
543 if data_type
== 1014:
544 sources
.append((row
[1], row
[2]))
545 elif data_type
== 1028:
546 sources
.append(("%", row
[2])) # hack...
547 elif data_type
== 0 and prev_type
== 0:
548 sources
.append((row
[1], ""))
549 prev_type
= data_type
552 def trace_param_helper(func
, param
, indent
= 0):
554 if func
in printed_funcs
:
556 print "%s%s(param %d)" %(" " * indent
, func
, param
)
557 if func
== "too common":
561 printed_funcs
.append(func
)
562 sources
= trace_callers(func
, param
)
565 if len(path
[1]) and path
[1][0] == 'p' and path
[1][1] == ' ':
567 trace_param_helper(path
[0], p
, indent
+ 2)
568 elif len(path
[0]) and path
[0][0] == '%':
569 print " %s%s" %(" " * indent
, path
[1])
571 print "* %s%s %s" %(" " * (indent
- 1), path
[0], path
[1])
573 def trace_param(func
, param
):
576 print "tracing %s %d" %(func
, param
)
577 trace_param_helper(func
, param
)
579 if len(sys
.argv
) < 2:
582 if len(sys
.argv
) == 2:
584 print_caller_info("", func
)
585 elif sys
.argv
[1] == "call_info":
586 if len(sys
.argv
) != 4:
588 filename
= sys
.argv
[2]
590 caller_info_values(filename
, func
)
591 print_caller_info(filename
, func
)
592 elif sys
.argv
[1] == "user_data":
594 print_caller_info(filename
, func
, "USER_DATA")
595 elif sys
.argv
[1] == "param_value":
597 print_caller_info(filename
, func
, "PARAM_VALUE")
598 elif sys
.argv
[1] == "function_ptr" or sys
.argv
[1] == "fn_ptr":
601 elif sys
.argv
[1] == "return_states":
603 print_return_states(func
)
604 print "================================================"
605 print_return_implies(func
)
606 elif sys
.argv
[1] == "return_implies":
608 print_return_implies(func
)
609 elif sys
.argv
[1] == "type_size" or sys
.argv
[1] == "buf_size":
610 struct_type
= sys
.argv
[2]
612 print_type_size(struct_type
, member
)
613 elif sys
.argv
[1] == "data_info":
614 struct_type
= sys
.argv
[2]
616 print_data_info(struct_type
, member
)
617 elif sys
.argv
[1] == "call_tree":
619 print_call_tree(func
)
620 elif sys
.argv
[1] == "where":
621 if len(sys
.argv
) == 3:
624 elif len(sys
.argv
) == 4:
625 struct_type
= sys
.argv
[2]
627 function_type_value(struct_type
, member
)
628 elif sys
.argv
[1] == "local":
629 filename
= sys
.argv
[2]
631 if len(sys
.argv
) == 4:
632 variable
= sys
.argv
[3]
633 local_values(filename
, variable
)
634 elif sys
.argv
[1] == "functions":
636 print_functions(member
)
637 elif sys
.argv
[1] == "trace_param":
638 if len(sys
.argv
) != 4:
641 param
= int(sys
.argv
[3])
642 trace_param(func
, param
)