之前总看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.<
自定义CallStack实现:使用libunwind解析栈轨迹

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

被折叠的 条评论
为什么被折叠?



