libcsdbg  1.28
C++ exception (and generic) stack trace debug library
filter.cpp
Go to the documentation of this file.
1 #include "../include/filter.hpp"
2 #include "../include/util.hpp"
3 
10 namespace csdbg {
11 
18 {
19 }
20 
21 
27 inline filter* filter::clone() const
28 {
29  return NULL;
30 }
31 
32 
40 inline filter& filter::operator=(const filter &rval)
41 {
42  return *this;
43 }
44 
45 
57 filter::filter(const i8 *expr, bool icase, bool mode):
58 m_mode(mode)
59 {
60  util::memset(&m_expr, 0, sizeof(regex_t));
61  set_expr(expr, icase);
62 }
63 
64 
69 {
70  regfree(&m_expr);
71 }
72 
73 
79 inline bool filter::mode() const
80 {
81  return m_mode;
82 }
83 
84 
96 filter& filter::set_expr(const i8 *expr, bool icase)
97 {
98  if ( unlikely(expr == NULL) )
99  throw exception("invalid argument: expr (=%p)", expr);
100 
101  regfree(&m_expr);
102  i32 flags = REG_EXTENDED | REG_NOSUB;
103  if ( unlikely(icase) )
104  flags |= REG_ICASE;
105 
106  /* Compile the regular expression */
107  i32 retval = regcomp(&m_expr, expr, flags);
108  if ( likely(retval == 0) )
109  return *this;
110 
111  /* If the expression compilation failed */
112  i32 len = regerror(retval, &m_expr, NULL, 0);
113  i8 errbuf[len];
114  regerror(retval, &m_expr, errbuf, len);
115  regfree(&m_expr);
116 
117  throw exception(
118  "failed to compile filter '%s' (regex errno %d - %s)",
119  expr,
120  retval,
121  errbuf
122  );
123 }
124 
125 
133 inline filter& filter::set_mode(bool mode)
134 {
135  m_mode = mode;
136  return *this;
137 }
138 
139 
147 inline bool filter::apply(const i8 *target) const
148 {
149  __D_ASSERT(target != NULL);
150  if ( unlikely(target == NULL) )
151  return false;
152 
153  return !regexec(&m_expr, target, 0, NULL, 0);
154 }
155 
156 }
157 
static void * memset(void *, u8, u32)
Fill a memory block with a constant byte.
Definition: util.cpp:320
char i8
8-bit signed integer
Definition: config.hpp:72
filter(const filter &)
Object copy constructor.
Definition: filter.cpp:17
virtual ~filter()
Object destructor.
Definition: filter.cpp:68
virtual filter & set_expr(const i8 *, bool)
Set the filter expression.
Definition: filter.cpp:96
#define likely(expr)
Offer a hint (positive) to the pipeline branch predictor.
Definition: config.hpp:344
bool m_mode
Filter type switch.
Definition: filter.hpp:31
virtual bool mode() const
Get the filter type.
Definition: filter.cpp:79
virtual filter & operator=(const filter &)
Assignment operator.
Definition: filter.cpp:40
virtual bool apply(const i8 *) const
Apply the filter to a function signature or a module absolute path.
Definition: filter.cpp:147
virtual filter * clone() const
Object virtual copy constructor.
Definition: filter.cpp:27
regex_t m_expr
Filter expression.
Definition: filter.hpp:29
Instrumentation filter.
Definition: filter.hpp:23
int i32
32-bit signed integer
Definition: config.hpp:82
virtual filter & set_mode(bool)
Set the filter type.
Definition: filter.cpp:133
This class is a throwable with a textual description of an error.
Definition: exception.hpp:26
#define unlikely(expr)
Offer a hint (negative) to the pipeline branch predictor.
Definition: config.hpp:349
#define __D_ASSERT(x)
Assertion macro.
Definition: config.hpp:268