diff options
author | uqs <uqs@FreeBSD.org> | 2011-01-23 11:08:28 +0000 |
---|---|---|
committer | uqs <uqs@FreeBSD.org> | 2011-01-23 11:08:28 +0000 |
commit | 7fdf187c116172ba5c8238926c41445a02f3a4ad (patch) | |
tree | affb155452e52759603cd28ea92e941e8e5ab8a0 /lib/libkvm/kvm.c | |
parent | 72b8fc74b4eb02687ea1cfa8837baa1fe5f697ec (diff) | |
download | FreeBSD-src-7fdf187c116172ba5c8238926c41445a02f3a4ad.zip FreeBSD-src-7fdf187c116172ba5c8238926c41445a02f3a4ad.tar.gz |
libkvm code janitoring
- make WARNS=6 clean for archs w/o strict alignment requirments
- add const, ANSIfy, remove unused vars, cast types for comparison
- thanks to differing definitions of VM_MIN_ADDRESS across our archs, we
need to trick the compiler to not complain about signedness. We could
either fix VM_MIN_ADDRESS to always be a simple integer or make the
check conditional on $ARCH.
Closes PRs: kern/42386, kern/83364
Reviewed by: bde
Diffstat (limited to 'lib/libkvm/kvm.c')
-rw-r--r-- | lib/libkvm/kvm.c | 157 |
1 files changed, 65 insertions, 92 deletions
diff --git a/lib/libkvm/kvm.c b/lib/libkvm/kvm.c index b6d3bcd..cf29f10 100644 --- a/lib/libkvm/kvm.c +++ b/lib/libkvm/kvm.c @@ -77,8 +77,7 @@ static char sccsid[] = "@(#)kvm.c 8.2 (Berkeley) 2/13/94"; int __fdnlist(int, struct nlist *); char * -kvm_geterr(kd) - kvm_t *kd; +kvm_geterr(kvm_t *kd) { return (kd->errbuf); } @@ -103,7 +102,7 @@ _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...) (void)fputc('\n', stderr); } else (void)vsnprintf(kd->errbuf, - sizeof(kd->errbuf), (char *)fmt, ap); + sizeof(kd->errbuf), fmt, ap); va_end(ap); } @@ -122,7 +121,7 @@ _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...) } else { char *cp = kd->errbuf; - (void)vsnprintf(cp, sizeof(kd->errbuf), (char *)fmt, ap); + (void)vsnprintf(cp, sizeof(kd->errbuf), fmt, ap); n = strlen(cp); (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s", strerror(errno)); @@ -131,25 +130,18 @@ _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...) } void * -_kvm_malloc(kd, n) - kvm_t *kd; - size_t n; +_kvm_malloc(kvm_t *kd, size_t n) { void *p; if ((p = calloc(n, sizeof(char))) == NULL) - _kvm_err(kd, kd->program, "can't allocate %u bytes: %s", + _kvm_err(kd, kd->program, "can't allocate %zu bytes: %s", n, strerror(errno)); return (p); } static kvm_t * -_kvm_open(kd, uf, mf, flag, errout) - kvm_t *kd; - const char *uf; - const char *mf; - int flag; - char *errout; +_kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout) { struct stat st; @@ -242,12 +234,8 @@ failed: } kvm_t * -kvm_openfiles(uf, mf, sf, flag, errout) - const char *uf; - const char *mf; - const char *sf __unused; - int flag; - char *errout; +kvm_openfiles(const char *uf, const char *mf, const char *sf __unused, int flag, + char *errout) { kvm_t *kd; @@ -260,12 +248,8 @@ kvm_openfiles(uf, mf, sf, flag, errout) } kvm_t * -kvm_open(uf, mf, sf, flag, errstr) - const char *uf; - const char *mf; - const char *sf __unused; - int flag; - const char *errstr; +kvm_open(const char *uf, const char *mf, const char *sf __unused, int flag, + const char *errstr) { kvm_t *kd; @@ -280,8 +264,7 @@ kvm_open(uf, mf, sf, flag, errstr) } int -kvm_close(kd) - kvm_t *kd; +kvm_close(kvm_t *kd) { int error = 0; @@ -316,8 +299,9 @@ kvm_fdnlist_prefix(kvm_t *kd, struct nlist *nl, int missing, const char *prefix, { struct nlist *n, *np, *p; char *cp, *ce; + const char *ccp; size_t len; - int unresolved; + int slen, unresolved; /* * Calculate the space we need to malloc for nlist and names. @@ -355,13 +339,13 @@ kvm_fdnlist_prefix(kvm_t *kd, struct nlist *nl, int missing, const char *prefix, continue; bcopy(p, np, sizeof(struct nlist)); /* Save the new\0orig. name so we can later match it again. */ - len = snprintf(cp, ce - cp, "%s%s%c%s", prefix, + slen = snprintf(cp, ce - cp, "%s%s%c%s", prefix, (prefix[0] != '\0' && p->n_name[0] == '_') ? (p->n_name + 1) : p->n_name, '\0', p->n_name); - if (len >= ce - cp) + if (slen < 0 || slen >= ce - cp) continue; np->n_name = cp; - cp += len + 1; + cp += slen + 1; np++; unresolved++; } @@ -385,8 +369,8 @@ kvm_fdnlist_prefix(kvm_t *kd, struct nlist *nl, int missing, const char *prefix, if (p->n_type != N_UNDF) continue; /* Skip expanded name and compare to orig. one. */ - cp = np->n_name + strlen(np->n_name) + 1; - if (strcmp(cp, p->n_name)) + ccp = np->n_name + strlen(np->n_name) + 1; + if (strcmp(ccp, p->n_name) != 0) continue; /* Update nlist with new, translated results. */ p->n_type = np->n_type; @@ -416,7 +400,8 @@ _kvm_nlist(kvm_t *kd, struct nlist *nl, int initialize) int nvalid; struct kld_sym_lookup lookup; int error; - char *prefix = "", symname[1024]; /* XXX-BZ symbol name length limit? */ + const char *prefix = ""; + char symname[1024]; /* XXX-BZ symbol name length limit? */ int tried_vnet, tried_dpcpu; /* @@ -458,9 +443,8 @@ again: error = snprintf(symname, sizeof(symname), "%s%s", prefix, (prefix[0] != '\0' && p->n_name[0] == '_') ? (p->n_name + 1) : p->n_name); - if (error >= sizeof(symname)) + if (error < 0 || error >= (int)sizeof(symname)) continue; - lookup.symname = symname; if (lookup.symname[0] == '_') lookup.symname++; @@ -470,11 +454,11 @@ again: p->n_other = 0; p->n_desc = 0; if (_kvm_vnet_initialized(kd, initialize) && - !strcmp(prefix, VNET_SYMPREFIX)) + !strcmp(prefix, VNET_SYMPREFIX) == 0) p->n_value = _kvm_vnet_validaddr(kd, lookup.symvalue); else if (_kvm_dpcpu_initialized(kd, initialize) && - !strcmp(prefix, DPCPU_SYMPREFIX)) + !strcmp(prefix, DPCPU_SYMPREFIX) == 0) p->n_value = _kvm_dpcpu_validaddr(kd, lookup.symvalue); else @@ -511,9 +495,7 @@ again: } int -kvm_nlist(kd, nl) - kvm_t *kd; - struct nlist *nl; +kvm_nlist(kvm_t *kd, struct nlist *nl) { /* @@ -524,13 +506,11 @@ kvm_nlist(kd, nl) } ssize_t -kvm_read(kd, kva, buf, len) - kvm_t *kd; - u_long kva; - void *buf; - size_t len; +kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len) { int cc; + ssize_t cr; + off_t pa; char *cp; if (ISALIVE(kd)) { @@ -540,59 +520,52 @@ kvm_read(kd, kva, buf, len) */ errno = 0; if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) { - _kvm_err(kd, 0, "invalid address (%x)", kva); + _kvm_err(kd, 0, "invalid address (%lx)", kva); return (-1); } - cc = read(kd->vmfd, buf, len); - if (cc < 0) { + cr = read(kd->vmfd, buf, len); + if (cr < 0) { _kvm_syserr(kd, 0, "kvm_read"); return (-1); - } else if (cc < len) + } else if (cr < (ssize_t)len) _kvm_err(kd, kd->program, "short read"); - return (cc); - } else { - cp = buf; - while (len > 0) { - off_t pa; - - cc = _kvm_kvatop(kd, kva, &pa); - if (cc == 0) - return (-1); - if (cc > len) - cc = len; - errno = 0; - if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) { - _kvm_syserr(kd, 0, _PATH_MEM); - break; - } - cc = read(kd->pmfd, cp, cc); - if (cc < 0) { - _kvm_syserr(kd, kd->program, "kvm_read"); - break; - } - /* - * If kvm_kvatop returns a bogus value or our core - * file is truncated, we might wind up seeking beyond - * the end of the core file in which case the read will - * return 0 (EOF). - */ - if (cc == 0) - break; - cp += cc; - kva += cc; - len -= cc; + return (cr); + } + + cp = buf; + while (len > 0) { + cc = _kvm_kvatop(kd, kva, &pa); + if (cc == 0) + return (-1); + if (cc > (ssize_t)len) + cc = len; + errno = 0; + if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) { + _kvm_syserr(kd, 0, _PATH_MEM); + break; + } + cr = read(kd->pmfd, cp, cc); + if (cr < 0) { + _kvm_syserr(kd, kd->program, "kvm_read"); + break; } - return (cp - (char *)buf); + /* + * If kvm_kvatop returns a bogus value or our core file is + * truncated, we might wind up seeking beyond the end of the + * core file in which case the read will return 0 (EOF). + */ + if (cr == 0) + break; + cp += cr; + kva += cr; + len -= cr; } - /* NOTREACHED */ + + return (cp - (char *)buf); } ssize_t -kvm_write(kd, kva, buf, len) - kvm_t *kd; - u_long kva; - const void *buf; - size_t len; +kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len) { int cc; @@ -602,14 +575,14 @@ kvm_write(kd, kva, buf, len) */ errno = 0; if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) { - _kvm_err(kd, 0, "invalid address (%x)", kva); + _kvm_err(kd, 0, "invalid address (%lx)", kva); return (-1); } cc = write(kd->vmfd, buf, len); if (cc < 0) { _kvm_syserr(kd, 0, "kvm_write"); return (-1); - } else if (cc < len) + } else if ((size_t)cc < len) _kvm_err(kd, kd->program, "short write"); return (cc); } else { |