Skip to content

Instantly share code, notes, and snippets.

@tuxology
Last active April 7, 2021 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuxology/7e6435b02645fa3532316143dfaaeb9c to your computer and use it in GitHub Desktop.
Save tuxology/7e6435b02645fa3532316143dfaaeb9c to your computer and use it in GitHub Desktop.
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.whereNot(_.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
}
@prabhu
Copy link

prabhu commented Apr 7, 2021

var calleeList = cpg.method.fullNameExact(methodName).callee.filterNot(_.name.contains("<operator>")).l

@tuxology
Copy link
Author

tuxology commented Apr 7, 2021

Updated filterNot to whereNot to account for API changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment