您好,欢迎来到微智科技网。
搜索
您的当前位置:首页print_hex_dump用法

print_hex_dump用法

来源:微智科技网

lib/hexdump.c 

/**
 * print_hex_dump - print a text hex dump to syslog for a binary blob of data
 * @level: kernel log level (e.g. KERN_DEBUG)
 * @prefix_str: string to prefix each line with;
 *  caller supplies trailing spaces for alignment if desired
 * @prefix_type: controls whether prefix of an offset, address, or none
 *  is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
 * @rowsize: number of bytes to print per line; must be 16 or 32
 * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
 * @buf: data blob to dump
 * @len: number of bytes in the @buf
 * @ascii: include ASCII after the hex output
 *
 * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
 * to the kernel log at the specified kernel log level, with an optional
 * leading prefix.
 *
 * print_hex_dump() works on one "line" of output at a time, i.e.,
 * 16 or 32 bytes of input data converted to hex + ASCII output.
 * print_hex_dump() iterates over the entire input @buf, breaking it into
 * "line size" chunks to format and print.
 *
 * E.g.:
 *   print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS,
 *		    16, 1, frame->data, frame->len, true);
 *
 * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
 * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
 * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
 * ffffffff880af0: 73727170 77767574 7b7a7978 7f7e7d7c  pqrstuvwxyz{|}~.
 */
void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
		    int rowsize, int groupsize,
		    const void *buf, size_t len, bool ascii)
{
	const u8 *ptr = buf;
	int i, linelen, remaining = len;
	unsigned char linebuf[32 * 3 + 2 + 32 + 1];

	if (rowsize != 16 && rowsize != 32)
		rowsize = 16;

	for (i = 0; i < len; i += rowsize) {
		linelen = min(remaining, rowsize);
		remaining -= rowsize;

		hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
				   linebuf, sizeof(linebuf), ascii);

		switch (prefix_type) {
		case DUMP_PREFIX_ADDRESS:
			printk("%s%s%p: %s\n",
			       level, prefix_str, ptr + i, linebuf);
			break;
		case DUMP_PREFIX_OFFSET:
			printk("%s%s%.8x: %s\n", level, prefix_str, i, linebuf);
			break;
		default:
			printk("%s%s%s\n", level, prefix_str, linebuf);
			break;
		}
	}
}
EXPORT_SYMBOL(print_hex_dump);

先看demo 

char buf[]={0x0,0x1,0x2,0x3,0x04,0x05,0x06,0x07,0x8,0x09,0xa,0xb,0xc,0x0d,0x0e,0x0f,0x10,0x11};
pr_info("111111\n");
print_hex_dump(KERN_DEBUG,"=====",DUMP_PREFIX_ADDRESS,16,1,buf,sizeof(buf),1);
print_hex_dump(KERN_DEBUG,"=====",DUMP_PREFIX_ADDRESS,16,2,buf,sizeof(buf),1);
print_hex_dump(KERN_DEBUG,"=====",DUMP_PREFIX_ADDRESS,16,4,buf,sizeof(buf),1);
print_hex_dump(KERN_DEBUG,"=====",DUMP_PREFIX_ADDRESS,16,8,buf,sizeof(buf),1);
pr_info("222222\n");
print_hex_dump(KERN_DEBUG,"=====",DUMP_PREFIX_ADDRESS,16,1,buf,sizeof(buf),0);
print_hex_dump(KERN_DEBUG,"=====",DUMP_PREFIX_ADDRESS,16,1,buf,sizeof(buf),1);
pr_info("333333\n");
print_hex_dump(KERN_DEBUG,"=====",DUMP_PREFIX_ADDRESS,16,1,buf,sizeof(buf),1);
print_hex_dump(KERN_DEBUG,"=====",DUMP_PREFIX_OFFSET,16,1,buf,sizeof(buf),1);
print_hex_dump(KERN_DEBUG,"=====",DUMP_PREFIX_NONE,16,1,buf,sizeof(buf),1);

打印信息 

[10274.3381] 111111
[10274.3391] =====00000000a2aa0bf4: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f  ................
[10274.3397] =====00000000ee9d42b6: 10 11                                            ..
[10274.3402] =====00000000a2aa0bf4: 0100 0302 0504 0706 0908 0b0a 0d0c 0f0e  ................
[10274.3405] =====00000000ee9d42b6: 1110                                     ..
[10274.3409] =====00000000a2aa0bf4: 03020100 07060504 0b0a0908 0f0e0d0c  ................
[10274.3412] =====00000000ee9d42b6: 10 11                                            ..
[10274.3415] =====00000000a2aa0bf4: 0706050403020100 0f0e0d0c0b0a0908  ................
[10274.3418] =====00000000ee9d42b6: 10 11                                            ..
[10274.3420] 222222
[10274.3422] =====00000000a2aa0bf4: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
[10274.3425] =====00000000ee9d42b6: 10 11
[10274.3427] =====00000000a2aa0bf4: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f  ................
[10274.3430] =====00000000ee9d42b6: 10 11                                            ..
[10274.3432] 333333
[10274.3434] =====00000000a2aa0bf4: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f  ................
[10274.3436] =====00000000ee9d42b6: 10 11                                            ..
[10274.3439] =====00000000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f  ................
[10274.3442] =====00000010: 10 11                                            ..
[10274.3445] =====00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f  ................
[10274.3447] =====10 11

 最简单的形式用这个

print_hex_dump(KERN_DEBUG,"=====",DUMP_PREFIX_NONE,16,1,buf,sizeof(buf),0);

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 7swz.com 版权所有 赣ICP备2024042798号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务