title: How to capture output of strace in C++ program
body: <p>I want to analyse the output of strace in my C++ program. While launching <code>/bin/strace ps</code> from my app I get an output from ps, but not from strace and strace output is printed to stdout (my terminal). I use standard technique of using pipes and redirecting streams.</p>
  
  <p>Here is my source:</p>
  
  <pre><code>#include &lt;stdlib.h&gt; 
  #include &lt;iostream&gt; 
  #include &lt;sys/types.h&gt; 
  #include &lt;sys/wait.h&gt; 
  #include &lt;unistd.h&gt; 
  #include &lt;fcntl.h&gt;
  int main(){
      char *const parmList[] = {"/bin/strace", "ps", NULL};
      int pipes[2];
      pipe(pipes);
      pid_t child = fork();
      if(child == 0){
          close(pipes[0]);
          dup2(pipes[1],1);
          execv(parmList[0], parmList);
      }
      else{
          int status;
          wait(&amp;status);
  
          fcntl(pipes[0], F_SETFL, O_NONBLOCK | O_ASYNC); 
          char buf[128] = {0}; 
          ssize_t bytesRead; 
          std::string stdOutBuf; 
          while(1) {
              bytesRead = read(pipes[0], buf, sizeof(buf)-1);
              if (bytesRead &lt;= 0)
                  break;
              buf[bytesRead] = 0;
              stdOutBuf += buf;
          } 
          std::cout &lt;&lt; "&lt;stdout&gt;\n" &lt;&lt; stdOutBuf &lt;&lt; "\n&lt;/stdout&gt;" &lt;&lt; std::endl; 
      }
  
      close(pipes[0]);
      close(pipes[1]);
  
      return 0;
   }
  </code></pre>
  
  <p>How can I get an output of strace in my program?</p>