驱动调试中,很多时候是二进制的,这个时候hexdump就是个非常有用的工具了。
不要再自己去实现类似的功能,kernel代码里面就有:
参考: kernel/lib/hexdump.c
// 0Xxx转换成 ASCII ,实现简单巧妙

const char hex_asc[] = "0123456789abcdef";
#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
int groupsize, char *linebuf, size_t linebuflen,
bool ascii)
{
const u8 *ptr = buf;
u8 ch;
int j, lx = 0;
int ascii_column;
// 每行固定16或者32个字节,符合国际审美观
if (rowsize != 16 && rowsize != 32)
rowsize = 16; if (!len)
goto nil;
if (len > rowsize) /* limit to one line at a time */
len = rowsize;
if ((len % groupsize) != 0) /* no mixed size output */
groupsize = 1; switch (groupsize) {
......
default:
// 转换成ASCii 形式
for (j = 0; (j < len) && (lx + 3) <= linebuflen; j++) {
ch = ptr[j];
linebuf[lx++] = hex_asc_hi(ch);
linebuf[lx++] = hex_asc_lo(ch);
linebuf[lx++] = ' ';
}
if (j)
lx--; ascii_column = 3 * rowsize + 2;
break;
}
if (!ascii)
goto nil;
// 加上对应的ASCII字符串,区别如下:
// 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
// 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
while (lx < (linebuflen - 1) && lx < (ascii_column - 1))
linebuf[lx++] = ' ';
for (j = 0; (j < len) && (lx + 2) < linebuflen; j++) {
ch = ptr[j];
linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
}
nil:
// 记得加上结束符号,有可能导致printk --> kernel panic
linebuf[lx++] = '\0';
}
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;
/* 每行数据类似下面
40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
| | | |
-----------------------------------------------
16*3 or 32*3 2 32+1
*/
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;
// linebuf 返回需要打印的字符串
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;
}
}
}
// 一般在kernel代码中,调用此接口,许多参数用默认的,不需要了解太多细节
void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
const void *buf, size_t len)
{
print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, 16, 1,
buf, len, true);
}

kernel hexdump分析的更多相关文章

  1. kernel hexdump分析 (2.0)

    有的时候print_hex_dump_bytes循环打印很多信息的时候(大于1K) 用UART串口打印的信息总是会有丢失,估计是printk缓冲区的问题把,具体原因不是太清楚 于是自己写了个比较笨的d ...

  2. u-boot bootz 加载kernel 流程分析

    u-boot 加载 kernel 的流程分析. image重要结构体头文件 // include/image.h * * Legacy and FIT format headers used by d ...

  3. Minix2.0操作系统kernel文件分析

    详细分析  MINIX消息机制的核心: mpx386.s start.c proc.c 保护模式分析: src/kernel/exception.c src/kernel/protect.c src/ ...

  4. Linux kernel kfifo分析【转】

    转自:https://zohead.com/archives/linux-kernel-kfifo/ 本文同步自(如浏览不正常请点击跳转):https://zohead.com/archives/li ...

  5. Linux Kernel PANIC(三)--Soft Panic/Oops调试及实例分析【转】

    转自:https://blog.csdn.net/gatieme/article/details/73715860 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原 ...

  6. linux 代码分析工具 gprof - 以wpa_supplicant为例

        当我们遇到一个新的程序的时候,经常会无从下手,需要debug一个功能的时候,我们不知道函数的运行流程是怎么样的,这就需要借助工具来帮助我们加快流程了.这里以分析wpa_supplicant为例 ...

  7. MIT JOS学习笔记03:kernel 02(2016.11.08)

    未经许可谢绝以任何形式对本文内容进行转载! 本篇接着上一篇对kernel的分析. (5)pte_t * pgdir_walk(pde_t *pgdir, const void *va, int cre ...

  8. MIT JOS学习笔记02:kernel 01(2016.10.28)

    未经许可谢绝以任何形式对本文内容进行转载! 在文章开头不得不说的是,因为这部分的代码需要仔细理清的东西太多,所以导致这篇分析显得很啰嗦,还请谅解. 我们在上一篇文章已经分析了Boot Loader的功 ...

  9. 【UVALive 7334】Kernel Knights

    题 题意 有两个队的骑士1到n和n+1到2n,每个骑士只能互相攻击对手队的一个骑士.kernel的意思是在这个kernel里的骑士不会互相攻击,在kernel外的骑士被kernel里的骑士攻击. 现在 ...

随机推荐

  1. ssh "openssh-daemon is stopped"操作之伤+sftp访问“-bash: /dev/null: Permission denied”

    1.ssh "openssh-daemon is stopped"操作之伤 原来好好的 sshd服务突然发现  不能重新启动了: [root@bogon rules.d]# ser ...

  2. 翻页采用jaxa

    <!-- 翻页采用jaxa --><script type="text/javascript">//class="page"下面的a被点 ...

  3. <转> 30 个有关 Python 的小技巧

    目录[+] 1.1 拆箱 1.2 拆箱变量交换 1.3 扩展拆箱(只兼容python3) 1.4 负数索引 1.5 切割列表 1.6 负数索引切割列表 1.7指定步长切割列表 1.8 负数步长切割列表 ...

  4. Week2(9月16日):动手做个简单的例子

    Part I:提问  =========================== 1.什么是ASP.NET MVC? 2.MVC的英文? 3.什么是模型? 4.什么是控制器? 5.什么是视图? 6.ASP ...

  5. “快的打车”创始人陈伟星的新项目招人啦,高薪急招Java服务端/Android/Ios 客户端研发工程师/ mysql DBA/ app市场推广专家,欢迎大家加入我们的团队! - V2EX

    "快的打车"创始人陈伟星的新项目招人啦,高薪急招Java服务端/Android/Ios 客户端研发工程师/ mysql DBA/ app市场推广专家,欢迎大家加入我们的团队! - ...

  6. C# 窗体在线2,8,16进制转换以及,在线更新时间

    class Program { static void Main(string[] args) { //十进制转二进制 Console.WriteLine(, )); //十进制转八进制 Consol ...

  7. jfinal集成spring cxf做webservice服务

    链接地址:http://zhengshuo.iteye.com/blog/2154047 废话不说,直接上代码 新增cxf的plugin CXFPlugin package com.jfinal.pl ...

  8. 我的Python成长之路---第一天---Python基础(2)---2015年12月26日(雾霾)

    三.数据类型 Python基本类型(能够直接处理的数据类型有以下几种)主要有5种 1.整数(int) Python可以处理任意大小的整数,当然包括负整数,在程序中的表示方法和数学上的写法一模一样,例如 ...

  9. ubuntu下配置qt+opengl+opencv

    原地址:http://www.cnblogs.com/aleny-liu/archive/2011/12/16/aleny-Qtnote1.html http://blog.csdn.net/jdh9 ...

  10. QtSoap调用Web Service(QtSoap是非官方应用)

    今天学习如何用QtSoap访问Web Service服务.这里调用的是查询QQ在线状态的服务qqOnlineWebService.调用的几个步骤: 1.创建QtSoapMessage对象 messag ...