HomeFreeBSD

ps(1): Aliases: Resolve once, merge specifications

Description

ps(1): Aliases: Resolve once, merge specifications

With this change, an alias keyword is resolved once and for all by
merging specifications of the keyword (or chain of keywords) it points
to. This merge in particular determines the final attributes of the
alias keyword, as well as the final keyword it points to, which uniquely
determines which information is printed (all non-alias keywords are
assumed to display different data).

Also, the alias resolving code has been moved into the resolve_alias()
function and helpers (e.g., merge_alias()). Aliases are still resolved
lazily as needed by default. The new top-level resolve_aliases()
function is used to resolve them at once at program startup if ps(1) has
been compiled with PS_CHECK_KEYWORDS defined. Else, it can also be
called directly from a debugger.

This is in preparation for removing columns that actually display the
same information in a subsequent commit, as this requires being able to
(quickly) determine if they are aliases to each other.


The merge process is now explicit and more flexible. Previously, all
fields of the resolved keyword were unconditionally used for the alias
(the pointer to an alias keyword structure was replaced by one to the
aliased keyword's one). Now, field 'final_kw' on the alias keyword will
store a pointer to the aliased keyword structure (and not only its name,
as a subsequent commit will need the structure address). Fields
'header', 'field' and 'flag' are taken from the aliased keyword if they
have default values (NULL or 0), else the alias' values prevail. This
allows an alias to override one or more of these fields. All fields
after 'oproc', because they describe the information to display
consistently with each other, are always taken from the aliased keyword.
merge_alias() checks that the values of these fields in the alias
keyword structure are unspecified (NULL, or some neutral value like
0 and UNSPEC).

While here, parsefmt() was reworked to avoid any direct recursion and
the break-up/recombination steps that were used when processing aliases.
The latter was due to the mutual recursion with findvar() and its
odd-for-that-purpose signature. findvar() has been removed in the
process. Simplification of parsefmt() also allows to be more precise
with the errors reported (in particular, the case of an empty keyword
with a specific header would just be reported as a "keyword not found"
message).

While here, introduce the check_keywords() function, performing sanity
checks on the declared keywords, currently only validating that they are
declared in alphabetical order. As for resolve_aliases(), this function
is called at startup on PS_CHECK_KEYWORDS, else it is available to be
called from a debugger.

Ideally, alias resolution should be done at compile time. In practice,
it seems doing so at runtime was never a problem (there are only a few
aliases compared to all available keywords, and there's currently at
most one level of aliasing). With the changes here, it seems very
unlikely to become one even if many more keywords, aliases or aliasing
levels are added.

MFC after: 3 days
Sponsored by: The FreeBSD Foundation

(cherry picked from commit 193e68d5174ffde70acc427f9d830797df9a13cb)

Details

Provenance
olceAuthored on Apr 25 2025, 1:20 PM
Parents
rG8cc27ecb1971: ps(1): Keywords: New UNSPEC type, rename 'alias', re-order fields
Branches
Unknown
Tags
Unknown