title: linux ptrace() get function information
body: <p>i want to catch information from user defined function using ptrace() calls. </p>
  
  <p>but function address is not stable(because ASLR). </p>
  
  <p>how can i get another program's function information like gdb programmatically?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/user.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;sys/ptrace.h&gt;
  #include &lt;dlfcn.h&gt;
  #include &lt;errno.h&gt;
  
  void error(char *msg)
  {
      perror(msg);
      exit(-1);
  }
  
  int main(int argc, char **argv)
  {
      long ret = 0;
      void *handle;
      pid_t pid = 0;
      struct user_regs_struct regs;
      int *hackme_addr = 0;
  
      pid = atoi(argv[1]);
  
      ret = ptrace(PTRACE_ATTACH, pid, NULL, NULL);
      if(ret&lt;0)
      {
          error("ptrace() error");
      }
  
      ret = waitpid(pid, NULL, WUNTRACED);
      if(ret&lt;0)
      {
          error("waitpid ()");
      }
  
      ret = ptrace(PTRACE_GETREGS, pid, NULL, &amp;regs);
      if(ret&lt;0)
      {
          error("GETREGS error");
      }
  
      printf("EIP : 0x%x\n", (int)regs.eip);
  
      ptrace(PTRACE_DETACH, pid, NULL, NULL);
  
      return 0;
  }
  </code></pre>