How can I print test name in valgrind report? - memory-leaks

I'm using valgrind to check memory leaks.
My application has two threads:
Main thread who running tests.
Worker thread who waiting for messages from main thread and
executing commands according to the received message.
When memory leak is found by valgrind, if leak is related to the worker thread, valgrind report shows stack trace of the worker thread.
In order to fix such leak, I need to know which test caused the leak. The test name/ID is coming from the main thread trougth message queue.
Is it possible to add test name or ID to the valgrind report?

Is it possible to add test name or ID to the valgrind report?
No, it is impossible, as far as I know.
But you can attach to your application with gdb when memory leak is found by valgrind. Here is the description of how to do it in valgrind manual:
If you want to debug a program with GDB when using the Memcheck tool,
start Valgrind like this:
valgrind --vgdb=yes --vgdb-error=0 prog
In another shell, start GDB:
gdb prog
Then give the following command to GDB:
(gdb) target remote | vgdb
Now you can find in stack trace the place where you receiving messages from main thread and print in gdb test name, ID or whatever you want.

The solution we found is to use valgrind macros: VALGRIND_DO_CHANGED_LEAK_CHECK, VALGRIND_COUNT_LEAKS, VALGRIND_COUNT_LEAK_BLOCKS in order to determinate which test causes leak

Related

Disabling SIGABRT for a program run (Valgrind)

I have the task to debug a program using Valgrind. The program becomes very slow due to the Valgrind usage. This is a problem, because the program has a watcher thread that kills slow threads with SIGABRT if they spend too much time in certain functions. The program is in a valid state when it exits in that way, so I would like to keep it running even if SIGABRT is cast. I cannot change the program to switch off the watcher thread from the source code.
Now my question:
Does Valgrind, or a tool compatible with Valgrind, give me the option to say to the program: "If you receive SIGABRT, treat it as a null op and go on?"
You might achieve what you want by running your program under valgrind + gdb, using vgdb.
With gdb, you can then control what to do with the SIGABRT signal.
For example, launch your program with:
valgrind --vgdb-stop-at=startup your_program
In another window, launch gdb:
(gdb) handle SIGABRT nostop print nopass
(gdb) target remote | vgdb
(gdb) continue
See http://www.valgrind.org/docs/manual/manual-core-adv.html#manual-core-adv.gdbserver for more information.

Backtrace of a crashed process without depending on an operating-system-specific core dump

I would like to know if there is a way we can get backtrace of a crashed process without depending on the core dump in FreeBSD or Linux.
Normally, to get a backtrace of a running process, we run bstack PID_OF_PROCESS on FreeBSD and pstack PID_OF_PROCESS on Linux.
But once the process gets crashed we need to depend on core file to get the backtrace, and we do not have the PID too if we want to execute bstack or pstack.
Is there a way like a kernel API or something to get the backtrace of crashed process, without doing a gdb on a core file?
Please let me know if I need to provide an extra info about the query.
You can run your application under gdb and make some macro which will do for example: "where" and "step" commands in the loop. After SIGSEGV this macro/script will stop and then you should be able to see the backtrace of your program. Of course it may take a lot of time to catch the problematic situation.
You can also modify your kernel to show the whole backtrace of user-space app, but it needs some knowledge of kernel API.
Maybe valgrind also could be used for such investigation?
Read man gcc about -fstack-protector also.
By the way - why don't you want to use core dump files?

Gdb on multiple instance

I have multiple instances of a particular process running on my system . At some point during the process execution, some of the internal data structures gets overwritten with invalid data. This happens on random instances at random intervals. Is there a way to debug this other than by setting memory access breakpoints?. Also, is it possible set memory access breakpoint on all these process simultaneously without starting a separate instance of gdb for each process?. The process runs on x86_64 linux system with 2.6 kernel.
If you haven't already done so, would recommend using valgrind (http://valgrind.org). It can detect many types of memory bugs including memory over/under runs, memory leaks, double frees, etc.
Also, is it possible set memory access break-point on all these process simultaneously without starting a separate instance of gdb for each process?
I don't think so that using gdb you can set breakpoints for all the processes in one go. According to me, you have separately attach each process and set the breakpoints.
For memory errors, valgrind is much more useful than GDB.
Assuming the instances you are talking about are forked or spawned from a single parent, you don't need separate instances of valgrind.
Just use valgrind --trace-children=yes
See http://man7.org/linux/man-pages/man1/valgrind.1.html
As to your question on GDB, an instance can debug one process at a time only.
You can only debug one process per gdb session. If your program forks, gdb follows the parent process if no other options to set follow-fork-mode was given.
see: http://www.delorie.com/gnu/docs/gdb/gdb_26.html
If you have memory problems it is even possible to run valgrind in combination with gdb or use some other memory debugging library like efence. Efence replaces some library calls e.g. malloc/free with own functions. After that efence and also valgrind use the mmu to catch invalid memory access. This is typically done by adding some space before and after each allocated memory block. If this spare memory is accessed by your application the library ( efence ) or valgrind stops execution. In connection with gdb you will be pointed to the source line which access the forbidden memory area.
Having multiple processes needs multiple instances of gdb which is in practive no real problem.

how to know state of the thread while debugging core dump with gdb on linux?

i am getting crash in the thread. while debugging coredump with gdb, i want to see the state of the thread just before crash.
in my program i am raising a signal for that thread and handling it. it would be helpful to know the state before the thread has crashed and the time before the signal raised for that thread. is it possible to obtain this information from gdb?
Thanks
With "Reversible Debugging" of gdb 7.4 it is possible. Look here for a little tutorial.
Please refer to this page
http://linux-hacks.blogspot.com/2009/07/looking-at-thread-state-inside-gdb.html

Thread stack backtraces when program running under valgrind is interrupted

I have a server program, which doesn't have a very clean/graceful shutdown (not supposed to terminate in general). When tracing memory leaks, I run it under valgrind, but finally have to kill the process by a signal (^C). Generally I try to terminate the process when the ambiance is quiet but still then some threads might have been busy processing jobs and memory held by them cause false alarms. To assist such analysis, is there any way (tool) in valgrind, so that it can print the backtrace of threads when the program exits (by a signal?).
I know it's inconvenient, but could you get your program to dump core when it gets this signal, then diagnose the core dump with gdb?
Don't sure I quite understand your question, but you can print backtrace of all pthreads by gdb:
thread apply all bt

Resources