filter.c 4.07 KB
Newer Older
Petr Machata's avatar
Petr Machata committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * This file is part of ltrace.
 * Copyright (C) 2012 Petr Machata, Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

21
#include <stdlib.h>
Petr Machata's avatar
Petr Machata committed
22
#include <assert.h>
23 24
#include <stdio.h>
#include <string.h>
Petr Machata's avatar
Petr Machata committed
25 26 27

#include "filter.h"
#include "library.h"
28
#include "callback.h"
Petr Machata's avatar
Petr Machata committed
29 30 31 32 33

void
filter_init(struct filter *filt)
{
	filt->rules = NULL;
Petr Machata's avatar
Petr Machata committed
34
	filt->next = NULL;
Petr Machata's avatar
Petr Machata committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
}

void
filter_destroy(struct filter *filt)
{
	struct filter_rule *it;
	for (it = filt->rules; it != NULL; ) {
		struct filter_rule *next = it->next;
		filter_rule_destroy(it);
		it = next;
	}
}

void
filter_rule_init(struct filter_rule *rule, enum filter_rule_type type,
		 struct filter_lib_matcher *matcher,
		 regex_t symbol_re)
{
	rule->type = type;
	rule->lib_matcher = matcher;
	rule->symbol_re = symbol_re;
	rule->next = NULL;
}

void
filter_rule_destroy(struct filter_rule *rule)
{
	filter_lib_matcher_destroy(rule->lib_matcher);
	regfree(&rule->symbol_re);
}

void
filter_add_rule(struct filter *filt, struct filter_rule *rule)
{
69 70 71 72
	struct filter_rule **rulep;
	for (rulep = &filt->rules; *rulep != NULL; rulep = &(*rulep)->next)
		;
	*rulep = rule;
Petr Machata's avatar
Petr Machata committed
73 74 75 76
}

void
filter_lib_matcher_name_init(struct filter_lib_matcher *matcher,
77
			     enum filter_lib_matcher_type type,
Petr Machata's avatar
Petr Machata committed
78 79
			     regex_t libname_re)
{
80 81 82 83 84 85 86 87 88 89
	switch (type) {
	case FLM_MAIN:
		assert(type != type);
		abort();

	case FLM_SONAME:
	case FLM_PATHNAME:
		matcher->type = type;
		matcher->libname_re = libname_re;
	}
Petr Machata's avatar
Petr Machata committed
90 91 92 93 94 95 96 97 98 99 100 101
}

void
filter_lib_matcher_main_init(struct filter_lib_matcher *matcher)
{
	matcher->type = FLM_MAIN;
}

void
filter_lib_matcher_destroy(struct filter_lib_matcher *matcher)
{
	switch (matcher->type) {
102 103
	case FLM_SONAME:
	case FLM_PATHNAME:
Petr Machata's avatar
Petr Machata committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
		regfree(&matcher->libname_re);
		break;
	case FLM_MAIN:
		break;
	}
}

static int
re_match_or_error(regex_t *re, const char *name, const char *what)
{
	int status = regexec(re, name, 0, NULL, 0);
	if (status == 0)
		return 1;
	if (status == REG_NOMATCH)
		return 0;

	char buf[200];
	regerror(status, re, buf, sizeof buf);
122
	fprintf(stderr, "Error when matching %s: %s\n", name, buf);
Petr Machata's avatar
Petr Machata committed
123 124 125 126 127 128 129 130

	return 0;
}

static int
matcher_matches_library(struct filter_lib_matcher *matcher, struct library *lib)
{
	switch (matcher->type) {
131 132 133 134 135 136
	case FLM_SONAME:
		return re_match_or_error(&matcher->libname_re, lib->soname,
					 "library soname");
	case FLM_PATHNAME:
		return re_match_or_error(&matcher->libname_re, lib->pathname,
					 "library pathname");
Petr Machata's avatar
Petr Machata committed
137
	case FLM_MAIN:
138
		return lib->type == LT_LIBTYPE_MAIN;
Petr Machata's avatar
Petr Machata committed
139 140 141 142 143 144 145 146
	}
	assert(matcher->type != matcher->type);
	abort();
}

int
filter_matches_library(struct filter *filt, struct library *lib)
{
Petr Machata's avatar
Petr Machata committed
147 148 149
	if (filt == NULL)
		return 0;

150 151 152 153 154 155 156 157 158 159 160
	for (; filt != NULL; filt = filt->next) {
		struct filter_rule *it;
		for (it = filt->rules; it != NULL; it = it->next)
			switch (it->type) {
			case FR_ADD:
				if (matcher_matches_library(it->lib_matcher, lib))
					return 1;
			case FR_SUBTRACT:
				continue;
			};
	}
Petr Machata's avatar
Petr Machata committed
161 162 163 164
	return 0;
}

int
165 166
filter_matches_symbol(struct filter *filt,
		      const char *sym_name, struct library *lib)
Petr Machata's avatar
Petr Machata committed
167
{
Petr Machata's avatar
Petr Machata committed
168
	for (; filt != NULL; filt = filt->next) {
169
		int matches = 0;
Petr Machata's avatar
Petr Machata committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
		struct filter_rule *it;
		for (it = filt->rules; it != NULL; it = it->next) {
			switch (it->type) {
			case FR_ADD:
				if (matches)
					continue;
				break;
			case FR_SUBTRACT:
				if (!matches)
					continue;
			}

			if (matcher_matches_library(it->lib_matcher, lib)
			    && re_match_or_error(&it->symbol_re, sym_name,
						 "symbol name"))
				matches = !matches;
Petr Machata's avatar
Petr Machata committed
186
		}
187 188
		if (matches)
			return 1;
Petr Machata's avatar
Petr Machata committed
189
	}
190
	return 0;
Petr Machata's avatar
Petr Machata committed
191
}