1

TO build the code: make BUILD = TARGET_ --> Builds the code

What command to be used to compile source code with Valgrind. Any idea?

Command to run valgrind for C/C++ file valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./<file_name>

This is the output:

==16702== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 14 from 1)
==16702== malloc/free: in use at exit: 13,165 bytes in 414 blocks.
==16702== malloc/free: 1,219 allocs, 805 frees, 176,926 bytes allocated.
==16702== For counts of detected errors, rerun with: -v
==16702== searching for pointers to 414 not-freed blocks.
==16702== checked 108,004 bytes.
==16702==
==16702== 80 bytes in 1 blocks are definitely lost in loss record 1 of 4
==16702==    at 0x4004405: malloc (vg_replace_malloc.c:149)
==16702==    by 0x8095A91: xmalloc (in /bin/bash)
==16702==    by 0x806AC84: execute_command_internal (in /bin/bash)
==16702==    by 0x806C72F: execute_command (in /bin/bash)
==16702==    by 0x805DE1A: reader_loop (in /bin/bash)
==16702==    by 0x805CEE1: main (in /bin/bash)
==16702==
==16702==
==16702== 128 bytes in 1 blocks are still reachable in loss record 2 of 4
==16702==    at 0x400579F: realloc (vg_replace_malloc.c:306)
==16702==    by 0x8095AFC: xrealloc (in /bin/bash)
==16702==    by 0x8067484: (within /bin/bash)
==16702==    by 0x8067525: (within /bin/bash)
==16702==    by 0x8066DD4: (within /bin/bash)
==16702==    by 0x806712D: (within /bin/bash)
==16702==    by 0x8067B92: print_simple_command (in /bin/bash)
==16702==    by 0x806A3D6: execute_command_internal (in /bin/bash)
==16702==    by 0x806C72F: execute_command (in /bin/bash)
==16702==    by 0x805DE1A: reader_loop (in /bin/bash)
==16702==    by 0x805CEE1: main (in /bin/bash)
==16702==
==16702==
==16702== 2,092 bytes in 4 blocks are still reachable in loss record 3 of 4
==16702==    at 0x4004405: malloc (vg_replace_malloc.c:149)
==16702==    by 0x8095B13: xrealloc (in /bin/bash)
==16702==    by 0x8085F7C: (within /bin/bash)
==16702==    by 0x8086146: fd_to_buffered_stream (in /bin/bash)
==16702==    by 0x8086613: with_input_from_buffered_stream (in /bin/bash)
==16702==    by 0x805CFC3: main (in /bin/bash)
==16702==
==16702==
==16702== 10,865 bytes in 408 blocks are still reachable in loss record 4 of 4
==16702==    at 0x4004405: malloc (vg_replace_malloc.c:149)
==16702==    by 0x8095A91: xmalloc (in /bin/bash)
==16702==    by 0x8090EE1: set_default_locale (in /bin/bash)
==16702==    by 0x805C19A: main (in /bin/bash)
==16702==
==16702== LEAK SUMMARY:
==16702==    definitely lost: 80 bytes in 1 blocks.
==16702==      possibly lost: 0 bytes in 0 blocks.
==16702==    still reachable: 13,085 bytes in 413 blocks.
==16702==         suppressed: 0 bytes in 0 blocks.

How can I compile and use commands in such a way that I get more file and line number and not (/bin/bash)?

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
  • Q: Is your "file" a shell script, or a binary executable? Q: Have you tried running Valgrind directly on you executable? NOTES: 1) As Paul Floyd said, to get line numbers, build your executable with `-g`. This will ONLY help with YOUR executables: it WON'T help with shell scripts. 2) Everything in your Valgrind output appears to be from your system's "/bin/bash" (vs. a compiled app you can have some control over). Here's an example of a Valgrind "false positive" with /bin/bash: https://bugzilla.redhat.com/show_bug.cgi?id=1746101. There's not much you can do about that. – paulsm4 May 27 '23 at 04:10

2 Answers2

1

Guessing that you are running a shell scrip that runs your test exe.

Try --trace-children=yes

If possible build your test exe with debug info. Typically add -g to CFLAGS and/or CXXFLAGS in your makefile.

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
0

hum... "compile source code with Valgrind"

Never done that before...Not sure what you mean..

Some useful settings I've used before

valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all --trace-children=yes --vgdb=true --log-file=log.txt program

also it helps if you compile with symbols. Meaning

when you compile use -O -g or -Og -g not -O2

Or if maybe even -O0 -g

Oh also you can try the

--vgdb-error=0

when running with valgind. I think it stop and pulls up gdb

$> man valgrind

or

$> valgrind -h

hope that helps

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
boardkeystown
  • 180
  • 1
  • 11