perf tools: Eliminate duplicate code and use PATH_MAX consistently

No need for multiple definitions for STR() and die(), also use SuSv2's
PATH_MAX instead of adding MAX_PATH.

Cc: David Ahern <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-qpujjkw7u0bf0tr4wt55cr9y@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Arnaldo Carvalho de Melo 2011-11-16 12:55:59 -02:00
parent c23205c848
commit c168fbfb93
6 changed files with 24 additions and 62 deletions

View file

@ -46,7 +46,6 @@
#define DEFAULT_VAR_FILTER "!__k???tab_* & !__crc_*" #define DEFAULT_VAR_FILTER "!__k???tab_* & !__crc_*"
#define DEFAULT_FUNC_FILTER "!_*" #define DEFAULT_FUNC_FILTER "!_*"
#define MAX_PATH_LEN 256
/* Session management structure */ /* Session management structure */
static struct { static struct {

View file

@ -3,7 +3,6 @@
#include "parse-options.h" #include "parse-options.h"
#include "evsel.h" #include "evsel.h"
#include "cgroup.h" #include "cgroup.h"
#include "debugfs.h" /* MAX_PATH, STR() */
#include "evlist.h" #include "evlist.h"
int nr_cgroups; int nr_cgroups;
@ -12,7 +11,7 @@ static int
cgroupfs_find_mountpoint(char *buf, size_t maxlen) cgroupfs_find_mountpoint(char *buf, size_t maxlen)
{ {
FILE *fp; FILE *fp;
char mountpoint[MAX_PATH+1], tokens[MAX_PATH+1], type[MAX_PATH+1]; char mountpoint[PATH_MAX + 1], tokens[PATH_MAX + 1], type[PATH_MAX + 1];
char *token, *saved_ptr = NULL; char *token, *saved_ptr = NULL;
int found = 0; int found = 0;
@ -25,8 +24,8 @@ cgroupfs_find_mountpoint(char *buf, size_t maxlen)
* and inspect every cgroupfs mount point to find one that has * and inspect every cgroupfs mount point to find one that has
* perf_event subsystem * perf_event subsystem
*/ */
while (fscanf(fp, "%*s %"STR(MAX_PATH)"s %"STR(MAX_PATH)"s %" while (fscanf(fp, "%*s %"STR(PATH_MAX)"s %"STR(PATH_MAX)"s %"
STR(MAX_PATH)"s %*d %*d\n", STR(PATH_MAX)"s %*d %*d\n",
mountpoint, type, tokens) == 3) { mountpoint, type, tokens) == 3) {
if (!strcmp(type, "cgroup")) { if (!strcmp(type, "cgroup")) {
@ -57,15 +56,15 @@ cgroupfs_find_mountpoint(char *buf, size_t maxlen)
static int open_cgroup(char *name) static int open_cgroup(char *name)
{ {
char path[MAX_PATH+1]; char path[PATH_MAX + 1];
char mnt[MAX_PATH+1]; char mnt[PATH_MAX + 1];
int fd; int fd;
if (cgroupfs_find_mountpoint(mnt, MAX_PATH+1)) if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1))
return -1; return -1;
snprintf(path, MAX_PATH, "%s/%s", mnt, name); snprintf(path, PATH_MAX, "%s/%s", mnt, name);
fd = open(path, O_RDONLY); fd = open(path, O_RDONLY);
if (fd == -1) if (fd == -1)

View file

@ -2,8 +2,10 @@
#include "debugfs.h" #include "debugfs.h"
#include "cache.h" #include "cache.h"
#include <sys/mount.h>
static int debugfs_premounted; static int debugfs_premounted;
static char debugfs_mountpoint[MAX_PATH+1]; static char debugfs_mountpoint[PATH_MAX + 1];
static const char *debugfs_known_mountpoints[] = { static const char *debugfs_known_mountpoints[] = {
"/sys/kernel/debug/", "/sys/kernel/debug/",
@ -64,9 +66,7 @@ const char *debugfs_find_mountpoint(void)
if (fp == NULL) if (fp == NULL)
die("Can't open /proc/mounts for read"); die("Can't open /proc/mounts for read");
while (fscanf(fp, "%*s %" while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
STR(MAX_PATH)
"s %99s %*s %*d %*d\n",
debugfs_mountpoint, type) == 2) { debugfs_mountpoint, type) == 2) {
if (strcmp(type, "debugfs") == 0) if (strcmp(type, "debugfs") == 0)
break; break;
@ -158,7 +158,7 @@ int debugfs_umount(void)
int debugfs_write(const char *entry, const char *value) int debugfs_write(const char *entry, const char *value)
{ {
char path[MAX_PATH+1]; char path[PATH_MAX + 1];
int ret, count; int ret, count;
int fd; int fd;
@ -203,7 +203,7 @@ int debugfs_write(const char *entry, const char *value)
*/ */
int debugfs_read(const char *entry, char *buffer, size_t size) int debugfs_read(const char *entry, char *buffer, size_t size)
{ {
char path[MAX_PATH+1]; char path[PATH_MAX + 1];
int ret; int ret;
int fd; int fd;

View file

@ -1,25 +1,14 @@
#ifndef __DEBUGFS_H__ #ifndef __DEBUGFS_H__
#define __DEBUGFS_H__ #define __DEBUGFS_H__
#include <sys/mount.h> const char *debugfs_find_mountpoint(void);
int debugfs_valid_mountpoint(const char *debugfs);
#ifndef MAX_PATH int debugfs_valid_entry(const char *path);
# define MAX_PATH 256 char *debugfs_mount(const char *mountpoint);
#endif int debugfs_umount(void);
int debugfs_write(const char *entry, const char *value);
#ifndef STR int debugfs_read(const char *entry, char *buffer, size_t size);
# define _STR(x) #x void debugfs_force_cleanup(void);
# define STR(x) _STR(x) int debugfs_make_path(const char *element, char *buffer, int size);
#endif
extern const char *debugfs_find_mountpoint(void);
extern int debugfs_valid_mountpoint(const char *debugfs);
extern int debugfs_valid_entry(const char *path);
extern char *debugfs_mount(const char *mountpoint);
extern int debugfs_umount(void);
extern int debugfs_write(const char *entry, const char *value);
extern int debugfs_read(const char *entry, char *buffer, size_t size);
extern void debugfs_force_cleanup(void);
extern int debugfs_make_path(const char *element, char *buffer, int size);
#endif /* __DEBUGFS_H__ */ #endif /* __DEBUGFS_H__ */

View file

@ -5,7 +5,6 @@
#include "util.h" #include "util.h"
#include "probe-event.h" #include "probe-event.h"
#define MAX_PATH_LEN 256
#define MAX_PROBE_BUFFER 1024 #define MAX_PROBE_BUFFER 1024
#define MAX_PROBES 128 #define MAX_PROBES 128

View file

@ -18,7 +18,8 @@
* *
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/ */
#define _GNU_SOURCE #include <ctype.h>
#include "util.h"
#include <dirent.h> #include <dirent.h>
#include <mntent.h> #include <mntent.h>
#include <stdio.h> #include <stdio.h>
@ -31,7 +32,6 @@
#include <pthread.h> #include <pthread.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <ctype.h>
#include <errno.h> #include <errno.h>
#include <stdbool.h> #include <stdbool.h>
#include <linux/list.h> #include <linux/list.h>
@ -44,10 +44,6 @@
#define VERSION "0.5" #define VERSION "0.5"
#define _STR(x) #x
#define STR(x) _STR(x)
#define MAX_PATH 256
#define TRACE_CTRL "tracing_on" #define TRACE_CTRL "tracing_on"
#define TRACE "trace" #define TRACE "trace"
#define AVAILABLE "available_tracers" #define AVAILABLE "available_tracers"
@ -73,26 +69,6 @@ struct events {
}; };
static void die(const char *fmt, ...)
{
va_list ap;
int ret = errno;
if (errno)
perror("perf");
else
ret = -1;
va_start(ap, fmt);
fprintf(stderr, " ");
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
exit(ret);
}
void *malloc_or_die(unsigned int size) void *malloc_or_die(unsigned int size)
{ {
void *data; void *data;