title: fstream does not work well with Pin
body: <p>I've written a very simple Pin tool that only contains one instrumention function, that is, <code>instruction</code> below. It has no analysis function/callback. Its purpose is to only store the address of instructions that may be executed.</p>
  
  <pre><code>#include &lt;cstdint&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;list&gt;
  #include "pin.H"
  
  std::ofstream of1;
  std::list&lt;uint64_t&gt; instrList;
  uint64_t instrCount = 0;
  
  VOID instruction(INS ins, VOID* v)
  {
      ADDRINT ip = INS_Address(ins);
      of1 &lt;&lt; ip &lt;&lt; std::endl;
      of1.flush();
      instrList.push_back(ip);
      ++instrCount;
  }
  
  VOID finishFunc(INT32 code, VOID *v)
  {
      of1.close();
  
      std::ofstream of2;
      of2.open("out2.txt", std::ofstream::out);
      of2 &lt;&lt; std::hex;
      for (std::list&lt;uint64_t&gt;::iterator i = instrList.begin(); i != instrList.end(); ++i)
          of2 &lt;&lt; *i &lt;&lt; std::endl;
      of2.close();
  
      std::cerr &lt;&lt; "Instruction count: " &lt;&lt; instrCount &lt;&lt; std::endl;
  }
  
  int main(int argc, char* argv[])
  {
      PIN_Init(argc, argv);
  
      of1.open("out1.txt", std::ofstream::out);
      of1 &lt;&lt; std::hex;
  
      INS_AddInstrumentFunction(instruction, 0);
      PIN_AddFiniFunction(finishFunc, 0);
  
      PIN_StartProgram();
  
      return 0;
  }
  </code></pre>
  
  <p>The contents of the files <code>out1.txt</code> and <code>out2.txt</code> should be the same.
  I attached this Pin tool to some programs such as <code>/bin/ls</code>. For them, <code>out1.txt</code> and <code>out2.txt</code> are the same. But when I attach it to <code>gcc</code> compiling a code, <code>out1.txt</code> would have more lines than <code>out2.txt</code>. <code>out2.txt</code> has a number of lines that is equal to <code>instrCount</code>, which seems reasonable. It seems that there's a problem with the <code>fstream</code> corresponding to <code>out1.txt</code>. <code>out1.txt</code> has a number of duplicate lines as compared to <code>out2.txt</code>.</p>
  
  <p>These snippets show where they start to differ (marked with <code>&lt;</code>):</p>
  
  <p>out1.txt:</p>
  
  <pre><code>7f87ceb050a2
  7f87ceb050a4
  7f87ceb050a5
  7f87ceb050a8
  7f87ceb050aa
  7f87ceb050b2
  7f87ceb050b7
  7f87ceb050b9
  &lt;7f87ceb050a4
  &lt;7f87ceb050a5
  &lt;7f87ceb050a8
  &lt;7f87ceb050aa
  &lt;7f87ceb050b2
  &lt;7f87ceb050b7
  &lt;7f87ceb050b9
  &lt;7f87ceb050b2
  45218c
  &lt;7f87ceb050b7
  45218e
  &lt;7f87ceb050b9
  452190
  </code></pre>
  
  <p>out2.txt:</p>
  
  <pre><code>7f87ceb050a2
  7f87ceb050a4
  7f87ceb050a5
  7f87ceb050a8
  7f87ceb050aa
  7f87ceb050b2
  7f87ceb050b7
  7f87ceb050b9
  45218c
  45218e
  452190
  </code></pre>
  
  <p>I used a <code>PIN_LOCK</code> inside <code>instruction</code> function, but that doesn't change anything. I also tried to write into output files using <code>fprintf</code>, but the problem still exists.</p>
  
  <p>Any idea how to solve this issue?</p>