You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/02/23 18:19:20 UTC

[incubator-nuttx-apps] 02/02: nshlib/cmd_memdump: support new command: memdump

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git

commit 283b1a4dfccc4723728ea613dc42e58ddc10fed2
Author: Jiuzhu Dong <do...@xiaomi.com>
AuthorDate: Thu Jan 20 21:12:14 2022 +0800

    nshlib/cmd_memdump: support new command: memdump
    
    Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
 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 16234af..5efdc89 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
@@ -1295,6 +1304,30 @@ 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);
+#endif
+
+/****************************************************************************
  * Name: nsh_foreach_direntry
  *
  * Description:
diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c
index ddee35f..e5e2fed 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 98fc334..65dc811 100644
--- a/nshlib/nsh_fsutils.c
+++ b/nshlib/nsh_fsutils.c
@@ -287,6 +287,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
  *
  * Description:
diff --git a/nshlib/nsh_mmcmds.c b/nshlib/nsh_mmcmds.c
index 898e127..2d8a1ac 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 */