好记性不如铅笔头

C && C++, 编程

工具代码笔记:快速dump内存

以前玩mbedtls的时候,经常会通过dump报文的方式来调试问题。最近也遇到了一些协议调试的需求,就顺手简单的写了一个dump内存的代码,实现比较简单,这里简单的笔记下:

#define DUMP_ASCII_CONVERT(value)  (((value >= 0x20)&&(value <= 0x7E)) ? value:'.')

int32_t dump_buffer_content(char *p_name, uint8_t *p_buffer, uint32_t buffer_len)
{
	__OUTPUT_RAW("++ Buffer:%s Len:%lu  Begin ++\r\n", p_name, buffer_len);
	uint32_t index = 0;
	for (index = 0; index < buffer_len / 16; ++index)
	{
		__OUTPUT_RAW("0x%02lX:\t %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X "
				"| %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c\r\n",
				index * 16,
				(p_buffer[index*16+0]),  (p_buffer[index*16+1]),  (p_buffer[index*16+2]),  (p_buffer[index*16+3]),
				(p_buffer[index*16+4]),  (p_buffer[index*16+5]),  (p_buffer[index*16+6]),  (p_buffer[index*16+7]),
				(p_buffer[index*16+8]),  (p_buffer[index*16+9]),  (p_buffer[index*16+10]), (p_buffer[index*16+11]),
				(p_buffer[index*16+12]), (p_buffer[index*16+13]), (p_buffer[index*16+14]), (p_buffer[index*16+15]),
				DUMP_ASCII_CONVERT(p_buffer[index*16+0]),  DUMP_ASCII_CONVERT(p_buffer[index*16+1]),  DUMP_ASCII_CONVERT(p_buffer[index*16+2]),  DUMP_ASCII_CONVERT(p_buffer[index*16+3]),
				DUMP_ASCII_CONVERT(p_buffer[index*16+4]),  DUMP_ASCII_CONVERT(p_buffer[index*16+5]),  DUMP_ASCII_CONVERT(p_buffer[index*16+6]),  DUMP_ASCII_CONVERT(p_buffer[index*16+7]),
				DUMP_ASCII_CONVERT(p_buffer[index*16+8]),  DUMP_ASCII_CONVERT(p_buffer[index*16+9]),  DUMP_ASCII_CONVERT(p_buffer[index*16+10]), DUMP_ASCII_CONVERT(p_buffer[index*16+11]),
				DUMP_ASCII_CONVERT(p_buffer[index*16+12]), DUMP_ASCII_CONVERT(p_buffer[index*16+13]), DUMP_ASCII_CONVERT(p_buffer[index*16+14]), DUMP_ASCII_CONVERT(p_buffer[index*16+15]) );
	}
	index = index * 16;

	if(index != buffer_len)
	{
		uint8_t tmp_buf[16] = {0};
		memset(tmp_buf, '*', 16);
		memcpy(tmp_buf, p_buffer + index, buffer_len - index);
		__OUTPUT_RAW("0x%02lX:\t %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X "
				"| %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c\r\n",
				index,
				(tmp_buf[0]), (tmp_buf[1]), (tmp_buf[2]),  (tmp_buf[3]),  (tmp_buf[4]),  (tmp_buf[5]),  (tmp_buf[6]),  (tmp_buf[7]),
				(tmp_buf[8]), (tmp_buf[9]), (tmp_buf[10]), (tmp_buf[11]), (tmp_buf[12]), (tmp_buf[13]), (tmp_buf[14]), (tmp_buf[15]),
				DUMP_ASCII_CONVERT(tmp_buf[0]), DUMP_ASCII_CONVERT(tmp_buf[1]), DUMP_ASCII_CONVERT(tmp_buf[2]),  DUMP_ASCII_CONVERT(tmp_buf[3]),  DUMP_ASCII_CONVERT(tmp_buf[4]),  DUMP_ASCII_CONVERT(tmp_buf[5]),  DUMP_ASCII_CONVERT(tmp_buf[6]),  DUMP_ASCII_CONVERT(tmp_buf[7]),
				DUMP_ASCII_CONVERT(tmp_buf[8]), DUMP_ASCII_CONVERT(tmp_buf[9]), DUMP_ASCII_CONVERT(tmp_buf[10]), DUMP_ASCII_CONVERT(tmp_buf[11]), DUMP_ASCII_CONVERT(tmp_buf[12]), DUMP_ASCII_CONVERT(tmp_buf[13]), DUMP_ASCII_CONVERT(tmp_buf[14]), DUMP_ASCII_CONVERT(tmp_buf[15]) );

	}

	__OUTPUT_RAW("-- Buffer:%s Len:%lu  End --\r\n", p_name, buffer_len);

	return 0;
}

 

Leave a Reply

1 × 2 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据