title: The shared memory between threads
body: <p>I use the Intel tool <code>Pin</code> to instrument the multi-thread process and monitor the shared memory access between thread on Linux, I develop a tool in <code>Pin</code> to record the shared memory address, the instrumentation code in Pin is as follow:</p>
  
  <pre><code>VOID Instruction(INS ins, VOID *v)
  {
      UINT32 memOperands = INS_MemoryOperandCount(ins);
  
      // Iterate over each memory operand of the instruction.
      for (UINT32 memOp = 0; memOp &lt; memOperands; memOp++)
      {
          if (INS_MemoryOperandIsRead(ins, memOp))
          {
              INS_InsertPredicatedCall(
                  ins, IPOINT_BEFORE, (AFUNPTR)RecordMemRead,
                  IARG_INST_PTR,
                  IARG_MEMORYOP_EA, memOp,
                  IARG_END);
          }
          // Note that in some architectures a single memory operand can be
          // both read and written (for instance incl (%eax) on IA-32)
          // In that case we instrument it once for read and once for write.
          if (INS_MemoryOperandIsWritten(ins, memOp))
          {
              INS_InsertPredicatedCall(
                  ins, IPOINT_BEFORE, (AFUNPTR)RecordMemWrite,
                  IARG_INST_PTR,
                  IARG_MEMORYOP_EA, memOp,
                  IARG_END);
          }
      }
  }
  </code></pre>
  
  <p>the function <code>RecordMemRead</code> and <code>RecordMemWrite</code> is used to record thread's information and memory address when read or write memory, and I have use lock in this function.<br />
  I want to record the memory address shared between threads, such as global variables or heap memory.<br />
  But when I use a simple multi-thread program to test my tool. The test is as follow. In this program, user have not define any shared variable or shared memory:<br /></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  
  void * fun1(void *arg)
  {
  
  }
  
  int main(int argc,char* argv[])
  {
      pthread_t npid1;
  
      pthread_create(&amp;npid1,NULL,fun1,NULL);
  
      pthread_join(npid1,NULL);
  
      return 0;
  }
  </code></pre>
  
  <p>and the result indicate the memory which have accessed by muti-thread and output the debugging information of memory access instruction in the next line :</p>
  
  <pre><code>read addr: b775252c
  line:0 col: 0 file: 
  write addr: b775252c
  line:0 col: 0 file: 
  write addr: b775252c
  line:0 col: 0 file: 
  write addr: b775252c
  line:0 col: 0 file: 
  write addr: b775252c
  line:0 col: 0 file: 
  write addr: b775252c
  line:0 col: 0 file: 
  write addr: b775252c
  line:0 col: 0 file: 
  read addr: b556ad64
  line:0 col: 0 file: 
  read addr: b556abc4
  line:0 col: 0 file: 
  write addr: b556abc4
  line:0 col: 0 file: 
  </code></pre>
  
  <p>The result indicate that there is some memory access by both two threads, and the read/write instruction has no debugging information( I have add -g option in compile), So maybe these memory is accessed by library<br />Q1: what threads do with these memory? <br />Q2: If I just want to monitor the memory defined by user, not defined in library, How to distinguish them?</p>