title: How to use etrace with a dynamic library for chronological traceing of function calls in C++?
body: <p><strong>Background:</strong></p>
  
  <p>I have one big simulation tool, and I need to understand its logical behavior. In order to do that, the most of help I would get if I have the chronological order of function calls, for a minimal working example.</p>
  
  <p>I found many tools online, like <a href="http://www.logix.cz/michal/devel/CygProfiler/" rel="nofollow noreferrer">CygProfiler</a> and <a href="https://github.com/elcritch/etrace" rel="nofollow noreferrer">etrace</a>. I became so miserable on finding a solution that I started to follow the craziest solution of using "step into" with the debugger. Which is a good option if you have a small program but not a complete simulation tool.</p>
  
  <hr>
  
  <p><strong>Problem:</strong></p>
  
  <p>One of the problems I face is that the above-mentioned solutions are originally meant for <code>C</code> and they generate a static file (<code>*.o</code>) when compiled. On the other hand the simulation tool generates a shared library (<code>.so</code>). I don't have much knowledge on lower level stuff so I seem to fail when I try linking them.</p>
  
  <p>I looked specifically at the <code>etrace</code> <a href="https://github.com/elcritch/etrace/blob/master/README" rel="nofollow noreferrer">documentation</a>, and it says:</p>
  
  <blockquote>
    <p>To see how to modify ptrace.c to work with a dynamic library, look at
    the example2 directory. The sources there also create a stand-alone
    executable, but the PTRACE_REFERENCE_FUNCTION macro is defined just as
    it would be for a dynamic library.</p>
  </blockquote>
  
  <p>If you look at the repo there is no difference between the files in <code>example</code> and <code>example2</code> folders. Only there is an extra <code>.h</code> file in <code>example2</code>.</p>
  
  <p>On the other hand, if you look at <code>src/ptrace.c</code> there it says:</p>
  
  <blockquote>
    <p>When using ptrace on a dynamic library, you must set the
      PTRACE_REFERENCE_FUNCTION macro to be the name of a function in the
      library. The address of this function when loaded will be the first
      line output to the trace file and will permit the translation of the
      other entry and exit pointers to their symbolic names. You may set
      the macro PTRACE_INCLUDE with any #include directives needed for
      that function to be accesible to this source file.</p>
  </blockquote>
  
  <p>a little below there is the commented code:</p>
  
  <pre><code>/* When using ptrace on a dynamic library, the following must be defined:
  #include "any files needed for PTRACE_REFERENCE_FUNCTION"
  #define PTRACE_REFERENCE_FUNCTION functionName
  `*/
  </code></pre>
  
  <hr>
  
  <p><strong>Question:</strong></p>
  
  <p>In essence the question is the following: How to use <code>etrace</code> with a dynamic library?</p>
  
  <p>Do I need to #include any files?</p>
  
  <blockquote>
    <p>To trace a stand-alone program, there is no need to #include any
    additional file. Just link your code against ptrace.c and use the
    -finstrument-functions option as a compile option for gcc. This should do it.</p>
  </blockquote>
  
  <p>How do I link a C++ code which is built via makefiles against <code>ptrace.c</code></p>
  
  <p><strong>Final Note:</strong> I would appreciate if someone bears with my ignorance and provides a step-by-step solution to my question.</p>
  
  <hr>
  
  <p><strong>Update 1:</strong></p>
  
  <p>I managed to add the libraries related to etrace to the simulation tool, and it executes fine. </p>
  
  <p>However, (probably because the scripts are too old, or are not meant for use with C++) I get the following error when using the <a href="https://github.com/elcritch/etrace/blob/master/src/etrace.pl" rel="nofollow noreferrer">perl script</a> provided by default by <code>etrace</code></p>
  
  <blockquote>
  <pre><code>Hexadecimal number &gt; 0xffffffff non-portable"
  </code></pre>
  </blockquote>
  
  <p>Probably this changes a bit the nature of this question, turning it more to a perl related issue at this point. </p>
  
  <p>If this problem is solved, I hope <code>etrace</code> will work with a complicated project and I will provide the details</p>
  
  <hr>
  
  <p><strong>Update 2:</strong></p>
  
  <p>I took the suggestions from @Harry, and I believe that would work in most projects. However in my case I get the following from the perl script:</p>
  
  <pre><code>Use of uninitialized value within %SYMBOLTABLE in list assignment at etrace2.pl line 99, &lt;CALL_DATA&gt; line 1.
  
  \-- ???
  |   \-- ???
  \-- ???
  |   \-- ???
  |   |   \-- ???
  \-- ???
  |   \-- ???
  \-- ???
  |   \-- ???
  \-- ???
  |   \-- ???
  \-- ???
  |   \-- ???
  \-- ???
  |   \-- ???
  </code></pre>
  
  <p>Due to autegenerated makefiles I used the <a href="https://stackoverflow.com/a/36959597/4317408">LD_PRELOAD</a> to load the shared library for etrace.so which I got as follows:</p>
  
  <pre><code>gcc -g -finstrument-functions -shared -fPIC ptrace.c -o etrace.so -I &lt;path-to-etrace&gt;
  </code></pre>
  
  <p>I created the dummy etrace.h inside the tool:</p>
  
  <pre><code>#ifndef __ETRACE_H_
  #define __ETRACE_H_
  
  #include &lt;stdio.h&gt;
  
  void Crumble_buy(char * what, int quantity, char * unit);
  
  
  void Crumble_buy(char * what, int quantity, char * unit)
  {
      printf("buy %d %s of %s\n", quantity, unit, what);
  }
  
  #endif
  </code></pre>
  
  <p>and used <code>Crumble_buy</code> for the <code>#define</code> and the <code>etrace.h</code> for the <code>#include</code>.</p>