Last active
April 7, 2021 19:39
-
-
Save tuxology/7e6435b02645fa3532316143dfaaeb9c to your computer and use it in GitHub Desktop.
Print call-tree in ASCII usig Ocular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// calltree.sc | |
// ----------- | |
// | |
// Suchakra Sharma <[email protected]> (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 | |
} |
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
var calleeList = cpg.method.fullNameExact(methodName).callee.filterNot(_.name.contains("<operator>")).l