简体   繁体   English

C ++程序意外退出,如何使用gdb调试它?

[英]C++ Program quits unexpectedly, how do I debug this with gdb?

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 . 我正在使用Google C ++测试框架 I run a function that spawns 3 threads, and then runs for 30 seconds. 我运行了一个产生3个线程的函数,然后运行了30秒钟。 After it runs, the program exits with status 0. This is not the expected behavior, obviously. 在运行之后,程序以状态0退出。显然,这不是预期的行为。 I know it doesn't make it any farther, because I put a cout statement in the next immediate line. 我知道它没有任何作用,因为我在下一行直接输入了cout语句。

My question is, what is the best way to go about debugging this with gdb? 我的问题是,使用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;

Besides break exit , there are a couple other places you might need to set breakpoints. 除了break exit ,您可能还需要在其他几个地方设置断点。 Take a look at this question and answers . 看看这个问题和答案

A simple break exit command in gdb should stop the program and allow you to examine the state when the program calls exit from any thread. gdb中的一个简单的break exit命令应该停止程序,并允许您在程序调用任何线程exit时检查状态。

This is of course assuming that the program is ending from exit being called and not for some other reason such as abort , an assertion failure, an unhandled exception, or returning from main. 当然,这是假定程序从被调用的exit结束,而不是出于某些其他原因,例如abort ,断言失败,未处理的异常或从主程序返回。

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

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