prife / goadb

A Golang library for interacting with adb. fork from https://github.com/zach-klippenstein/goadb
Apache License 2.0
8 stars 1 forks source link

support logcat binary #9

Open prife opened 1 month ago

prife commented 1 month ago
image

reference: https://cs.android.com/android/platform/superproject/main/+/main:system/logging/liblog/include/log/log_read.h

前面1C个字符(28个)是header,结构如下

struct logger_entry { uint16_t len; / length of the payload / uint16_t hdr_size; / sizeof(struct logger_entry) / int32_t pid; / generating process's pid / uint32_t tid; / generating process's tid / uint32_t sec; / seconds since Epoch / uint32_t nsec; / nanoseconds / uint32_t lid; / log id of the payload, bottom 4 bits currently / uint32_t uid; / generating process's uid / };

随后是1个字节的loglevel。接下来是payload,长度为header的前4个字节表示。

在这篇官方的协议文档中进一步做了说明。

prife commented 1 month ago

根据这段注释:https://cs.android.com/android/platform/superproject/main/+/main:system/logging/liblog/include/log/log_read.h

/*
 * Native log reading interface section. See logcat for sample code.
 *
 * The preferred API is an exec of logcat. Likely uses of this interface
 * are if native code suffers from exec or filtration being too costly,
 * access to raw information, or parsing is an issue.
 */
/*
 * 本地日志读取接口部分。请参阅 logcat 以获取示例代码。
 *
 * 首选的 API 是执行 logcat。使用此接口的可能情况包括:
 * 如果本地代码在执行或过滤时开销过大,
 * 需要访问原始信息,
 * 或者解析存在问题。
 */

解析logcat日志的应该使用binary格式,也就是logcat -B方式来捕获,默认的文本模式解析成本较高(costly)

prife commented 1 month ago

logcat events分析

Android 11之后,改动一些事件的命名,将am的部分event移动到wm下,详见下面这篇文章 https://blog.csdn.net/chi_wy/article/details/114639207

logcat-bin.zip

Android5.1在logcat -b events -B时,格式输出有错误,每次都会多一个字符,所以这带来了两个问题:

  1. 在-B模式,混用-b events -b main等,可能带来格式混乱的问题。
  2. 仅-b events -B二进制格式详情