- pass lint

- minor spacing nits
- check allocations
This commit is contained in:
christos 2010-11-24 13:17:56 +00:00
parent c21ac41b0b
commit 4ef443d8ef
4 changed files with 31 additions and 29 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.1 2010/11/23 20:48:40 yamt Exp $
# $NetBSD: Makefile,v 1.2 2010/11/24 13:17:56 christos Exp $
PROG= tpfmt
NOMAN=
@ -8,8 +8,10 @@ SRCS= tpfmt.c sym.c
LDADD+= -lpthread
LDADD+= -lelf
LDADD+= -lutil
DPADD+= ${LIBPTHREAD}
DPADD+= ${LIBELF}
DPADD+= ${LIBUTIL}
.include <bsd.own.mk>
.include <bsd.prog.mk>

View File

@ -1,7 +1,7 @@
/* $NetBSD: sym.c,v 1.1 2010/11/23 20:48:40 yamt Exp $ */
/* $NetBSD: sym.c,v 1.2 2010/11/24 13:17:56 christos Exp $ */
/*-
* Copyright (c)2010 YAMAMOTO Takashi,
* Copyright (c) 2010 YAMAMOTO Takashi,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -28,7 +28,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: sym.c,v 1.1 2010/11/23 20:48:40 yamt Exp $");
__RCSID("$NetBSD: sym.c,v 1.2 2010/11/24 13:17:56 christos Exp $");
#endif /* not lint */
#include <assert.h>
@ -39,6 +39,7 @@ __RCSID("$NetBSD: sym.c,v 1.1 2010/11/23 20:48:40 yamt Exp $");
#include <libelf.h>
#include <stdlib.h>
#include <string.h>
#include <util.h>
#include "sym.h"
@ -49,7 +50,7 @@ struct sym {
};
static struct sym **syms = NULL;
static unsigned int nsyms = 0;
static size_t nsyms = 0;
static int
compare_value(const void *p1, const void *p2)
@ -74,8 +75,7 @@ ksymload(const char *ksyms)
GElf_Shdr *sh;
Elf_Data *d;
int fd;
size_t size;
unsigned int i;
size_t size, i;
fd = open(ksyms, O_RDONLY);
if (fd == -1) {
@ -111,22 +111,19 @@ ksymload(const char *ksyms)
GElf_Sym *st;
struct sym *sym;
st = gelf_getsym(d, i, &st_store);
st = gelf_getsym(d, (int)i, &st_store);
if (st == NULL) {
goto elffail;
}
if (ELF_ST_TYPE(st->st_info) != STT_FUNC) {
continue;
}
sym = malloc(sizeof(*sym));
sym->name = strdup(elf_strptr(e, sh->sh_link, st->st_name));
sym = emalloc(sizeof(*sym));
sym->name = estrdup(elf_strptr(e, sh->sh_link, st->st_name));
sym->value = (uint64_t)st->st_value;
sym->size = st->st_size;
nsyms++;
syms = realloc(syms, sizeof(*syms) * nsyms);
if (syms == NULL) {
err(EXIT_FAILURE, "realloc");
}
syms = erealloc(syms, sizeof(*syms) * nsyms);
syms[nsyms - 1] = sym;
}
qsort(syms, nsyms, sizeof(*syms), compare_value);
@ -138,7 +135,7 @@ elffail:
const char *
ksymlookup(uint64_t value, uint64_t *offset)
{
unsigned int i;
size_t i;
for (i = 0; i < nsyms; i++) {
const struct sym *sym = syms[i];

View File

@ -1,7 +1,7 @@
/* $NetBSD: sym.h,v 1.1 2010/11/23 20:48:40 yamt Exp $ */
/* $NetBSD: sym.h,v 1.2 2010/11/24 13:17:56 christos Exp $ */
/*-
* Copyright (c)2010 YAMAMOTO Takashi,
* Copyright (c) 2010 YAMAMOTO Takashi,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without

View File

@ -1,7 +1,7 @@
/* $NetBSD: tpfmt.c,v 1.1 2010/11/23 20:48:40 yamt Exp $ */
/* $NetBSD: tpfmt.c,v 1.2 2010/11/24 13:17:56 christos Exp $ */
/*-
* Copyright (c)2010 YAMAMOTO Takashi,
* Copyright (c) 2010 YAMAMOTO Takashi,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -28,7 +28,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: tpfmt.c,v 1.1 2010/11/23 20:48:40 yamt Exp $");
__RCSID("$NetBSD: tpfmt.c,v 1.2 2010/11/24 13:17:56 christos Exp $");
#endif /* not lint */
#include <sys/rbtree.h>
@ -40,10 +40,11 @@ __RCSID("$NetBSD: tpfmt.c,v 1.1 2010/11/23 20:48:40 yamt Exp $");
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <util.h>
#include "sym.h"
const char *ksyms = "/dev/ksyms";
static const char ksyms[] = "/dev/ksyms";
struct addr {
struct rb_node node;
@ -51,9 +52,10 @@ struct addr {
unsigned int nsamples; /* number of samples taken for the address */
};
rb_tree_t addrtree;
static rb_tree_t addrtree;
static signed int
/*ARGSUSED1*/
addrtree_compare_key(void *ctx, const void *n1, const void *keyp)
{
const struct addr *a1 = n1;
@ -95,13 +97,13 @@ compare_nsamples(const void *p1, const void *p2)
}
int
/*ARGSUSED*/
main(int argc, char *argv[])
{
struct addr *a;
struct addr **l;
struct addr **p;
unsigned int naddrs;
unsigned int i;
size_t naddrs, i;
ksymload(ksyms);
rb_tree_init(&addrtree, &addrtree_ops);
@ -124,7 +126,7 @@ main(int argc, char *argv[])
err(EXIT_FAILURE, "fread");
}
}
a = malloc(sizeof(*a));
a = emalloc(sizeof(*a));
a->addr = (uint64_t)sample;
a->nsamples = 1;
o = rb_tree_insert_node(&addrtree, a);
@ -140,7 +142,7 @@ main(int argc, char *argv[])
* sort samples by addresses.
*/
l = malloc(naddrs * sizeof(*l));
l = emalloc(naddrs * sizeof(*l));
p = l;
RB_TREE_FOREACH(a, &addrtree) {
*p++ = a;
@ -161,14 +163,15 @@ main(int argc, char *argv[])
a = l[i];
name = ksymlookup(a->addr, &offset);
if (name == NULL) {
snprintf(buf, sizeof(buf), "<%016" PRIx64 ">", a->addr);
(void)snprintf(buf, sizeof(buf), "<%016" PRIx64 ">",
a->addr);
name = buf;
} else if (offset != 0) {
snprintf(buf, sizeof(buf), "%s+0x%" PRIx64, name,
(void)snprintf(buf, sizeof(buf), "%s+0x%" PRIx64, name,
offset);
name = buf;
}
printf("%8u %016" PRIx64 " %s\n", a->nsamples, a->addr, name);
}
exit(EXIT_SUCCESS);
return EXIT_SUCCESS;
}