Add utility functions

Move helpers out to separate files.
Mostly borrowed from perf

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
This commit is contained in:
Cyrill Gorcunov
2010-03-25 00:14:25 +03:00
committed by Will Deacon
parent aee6632eee
commit ad054a21d2
4 changed files with 109 additions and 18 deletions
+1
View File
@@ -2,6 +2,7 @@ PROGRAM = kvm
OBJS += kvm.o
OBJS += cpu.o
OBJS += util.o
CFLAGS += -Iinclude
+2 -18
View File
@@ -12,6 +12,8 @@
#include <stdio.h>
#include <fcntl.h>
#include "util.h"
/*
* Compatibility code. Remove this when we move to tools/kvm.
*/
@@ -31,24 +33,6 @@ struct kvm {
struct kvm_regs regs;
};
static void die_perror(const char *s)
{
perror(s);
exit(1);
}
static void die(const char *format, ...)
{
va_list ap;
va_start(ap, format);
vprintf(format, ap);
va_end(ap);
printf("\n");
exit(1);
}
static inline bool kvm__supports_extension(struct kvm *self, unsigned int extension)
{
int ret;
+62
View File
@@ -0,0 +1,62 @@
/*
* Taken from perf which in turn take it from GIT
*/
#include "util.h"
static void report(const char *prefix, const char *err, va_list params)
{
char msg[1024];
vsnprintf(msg, sizeof(msg), err, params);
fprintf(stderr, " %s%s\n", prefix, msg);
}
static NORETURN void die_builtin(const char *err, va_list params)
{
report(" Fatal: ", err, params);
exit(128);
}
static void error_builtin(const char *err, va_list params)
{
report(" Error: ", err, params);
}
static void warn_builtin(const char *warn, va_list params)
{
report(" Warning: ", warn, params);
}
void die(const char *err, ...)
{
va_list params;
va_start(params, err);
die_builtin(err, params);
va_end(params);
}
int error(const char *err, ...)
{
va_list params;
va_start(params, err);
error_builtin(err, params);
va_end(params);
return -1;
}
void warning(const char *warn, ...)
{
va_list params;
va_start(params, warn);
warn_builtin(warn, params);
va_end(params);
}
void die_perror(const char *s)
{
perror(s);
exit(1);
}
+44
View File
@@ -0,0 +1,44 @@
#ifndef UTIL_H_
#define UTIL_H_
/*
* Some bits are stolen from perf tool :)
*/
#include <unistd.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <sys/param.h>
#include <sys/types.h>
#ifdef __GNUC__
#define NORETURN __attribute__((__noreturn__))
#else
#define NORETURN
#ifndef __attribute__
#define __attribute__(x)
#endif
#endif
#define __stringify_1(x) #x
#define __stringify(x) __stringify_1(x)
extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
extern void die_perror(const char *s) NORETURN;
extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
#define DIE_IF(cnd) \
do { \
if (cnd) \
die(" at (" __FILE__ ":" __stringify(__LINE__) "): " \
__stringify(cnd) "\n"); \
} while (0)
#endif /* UTIL_H_ */