{"id":19,"date":"2009-10-03T13:04:00","date_gmt":"2009-10-03T20:04:00","guid":{"rendered":"http:\/\/samueldotj.com\/blog\/?p=19"},"modified":"2013-09-04T14:51:01","modified_gmt":"2013-09-04T21:51:01","slug":"call-trace-without-modifying-the-source","status":"publish","type":"post","link":"http:\/\/samueldotj.com\/blog\/call-trace-without-modifying-the-source\/","title":{"rendered":"Call Trace without modifying the source"},"content":{"rendered":"<p>While investigating about gcc flag &#8220;-fprofile-arcs&#8221;, I came to know about a new(to me) gcc flag and this blog entry is about it. For any large C project it is hard to learn\/find call graph through code walk. From C prospective unless otherwise you put a printf in each function entry\/exit it is hard to find the call trace.<\/p>\n<p>GCC and ICC has a wonderful option &#8220;-finstrument-functions&#8221; to solve this. This option instructs the compiler to emit instructions to call a external function on each function&#8217;s entry\/exit. Defining these two functions like the following and adding the above option -finstrument-function to your makefile will do the magic.<\/p>\n<p>[c]<br \/>\nvoid noinstrument\u00a0__cyg_profile_func_enter(void *this_fn, void *call_site)<br \/>\n{<br \/>\n  printf(&#8220;%p called from %p\\n&#8221;, this_fn, call_site);<br \/>\n}<\/p>\n<p>void noinstrument\u00a0__cyg_profile_func_exit(void *this_fn, void *call_site)<br \/>\n{<br \/>\n  printf(&#8220;%p returns\\n&#8221;, this_fn);<br \/>\n}<br \/>\n[\/c]<\/p>\n<p>Of course, you can do anything in these functions, for simplicity sake I just defined them as printfs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While investigating about gcc flag &#8220;-fprofile-arcs&#8221;, I came to know about a new(to me) gcc flag and this blog entry is about it. For any large C project it is hard to learn\/find call graph through code walk. From C prospective unless otherwise you put a printf in each function entry\/exit it is hard to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,10,9,5],"tags":[],"class_list":["post-19","post","type-post","status-publish","format-standard","hentry","category-c","category-compiler","category-gcc","category-programming"],"_links":{"self":[{"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/posts\/19","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":2,"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":194,"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/posts\/19\/revisions\/194"}],"wp:attachment":[{"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/media?parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/samueldotj.com\/blog\/wp-json\/wp\/v2\/tags?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}