2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/nshlib/nsh_proccmds.c
|
|
|
|
*
|
2015-07-23 18:15:57 +02:00
|
|
|
* Copyright (C) 2007-2009, 2011-2012, 2014-2015 Gregory Nutt. All rights reserved.
|
2012-02-02 17:04:09 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2015-11-28 22:03:21 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2014-02-27 18:11:24 +01:00
|
|
|
#include <fcntl.h>
|
2015-11-28 22:03:21 +01:00
|
|
|
#include <dirent.h>
|
2011-03-18 20:46:25 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "nsh.h"
|
2012-02-02 17:04:09 +01:00
|
|
|
#include "nsh_console.h"
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2014-02-27 18:11:24 +01:00
|
|
|
* Pre-processor Definitions
|
2011-03-18 20:46:25 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-12-01 13:41:30 +01:00
|
|
|
#ifndef CONFIG_NSH_PROC_MOUNTPOINT
|
|
|
|
# define CONFIG_NSH_PROC_MOUNTPOINT "/proc"
|
2014-02-27 18:11:24 +01:00
|
|
|
#endif
|
|
|
|
|
2015-11-30 13:58:56 +01:00
|
|
|
/* See include/nuttx/sched.h: */
|
|
|
|
|
|
|
|
#undef HAVE_GROUPID
|
|
|
|
|
|
|
|
#if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS)
|
|
|
|
# define HAVE_GROUPID 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_DISABLE_PTHREAD
|
|
|
|
# undef HAVE_GROUPID
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-27 18:11:24 +01:00
|
|
|
/* The returned value should be zero for success or TRUE or non zero for
|
2011-03-18 20:46:25 +01:00
|
|
|
* failure or FALSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef int (*exec_t)(void);
|
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
/* This structure represents the parsed task characteristics */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
struct nsh_taskstatus_s
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2015-11-28 22:03:21 +01:00
|
|
|
FAR const char *td_type; /* Thread type */
|
2015-11-30 13:58:56 +01:00
|
|
|
#ifdef CONFIG_SCHED_HAVE_PARENT
|
|
|
|
#ifdef HAVE_GROUPID
|
|
|
|
FAR const char *td_groupid; /* Group ID */
|
|
|
|
#else
|
2016-02-19 22:33:32 +01:00
|
|
|
FAR const char *td_ppid; /* Parent thread ID */
|
2015-11-30 13:58:56 +01:00
|
|
|
#endif
|
2016-02-19 22:33:32 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
FAR const char *td_cpu; /* CPU */
|
2015-11-30 13:58:56 +01:00
|
|
|
#endif
|
2015-11-28 22:03:21 +01:00
|
|
|
FAR const char *td_state; /* Thread state */
|
|
|
|
FAR const char *td_event; /* Thread wait event */
|
|
|
|
FAR const char *td_flags; /* Thread flags */
|
|
|
|
FAR const char *td_priority; /* Thread priority */
|
|
|
|
FAR const char *td_policy; /* Thread scheduler */
|
|
|
|
FAR const char *td_sigmask; /* Signal mask */
|
2011-03-18 20:46:25 +01:00
|
|
|
};
|
2011-04-07 03:54:59 +02:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
/* Status strings */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2017-03-07 22:10:17 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_PS
|
2015-11-28 22:03:21 +01:00
|
|
|
#if 0 /* Not used */
|
|
|
|
static const char g_name[] = "Name:";
|
|
|
|
#endif
|
2017-03-07 22:10:17 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
static const char g_type[] = "Type:";
|
2017-03-07 22:10:17 +01:00
|
|
|
|
2015-11-30 13:58:56 +01:00
|
|
|
#ifdef CONFIG_SCHED_HAVE_PARENT
|
|
|
|
#ifdef HAVE_GROUPID
|
|
|
|
static const char g_groupid[] = "Group:";
|
|
|
|
#else
|
|
|
|
static const char g_ppid[] = "PPID:";
|
|
|
|
#endif
|
2017-03-07 22:10:17 +01:00
|
|
|
#endif /* CONFIG_SCHED_HAVE_PARENT */
|
|
|
|
|
2016-02-19 22:33:32 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
static const char g_cpu[] = "CPU:";
|
|
|
|
#endif
|
2017-03-07 22:10:17 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
static const char g_state[] = "State:";
|
|
|
|
static const char g_flags[] = "Flags:";
|
|
|
|
static const char g_priority[] = "Priority:";
|
|
|
|
static const char g_scheduler[] = "Scheduler:";
|
|
|
|
static const char g_sigmask[] = "SigMask:";
|
2015-07-23 18:15:57 +02:00
|
|
|
|
2016-07-01 21:45:00 +02:00
|
|
|
#if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE)
|
2016-06-30 15:28:42 +02:00
|
|
|
static const char g_stacksize[] = "StackSize:";
|
2016-07-13 15:06:32 +02:00
|
|
|
#ifdef CONFIG_STACK_COLORATION
|
2016-06-30 15:28:42 +02:00
|
|
|
static const char g_stackused[] = "StackUsed:";
|
|
|
|
#endif
|
2017-03-07 22:10:17 +01:00
|
|
|
#endif /* !CONFIG_NSH_DISABLE_PSSTACKUSAGE */
|
|
|
|
#endif /* !CONFIG_NSH_DISABLE_PS */
|
2016-06-30 15:28:42 +02:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-27 18:11:24 +01:00
|
|
|
/****************************************************************************
|
2016-06-30 15:28:42 +02:00
|
|
|
* Name: nsh_parse_statusline
|
2014-02-27 18:11:24 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_PS
|
|
|
|
static void nsh_parse_statusline(FAR char *line,
|
|
|
|
FAR struct nsh_taskstatus_s *status)
|
2014-02-27 18:11:24 +01:00
|
|
|
{
|
2015-11-28 22:03:21 +01:00
|
|
|
/* Parse the task status.
|
|
|
|
*
|
|
|
|
* Format:
|
|
|
|
*
|
|
|
|
* 111111111122222222223
|
|
|
|
* 123456789012345678901234567890
|
|
|
|
* Name: xxxx... Task/thread name (See CONFIG_TASK_NAME_SIZE)
|
|
|
|
* Type: xxxxxxx {Task, pthread, Kthread, Invalid}
|
2015-11-30 13:58:56 +01:00
|
|
|
* Type: xxxxxxx {Task, pthread, Kthread, Invalid}
|
|
|
|
* PPID: xxxxx Parent thread ID
|
2015-11-28 22:03:21 +01:00
|
|
|
* State: xxxxxxxx,xxxxxxxxx {Invalid, Waiting, Ready, Running, Inactive},
|
|
|
|
* {Unlock, Semaphore, Signal, MQ empty, MQ full}
|
|
|
|
* Flags: xxx N,P,X
|
|
|
|
* Priority: nnn Decimal, 0-255
|
|
|
|
* Scheduler: xxxxxxxxxxxxxx {SCHED_FIFO, SCHED_RR, SCHED_SPORADIC, SCHED_OTHER}
|
|
|
|
* Sigmask: nnnnnnnn Hexadecimal, 32-bit
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if 0 /* Not used */
|
|
|
|
/* Task name */
|
|
|
|
|
|
|
|
if (strncmp(line, g_name, strlen(g_name)) == 0)
|
|
|
|
{
|
|
|
|
/* Not used */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
/* Task/thread type */
|
2014-02-27 18:11:24 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
if (strncmp(line, g_type, strlen(g_type)) == 0)
|
|
|
|
{
|
|
|
|
/* Save the thread type */
|
2014-02-27 18:11:24 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
status->td_type = nsh_trimspaces(&line[12]);
|
|
|
|
}
|
2015-11-30 13:58:56 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_HAVE_PARENT
|
|
|
|
#ifdef HAVE_GROUPID
|
|
|
|
else if (strncmp(line, g_groupid, strlen(g_groupid)) == 0)
|
|
|
|
{
|
|
|
|
/* Save the Group ID */
|
|
|
|
|
|
|
|
status->td_groupid = nsh_trimspaces(&line[12]);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
else if (strncmp(line, g_ppid, strlen(g_ppid)) == 0)
|
|
|
|
{
|
|
|
|
/* Save the parent thread id */
|
|
|
|
|
|
|
|
status->td_ppid = nsh_trimspaces(&line[12]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2016-02-19 22:33:32 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
else if (strncmp(line, g_cpu, strlen(g_cpu)) == 0)
|
|
|
|
{
|
|
|
|
/* Save the current CPU */
|
|
|
|
|
|
|
|
status->td_cpu = nsh_trimspaces(&line[12]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
else if (strncmp(line, g_state, strlen(g_state)) == 0)
|
|
|
|
{
|
|
|
|
FAR char *ptr;
|
2014-02-27 18:11:24 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
/* Save the thread state */
|
2014-02-27 18:11:24 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
status->td_state = nsh_trimspaces(&line[12]);
|
|
|
|
|
|
|
|
/* Check if an event follows the state */
|
|
|
|
|
|
|
|
ptr = strchr(status->td_state, ',');
|
|
|
|
if (ptr != NULL)
|
|
|
|
{
|
|
|
|
*ptr++ = '\0';
|
|
|
|
status->td_event = nsh_trimspaces(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strncmp(line, g_flags, strlen(g_flags)) == 0)
|
|
|
|
{
|
|
|
|
status->td_flags = nsh_trimspaces(&line[12]);
|
|
|
|
}
|
|
|
|
else if (strncmp(line, g_priority, strlen(g_priority)) == 0)
|
|
|
|
{
|
2016-07-15 16:31:47 +02:00
|
|
|
FAR char *ptr = nsh_trimspaces(&line[12]);
|
|
|
|
status->td_priority = ptr;
|
2016-07-15 15:01:02 +02:00
|
|
|
|
|
|
|
/* If priority inheritance is enabled, use current pri, ignore base */
|
|
|
|
|
|
|
|
while (isdigit(*ptr))
|
|
|
|
{
|
|
|
|
++ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ptr = '\0';
|
2015-11-28 22:03:21 +01:00
|
|
|
}
|
|
|
|
else if (strncmp(line, g_scheduler, strlen(g_scheduler)) == 0)
|
|
|
|
{
|
|
|
|
/* Skip over the SCHED_ part of the policy. Resultis max 8 bytes */
|
|
|
|
|
|
|
|
status->td_policy = nsh_trimspaces(&line[12+6]);
|
|
|
|
}
|
|
|
|
else if (strncmp(line, g_sigmask, strlen(g_sigmask)) == 0)
|
|
|
|
{
|
|
|
|
status->td_sigmask = nsh_trimspaces(&line[12]);
|
|
|
|
}
|
2014-02-27 18:11:24 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
2015-11-28 15:29:27 +01:00
|
|
|
* Name: ps_callback
|
2011-03-18 20:46:25 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_PS
|
2015-11-28 22:03:21 +01:00
|
|
|
static int ps_callback(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath,
|
|
|
|
FAR struct dirent *entryp, FAR void *pvarg)
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2015-11-28 22:03:21 +01:00
|
|
|
struct nsh_taskstatus_s status;
|
|
|
|
FAR char *filepath;
|
|
|
|
FAR char *line;
|
|
|
|
FAR char *nextline;
|
2014-02-27 18:11:24 +01:00
|
|
|
int ret;
|
2011-03-18 20:46:25 +01:00
|
|
|
int i;
|
2016-07-01 21:45:00 +02:00
|
|
|
#if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE)
|
2016-07-12 14:48:30 +02:00
|
|
|
uint32_t stack_size;
|
2016-07-13 15:06:32 +02:00
|
|
|
#ifdef CONFIG_STACK_COLORATION
|
2016-07-12 14:48:30 +02:00
|
|
|
uint32_t stack_used;
|
|
|
|
uint32_t stack_filled;
|
2016-07-13 15:06:32 +02:00
|
|
|
#endif
|
2016-06-30 15:28:42 +02:00
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2016-06-30 15:28:42 +02:00
|
|
|
/* Task/thread entries in the /proc directory will all be (1) directories
|
|
|
|
* with (2) all numeric names.
|
2015-11-28 22:03:21 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!DIRENT_ISDIRECTORY(entryp->d_type))
|
|
|
|
{
|
|
|
|
/* Not a directory... skip this entry */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
/* Check each character in the name */
|
2014-02-27 18:11:24 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
for (i = 0; i < NAME_MAX && entryp->d_name[i] != '\0'; i++)
|
2014-02-27 18:11:24 +01:00
|
|
|
{
|
2015-11-28 22:03:21 +01:00
|
|
|
if (!isdigit(entryp->d_name[i]))
|
|
|
|
{
|
|
|
|
/* Name contains something other than a numeric character */
|
2014-02-27 18:11:24 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2014-02-27 18:11:24 +01:00
|
|
|
}
|
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
/* Set all pointers to the empty string. */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
status.td_type = "";
|
2015-11-30 13:58:56 +01:00
|
|
|
#ifdef CONFIG_SCHED_HAVE_PARENT
|
|
|
|
#ifdef HAVE_GROUPID
|
|
|
|
status.td_groupid = "";
|
|
|
|
#else
|
|
|
|
status.td_ppid = "";
|
|
|
|
#endif
|
2016-02-19 22:33:32 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
status.td_cpu = "";
|
2015-11-30 13:58:56 +01:00
|
|
|
#endif
|
2015-11-28 22:03:21 +01:00
|
|
|
status.td_state = "";
|
|
|
|
status.td_event = "";
|
|
|
|
status.td_flags = "";
|
|
|
|
status.td_priority = "";
|
|
|
|
status.td_policy = "";
|
|
|
|
status.td_sigmask = "";
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
/* Read the task status */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
filepath = NULL;
|
|
|
|
ret = asprintf(&filepath, "%s/%s/status", dirpath, entryp->d_name);
|
|
|
|
if (ret < 0 || filepath == NULL)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, "ps", "asprintf", NSH_ERRNO);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2013-02-08 01:17:54 +01:00
|
|
|
else
|
|
|
|
{
|
2015-11-28 22:03:21 +01:00
|
|
|
ret = nsh_readfile(vtbl, "ps", filepath, vtbl->iobuffer, IOBUFFERSIZE);
|
|
|
|
free(filepath);
|
2013-02-08 01:17:54 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
if (ret >= 0)
|
2013-02-08 01:17:54 +01:00
|
|
|
{
|
2015-11-28 22:03:21 +01:00
|
|
|
/* Parse the task status. */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
nextline = vtbl->iobuffer;
|
|
|
|
do
|
2014-09-02 00:47:20 +02:00
|
|
|
{
|
2015-11-28 22:03:21 +01:00
|
|
|
/* Find the beginning of the next line and NUL-terminat the
|
|
|
|
* current line.
|
|
|
|
*/
|
|
|
|
|
|
|
|
line = nextline;
|
|
|
|
for (nextline++;
|
|
|
|
*nextline != '\n' && *nextline != '\0';
|
|
|
|
nextline++);
|
|
|
|
|
|
|
|
if (*nextline == '\n')
|
|
|
|
{
|
|
|
|
*nextline++ = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nextline = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the current line */
|
|
|
|
|
|
|
|
nsh_parse_statusline(line, &status);
|
2014-09-02 00:47:20 +02:00
|
|
|
}
|
2015-11-28 22:03:21 +01:00
|
|
|
while (nextline != NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally, print the status information */
|
|
|
|
|
2016-01-17 14:39:33 +01:00
|
|
|
nsh_output(vtbl, "%5s ", entryp->d_name);
|
2015-11-30 13:58:56 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_HAVE_PARENT
|
|
|
|
#ifdef HAVE_GROUPID
|
2016-01-17 14:39:33 +01:00
|
|
|
nsh_output(vtbl, "%5s ", status.td_groupid);
|
2015-11-30 13:58:56 +01:00
|
|
|
#else
|
2016-01-17 14:39:33 +01:00
|
|
|
nsh_output(vtbl, "%5s ", status.td_ppid);
|
2015-11-30 13:58:56 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2016-02-19 22:33:32 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
nsh_output(vtbl, "%3s ", status.td_cpu);
|
|
|
|
#endif
|
|
|
|
|
2016-01-17 14:39:33 +01:00
|
|
|
nsh_output(vtbl, "%3s %-8s %-7s %3s %-8s %-9s ",
|
2015-11-30 13:58:56 +01:00
|
|
|
status.td_priority, status.td_policy, status.td_type,
|
|
|
|
status.td_flags, status.td_state, status.td_event);
|
2016-01-17 14:39:33 +01:00
|
|
|
nsh_output(vtbl, "%-8s ", status.td_sigmask);
|
2015-11-28 22:03:21 +01:00
|
|
|
|
2016-07-01 21:45:00 +02:00
|
|
|
#if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE)
|
2016-06-30 15:28:42 +02:00
|
|
|
/* Get the StackSize and StackUsed */
|
|
|
|
|
|
|
|
stack_size = 0;
|
2016-07-13 15:06:32 +02:00
|
|
|
#ifdef CONFIG_STACK_COLORATION
|
2016-06-30 15:28:42 +02:00
|
|
|
stack_used = 0;
|
2016-07-13 15:06:32 +02:00
|
|
|
#endif
|
2016-06-30 15:28:42 +02:00
|
|
|
filepath = NULL;
|
|
|
|
|
|
|
|
ret = asprintf(&filepath, "%s/%s/stack", dirpath, entryp->d_name);
|
|
|
|
if (ret < 0 || filepath == NULL)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, "ps", "asprintf", NSH_ERRNO);
|
2016-06-30 15:28:42 +02:00
|
|
|
vtbl->iobuffer[0] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-12 14:48:30 +02:00
|
|
|
ret = nsh_readfile(vtbl, "ps", filepath, vtbl->iobuffer,
|
|
|
|
IOBUFFERSIZE);
|
2016-06-30 15:28:42 +02:00
|
|
|
free(filepath);
|
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
nextline = vtbl->iobuffer;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Find the beginning of the next line and NUL-terminate the
|
|
|
|
* current line.
|
|
|
|
*/
|
|
|
|
|
|
|
|
line = nextline;
|
|
|
|
for (nextline++;
|
|
|
|
*nextline != '\n' && *nextline != '\0';
|
|
|
|
nextline++);
|
|
|
|
|
|
|
|
if (*nextline == '\n')
|
|
|
|
{
|
|
|
|
*nextline++ = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nextline = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the current line
|
|
|
|
*
|
|
|
|
* Format:
|
|
|
|
*
|
|
|
|
* 111111111122222222223
|
|
|
|
* 123456789012345678901234567890
|
|
|
|
* StackBase: xxxxxxxxxx
|
|
|
|
* StackSize: xxxx
|
|
|
|
* StackUsed: xxxx
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (strncmp(line, g_stacksize, strlen(g_stacksize)) == 0)
|
|
|
|
{
|
2016-07-12 14:48:30 +02:00
|
|
|
stack_size = (uint32_t)atoi(&line[12]);
|
2016-06-30 15:28:42 +02:00
|
|
|
}
|
2016-07-13 15:06:32 +02:00
|
|
|
#ifdef CONFIG_STACK_COLORATION
|
2016-06-30 15:28:42 +02:00
|
|
|
else if (strncmp(line, g_stackused, strlen(g_stackused)) == 0)
|
|
|
|
{
|
2016-07-12 14:48:30 +02:00
|
|
|
stack_used = (uint32_t)atoi(&line[12]);
|
2016-06-30 15:28:42 +02:00
|
|
|
}
|
2016-07-13 15:06:32 +02:00
|
|
|
#endif
|
2016-06-30 15:28:42 +02:00
|
|
|
}
|
|
|
|
while (nextline != NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 09:39:51 +01:00
|
|
|
nsh_output(vtbl, "%06u ", (unsigned int)stack_size);
|
2016-07-13 15:06:32 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_STACK_COLORATION
|
2017-03-21 09:39:51 +01:00
|
|
|
nsh_output(vtbl, "%06u ", (unsigned int)stack_used);
|
2016-06-30 15:28:42 +02:00
|
|
|
|
2016-07-12 14:48:30 +02:00
|
|
|
stack_filled = 0;
|
|
|
|
if (stack_size > 0 && stack_used > 0)
|
2016-06-30 15:28:42 +02:00
|
|
|
{
|
2016-07-12 14:48:30 +02:00
|
|
|
/* Use fixed-point math with one decimal place */
|
|
|
|
|
|
|
|
stack_filled = 10 * 100 * stack_used / stack_size;
|
2016-06-30 15:28:42 +02:00
|
|
|
}
|
|
|
|
|
2016-07-15 15:01:02 +02:00
|
|
|
/* Additionally print a '!' if the stack is filled more than 80% */
|
2016-06-30 15:28:42 +02:00
|
|
|
|
2016-07-15 15:01:02 +02:00
|
|
|
nsh_output(vtbl, "%3d.%1d%%%c ",
|
2016-07-12 14:48:30 +02:00
|
|
|
stack_filled / 10, stack_filled % 10,
|
2016-07-15 15:01:02 +02:00
|
|
|
(stack_filled >= 10 * 80 ? '!' : ' '));
|
2016-07-11 01:49:58 +02:00
|
|
|
#endif
|
2016-07-13 15:06:32 +02:00
|
|
|
#endif
|
2016-07-11 01:49:58 +02:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
#ifdef NSH_HAVE_CPULOAD
|
|
|
|
/* Get the CPU load */
|
|
|
|
|
2016-01-17 14:39:33 +01:00
|
|
|
filepath = NULL;
|
2015-11-28 22:03:21 +01:00
|
|
|
ret = asprintf(&filepath, "%s/%s/loadavg", dirpath, entryp->d_name);
|
|
|
|
if (ret < 0 || filepath == NULL)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, "ps", "asprintf", NSH_ERRNO);
|
2015-11-28 22:03:21 +01:00
|
|
|
vtbl->iobuffer[0] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = nsh_readfile(vtbl, "ps", filepath, vtbl->iobuffer, IOBUFFERSIZE);
|
|
|
|
free(filepath);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
vtbl->iobuffer[0] = '\0';
|
2014-09-02 00:47:20 +02:00
|
|
|
}
|
2013-02-08 01:17:54 +01:00
|
|
|
}
|
|
|
|
|
2016-01-17 14:39:33 +01:00
|
|
|
nsh_output(vtbl, "%6s ", nsh_trimspaces(vtbl->iobuffer));
|
2015-11-28 22:03:21 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Read the task/tread command line */
|
|
|
|
|
|
|
|
filepath = NULL;
|
|
|
|
ret = asprintf(&filepath, "%s/%s/cmdline", dirpath, entryp->d_name);
|
|
|
|
|
|
|
|
if (ret < 0 || filepath == NULL)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, "ps", "asprintf", NSH_ERRNO);
|
2015-11-28 22:03:21 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = nsh_readfile(vtbl, "ps", filepath, vtbl->iobuffer, IOBUFFERSIZE);
|
|
|
|
free(filepath);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2016-01-17 14:39:33 +01:00
|
|
|
nsh_output(vtbl, "%s\n", nsh_trimspaces(vtbl->iobuffer));
|
2015-11-28 22:03:21 +01:00
|
|
|
return OK;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_exec
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_EXEC
|
2015-11-28 15:29:27 +01:00
|
|
|
int cmd_exec(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2015-11-28 15:29:27 +01:00
|
|
|
FAR char *endptr;
|
2011-06-17 20:20:14 +02:00
|
|
|
uintptr_t addr;
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2011-06-17 20:20:14 +02:00
|
|
|
addr = (uintptr_t)strtol(argv[1], &endptr, 0);
|
2011-03-18 20:46:25 +01:00
|
|
|
if (!addr || endptr == argv[1] || *endptr != '\0')
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2011-03-18 20:46:25 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2016-01-17 14:39:33 +01:00
|
|
|
nsh_output(vtbl, "Calling %p\n", (exec_t)addr);
|
2011-03-18 20:46:25 +01:00
|
|
|
return ((exec_t)addr)();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_ps
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_PS
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_ps(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
2016-01-17 14:39:33 +01:00
|
|
|
nsh_output(vtbl, "%5s ", "PID");
|
2015-11-30 13:58:56 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_HAVE_PARENT
|
|
|
|
#ifdef HAVE_GROUPID
|
2016-01-17 14:39:33 +01:00
|
|
|
nsh_output(vtbl, "%5s ", "GROUP");
|
2015-11-30 13:58:56 +01:00
|
|
|
#else
|
2016-01-17 14:39:33 +01:00
|
|
|
nsh_output(vtbl, "%5s ", "PPID");
|
2015-11-30 13:58:56 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2016-02-19 22:33:32 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
nsh_output(vtbl, "%3s ", "CPU");
|
|
|
|
#endif
|
|
|
|
|
2016-01-17 14:39:33 +01:00
|
|
|
nsh_output(vtbl, "%3s %-8s %-7s %3s %-8s %-9s ",
|
2015-11-30 13:58:56 +01:00
|
|
|
"PRI", "POLICY", "TYPE", "NPX", "STATE", "EVENT");
|
2016-01-17 14:39:33 +01:00
|
|
|
nsh_output(vtbl, "%-8s ", "SIGMASK");
|
2016-07-13 15:06:32 +02:00
|
|
|
|
2016-07-01 21:45:00 +02:00
|
|
|
#if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE)
|
2016-06-30 15:28:42 +02:00
|
|
|
nsh_output(vtbl, "%6s ", "STACK");
|
2016-07-13 15:06:32 +02:00
|
|
|
#ifdef CONFIG_STACK_COLORATION
|
2016-06-30 15:28:42 +02:00
|
|
|
nsh_output(vtbl, "%6s ", "USED");
|
|
|
|
nsh_output(vtbl, "%7s ", "FILLED");
|
|
|
|
#endif
|
2016-07-13 15:06:32 +02:00
|
|
|
#endif
|
|
|
|
|
2015-11-28 14:23:08 +01:00
|
|
|
#ifdef NSH_HAVE_CPULOAD
|
2016-01-17 14:39:33 +01:00
|
|
|
nsh_output(vtbl, "%6s ", "CPU");
|
2014-02-27 18:11:24 +01:00
|
|
|
#endif
|
2016-01-17 14:39:33 +01:00
|
|
|
nsh_output(vtbl, "%s\n", "COMMAND");
|
2015-11-28 22:03:21 +01:00
|
|
|
|
|
|
|
return nsh_foreach_direntry(vtbl, "ps", CONFIG_NSH_PROC_MOUNTPOINT,
|
|
|
|
ps_callback, NULL);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_kill
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_KILL
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_kill(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
char *endptr;
|
|
|
|
long signal;
|
|
|
|
long pid;
|
|
|
|
|
|
|
|
/* Check incoming parameters. The first parameter should be "-<signal>" */
|
|
|
|
|
|
|
|
ptr = argv[1];
|
|
|
|
if (*ptr != '-' || ptr[1] < '0' || ptr[1] > '9')
|
|
|
|
{
|
|
|
|
goto invalid_arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract the signal number */
|
|
|
|
|
|
|
|
signal = strtol(&ptr[1], &endptr, 0);
|
|
|
|
|
|
|
|
/* The second parameter should be <pid> */
|
|
|
|
|
|
|
|
ptr = argv[2];
|
|
|
|
if (*ptr < '0' || *ptr > '9')
|
|
|
|
{
|
|
|
|
goto invalid_arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract athe pid */
|
|
|
|
|
|
|
|
pid = strtol(ptr, &endptr, 0);
|
|
|
|
|
|
|
|
/* Send the signal. Kill return values:
|
|
|
|
*
|
|
|
|
* EINVAL An invalid signal was specified.
|
|
|
|
* EPERM The process does not have permission to send the signal to any
|
|
|
|
* of the target processes.
|
|
|
|
* ESRCH The pid or process group does not exist.
|
|
|
|
* ENOSYS Do not support sending signals to process groups.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (kill((pid_t)pid, (int)signal) == 0)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EINVAL:
|
|
|
|
goto invalid_arg;
|
|
|
|
|
|
|
|
case ESRCH:
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtnosuch, argv[0], "task", argv[2]);
|
2011-03-18 20:46:25 +01:00
|
|
|
return ERROR;
|
|
|
|
|
|
|
|
case EPERM:
|
|
|
|
case ENOSYS:
|
|
|
|
default:
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "kill", NSH_ERRNO);
|
2011-03-18 20:46:25 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
invalid_arg:
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2011-03-18 20:46:25 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_sleep
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_SLEEP
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_sleep(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
long secs;
|
|
|
|
|
|
|
|
secs = strtol(argv[1], &endptr, 0);
|
|
|
|
if (!secs || endptr == argv[1] || *endptr != '\0')
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2011-03-18 20:46:25 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
2014-08-10 21:11:31 +02:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
sleep(secs);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_usleep
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_USLEEP
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_usleep(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
long usecs;
|
|
|
|
|
|
|
|
usecs = strtol(argv[1], &endptr, 0);
|
|
|
|
if (!usecs || endptr == argv[1] || *endptr != '\0')
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2011-03-18 20:46:25 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
2016-06-30 15:28:42 +02:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
usleep(usecs);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|