Analyse dynamique par Valgrind

introduction

Qu'est-ce que l'analyse dynamique?

type La description Exemple
Test statique Vérifier les défauts sans exécuter la source Révision, vérification par compilateur
Test dynamique Exécutez la source et vérifiez les défauts Test fonctionnel par testeur, contrôle par outil
type Exemple
Analyse statique Compilateur, CppCheck et Sourcetrail
Analyse dynamique Valgrind

Qu'est-ce que Valgrind

une fonction

type La description
Memcheck Principalement langage C, C++Rechercher des problèmes de gestion de la mémoire pour les programmes
Cachegrind Programme de profilage de trésorerie. CPU I1,Effectuez une simulation détaillée du cache D1 et L2.
Callgrind Les graphes d'appels peuvent être créés et visualisés avec la version étendue de Cachegrind.
Massif Profileur de mémoire de tas
Helgrind Débogueur de thread
DRD Langage C multithread, C++Détectez les erreurs dans le programme.

Exemple d'exécution Memcheck

Cible

#include <iostream>
#include <string>

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

Contenu d'exécution

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

Résultat d'exécution

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== 
Valeur d'entrée: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 (lorsqu'il est construit avec l'option -g dans 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== 
Valeur d'entrée: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)

Exemple d'exécution Massif

Cible

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

int main() {
    std::ifstream ifs( "./sample.txt" );
    if ( !ifs ) {
        std::cerr << "Erreur d'ouverture de fichier" << std::endl;
        return 1;
    }

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

    return 0;
}

Contenu d'exécution

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

Résultat d'exécution

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

Matériel de référence

Recommended Posts

Analyse dynamique par Valgrind
Analyse de données avec python 2
Analyse du panier avec Spark (1)
[Diverses analyses d'images avec plotly] Visualisation dynamique avec plotly [python, image]
Analyse de dépendance avec CaboCha
Analyse vocale par python
Analyse vocale par python
Effectuer une analyse de régression avec NumPy
Analyse de données avec Python
[Python] Analyse morphologique avec MeCab
[Analyse de co-occurrence] Analyse de co-occurrence facile avec Python! [Python]
Analyse de régression multiple avec Keras
Analyse des émotions par Python (word2vec)
Analyse de texture apprise avec la pyradiomique
Analyse de squelette planaire avec Python
Analyse morphologique japonaise avec Python
Analyse des secousses musculaires avec Python
[PowerShell] Analyse morphologique avec SudachiPy
Analyse des émotions par SMS avec ML-Ask
Obtenez des commandes PowerShell à partir d'un site d'analyse dynamique de logiciels malveillants avec BeautifulSoup + Python
Analyse de la structure du squelette en trois dimensions avec Python
Analyse d'impédance (EIS) avec python [impedance.py]
Text mining avec Python ① Analyse morphologique
Analyse des composants principaux avec Spark ML
Analyse pratique avec Pandas + notebook Jupyter
J'ai joué avec Mecab (analyse morphologique)!
Analyse de données à partir de python (visualisation de données 1)
Analyse de régression logistique Self-made avec python
Analyse de données à partir de python (visualisation de données 2)
J'ai essayé l'analyse de régression multiple avec régression polypoly
L'analyse de clustering la plus basique avec scikit-learn
Analyse des composants principaux avec le corpus d'actualités Livedoor - Pratique--
[Didacticiel d'analyse Python en base de données avec SQL Server 2017]
Analyse bidimensionnelle du flux de perméation saturée-insaturée avec Python
Apprentissage automatique avec python (2) Analyse de régression simple
Programme d'analyse des contraintes FEM 2D par Python
J'ai essayé l'analyse factorielle avec des données Titanic!
Raclons un site dynamique avec Docker
[Analyse vocale] Trouvez une similitude croisée avec Librosa
Analyse des conversations en ligne avec Janome (OSS publié)
Analyse émotionnelle des tweets avec apprentissage en profondeur
Analyse des tweets avec Python, Mecab et CaboCha
Visualisons le fil 2ch avec WordCloud-analyse morphologique / édition WordCloud-
Analyse de données à partir de python (pré-traitement des données-apprentissage automatique)
Analyse de conduction thermique bidimensionnelle non stationnaire avec Python
Python: analyse morphologique simplifiée avec des expressions régulières
Que diriez-vous de l'analyse de polarité avec "ordre" ajouté?