在Linux中打印函数调用栈

Linux下使用backtrace打印函数调用栈信息 Linux下使用backtrace打印函数调用栈信息 Java和Python等语言都有比较简便的方法可以打印函数调用栈,那么在Linux下使用C语言有没有办法呢? 据说有多种方法。本文介绍最基本的方法,即使用 glibc 的 backtrace() 和 backtrace_symbols() 等 API. 在 Linux 下,运行 man 命令可以查看到帮助文档。 man 3 backtrace 文档并不长。下面翻译主要部分如下: 首先,需要包含头文件,并看一下相关的3个API的声明: #include 阅读详情

在实际调试程序时,往往需要知道函数的调用关系,尤其是在阅读一些开源代码或者linux kernel程序时,打印出函数的调用栈有助于我们从整体上把握代码框架结构。linux提供了三个函数帮助我们打印函数调用栈。

/*
* 函数说明: 取得当前函数的调用堆栈
* 参数:
*     buffer:用于存储函数地址的数组
*     size:buffer数组的长度
* 返回值:
*      存储到数组中的函数个数
*/
int backtrace(void **buffer, int size);
 
/*
*
* 函数说明:将一组函数地址转换为字符串
* 参数:
*      buffer: 经由backtrace得到的函数地址
*      size: buffer数组的长度
* 返回值:
*       函数在系统中对应用字符串
*/
char **backtrace_symbols(void *const *buffer, int size);
 
/*
* 函数说明:将一组函数地址转换为字符串
* 参数:
*      buffer: 经由backtrace得到的函数地址
*      size: buffer数组的长度
*      fd: 输出结果文件描述符
*/
void backtrace_symbols_fd(void *const *buffer, int size, int fd);

针对上面三个函数,进行了如下的封装,可以直接调用以下代码,通过定义宏控制是否调用。

#ifdef  __DEBUG_CALL_STACK__

#define DEBUG_MSG_1(fmt, args...)     do {FILE *fp = fopen("/www/abc", "a+");\
     fprintf(fp, ":"fmt"",    ##args); \
     fclose(fp); fp= NULL; } while(0)
    {

        void *array[10];
        size_t size;
        char **strings;
        size_t i;
        size = backtrace(array, 10);
        strings = backtrace_symbols(array, size);
        DEBUG_MSG_1("pid=%d, procname=%s,vpn4a_global_mutex_lock",
                getpid(), getenv("_")
            );
        DEBUG_MSG_1("Obtained %zd stack frames.\n", size);
        for (i = 0; i < size; i++)
            DEBUG_MSG_1("%s\n", strings[i]);

        DEBUG_MSG_1("\n");
        free(strings);
    }
#endif

多模态大模型助力AI人工智能领域的智能决策 我们的目的是深入了解多模态大模型是怎样在AI人工智能领域帮助做出智能决策的。范围涵盖了多模态大模型的基本概念、工作原理、在不同场景下的应用,以及未来的发展方向等。接下来,我们会先介绍多模态大模型和智能决策的核心概念,再讲讲它们之间的关系。然后说说多模态大模型在智能决策中的算法原理和操作步骤,用数学公式来解释一下。接着通过一个项目实战案例让大家更直观地感受。之后看看多模态大模型在实际中的应用场景,推荐一些学习的工具和资源。最后探讨未来的发展趋势和挑战,进行总结并提出一些思考题。多模态大模型。 阅读详情

相关推荐

MATLAB功率谱函数psd与pwelch的使用

在网上看到 clear; Fs=1000; n=0:1/Fs:1; y=cos(2*pi*40*n)+3*cos(2*pi*100*n)+randn(size(n)); Nfft=1000; window=hanning(100); noverlap=20; dflag='none'; [Pxx,f]=psd(y,Nfft,Fs,window,noverlap); figure plot(f,10*log10(Pxx)); xlabel('frequency/Hz');ylab

qq_41687938的博客 2万+

Android动态语言切换全解析:从Android 4.4到10的兼容实践

在移动应用开发中,多语言支持是拓展海外市场的关键。Android系统通过Locale、Configuration和Resources的协同工作实现语言适配,但原生方案仅支持跟随系统语言。动态语言切换技术允许应用内部自由切换语言并立即生效,其核心原理是手动构造带目标Locale的Configuration,并通过createConfigurationContext创建新的Context,从而加载对应语言的资源。这项技术能显著提升用户体验,尤其适用于工具类、教育类等需要多语言切换的App。实现时需注意Andro

weixin_34279579的博客 675

CentOS7.9 安装 ImageMagick,图片转换为HEIC图像格式

由于ImageMagick7以下不支持heic等图片格式,所以重新安装了ImageMagick7.1版本支持heic格式,在网上找了很多资料,归纳总结并写此文章记录一下。在Linux(CentOS7.9)上安装ImageMagick7.1.1-38。

shmily_i的博客 1940

在linux中如何调用函数调用函数,在linux代码中打印函数调用的堆栈的方法

之前一直有这样的需求,当时问到,也没搜到方法,现在竟然既问到了,也搜到了,哎,世事真是不能强求啊!在Linux内核调试中,经常用到的打印函数调用堆栈的方法非常简单,只需在需要查看堆栈的函数中加入:dump_stack();或 __backtrace();即可。dump_stack()在~/kernel/lib/Dump_stack.c中定义void dump_stack(void){printk...

weixin_42405371的博客 269

【模拟电子设计实战】开源!基于Multisim与AD620的心电采集电路仿真与优化

本文详细介绍了基于Multisim与AD620的心电采集电路仿真与优化实战。通过分析心电信号采集的挑战,重点讲解了AD620仪表放大器和OP07运算放大器的选型优势及电路设计技巧,包括对抗50Hz工频干扰和基线漂移的优化方法。文章还分享了Multisim仿真参数设置与实测对比经验,最终实现性能达标的心电采集电路设计。

weixin_30555125的博客 95

ArcGIS图解建模工具与DEM提取山脊、山谷线(水文分析、表面分析)

参考汤国安、杨昕等编著的《地理信息系统空间分析实验教程第二版》 利用水文分析(流量)和表面分析(坡向变率)得到山脊和山谷线 文章目录1、基本概念1.1 正地形1.2 负地形正负地形的影响2、水文分析方法2.1 基本思想原理2.2 技术路线2.3 操作步骤2.3.1 正负地形的提取2.3.2 利用水文分析思想和工具提取研究区域的山脊线2.3.3 利用水文分析思想和工具提取研究区域的山谷线2.3.4 利用水文分析提取山脊线和山谷线结果3、表面分析方法3.1 基本思想原理3.2 技术路线3.3 操作步骤4、图.

fungis 1万+

Linux打印内核函数调用栈(dump_stack)

在Linux内核中提供了一个可以打印出内核调用堆栈的函数 dump_stack() 该函数在我们调试内核的过程中可以打印出函数调用关系,该函数可以帮助我们进行内核调试,以及让我们了解内核的调用关系。 该函数头文件为: #include <asm/ptrace.h> 使用方式: 直接在想要查看的函数中添加 dump_stack(); 案例: 随便写了一个模块test.c,test.c代码如下: #include <linux/module.h> ...

一口Linux的专栏 1981

Dzzoffice结合OnlyOffice 报错排查流程总结

首先访问 OnlyOffice 首页,如下图:出现上图仍旧不能说明你的 OnlyOffice 服务已经成功安装,我们需要启动 OnlyOffice 服务测试用例来检测,可以看到上图出现了两条命令,第一条命令是用来启动测试用例服务的,我们只需要在后台执行该命令即可,Windows环境是以管理员身份运行Windows PowerShell输入即可运行了。

小胡的博客 4597

linux c 打印堆栈 函数名,用C或C ++打印调用堆栈

提升堆栈跟踪这是我到目前为止看到的最方便的选项,因为它:实际上可以打印出行号。addr2line然而,它只是调用,这很难看,如果你的痕迹太多,可能会很慢。demangles默认情况下Boost只是标题,因此最不需要修改构建系统main.cpp中#include#defineBOOST_STACKTRACE_USE_ADDR2LINE#includevoidmy_func_2(void){...

weixin_32642653的博客 811

GeoScene Pro教程(002):GeoScenePro基础操作

GeoScenePro基础操作介绍。

WwLK123的博客 2461

人群仿真软件:Pathfinder_(14).与其他软件的集成与互操作

在人群仿真软件的二次开发中,与其他软件的集成和互操作是非常重要的。通过数据导入导出、接口调用和脚本编写,可以显著提升仿真的准确性和实用性。Pathfinder提供了多种数据格式的支持和丰富的API,使得与其他软件的集成变得简单高效。无论是建筑模型的导入、仿真结果的导出,还是实时数据的交换,Pathfinder都能满足用户的需求。

kkchenjj的博客 1016

linux命令以及函数堆栈结构随记

关于查看当前窗口命令tty。

ethan18的博客 185

结构光之相移法+多频外差的数学原理推导

‍论文:基于数字光栅投影的结构光三维测量技术与系统研究 [博]作者:李中伟 华中科技大学 2007功能:相移法:多频外差(三频N步相移法)01 相移法图1 待写入投影仪的理想N步相移条纹通常对于 步相移来说,生成第 幅条纹图像(如图1所示)的公式如下:402 Payment Required其中::背景光强:调制强度:像素点横向坐标:相移值图2 相机拍摄到的实际 N ......

3D视觉工坊 1189

Linux打印函数调用栈

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 欢迎使用Markdown编辑器 你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Mar

u014158879的博客 997

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

在Linux应用程序中打印函数调用栈

在Linux中打印函数调用栈 要求 在Linux系统的应用程序中写一个函数print_stackframe(),用于获取当前位置的函数调用栈信息 方法 execinfo.h库下的函数backtrace可以得到当前线程的函数调用栈指针和调用栈深度,backtrace_symbols可以将调用栈指针转化为字符串信息。详见Backtraces或在Linux命令行运行命令man backtrace. 代码...

da_kao_la的博客 5204

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

【微知】Linux内核态和用户态如何打印函数的调用栈?( dump_stack)

Linux内核态和用户态打印函数调用栈的方法: 内核态:直接调用dump_stack()函数即可打印调用栈。 用户态:需使用backtrace相关功能实现,方法如下: 使用backtrace()获取调用栈地址数组 通过backtrace_symbols()转换地址为可读符号 打印调用栈信息 若符号解析不完整,可采用addr2line工具进行精确解析: 通过/proc/self/maps获取模块加载基址 计算相对地址(绝对地址-基址) 使用addr2line命令解析函数名和行号 输出格式化的调用栈信息 该方法

技术札记 578

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

【Linux】内核调用栈打印函数dump_stack使用效果

【代码】【Linux】内核调用栈打印函数dump_stack使用效果。

656

linux下打印函数调用栈(stack backtrace in Linux)

linux下打印函数调用栈(stack backtrace in Linux) 2010-01-14 23:20 今天遇到个这样的需求,希望能当动态链接库中抛出异常时,能把调用栈打印出来。经过在google上一翻搜寻,找到了一些线索。 我的文件目录如下:当前目录下是main.cpp 和test/ test下面是A.h, A.cpp(定

IT小小鸟 1206

在linux中打印函数调用栈

在linux中打印函数调用栈的方法如下。 头文件 #include Code: void *bt[30]; char **strings; size_t sz; sz = backtrace(bt, 30); strings = backtrace_symbols(bt, sz); for(int i = 0; i < sz; ++i) printf("%s\n", stri

Rubyer_Perler的专栏 779

linux 打印函数地址,linux下打印函数调用栈(stack backtrace in Linux)

今天遇到个这样的需求,希望能当动态链接库中抛出异常时,能把调用栈打印出来。经过在google上一翻搜寻,找到了一些线索。我的文件目录如下:当前目录下是main.cpp 和test/test下面是A.h, A.cpp(定义一个类)和backtrace.h, backtrace.cpp第一步,实现打印调用栈,backtrace函数实现如下:backtrace.cpp#include "backtrac...

weixin_42516967的博客 619

linux--打印函数调用栈

1、示例程序 #include <stdio.h> #include <stdlib.h> #include <execinfo.h> #include <unistd.h> int func1() { printf("1234"); #define SIZE 100 void *buffer[1...

u012967763的专栏 321

linux使用back trace打印函数调用栈,捕捉段错误

在大型项目开发过程中,经常遇到一些段错误问题或者查询是哪个函数调用了当前函数。通过代码排查手段太费时了,下面利用函数库打印出段错误时刻的函数调用栈,很容易找到代码异常之处。 在glibc头文件&amp;amp;quot;execinfo.h&amp;amp;quot;中声明了三个函数用于获取当前线程的函数调用堆栈 int backtrace(void **buffer,int size) 该函数用于获取当前线程的调用堆栈,获取的信息将会被存放...

轻飘飞扬 4788

linux打印函数调用栈,Linux调用backtrack函数打印程序崩溃时的调用堆栈

介绍几个关于C/C程序调试的函数 最近调试程序学到的几个挺有用的函数,分享一下,希望对用C/C的朋友有所帮助! 1. 调用栈系列 下面是函数原型: 1 2 3 4 #include execinfo .h int backtrace ( void ** buffer, int size ) ; char ** backtrace_symbols ( vo介绍几个关于C/C++程序调试的函数最近调试...

weixin_42515301的博客 396
上一篇: 程序调试之打印日志
server_df
博客等级 码龄12年 0粉丝 2原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值