aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Grafton <john.grafton@gmail.com>2021-06-16 19:40:21 +0000
committerWarner Losh <imp@FreeBSD.org>2021-09-13 01:08:08 +0000
commit5eca96eee3f48c0659175f78f9630b59636c2970 (patch)
treef8fe90e55d0eece7f9af828c351006779459b4e3
parent7a26124388ef3ce76ad7b9d8c45d20f222d73a41 (diff)
downloadsrc-5eca96eee3f48c0659175f78f9630b59636c2970.tar.gz
src-5eca96eee3f48c0659175f78f9630b59636c2970.zip
top(1): support command name and argument grepping
Obtained from: OpenBSD Reviewed by: imp@ Pull Request: https://github.com/freebsd/freebsd-src/pull/479 (cherry picked from commit a00d703f2f438b199d3933d19d535540586b7792)
-rw-r--r--usr.bin/top/commands.c1
-rw-r--r--usr.bin/top/commands.h1
-rw-r--r--usr.bin/top/machine.c35
-rw-r--r--usr.bin/top/machine.h2
-rw-r--r--usr.bin/top/top.15
-rw-r--r--usr.bin/top/top.c15
-rw-r--r--usr.bin/top/top.h2
7 files changed, 60 insertions, 1 deletions
diff --git a/usr.bin/top/commands.c b/usr.bin/top/commands.c
index 88f4b0867d47..9efc693020ba 100644
--- a/usr.bin/top/commands.c
+++ b/usr.bin/top/commands.c
@@ -59,6 +59,7 @@ const struct command all_commands[] =
{'H', "toggle the displaying of threads", false, CMD_thrtog},
{'h', "show this help text", true, CMD_help},
{'?', NULL, true, CMD_help},
+ {'/', "filter on command name (+ selects all commands)", false, CMD_grep},
{'i', "toggle the displaying of idle processes", false, CMD_idletog},
{'I', NULL, false, CMD_idletog},
{'j', "toggle the displaying of jail ID", false, CMD_jidtog},
diff --git a/usr.bin/top/commands.h b/usr.bin/top/commands.h
index 0071fbe62fc6..863effd2ddd9 100644
--- a/usr.bin/top/commands.h
+++ b/usr.bin/top/commands.h
@@ -24,6 +24,7 @@ enum cmd_id {
CMD_update,
CMD_quit,
CMD_help,
+ CMD_grep,
CMD_errors,
CMD_number,
CMD_delay,
diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c
index 1a2c608f3045..6116a06bb28b 100644
--- a/usr.bin/top/machine.c
+++ b/usr.bin/top/machine.c
@@ -220,6 +220,7 @@ static void getsysctl(const char *name, void *ptr, size_t len);
static int swapmode(int *retavail, int *retfree);
static void update_layout(void);
static int find_uid(uid_t needle, int *haystack);
+static int cmd_matches(struct kinfo_proc *, const char *);
static int
find_uid(uid_t needle, int *haystack)
@@ -853,6 +854,10 @@ get_process_info(struct system_info *si, struct process_select *sel,
if (sel->pid != -1 && pp->ki_pid != sel->pid)
continue;
+ if (!cmd_matches(pp, sel->command))
+ /* skip proc. that doesn't match grep string */
+ continue;
+
*prefp++ = pp;
active_procs++;
}
@@ -871,6 +876,36 @@ get_process_info(struct system_info *si, struct process_select *sel,
return (&handle);
}
+static int
+cmd_matches(struct kinfo_proc *proc, const char *term)
+{
+ extern int show_args;
+ char **args = NULL;
+
+ if (!term) {
+ /* No command filter set */
+ return 1;
+ } else {
+ /* Filter set, does process name contain term? */
+ if (strstr(proc->ki_comm, term))
+ return 1;
+ /* Search arguments only if arguments are displayed */
+ if (show_args) {
+ args = kvm_getargv(kd, proc, 1024);
+ if (args == NULL) {
+ /* Failed to get arguments so can't search them */
+ return 0;
+ }
+ while (*args != NULL) {
+ if (strstr(*args, term))
+ return 1;
+ args++;
+ }
+ }
+ }
+ return 0;
+}
+
char *
format_next_process(struct handle * xhandle, char *(*get_userid)(int), int flags)
{
diff --git a/usr.bin/top/machine.h b/usr.bin/top/machine.h
index c3c7777d910e..f73f8f30e1aa 100644
--- a/usr.bin/top/machine.h
+++ b/usr.bin/top/machine.h
@@ -73,7 +73,7 @@ struct process_select
bool swap; /* show swap usage */
bool kidle; /* show per-CPU idle threads */
int pid; /* only this pid (unless pid == -1) */
- const char *command; /* only this command (unless == NULL) */
+ char *command; /* only this command (unless == NULL) */
};
/* routines defined by the machine dependent module */
diff --git a/usr.bin/top/top.1 b/usr.bin/top/top.1
index cd297b5aa3a1..104b979abd00 100644
--- a/usr.bin/top/top.1
+++ b/usr.bin/top/top.1
@@ -240,6 +240,11 @@ Remember that the next display counts as one, so typing
will make
.Nm
show one final display and then immediately exit.
+.It /
+Display only processes that contain the specified string in their
+command name.
+If displaying arguments is enabled, the arguments are searched
+too. '+' shows all processes.
.It m
Toggle the display between 'cpu' and 'io' modes.
.It n or #
diff --git a/usr.bin/top/top.c b/usr.bin/top/top.c
index 058a53b5f0a3..deba4d8e1ef3 100644
--- a/usr.bin/top/top.c
+++ b/usr.bin/top/top.c
@@ -52,6 +52,7 @@ typedef void sigret_t;
static char stdoutbuf[Buffersize];
static int fmt_flags = 0;
+int show_args = false;
int pcpu_stats = false;
/* signal handling routines */
@@ -898,6 +899,19 @@ restart:
}
break;
+ case CMD_grep: /* grep command name */
+ new_message(MT_standout,
+ "Grep command name: ");
+ if (readline(tempbuf1, sizeof(tempbuf1), false) > 0) {
+ free(ps.command);
+ if (tempbuf1[0] == '+' && tempbuf1[1] == '\0') {
+ ps.command = NULL;
+ } else if ((ps.command = strdup(tempbuf1)) == NULL)
+ quit(1);
+ }
+ clear_message();
+ break;
+
case CMD_displays: /* change display count */
new_message(MT_standout,
"Displays to show (currently %s): ",
@@ -1016,6 +1030,7 @@ restart:
break;
case CMD_showargs:
fmt_flags ^= FMT_SHOWARGS;
+ show_args = fmt_flags & FMT_SHOWARGS;
new_message(MT_standout | MT_delayed,
" %sisplaying process arguments.",
fmt_flags & FMT_SHOWARGS ? "D" : "Not d");
diff --git a/usr.bin/top/top.h b/usr.bin/top/top.h
index 7ec2d7f2c199..2f31d5812ee4 100644
--- a/usr.bin/top/top.h
+++ b/usr.bin/top/top.h
@@ -37,6 +37,8 @@ extern pid_t mypid;
extern int (*compares[])(const void*, const void*);
+extern int show_args;
+
const char* kill_procs(char *);
const char* renice_procs(char *);