The output from call_tree can be overwhelming. This commit provides a
depth option to limit the call_tree output to fewer levels.

When depth is not provided, output is not limited as before.

Signed-off-by: Amit Dhingra <[email protected]>
---
 smatch_data/db/smdb.py | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/smatch_data/db/smdb.py b/smatch_data/db/smdb.py
index 79b981d4..33bee138 100755
--- a/smatch_data/db/smdb.py
+++ b/smatch_data/db/smdb.py
@@ -21,7 +21,7 @@ def usage():
     print("<function> - how a function is called")
     print("info <function> <type> - how a function is called,
filtered by type")
     print("return_states <function> - what a function returns")
-    print("call_tree <function> - show the call tree")
+    print("call_tree <function> [depth] - show the call tree")
     print("where <struct_type> <member> - where a struct member is set")
     print("type_size <struct_type> <member> - how a struct member is
allocated")
     print("type_info <struct_type> <member> - stuff from type_info")
@@ -718,7 +718,7 @@ def get_callers(func, restrict = ""):
     return ret

 printed_funcs = []
-def call_tree_helper(func, restrict = "", indent = 0):
+def call_tree_helper(func, restrict = "", indent = 0, maxdepth = 999,
depth = 0):
     global printed_funcs
     if func in printed_funcs:
         return
@@ -726,6 +726,8 @@ def call_tree_helper(func, restrict = "", indent = 0):
         return
     if indent > 30:
         return
+    if depth >= maxdepth:
+        return
     printed_funcs.append(func)
     callers = get_callers(func, restrict)
     if len(callers) >= 20:
@@ -736,13 +738,13 @@ def call_tree_helper(func, restrict = "", indent = 0):
             print("%s+ %s()" %(" " * indent, caller))
         else:
             print("%s%s()" %(" " * (indent + 2), caller))
-        call_tree_helper(caller, restrict, indent + 2)
+        call_tree_helper(caller, restrict, indent + 2, maxdepth, depth + 1)

-def print_call_tree(func):
+def print_call_tree(func, maxdepth):
     global printed_funcs
     printed_funcs = []
     print("%s()" %(func))
-    call_tree_helper(func)
+    call_tree_helper(func, maxdepth = maxdepth)

 def get_type_callers(call_tree, branch, func, my_type):
     cur = con.cursor()
@@ -996,7 +998,13 @@ elif sys.argv[1] == "data_info":
     print_data_info(struct_type, member)
 elif sys.argv[1] == "call_tree":
     func = sys.argv[2]
-    print_call_tree(func)
+    maxdepth = 999
+    if len(sys.argv) >= 4:
+        maxdepth = int(sys.argv[3])
+        if (maxdepth <= 0):
+            print("depth for call_tree must be greater than zero")
+            sys.exit(1)
+    print_call_tree(func, maxdepth)
 elif sys.argv[1] == "preempt":
     func = sys.argv[2]
     print_preempt_tree(func)
-- 
2.52.0

Reply via email to