473,581 Members | 2,676 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,
cwd=workingDir)
csoutput= process.stdout. read()
del process
cslines=[arr.strip().spl it(' ') for arr in csoutput.split( '\n') if
len(arr.split(' '))>1]
funsCalled={}
for fl in cslines:
if funsCalled.has_ key(fl[0]):
funsCalled[fl[0]]|=set([fl[1]])
else:
funsCalled[fl[0]]=set([fl[1]])
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)
cg[entryFun]=funsCalled
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
x:'%s:\n%s'%(x[0],x[1]),fileFuns))
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 ])
weights={}
def calcWeights(af) :
if not weights.has_key (af):
subFuns=funWeig hts[af]
weights[af]=1
for f in subFuns:
calcWeights(f)
weights[af]+=weights[f]
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
x:'%s:\n%s'%(x[0],x[1]),fileFuns))
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'
sys.exit()
entryFun=sys.ar gv[1]
workingDir=os.g etcwd()
acg=callGraph(e ntryFun,working Dir)
print htmlCallGraph(a cg)
Jan 10 '07 #1
0 2305

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

Similar topics

25
3773
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...
6
16432
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...
6
6103
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
7
6840
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...
5
2518
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...
2
3191
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
9
2366
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.
1
6740
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,
5
4435
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...
0
8156
Oralloy
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. ...
0
8310
jinu1996
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...
1
7910
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6563
agi2029
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...
1
5681
isladogs
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...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2307
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
0
1144
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.