简体   繁体   English

使用eclipse和cygwin调试C.

[英]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. 我试图在64位窗口上使用eclipse-Juno-CDT和cygwin-gcc(cygwin版本= 1.7.16,gcc版本= 3.4.4,gdb版本= 7.5.50)调试以下C代码。 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:\\). 然后我搜索并添加了路径映射信息(从/ cygdrive / c到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. 我在“hello c 1”行之前放了一个断点,然后单步执行。 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 ..." 单步执行最后一行(“退出”)后,我收到错误:“_cygwin_exit_return()没有可用的源...”

     // 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. 所以我添加了额外的“fflush(stdout)”行,现在我可以在打印时看到输出。

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? 此外,编辑文件以添加fflush以查看调试输出是一件痛苦的事 - 有没有办法避免这种情况? 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. 虽然逻辑上 C程序从int main()开始并在该函数返回时结束,但环境(如Windows或Cygwin)经常添加前后代码,用于初始化/分解内存管理,打开/关闭标准流,以及其他这样的簿记。 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. int main()返回后,使用Cygwin编译的可执行文件切换到由Cygwin运行时提供的清理函数_cygwin_exit_return() - 您没有源代码,因此您的调试器会抱怨。

As for getting the output immediately, you could use an unbuffered output stream. 至于立即获取输出,您可以使用无缓冲的输出流。

Option one, use fprintf( stderr, ... ) (since stderr is by definition unbuffered). 选项一,使用fprintf( stderr, ... ) (因为stderr根据定义是无缓冲的)。 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. 回到我学习多线程的时候,我很好奇线程是否比进程快,而且iirc我甚至不得不在windows上使用stderr / stdout。

Memories aside, you can wrap those printf() in a function that calls fflush, or call setvbuf() to disable buffering. 除了回忆,你可以将那些printf()包装在一个调用fflush的函数中,或者调用setvbuf()来禁用缓冲。

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. 关于退出:“没有源可用”只意味着你的程序的一部分缺少调试信息,所以这不是一个真正的错误 - 除非你自己构建cygwin,我想cygwin dll被剥夺了调试符号。 Or maybe you want to debug cygwin's exit()? 或者你想调试cygwin的exit()?

EDIT: crap, concurrent answers :) 编辑:废话,并发答案:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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