Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C Programming Language

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.

In article <1180388065.305047.201@h2g2000hsg.googlegroups.com>,

 <horacius.@gmail.com> wrote:
>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
horacius.@gmail.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  RotateBufferLeft
 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           RotateBufferLeft [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", callingfunctionname, calledfunctionname);

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 -> RotateBufferLeft;
  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.

On Mon, 28 May 2007 22:41:43 +0000, Richard Heathfield
<r@see.sig.invalid> wrote in comp.lang.c:

   [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.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

In article <78qm531nttobha688u08jcrsin9cftj@4ax.com>,
Jack Klein  <jackkl@spamcop.net> wrote:

>On Mon, 28 May 2007 22:41:43 +0000, Richard Heathfield
><r@see.sig.invalid> wrote 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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.

Jack Klein said:

> On Mon, 28 May 2007 22:41:43 +0000, Richard Heathfield
> <r@see.sig.invalid> wrote 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.

horacius.@gmail.com skrev:

I found this tool http://www.ida.liu.se/~vaden/cgdi/ witch can generate
call graphs, maybe can by useful ?
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.net> wrote:

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc