How to get a call stack backtrace? (deeply embedded, no library support)

I want my exception handlers and debug functions to be able to print call stack backtraces, basically just like the backtrace() library function in glibc. Unfortunately, my C library (Newlib) doesn't provide such a call. I've got something like this: #include <unwind.h> // GCC's internal unwinder, part of libgcc _Unwind_Reason_Code trace_fcn(_Unwind_Context *ctx, void *d) { i

如何获得调用堆栈回溯? (深嵌,无图书馆支持)

我希望我的异常处理程序和调试功能能够打印调用堆栈回溯,基本上就像glibc中的backtrace()库函数一样。 不幸的是,我的C库(Newlib)没有提供这样的调用。 我有这样的东西: #include <unwind.h> // GCC's internal unwinder, part of libgcc _Unwind_Reason_Code trace_fcn(_Unwind_Context *ctx, void *d) { int *depth = (int*)d; printf("t#%d: program counter at %08xn", *depth, _Unwind_GetIP(ctx));

Find memory leaks caused by smart pointers

Does anybody know a "technique" to discover memory leaks caused by smart pointers? I am currently working on a large project written in C++ that heavily uses smart pointers with reference counting. Obviously we have some memory leaks caused by smart pointers, that are still referenced somewhere in the code, so that their memory does not get free'd. It's very hard to find the

查找由智能指针引起的内存泄漏

有人知道一种“技巧”来发现由智能指针引起的内存泄漏吗? 我目前正在使用C ++编写的大型项目中大量使用智能指针和引用计数。 显然,我们有一些由智能指针引起的内存泄漏,这些内存泄漏仍然在代码中引用,所以它们的内存不会被释放。 用“不需要的”引用找到代码行非常困难,这会导致相应的对象不被释放(尽管它不再使用)。 我在网络上发现了一些建议,建议收集引用计数器的增量/减量操作的调用堆栈。 这给了我一个很好的提示

stack for exceptions in C++

Today, in my C++ multi-platform code, I have a try-catch around every function. In every catch block I add the current function's name to the exception and throw it again, so that in the upmost catch block (where I finally print the exception's details) I have the complete call stack, which helps me to trace the exception's cause. Is it a good practice, or are there better ways to

在C ++中用于异常堆栈

今天,在我的C ++多平台代码中,我尝试了解每个函数。 在每个catch块中,我将当前函数的名称添加到异常并再次抛出,以便在最上面的catch块(我最终打印异常的详细信息的地方)中有完整的调用堆栈,这有助于我追踪异常的原因。 这是一个很好的做法,还是有更好的方法来获取异常的调用堆栈? 不,它是非常可怕的,我不明白为什么你需要在异常本身中调用堆栈 - 我发现异常的原因,行号和代码的文件名,这些代码在发生初始异常

How do I find the name of the calling function?

I have been using PRETTY_FUNCTION to output the current function name, however I have reimplemented some functions and would like to find out which functions are calling them. In C++ how can I get the function name of the calling routine? Here are two options: You can get a full stacktrace (including the name, module, and offset of the calling function) with recent versions of glibc with th

我如何找到调用函数的名称?

我一直在使用PRETTY_FUNCTION来输出当前的函数名称,但是我已经重新实现了一些函数,并想找出哪些函数正在调用它们。 在C ++中,我如何获得调用例程的函数名称? 这里有两个选项: 您可以使用GNU backtrace函数获取最新版本的glibc的完整堆栈跟踪(包括调用函数的名称,模块和偏移量)。 有关详细信息,请参阅我的答案。 这可能是最简单的事情。 如果这不正是你想要的,那么你可能会尝试libunwind,但它会涉及更多的工

How do you read a segfault kernel log message

This can be a very simple question, I'm am attempting to debug an application which generates the following segfault error in the kern.log kernel: myapp[15514]: segfault at 794ef0 ip 080513b sp 794ef0 error 6 in myapp[8048000+24000] Here are my questions: Is there any documentation as to what are the diff error numbers on segfault, in this instance it is error 6, but i've seen error

你如何阅读segfault内核日志消息

这可能是一个非常简单的问题,我正试图调试一个在kern.log生成以下segfault错误的应用程序 kernel: myapp[15514]: segfault at 794ef0 ip 080513b sp 794ef0 error 6 in myapp[8048000+24000] 这是我的问题: 是否有任何关于segfault的diff错误编号的文档,在这种情况下,它是错误6,但我已经看到错误4,5 at bf794ef0 ip 0805130b sp bf794ef0 and myapp[8048000+24000]的信息含义是什么? 到目前为止,我能够使用符号

How do I find where an exception was thrown in C++?

I have a program that throws an uncaught exception somewhere. All I get is a report of an exception being thrown, and no information as to where it was thrown. It seems illogical for a program compiled to contain debug symbols not to notify me of where in my code an exception was generated. Is there any way to tell where my exceptions are coming from short of setting 'catch throw' in

如何找到在C ++中抛出异常的位置?

我有一个程序在某处引发一个未捕获的异常。 我所得到的只是一个抛出异常的报告,并没有关于抛出异常的信息。 编译为包含调试符号的程序似乎不合逻辑,不会通知我在代码中生成异常的位置。 有没有什么办法可以告诉我哪些异常是由于在gdb中设置'catch throw'而导致的,并且为每个抛出的异常调用了一个回溯? 以下是可能用于调试问题的一些信息 如果一个异常未被捕获,则自动调用特殊库函数std::terminate() 。 Te

C++ display stack trace on exception

I want to have a way to report the stack trace to the user if an exception is thrown. What is the best way to do this? Does it take huge amounts of extra code? To answer questions: I'd like it to be portable if possible. I want information to pop up, so the user can copy the stack trace and email it to me if an error comes up. It depends which platform. On GCC it's pretty trivi

C ++显示堆栈跟踪异常

我想有一种方法可以在引发异常时向用户报告堆栈跟踪。 做这个的最好方式是什么? 是否需要大量额外的代码? 回答问题: 如果可能,我希望它是便携式的。 我想要弹出信息,因此如果出现错误,用户可以复制堆栈跟踪并将其发送给我。 这取决于哪个平台。 在海湾合作委员会这是非常微不足道的,看到这个职位了解更多细节。 在MSVC上,您可以使用StackWalker库来处理Windows所需的所有底层API调用。 您必须找出将此功

How to pass arguments and redirect stdin from a file to program run in gdb?

I usually run a program as : ./a.out arg1 arg2 <file I would like to debug it using gdb. I am aware of the set args functionality, but that only works from the gdb prompt. 将参数从gdb中传递给run命令。 $ gdb ./a.out (gdb) r < t Starting program: /dir/a.out < t You can do this: gdb --args path/to/executable -every -arg you can=think < of The magic bit being --args . Just type

如何传递参数并将stdin从文件重定向到程序在gdb中运行?

我通常运行一个程序为: ./a.out arg1 arg2 <file 我想用gdb调试它。 我知道set args功能,但只能从gdb提示符下工作。 将参数从gdb中传递给run命令。 $ gdb ./a.out (gdb) r < t Starting program: /dir/a.out < t 你可以这样做: gdb --args path/to/executable -every -arg you can=think < of 魔术比特是--args 。 只需在gdb命令控制台中键入run以开始调试。 如果你想在gdb run裸指令来执行带有重定

How do I print the elements of a C++ vector in GDB?

I want to examine the contents of a std::vector in GDB, how do I do it? Let's say it's a std::vector<int> for the sake of simplicity. To view vector std::vector myVector contents, just type in GDB: (gdb) print myVector This will produce an output similar to: $1 = std::vector of length 3, capacity 4 = {10, 20, 30} To achieve above, you need to have gdb 7 (I tested it on gdb 7.

如何在GDB中打印C ++向量的元素?

我想检查GDB中std::vector的内容,我该怎么做? 为了简单起见,我们假设它是一个std::vector<int> 。 要查看向量std :: vector myVector的内容,只需输入GDB: (gdb) print myVector 这将产生类似于以下的输出: $1 = std::vector of length 3, capacity 4 = {10, 20, 30} 为了达到上述目的,你需要有gdb 7(我在gdb 7.01上测试过)和一些python漂亮的打印机。 这些的安装过程在gdb wiki上描述。 更重要的是,在

How do I print the full value of a long string in gdb?

I want to print the full length of a C-string in GDB. By default it's being abbreviated, how do I force GDB to print the whole string? set print elements 0 From the GDB manual: set print elements number-of-elements Set a limit on how many elements of an array GDB will print. If GDB is printing a large array, it stops printing after it has printed the number of elements set by the set pr

如何在gdb中打印长字符串的完整值?

我想在GDB中打印C字符串的全长。 默认情况下它是缩写,我如何强制GDB打印整个字符串? set print elements 0 从GDB手册: set print elements number-of-elements设置数组GDB将打印多少个元素的限制。 如果GDB正在打印大型数组,它会在打印了由set print elements命令set print elements的元素数量后停止打印。 这个限制也适用于字符串的显示。 当GDB启动时,此限制设置为200.将元素数设置为零意味着打印是无限的。 只