Use valgrind on Ubuntu

Source: Internet
Author: User
Tags valgrind

Valgrind is a GPL software used for memory debugging and code analysis of Linux (for x86, amd64 and ppc32) programs. You can run your program in its environment to monitor memory usage, such as malloc and free in C or new and delete in C ++. With the valgrind toolkit, You can automatically detect many memory management and thread bugs, avoiding spending too much time searching for bugs, making your program more stable.

Valgrind's main functions
The valgrind toolkit contains multiple tools, such as memcheck, cachegrind, helgrind, callgrind, and massif. The functions of the tools are described as follows:

MemcheckThe tool mainly checks the following program errors:

1. Use uninitialized memory (use of uninitialised memory)
2. Use the released memory (reading/writing memory after it has been free 'd)
3. Use the memory space that exceeds the malloc allocation (reading/writing off the end of malloc 'd blocks)
4. unauthorized access to the stack (reading/writing inappropriate areas on the stack)
5. Whether the applied space is released (memory leaks-where pointers to malloc 'd blocks are lost forever)
6. Match malloc/free/New/delete application and released memory (mismatched use of malloc/New/new [] vs free/delete/Delete [])
7. Overlapping SRC and DST (overlapping SRC and DST pointers in memcpy () and related functions)
Callgrind
Callgrind collects some data, function call relationships, and other information during the running of the program. It can also selectively simulate cache. At the end of the operation, it will write the analysis data into a file. Callgrind_annotate can convert the content of this file into a readable form.

Cachegrind
It simulates the level-1 cache I1, D1, and L2 level-2 cache in the CPU, and can accurately point out the cache loss and hit in the program. If needed, it can also provide us with the number of cache loss, memory reference times, and the number of commands generated by each code line, each function, each module, and the entire program. This is of great help to the optimization program.

Helgrind
It is mainly used to check competition problems in multi-threaded programs. Helgrind looks for areas in the memory that are accessed by multiple threads but are not always locked. These areas are often the places where synchronization is lost between threads and lead to undiscovered errors. Helgrind implements the competition detection algorithm named "eraser" and makes further improvements to reduce the number of errors reported.

Massif
Stack analyzer, which can measure the memory used by the program in the stack, tells us the heap block, heap management block and stack size. Massif can help us reduce memory usage. In modern systems with virtual memory, it can also accelerate the running of our programs and reduce the chance that programs stay in the SWAp zone.

Install valgrind in Ubuntu:

#> Sudo apt-Get install valgrind

Valgrind usage:

#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){    char *ptr = (char *)malloc(1024);        ptr[1024] = 0; //    memcpy(ptr+1, ptr, 100);    char ch = ptr[1024];    free(ptr);    free(ptr);    char *ptr1;    *ptr1 = ‘a‘;    char *ptr2 = (char *)malloc(1024);    return 0;}

#> Valgrind -- tool = memcheck -- leak-check = yes./valgtest

Note that the-G option is added when the program is compiled, and the row number is displayed when the error message is printed. The preceding execution result shows six errors.

Here is a multi-thread competition:

#include <pthread.h>#include <stdio.h> int a = 0; void* child_fn (void* arg){         a++;         return NULL;} int main () {         pthread_t child;         pthread_create(&child,NULL, child_fn, NULL);         a++;         pthread_join(child,NULL);         return 0;}

 

[Email protected]: ~ /Valgrind $ valgrind -- tool = helgrind./heltest
= 5639 = helgrind, a thread error detector
= 5639 = copyright (c) 2007-2013, and gnu gpl 'd, by openworks LLP et al.
= 5639 = Using Valgrind-3.10.0.SVN and libvex; rerun with-H for copyright info
= 5639 = command:./heltest
= 5639 =
= 5639 = --- thread-announcement ------------------------------------------
= 5639 =
= 5639 = thread #1 is the program's root thread
= 5639 =
= 5639 = --- thread-announcement ------------------------------------------
= 5639 =
==5639 = thread #2 was created
= 5639 = at 0x51562ce: Clone (clone. S: 74)
= 5639 = by 0x4e44199: do_clone.constprop.3 (createthread. C: 75)
= 5639 = by 0x4e458ba: [email protected] @ glibc_2.2.5 (createthread. C: 245)
= 5639 = by 0x4c30c90 :??? (In/usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==5639 = by 0x40068d: Main (heltest. C: 15)
= 5639 =
= 5639 = ----------------------------------------------------------------
= 5639 =
==5639 = possible data race during read of size 4 at 0x60104c by thread #1
==5639 = locks held: None
= 5639 = at 0x40068e: Main (heltest. C: 16)
= 5639 =
= 5639 = This conflicts with a previous write of size 4 by thread #2
==5639 = locks held: None
= 5639 = at 0x40065e: child_fn (heltest. C: 8)
= 5639 = by 0x4c30e26 :??? (In/usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
= 5639 = by 0x4e45181: start_thread (FIG: 312)
= 5639 = by 0x515630c: Clone (clone. S: 111)
= 5639 =
= 5639 = ----------------------------------------------------------------
= 5639 =
==5639 = possible data race during write of size 4 at 0x60104c by thread #1
==5639 = locks held: None
= 5639 = at 0x400697: Main (heltest. C: 16)
= 5639 =
= 5639 = This conflicts with a previous write of size 4 by thread #2
==5639 = locks held: None
= 5639 = at 0x40065e: child_fn (heltest. C: 8)
= 5639 = by 0x4c30e26 :??? (In/usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
= 5639 = by 0x4e45181: start_thread (FIG: 312)
= 5639 = by 0x515630c: Clone (clone. S: 111)
= 5639 =
= 5639 =
= 5639 = for counts of detected and suppressed errors, Rerun with:-V
= 5639 = use -- history-level = approx OR = none to gain increased speed,
= 5639 = the cost of your CED accuracy of conflicting-access information
= 5639 = Error Summary: 2 errors from 2 contexts (suppressed: 0 from 0)

I have previously written a method for using coredump to debug deadlocks. Here, the valgrind helgrind tool can also be used to check for deadlocks.

#include <iostream>#include <thread>#include <mutex>#include <chrono>using namespace std;mutex m1,m2;void func_2(){    m2.lock();    cout<< "about to dead_lock"<<endl;    m1.lock();    }void func_1(){    m1.lock();        chrono::milliseconds dura( 1000 );// delay to trigger dead_lock    this_thread::sleep_for( dura );            m2.lock();    }int main(){    thread t1(func_1);    thread t2(func_2);        t1.join();    t2.join();    return 0;}

 

[Email protected]: ~ /Valgrind $ g ++-wall dead_lock_demo.cpp-O dead_lock_demo-g-STD = C ++ 11-lpthread
[Email protected]: ~ /Valgrind $ valgrind -- tool = helgrind./dead_lock_demo
= 5646 = helgrind, a thread error detector
= 5646 = copyright (c) 2007-2013, and gnu gpl 'd, by openworks LLP et al.
= 5646 = Using Valgrind-3.10.0.SVN and libvex; rerun with-H for copyright info
= 5646 = command:./dead_lock_demo
= 5646 =
About to dead_lock // execute here. When Ctrl + C finishes the process, the following information is printed:
^ C = 5646 = --- thread-announcement ------------------------------------------
= 5646 =
==5646 = thread #2 was created
= 5646 = at 0x56702ce: Clone (clone. S: 74)
= 5646 = by 0x4e44199: do_clone.constprop.3 (createthread. C: 75)
= 5646 = by 0x4e458ba: [email protected] @ glibc_2.2.5 (createthread. C: 245)
= 5646 = by 0x4c30c90 :??? (In/usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
= 5646 = by 0x510ce3e: STD: thread: _ m_start_thread (STD: shared_ptr <STD: thread: _ impl_base>) (in/usr/lib/x86_64-linux-gnu/libstdc ++. so.6.0.19)
= 5646 = by 0x401789: STD: thread <void (&) ()> (void (&) (thread: 135)
==5646 = by 0x40112d: Main (dead_lock_demo.cpp: 33)
= 5646 =
= 5646 = ----------------------------------------------------------------
= 5646 =
= 5646 = thread #2: exiting thread still holds 1 lock
= 5646 = at 0x4e4bf2c: _ lll_lock_wait (lowlevellock. S: 135)
= 5646 = by 0x4e47656: _ l_lock_909 (pthread_mutex_lock.c: 151)
= 5646 = by 0x4e4747e: pthread_mutex_lock (pthread_mutex_lock.c: 79)
= 5646 = by 0x4c32072: pthread_mutex_lock (in/usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
= 5646 = by 0x40101b: _ gthread_mutex_lock (pthread_mutex_t *) (gthr-default.h: 748)
= 5646 = by 0x401421: STD: mutex: Lock () (mutex: 134)
= 5646 = by 0x401111: func_1 () (dead_lock_demo.cpp: 25)
= 5646 = by 0x40278c: void STD: _ bind_simple <void (* () >:: _ m_invoke <> (STD: _ index_tuple <>) (functional: 1732)
= 5646 = by 0x4026e6: STD: _ bind_simple <void (* () >:: operator () (functional: 1720)
= 5646 = by 0x40267f: STD: thread: _ impl <STD: _ bind_simple <void (* () >:: _ m_run () (thread: 115)
= 5646 = by 0x510cbef :??? (In/usr/lib/x86_64-linux-gnu/libstdc ++. so.6.0.19)
= 5646 = by 0x4c30e26 :??? (In/usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
= 5646 =
= 5646 = --- thread-announcement ------------------------------------------
= 5646 =
= 5646 = thread #3 was created
= 5646 = at 0x56702ce: Clone (clone. S: 74)
= 5646 = by 0x4e44199: do_clone.constprop.3 (createthread. C: 75)
= 5646 = by 0x4e458ba: [email protected] @ glibc_2.2.5 (createthread. C: 245)
= 5646 = by 0x4c30c90 :??? (In/usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
= 5646 = by 0x510ce3e: STD: thread: _ m_start_thread (STD: shared_ptr <STD: thread: _ impl_base>) (in/usr/lib/x86_64-linux-gnu/libstdc ++. so.6.0.19)
= 5646 = by 0x401789: STD: thread <void (&) ()> (void (&) (thread: 135)
==5646 = by 0x40113e: Main (dead_lock_demo.cpp: 35)
= 5646 =
= 5646 = ----------------------------------------------------------------
= 5646 =
= 5646 = thread #3: exiting thread still holds 1 lock
= 5646 = at 0x4e4bf2c: _ lll_lock_wait (lowlevellock. S: 135)
= 5646 = by 0x4e47656: _ l_lock_909 (pthread_mutex_lock.c: 151)
= 5646 = by 0x4e4747e: pthread_mutex_lock (pthread_mutex_lock.c: 79)
= 5646 = by 0x4c32072: pthread_mutex_lock (in/usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
= 5646 = by 0x40101b: _ gthread_mutex_lock (pthread_mutex_t *) (gthr-default.h: 748)
= 5646 = by 0x401421: STD: mutex: Lock () (mutex: 134)
= 5646 = by 0x4010df: func_2 () (dead_lock_demo.cpp: 14)
= 5646 = by 0x40278c: void STD: _ bind_simple <void (* () >:: _ m_invoke <> (STD: _ index_tuple <>) (functional: 1732)
= 5646 = by 0x4026e6: STD: _ bind_simple <void (* () >:: operator () (functional: 1720)
= 5646 = by 0x40267f: STD: thread: _ impl <STD: _ bind_simple <void (* () >:: _ m_run () (thread: 115)
= 5646 = by 0x510cbef :??? (In/usr/lib/x86_64-linux-gnu/libstdc ++. so.6.0.19)
= 5646 = by 0x4c30e26 :??? (In/usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
= 5646 =
= 5646 =
= 5646 = for counts of detected and suppressed errors, Rerun with:-V
= 5646 = use -- history-level = approx OR = none to gain increased speed,
= 5646 = the cost of your CED accuracy of conflicting-access information
= 5646 = Error Summary: 2 errors from 2 contexts (suppressed: 1 from 1)

Just now, I have been operating on Linux and editing this blog post. It is easier to switch to Windows for editing...

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.