Using valgrind's massif with argument parameters for an application - c++

I have a code executable as "demonstrate"> I want to have a neat memory profiling for thios executable. I have gone through the valgrind's massif tool and found out to be something which could fetch me results that I am looking for. Although there is a slight issues I am coming across.
My executable takes various arguments and w/o it it wont run e.g ./demonstrate -t 10 -p 1 -P 3
As soon as I run massif on the above statement it tells me
"Error allocating heap"
taskset -c 0-10 valgrind --tool=massif ./demonstrate -t 10 -p 1 -P 3
==4140== Massif, a heap profiler
==4140== Copyright (C) 2003-2010, and GNU GPL'd, by Nicholas Nethercote
==4140== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info
==4140== Command: ./demonstrate -t 10
==4140==
Error allocating heap
==4140==
Quite similar issue happened with mtrace. Included mcheck header and called mtrace and muntrace at the beginning and end and tried calling demonstrate
e.g
mtrace ./demonstrate -t 10 -p 1 -P 3 -f file.txt mALLOCoUT.TXT
Wrong number of arguments, run /usr/bin/mtrace --help for help. at /usr/bin/mtrace line 89.
Although when I run the application without the valgrind option the application runs fine:
./demonstrate -t 10 -p 1 -P 3
Opened 1
Opened 2
Opened 3
Allocated huge page size=1073741824
Pushed 262144 pages
Any suggestion on how to get any of these working with several command line arguments.

Related

remote core file analysis with eclipse

I want to use eclipse IDE as front-end GUI to debug a core file of another OS that is docker'ed in my host. With 'gdbserver' in the docker i can only debug a running process. But i need post-mortem debugging of core files.
I tried some /home/me/bin/remote_gdb wrapper that uses
docker exec -it $DOCKER_CONTAINER bash -c "gdb /usr/bin/executable /opt/crash/executable.core
This works using in a shell. But it fails using in 'eclipse' because of
Could not determine GDB version using command: remote_gdb --version
even though 'remote_gdb --version' prints out exactly the same as the original 'gdb --version'.
I could make something work with 'ddd' as a front-end - let me exercise:
> cd $HOME/SRC
> ls -l
total 4
-rw-rw-r-- 1 frank frank 96 Aug 24 18:04 test.c
> cat test.c
#include <stdio.h>
int main(int argc, char** argv)
{
printf("hello, world!\n");
return 0;
}
> docker run -v $HOME/SRC:/SRC -t fedora /bin/bash
// IN DOCKER NOW:
>> yum install gcc
[...]
>> yum install gdb
[...]
>> cd /SRC
>> gcc -g3 -O0 -o test test.c
>> ls -l
total 40
-rwxr-xr-x 1 root root 34864 Aug 24 16:26 test
-rw-rw-r-- 1 1000 1000 96 Aug 24 16:04 test.c
Back in Host:
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e501d8384060 fedora "/bin/bash" 4 minutes ago Up 4 minutes stupefied_murdock
> cat $HOME/bin/remote_gdb
#!/bin/bash
docker exec -it e501d8384060 gdb /SRC/test
> remote_gdb # $HOME/bin/ is in $PATH
GNU gdb (GDB) Fedora 8.0-20.fc26
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /SRC/test...done.
(gdb) list
1 #include <stdio.h>
2
3 int main(int argc, char** argv)
4 {
5 printf("hello, world!\n");
6 return 0;
7 }
8
(gdb)
Switching back to the shell, where i am in the docker - see 'gdb' is running:
>> yum install procps-ng
[...]
>> ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 16:22 ? 00:00:00 /bin/bash
root 16 0 0 16:23 ? 00:00:00 bash
root 116 0 0 16:28 ? 00:00:00 gdb /SRC/test
root 131 16 0 16:30 ? 00:00:00 ps -ef
Back again on the host shell (after quit gdb session in docker):
> ddd --debugger remote_gdb
The 'ddd' plays nicely with gdb started in docker.
And it also works with core-files (add a "sleep(100);" and kill the process).
But 'ddd' doesn't have a really good GUI.
So what's the way to make such work with 'eclipse'?
I also couldn't make it work with other options: 'qtcreator', 'kdevelop', 'nemiver', 'kdbg', etc
Addendum:
It also doesn't work with 'ssh' into container.
But when i execute the same 'remote_gdb' wrapper script as 'External Tool' it can be executed(?!)
I partially could solve the issue.
The main problem was: my docker command does not provide a tty to eclipse.
As a workaround i switched over to 'ssh' into the docker image.
Moreover i had to align the file locations (executable, core-file and source-files) between host and docker image. Because using a 'gdb' wrapper script, 'eclipse' is not aware that i am debugging remotely and verifies all files for local availability.
This works for post-mortem debugging (core file analysis) and attaching a running process in docker (in general on the remote system).
However, it does not work for starting up a process in gdb via eclipse.
So used 'strace' to check for the activities between 'eclipse' and 'gdb' and found out, that for starting up a new process in 'gdb', 'eclipse' tries to apply some '--interpreter mi2 -nx' command:
inferior-tty-set --thread-group i1 /dev/pts/27
Does it mean it tries to bind a local tty to gdb?
That of course does not work remotely.
What is the reason to use "inferior-tty-set" by 'eclipse'?
It doesn't use this command for doing post-mortem debugging or attaching a running process - while for both i even have a gdb console operable in 'eclipse'
?

c++ - Valgrind on codeblocks (linux)

I already use the Valgrind in small programs to check memorys leaks and its work good.
Now i have a big program with many class and .cpp and .h files and i'm trying to use Valgrind to check the memory leak because i use a lot of pointers, memory, etc.
I'm using linux and codeblocks 16.01 with gcc and i trying to run the Valgrind directly in codeblocks but i'm getting the follow error:
--------------- Application output --------------
valgrind: /myPathToTheProject/ValgrindOut.xml: No such file or directory
If i test with a small project with only a .cpp file and main it works good and the Valgrind generate the ValgrindOut.xml. In this big project i always getting this error. Someone have some idea what is wrong? or other way or tool to test memory leak?
EDIT - LEAK SUMMARY after running Valgrind
Leak summary:
definitely lost: 673 bytes in 6 blocks.
indirectly lost: 89,128 bytes in 68 blocks.
possibly lost: 232 bytes in 2 blocks.
still reachable: 80,944 bytes in 6 blocks.
suppressed: 0 bytes in 0 blocks.
I am not sure how to run valgrind directly from codeblocks. I suggest you build your project using codeblocks. While executing, use valgrind as per below command.
Command
valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all --log-file=leak.txt ./myexecutable <my command line arguments>
Example
valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all --log-file=leak.txt ./myexecutable -i 192.168.1.10 -p 5000
This way you can generate valgrind output file, that is leak.txt that contains memory leaks etc.

ctest does not find valgrind

Calling
ctest -j4 -DCTEST_MEMORYCHECK_COMMAND="/usr/bin/valgrind" -DMemoryCheckCommand="/usr/bin/valgrind" --output-on-failure -T MemCheck
says
Memory checker (MemoryCheckCommand) not set, or cannot find the specified program.
Why doesn't it find valgrind automatically nor when specified manually?
As described on the CTest Wiki page, CTest reads the location of the memory check command (among other settings) from a file DartConfiguration.tcl in the build directory. One way to create the dart configuration file is to simply include the CTest CMake module in your CMakeLists.txt:
include (CTest)
The CTest module will find your valgrind installation in /usr/bin and put a variable MemoryCheckCommand pointing to it in the DartConfiguration.tcl file.
After install valgrind by "apt-get install valgrind", the error is gone.
The Error was
"Memory checker (MemoryCheckCommand) not set, or cannot find the specified program.
Errors while running CTest"
New result
Processing memory checking output:
1/1 MemCheck: cpp_test Defects: 1
MemCheck log files can be found here: (<#> corresponds to test number)
Memory checking results:
Memory Leak - 1
The detail log is logged
/build/Testing/Temporary# cat MemoryChecker.1.log
Showed detail leaking information
HEAP SUMMARY:
in use at exit: 8,000 bytes in 1 blocks
total heap usage: 2 allocs, 1 frees, 80,704 bytes allocated
you should start over from cmake .. , cmake --build . and ctest -T memcheck

How to associate valgrind with a running process?

I have a binary called "ifmFuseHandler" which is invoked by a shell-script something like this:
su - $USER -c "cd $DIR_INDEX;valgrind --leak-check=full ifmFuseHandler $IFM_SINGLE_THREAD -o allow_other $IFM_MOUNT"
But the problem is, i can see the valgrind logs only once, when the control hits this line of shell, and post that even though the ifmFuseHandler is up and running, valgrind does not provide any profiling info.
So, as you can see below, but no further logging:
insite1#POC-Messaging1:/opt/insiteone/log> ps -ef | grep ifmFuseHandler
insite1 622 1 0 04:19 ? 00:00:02 valgrind --leak-check=full ifmFuseHandler -o allow_other /opt/insiteone/fuse-mount
insite1 3225 26014 0 04:32 pts/2 00:00:00 grep ifmFuseHandler
You need to adjust your shell-script to explicitly log the output of valgrind to a file as described here.
If your program is invoked multiple times, try using a filename with the PID, as described in the usage of --log-file=<filename> here.
Just make sure your output file is in an easily readable location.

gdb fails on mountain lion

I tried to compile a 7.x Version of gdb without any luck.
I codesigned the executable(http://sourceware.org/gdb/wiki/BuildingOnDarwin).
With following version there were these problems.
7.5,7.4,git clone: unknown load command 0x2a (and others) for my application and different system libraries when starting gbd. When trying to print a vector for example i always get:
Could not find the frame base for "main(int, char**)"
7.3 (macports and from gdb-website): on starting the application it fails to set the breakpoint and continues to run.
(gdb) start
Temporary breakpoint 1 at 0x100000950: file ../src/main.cpp, line 15.
Starting program: [...]
BFD: unable to read unknown load command 0x24
BFD: unable to read unknown load command 0x2a
BFD: unable to read unknown load command 0x26
Error in re-setting breakpoint 1: Cannot access memory at address 0x100000950
[application continues]
I used the system llvm-gcc, gcc4.7 and svn-gcc4.8 to compile.
Has anybody succeeded in installing gdb on Mountain Lion?
i installed gdb 7.5 on mountain lion without problems...
those steps might help you: ./configure --prefix=/usr/local
--enable-targets=x86_64-apple-darwin10 --enable-64-bit-bfd --disable-werror --build=x86_64-apple-darwin10 --host=x86_64-apple-darwin10 --target=x86_64-apple-darwin10
then make install gdb.
create your own certificate like discribed here and sign the gdb
http://sourceware.org/gdb/wiki/BuildingOnDarwin
you have to explicitly state: codesign -s gdb-cert /usr/local/gdb
before you sign the gdb make sure you already set your certificate
to trusted. also close the keychain before signing your gdb.
hope this also works for your
I resolved this issue by following the steps tried here: http://coding.derkeiler.com/Archive/Ada/comp.lang.ada/2012-09/msg00305.html
Steps
sudo chgrp procmod /usr/local/bin/gdb
sudo chmod g+s /usr/local/bin/gdb
Edit /System/Library/LaunchDaemons/com.apple.taskgated.plist and add +p argument to taskgated process
Force kill taskgated process (it will restart)
Try again
Other links:
https://blogs.oracle.com/dns/entry/understanding_the_authorization_framework_on
it doesn't seem to work for me... same issue as with the default homebrew settings
v1:src zeph$ brew install gdb
==> Downloading http://ftpmirror.gnu.org/gdb/gdb-7.5.tar.bz2
Already downloaded: /Library/Caches/Homebrew/gdb-7.5.tar.bz2
==> ./configure --prefix=/usr/local/Cellar/gdb/7.5 --with-python=/usr --with-system-readline --enable-targets=x86_64-apple-darwin10 --enable-64-bit-bfd --disable-werror --build=x86_64-apple-darwin10 --hos
==> make
==> make install
==> Caveats
gdb requires special privileges to access Mach ports.
You will need to codesign the binary. For instructions, see:
http://sourceware.org/gdb/wiki/BuildingOnDarwin
==> Summary
/usr/local/Cellar/gdb/7.5: 62 files, 9.3M, built in 119 seconds
v1:src zeph$ codesign -s gdb-cert /usr/local/Cellar/gdb/7.5/bin/gdb
v1:src zeph$ /usr/local/Cellar/gdb/7.5/bin/gdb --args /Users/zeph/tmp/CouchBase/src/install/bin/memcached -d -u root -P /tmp/0libmemcached_memc.pid -t 1 -p 11221 -U 11221 -m 128
GNU gdb (GDB) 7.5
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin10".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
BFD: /Users/zeph/tmp/CouchBase/src/install/bin/memcached: unknown load command 0x29
BFD: /Users/zeph/tmp/CouchBase/src/install/bin/memcached: unknown load command 0x29
Reading symbols from /Users/zeph/tmp/CouchBase/src/install/bin/memcached...done.
(gdb) run
Starting program: /Users/zeph/tmp/CouchBase/src/install/bin/memcached -d -u root -P /tmp/0libmemcached_memc.pid -t 1 -p 11221 -U 11221 -m 128
Unable to find Mach task port for process-id 28755: (os/kern) failure (0x5).
(please check gdb is codesigned - see taskgated(8))
(gdb)
k, I had to re-sign it after having TRUSTED the certificate all across the board
codesign -fs gdb-cert /usr/local/Cellar/gdb/7.5/bin/gdb
There were two issues in the OP's question. The one regarding signing the executable or modifying taskgated and using setgid procmod has been covered. The second issue is the warnings about unknown load commands. I too ran into this and after a bunch of searching came across the following patches, that fix it:
https://gist.github.com/davidbalbert/4197567