Blame liblttng-ust/lttng-filter.h

Packit c04fcb
#ifndef _LTTNG_FILTER_H
Packit c04fcb
#define _LTTNG_FILTER_H
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * lttng-filter.h
Packit c04fcb
 *
Packit c04fcb
 * LTTng UST filter header.
Packit c04fcb
 *
Packit c04fcb
 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Packit c04fcb
 *
Packit c04fcb
 * This library is free software; you can redistribute it and/or
Packit c04fcb
 * modify it under the terms of the GNU Lesser General Public
Packit c04fcb
 * License as published by the Free Software Foundation; only
Packit c04fcb
 * version 2.1 of the License.
Packit c04fcb
 *
Packit c04fcb
 * This library is distributed in the hope that it will be useful,
Packit c04fcb
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit c04fcb
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Packit c04fcb
 * Lesser General Public License for more details.
Packit c04fcb
 *
Packit c04fcb
 * You should have received a copy of the GNU Lesser General Public
Packit c04fcb
 * License along with this library; if not, write to the Free Software
Packit c04fcb
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Packit c04fcb
 */
Packit c04fcb
Packit c04fcb
#include <errno.h>
Packit c04fcb
#include <stdio.h>
Packit c04fcb
#include <helper.h>
Packit c04fcb
#include <lttng/ust-events.h>
Packit c04fcb
#include <lttng/ust-context-provider.h>
Packit c04fcb
#include <stdint.h>
Packit c04fcb
#include <assert.h>
Packit c04fcb
#include <errno.h>
Packit c04fcb
#include <string.h>
Packit c04fcb
#include <inttypes.h>
Packit c04fcb
#include <limits.h>
Packit c04fcb
#include <usterr-signal-safe.h>
Packit c04fcb
#include "filter-bytecode.h"
Packit c04fcb
Packit c04fcb
/* Filter stack length, in number of entries */
Packit c04fcb
#define FILTER_STACK_LEN	10	/* includes 2 dummy */
Packit c04fcb
#define FILTER_STACK_EMPTY	1
Packit c04fcb
Packit c04fcb
#ifndef min_t
Packit c04fcb
#define min_t(type, a, b)	\
Packit c04fcb
		((type) (a) < (type) (b) ? (type) (a) : (type) (b))
Packit c04fcb
#endif
Packit c04fcb
Packit c04fcb
#ifndef likely
Packit c04fcb
#define likely(x)	__builtin_expect(!!(x), 1)
Packit c04fcb
#endif
Packit c04fcb
Packit c04fcb
#ifndef unlikely
Packit c04fcb
#define unlikely(x)	__builtin_expect(!!(x), 0)
Packit c04fcb
#endif
Packit c04fcb
Packit c04fcb
#ifdef DEBUG
Packit c04fcb
#define dbg_printf(fmt, args...)				\
Packit c04fcb
	printf("[debug bytecode in %s:%s@%u] " fmt,		\
Packit c04fcb
		__FILE__, __func__, __LINE__, ## args)
Packit c04fcb
#else
Packit c04fcb
#define dbg_printf(fmt, args...)				\
Packit c04fcb
do {								\
Packit c04fcb
	/* do nothing but check printf format */		\
Packit c04fcb
	if (0)							\
Packit c04fcb
		printf("[debug bytecode in %s:%s@%u] " fmt,	\
Packit c04fcb
			__FILE__, __func__, __LINE__, ## args);	\
Packit c04fcb
} while (0)
Packit c04fcb
#endif
Packit c04fcb
Packit c04fcb
/* Linked bytecode. Child of struct lttng_bytecode_runtime. */
Packit c04fcb
struct bytecode_runtime {
Packit c04fcb
	struct lttng_bytecode_runtime p;
Packit c04fcb
	uint16_t len;
Packit c04fcb
	char data[0];
Packit c04fcb
};
Packit c04fcb
Packit c04fcb
enum entry_type {
Packit c04fcb
	REG_S64,
Packit c04fcb
	REG_DOUBLE,
Packit c04fcb
	REG_STRING,
Packit c04fcb
	REG_UNKNOWN,
Packit c04fcb
};
Packit c04fcb
Packit c04fcb
/* Validation stack */
Packit c04fcb
struct vstack_entry {
Packit c04fcb
	enum entry_type type;
Packit c04fcb
};
Packit c04fcb
Packit c04fcb
struct vstack {
Packit c04fcb
	int top;	/* top of stack */
Packit c04fcb
	struct vstack_entry e[FILTER_STACK_LEN];
Packit c04fcb
};
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
void vstack_init(struct vstack *stack)
Packit c04fcb
{
Packit c04fcb
	stack->top = -1;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
struct vstack_entry *vstack_ax(struct vstack *stack)
Packit c04fcb
{
Packit c04fcb
	if (unlikely(stack->top < 0))
Packit c04fcb
		return NULL;
Packit c04fcb
	return &stack->e[stack->top];
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
struct vstack_entry *vstack_bx(struct vstack *stack)
Packit c04fcb
{
Packit c04fcb
	if (unlikely(stack->top < 1))
Packit c04fcb
		return NULL;
Packit c04fcb
	return &stack->e[stack->top - 1];
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
int vstack_push(struct vstack *stack)
Packit c04fcb
{
Packit c04fcb
	if (stack->top >= FILTER_STACK_LEN - 1) {
Packit c04fcb
		ERR("Stack full\n");
Packit c04fcb
		return -EINVAL;
Packit c04fcb
	}
Packit c04fcb
	++stack->top;
Packit c04fcb
	return 0;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
int vstack_pop(struct vstack *stack)
Packit c04fcb
{
Packit c04fcb
	if (unlikely(stack->top < 0)) {
Packit c04fcb
		ERR("Stack empty\n");
Packit c04fcb
		return -EINVAL;
Packit c04fcb
	}
Packit c04fcb
	stack->top--;
Packit c04fcb
	return 0;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/* Execution stack */
Packit c04fcb
struct estack_entry {
Packit c04fcb
	enum entry_type type;	/* For dynamic typing. */
Packit c04fcb
	union {
Packit c04fcb
		int64_t v;
Packit c04fcb
		double d;
Packit c04fcb
Packit c04fcb
		struct {
Packit c04fcb
			const char *str;
Packit c04fcb
			size_t seq_len;
Packit c04fcb
			int literal;		/* is string literal ? */
Packit c04fcb
		} s;
Packit c04fcb
	} u;
Packit c04fcb
};
Packit c04fcb
Packit c04fcb
struct estack {
Packit c04fcb
	int top;	/* top of stack */
Packit c04fcb
	struct estack_entry e[FILTER_STACK_LEN];
Packit c04fcb
};
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Always use aliased type for ax/bx (top of stack).
Packit c04fcb
 * When ax/bx are S64, use aliased value.
Packit c04fcb
 */
Packit c04fcb
#define estack_ax_v	ax
Packit c04fcb
#define estack_bx_v	bx
Packit c04fcb
#define estack_ax_t	ax_t
Packit c04fcb
#define estack_bx_t	bx_t
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * ax and bx registers can hold either integer, double or string.
Packit c04fcb
 */
Packit c04fcb
#define estack_ax(stack, top)					\
Packit c04fcb
	({							\
Packit c04fcb
		assert((top) > FILTER_STACK_EMPTY);		\
Packit c04fcb
		&(stack)->e[top];				\
Packit c04fcb
	})
Packit c04fcb
Packit c04fcb
#define estack_bx(stack, top)					\
Packit c04fcb
	({							\
Packit c04fcb
		assert((top) > FILTER_STACK_EMPTY + 1);		\
Packit c04fcb
		&(stack)->e[(top) - 1];				\
Packit c04fcb
	})
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Currently, only integers (REG_S64) can be pushed into the stack.
Packit c04fcb
 */
Packit c04fcb
#define estack_push(stack, top, ax, bx, ax_t, bx_t)		\
Packit c04fcb
	do {							\
Packit c04fcb
		assert((top) < FILTER_STACK_LEN - 1);		\
Packit c04fcb
		(stack)->e[(top) - 1].u.v = (bx);		\
Packit c04fcb
		(stack)->e[(top) - 1].type = (bx_t);		\
Packit c04fcb
		(bx) = (ax);					\
Packit c04fcb
		(bx_t) = (ax_t);				\
Packit c04fcb
		++(top);					\
Packit c04fcb
	} while (0)
Packit c04fcb
Packit c04fcb
#define estack_pop(stack, top, ax, bx, ax_t, bx_t)		\
Packit c04fcb
	do {							\
Packit c04fcb
		assert((top) > FILTER_STACK_EMPTY);		\
Packit c04fcb
		(ax) = (bx);					\
Packit c04fcb
		(ax_t) = (bx_t);				\
Packit c04fcb
		(bx) = (stack)->e[(top) - 2].u.v;		\
Packit c04fcb
		(bx_t) = (stack)->e[(top) - 2].type;		\
Packit c04fcb
		(top)--;					\
Packit c04fcb
	} while (0)
Packit c04fcb
Packit c04fcb
const char *print_op(enum filter_op op);
Packit c04fcb
Packit c04fcb
int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode);
Packit c04fcb
int lttng_filter_specialize_bytecode(struct bytecode_runtime *bytecode);
Packit c04fcb
Packit c04fcb
uint64_t lttng_filter_false(void *filter_data,
Packit c04fcb
		const char *filter_stack_data);
Packit c04fcb
uint64_t lttng_filter_interpret_bytecode(void *filter_data,
Packit c04fcb
		const char *filter_stack_data);
Packit c04fcb
Packit c04fcb
#endif /* _LTTNG_FILTER_H */