Skip to content

Instantly share code, notes, and snippets.

@tuxology tuxology/calltree.sc
Last active Jan 14, 2020

Embed
What would you like to do?
Print call-tree in ASCII usig Ocular
// calltree.sc
// -----------
//
// Suchakra Sharma <suchakra@shiftleft.io> (2019)
//
// Prints the complete call-tree with all possible branches starting from the top of the input method. It is recommended to increase
// the max steps per query to a higher number (eg. "config.query.maxStepsPerCallQuery = 1000000L") for better results.
//
// Example Usage:
// ocular> import $file.scripts.calltree
// ocular> calltree.printCallTree("org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest.parse:void(javax.servlet.http.HttpServletRequest,java.lang.String)")
def printCallTree(callerFullName : String ) {
var dashCount = 0
var lastCallerMethod = callerFullName
var lastDashCount = 0
println(callerFullName)
def findCallee(methodName: String) {
var calleeList = cpg.method.fullNameExact(methodName).callee.filterNot(_.name(".*<operator>.*")).l
var callerNameList = cpg.method.fullNameExact(methodName).caller.fullName.l
if (callerNameList.contains(lastCallerMethod) || (callerNameList.size == 0)) {
dashCount = lastDashCount
} else {
lastDashCount = dashCount
lastCallerMethod = methodName
dashCount += 1
}
calleeList foreach { c =>
print(printDashes(dashCount) + c.fullName + "\n")
findCallee(c.fullName)
}
}
findCallee(lastCallerMethod)
}
def printDashes(count: Int) = {
var tabStr = "|__"
var i = 0
while (i < count) {
tabStr = " " + tabStr
i += 1
}
tabStr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.