Skip to content

Commit 151cd1d

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

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

ctshell.c

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

extension/logger/log.c

Whitespace-only changes.

extension/logger/log.h

Whitespace-only changes.

0 commit comments

Comments
 (0)