From 283b1a4dfccc4723728ea613dc42e58ddc10fed2 Mon Sep 17 00:00:00 2001 From: Jiuzhu Dong Date: Thu, 20 Jan 2022 21:12:14 +0800 Subject: [PATCH] nshlib/cmd_memdump: support new command: memdump Signed-off-by: Jiuzhu Dong --- nshlib/nsh.h | 33 +++++++++++++++++++++++++ nshlib/nsh_command.c | 6 +++++ nshlib/nsh_fsutils.c | 59 ++++++++++++++++++++++++++++++++++++++++++++ nshlib/nsh_mmcmds.c | 25 +++++++++++++++++-- 4 files changed, 121 insertions(+), 2 deletions(-) diff --git a/nshlib/nsh.h b/nshlib/nsh.h index 16234af09..5efdc89e0 100644 --- a/nshlib/nsh.h +++ b/nshlib/nsh.h @@ -565,9 +565,15 @@ # define CONFIG_NSH_DISABLE_FREE 1 #endif +#if !defined(CONFIG_FS_PROCFS) || defined(CONFIG_FS_PROCFS_EXCLUDE_MEMDUMP) +# undef CONFIG_NSH_DISABLE_MEMDUMP +# define CONFIG_NSH_DISABLE_MEMDUMP 1 +#endif + /* Suppress unused file utilities */ #define NSH_HAVE_CATFILE 1 +#define NSH_HAVE_WRITEFILE 1 #define NSH_HAVE_READFILE 1 #define NSH_HAVE_FOREACH_DIRENTRY 1 #define NSH_HAVE_TRIMDIR 1 @@ -940,6 +946,9 @@ void nsh_usbtrace(void); #ifndef CONFIG_NSH_DISABLE_FREE int cmd_free(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); #endif +#ifndef CONFIG_NSH_DISABLE_MEMDUMP + int cmd_memdump(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); +#endif #ifndef CONFIG_NSH_DISABLE_TIME int cmd_time(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); #endif @@ -1294,6 +1303,30 @@ int nsh_readfile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, FAR const char *filepath, FAR char *buffer, size_t buflen); #endif +/**************************************************************************** + * Name: nsh_writefile + * + * Description: + * Dump the contents of a file to the current NSH terminal. + * + * Input Paratemets: + * vtbl - session vtbl + * cmd - NSH command name to use in error reporting + * buffer - The pointer of writting buffer + * len - The length of writting buffer + * filepath - The full path to the file to be dumped + * + * Returned Value: + * Zero (OK) on success; -1 (ERROR) on failure. + * + ****************************************************************************/ + +#ifdef NSH_HAVE_WRITEFILE +int nsh_writefile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, + FAR const char *buffer, size_t len, + FAR const char *filepath); +#endif + /**************************************************************************** * Name: nsh_foreach_direntry * diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c index ddee35f8f..e5e2fed4d 100644 --- a/nshlib/nsh_command.c +++ b/nshlib/nsh_command.c @@ -216,6 +216,12 @@ static const struct cmdmap_s g_cmdmap[] = { "free", cmd_free, 1, 1, NULL }, #endif +#ifdef CONFIG_DEBUG_MM +# ifndef CONFIG_NSH_DISABLE_MEMDUMP + { "memdump", cmd_memdump, 1, 3, "[pid/used/free]" }, +# endif +#endif + #ifdef CONFIG_NET_UDP # ifndef CONFIG_NSH_DISABLE_GET { "get", cmd_get, 4, 7, diff --git a/nshlib/nsh_fsutils.c b/nshlib/nsh_fsutils.c index 98fc334ed..65dc811c3 100644 --- a/nshlib/nsh_fsutils.c +++ b/nshlib/nsh_fsutils.c @@ -286,6 +286,65 @@ int nsh_readfile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, } #endif +/**************************************************************************** + * Name: nsh_writefile + * + * Description: + * Dump the contents of a file to the current NSH terminal. + * + * Input Paratemets: + * vtbl - session vtbl + * cmd - NSH command name to use in error reporting + * buffer - The pointer of writting buffer + * len - The length of writting buffer + * filepath - The full path to the file to be dumped + * + * Returned Value: + * Zero (OK) on success; -1 (ERROR) on failure. + * + ****************************************************************************/ + +#ifdef NSH_HAVE_WRITEFILE +int nsh_writefile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, + FAR const char *buffer, size_t len, + FAR const char *filepath) +{ + int fd; + int ret; + + /* Open the file for reading */ + + fd = open(filepath, O_WRONLY); + if (fd < 0) + { +#if defined(CONFIG_NSH_PROC_MOUNTPOINT) + if (strncmp(filepath, CONFIG_NSH_PROC_MOUNTPOINT, + strlen(CONFIG_NSH_PROC_MOUNTPOINT)) == 0) + { + nsh_error(vtbl, + "nsh: %s: Could not open %s (is procfs mounted?): %d\n", + cmd, filepath, NSH_ERRNO); + } + else +#endif + { + nsh_error(vtbl, g_fmtcmdfailed, cmd, "open", NSH_ERRNO); + } + + return ERROR; + } + + ret = write(fd, buffer, len); + if (ret < 0) + { + nsh_error(vtbl, g_fmtcmdfailed, cmd, "write", NSH_ERRNO); + } + + close(fd); + return ret > 0 ? OK : ERROR; +} +#endif + /**************************************************************************** * Name: nsh_foreach_direntry * diff --git a/nshlib/nsh_mmcmds.c b/nshlib/nsh_mmcmds.c index 898e127fd..2d8a1ac84 100644 --- a/nshlib/nsh_mmcmds.c +++ b/nshlib/nsh_mmcmds.c @@ -27,12 +27,12 @@ #include "nsh.h" #include "nsh_console.h" -#if !defined(CONFIG_NSH_DISABLE_FREE) && defined(NSH_HAVE_CATFILE) - /**************************************************************************** * Public Functions ****************************************************************************/ +#if !defined(CONFIG_NSH_DISABLE_FREE) && defined(NSH_HAVE_CATFILE) + /**************************************************************************** * Name: cmd_free ****************************************************************************/ @@ -43,3 +43,24 @@ int cmd_free(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) } #endif /* !CONFIG_NSH_DISABLE_FREE && NSH_HAVE_CATFILE */ + +#if !defined(CONFIG_NSH_DISABLE_MEMDUMP) && defined(NSH_HAVE_WRITEFILE) + +/**************************************************************************** + * Name: cmd_memdump + ****************************************************************************/ + +int cmd_memdump(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) +{ + FAR const char *arg = "used"; + + if (argc > 1) + { + arg = argv[1]; + } + + return nsh_writefile(vtbl, argv[0], arg, strlen(arg), + CONFIG_NSH_PROC_MOUNTPOINT "/memdump"); +} + +#endif /* !CONFIG_NSH_DISABLE_MEMDUMP && NSH_HAVE_WRITEFILE */