[LINUX] Dynamic analysis with Valgrind

Introduction

What is dynamic analysis?

type Description Example
Static test Check for defects without running the source Review, check by compiler
Dynamic test Run the source and check for defects Functional test by tester, check by tool
type Example
Static analysis Compiler, CppCheck and Sourcetrail
Dynamic analysis Valgrind

What is Valgrind

function

type Description
Memcheck Mainly C language, C++Check for problems with memory management for programs
Cachegrind Cache profiler program. CPU I1,Perform a detailed simulation of the D1 and L2 cache.
Callgrind Call graphs can be created and visualized with the extended version of Cachegrind.
Massif Heap memory profiler
Helgrind Thread debugger
DRD Multithreaded C language, C++Detect errors in the program.

Memcheck execution example

Target

#include <iostream>
#include <string>

int main() {
    char *input = nullptr;
    input = new char[ 5 ];
    std::cin >> input;
    std::cout << input << std::endl;
}

Execution content

valgrind --leak-check=full -s ./Sample

Execution result

Memcheck
==4952== Memcheck, a memory error detector
==4952== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4952== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==4952== Command: ./Sample
==4952== 
12345
==4952== Invalid write of size 1
==4952==    at 0x491E56E: std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==4952==    by 0x109215: main (in /usr/local/bin/valgrind/Sample)
==4952==  Address 0x4daec85 is 0 bytes after a block of size 5 alloc'd
==4952==    at 0x483C583: operator new[](unsignedlong)(in/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4952==    by 0x1091FE: main (in /usr/local/bin/valgrind/Sample)
==4952== 
==4952== Invalid read of size 1
==4952==    at 0x483EF54: strlen (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4952==    by 0x498EB1D: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==4952==    by 0x10923A: main (in /usr/local/bin/valgrind/Sample)
==4952==  Address 0x4daec85 is 0 bytes after a block of size 5 alloc'd
==4952==    at 0x483C583: operator new[](unsignedlong)(in/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4952==    by 0x1091FE: main (in /usr/local/bin/valgrind/Sample)
==4952== 
Input value:12345
==4952== 
==4952== HEAP SUMMARY:
==4952==     in use at exit: 5 bytes in 1 blocks
==4952==   total heap usage: 4 allocs, 3 frees, 74,757 bytes allocated
==4952== 
==4952== 5 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4952==    at 0x483C583: operator new[](unsignedlong)(in/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4952==    by 0x1091FE: main (in /usr/local/bin/valgrind/Sample)
==4952== 
==4952== LEAK SUMMARY:
==4952==    definitely lost: 5 bytes in 1 blocks
==4952==    indirectly lost: 0 bytes in 0 blocks
==4952==      possibly lost: 0 bytes in 0 blocks
==4952==    still reachable: 0 bytes in 0 blocks
==4952==         suppressed: 0 bytes in 0 blocks
==4952== 
==4952== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)
==4952== 
==4952== 1 errors in context 1 of 3:
==4952== Invalid read of size 1
==4952==    at 0x483EF54: strlen (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4952==    by 0x498EB1D: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==4952==    by 0x10923A: main (in /usr/local/bin/valgrind/Sample)
==4952==  Address 0x4daec85 is 0 bytes after a block of size 5 alloc'd
==4952==    at 0x483C583: operator new[](unsignedlong)(in/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4952==    by 0x1091FE: main (in /usr/local/bin/valgrind/Sample)
==4952== 
==4952== 
==4952== 1 errors in context 2 of 3:
==4952== Invalid write of size 1
==4952==    at 0x491E56E: std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==4952==    by 0x109215: main (in /usr/local/bin/valgrind/Sample)
==4952==  Address 0x4daec85 is 0 bytes after a block of size 5 alloc'd
==4952==    at 0x483C583: operator new[](unsignedlong)(in/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4952==    by 0x1091FE: main (in /usr/local/bin/valgrind/Sample)
==4952== 
==4952== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)
Memcheck (when built with -g option in g ++)
==9843== Memcheck, a memory error detector
==9843== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==9843== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==9843== Command: ./Sample
==9843== 
12345
==9843== Invalid write of size 1
==9843==    at 0x492056E: std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==9843==    by 0x109140: main (Sample.cpp:6)
==9843==  Address 0x4db0c85 is 0 bytes after a block of size 5 alloc'd
==9843==    at 0x483C583: operator new[](unsignedlong)(in/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==9843==    by 0x10912E: main (Sample.cpp:5)
==9843== 
==9843== Invalid read of size 1
==9843==    at 0x483EF54: strlen (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==9843==    by 0x4990B1D: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==9843==    by 0x109167: main (Sample.cpp:7)
==9843==  Address 0x4db0c85 is 0 bytes after a block of size 5 alloc'd
==9843==    at 0x483C583: operator new[](unsignedlong)(in/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==9843==    by 0x10912E: main (Sample.cpp:5)
==9843== 
Input value:12345
==9843== 
==9843== HEAP SUMMARY:
==9843==     in use at exit: 5 bytes in 1 blocks
==9843==   total heap usage: 4 allocs, 3 frees, 74,757 bytes allocated
==9843== 
==9843== 5 bytes in 1 blocks are definitely lost in loss record 1 of 1
==9843==    at 0x483C583: operator new[](unsignedlong)(in/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==9843==    by 0x10912E: main (Sample.cpp:5)
==9843== 
==9843== LEAK SUMMARY:
==9843==    definitely lost: 5 bytes in 1 blocks
==9843==    indirectly lost: 0 bytes in 0 blocks
==9843==      possibly lost: 0 bytes in 0 blocks
==9843==    still reachable: 0 bytes in 0 blocks
==9843==         suppressed: 0 bytes in 0 blocks
==9843== 
==9843== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)
==9843== 
==9843== 1 errors in context 1 of 3:
==9843== Invalid read of size 1
==9843==    at 0x483EF54: strlen (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==9843==    by 0x4990B1D: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==9843==    by 0x109167: main (Sample.cpp:7)
==9843==  Address 0x4db0c85 is 0 bytes after a block of size 5 alloc'd
==9843==    at 0x483C583: operator new[](unsignedlong)(in/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==9843==    by 0x10912E: main (Sample.cpp:5)
==9843== 
==9843== 
==9843== 1 errors in context 2 of 3:
==9843== Invalid write of size 1
==9843==    at 0x492056E: std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==9843==    by 0x109140: main (Sample.cpp:6)
==9843==  Address 0x4db0c85 is 0 bytes after a block of size 5 alloc'd
==9843==    at 0x483C583: operator new[](unsignedlong)(in/usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==9843==    by 0x10912E: main (Sample.cpp:5)
==9843== 
==9843== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)

Massif execution example

Target

#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ifstream ifs( "./sample.txt" );
    if ( !ifs ) {
        std::cerr << "File open error" << std::endl;
        return 1;
    }

    std::string str = "";
    while ( getline( ifs, str ) ) {
        std::cout << str << std::endl;
    }

    return 0;
}

Execution content

valgrind --tool=massif --time-unit=B --stacks=yes ./SampleM
ms_print massif.out.pid

Execution result

Massif
--------------------------------------------------------------------------------
Command:            ./SampleM
Massif arguments:   --time-unit=B --stacks=yes
ms_print arguments: massif.out.9470
--------------------------------------------------------------------------------


    KB
82.96^                                                                ##      
     |                                                                # ::::  
     |                                                                # ::::  
     |                                                       :::::::::# ::::: 
     |                                                       ::: :: ::# ::::: 
     |                                                       ::: :: ::# ::::: 
     |                                                       ::: :: ::# ::::: 
     |                                                       ::: :: ::# ::::: 
     |                                                       ::: :: ::# ::::: 
     |                                                       ::: :: ::# ::::: 
     |                                                       ::: :: ::# ::::: 
     |                                                       ::: :: ::# ::::: 
     |                                                       ::: :: ::# ::::: 
     |                                                       ::: :: ::# ::::: 
     |                                                       ::: :: ::# ::::: 
     |                                                       ::: :: ::# ::::: 
     |                                                       ::: :: ::# ::::: 
     |                                                       ::: :: ::# ::::: 
     |                                                       ::: :: ::# ::::: 
     |                                                       ::: :: ::# ::::: 
   0 +----------------------------------------------------------------------->MB
     0                                                                   3.317

Number of snapshots: 55
 Detailed snapshots: [2, 48 (peak)]

--------------------------------------------------------------------------------
  n        time(B)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
  0              0                0                0             0            0
  1         83,280            3,712                0             0        3,712
  2        134,240            1,328                0             0        1,328
00.00% (0B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.

--------------------------------------------------------------------------------
  n        time(B)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
  3        199,968            1,136                0             0        1,136
  4        296,704            1,328                0             0        1,328
  5        352,768            1,328                0             0        1,328
  6        422,848            1,328                0             0        1,328
  7        517,456            1,328                0             0        1,328
  8        585,848            1,176                0             0        1,176
  9        627,720            1,432                0             0        1,432
 10        694,312            1,416                0             0        1,416
 11        762,088            1,384                0             0        1,384
 12        838,552            1,368                0             0        1,368
 13        906,328            1,400                0             0        1,400
 14        974,256            1,584                0             0        1,584
 15      1,067,512            1,400                0             0        1,400
 16      1,130,512            1,328                0             0        1,328
 17      1,193,584            1,328                0             0        1,328
 18      1,256,656            1,328                0             0        1,328
 19      1,319,728            1,328                0             0        1,328
 20      1,382,800            1,328                0             0        1,328
 21      1,445,872            1,328                0             0        1,328
 22      1,508,944            1,328                0             0        1,328
 23      1,572,016            1,328                0             0        1,328
 24      1,635,088            1,328                0             0        1,328
 25      1,698,160            1,328                0             0        1,328
 26      1,761,232            1,328                0             0        1,328
 27      1,824,304            1,328                0             0        1,328
 28      1,911,904            1,328                0             0        1,328
 29      1,967,968            1,328                0             0        1,328
 30      2,024,032            1,328                0             0        1,328
 31      2,080,096            1,328                0             0        1,328
 32      2,136,160            1,328                0             0        1,328
 33      2,192,224            1,328                0             0        1,328
 34      2,248,288            1,328                0             0        1,328
 35      2,304,352            1,408                0             0        1,408
 36      2,360,496            1,328                0             0        1,328
 37      2,416,696            1,176                0             0        1,176
 38      2,472,856            1,144                0             0        1,144
 39      2,529,208            1,176                0             0        1,176
 40      2,585,272            1,624                0             0        1,624
 41      2,683,960           72,920           72,704             8          208
 42      2,743,880           73,336           72,704             8          624
 43      2,799,960           73,352           72,704             8          640
 44      2,856,080           73,392           72,704             8          680
 45      2,912,176           74,720           72,704             8        2,008
 46      2,996,696           74,392           72,704             8        1,680
 47      3,052,760           73,832           72,704             8        1,120
 48      3,135,528           84,952           82,392            40        2,520
96.99% (82,392B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->85.58% (72,704B) 0x48FAC19: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
| ->85.58% (72,704B) 0x4011B89: call_init.part.0 (dl-init.c:72)
|   ->85.58% (72,704B) 0x4011C90: call_init (dl-init.c:30)
|     ->85.58% (72,704B) 0x4011C90: _dl_init (dl-init.c:119)
|       ->85.58% (72,704B) 0x4001139: ??? (in /usr/lib/x86_64-linux-gnu/ld-2.31.so)
|         
->09.64% (8,192B) 0x495FDD3: std::basic_filebuf<char, std::char_traits<char> >::_M_allocate_internal_buffer() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
| ->09.64% (8,192B) 0x4964006: std::basic_filebuf<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
|   ->09.64% (8,192B) 0x496493F: std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(char const*, std::_Ios_Openmode) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
|     ->09.64% (8,192B) 0x109362: main (in /usr/local/bin/valgrind/SampleM)
|       
->01.21% (1,024B) 0x4AD4E83: _IO_file_doallocate (filedoalloc.c:101)
| ->01.21% (1,024B) 0x4AE504F: _IO_doallocbuf (genops.c:347)
|   ->01.21% (1,024B) 0x4AE40AF: _IO_file_overflow@@GLIBC_2.2.5 (fileops.c:745)
|     ->01.21% (1,024B) 0x4AE2834: _IO_new_file_xsputn (fileops.c:1244)
|       ->01.21% (1,024B) 0x4AE2834: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1197)
|         ->01.21% (1,024B) 0x4AD6540: fwrite (iofwrite.c:39)
|           ->01.21% (1,024B) 0x4987783: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
|             ->01.21% (1,024B) 0x109430: main (in /usr/local/bin/valgrind/SampleM)
|               
->00.56% (472B) in 1+ places, all below ms_print's threshold (01.00%)

--------------------------------------------------------------------------------
  n        time(B)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 49      3,219,624           83,384           82,392            40          952
 50      3,261,536           83,392           82,392            40          960
 51      3,303,408           83,488           82,392            40        1,056
 52      3,345,280           83,312           82,392            40          880
 53      3,387,392           74,624           73,728            16          880
 54      3,477,944            1,544            1,024             8          512

Reference material

Recommended Posts

Dynamic analysis with Valgrind
Data analysis with python 2
Basket analysis with Spark (1)
[Various image analysis with plotly] Dynamic visualization with plotly [python, image]
Dependency analysis with CaboCha
Voice analysis with python
Voice analysis with python
Regression analysis with NumPy
Data analysis with Python
[Python] Morphological analysis with MeCab
[Co-occurrence analysis] Easy co-occurrence analysis with Python! [Python]
Multiple regression analysis with Keras
Sentiment analysis with Python (word2vec)
Texture analysis learned with pyradiomics
Planar skeleton analysis with Python
Japanese morphological analysis with Python
Muscle jerk analysis with Python
[PowerShell] Morphological analysis with SudachiPy
Text sentiment analysis with ML-Ask
Get PowerShell commands from malware dynamic analysis site with BeautifulSoup + Python
3D skeleton structure analysis with Python
Impedance analysis (EIS) with python [impedance.py]
Text mining with Python ① Morphological analysis
Principal component analysis with Spark ML
Convenient analysis with Pandas + Jupyter notebook
I played with Mecab (morphological analysis)!
Data analysis starting with python (data visualization 1)
Logistic regression analysis Self-made with python
Data analysis starting with python (data visualization 2)
I tried multiple regression analysis with polynomial regression
The most basic clustering analysis with scikit-learn
Principal Component Analysis with Livedoor News Corpus-Practice-
[In-Database Python Analysis Tutorial with SQL Server 2017]
Marketing analysis with Python ① Customer analysis (decyl analysis, RFM analysis)
Two-dimensional saturated-unsaturated osmotic flow analysis with Python
Machine learning with python (2) Simple regression analysis
2D FEM stress analysis program with Python
I tried factor analysis with Titanic data!
Let's scrape a dynamic site with Docker
[Voice analysis] Find Cross Similarity with Librosa
Line talk analysis with janome (OSS released)
Sentiment analysis of tweets with deep learning
Tweet analysis with Python, Mecab and CaboCha
Principal component analysis with Power BI + Python
Visualize 2ch threads with WordCloud-Morphological analysis / WordCloud-
Data analysis starting with python (data preprocessing-machine learning)
Two-dimensional unsteady heat conduction analysis with Python
Python: Simplified morphological analysis with regular expressions
How about polarity analysis with "order" added?