473,699 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

call graph using python and cscope

Sometimes it is nice to have the data used by cscope accessible in a
programatic way.
The following python script extract the "functions called" information
from cscope (function: callGraph)
and produced an html file from them.

from csCallGraph import *
acg=callGraph(e ntryFun,working Dir)

entryFun is the function to start with (e.g. main)
workingDir is the directory where cscope.out is located

As a script it can be called like:
csCallGraph main myprogram.html

import subprocess , os, sys

def functionsCalled (entryFun,worki ngDir):
cmd = "cscope -d -l -L -2%s"%entryFun
process = subprocess.Pope n(cmd, stdout=subproce ss.PIPE, shell=True,
csoutput= process.stdout. read()
del process
cslines=[arr.strip().spl it(' ') for arr in csoutput.split( '\n') if
len(arr.split(' '))>1]
for fl in cslines:
if funsCalled.has_ key(fl[0]):
allFuns=set(map (lambda x:x[1],cslines))
return (allFuns,funsCa lled)

def callGraph(entry Fun,workingDir, cg={}):
if not cg.has_key(entr yFun):
allFuns,funsCal led=functionsCa lled(entryFun,w orkingDir)
for af in allFuns:
cg=callGraph(af ,workingDir,cg)
return cg

def textCallGraph(a cg):
innerFuns=[(f,d,len(reduce (lambda x,y:x|y,d.value s()))) for f,d in
acg.items() if len(d)>0 ]
leafFuns=[(f,d,0) for f,d in acg.items() if not len(d)>0 ]
innerFuns.sort( lambda x,y: y[2]-x[2])
innerLen=len(in nerFuns)
leafLen=len(lea fFuns)
title=lambda aFun: '\n' + aFun + '\n' + '-'*len(aFun)
def ff(aFun,funsCal led):
fileFuns=zip(fu nsCalled.keys() ,[' '+',\n '.join(funsCall edInFile)
for funsCalledInFil e in funsCalled.valu es()])
funIn=lambda f: '\n%s in '%f
return title(aFun) + funIn(aFun) + funIn(aFun).joi n(map(lambda
strInner='\n'.j oin([ff(f[0],f[1]) for f in innerFuns])
strLeaves='\n'. join(map(lambda x:title(x[0]),leafFuns))
return strInner+'\n'+s trLeaves

def funWeights(acg) :
funWeights=dict ([(f,reduce(lambd a x,y:x|y,d.value s())) for f,d in
acg.items() if len(d)>0 ]+
[(f,[]) for f,d in acg.items() if not len(d)>0 ])
def calcWeights(af) :
if not weights.has_key (af):
subFuns=funWeig hts[af]
for f in subFuns:
for af in funWeights.keys (): calcWeights(af)
return weights

def htmlCallGraph(a cg):
funW=funWeights (acg)
innerFuns=[(f,d,funW[f]) for f,d in acg.items() if len(d)>0 ]
leafFuns=[(f,d,0) for f,d in acg.items() if not len(d)>0 ]
#innerFuns.sort (lambda x,y: y[2]-x[2]))
def cfun(a,b):
if b a:
return 1
elif b < a:
return -1
return 0
innerFuns.sort( lambda x,y: cfun(x[2],y[2]))
innerLen=len(in nerFuns)
leafLen=len(lea fFuns)
funDict=dict(zi p(map(lambda x:x[0],innerFuns)+map (lambda
x:x[0],leafFuns),rang e(innerLen+leaf Len)))
title=lambda aFun: '<hr><h4><a name=#f%i'%funD ict[aFun]+'>' + aFun + '
(%i)'%funW[aFun] + '</a></h4>\n'
def ff(aFun,funsCal led):
fun=lambda y:'<a href=#f%i'%funD ict[y]+'
style="text-decoration:none ">'+y+'</a>'
fileFuns=zip(fu nsCalled.keys() ,[',\n'.join(map( fun,funsCalledI nFile))
for funsCalledInFil e in funsCalled.valu es()])
funIn=lambda f: '<br><em>%s</emin '%f
return title(aFun) + funIn(aFun) + funIn(aFun).joi n(map(lambda
strInner='\n'.j oin([ff(f[0],f[1]) for f in innerFuns])
strLeaves='\n'. join(map(lambda x:title(x[0]),leafFuns))
return '<html>\n<body> \n'+strInner+'\ n'+strLeaves+"</body>\n</html>\n"

if __name__ == '__main__':
if len(sys.argv) < 2:
print 'Usage: csGragh.py entryFunction'
entryFun=sys.ar gv[1]
workingDir=os.g etcwd()
acg=callGraph(e ntryFun,working Dir)
print htmlCallGraph(a cg)
Jan 10 '07 #1
0 2311

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Magnus Lie Hetland | last post by:
Is there any interest in a (hypothetical) standard graph API (with 'graph' meaning a network, consisting of nodes and edges)? Yes, we have the standard ways of implementing graphs through (e.g.) dicts mapping nodes to neighbor-sets, but if one wants a graph that's implemented in some other way, this may not be the most convenient (or abstract) interface to emulate. It might be nice to have the kind of polymorphic freedom that one has with,...
by: Xing Xu | last post by:
Hi guiders, sorry , since I don't know which group suit for this question,I just post this question at these group. As we know , we can get the run-time call graph by some proved tools . now I have a problem about the extract call graph from statice C source code , not from binary. I have try to use cscope and write some perl script for it .
by: Vinay | last post by:
Hi Everybody I have been looking out for a Function call tree command in Unix environment. Given a piece of source code(millions of lines), there is a function F1() calling F2() calling F3(). The tools should analyse the C/C++ source code & provide a tree-like structure denoting the sequence of calls made by a particular
by: diffuser78 | last post by:
My python program spits lot of data. I take that data and plot graphs using OfficeOrg spredsheet. I want to automate this task as this takes so much of time. I have some questions. 1. Which is the best graph plotting utility in python or linux. Can I write a code in such a way that my python code automatically gives me a graph. I know little about gnuplot. If you know any better tool without much learning curve please tell me in Linux. ...
by: Ramon Diaz-Uriarte | last post by:
Dear All, Has anybody tried to use ID Utils (http://www.gnu.org/software/idutils/46) with Python? I've googled, searched the mailing list, and have found nothing. A silly, simple use of IDUtils with Python code does work, using a language map that says *.py files are text files. But I am wondering if someone has done something more sophisticated. (For instance, I get matches to commented out functions, which I'd rather not, lots of...
by: sunset85 | last post by:
Hi. I'm trying to scan linked source directory using "cscope" In normal case (without linked source directory), "cscope -R " works vety well. But when linked source directory included, "cscope -R" wouldn't scan that linked directoty. What option should I use to scan linked source directory using
by: Hadron | last post by:
Could anyone put me on the right track to developing Python with emacs please : modes to consider, debugging etc hopefully all within emacs. Any help and shared experiences much appreciated.
by: Evan | last post by:
Hi, guys ~~ How can i work on VIM for python code? I use cscope plugin on VIM for C code before, it helps me to different function and search C variable where it is defined. I'm now starting to learn python, and I'd like to know how works on VIM. I saw that the VIM is support python such as syntax highlighting,
by: chrispoliquin | last post by:
I need to represent the hyperlinks between a large number of HTML files as a graph. My non-directed graph will have about 63,000 nodes and and probably close to 500,000 edges. I have looked into igraph (http://cneurocvs.rmki.kfki.hu/igraph/doc/ python/index.html) and networkX (https://networkx.lanl.gov/wiki) for generating a file to store the graph, and I have also looked into Graphviz for visualization. I'm just not sure which modules...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.