From owner-dev-commits-src-main@freebsd.org Tue Jun 29 23:52:39 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 94A8F657138; Tue, 29 Jun 2021 23:52:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4GF1Vv3jfbz4k3l; Tue, 29 Jun 2021 23:52:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 6882C2264E; Tue, 29 Jun 2021 23:52:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 15TNqd80084827; Tue, 29 Jun 2021 23:52:39 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 15TNqdmX084826; Tue, 29 Jun 2021 23:52:39 GMT (envelope-from git) Date: Tue, 29 Jun 2021 23:52:39 GMT Message-Id: <202106292352.15TNqdmX084826@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: a00d703f2f43 - main - top(1): support command name and argument grepping MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: a00d703f2f438b199d3933d19d535540586b7792 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 29 Jun 2021 23:52:39 -0000 The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=a00d703f2f438b199d3933d19d535540586b7792 commit a00d703f2f438b199d3933d19d535540586b7792 Author: John Grafton AuthorDate: 2021-06-16 19:40:21 +0000 Commit: Warner Losh CommitDate: 2021-06-29 23:51:18 +0000 top(1): support command name and argument grepping Obtained from: OpenBSD Reviewed by: imp@ Pull Request: https://github.com/freebsd/freebsd-src/pull/479 --- usr.bin/top/commands.c | 1 + usr.bin/top/commands.h | 1 + usr.bin/top/machine.c | 35 +++++++++++++++++++++++++++++++++++ usr.bin/top/machine.h | 2 +- usr.bin/top/top.1 | 5 +++++ usr.bin/top/top.c | 15 +++++++++++++++ usr.bin/top/top.h | 2 ++ 7 files changed, 60 insertions(+), 1 deletion(-) 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 c7b3e13c700b..1fe2a91a655c 100644 --- a/usr.bin/top/machine.c +++ b/usr.bin/top/machine.c @@ -224,6 +224,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) @@ -869,6 +870,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++; } @@ -887,6 +892,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 c2616e9052e3..af66eaead76b 100644 --- a/usr.bin/top/machine.h +++ b/usr.bin/top/machine.h @@ -75,7 +75,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 03301b2f89fc..42282bebdafe 100644 --- a/usr.bin/top/top.1 +++ b/usr.bin/top/top.1 @@ -243,6 +243,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 9853ecf914b0..4fbf11b50299 100644 --- a/usr.bin/top/top.c +++ b/usr.bin/top/top.c @@ -53,6 +53,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 */ @@ -907,6 +908,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): ", @@ -1025,6 +1039,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 *);