title: Why does gdb mi give me &"\n" as return to my -gdb-exit command?
body: <p>I am writing some code that has to communicate with gdb mi. So I forked my programm, put two pipes in place and started gdb mi in the child, so that I can communicate with gdb mi from the parent.
  gdb always returns &quot;(gdb) \n&quot; when it is finished, so I look for that and write my next command. This is a minimum example of my code:</p>
  <pre><code>int main(){
      printf(&quot;Starting Test\n&quot;);
  
      int fromGDB[2], toGDB[2], nbytes;
      pid_t childpid;
      char readbuffer[80] = &quot;&quot;;
  
      pipe(fromGDB);
      pipe(toGDB);
  
      if((childpid = fork())==-1)
      {
          perror(&quot;fork&quot;);
          exit(1);
      }
  
      if(childpid == 0){
          
          close(toGDB[1]);
          close(fromGDB[0]);
          
          int backup = dup(1);    //store stdout
          if (dup2(fromGDB[1],1) &lt; 0){puts(&quot;hat nicht geklappt&quot;);}
          
          int backupStdin = dup(0); //store stdin
          if (dup2(toGDB[0],0) &lt; 0){puts(&quot;hat nicht geklappt&quot;);}
  
          system(&quot;gdb -q --interpreter=mi2&quot;); // start gdb mi
  
          dup2(backup,1); // restore stdout
          
          puts(&quot;child fertig&quot;);
          exit(0);
          
      }else{
          close(toGDB[0]);
          close(fromGDB[1]);
  
          char* writeCommand = &quot;&quot;;
          int commandCounter = 0;
  
          while (commandCounter &lt;3){
              nbytes = read(fromGDB[0],readbuffer,sizeof(readbuffer));
              printf(&quot;parent recived: %s&quot;, readbuffer);
              if (strncmp(readbuffer+strlen(readbuffer)-strlen(&quot;(gdb) \n&quot;),&quot;(gdb) \n&quot;, strlen(&quot;(gdb) \n&quot;)) == 0){
                  switch (commandCounter){
                      case 0: writeCommand = &quot;-file-exec-and-symbols /home/dev/spielwiese/build/main\n&quot;; break;
                      case 1: writeCommand = &quot;-gdb-exit\n&quot;; break;
                      default: writeCommand = &quot;you should never reach here\n&quot;; break;
                  }
                  write(toGDB[1],writeCommand,strlen(writeCommand)+1);
                  printf(&quot;wrote: %s&quot;, writeCommand);
                  commandCounter++;
              }else if(strncmp(readbuffer,&quot;^exit&quot;, sizeof(&quot;^exit&quot;)-1) == 0){
                  break;
              }
              memset(readbuffer,'\0',strlen(readbuffer)); //clear the readbuffer
          }
  
          puts(&quot;parent fertig&quot;);
          sleep(5);
      }
      
      return 0;
  }
  </code></pre>
  <p>If I call the same commands by hand, thats the output I get (-&gt; means input from me)</p>
  <pre><code>-&gt; gdb -q --interpreter=mi2
  =thread-group-added,id=&quot;i1&quot;
  (gdb) 
  -&gt; -file-exec-and-symbols /home/dev/spielwiese/build/main
  ^done
  (gdb) 
  -&gt; -gdb-exit
  ^exit
  </code></pre>
  <p>But if I run my code, which should be essentialy the same, I get this output:</p>
  <pre><code>Starting Test
  parent recived: =thread-group-added,id=&quot;i1&quot;
  parent recived: (gdb) 
  wrote: -file-exec-and-symbols /home/dev/spielwiese/build/main
  parent recived: ^done
  (gdb) 
  wrote: -gdb-exit
  parent recived: &amp;&quot;\n&quot;
  parent recived: ^done
  (gdb) 
  wrote: you should never reach here
  parent fertig
  </code></pre>
  <p>According to the gdb mi manual, an &amp; preceeds a log entry, but this log entry is empty, exept for the newline. Also, I don't know why there should be a log entry for exiting, or why it fails to exit, but doesen't produce an error.
  Also, if you know any better sources for gdb mi than this: <a href="https://sourceware.org/gdb/current/onlinedocs/gdb/GDB_002fMI.html#GDB_002fMI" rel="nofollow noreferrer">https://sourceware.org/gdb/current/onlinedocs/gdb/GDB_002fMI.html#GDB_002fMI</a> , please let me know.</p>