473,608 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

profiling C code an generating call graphs

Hi,

I want to profile a C program I am writting, so I compile it with -pg
options and use gprof to obtain information about call graphs, which I
am really interested in. I look at the text files but when I have a
large number of functions, it looks really embarrasing. So somebody
recommended me about graphviz. My problem was to find something to
convert between gprof format and graphviz .dot format. I finally found
gprof2dot.py and tried. But it only converts percentage time
functions, and what I want is the call graph.

Do you know how to get it with these programs or there around there
are another ones capable of doing so ?

If not, what software could I use to obtain call graphs from gprof ?

Thanks.

May 28 '07 #1
7 3910
In article <11************ **********@h2g2 000hsg.googlegr oups.com>,
<ho**********@g mail.comwrote:
>I want to profile a C program I am writting, so I compile it with -pg
options and use gprof to obtain information about call graphs, which I
am really interested in.
Profiling, specific compiler options, call graphs, and gprof are all
not part of the C language itself. I suggest you ask in a newsgroup
that deals with the toolset that includes gprof; possibly
one of the gnu.utils.* newsgroups, or gnu.gcc.help possibly.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
May 28 '07 #2
ho**********@gm ail.com said:
Hi,

I want to profile a C program I am writting, so I compile it with -pg
options and use gprof to obtain information about call graphs, which I
am really interested in. I look at the text files but when I have a
large number of functions, it looks really embarrasing. So somebody
recommended me about graphviz.
Way to go, definitely, if you can't get cflow to work (and believe me,
that's harder than it sounds).
My problem was to find something to
convert between gprof format and graphviz .dot format. I finally found
gprof2dot.py and tried. But it only converts percentage time
functions, and what I want is the call graph.
Well, let's see...

The gprof output is in several sections. The first of these contains a
list of the functions that it profiled:

Flat profile:

Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls ms/call ms/call name
34.12 1.44 1.44 9 160.00 160.00 XORBuffer
27.73 2.61 1.17 37952262 0.00 0.00 Subst
12.56 3.14 0.53 37952263 0.00 0.00 GetSBoxCount
12.56 3.67 0.53 9 58.89 58.89 RotateBufferLef t
12.56 4.20 0.53 1 530.00 4220.00 encrypt
0.47 4.22 0.02 2 10.00 10.00 GetFileLength
0.00 4.22 0.00 1 0.00 0.00 CheckArgs
0.00 4.22 0.00 1 0.00 10.00 GetPass

followed by a blank line, and then some explanatory text. As you can
see, the names on the right side of this section are what we're after,
but this isn't really the best place to capture them. See below.

Do this:

printf("digraph g\n{\n");

In due course, the phrase "Call graph" appears for the first time - and
that's a useful marker for your parsing process. The following five
lines can be ignored, and then we get output like this:

0.53 3.69 1/1 main [2]
[1] 100.0 0.53 3.69 1 encrypt [1]
1.17 0.53 37952262/37952262 Subst [3]
1.44 0.00 9/9 XORBuffer [4]
0.53 0.00 9/9 RotateBufferLef t [6]
0.01 0.00 1/2 GetFileLength [7]
0.00 0.01 1/1 GetPass [8]
0.00 0.00 1/37952263 GetSBoxCount [5]
-----------------------------------------------
POINT A (see below for why I wrote this here)

So - whilst you don't encounter a [number in square brackets] on the
left, ignore the line and read another. Once you encounter the square
bracket as the first character, you're at a calling function. To get at
its name, start at the end, skip backwards past the bracketed number at
the very end, and past the space (banging a '\0' in your buffer at this
point will be a useful thing to do), and then keep going backwards
through the name itself until you hit whitespace again. Slide forward
again to the first character of the function name.

Store that name, and then roll through each subsequent line (stopping
when you hit a line that starts with ---- characters), parsing out the
called function in the same way as above, and doing this for each line:

printf(" %s -%s;\n", callingfunction name, calledfunctionn ame);

for each function name that appears in these subsequent lines.

If the next line is blank, you're done. Otherwise, repeat from POINT A.

When you finally hit that blank line, write this:

printf("}\n");

and you're done. It's ready to go through dot.

I haven't actually written the code (although I'm very tempted to do
just that), but if I work this algorithm by hand on a program whose
profile I've just generated for this article, I get the following dot
file:

digraph g
{
encrypt -Subst;
encrypt -XORBuffer;
encrypt -RotateBufferLef t;
encrypt -GetFileLength;
encrypt -GetPass;
encrypt -GetSBoxCount;
main -encrypt;
main -CheckArgs;
Subst -GetSBoxCount;
GetPass -GetFileLength;
}

How you use dot to render this into an image is of course off-topic, but
I can attest to the fact that it gives a very nice call-graph indeed.
Furthermore, the C code you'll need for this is reasonably simple, and
shouldn't take you more than an hour or so to write and test.

Using gprof and dot in combination to get a graphical representation of
the program's call graph is an interesting idea. (I'd thought of the
dot part before, of course, but not the gprof part.) Do let us know how
you get on with it. And if you don't, I might have to write it myself
(any decade now), just to scratch that itch.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 28 '07 #3
On Mon, 28 May 2007 22:41:43 +0000, Richard Heathfield
<rj*@see.sig.in validwrote in comp.lang.c:
ho**********@gm ail.com said:
Hi,

I want to profile a C program I am writting, so I compile it with -pg
options and use gprof to obtain information about call graphs, which I
am really interested in. I look at the text files but when I have a
large number of functions, it looks really embarrasing. So somebody
recommended me about graphviz.

Way to go, definitely, if you can't get cflow to work (and believe me,
that's harder than it sounds).
My problem was to find something to
convert between gprof format and graphviz .dot format. I finally found
gprof2dot.py and tried. But it only converts percentage time
functions, and what I want is the call graph.

Well, let's see...

The gprof output is in several sections. The first of these contains a
list of the functions that it profiled:
[snip]

Where, in any version of ISO 9899, is "gprof output" defined?

If you are proud of yourself for having mastered a difficult, and
off-topic for clc, tool chain, and are eager to share your knowledge,
do so by email. And include the fact that the question was off-topic.

Thank you.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
May 28 '07 #4
In article <78************ *************** *****@4ax.com>,
Jack Klein <ja*******@spam cop.netwrote:
>On Mon, 28 May 2007 22:41:43 +0000, Richard Heathfield
<rj*@see.sig.i nvalidwrote in comp.lang.c:
[description of processing gprof output]
>If you are proud of yourself for having mastered a difficult, and
off-topic for clc, tool chain, and are eager to share your knowledge,
do so by email. And include the fact that the question was off-topic.
I urge other readers to use comp.lang.c for interesting, relevant, and
informative articles such as Richard Heathfield's.

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
May 28 '07 #5
Jack Klein said:
On Mon, 28 May 2007 22:41:43 +0000, Richard Heathfield
<rj*@see.sig.in validwrote in comp.lang.c:
<snip>
>>
The gprof output is in several sections. The first of these contains
a list of the functions that it profiled:

[snip]

Where, in any version of ISO 9899, is "gprof output" defined?
Nowhere, obviously - but what I was trying to demonstrate was that to
write this C program for himself is not so daunting as the OP appeared
to believe. I happen to think that encouraging people to write C
programs is a Good Thing, and topical to boot.

It is very easy to dismiss an article as off-topic, but rather harder to
dig into it a little way and find something topical to discuss.
If you are proud of yourself for having mastered a difficult, and
off-topic for clc, tool chain
Where did *that* come from? Did you get out of bed the wrong side this
morning or something? And since when were gprof and dot difficult? And
since when were we not allowed even to *mention* tools? A quick Google
search reveals that you yourself are not averse to discussing lint and
even gcc on occasion. No version of ISO 9899 mentions these tools,
either. In any case, I was careful to avoid any detailed discussion of
gprof or dot - all I did was look at the format of the output that
gprof produces, and demonstrate how it could be turned into a dot file,
provided only that the OP was prepared to spend a little time and
effort writing the C code that would perform this transformation.

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 29 '07 #6
ho**********@gm ail.com skrev:
Hi,

I want to profile a C program I am writting, so I compile it with -pg
options and use gprof to obtain information about call graphs, which I
am really interested in. I look at the text files but when I have a
large number of functions, it looks really embarrasing. So somebody
recommended me about graphviz. My problem was to find something to
convert between gprof format and graphviz .dot format. I finally found
gprof2dot.py and tried. But it only converts percentage time
functions, and what I want is the call graph.

Do you know how to get it with these programs or there around there
are another ones capable of doing so ?

If not, what software could I use to obtain call graphs from gprof ?

Thanks.
I found this tool http://www.ida.liu.se/~vaden/cgdi/ witch can generate
call graphs, maybe can by useful ?
May 29 '07 #7
ok, I tried

but when getting vcg, I try to compile on suse 10.1 and no way ! (this
program last version is of 1995)

there is another place to get "vcg" ?

Thanks
On May 29, 9:12 am, Carramba <u...@example.n etwrote:
horacius....@gm ail.com skrev:
Hi,
I want to profile a C program I am writting, so I compile it with -pg
options and use gprof to obtain information about call graphs, which I
am really interested in. I look at the text files but when I have a
large number of functions, it looks really embarrasing. So somebody
recommended me about graphviz. My problem was to find something to
convert between gprof format and graphviz .dot format. I finally found
gprof2dot.py and tried. But it only converts percentage time
functions, and what I want is the call graph.
Do you know how to get it with these programs or there around there
are another ones capable of doing so ?
If not, what software could I use to obtain call graphs from gprof ?
Thanks.

I found this toolhttp://www.ida.liu.se/~vaden/cgdi/witch can generate
call graphs, maybe can by useful ?


May 29 '07 #8

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

Similar topics

13
4632
by: Tim Henderson | last post by:
Hi I want to dynamically generate a graph in python that would be displayable on a web page. What would be the best way to do this? The reason I want to do this, is because I am making a program to convert data from scientic probes into web pages. I have been able to do every thing except the graph generation. Your help would be much appriciated
0
464
by: Irmen de Jong | last post by:
Okay I tried some profiling, but am uncertain about the results I'm getting. They confuse the hell out of me. I have a test program (see below) that essentially has two loops that get called repeatedly. One that is an idle wait loop, and one that is a busy-wait CPU hogger. I wanted to see what profiling results that would give. The total runtime of the program is 10 seconds, where 5 seconds are spent in the CPU-loop and 5 seconds in the...
6
3744
by: cournape | last post by:
Hi there, I have some scientific application written in python. There is a good deal of list processing, but also some "simple" computation such as basic linear algebra involved. I would like to speed things up implementing some of the functions in C. So I need profiling. I first tried to use the default python profiler, but profiling my application multiplies the execution time by a factor between 10 and 100 ! So I decided to give a...
8
3027
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to what I really should do. I've had a look through Programming Python and the Python Cookbook, which have given me ideas, but nothing has gelled yet, so I thought I'd put the question to the community. But first, let me be a little more detailed...
13
2264
by: Jens Theisen | last post by:
Hello, I want to apologise in advance for this being off topic. It's not neither A C nor a C++ question, but to profiling in general, though I my chances are best to find the answer in the C/C++ community. I have a C++ program to profile and went about it by producing large history files of calling dependencies with associated times. It is presumably similar to gprof's data format and could be converted.
19
5188
by: colin | last post by:
Hi, Im trying to time some parts of my code using the Stopwatch class, but it is so slow, I tried the QueryPerformanceFrequency() but this seems to be just as slow, if I just simply call this function in a loop it takes 21 ticks with a reported frequency of 3.5mhz its is about 6us wich is over 12k instructions on my 2ghz cpu. this negates the idea of having a high res timer ...
9
1798
by: Peter Duniho | last post by:
I'm especially hoping Ben Voigt and/or Bob Powell see this (I saw their names on the m.p.d.f.performance newsgroup :) ) I would have posted to the performance newsgroup, but I see very little on there that actually seems to relate to the _tools_ per se while this newsgroup is actually somewhat related to the tools, and most of the useful answers come from people known to frequent this newsgroup anyway, so... :) My basic question: I...
5
4748
by: Peter Olcott | last post by:
Does anyone know of any good software that automatically produces a function call invocation diagram for C/C++ programs? I think that this would be very useful for determining the possible side-effects of making changes to a function.
2
17136
by: Peter Olcott | last post by:
http://www-scf.usc.edu/~peterchd/doxygen/ The above is an example of what the output should look like. I ran Doxygen on a simple program that I wrote to test Doxygen, and all of the Call Graph Images were missing. The Doxygen output indicated that it did generate the Call Graph, showing where the images should be in the HTML output, but, the images themselves were missing. Does anyone know how to fix this problem?
0
8010
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,...
0
8501
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8483
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8349
tracyyun
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...
0
6820
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5479
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3967
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2477
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
1336
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.