title: Why does Cygwin gdb have a problem debugging a program with threads?
body: <p>I am having trouble debugging programs with threads on Cygwin.</p>
  
  <p>When the following program gets to the <code>pthread_create()</code> call, the debug session gets an unknown target exception, apparently in a call to <code>ntdll!RtlAllocateHeap()</code>.</p>
  
  <p>I know in the past I have been able to debug threads in Eclipse -- are there some new secret settings I missed, or is this a problem with Eclipse?</p>
  
  <p>For the record, I am running Cygwin64 (CYGWIN_NT-6.1) with gcc 7.4.0 / gdb 8.1.1 / Eclipse 4.13.0.</p>
  
  <p>The programs runs fine when run directly from the command line. I can also start the program in gdb, and step through statements until it gets to the <code>pthread_create()</code> call.</p>
  
  <p>The gdb session:</p>
  
  <pre><code>jay.elston@M4800-1RBTK12 ~/threadTest
  $ cc -g threads.c
  
  jay.elston@M4800-1RBTK12 ~/threadTest
  $ gdb a.exe
  GNU gdb (GDB) (Cygwin 8.1.1-1) 8.1.1
  Copyright (C) 2018 Free Software Foundation, Inc.
  License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
  This is free software: you are free to change and redistribute it.
  There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
  and "show warranty" for details.
  This GDB was configured as "x86_64-pc-cygwin".
  Type "show configuration" for configuration details.
  For bug reporting instructions, please see:
  &lt;http://www.gnu.org/software/gdb/bugs/&gt;.
  Find the GDB manual and other documentation resources online at:
  &lt;http://www.gnu.org/software/gdb/documentation/&gt;.
  For help, type "help".
  Type "apropos word" to search for commands related to "word"...
  Reading symbols from a.exe...done.
  (gdb) b 13
  Breakpoint 1 at 0x10040108c: file threads.c, line 13.
  (gdb) run
  Starting program: /home/jay.elston/threadTest/a.exe
  [New Thread 624.0xebc]
  [New Thread 624.0x2ef0]
  [New Thread 624.0x1b24]
  [New Thread 624.0x1074]
  [New Thread 624.0x2088]
  [New Thread 624.0x2710]
  [New Thread 624.0x2a1c]
  gdb: unknown target exception 0x80000001 at 0x77a97b97
  
  Thread 7 received signal ?, Unknown signal.
  [Switching to Thread 624.0x2a1c]
  0x0000000077a97b97 in ntdll!RtlAllocateHeap ()
     from /cygdrive/c/Windows/SYSTEM32/ntdll.dll
  (gdb)c
  Continuing.
  [New Thread 624.0x2374]
  [Thread 624.0x2710 exited with code 2147483649]
  [Thread 624.0x2a1c exited with code 2147483649]
  [Thread 624.0x2088 exited with code 2147483649]
  [Thread 624.0x1b24 exited with code 2147483649]
  [Thread 624.0x1074 exited with code 2147483649]
  [Thread 624.0x2374 exited with code 2147483649]
  [Inferior 1 (process 624) exited with code 020000000001]
  (gdb) quit
  
  jay.elston@M4800-1RBTK12 ~/threadTest
  $ ./a.exe
  1
  2
  3
  4
  5
  6
  7
  8
  9
  10
  11
  12
  13
  14
  15
  </code></pre>
  
  <p>The program:</p>
  
  <pre><code>// Start a thread using pthread_create
  #include &lt;stdio.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;time.h&gt;
  #include &lt;string.h&gt;
  #include &lt;pthread.h&gt;
  
  void *fifteenSeconds(void *arg)
  {
      struct timespec sleepTime;
      struct timespec sleptTime;
  
      int i = 0;
  
      while ( i++ &lt; 15 ) {
          printf("%d\n", i);
          sleepTime.tv_sec = 01;
          sleepTime.tv_nsec = 000000000;
          nanosleep(&amp;sleepTime, &amp;sleptTime);
      }
      return (void *)0;
  }
  
  int main ( int argc, char *argv[] )
  {
      pthread_t fifteenSecondsThreadId;
  
      int rc = 0;
  
      rc = pthread_create(&amp;fifteenSecondsThreadId, (void *)0, &amp;fifteenSeconds, (void *)0);
      if ( rc != 0 ) {
          fprintf(stderr, "(%s,%d): Error %d creating thread for io handler: %s\n"
                  , __FILE__, __LINE__
                  , errno, strerror(errno)
                  );
          return rc;
      }
  
      pthread_join(fifteenSecondsThreadId, (void *)0);
  
      return 0;
  }
  </code></pre>