nshlib/cmd_memdump: support new command: memdump
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
This commit is contained in:
parent
372621ed06
commit
283b1a4dfc
33
nshlib/nsh.h
33
nshlib/nsh.h
@ -565,9 +565,15 @@
|
|||||||
# define CONFIG_NSH_DISABLE_FREE 1
|
# define CONFIG_NSH_DISABLE_FREE 1
|
||||||
#endif
|
#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 */
|
/* Suppress unused file utilities */
|
||||||
|
|
||||||
#define NSH_HAVE_CATFILE 1
|
#define NSH_HAVE_CATFILE 1
|
||||||
|
#define NSH_HAVE_WRITEFILE 1
|
||||||
#define NSH_HAVE_READFILE 1
|
#define NSH_HAVE_READFILE 1
|
||||||
#define NSH_HAVE_FOREACH_DIRENTRY 1
|
#define NSH_HAVE_FOREACH_DIRENTRY 1
|
||||||
#define NSH_HAVE_TRIMDIR 1
|
#define NSH_HAVE_TRIMDIR 1
|
||||||
@ -940,6 +946,9 @@ void nsh_usbtrace(void);
|
|||||||
#ifndef CONFIG_NSH_DISABLE_FREE
|
#ifndef CONFIG_NSH_DISABLE_FREE
|
||||||
int cmd_free(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
|
int cmd_free(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
|
||||||
#endif
|
#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
|
#ifndef CONFIG_NSH_DISABLE_TIME
|
||||||
int cmd_time(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
|
int cmd_time(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
|
||||||
#endif
|
#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);
|
FAR const char *filepath, FAR char *buffer, size_t buflen);
|
||||||
#endif
|
#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
|
* Name: nsh_foreach_direntry
|
||||||
*
|
*
|
||||||
|
@ -216,6 +216,12 @@ static const struct cmdmap_s g_cmdmap[] =
|
|||||||
{ "free", cmd_free, 1, 1, NULL },
|
{ "free", cmd_free, 1, 1, NULL },
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef CONFIG_DEBUG_MM
|
||||||
|
# ifndef CONFIG_NSH_DISABLE_MEMDUMP
|
||||||
|
{ "memdump", cmd_memdump, 1, 3, "[pid/used/free]" },
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_NET_UDP
|
#ifdef CONFIG_NET_UDP
|
||||||
# ifndef CONFIG_NSH_DISABLE_GET
|
# ifndef CONFIG_NSH_DISABLE_GET
|
||||||
{ "get", cmd_get, 4, 7,
|
{ "get", cmd_get, 4, 7,
|
||||||
|
@ -286,6 +286,65 @@ int nsh_readfile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
|
|||||||
}
|
}
|
||||||
#endif
|
#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
|
* Name: nsh_foreach_direntry
|
||||||
*
|
*
|
||||||
|
@ -27,12 +27,12 @@
|
|||||||
#include "nsh.h"
|
#include "nsh.h"
|
||||||
#include "nsh_console.h"
|
#include "nsh_console.h"
|
||||||
|
|
||||||
#if !defined(CONFIG_NSH_DISABLE_FREE) && defined(NSH_HAVE_CATFILE)
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Functions
|
* Public Functions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
#if !defined(CONFIG_NSH_DISABLE_FREE) && defined(NSH_HAVE_CATFILE)
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: cmd_free
|
* 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 */
|
#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 */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user