Bug 454860

Summary: Massif allocation tree missing with gcc -Og flag
Product: [Developer tools] valgrind Reporter: Nikolaos Chatzikonstantinou <nchatz314>
Component: massifAssignee: Nicholas Nethercote <njn>
Status: RESOLVED NOT A BUG    
Severity: minor CC: pjfloyd
Priority: NOR    
Version First Reported In: 3.19.0   
Target Milestone: ---   
Platform: Fedora RPMs   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description Nikolaos Chatzikonstantinou 2022-06-05 05:25:11 UTC
SUMMARY

I was following the manual in <https://valgrind.org/docs/manual/ms-manual.html> and I noticed that I did not obtain allocation trees in detailed snapshots. I realized it's because I compiled with the -Og flag instead of the -O0 flag. 

STEPS TO REPRODUCE

1. Use the source file in <https://valgrind.org/docs/manual/ms-manual.html#ms-manual.anexample>
2. Compile with `gcc -Og`
3. Use valgrind as `valgrind --tool=massif --time-unit=B prog`
4. Look at `ms_print` output.

OBSERVED RESULT

The detailed snapshot bottom text was, for example,

98.43% (10,000B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->98.43% (10,000B) 0x40114D: main (in /home/fox/code/c/custom/a)

EXPECTED RESULT

Without the -Og flag I obtain the following allocation tree:

99.09% (20,000B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->49.54% (10,000B) 0x401178: main (massif_example.c:17)
| 
->39.64% (8,000B) 0x401143: g (massif_example.c:4)
| ->19.82% (4,000B) 0x401159: f (massif_example.c:9)
| | ->19.82% (4,000B) 0x401195: main (massif_example.c:20)
| |   
| ->19.82% (4,000B) 0x40119A: main (massif_example.c:22)
|   
->09.91% (2,000B) 0x401154: f (massif_example.c:8)
  ->09.91% (2,000B) 0x401195: main (massif_example.c:20)

SOFTWARE/OS VERSIONS

Linux: Linux fedora 5.17.11-300.fc36.x86_64 

ADDITIONAL INFORMATION
Comment 1 Paul Floyd 2022-06-07 08:07:52 UTC
We can't do anything in Valgrind for stuff that gets optimized away by the compiler. The example is trivial and would need modifying to work with optimization.

On Linux amd64 I see, in the objdump -d disassembly output

0000000000401146 <g>:
  401146:	c3                   	ret    

0000000000401147 <f>:
  401147:	c3                   	ret    

The calls to malloc that do not use the return value have been optimized away, which affects the massif output.
Comment 2 Nikolaos Chatzikonstantinou 2022-06-07 14:52:33 UTC
On Tue, Jun 7, 2022 at 5:07 PM Paul Floyd <bugzilla_noreply@kde.org> wrote:
> We can't do anything in Valgrind for stuff that gets optimized away by the
> compiler. The example is trivial and would need modifying to work with
> optimization.
>
> On Linux amd64 I see, in the objdump -d disassembly output
>
> 0000000000401146 <g>:
>   401146:       c3                      ret
>
> 0000000000401147 <f>:
>   401147:       c3                      ret
>
> The calls to malloc that do not use the return value have been optimized away,
> which affects the massif output.

I'm surprised that -Og optimizes away the code in f() and g(). I had
misread the gcc manual. Thanks for the help!