c语音中打印参数调用层级即call stack, call trace
http://stackoverflow.com/questions/105659/how-can-one-grab-a-stack-trace-in-c
There's backtrace(), and backtrace_symbols():
From the man page:
#include <execinfo.h>
#include <stdio.h>
...
void* callstack[];
int i, frames = backtrace(callstack, );
char** strs = backtrace_symbols(callstack, frames);
for (i = ; i < frames; ++i) {
printf("%s\n", strs[i]);
}
free(strs);
...
One way to use this in a more convenient/OOP way is to save the result of backtrace_symbols() in an exception class constructor. Thus, whenever you throw that type of exception you have the stack trace. Then, just provide a function for printing it out. For example:
class MyException : public std::exception {
char ** strs;
MyException( const std::string & message ) {
int i, frames = backtrace(callstack, );
strs = backtrace_symbols(callstack, frames);
}
void printStackTrace() {
for (i = ; i
......
try {
throw MyException("Oops!");
} catch ( MyException e ) {
e.printStackTrace();
}
提示,最好把编译优化去掉,不然打印可能不准确。
http://www.gnu.org/software/libc/manual/html_node/Backtraces.html
A backtrace is a list of the function calls that are currently active in a thread. The usual way to inspect a backtrace of a program is to use an external debugger such as gdb. However, sometimes it is useful to obtain a backtrace programmatically from within a program, e.g., for the purposes of logging or diagnostics.
The header fileexecinfo.hdeclares three functions that obtain and manipulate backtraces of the current thread.
- Function: int backtrace (void **buffer, int size)
-
Preliminary: | MT-Safe | AS-Unsafe init heap dlopen plugin lock | AC-Unsafe init mem lock fd | See POSIX Safety Concepts.
The
backtracefunction obtains a backtrace for the current thread, as a list of pointers, and places the information into buffer. The argument size should be the number ofvoid *elements that will fit into buffer. The return value is the actual number of entries of buffer that are obtained, and is at most size.The pointers placed in buffer are actually return addresses obtained by inspecting the stack, one return address per stack frame.
Note that certain compiler optimizations may interfere with obtaining a valid backtrace. Function inlining causes the inlined function to not have a stack frame; tail call optimization replaces one stack frame with another; frame pointer elimination will stop
backtracefrom interpreting the stack contents correctly.
- Function: char ** backtrace_symbols (void *const *buffer, int size)
-
Preliminary: | MT-Safe | AS-Unsafe heap | AC-Unsafe mem lock | See POSIX Safety Concepts.
The
backtrace_symbolsfunction translates the information obtained from thebacktracefunction into an array of strings. The argument buffer should be a pointer to an array of addresses obtained via thebacktracefunction, and size is the number of entries in that array (the return value ofbacktrace).The return value is a pointer to an array of strings, which has size entries just like the array buffer. Each string contains a printable representation of the corresponding element of buffer. It includes the function name (if this can be determined), an offset into the function, and the actual return address (in hexadecimal).
Currently, the function name and offset only be obtained on systems that use the ELF binary format for programs and libraries. On other systems, only the hexadecimal return address will be present. Also, you may need to pass additional flags to the linker to make the function names available to the program. (For example, on systems using GNU ld, you must pass (
-rdynamic.)The return value of
backtrace_symbolsis a pointer obtained via themallocfunction, and it is the responsibility of the caller tofreethat pointer. Note that only the return value need be freed, not the individual strings.The return value is
NULLif sufficient memory for the strings cannot be obtained.
- Function: void backtrace_symbols_fd (void *const *buffer, int size, int fd)
-
Preliminary: | MT-Safe | AS-Safe | AC-Unsafe lock | See POSIX Safety Concepts.
The
backtrace_symbols_fdfunction performs the same translation as the functionbacktrace_symbolsfunction. Instead of returning the strings to the caller, it writes the strings to the file descriptor fd, one per line. It does not use themallocfunction, and can therefore be used in situations where that function might fail.
The following program illustrates the use of these functions. Note that the array to contain the return addresses returned by backtrace is allocated on the stack. Therefore code like this can be used in situations where the memory handling via malloc does not work anymore (in which case the backtrace_symbols has to be replaced by a backtrace_symbols_fd call as well). The number of return addresses is normally not very large. Even complicated programs rather seldom have a nesting level of more than, say, 50 and with 200 possible entries probably all programs should be covered.
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h> /* Obtain a backtrace and print it to stdout. */
void
print_trace (void)
{
void *array[];
size_t size;
char **strings;
size_t i; size = backtrace (array, );
strings = backtrace_symbols (array, size); printf ("Obtained %zd stack frames.\n", size); for (i = ; i < size; i++)
printf ("%s\n", strings[i]); free (strings);
} /* A dummy function to make the backtrace more interesting. */
void
dummy_function (void)
{
print_trace ();
} int
main (void)
{
dummy_function ();
return ;
}
c语音中打印参数调用层级即call stack, call trace的更多相关文章
- JAVA学习笔记--方法中的参数调用是引用调用or值调用
文献来源:<JAVA核心技术卷Ⅰ>,第4章第5节 (没有相关书籍的可看传送门) ps:测试demo因为偷懒,用的是String对象 结论:Java使用的是对象的值引用.即将任何对象所在内存 ...
- unity 如何在botton AddListen中传递参数调用函数
使用Deleget方法包含该函数即可. levelItem.GetComponent<Toggle().onValueChanged.AddListener(SetSelectedLevel(l ...
- 描述了say_hello函数的具体内容,调用zend_printf系统函数在php中打印字符串
下载一个php的源代码包,这里使用的是php 4.0.5版,解压后会看到php的根目录下会有README.EXT_SKEL这样一个文件,打开详细阅读了一下,发现了一个非常好用的工具,这个工具可以帮你构 ...
- Liferay中SQL打印参数
XX\tomcat-7.0.42\webapps\ROOT\WEB-INF\classes\log4j.properties log4j.rootLogger=INFO, CONSOLE log4 ...
- 程序中打印当前进程的调用堆栈(backtrace)
为了方便调式程序,产品中需要在程序崩溃或遇到问题时打印出当前的调用堆栈.由于是基于Linux的ARM嵌入式系统,没有足够的空间来存放coredump文件. 实现方法,首先用__builtin_fram ...
- 在go中通过cmd调用python命令行参数量级过大问题解决
问题描述如下: 在go中使用cmd调用python命令行 cmd := exec.Command("python", "dimine/Kriging/matrix.py& ...
- Tom_No_02 Servlet向流中打印内容,之后在调用finsihResponse,调用上是先发送了body,后发送Header的解释
上次在培训班学上网课的时候就发现了这个问题,一直没有解决,昨天又碰到了,2-3小时也未能发现点端倪,今早又仔细缕了下,让我看了他的秘密 1.Servlet向流中打印内容,之后在调用finsihResp ...
- vlc 详细使用方法:libvlc_media_add_option 函数中的参数设置
vlc 详细使用方法:libvlc_media_add_option 函数中的参数设置 [转载自]tinyle的专栏 [原文链接地址]http://blog.csdn.net/myaccella/ar ...
- UIViewController中各方法调用顺序及功能详解
UIViewController中各方法调用顺序及功能详解 UIViewController中loadView, viewDidLoad, viewWillUnload, viewDidUnload, ...
随机推荐
- starling 中的 EventDispatcher 和 Flash中原生的 EventDispatcher
starling 比较早之前就有开始解了,但只到最近参与一个用starling 做为框架的手游项目才真正做为一程来使用它. 项目也是刚开始搭建,在这做些笔记. 在写一个管理类时, 遇到 starlin ...
- jquery-ui-datepicker定制化,汉化,因手机布局美观化源码修改
感谢浏览,欢迎交流=.= 公司微信网页需要使用日历控件,想到jquery-mobile,但是css影响页面布局,放弃后使用jquery-ui-datepicker. 话不多说,进入正题: 1.jque ...
- PHP文件下载方式
<?php// 不能是中文目录,其实如果是 .rar, .zip 类的这些文件,直接连接就可以下载了!function download($file_dir,$file_name)//参数说明: ...
- c 的ui
你这些问题是必须要搞清楚的. 1. 首先,UI的实现在不同的操作系统平台上是有所不同的.Windows的UI是内置于操作系统的,Linux/Unix的UI则是通过一个应用程序实现的.由此看来,变成语言 ...
- 学一点 mysql 双机异地热备份----快速理解mysql主从,主主备份原理及实践
双机热备的概念简单说一下,就是要保持两个数据库的状态 自动同步.对任何一个数据库的操作都自动应用到另外一个数据库,始终保持两个数据库数据一致. 这样做的好处多. 1. 可以做灾备,其中一个坏了可以切换 ...
- Codeforces Round #336 (Div. 2) D. Zuma
Codeforces Round #336 (Div. 2) D. Zuma 题意:输入一个字符串:每次消去一个回文串,问最少消去的次数为多少? 思路:一般对于可以从中间操作的,一般看成是从头开始(因 ...
- 离开ACM了,总结一下
写这篇博客,一如当初我对着电脑显示器,不知道从哪里下手才是,所以没准写着写着就出现了倒叙插叙补叙等充满语文功底的修辞手法,不过不会有45度的妩媚和忧伤. 像一位程序员所说:今天的努力是为了儿时吹过的牛 ...
- 阿里云centOS6 下python安装及配置、pip安装及配置、ipython安装及配置
我是在阿里云服务器上进行的python环境搭建,阿里云服务器会自带python但是版本低,所以打算自己安装一个,期间遇到各种问题,而且百度根本不够用无奈上的外网很快解决了.在此分享一下. 一.pyth ...
- DM8168 编译filesystem步骤
在板子跑起来之前,需要先编译好8168的文件系统.前提是已经设置好板子的类型等参数,详见<DM8168环境搭建> 1.进入<DVR_RDK_BASE>/dvr_rdk目录 ma ...
- python中的__init__ 、__new__、__call__等内置函数的剖析
1.__new__(cls, *args, **kwargs) 创建对象时调用,返回当前对象的一个实例;注意:这里的第一个参数是cls即class本身2.__init__(self, *args, ...