2 * This file is part of ltrace.
3 * Copyright (C) 2012 Petr Machata, Red Hat Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
31 filter_init(struct filter *filt)
38 filter_destroy(struct filter *filt)
40 struct filter_rule *it;
41 for (it = filt->rules; it != NULL; ) {
42 struct filter_rule *next = it->next;
43 filter_rule_destroy(it);
49 filter_rule_init(struct filter_rule *rule, enum filter_rule_type type,
50 struct filter_lib_matcher *matcher,
54 rule->lib_matcher = matcher;
55 rule->symbol_re = symbol_re;
60 filter_rule_destroy(struct filter_rule *rule)
62 filter_lib_matcher_destroy(rule->lib_matcher);
63 regfree(&rule->symbol_re);
67 filter_add_rule(struct filter *filt, struct filter_rule *rule)
69 struct filter_rule **rulep;
70 for (rulep = &filt->rules; *rulep != NULL; rulep = &(*rulep)->next)
76 filter_lib_matcher_name_init(struct filter_lib_matcher *matcher,
77 enum filter_lib_matcher_type type,
88 matcher->libname_re = libname_re;
93 filter_lib_matcher_main_init(struct filter_lib_matcher *matcher)
95 matcher->type = FLM_MAIN;
99 filter_lib_matcher_destroy(struct filter_lib_matcher *matcher)
101 switch (matcher->type) {
104 regfree(&matcher->libname_re);
112 re_match_or_error(regex_t *re, const char *name, const char *what)
114 int status = regexec(re, name, 0, NULL, 0);
117 if (status == REG_NOMATCH)
121 regerror(status, re, buf, sizeof buf);
122 fprintf(stderr, "Error when matching %s: %s\n", name, buf);
128 matcher_matches_library(struct filter_lib_matcher *matcher, struct library *lib)
130 switch (matcher->type) {
132 return re_match_or_error(&matcher->libname_re, lib->soname,
135 return re_match_or_error(&matcher->libname_re, lib->pathname,
138 return lib->type == LT_LIBTYPE_MAIN;
140 assert(matcher->type != matcher->type);
145 filter_matches_library(struct filter *filt, struct library *lib)
150 for (; filt != NULL; filt = filt->next) {
151 struct filter_rule *it;
152 for (it = filt->rules; it != NULL; it = it->next)
155 if (matcher_matches_library(it->lib_matcher, lib))
165 filter_matches_symbol(struct filter *filt,
166 const char *sym_name, struct library *lib)
168 for (; filt != NULL; filt = filt->next) {
170 struct filter_rule *it;
171 for (it = filt->rules; it != NULL; it = it->next) {
182 if (matcher_matches_library(it->lib_matcher, lib)
183 && re_match_or_error(&it->symbol_re, sym_name,