title: Line numbers in malloc trace using Pin
body: <p>I am working on Pintool for tracing malloc/free operations. From the ManualExamples/malloc_trace.cpp I was able to print out malloc/free in a sample code. </p>
  
  <p>I was wondering if there is a way to print the line numbers in the source code where malloc/free is encountered. In the following malloctrace.cpp where a malloc is instrumented, can I add any parameter or another call to print the line number.</p>
  
  <pre><code>if (RTN_Valid(mallocRtn))
   {   
       RTN_Open(mallocRtn);
       // Instrument malloc() to print the input argument value and the return value.
       RTN_InsertCall(mallocRtn, IPOINT_BEFORE, (AFUNPTR)Arg1Before,
                      IARG_ADDRINT, MALLOC,
                      IARG_FUNCARG_ENTRYPOINT_VALUE, 0, IARG_END); 
       RTN_InsertCall(mallocRtn, IPOINT_AFTER, (AFUNPTR)MallocAfter,
                     IARG_FUNCRET_EXITPOINT_VALUE, IARG_END);
      RTN_Close(mallocRtn);
  }
  </code></pre>
  
  <p>In DebugTrace.cpp, I see there is a function </p>
  
  <pre><code> string FormatAddress(ADDRINT address, RTN rtn)
  </code></pre>
  
  <p>that can print the line number for a RTN. Is there a way to use this function to get the line numbers for malloc?</p>
  
  <p>// EDITED</p>
  
  <p>I have written a new function</p>
  
  <pre><code> VOID printline(ADDRINT instr_ptr )
      {
  // get source line
  
          INT32 line;
          string file,s ;
  
          PIN_LockClient();
           LEVEL_PINCLIENT::PIN_GetSourceLocation(instr_ptr, NULL, &amp;line, &amp;file);
          PIN_UnlockClient();
  
          if (file != "") 
          {   
          TraceFile &lt;&lt; file &lt;&lt; " " &lt;&lt; decstr(line) &lt;&lt; endl;           
          }   
  }
  </code></pre>
  
  <p>and I'm calling in my main function</p>
  
  <pre><code> RTN_InsertCall(mallocRtn, IPOINT_BEFORE, (AFUNPTR)printline,
                         IARG_INST_PTR, IARG_END);
  </code></pre>
  
  <p>Is this the correct way to use "GetSourceLocation" function, I've tried this, it compiles fine but does not print the line numbers.</p>
  
  <p>Thanks,
  K</p>