📄 cmddebug.c
字号:
#include <stdio.h>#include <string.h>#include <ctype.h>#include <network.h>#include <command.h>static void memcpy_usage(void){ usage_format("memcpy dest src len", "copy to sdram from flash or sdram"); return;}static bool do_memcpy(int argc, char **argv){ bool res; ulong dest, src, len; if (argc != 4) goto invalid; res = strtoul(argv[1], &dest, 16); if (!res) goto invalid; res = strtoul(argv[2], &src, 16); if (!res) goto invalid; res = strtoul(argv[3], &len, 16); if (!res) goto invalid; memcpy((char *)dest, (char *)src, len); return true;invalid : memcpy_usage(); return false;}struct command_t cmd_memcpy = { .name = "memcpy", .run = do_memcpy, .usage = memcpy_usage};static void memcmp_usage(void){ usage_format("memcmp addr1 addr2 len", "compare data in addr1 and addr2"); return;}static bool do_memcmp(int argc, char **argv){ bool res; ulong len, tmp; uint8 *p1, *p2; if (argc < 4) return false; res = strtoul(argv[1], &tmp, 16); if (!res) goto invalid; p1 = (void *)tmp; res = strtoul(argv[2], &tmp, 16); if (!res) goto invalid; p2 = (void *)tmp; res = strtoul(argv[3], &tmp, 16); if (!res) goto invalid; len = tmp; if (len) do { if (*p1++ != *p2++) goto diff; } while (--len); return true;invalid : memcmp_usage(); return false;diff : p1--; p2--; printf("diff at addr1:%p=value:0x%02x, addr2:%p=value:0x%02x.\n", p1, *p1, p2, *p2); return false;}struct command_t cmd_memcmp = { .name = "memcmp", .run = do_memcmp, .usage = memcmp_usage};static void memset_usage(void){ usage_format("memset addr value len", "fill memory with value"); return;}static bool do_memset(int argc, char **argv){ bool res; ulong value, size, tmp; void *addr; if (argc < 4) return false; res = strtoul(argv[1], &tmp, 16); if (!res) goto invalid; addr = (void *)tmp; res = strtoul(argv[2], &tmp, 16); if (!res) goto invalid; value = tmp; res = strtoul(argv[3], &tmp, 16); if (!res) goto invalid; size = tmp; memset(addr, value, size); return true;invalid : memset_usage(); return false;}struct command_t cmd_memset = { .name = "memset", .run = do_memset, .usage = memset_usage};static void read_usage(void){ usage_format("read {c/s/l} addr", "read in addr. c:8 s:16 l:32 bits"); return;}static bool do_read(int argc, char **argv){ int type; bool res; ulong tmp; void *addr; if (argc < 3) goto invalid; res = strtoul(argv[2], &tmp, 16); if (!res) goto invalid; addr = (void *)tmp; type = argv[1][0]; printf(" read : address=0x%08lX, value=0x", (long)addr); if (type == 'c'){ printf("%02X", *(uint8 *)addr); } else if (type == 's'){ printf("%04X", *(uint16 *)addr); } else if (type == 'l'){ printf("%08lX", *(uint32 *)addr); } else goto invalid; printf(".\n"); return true;invalid : read_usage(); return false;}struct command_t cmd_read = { .name = "read", .run = do_read, .usage = read_usage};static void write_usage(void){ usage_format("write {c/s/l} addr value", "write in addr. c:8 s:16 l:32 bits."); return;}static bool do_write(int argc, char **argv){ int type; bool res; void *addr; ulong value, tmp; if (argc < 4) goto invalid; type = argv[1][0]; if (!strchr("csl", type)) goto invalid; res = strtoul(argv[2], &tmp, 16); if (!res) goto invalid; addr = (void *)tmp; res = strtoul(argv[3], &tmp, 16); if (!res) goto invalid; value = tmp; if (type == 'c') *(uint8 *)addr = value; else if (type == 's') *(uint16 *)addr = value; else if (type == 'l') *(uint32 *)addr = value; return true;invalid : write_usage(); return false;}struct command_t cmd_write = { .name = "write", .run = do_write, .usage = write_usage};static void hexdump_usage(void){ usage_format("hexdump addr len", "dump memory"); return;}extern void hexdump(void *addr, int len);static bool do_hexdump(int argc, char **argv){ bool res; void *addr; ulong size, tmp; if (argc < 3) goto invalid; res = strtoul(argv[1], &tmp, 16); if (!res) goto invalid; addr = (void *)tmp; res = strtoul(argv[2], &tmp, 16); if (!res) goto invalid; size = tmp; hexdump(addr, size); return true;invalid : hexdump_usage(); return false;}struct command_t cmd_hexdump = { .name = "hexdump", .run = do_hexdump, .usage = hexdump_usage};#define block_size 16extern void hexdump(void *addr, int len){ int i, nblock, wcnt; unsigned char *s; printf("\nOffset Hex Value Ascii value\n"); s = addr; nblock = 0; while (len > 0){ printf("0x%08lX ", (unsigned long)(block_size * nblock++)); wcnt = (len > block_size) ? block_size : len; for (i=0; i < wcnt; i++){ printf("%02X", s[i]); if ((i+1) % 4 == 0) printf(" "); } for ( ; i < block_size; i++){ printf(" "); if ((i+1) % 4 == 0) printf(" "); } printf(" "); // Ascii 蔼 免仿. for (i=0; i < wcnt; i++) printf("%c", (!(s[i] & 0x80) && isprint(s[i])) ? s[i] : '.'); printf("\n"); s += wcnt; len -= wcnt; } return;}static void ping_message(uint32 dip, uint16 seq){ printf(" from %s : icmp_seq=%d\n", inet_ntoa(dip), seq); return;}static void ping_usage(void){ usage_format("ping ipaddr", "ping test"); return;}static bool do_ping(int argc, char **argv){ uint32 ip; if (argc != 2) goto invalid; ip = inet_addr(argv[1]); if (!ip) goto invalid; ping(ip, ping_message); return true;invalid : ping_usage(); return false;}struct command_t cmd_ping = { .name = "ping", .run = do_ping, .usage = ping_usage};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -