简体   繁体   中英

debugging C with eclipse and cygwin

I am trying to debug the following C code with eclipse-Juno-CDT, and cygwin-gcc (cygwin version=1.7.16, gcc version=3.4.4, gdb version=7.5.50), on 64bit windows. The code works fine in normal mode. Initially debugger was not running, because the source file was not found. Then I searched around and added the path mapping information (from /cygdrive/c to C:\\). Now it is running but with the following problems:

  1. I have put a breakpoint before the "hello c 1" line, and then single stepping. But nothing gets printed on the console.

  2. after single stepping on the last line ("exit"), I get the error: "No source available for _cygwin_exit_return() at ..."

     // stdio.h and stdlib.h are included, but when I put a #include the code // they dont show up, so I deleted those lines in this code fragment. int main(void) { int a=10; int b=5; // breakpoint on this line, single step after this printf("hello c 1\\n"); // these outputs are not printed in console // fflush(stdout); printf("A=%d, B=%d\\n", a, b); // but debugger shows the correct values in data window // fflush(stdout); return EXIT_SUCCESS; // error on this line } 

Added later: After some more debugging, I figured that even after the exit-error, if I do a "continue", then I am getting the lines on the console after the program terminates. So I added extra "fflush(stdout)" lines, and now I can see the outputs when they are being printed.

But how to fix the exit-error problem? Also, editing the file to add fflush to see debug outputs is a pain - is there a way to avoid this? Can somebody help me with this very basic problem, or point me to a place where the solution is given? Thanks in advance.

While logically a C program begins at int main() and ends when that function returns, environments (like Windows or Cygwin) frequently add pre- and post-code, for initializing / breaking down memory management, opening / closing standard streams, and other such bookkeeping. An executable compiled with Cygwin, after returning from int main() , switches to a cleanup function _cygwin_exit_return() , provided by the Cygwin runtime - for which you don't have sources, so your debugger complains.

As for getting the output immediately, you could use an unbuffered output stream.

Option one, use fprintf( stderr, ... ) (since stderr is by definition unbuffered). This, however, also affects the non-debugging behaviour of your program.

Option two:

int main()
{
// Using NDEBUG as also used by <assert.h>; feel free to use a different define
#ifndef NDEBUG
    // For debugging, set stdout to unbuffered
    setbuf( stdout, NULL );
#endif
    ....

Back when I was learning multithreading I was curious if threads were any faster than processes, and iirc I had to fflush even stderr/stdout on windows.

Memories aside, you can wrap those printf() in a function that calls fflush, or call setvbuf() to disable buffering.

About the exit: "no source available" only means that a part of your program lacks the info for debugging, so it's not a real error -- unless you build cygwin yourself, I guess the cygwin dll is stripped of debug symbols. Or maybe you want to debug cygwin's exit()?

EDIT: crap, concurrent answers :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM