Skip to content

Commit

Permalink
libkvm: Fix kvm_getloadavg() on modern kernel vmcores
Browse files Browse the repository at this point in the history
Fix kvm_getloadavg() to work correctly on vmcores for modern kernel
versions.  The kernels no longer include the `_fscale` symbol causing
the kvm_nlist() invocation to fail.  The code seemed to already assume
that `_fscale` could be missing but the early kvm_nlist() result check
has caused the function to fail if any symbol were missing.  Modify
it to only treat `_averunnable` as obligatory, and handle missing
`_fscale` gracefully.

Sponsored by:   The FreeBSD Foundation
  • Loading branch information
mgorny committed Dec 7, 2021
1 parent 22c4ab6 commit 7bcf80c
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/libkvm/kvm_getloadavg.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ int
kvm_getloadavg(kvm_t *kd, double loadavg[], int nelem)
{
struct loadavg loadinfo;
struct nlist *p;
int fscale, i;

if (ISALIVE(kd))
Expand All @@ -74,10 +73,9 @@ kvm_getloadavg(kvm_t *kd, double loadavg[], int nelem)
return (-1);
}

if (kvm_nlist(kd, nl) != 0) {
for (p = nl; p->n_type != 0; ++p);
if (kvm_nlist(kd, nl) != 0 && nl[X_AVERUNNABLE].n_type == 0) {
_kvm_err(kd, kd->program,
"%s: no such symbol", p->n_name);
"%s: no such symbol", nl[X_AVERUNNABLE].n_name);
return (-1);
}

Expand All @@ -92,7 +90,8 @@ kvm_getloadavg(kvm_t *kd, double loadavg[], int nelem)
* Old kernels have fscale separately; if not found assume
* running new format.
*/
if (!KREAD(kd, nl[X_FSCALE].n_value, &fscale))
if (nl[X_FSCALE].n_type != 0 &&
!KREAD(kd, nl[X_FSCALE].n_value, &fscale))
loadinfo.fscale = fscale;

nelem = MIN(nelem, (int)(sizeof(loadinfo.ldavg) / sizeof(fixpt_t)));
Expand Down

0 comments on commit 7bcf80c

Please sign in to comment.