backtrace java_backtrace的用法

backtrace java_backtrace的用法33 1BacktracesA Theusualwayt However

33.1 Backtraces

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 file execinfo.h declares three functions that obtain and manipulate backtraces of the current thread.

— Function: int backtrace (void buffer, int size)

The backtrace function 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 of void * 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 backtrace from interpreting the stack contents correctly.

— Function: char backtrace_symbols (void *const *buffer, int size)

The backtrace_symbols function translates the information obtained from the backtrace function into an array of strings. The argument buffer should be a pointer to an array of addresses obtained via the backtrace function, and size is the number of entries in that array (the return value of backtrace).

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_symbols is a pointer obtained via the malloc function, and it is the responsibility of the caller to free that pointer. Note that only the return value need be freed, not the individual strings.

The return value is NULL if sufficient memory for the strings cannot be obtained.

— Function: void backtrace_symbols_fd (void *const *buffer, int size, int fd)

The backtrace_symbols_fd function performs the same translation as the function backtrace_symbols function. Instead of returning the strings to the caller, it writes the strings to the file descriptor fd, one per line. It does not use the malloc function, 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

#include

#include

/* Obtain a backtrace and print it tostdout. */

void

print_trace (void)

{

void *array[10];

size_t size;

char strings;

size_t i;

size = backtrace (array, 10);

strings = backtrace_symbols (array, size);

printf (“Obtained %zd stack frames.\n”, size);

for (i = 0; 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 0;

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/204937.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午7:11
下一篇 2026年3月19日 下午7:11


相关推荐

  • Ubuntu18.04下卸载CUDA11.0

    Ubuntu18.04下卸载CUDA11.0由于深度学习部分代码仅支持CUDA11.0版本之前的Pytorch,Pytorch官网也没有提供与CUDA11.0配套的版本,因此决定卸载CUDA11.0,但是网上也都是11.0版本之前的卸载方式,因此按照网上的方法进行卸载,由于本人linux操作系统不熟,只能自己尝试首先在安装完CUDA11.0版本后,进入CUDA官网准备安装CUDA10.2,本来没想卸载CUDA11.0但在安装的最后一步出现:说明,在存在更高版本(11.0)情况下,无法正常安装,于是进行卸载:为什么不按照网.

    2022年6月17日
    49
  • 感知机算法详解

    感知机算法详解今天小编给大家带来感知机算法的详解 感知机算法由 Rosenblatt 在 1957 年提出 是一类简单的线性判别算法 通过扩展又可以与许多其他算法密切相关 如逻辑回归模型 支持向量机 前馈神经网络 多层感知机 线性判别分析等 因此感知机算法尽管很少单独使用 但它对于理解其他模型和算法非常有用 很适合作为开始机器学习的一个切入点 同时也是建立知识体系的一个枢纽 本文首先简要介绍感知机 然后讲解感知机的

    2026年3月26日
    2
  • Linux小技巧:tail -f —— 查看动态文本

    Linux小技巧:tail -f —— 查看动态文本说道查看文本:大家很肯定有用过cat:一次查看所有内容,不过文本行数过多,不能全部显示[root@localhostyum.repos.d]#cat163.repo.bak[163repo]name=linuxredhat163.repobaseurl=http://mirrors.163.com/centos/7/os/x86_64/gpgcheck=0enabl…

    2022年6月4日
    44
  • VirtualBox命令行VBoxManage创建与管理虚拟机教程

    VirtualBox命令行VBoxManage创建与管理虚拟机教程VBoxManageisthecommand-lineinterfacetoVirtualBox.前言本文要操作的虚拟机信息如下:名称:UbuntuRDHome镜像名称:ubuntu-16.04.3-server-amd64.iso网络连接:桥接主机环境:$uname-a命令输出:LinuxUbuntuServer

    2022年6月8日
    31
  • FlashFXP注册码、密钥

    FlashFXP注册码、密钥flashfxp3.41中文版注册码:(适合最新版本)推荐(尚未被封的Realkey)FLASHFXPvACq2ssbvAAAAAC1W7cJKQTzmx77zmqJICvA7d3WnUtWNXdrp8YuERRFdIvXfOPbcpABkVix2aRTgg6afcIKFPxS72XYljdE9tgQD/2r+kmfVBngGM4Qc9p7e0PcTfFF/1tt2bqlxS8r0L7…

    2022年7月26日
    45
  • 机器学习之网格搜索(GridSearch)及参数说明,实例演示

    机器学习之网格搜索(GridSearch)及参数说明,实例演示一 GridSearchCV 简介网格搜索 GridSearch 用于选取模型的最优超参数 获取最优超参数的方式可以绘制验证曲线 但是验证曲线只能每次获取一个最优超参数 如果多个超参数有很多排列组合的话 就可以使用网格搜索寻求最优超参数的组合 网格搜索针对超参数组合列表中的每一个组合 实例化给定的模型 做 cv 次交叉验证 将平均得分最高的超参数组合作为最佳的选择 返回模型对象 GridSearc

    2026年3月26日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号