C++程序意外退出,如何使用 gdb 进行调试?
我正在编写一个程序,对我的同事编写的代码运行一些单元测试。我正在使用 Google C++ 测试框架。我运行一个生成 3 个线程的函数,然后运行 30 秒。运行后,程序以状态 0 退出。显然,这不是预期的行为。我知道它不会再继续下去,因为我在下一行中放置了一个 cout 语句。
我的问题是,使用 gdb 进行调试的最佳方法是什么?这很困难,因为程序没有段错误或类似的东西,它只是退出。有没有办法挂钩退出调用,然后获得长回溯?
感谢您的帮助。
编辑:
cSystemCfg* pSystemCfg = new cSystemCfg();
std::cout << "Before runThing" << std::endl;
pSomething->runThing(&acq, pHwIf, pSystemCfg, pIf);
//Exits here, never gets to the next line
std::cout << "After runThing" << std::endl;
I am writing a program that runs some unit tests on code that that been written by my colleagues. I am using the Google C++ testing framework. I run a function that spawns 3 threads, and then runs for 30 seconds. After it runs, the program exits with status 0. This is not the expected behavior, obviously. I know it doesn't make it any farther, because I put a cout statement in the next immediate line.
My question is, what is the best way to go about debugging this with gdb? It is difficult because the program doesn't segfault or anything like that, it just exits. Is there a way to hook an exit call, and then get a long backtrace?
Thank you for your help.
Edit:
cSystemCfg* pSystemCfg = new cSystemCfg();
std::cout << "Before runThing" << std::endl;
pSomething->runThing(&acq, pHwIf, pSystemCfg, pIf);
//Exits here, never gets to the next line
std::cout << "After runThing" << std::endl;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了
break exit
之外,还有其他几个地方可能需要设置断点。查看此问题和答案。Besides
break exit
, there are a couple other places you might need to set breakpoints. Take a look at this question and answers.gdb 中的一个简单的
break exit
命令应该会停止程序,并允许您在程序从任何线程调用exit
时检查状态。当然,这是假设程序因
exit
被调用而结束,而不是由于其他原因,例如abort
、断言失败、未处理的异常或从 main 返回。A simple
break exit
command in gdb should stop the program and allow you to examine the state when the program callsexit
from any thread.This is of course assuming that the program is ending from
exit
being called and not for some other reason such asabort
, an assertion failure, an unhandled exception, or returning from main.