From 9a118ecc74d7ea27e4eb3f382d946f79e20b266d Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Sat, 20 Apr 2013 13:43:26 +0300 Subject: [PATCH] db: replace database info scripts with smdb.py The database has a lot of really useful information for understanding code, especially for looking up how function pointers are called. The shell scripts weren't good at handling function pointers. Signed-off-by: Dan Carpenter --- smatch_data/db/smdb.py | 178 ++++++++++++++++++++++++++++++++++ smatch_data/db/smdb_function_info.sh | 30 ------ smatch_data/db/smdb_param_buf_size.sh | 41 -------- smatch_data/db/smdb_param_values.sh | 41 -------- smatch_data/db/smdb_return_values.sh | 15 --- 5 files changed, 178 insertions(+), 127 deletions(-) create mode 100755 smatch_data/db/smdb.py delete mode 100755 smatch_data/db/smdb_function_info.sh delete mode 100755 smatch_data/db/smdb_param_buf_size.sh delete mode 100755 smatch_data/db/smdb_param_values.sh delete mode 100755 smatch_data/db/smdb_return_values.sh diff --git a/smatch_data/db/smdb.py b/smatch_data/db/smdb.py new file mode 100755 index 00000000..e0e30704 --- /dev/null +++ b/smatch_data/db/smdb.py @@ -0,0 +1,178 @@ +#!/usr/bin/python + +# Copyright (C) 2013 Oracle. +# +# Licensed under the Open Software License version 1.1 + +import sqlite3 +import sys +import re + +try: + con = sqlite3.connect('smatch_db.sqlite') +except sqlite3.Error, e: + print "Error %s:" % e.args[0] + sys.exit(1) + +def usage(): + print "%s [table] [type] [parameter]" %(sys.argv[0]) + sys.exit(1) + +function_ptrs = [] +searched_ptrs = [] +def get_function_pointers_helper(func): + cur = con.cursor() + cur.execute("select distinct ptr from function_ptr where function = '%s';" %(func)) + for row in cur: + ptr = row[0] + if ptr in function_ptrs: + continue + function_ptrs.append(ptr) + if not ptr in searched_ptrs: + searched_ptrs.append(ptr) + get_function_pointers_helper(ptr) + +def get_function_pointers(func): + global function_ptrs + global searched_ptrs + function_ptrs = [func] + searched_ptrs = [func] + get_function_pointers_helper(func) + return function_ptrs + +db_types = [ "INTERNAL", "PARAM_VALUE", "BUF_SIZE", "USER_DATA", "CAPPED_DATA", + "RETURN_VALUE", "DEREFERENCE", "RANGE_CAP", "LOCK_HELD", + "LOCK_RELEASED", "ABSOLUTE_LIMITS", "LIMITED_VALUE", + "ADDED_VALUE", "FILTER_VALUE", "PARAM_CLEARED", + "UPPER_CONSTRAINT" ] + +def type_to_str(type_int): + + t = int(type_int) + if t < len(db_types): + return db_types[t] + return type_int + +def type_to_int(type_string): + for i in range(len(db_types)): + if db_types[i] == type_string: + return i + return -1 + +def display_caller_info(printed, cur): + for txt in cur: + if not printed: + print "file | caller | function | type | parameter | key | value |" + printed = 1 + print "%20s | %20s | %20s |" %(txt[0], txt[1], txt[2]), + print " %10s |" %(type_to_str(txt[5])), + print " %d | %s | %s" %(txt[6], txt[7], txt[8]) + return printed + +def get_caller_info(ptrs, my_type): + cur = con.cursor() + printed = 0 + type_filter = "" + if my_type != "": + type_filter = "and type = %d" %(type_to_int(my_type)) + for ptr in ptrs: + cur.execute("select * from caller_info where function = '%s' %s;" %(ptr, type_filter)) + printed = display_caller_info(printed, cur) + +def print_caller_info(func, my_type = ""): + ptrs = get_function_pointers(func) + get_caller_info(ptrs, my_type) + +def print_return_states(func): + cur = con.cursor() + cur.execute("select * from return_states where function = '%s';" %(func)) + count = 0 + for txt in cur: + printed = 1 + if count == 0: + print "file | function | return_id | return_value | type | param | key | value |" + count += 1 + print "%s | %s | %2s | %13s" %(txt[0], txt[1], txt[3], txt[4]), + print "| %13s |" %(type_to_str(txt[6])), + print " %2d | %20s | %20s |" %(txt[7], txt[8], txt[9]) + +def print_return_values(func): + cur = con.cursor() + cur.execute("select * from return_values where function = '%s';" %(func)) + count = 0 + for txt in cur: + printed = 1 + if count == 0: + print "file | function | value |" + count += 1 + print "%s | %s | %s |" %(txt[0], txt[1], txt[4]), + +def print_call_implies(func): + cur = con.cursor() + cur.execute("select * from call_implies where function = '%s';" %(func)) + count = 0 + for txt in cur: + if not count: + print "file | function | type | param | key | value |" + count += 1 + print "%15s | %15s" %(txt[0], txt[1]), + print "| %15s" %(type_to_str(txt[4])), + print "| %3d | %15s |" %(txt[5], txt[6]) + +def print_type_size(var): + cur = con.cursor() + if not re.search("^\(struct ", var): + m = re.search('(?<=->)\w+', var) + print "searched" + if not m: + print "Can't determine type for %s" %(var) + return + var = "%->" + m.group(0) + cur.execute("select * from type_size where type like '%s';" %(var)) + print "file | type | size" + for txt in cur: + printed = 1 + print "%15s | %15s | %d" %(txt[0], txt[1], txt[2]) + +def print_fn_ptrs(func): + ptrs = get_function_pointers(func) + if not ptrs: + return + print "%s = " %(func), + i = 0 + for p in ptrs: + if i > 0: + print ",", + i = i + 1 + print "'%s'" %(p), + print "" + +if len(sys.argv) < 2: + usage() + +if len(sys.argv) == 2: + func = sys.argv[1] + print_caller_info(func) +elif sys.argv[1] == "user_data": + func = sys.argv[2] + print_caller_info(func, "USER_DATA") +elif sys.argv[1] == "param_value": + func = sys.argv[2] + print_caller_info(func, "PARAM_VALUE") +elif sys.argv[1] == "function_ptr" or sys.argv[1] == "fn_ptr": + func = sys.argv[2] + print_fn_ptrs(func) +elif sys.argv[1] == "return_states": + func = sys.argv[2] + print_return_states(func) +elif sys.argv[1] == "return_values": + func = sys.argv[2] + print_return_values(func) +elif sys.argv[1] == "call_implies": + func = sys.argv[2] + print_call_implies(func) +elif sys.argv[1] == "type_size": + var = sys.argv[2] + print_type_size(var) +else: + usage() diff --git a/smatch_data/db/smdb_function_info.sh b/smatch_data/db/smdb_function_info.sh deleted file mode 100755 index 093777dd..00000000 --- a/smatch_data/db/smdb_function_info.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/bash - -usage() -{ - echo "Usage $0 " - exit 1 -} - -get_function_pointers() -{ - local func=$1 - - OLD_IFS=$IFS - IFS=" -" - ptrs=$(echo "select distinct ptr from function_ptr where function = '$func';" | sqlite3 smatch_db.sqlite) - for ptr in $ptrs ; do - echo "select * from caller_info where function = '$ptr';" | sqlite3 smatch_db.sqlite - done - IFS=$OLD_IFS -} - -func=$1 - -if [ "$func" = "" ] ; then - usage -fi - -echo "select * from caller_info where function = '$func';" | sqlite3 smatch_db.sqlite -get_function_pointers $func diff --git a/smatch_data/db/smdb_param_buf_size.sh b/smatch_data/db/smdb_param_buf_size.sh deleted file mode 100755 index 56e5c1ff..00000000 --- a/smatch_data/db/smdb_param_buf_size.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/bash - -usage() -{ - echo "Usage $0 " - exit 1 -} - -PARAM=$2 -TYPE=2 - -get_function_pointers() -{ - local func=$1 - - OLD_IFS=$IFS - IFS=" -" - ptrs=$(echo "select distinct ptr from function_ptr where function = '$func';" | sqlite3 smatch_db.sqlite) - for ptr in $ptrs ; do - if [ "$PARAM" = "" ] ; then - echo "select * from caller_info where function = '$ptr' and type='$TYPE';" | sqlite3 smatch_db.sqlite - else - echo "select * from caller_info where function = '$ptr' and type='$TYPE' and parameter='$PARAM';" | sqlite3 smatch_db.sqlite - fi - done - IFS=$OLD_IFS -} - -func=$1 - -if [ "$func" = "" ] ; then - usage -fi - -if [ "$PARAM" = "" ] ; then - echo "select * from caller_info where function = '$func' and type='$TYPE';" | sqlite3 smatch_db.sqlite -else - echo "select * from caller_info where function = '$func' and type='$TYPE' and parameter='$PARAM';" | sqlite3 smatch_db.sqlite -fi -get_function_pointers $func diff --git a/smatch_data/db/smdb_param_values.sh b/smatch_data/db/smdb_param_values.sh deleted file mode 100755 index afd81a04..00000000 --- a/smatch_data/db/smdb_param_values.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/bash - -usage() -{ - echo "Usage $0 " - exit 1 -} - -PARAM=$2 -TYPE=1 - -get_function_pointers() -{ - local func=$1 - - OLD_IFS=$IFS - IFS=" -" - ptrs=$(echo "select distinct ptr from function_ptr where function = '$func';" | sqlite3 smatch_db.sqlite) - for ptr in $ptrs ; do - if [ "$PARAM" = "" ] ; then - echo "select * from caller_info where function = '$ptr' and type='$TYPE';" | sqlite3 smatch_db.sqlite - else - echo "select * from caller_info where function = '$ptr' and type='$TYPE' and parameter='$PARAM';" | sqlite3 smatch_db.sqlite - fi - done - IFS=$OLD_IFS -} - -func=$1 - -if [ "$func" = "" ] ; then - usage -fi - -if [ "$PARAM" = "" ] ; then - echo "select * from caller_info where function = '$func' and type='$TYPE';" | sqlite3 smatch_db.sqlite -else - echo "select * from caller_info where function = '$func' and type='$TYPE' and parameter='$PARAM';" | sqlite3 smatch_db.sqlite -fi -get_function_pointers $func diff --git a/smatch_data/db/smdb_return_values.sh b/smatch_data/db/smdb_return_values.sh deleted file mode 100755 index 6f334bbc..00000000 --- a/smatch_data/db/smdb_return_values.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -usage() -{ - echo "Usage $0 " - exit 1 -} - -func=$1 - -if [ "$func" = "" ] ; then - usage -fi - -echo "select * from return_values where function = '$func';" | sqlite3 smatch_db.sqlite -- 2.11.4.GIT