Skip to content

Commit 5468897

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

7 files changed

Lines changed: 67 additions & 5 deletions

File tree

.github/workflows/fuzzing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- name: Configure fuzz target
2525
run: |
2626
cmake -S . -B build-fuzz -G Ninja \
27-
-DCTSHELL_ENABLE_FUZZING=ON \
27+
-DCONFIG_CTSHELL_ENABLE_FUZZING=ON \
2828
-DCMAKE_C_COMPILER=clang
2929
3030
- name: Build fuzz target

CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ set(ctshell_incs
88

99
set(CTSHELL_DEFINITIONS "")
1010

11-
option(CTSHELL_ENABLE_FUZZING "Enable libFuzzer based fuzz testing" OFF)
11+
option(CONFIG_CTSHELL_ENABLE_FUZZING "Enable libFuzzer based fuzz testing" OFF)
1212

1313
if(ESP_PLATFORM)
1414
set(CONFIG_CTSHELL_PORT_ESP32 1)
@@ -81,9 +81,8 @@ if(ESP_PLATFORM)
8181

8282
target_link_libraries(${COMPONENT_LIB} INTERFACE "-T ${CMAKE_CURRENT_SOURCE_DIR}/port/esp32/ctshell_cmd.ld")
8383
else()
84-
set(CTSHELL_DEFINITIONS ${CTSHELL_DEFINITIONS} CACHE INTERNAL "ctshell definitions")
85-
86-
if(CTSHELL_ENABLE_FUZZING)
84+
if(CONFIG_CTSHELL_ENABLE_FUZZING)
85+
list(APPEND CTSHELL_DEFINITIONS "CONFIG_CTSHELL_ENABLE_FUZZING=1")
8786
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
8887
set(CTSHELL_FUZZ_TARGET ctshell_fuzz)
8988
set(CTSHELL_FUZZ_SRC "${CMAKE_CURRENT_SOURCE_DIR}/test/fuzz/ctshell_fuzz.c")
@@ -110,4 +109,6 @@ else()
110109
message(FATAL_ERROR "Fuzzing requires Clang compiler. Run with: CC=clang cmake ..")
111110
endif()
112111
endif()
112+
113+
set(CTSHELL_DEFINITIONS ${CTSHELL_DEFINITIONS} CACHE INTERNAL "ctshell definitions")
113114
endif()

ctshell.c

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