{"id":26,"date":"2011-04-17T12:57:00","date_gmt":"2011-04-17T12:57:00","guid":{"rendered":"http:\/\/mattdturner.com\/wordpress\/?p=26"},"modified":"2011-04-26T14:25:29","modified_gmt":"2011-04-26T20:25:29","slug":"using-the-debugging-tool-valgrind","status":"publish","type":"post","link":"https:\/\/mattdturner.com\/wordpress\/2011\/04\/using-the-debugging-tool-valgrind\/","title":{"rendered":"Using the Debugging Tool Valgrind"},"content":{"rendered":"<p><span style=\"line-height: 24px;\"><span style=\"font-family: inherit;\"><a style=\"text-decoration: none;\" rel=\"self\" href=\"http:\/\/valgrind.org\/\"><\/a><a href=\"http:\/\/mattdturner.com\/wordpress\/wp-content\/uploads\/2011\/04\/valgrind.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-81\" title=\"valgrind\" src=\"http:\/\/mattdturner.com\/wordpress\/wp-content\/uploads\/2011\/04\/valgrind-300x196.png\" alt=\"\" width=\"300\" height=\"196\" srcset=\"https:\/\/mattdturner.com\/wordpress\/wp-content\/uploads\/2011\/04\/valgrind-300x196.png 300w, https:\/\/mattdturner.com\/wordpress\/wp-content\/uploads\/2011\/04\/valgrind.png 506w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a>Valgrind is an excellent, and sometimes necessary, tool for debugging troublesome programs. According to Valgrind\u2019s website, the program can \u201cautomatically detect many memory management and threading bugs, avoiding hours of frustrating bug-hunting, making your programs more stable. You can also perform detailed profiling to help speed up your programs\u201d.<\/span><\/span><\/p>\n<p>Valgrind contains a variety of tools for use while debugging. The program can be used to debug a wide variety of programming languages including, but not limited to, C, C++, Java, Perl, Python, Fortran, Ada, etc. Using these tools, you can:<\/p>\n<ul class=\"disc\" style=\"list-style-type: disc;\">\n<li><span style=\"font-family: inherit;\">Detect memory errors (using the tool\u00a0<strong>Memcheck<\/strong>)<\/span><\/li>\n<li><span style=\"font-family: inherit;\">Make programs run faster (using the cache and branch-prediction profiler<strong>Cachegrind<\/strong>)<\/span><\/li>\n<li><span style=\"font-family: inherit;\">Detect thread errors (using the tool\u00a0<strong>Helgrind<\/strong> and\/or\u00a0<strong>DRD<\/strong>)<\/span><\/li>\n<li><span style=\"font-family: inherit;\">Profile heap usage (using the tool\u00a0<strong>Massif<\/strong> and\/or\u00a0<strong>DHAT<\/strong>)<\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: inherit;\"><!--more--><\/span><\/p>\n<h4 style=\"margin-bottom: -20px; margin-top: -20px;\"><span style=\"font-family: inherit;\"><br \/>How to Use Valgrind\u2019s Memcheck Tool<br \/><\/span><\/h4>\n<p><\/p>\n<p><span style=\"font-family: inherit;\"><br \/>\nPossibly the best thing about Valgrind is the fact that it is very easy to use. There is only 1 thing that you need to do in order to prepare your program for use with Memcheck; compile your program with the\u00a0<code>-g<\/code> and\u00a0<code>-O0<\/code> compiler flags. That\u2019s it. Now all you have to do is run your program using valgrind.<br \/>\n<\/span><br \/>\n<span style=\"font-family: inherit;\"><br \/>\nHere is an example of how to run a program using Valgrind\u2019s Memcheck tool (example copied from Valgrind User Manual):<\/span><br \/>\nIf you normally run your program like this:<br \/>\n<span style=\"font-family: inherit;\"><code>myprog arg1 arg2<\/code><\/span><br \/>\n<span style=\"font-family: inherit;\">Use this command line to run the program with valgrind:<\/span><br \/>\n<span style=\"font-family: inherit;\"><code>valgrind --leak-check=yes myprog arg1 arg2<\/code><\/span><\/p>\n<p><span style=\"font-family: inherit;\"> <\/span><\/p>\n<p>Your program will run a lot slower (around 20-30 times slower) than normal, and it will use a lot more memory.<\/p>\n<div>\n<p>&nbsp;<\/p>\n<div>\n<p><span style=\"font-family: inherit;\"> <\/span><\/p>\n<h4 style=\"margin-bottom: -20px; margin-top: -20px;\"><span style=\"font-family: inherit;\">Example Run<\/span><\/h4>\n<p><span style=\"font-family: inherit;\"><br \/>\n<\/span><\/p>\n<p><span style=\"font-family: inherit;\"><br \/>\nHere is an example program (again taken from the Valgrind User Manual). The file is called a.c.<br \/>\n<\/span><\/p>\n<div class=\"separator\" style=\"clear: both; text-align: center;\"><span style=\"font-family: inherit;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"http:\/\/2.bp.blogspot.com\/-1OrQI5pLGi0\/Tatm6sFU7XI\/AAAAAAAAAB8\/iazJuaW_PuE\/s1600\/a.c.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/2.bp.blogspot.com\/-1OrQI5pLGi0\/Tatm6sFU7XI\/AAAAAAAAAB8\/iazJuaW_PuE\/s640\/a.c.jpg\" border=\"0\" alt=\"\" width=\"515\" height=\"223\" \/><\/a><\/span><\/div>\n<div class=\"separator\" style=\"clear: both; text-align: left;\"><span style=\"font-family: inherit;\"><span style=\"color: black;\"><br \/>\n<\/span><\/span><\/div>\n<div class=\"separator\" style=\"clear: both; text-align: left;\"><span style=\"font-family: inherit;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"http:\/\/2.bp.blogspot.com\/-1OrQI5pLGi0\/Tatm6sFU7XI\/AAAAAAAAAB8\/iazJuaW_PuE\/s1600\/a.c.jpg\"><\/a><\/span><\/div>\n<div class=\"separator\" style=\"clear: both; text-align: center;\"><span style=\"font-family: inherit;\"> <\/span><\/div>\n<h4 style=\"margin-bottom: -20px; margin-top: -20px;\"><span style=\"font-family: inherit;\"><br \/>\n<\/span><\/h4>\n<div>\n<p><span style=\"font-family: inherit;\"><span style=\"font-family: inherit;\"> <\/span><\/span><\/p>\n<div style=\"clear: both; text-align: left; margin: 0px;\"><span style=\"font-family: inherit;\"><span style=\"font-family: inherit;\"><span style=\"font-family: inherit;\">Running the program with Valgrind provides memory leak messages similar to those shown below:<\/span><\/span><\/span><\/div>\n<div class=\"separator\" style=\"clear: both; text-align: center;\"><span style=\"font-family: inherit;\"><span style=\"font-family: inherit;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"http:\/\/3.bp.blogspot.com\/-N_OJdrFC1Co\/Tatm_aMnQ8I\/AAAAAAAAACE\/bEEafww7vgQ\/s1600\/a.c_out.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/3.bp.blogspot.com\/-N_OJdrFC1Co\/Tatm_aMnQ8I\/AAAAAAAAACE\/bEEafww7vgQ\/s400\/a.c_out.jpg\" border=\"0\" alt=\"\" width=\"515\" height=\"60\" \/><\/a><\/span><\/span><\/div>\n<div><span style=\"font-family: inherit;\"><span style=\"font-family: inherit;\"><br \/>\n<\/span><\/span><\/div>\n<\/div>\n<p><span style=\"font-family: inherit;\">Some things to note about the output:<br \/>\n<\/span><\/p>\n<ul class=\"disc\" style=\"list-style-type: disc;\">\n<li><span style=\"font-family: inherit;\">The 19182 is the process ID<\/span><\/li>\n<p><span style=\"font-family: inherit;\"> <\/span><\/p>\n<li>The \u201cInvalid write&#8230;\u201d tells you what kind of error it is. Here, the program wrote to memory that it should not have<\/li>\n<li>Below the first line is the stack trace telling you where the problem occurred.<\/li>\n<\/ul>\n<p><span style=\"font-family: inherit;\">Valgrind is available for download\u00a0<a style=\"text-decoration: none;\" rel=\"self\" href=\"http:\/\/valgrind.org\/downloads\/current.html\">here<\/a>.<br \/>\n<a style=\"text-decoration: none;\" rel=\"self\" href=\"http:\/\/valgrind.org\/docs\/manual\/manual.html\">Here<\/a> is a direct link to the User Manual.<\/span><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Valgrind is an excellent, and sometimes necessary, tool for debugging troublesome programs. According to Valgrind\u2019s website, the program can \u201cautomatically detect many memory management and threading bugs, avoiding hours of frustrating bug-hunting, making your programs more stable. You can also perform detailed profiling to help speed up your programs\u201d. Valgrind contains a variety of tools [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,32,64,11],"tags":[70,31,50,69,72,48,71],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-command-line","category-debugging","category-linux-2","category-mac","tag-c","tag-debug","tag-linux","tag-mac-2","tag-memory-leak","tag-unix","tag-valgrind","no-thumb"],"_links":{"self":[{"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/posts\/26"}],"collection":[{"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":2,"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":74,"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/posts\/26\/revisions\/74"}],"wp:attachment":[{"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}