strace/filter.c
Nikolay Marchuk 2de045936f Implement new filtering language parsing
* basic_filters.c (lookup_class, qualify_syscall_class, qualify_syscall,
qualify_syscall_tokens, parse_syscall_filter, qualify_tokens,
parse_fd_filter, parse_path_filter): Add qualify_mode argument.
(qualify_tokens, qualify_syscall_tokens): Use set inversion only in
qualify mode.
(lookup_class): Use deprecated class names only in qualify mode.
* defs.h (qualify): Remove declaration.
(filtering_parse): Add new declaration.
* filter.c (struct filter_type, parse_filter):
Add bool argument to parse_*_filter declarations.
* filter.h (parse_filter_action, parse_qualify_action,
parse_filter_expression): Add new declarations.
(parse_filter, qualify_tokens, qualify_syscall_tokens):
Add qualify_mode argument.
(DECL_FILTER): Add bool argument to parse_*_filter declarations.
* filter_action.c (parse_filter_action): Add new parsing function.
(inject_path_tracing): Use filtering_parse instead of qualify.
* filter_expression.c (parse_filter_expression): Implement parsing of filter
expression.
(parse_operator, push_operator, is_higher_priority): Add helper functions.
(is_space_ascii, is_allowed_in_name): Add new declarations.
* filter_parse.c: New file.
* filter_qualify.c (qualify_read, qualify_write, qualify_signals,
qualify_trace, qualify_abbrev, qualify_verbose, qualify_raw,
qualify_inject_common, qualify_fault, qualify_inject): Use main_part and args
arguments.
* strace.c (init): Use filtering_parse instead of qualify.
* Makefile.am (strace_SOURCES): Add filter_parse.c.
2017-12-21 13:12:36 +00:00

131 lines
3.7 KiB
C

/*
* Copyright (c) 2017 Nikolay Marchuk <marchuk.nikolay.a@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "defs.h"
#include "filter.h"
#define FILTER_TYPE(name) \
{#name, parse_ ## name ## _filter, run_ ## name ## _filter, \
free_ ## name ## _filter}
/* End of FILTER_TYPE definition. */
static const struct filter_type {
const char *name;
void *(*parse_filter)(const char *, bool);
bool (*run_filter)(struct tcb *, void *);
void (*free_priv_data)(void *);
} filter_types[] = {
FILTER_TYPE(syscall),
FILTER_TYPE(fd),
FILTER_TYPE(path),
};
#undef FILTER_TYPE
struct filter {
const struct filter_type *type;
void *priv_data;
};
static const struct filter_type *
lookup_filter_type(const char *str)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(filter_types); i++) {
if (!strcmp(filter_types[i].name, str))
return &filter_types[i];
}
return NULL;
}
struct filter *
add_filter_to_array(struct filter **filters, unsigned int *nfilters,
const char *name)
{
const struct filter_type *type = lookup_filter_type(name);
struct filter *filter;
if (!type)
error_msg_and_die("invalid filter '%s'", name);
*filters = xreallocarray(*filters, ++(*nfilters),
sizeof(struct filter));
filter = &((*filters)[*nfilters - 1]);
filter->type = type;
return filter;
}
void
parse_filter(struct filter *filter, const char *str, bool qualify_mode)
{
filter->priv_data = filter->type->parse_filter(str, qualify_mode);
}
static bool
run_filter(struct tcb *tcp, struct filter *filter)
{
return filter->type->run_filter(tcp, filter->priv_data);
}
void
run_filters(struct tcb *tcp, struct filter *filters, unsigned int nfilters,
bool *variables_buf)
{
unsigned int i;
for (i = 0; i < nfilters; ++i)
variables_buf[i] = run_filter(tcp, &filters[i]);
}
void
free_filter(struct filter *filter)
{
if (!filter)
return;
filter->type->free_priv_data(filter->priv_data);
}
void
set_filters_qualify_mode(struct filter **filters, unsigned int *nfilters,
unsigned int filters_left)
{
unsigned int i;
for (i = 0; i < *nfilters - filters_left; ++i)
free_filter(*filters + i);
for (i = 0; i < filters_left; ++i)
(*filters)[i] = (*filters)[*nfilters - filters_left + i];
*filters = xreallocarray(*filters, filters_left, sizeof(struct filter));
*nfilters = filters_left;
}
void
set_filter_priv_data(struct filter *filter, void *priv_data)
{
if (filter)
filter->priv_data = priv_data;
}