db: replace database info scripts with smdb.py
authorDan Carpenter <dan.carpenter@oracle.com>
Sat, 20 Apr 2013 10:43:26 +0000 (20 13:43 +0300)
committerDan Carpenter <dan.carpenter@oracle.com>
Sat, 20 Apr 2013 10:43:26 +0000 (20 13:43 +0300)
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 <dan.carpenter@oracle.com>
smatch_data/db/smdb.py [new file with mode: 0755]
smatch_data/db/smdb_function_info.sh [deleted file]
smatch_data/db/smdb_param_buf_size.sh [deleted file]
smatch_data/db/smdb_param_values.sh [deleted file]
smatch_data/db/smdb_return_values.sh [deleted file]

diff --git a/smatch_data/db/smdb.py b/smatch_data/db/smdb.py
new file mode 100755 (executable)
index 0000000..e0e3070
--- /dev/null
@@ -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 <function> [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 (executable)
index 093777d..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-#!/bin/bash
-
-usage()
-{
-    echo "Usage $0 <function>"
-    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 (executable)
index 56e5c1f..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-#!/bin/bash
-
-usage()
-{
-    echo "Usage $0 <function>"
-    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 (executable)
index afd81a0..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-#!/bin/bash
-
-usage()
-{
-    echo "Usage $0 <function>"
-    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 (executable)
index 6f334bb..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-#!/bin/bash
-
-usage()
-{
-    echo "Usage $0 <function>"
-    exit 1
-}
-
-func=$1
-
-if [ "$func" = "" ] ; then
-    usage
-fi
-
-echo "select * from return_values where function = '$func';" | sqlite3 smatch_db.sqlite