Skip to content

Commit fd91c80

Browse files
committed
feat(cmd): add hexdump command for memory inspection
1 parent 193b7ab commit fd91c80

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

ctshell.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,59 @@ static int cmd_unset(int argc, char *argv[]) {
887887
}
888888
CTSHELL_EXPORT_CMD(unset, cmd_unset, "Unset a variable", CTSHELL_ATTR_NONE);
889889

890+
static int cmd_hexdump(int argc, char *argv[]) {
891+
if (argc != 3) {
892+
ctshell_printf("Usage: hexdump <base> <len>\r\n");
893+
return 0;
894+
}
895+
896+
char *end_base = NULL;
897+
char *end_len = NULL;
898+
uintptr_t base = (uintptr_t) strtoull(argv[1], &end_base, 0);
899+
unsigned long length = strtoul(argv[2], &end_len, 0);
900+
901+
if (!argv[1][0] || !argv[2][0] || *end_base != '\0' || *end_len != '\0') {
902+
ctshell_printf("hexdump: invalid base or length\r\n");
903+
return 0;
904+
}
905+
if (length == 0) {
906+
return 0;
907+
}
908+
909+
const unsigned char *mem = (const unsigned char *) base;
910+
uintptr_t aligned_base = base & ~(uintptr_t) 0x0F;
911+
uintptr_t data_end = base + (uintptr_t) length;
912+
uintptr_t aligned_end = (data_end + 15U) & ~(uintptr_t) 0x0F;
913+
914+
for (uintptr_t addr = aligned_base; addr < aligned_end; addr += 16U) {
915+
ctshell_printf("%08lX ", (unsigned long) addr);
916+
for (size_t i = 0; i < 16; i++) {
917+
uintptr_t cur = addr + i;
918+
if (cur < base || cur >= data_end) {
919+
ctshell_printf(" ");
920+
} else {
921+
ctshell_printf("%02X ", mem[cur - base]);
922+
}
923+
if (i == 7) {
924+
ctshell_printf(" ");
925+
}
926+
}
927+
ctshell_printf(" |");
928+
for (size_t i = 0; i < 16; i++) {
929+
uintptr_t cur = addr + i;
930+
if (cur < base || cur >= data_end) {
931+
ctshell_printf(" ");
932+
} else {
933+
unsigned char ch = mem[cur - base];
934+
ctshell_printf("%c", isprint(ch) ? ch : '.');
935+
}
936+
}
937+
ctshell_printf("|\r\n");
938+
}
939+
return 0;
940+
}
941+
CTSHELL_EXPORT_CMD(hexdump, cmd_hexdump, "Display memory content in hex format", CTSHELL_ATTR_NONE);
942+
890943
#ifdef CONFIG_CTSHELL_USE_FS
891944
static int cmd_ls(int argc, char *argv[]) {
892945
CHECK_FS_READY();

docs/en/api/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ Ctshell provides the following built-in commands:
458458
* Usage: ``set [NAME] [VALUE]``
459459
5. **unset**: Delete an environment variable.
460460
* Usage: ``unset [NAME]``
461+
6. **hexdump**: Display memory contents in hexadecimal and ASCII format.
462+
* Usage: ``hexdump <base> <len>``
461463

462464
If file system support is enabled, the following built-in commands are available:
463465

docs/zh/api/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ Ctshell 提供以下内置命令:
458458
* 用法: ``set [NAME] [VALUE]``
459459
5. **unset**: 删除环境变量。
460460
* 用法: ``unset [NAME]``
461+
6. **hexdump**: 十六进制转储。
462+
* 用法: ``hexdump <base> <len>``
461463

462464
若开启文件系统支持,则下面内置命令可用:
463465

0 commit comments

Comments
 (0)