Skip to content

Commit

Permalink
perf symbols: Move hex2u64 and strxfrchar to symbol.c
Browse files Browse the repository at this point in the history
Mostly used in symbol.c so move them there to reduce the number
of files needed to use the symbol system.

Also do some header adjustments with the same intent.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Frédéric Weisbecker <fweisbec@gmail.com>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Paul Mackerras <paulus@samba.org>
LKML-Reference: <1269557941-15617-5-git-send-email-acme@infradead.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
  • Loading branch information
Arnaldo Carvalho de Melo authored and Ingo Molnar committed Mar 26, 2010
1 parent 618038d commit 5aab621
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 65 deletions.
1 change: 1 addition & 0 deletions tools/perf/util/parse-events.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "parse-events.h"
#include "exec_cmd.h"
#include "string.h"
#include "symbol.h"
#include "cache.h"
#include "header.h"
#include "debugfs.h"
Expand Down
43 changes: 0 additions & 43 deletions tools/perf/util/string.c
Original file line number Diff line number Diff line change
@@ -1,49 +1,6 @@
#include "string.h"
#include "util.h"

static int hex(char ch)
{
if ((ch >= '0') && (ch <= '9'))
return ch - '0';
if ((ch >= 'a') && (ch <= 'f'))
return ch - 'a' + 10;
if ((ch >= 'A') && (ch <= 'F'))
return ch - 'A' + 10;
return -1;
}

/*
* While we find nice hex chars, build a long_val.
* Return number of chars processed.
*/
int hex2u64(const char *ptr, u64 *long_val)
{
const char *p = ptr;
*long_val = 0;

while (*p) {
const int hex_val = hex(*p);

if (hex_val < 0)
break;

*long_val = (*long_val << 4) | hex_val;
p++;
}

return p - ptr;
}

char *strxfrchar(char *s, char from, char to)
{
char *p = s;

while ((p = strchr(p, from)) != NULL)
*p++ = to;

return s;
}

#define K 1024LL
/*
* perf_atoll()
Expand Down
2 changes: 0 additions & 2 deletions tools/perf/util/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <stdbool.h>
#include "types.h"

int hex2u64(const char *ptr, u64 *val);
char *strxfrchar(char *s, char from, char to);
s64 perf_atoll(const char *str);
char **argv_split(const char *str, int *argcp);
void argv_free(char **argv);
Expand Down
85 changes: 67 additions & 18 deletions tools/perf/util/symbol.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#include "util.h"
#include "../perf.h"
#include "sort.h"
#include "string.h"
#define _GNU_SOURCE
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <libgen.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <fcntl.h>
#include <unistd.h>
#include "symbol.h"
#include "thread.h"
#include "strlist.h"

#include "debug.h"

#include <asm/bug.h>
#include <libelf.h>
#include <gelf.h>
#include <elf.h>
Expand Down Expand Up @@ -114,8 +120,8 @@ static void map_groups__fixup_end(struct map_groups *self)
static struct symbol *symbol__new(u64 start, u64 len, const char *name)
{
size_t namelen = strlen(name) + 1;
struct symbol *self = zalloc(symbol_conf.priv_size +
sizeof(*self) + namelen);
struct symbol *self = calloc(1, (symbol_conf.priv_size +
sizeof(*self) + namelen));
if (self == NULL)
return NULL;

Expand Down Expand Up @@ -166,7 +172,7 @@ static void dso__set_basename(struct dso *self)

struct dso *dso__new(const char *name)
{
struct dso *self = zalloc(sizeof(*self) + strlen(name) + 1);
struct dso *self = calloc(1, sizeof(*self) + strlen(name) + 1);

if (self != NULL) {
int i;
Expand Down Expand Up @@ -1382,13 +1388,13 @@ static int dso__kernel_module_get_build_id(struct dso *self)
return 0;
}

static int map_groups__set_modules_path_dir(struct map_groups *self, char *dirname)
static int map_groups__set_modules_path_dir(struct map_groups *self, char *dir_name)
{
struct dirent *dent;
DIR *dir = opendir(dirname);
DIR *dir = opendir(dir_name);

if (!dir) {
pr_debug("%s: cannot open %s dir\n", __func__, dirname);
pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
return -1;
}

Expand All @@ -1401,7 +1407,7 @@ static int map_groups__set_modules_path_dir(struct map_groups *self, char *dirna
continue;

snprintf(path, sizeof(path), "%s/%s",
dirname, dent->d_name);
dir_name, dent->d_name);
if (map_groups__set_modules_path_dir(self, path) < 0)
goto failure;
} else {
Expand All @@ -1421,7 +1427,7 @@ static int map_groups__set_modules_path_dir(struct map_groups *self, char *dirna
continue;

snprintf(path, sizeof(path), "%s/%s",
dirname, dent->d_name);
dir_name, dent->d_name);

long_name = strdup(path);
if (long_name == NULL)
Expand Down Expand Up @@ -1458,8 +1464,8 @@ static int map_groups__set_modules_path(struct map_groups *self)
*/
static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
{
struct map *self = zalloc(sizeof(*self) +
(dso->kernel ? sizeof(struct kmap) : 0));
struct map *self = calloc(1, (sizeof(*self) +
(dso->kernel ? sizeof(struct kmap) : 0)));
if (self != NULL) {
/*
* ->end will be filled after we load all the symbols
Expand Down Expand Up @@ -1963,3 +1969,46 @@ int map_groups__create_kernel_maps(struct map_groups *self,
map_groups__fixup_end(self);
return 0;
}

static int hex(char ch)
{
if ((ch >= '0') && (ch <= '9'))
return ch - '0';
if ((ch >= 'a') && (ch <= 'f'))
return ch - 'a' + 10;
if ((ch >= 'A') && (ch <= 'F'))
return ch - 'A' + 10;
return -1;
}

/*
* While we find nice hex chars, build a long_val.
* Return number of chars processed.
*/
int hex2u64(const char *ptr, u64 *long_val)
{
const char *p = ptr;
*long_val = 0;

while (*p) {
const int hex_val = hex(*p);

if (hex_val < 0)
break;

*long_val = (*long_val << 4) | hex_val;
p++;
}

return p - ptr;
}

char *strxfrchar(char *s, char from, char to)
{
char *p = s;

while ((p = strchr(p, from)) != NULL)
*p++ = to;

return s;
}
10 changes: 8 additions & 2 deletions tools/perf/util/symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

#include <linux/types.h>
#include <stdbool.h>
#include "types.h"
#include <stdint.h>
#include "map.h"
#include <linux/list.h>
#include <linux/rbtree.h>
#include "event.h"
#include <stdio.h>

#define DEBUG_CACHE_DIR ".debug"

Expand All @@ -29,6 +30,9 @@ static inline char *bfd_demangle(void __used *v, const char __used *c,
#endif
#endif

int hex2u64(const char *ptr, u64 *val);
char *strxfrchar(char *s, char from, char to);

/*
* libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
* for newer versions we can use mmap to reduce memory usage:
Expand All @@ -44,6 +48,8 @@ static inline char *bfd_demangle(void __used *v, const char __used *c,
#define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
#endif

#define BUILD_ID_SIZE 20

struct symbol {
struct rb_node rb_node;
u64 start;
Expand Down

0 comments on commit 5aab621

Please sign in to comment.