Android adb自身调试log开关

本文介绍下如何打开adb源码中的debug log

1.adb源码log是可以动态打开和关闭的,控制逻辑代码如下

复制代码
static NoDestructor<std::mutex> log_mutex;
static NoDestructor<CachedProperty> log_property GUARDED_BY(log_mutex)("debug.adbd.logging");
static std::optional<LogStatus> cached_log_status GUARDED_BY(log_mutex);

static NoDestructor<CachedProperty> persist_log_property
        GUARDED_BY(log_mutex)("persist.debug.adbd.logging");
static std::optional<LogStatus> cached_persist_log_status GUARDED_BY(log_mutex);

static LogStatus ParseLogStatus(std::string_view str) {
    LogStatus result = {};
    for (const auto& part : android::base::Split(std::string(str), ",")) {
        if (part == "cnxn") {
            result[adb::LogType::Connection] = true;
        } else if (part == "service") {
            result[adb::LogType::Service] = true;
        } else if (part == "shell") {
            result[adb::LogType::Shell] = true;
        } else if (part == "all") {
            result[adb::LogType::Connection] = true;
            result[adb::LogType::Service] = true;
            result[adb::LogType::Shell] = true;
        }
    }
    return result;
}

static LogStatus GetLogStatus(android::base::CachedProperty* property,
                              std::optional<LogStatus>* cached_status) REQUIRES(log_mutex) {
    bool changed;
    const char* value = property->Get(&changed);
    if (changed || !*cached_status) {
        **cached_status = ParseLogStatus(value);
    }
    return **cached_status;
}

namespace adb {
bool is_logging_enabled(LogType type) {
    std::lock_guard<std::mutex> lock(*log_mutex);
    return GetLogStatus(log_property.get(), &cached_log_status)[type] ||
           GetLogStatus(persist_log_property.get(), &cached_persist_log_status)[type];
}
}  // namespace adb

2.配置方法

adb shell setprop persist.debug.adbd.logging service //打开LogType为service的log

例如:

复制代码
daemon/services.cpp:262:    ADB_LOG(Service) << "transport " << transport->serial_name() << " opening service " << name;

打开后执行adb remount,可以抓到如下log

复制代码
I adbd    : transport UsbFfs opening service shell,v2,TERM=xterm-256color,raw:remount

adb shell setprop persist.debug.adbd.logging all //打开所有logtype

3.打开adb trace log方法

复制代码
adb shell setprop persist.adb.trace_mask 1
adb shell pkill adbd   //需要重启adbd 生效

trace log会保存在/data/adb/文件夹下

相关推荐
一航jason5 小时前
Android平台推理框架及试用场景模型对比
android·人工智能·ai·架构·ai编程·llama
一航jason5 小时前
Android 端侧大模型推理框架对比
android·人工智能·ai·ai编程·llama·ai-native
新时代牛马5 小时前
cyclictest 毛刺从哪来?从ftrace、latencytop、perf到IRQ/调度延迟定位
android·开发语言·python·kotlin
光电的一只菜鸡7 小时前
为什么调整LSC会对AF产生影响
android·开发语言·kotlin
凛_Lin~~7 小时前
LiveData 源码解析
android·安卓·livedata
hai_android8 小时前
FusibleFlow:Kotlin Flow 的融合机制原理
android·kotlin
龚寿生8 小时前
23-敏捷开发实战:从Scrum到看板的团队协作方法论
android·学习·scrum·敏捷流程
zhangguojia79 小时前
Activity启动流程(四):从setContentView到View树创建与Window挂载
android
三少爷的鞋12 小时前
Kotlin 2026:裁员、AI、Rust——黄金时代结束了吗?Jake Wharton 为你解答
android