CallStack获取函数堆栈

自定义CallStack实现:使用libunwind解析栈轨迹
这篇博客介绍了作者如何利用libunwind库创建自己的CallStack类,用于收集和记录调用栈信息。代码示例展示了从源码到实现的详细过程,包括更新栈信息、日志输出和转换为字符串等功能。此外,还提供了一个测试用例来展示CallStack的使用。

之前总看Android的源码感觉CallStack做的很不错,现在终于用户三方库libunwind做出了自己的CallStack,虽然代码不多,但也是自己写的

上代码
/*************************************************************************
    > File Name: callstack.h
    > Author: hsz
    > Mail:
    > Created Time: Tue 27 Jul 2021 06:02:23 PM CST
 ************************************************************************/

#ifndef __ALIAS_CALLSTACK_H__
#define __ALIAS_CALLSTACK_H__
#include <log/log.h>			// 自己写的log
#include <stdio.h>
#include <vector>
#include <utils/string8.h>		// 仿照Android String8,添加了移动构造和移动赋值

namespace Alias {
   
   

class CallStack {
   
   
public:
    CallStack();
    CallStack(const char* logtag, int32_t ignoreDepth = 1);
    ~CallStack();

    void clear() {
   
    mStackFrame.clear(); }

    // Immediately collect the stack traces for the specified thread.
    // The default is to dump the stack of the current call.
    void update(int32_t ignoreDepth = 2);

    void log(const char* logtag,
             LogLeval::Leval leval = LogLeval::DEBUG) const;

    // Return a string (possibly very long) containing the complete stack trace.
    String8 toString() const;

    // Get the count of stack frames that are in this call stack.
    size_t size() const {
   
    return mStackFrame.<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值