[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Add "comm" as a format spec to ps

From: Andrea Monaco
Subject: [PATCH] Add "comm" as a format spec to ps
Date: Wed, 08 Dec 2021 15:38:09 +0100


on my system I found that doing

 $ gdb foo [TAB]

that is, trying completion on the second argument of a call to gdb,
yields a

 ps: comm: Unknown format spec

That's because bash has specific completion rules for many common
programs.  In particular in /usr/share/bash-completion/completions/gdb I
have "ps axo comm,pid"; comm is not recognized by Hurd's ps as a format
spec, but is required by POSIX to display the command name (like "exe",
but without the full path).

This patch adds it.  So now we have completion of PIDs matching a given
program, which is useful when attaching gdb to a running process.


Andrea Monaco

diff --git a/libps/spec.c b/libps/spec.c
index c9164f9d..cbb00a3f 100644
--- a/libps/spec.c
+++ b/libps/spec.c
@@ -372,6 +372,17 @@ ps_get_exe (struct proc_stat *ps, char **exe_p, int 
 const struct ps_getter ps_exe_getter =
 {"exe", PSTAT_EXE, ps_get_exe};
+static void
+ps_get_comm (struct proc_stat *ps, char **comm_p, int *comm_len_p)
+  /* The GNU basename doesn't alter the string passed as arg */
+  *comm_p = basename (proc_stat_exe (ps));
+  *comm_len_p = strlen (*comm_p);
+const struct ps_getter ps_comm_getter =
+{"comm", PSTAT_EXE, ps_get_comm};
 /* ---------------------------------------------------------------- */
 /* some printing functions */
@@ -1128,6 +1139,8 @@ static const struct ps_fmt_spec specs[] =
    &ps_args_getter,       ps_emit_args,    ps_cmp_strings,ps_nominal_string},
   {"Arg0",     0,      0, -1, 0,
    &ps_args_getter,       ps_emit_string,  ps_cmp_strings,ps_nominal_string},
+  {"Comm",      "COMMAND", 0, -1, 0,
+   &ps_comm_getter,       ps_emit_string,  ps_cmp_strings,ps_nominal_string},
   {"Env",      0,       0, -1, 0,
    &ps_env_getter,        ps_emit_args,    ps_cmp_strings,ps_nominal_string},
   {"Start",    0,      -7, 1, 0,

reply via email to

[Prev in Thread] Current Thread [Next in Thread]