#!/usr/bin/python3
#
# callprofiler.py
#

###########################################################################
#
# cscope command line searches
# cscope.out should have been built.
#
# cscope -d -R -L<n> where n is one of ...
# 0: Find this C symbol
# 1: Find this definition
# 2: Find functions called by this function
# 3: Find functions calling this function
# 4: Find this text string
# 6: Find this egrep pattern
# 7: Find this file
# 8: Find files #including this file
# 9: Find places where this symbol is assigned a value
#
###########################################################################

#
# Find the callers at the first tier and store them in a list.
#

import sys
import os
import subprocess
from subprocess import Popen, PIPE, STDOUT
import array
import numpy as np  # efficient memory management

scriptname = os.path.basename(__file__)
# scriptname = sys.argv[0]

usagestr = """
Trace callers of the named function. Without a named directory, the trace
will be kernel-wide.

NOTE: Must have cscope.out in the Current Working Directory.
      Current Working Directory must be the top of a linux kernel tree.
      Current Working Directory: $BLD$PWD$OFF

Arguments:
  function  - function to trace
  directory - limits search to the named directory and its subdirectories

Options:
  -h  - help
"""

def usage():
    print("\n%s function [directory]" % scriptname)
    print(usagestr)

def get_callers(func):
    print("Getting ", func)
    args = ["cscope", "-d", "-R", "-L3", func]
    p = Popen(args, shell=True, stdout=PIPE)
    output = p.communicate()
    #callers = str(output)
    #callers = callers.split('\\n')

    # callers = process.communicate()

    #print("length: ", len(callers))

    #for line in range(len(callers)):
    #    print(callers[line])

def main():

    # usage()
    func = sys.argv[1]
    get_callers(func)

    return 0

exit(main())
