git: 3dcd4a2fd6b8 - main - top: implement pid highlighting
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Sun, 15 Nov 2026 08:15:09 UTC
The branch main has been updated by novel:
URL: https://cgit.FreeBSD.org/src/commit/?id=3dcd4a2fd6b84fe962897f3c07e684ce166798a4
commit 3dcd4a2fd6b84fe962897f3c07e684ce166798a4
Author: Roman Bogorodskiy <novel@FreeBSD.org>
AuthorDate: 2026-11-01 07:28:37 +0000
Commit: Roman Bogorodskiy <novel@FreeBSD.org>
CommitDate: 2026-11-15 08:14:22 +0000
top: implement pid highlighting
Implement PID highlighting. This can be activated by pressing
'L' and entering a PID to highlight. Highlighting can be cleared
by entering '+' as a PID to highlight. This feature is inspired by
the OpenBSD top, which uses 'P' key for that. We have to use 'L'
because 'P' is already used for toggling per-CPU statistics.
Reviewed by: kib
Obtained from: OpenBSD (partially)
MFC after: 2 weeks
Differential Revision: https://reviews.freebsd.org/D61090
---
usr.bin/top/commands.c | 2 +-
usr.bin/top/commands.h | 1 +
usr.bin/top/display.c | 44 ++++++++++++++++++++++++++++++++++----
usr.bin/top/display.h | 4 ++--
usr.bin/top/machine.c | 5 ++++-
usr.bin/top/machine.h | 4 ++--
usr.bin/top/top.1 | 9 +++++++-
usr.bin/top/top.c | 58 ++++++++++++++++++++++++++++++++++++++++----------
8 files changed, 105 insertions(+), 22 deletions(-)
diff --git a/usr.bin/top/commands.c b/usr.bin/top/commands.c
index 3fa63459f2e1..e4e71862c1ad 100644
--- a/usr.bin/top/commands.c
+++ b/usr.bin/top/commands.c
@@ -65,6 +65,7 @@ const struct command all_commands[] =
{'J', "display processes for only one jail (+ selects all jails)", false, CMD_jail},
{'j', "toggle the display of jail ID", false, CMD_jidtog},
{'k', "kill processes; send a signal to a list of processes", false, CMD_kill},
+ {'L', "highlight one process (+ clears highlighting)", false, CMD_hl},
{'m', "toggle the display between 'cpu' and 'io' modes", false, CMD_viewtog},
{'n', "change number of processes to display", false, CMD_number},
{'#', NULL, false, CMD_number},
@@ -515,4 +516,3 @@ renice_procs(char *str)
/* return appropriate error string */
return(err_string());
}
-
diff --git a/usr.bin/top/commands.h b/usr.bin/top/commands.h
index 63ed2f9b6471..25ac80c6cd6a 100644
--- a/usr.bin/top/commands.h
+++ b/usr.bin/top/commands.h
@@ -23,6 +23,7 @@ enum cmd_id {
CMD_quit,
CMD_help,
CMD_grep,
+ CMD_hl,
CMD_errors,
CMD_number,
CMD_delay,
diff --git a/usr.bin/top/display.c b/usr.bin/top/display.c
index b6cde0e63aa7..99dbefede237 100644
--- a/usr.bin/top/display.c
+++ b/usr.bin/top/display.c
@@ -64,6 +64,7 @@ static int lastline = 0;
/* buffer of proc information lines for display updating */
static char *screenbuf = NULL;
+static bool *screenhl = NULL;
static const char * const *procstate_names;
static const char * const *cpustate_names;
@@ -132,6 +133,8 @@ display_resize(void)
{
free(screenbuf);
}
+ free(screenhl);
+ screenhl = NULL;
/* calculate the current dimensions */
/* if operating in "dumb" mode, we only need one line */
@@ -147,6 +150,12 @@ display_resize(void)
/* oops! */
return(-1);
}
+ screenhl = calloc(lines, sizeof(*screenhl));
+ if (screenhl == NULL) {
+ free(screenbuf);
+ screenbuf = NULL;
+ return (-1);
+ }
/* return number of lines available */
/* for dumb terminals, pretend like we can show any amount */
@@ -817,10 +826,11 @@ u_header(const char *text __unused)
* *_process(line, thisline) - print one process line
*
* Assumptions: lastline is consistent
+ * u_process(): smart_terminal is true
*/
void
-i_process(int line, char *thisline)
+i_process(int line, char *thisline, bool hl)
{
char *p;
char *base;
@@ -840,11 +850,16 @@ i_process(int line, char *thisline)
}
/* write the line out */
- fputs(thisline, stdout);
+ if (hl)
+ top_standout(thisline);
+ else
+ fputs(thisline, stdout);
/* copy it in to our buffer */
base = smart_terminal ? screenbuf + lineindex(line) : screenbuf;
p = stpcpy(base, thisline);
+ if (smart_terminal)
+ screenhl[line] = hl;
/* zero fill the rest of it */
if (p - base < screen_width)
@@ -854,7 +869,7 @@ i_process(int line, char *thisline)
}
void
-u_process(int line, char *newline)
+u_process(int line, char *newline, bool hl)
{
char *optr;
int screen_line = line + Header_lines;
@@ -887,7 +902,10 @@ u_process(int line, char *newline)
}
/* now write the line */
- fputs(newline, stdout);
+ if (hl)
+ top_standout(newline);
+ else
+ fputs(newline, stdout);
/* copy it in to the buffer */
optr = stpcpy(bufferline, newline);
@@ -897,6 +915,24 @@ u_process(int line, char *newline)
{
memset(optr, 0, screen_width - (optr - bufferline));
}
+ screenhl[line] = hl;
+ } else if (hl || screenhl[line] != hl) {
+ int oldlen;
+
+ /* line_update() cannot change or preserve standout attributes */
+ oldlen = strlen(bufferline);
+ Move_to(0, screen_line);
+ lastline = screen_line;
+ if (hl)
+ top_standout(newline);
+ else
+ fputs(newline, stdout);
+ if (len < oldlen)
+ (void)clear_eol(oldlen - len);
+ optr = stpcpy(bufferline, newline);
+ if (optr - bufferline < screen_width)
+ memset(optr, 0, screen_width - (optr - bufferline));
+ screenhl[line] = hl;
}
else
{
diff --git a/usr.bin/top/display.h b/usr.bin/top/display.h
index 6e930f5ae6eb..b9764bd8ae89 100644
--- a/usr.bin/top/display.h
+++ b/usr.bin/top/display.h
@@ -19,7 +19,7 @@ void i_cpustates(int *states);
void i_loadave(int mpid, double *avenrun);
void i_memory(int *stats);
void i_message(void);
-void i_process(int line, char *thisline);
+void i_process(int line, char *thisline, bool hl);
void i_procstates(int total, int *brkdn);
void i_swap(int *stats);
void i_timeofday(time_t *tod);
@@ -35,7 +35,7 @@ void u_header(const char *text);
void u_loadave(int mpid, double *avenrun);
void u_memory(int *stats);
void u_message(void);
-void u_process(int line, char *newline);
+void u_process(int line, char *newline, bool hl);
void u_procstates(int total, int *brkdn);
void u_swap(int *stats);
void z_cpustates(void);
diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c
index 1f70ee9281e8..3e43b407a9df 100644
--- a/usr.bin/top/machine.c
+++ b/usr.bin/top/machine.c
@@ -1042,7 +1042,8 @@ cmd_matches(struct kinfo_proc *proc, const char *term)
}
char *
-format_next_process(struct handle * xhandle, char *(*get_userid)(int), int flags)
+format_next_process(struct handle *xhandle, char *(*get_userid)(int), int flags,
+ pid_t *pid)
{
struct kinfo_proc *pp;
const struct kinfo_proc *oldp;
@@ -1067,6 +1068,8 @@ format_next_process(struct handle * xhandle, char *(*get_userid)(int), int flags
pp = *(xhandle->next_proc++);
xhandle->remaining--;
+ *pid = pp->ki_pid;
+
/* get the process's command name */
if ((pp->ki_flag & P_INMEM) == 0) {
/*
diff --git a/usr.bin/top/machine.h b/usr.bin/top/machine.h
index b4f2f1a8bd50..cc038aeac407 100644
--- a/usr.bin/top/machine.h
+++ b/usr.bin/top/machine.h
@@ -82,8 +82,8 @@ struct process_select
struct handle;
char *format_header(const char *uname_field);
-char *format_next_process(struct handle* handle, char *(*get_userid)(int),
- int flags);
+char *format_next_process(struct handle *handle, char *(*get_userid)(int),
+ int flags, pid_t *pid);
void toggle_pcpustats(void);
void get_system_info(struct system_info *si);
int machine_init(struct statics *statics);
diff --git a/usr.bin/top/top.1 b/usr.bin/top/top.1
index 6c2597aa1334..b22442c4e078 100644
--- a/usr.bin/top/top.1
+++ b/usr.bin/top/top.1
@@ -1,4 +1,4 @@
-.Dd November 11, 2026
+.Dd November 15, 2026
.Dt TOP 1
.Os
.Sh NAME
@@ -318,6 +318,13 @@ Send a signal
to a list of processes.
This acts similarly to the command
.Xr kill 1 .
+.It Ic L
+Highlight a specific process.
+Prompt for
+.Ar PID
+or
+.Ql Ic +
+to clear highlighting.
.It Ic m
Toggle the display between 'cpu' and 'io' modes.
.It Ic n No or Ic #
diff --git a/usr.bin/top/top.c b/usr.bin/top/top.c
index 3ca766250297..a7463e199a22 100644
--- a/usr.bin/top/top.c
+++ b/usr.bin/top/top.c
@@ -69,6 +69,7 @@ static int max_topn; /* maximum displayable processes */
/* miscellaneous things */
struct process_select ps;
pid_t mypid;
+static pid_t hlpid = (pid_t)-1;
/* pointers to display routines */
static void (*d_loadave)(int mpid, double *avenrun) = i_loadave;
@@ -80,7 +81,7 @@ static void (*d_carc)(int *stats) = i_carc;
static void (*d_swap)(int *stats) = i_swap;
static void (*d_message)(void) = i_message;
static void (*d_header)(const char *text) = i_header;
-static void (*d_process)(int line, char *thisline) = i_process;
+static void (*d_process)(int line, char *thisline, bool hl) = i_process;
static void reset_display(void);
@@ -163,6 +164,19 @@ rem_uid(int uid)
}
}
+static pid_t
+parse_pid(const char *buf)
+{
+ unsigned long long num;
+ const char *errstr;
+
+ num = strtonum(buf, 0, INT_MAX, &errstr);
+ if (errstr != NULL || !find_pid(num))
+ return ((pid_t)-1);
+
+ return ((pid_t)num);
+}
+
static int
handle_user(char *buf, size_t buflen)
{
@@ -372,19 +386,13 @@ main(int argc, const char *argv[])
err(1, NULL);
break;
- case 'p': {
- unsigned long long num;
- const char *errstr;
-
- num = strtonum(optarg, 0, INT_MAX, &errstr);
- if (errstr != NULL || !find_pid(num)) {
+ case 'p':
+ if ((ps.pid = parse_pid(optarg)) == (pid_t)-1) {
fprintf(stderr, "%s: unknown pid\n", optarg);
exit(1);
}
- ps.pid = (pid_t)num;
ps.system = true;
break;
- }
case 's':
{
@@ -667,8 +675,12 @@ restart:
/* now show the top "n" processes. */
for (i = 0; i < active_procs; i++)
{
- (*d_process)(i, format_next_process(processes, get_userid,
- fmt_flags));
+ char *line;
+ pid_t pid;
+
+ line = format_next_process(processes, get_userid,
+ fmt_flags, &pid);
+ (*d_process)(i, line, pid == hlpid);
}
}
else
@@ -922,6 +934,30 @@ restart:
clear_message();
break;
+ case CMD_hl: /* highlight PID */
+ new_message(MT_standout,
+ "Process ID to highlight: ");
+ if (readline(tempbuf1, sizeof(tempbuf1),
+ false) > 0) {
+ pid_t pid;
+
+ if (tempbuf1[0] == '+' &&
+ tempbuf1[1] == '\0') {
+ hlpid = (pid_t)-1;
+ } else if ((pid = parse_pid(tempbuf1)) ==
+ (pid_t)-1) {
+ new_message(MT_standout,
+ " %s: unknown pid",
+ tempbuf1);
+ no_command = true;
+ } else
+ hlpid = pid;
+ if (!no_command)
+ clear_message();
+ } else
+ clear_message();
+ break;
+
case CMD_displays: /* change display count */
new_message(MT_standout,
"Displays to show (currently %s): ",