{"id":469,"date":"2011-01-11T17:13:58","date_gmt":"2011-01-11T06:13:58","guid":{"rendered":"http:\/\/blog.mozilla.org\/nnethercote\/?p=469"},"modified":"2011-01-11T17:14:56","modified_gmt":"2011-01-11T06:14:56","slug":"using-valgrind-to-get-stack-traces","status":"publish","type":"post","link":"https:\/\/blog.mozilla.org\/nnethercote\/2011\/01\/11\/using-valgrind-to-get-stack-traces\/","title":{"rendered":"Using Valgrind to get stack traces"},"content":{"rendered":"<p>Sometimes I want to do some printf-style debugging where I print not only some values, but also the stack trace each time a particular code point is hit. GNU provides a <code>backtrace()<\/code> function that supposedly does this, but I tried it and got hopeless results, little more than code addresses.<\/p>\n<p>Fortunately, you can do this pretty easily with Valgrind.\u00a0 First, add this line somewhere in your source code:<\/p>\n<pre>  #include &lt;valgrind\/valgrind.h&gt;\r\n<\/pre>\n<p>Then, at the point where you want to print the stack trace, add this:<\/p>\n<pre>  VALGRIND_PRINTF_BACKTRACE(\"foo\");\r\n<\/pre>\n<p>You can of course print something other than &#8220;foo&#8221;.\u00a0 In fact, <code>VALGRIND_PRINTF_BACKTRACE<\/code> is a variadic printf-style function, so you can do stuff like this:<\/p>\n<pre>\u00a0 VALGRIND_PRINTF_BACKTRACE(\"%s: %d\\n\", str, i);\r\n<\/pre>\n<p>You then have to run the program under Valgrind as usual, except you probably should use <code>--tool=none<\/code> because that&#8217;ll run the quickest.<\/p>\n<p>This is a trick I find occasionally invaluable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes I want to do some printf-style debugging where I print not only some values, but also the stack trace each time a particular code point is hit. GNU provides a backtrace() function that supposedly does this, but I tried it and got hopeless results, little more than code addresses. Fortunately, you can do this [&hellip;]<\/p>\n","protected":false},"author":139,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[617,484],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/posts\/469"}],"collection":[{"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/users\/139"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/comments?post=469"}],"version-history":[{"count":0,"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/posts\/469\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/media?parent=469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/categories?post=469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/tags?post=469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}