parse printk log
参考:http://www.coder4.com/archives/3838
https://docs.python.org/2/library/struct.html#module-struct
struct printk_log {
u64 ts_nsec; /* timestamp in nanoseconds */
u16 len; /* length of entire record */
u16 text_len; /* length of text buffer */
u16 dict_len; /* length of dictionary buffer */
u8 facility; /* syslog facility */
u8 flags:5; /* internal record flags */
u8 level:3; /* syslog level */
};
static void log_store
python struct pack & unpack
- 大小端 & 对齐
The byte order character '=' chooses to use little- or big-endian ordering based on the host system.
Use sys.byteorder to check the endianness of your system
为了同c中的结构体交换数据,还要考虑有的c或c++编译器使用了字节对齐,通常是以4个字节为单位的32位系统,故而struct根据本地机器字节顺序转换.可以用格式中的第一个字符来改变对齐方式.定义如下:
| CHARACTER | BYTE ORDER | SIZE | ALIGNMENT |
|---|---|---|---|
| @ | native | native | native |
| = | native | standard | none |
| < | little-endian | standard | none |
| > | big-endian | standard | none |
| ! | network (= big-endian) | standard | none |
- A format character may be preceded by an integral repeat count. For example, the format string '4h' means exactly the same as 'hhhh'.
- For the 's' format character, the count is interpreted as the size of the string, not a repeat count like for the other format characters; for example,'10s' means a single 10-byte string, while '10c' means 10 characters.
- Whitespace characters between formats are ignored; a count and its format must not contain whitespace though.
struct 类型表
| Format | C Type | Python type | Standard size |
|---|---|---|---|
| x | pad byte | no value | |
| c | char | string of length 1 | 1 |
| b | signed char | integer | 1 |
| B | unsigned char | integer | 1 |
| ? | _Bool | bool | 1 |
| h | short | integer | 2 |
| H | unsigned short | integer | 2 |
| i | int | integer | 4 |
| I | unsigned int | integer | 4 |
| l | long | integer | 4 |
| L | unsigned long | integer | 4 |
| q | long long | integer | 8 |
| Q | unsigned long long | integer | 8 |
| f | float | float | 4 |
| d | double | float | 8 |
| s | char[] | string | 1 |
| p | char[] | string | |
| P | void * | integer |
- struct.unpack(fmt, string)
Unpack the string (presumably packed by pack(fmt, ...)) according to the given format. The result is a tuple even if it contains exactly one item. The string must contain exactly the amount of data required by the format (len(string) must equal calcsize(fmt)).
- struct.pack(fmt, v1, v2, ...)
Return a string containing the values v1, v2, ... packed according to the given format. The arguments must match the values required by the format exactly.
此份脚本参考自公司同事的文件,故不予显示。解析脚本(平台小端、主机小端): 返回上一级 | 全部文件 > ... > Python > scripts > parse_logbuffer
使用该脚本解析的前提是:被解析文件自始至终都符合 printk_log 结构。
由于log_buffer是由数组构成的环形队列,那么就存在如何保证“被解析文件自始至终都符合 printk_log 结构”,我采用的方法:
vim file ----> %!xxd 由此可以看出第一个可以被解析结构的位置。 wc -c file -----> 得到文件字节数 file_len xxd -l file_len -c file_len -p file > temp_file_one --------> 得到文件中存放的十六进制数 删除 temp_file_one 中开头出不能被解析的结构,得到文件 temp_file_two xxd -p -r temp_file_two > file_could_be_used_to_parse
注意:除了 %!xxd 是在 vim 中执行之外,其余的命令不要放在 vim 中执行,原因未知(我多次尝试在vim中执行 xxd 相关命令,但是得到的文件总是有问题,不知为何。)
如果觉得要把 file 中末尾的字符去掉也可以,但是由于该脚本每解析完一个结构就写入到文件,所以就算文件末尾结构不对,也勿用担心。
浙公网安备 33010602011771号