[LINUX] Dynamische Analyse von Valgrind

Einführung

Was ist dynamische Analyse?

Art Erläuterung Beispiel
Statischer Test Prüfen Sie auf Fehler, ohne die Quelle laufen zu lassen Überprüfen Sie, überprüfen Sie durch Compiler
Dynamischer Test Führen Sie die Quelle aus und überprüfen Sie sie auf Fehler Funktionsprüfung durch Tester, Prüfung durch Werkzeug
Art Beispiel
Statische Analyse Compiler, CppCheck und Sourcetrail
Dynamische Analyse Valgrind

Was ist Valgrind?

Funktion

Art Erläuterung
Memcheck Hauptsächlich C-Sprache, C.++Überprüfen Sie, ob Probleme mit der Speicherverwaltung für Programme vorliegen
Cachegrind Cash Profiler Programm. CPU I1,Führen Sie eine detaillierte Simulation des D1- und L2-Cache durch.
Callgrind Anrufdiagramme können mit der erweiterten Version von Cachegrind erstellt und visualisiert werden.
Massif Heap-Speicherprofiler
Helgrind Thread-Debugger
DRD Multithread-C-Sprache, C.++Erkennen Sie Fehler im Programm.

Beispiel für die Ausführung von Memchecks

Ziel

#include <iostream>
#include <string>

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

Ausführungsinhalt

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

Ausführungsergebnis

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== 
Eingegebener Wert: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 (wenn mit der Option -g in g ++ erstellt)
==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== 
Eingegebener Wert: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)

Beispiel für die Ausführung eines Massivs

Ziel

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

int main() {
    std::ifstream ifs( "./sample.txt" );
    if ( !ifs ) {
        std::cerr << "Fehler beim Öffnen der Datei" << std::endl;
        return 1;
    }

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

    return 0;
}

Ausführungsinhalt

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

Ausführungsergebnis

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

Referenzmaterial

Recommended Posts

Dynamische Analyse von Valgrind
Datenanalyse mit Python 2
Korbanalyse mit Spark (1)
[Verschiedene Bildanalysen mit Plotly] Dynamische Visualisierung mit Plotly [Python, Bild]
Abhängigkeitsanalyse mit CaboCha
Sprachanalyse mit Python
Sprachanalyse mit Python
Führen Sie eine Regressionsanalyse mit NumPy durch
Datenanalyse mit Python
[Python] Morphologische Analyse mit MeCab
[Analyse des gemeinsamen Auftretens] Einfache Analyse des gemeinsamen Auftretens mit Python! [Python]
Multiple Regressionsanalyse mit Keras
Emotionsanalyse von Python (word2vec)
Mit Pyradiomics erlernte Texturanalyse
Planare Skelettanalyse mit Python
Japanische morphologische Analyse mit Python
Muskel-Ruck-Analyse mit Python
[PowerShell] Morphologische Analyse mit SudachiPy
Text Emotionsanalyse mit ML-Ask
Rufen Sie mit BeautifulSoup + Python PowerShell-Befehle von einer Website für dynamische Malware-Analysen ab
Dreidimensionale Skelettstrukturanalyse mit Python
Impedanzanalyse (EIS) mit Python [impedance.py]
Text Mining mit Python ① Morphologische Analyse
Hauptkomponentenanalyse mit Spark ML
Bequeme Analyse mit Pandas + Jupyter Notebook
Ich habe mit Mecab gespielt (morphologische Analyse)!
Datenanalyse beginnend mit Python (Datenvisualisierung 1)
Logistische Regressionsanalyse Selbst erstellt mit Python
Datenanalyse beginnend mit Python (Datenvisualisierung 2)
Ich habe eine multiple Regressionsanalyse mit Polypoly-Regression versucht
Die grundlegendste Clusteranalyse mit Scikit-Learn
Hauptkomponentenanalyse mit Livedoor News Corpus --Practice--
[In-Database Python Analysis Tutorial mit SQL Server 2017]
Zweidimensionale Analyse des gesättigten und ungesättigten Permeationsflusses mit Python
Maschinelles Lernen mit Python (2) Einfache Regressionsanalyse
2D FEM Stressanalyseprogramm von Python
Ich habe versucht, Faktoren mit Titanic-Daten zu analysieren!
Lassen Sie uns mit Docker eine dynamische Site kratzen
[Sprachanalyse] Finden Sie Kreuzähnlichkeit mit Librosa
Line Talk Analyse mit Janome (OSS veröffentlicht)
Emotionale Analyse von Tweets mit Deep Learning
Tweet-Analyse mit Python, Mecab und CaboCha
Lassen Sie uns 2ch Thread mit WordCloud-morphologische Analyse / WordCloud Edition- visualisieren
Datenanalyse beginnend mit Python (Datenvorverarbeitung - maschinelles Lernen)
Zweidimensionale instationäre Wärmeleitungsanalyse mit Python
Python: Vereinfachte morphologische Analyse mit regulären Ausdrücken
Wie wäre es mit einer Polaritätsanalyse mit hinzugefügtem "Auftrag"?