|
From: Jonny T. <j.m...@du...> - 2008-10-29 16:55:26
|
This is probably a basic question, but I can't find any information in the docs or through web searches. How can I get cachegrind to annotate my code at the instruction level when the source is C code? I can get C code annotated at source level, and the docs describe how to get assembly code annotated at instruction level, but can't work out how to get an instruction-level annotation for what was originally C code. The instructions in the docs suggest: as --gstabs foo.s This seems to be intended for assembly source; if I generate an assembly file from my C source and then use this command, with debug symbols disabled when doing the disassembly, cg_annotate reports ??? for the filename and I can't persuade it to do any annotation. If I enable debug symbols when doing the disassembly, I can get cg_annotate to give me source-level annotation, but still no instruction-level annotation. So: please can somebody suggest how to get instruction-level annotation for what started out as C source code? Incidentally, I don't care about how the instructions match up to source lines: anything that will give me annotated assembly instructions and preferably a function name will do. Thanks in advance from a new valgrind user... Jonny Taylor |
|
From: Nicholas N. <nj...@cs...> - 2008-10-30 02:35:32
|
On Wed, 29 Oct 2008, Jonny Taylor wrote: > This is probably a basic question, but I can't find any information in > the docs or through web searches. How can I get cachegrind to annotate > my code at the instruction level when the source is C code? I can get > C code annotated at source level, and the docs describe how to get > assembly code annotated at instruction level, but can't work out how > to get an instruction-level annotation for what was originally C code. What would the annotated code look like? I think you'll just have to annotate the assembly code, and then mentally map that back to the C code. > The instructions in the docs suggest: > as --gstabs foo.s > This seems to be intended for assembly source; if I generate an > assembly file from my C source and then use this command, with debug > symbols disabled when doing the disassembly, cg_annotate reports ??? > for the filename and I can't persuade it to do any annotation. If I > enable debug symbols when doing the disassembly, I can get cg_annotate > to give me source-level annotation, but still no instruction-level > annotation. I'm confused by some of your terminology, particularly "disassembly", as in "with debugs symbols disabled when doing the disassembly". BTW, you might find "gcc -g foo.s" works better than "as --stabs foo.s". Nick |
|
From: Jonny T. <j.m...@du...> - 2008-10-30 08:56:22
|
On 30 Oct 2008, at 02:34, Nicholas Nethercote wrote:
> On Wed, 29 Oct 2008, Jonny Taylor wrote:
>> This is probably a basic question, but I can't find any
>> information in
>> the docs or through web searches. How can I get cachegrind to
>> annotate
>> my code at the instruction level when the source is C code? I can get
>> C code annotated at source level, and the docs describe how to get
>> assembly code annotated at instruction level, but can't work out how
>> to get an instruction-level annotation for what was originally C
>> code.
>
> What would the annotated code look like? I think you'll just have
> to annotate the assembly code, and then mentally map that back to
> the C code.
...
> I'm confused by some of your terminology, particularly
> "disassembly", as in "with debugs symbols disabled when doing the
> disassembly".
Apologies for not being clear - I talked a lot about what I'd tried
and failed with, but what I want to achieve is very simple I think.
Probably best to ignore what I wrote the first time if I was unclear.
I have a program written in C++, which is normally compiled with gcc
to object files and then linked (with gcc). I can get cachegrind/
cg_annotate to display source-level output like this:
. . . . . . . . . void init_hash_table(char *file_name, Word_Node
*table[])
3 1 1 . . . 1 0 0 {
. . . . . . . . . FILE *file_ptr;
. . . . . . . . . Word_Info *data;
1 0 0 . . . 1 1 1 int line = 1, i;
. . . . . . . . .
5 0 0 . . . 3 0 0 data = (Word_Info *) create(sizeof(Word_Info));
but I would like to see instruction-level output something like this
init_hash_table:
1 0 0 . . . . . . leal -12(%ebp),%eax
1 0 0 . . . 1 0 0 movl %eax,84(%ebx)
1 0 0 0 0 0 1 0 0 movl $1,-20(%ebp)
1 0 0 . . . . . . movl $.LnrB,%eax
1 0 0 . . . 1 0 0 movl %eax,-16(%ebp)
Any advice on how to achieve that would be very helpful!
Cheers
Jonny |
|
From: Josef W. <Jos...@gm...> - 2008-10-30 13:53:22
|
On Thursday 30 October 2008, Jonny Taylor wrote:
> I have a program written in C++, which is normally compiled with gcc
> to object files and then linked (with gcc). I can get cachegrind/
> cg_annotate to display source-level output like this:
>
> . . . . . . . . . void init_hash_table(char *file_name, Word_Node
> *table[])
> 3 1 1 . . . 1 0 0 {
> . . . . . . . . . FILE *file_ptr;
> . . . . . . . . . Word_Info *data;
> 1 0 0 . . . 1 1 1 int line = 1, i;
> . . . . . . . . .
> 5 0 0 . . . 3 0 0 data = (Word_Info *) create(sizeof(Word_Info));
>
> but I would like to see instruction-level output something like this
>
> init_hash_table:
> 1 0 0 . . . . . . leal -12(%ebp),%eax
> 1 0 0 . . . 1 0 0 movl %eax,84(%ebx)
> 1 0 0 0 0 0 1 0 0 movl $1,-20(%ebp)
> 1 0 0 . . . . . . movl $.LnrB,%eax
> 1 0 0 . . . 1 0 0 movl %eax,-16(%ebp)
Hmm... if your C++ code is in foo.cpp, you get the assembler file with
g++ -S foo.cpp -o foo.s
then, the binary with
gcc -g foo.s -o foo
For me, running
valgrind --tool=cachegrind ./foo
and cg_annotate, I get annotated disassembly like
. . . . . . . . . .globl main
. . . . . . . . . .type main, @function
. . . . . . . . . main:
1 0 0 0 0 0 0 0 0 leal 4(%esp), %ecx
1 0 0 0 0 0 0 0 0 andl $-16, %esp
1 0 0 1 0 0 1 0 0 pushl -4(%ecx)
1 0 0 0 0 0 1 0 0 pushl %ebp
I assume this is what you want. As Nick said, you have to map instructions to C++ lines
yourself. For this, you also can compare the annotation with foo-g.s from
g++ -g -S foo.cpp -o foo-g.s
as here, foo-g.s contains ".file"/".loc" macros providing the source lines for instructions.
However, if you are comfortable with KDE GUIs, you can get at disassembly annotation by
using
g++ -g foo.cpp -o foo
valgrind --tool=callgrind --simulate-cache=yes --dump-instr=yes ./foo
and visualizing the result in KCachegrind, more especially look at the disassembly view.
Josef
>
> Any advice on how to achieve that would be very helpful!
> Cheers
> Jonny
|
|
From: Jonny T. <j.m...@du...> - 2008-11-03 08:19:36
|
On 30 Oct 2008, at 13:31, Josef Weidendorfer wrote:
>> I have a program written in C++, which is normally compiled with gcc
>> to object files and then linked (with gcc). I can get cachegrind/
>> cg_annotate to display source-level output like this:
>>
>> . . . . . . . . . void init_hash_table(char *file_name, Word_Node
>> *table[])
>> 3 1 1 . . . 1 0 0 {
>> . . . . . . . . . FILE *file_ptr;
>> . . . . . . . . . Word_Info *data;
>> 1 0 0 . . . 1 1 1 int line = 1, i;
>> . . . . . . . . .
>> 5 0 0 . . . 3 0 0 data = (Word_Info *) create(sizeof
>> (Word_Info));
>>
>> but I would like to see instruction-level output something like this
>>
>> init_hash_table:
>> 1 0 0 . . . . . . leal -12(%ebp),%eax
>> 1 0 0 . . . 1 0 0 movl %eax,84(%ebx)
>> 1 0 0 0 0 0 1 0 0 movl $1,-20(%ebp)
>> 1 0 0 . . . . . . movl $.LnrB,%eax
>> 1 0 0 . . . 1 0 0 movl %eax,-16(%ebp)
>
> Hmm... if your C++ code is in foo.cpp, you get the assembler file with
> g++ -S foo.cpp -o foo.s
> then, the binary with
> gcc -g foo.s -o foo
Thanks very much Nick and Josef, that sequence works for me.
I think the docs could be made clearer on this - I read sec 5.2.4 as
definitely saying that gcc _won't_ work in this way, and that you
have to use 'as' instead. I assume that comment actually applies only
to code originally written as assembly...
Cheers
Jonny
|