Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
d64b721
Documentation
arch
block
certs
crypto
drivers
firmware
fs
include
init
ipc
kernel
lib
mm
net
samples
scripts
security
sound
tools
accounting
arch
build
cgroup
firewire
gpio
hv
iio
include
kvm
laptop
leds
lguest
lib
net
nfsd
objtool
pcmcia
perf
Documentation
arch
bench
jvmti
pmu-events
python
scripts
tests
trace
ui
util
c++
include
intel-pt-decoder
libunwind
scripting-engines
Build
PERF-VERSION-GEN
alias.c
annotate.c
annotate.h
auxtrace.c
auxtrace.h
block-range.c
block-range.h
bpf-loader.c
bpf-loader.h
bpf-prologue.c
bpf-prologue.h
build-id.c
build-id.h
cache.h
call-path.c
call-path.h
callchain.c
callchain.h
cgroup.c
cgroup.h
cloexec.c
cloexec.h
color.c
color.h
comm.c
comm.h
config.c
config.h
counts.c
counts.h
cpumap.c
cpumap.h
cs-etm.h
ctype.c
data-convert-bt.c
data-convert-bt.h
data-convert.h
data.c
data.h
db-export.c
db-export.h
debug.c
debug.h
demangle-java.c
demangle-java.h
demangle-rust.c
demangle-rust.h
drv_configs.c
drv_configs.h
dso.c
dso.h
dwarf-aux.c
dwarf-aux.h
dwarf-regs.c
env.c
env.h
event.c
event.h
evlist.c
evlist.h
evsel.c
evsel.h
evsel_fprintf.c
find-vdso-map.c
genelf.c
genelf.h
genelf_debug.c
generate-cmdlist.sh
group.h
header.c
header.h
help-unknown-cmd.c
help-unknown-cmd.h
hist.c
hist.h
intel-bts.c
intel-bts.h
intel-pt.c
intel-pt.h
intlist.c
intlist.h
jit.h
jitdump.c
jitdump.h
kvm-stat.h
levenshtein.c
levenshtein.h
llvm-utils.c
llvm-utils.h
lzma.c
machine.c
machine.h
map.c
map.h
mem-events.c
mem-events.h
ordered-events.c
ordered-events.h
parse-branch-options.c
parse-branch-options.h
parse-events.c
parse-events.h
parse-events.l
parse-events.y
parse-regs-options.c
parse-regs-options.h
path.c
perf-hooks-list.h
perf-hooks.c
perf-hooks.h
perf_regs.c
perf_regs.h
pmu.c
pmu.h
pmu.l
pmu.y
probe-event.c
probe-event.h
probe-file.c
probe-file.h
probe-finder.c
probe-finder.h
pstack.c
pstack.h
python-ext-sources
python.c
quote.c
quote.h
rb_resort.h
rblist.c
rblist.h
record.c
session.c
session.h
setup.py
sort.c
sort.h
srcline.c
stat-shadow.c
stat.c
stat.h
strbuf.c
strbuf.h
strfilter.c
strfilter.h
string.c
strlist.c
strlist.h
svghelper.c
svghelper.h
symbol-elf.c
symbol-minimal.c
symbol.c
symbol.h
symbol_fprintf.c
syscalltbl.c
syscalltbl.h
target.c
target.h
term.c
term.h
thread-stack.c
thread-stack.h
thread.c
thread.h
thread_map.c
thread_map.h
time-utils.c
time-utils.h
tool.h
top.c
top.h
trace-event-info.c
trace-event-parse.c
trace-event-read.c
trace-event-scripting.c
trace-event.c
trace-event.h
trigger.h
tsc.c
tsc.h
unwind-libdw.c
unwind-libdw.h
unwind-libunwind-local.c
unwind-libunwind.c
unwind.h
usage.c
util-cxx.h
util.c
util.h
values.c
values.h
vdso.c
vdso.h
xyarray.c
xyarray.h
zlib.c
.gitignore
Build
CREDITS
MANIFEST
Makefile
Makefile.config
Makefile.perf
builtin-annotate.c
builtin-bench.c
builtin-buildid-cache.c
builtin-buildid-list.c
builtin-c2c.c
builtin-config.c
builtin-data.c
builtin-diff.c
builtin-evlist.c
builtin-ftrace.c
builtin-help.c
builtin-inject.c
builtin-kallsyms.c
builtin-kmem.c
builtin-kvm.c
builtin-list.c
builtin-lock.c
builtin-mem.c
builtin-probe.c
builtin-record.c
builtin-report.c
builtin-sched.c
builtin-script.c
builtin-stat.c
builtin-timechart.c
builtin-top.c
builtin-trace.c
builtin-version.c
builtin.h
check-headers.sh
command-list.txt
design.txt
perf-archive.sh
perf-completion.sh
perf-read-vdso.c
perf-sys.h
perf-with-kcore.sh
perf.c
perf.h
power
scripts
spi
testing
thermal
time
usb
virtio
vm
Makefile
usr
virt
.cocciconfig
.get_maintainer.ignore
.gitattributes
.gitignore
.mailmap
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
Breadcrumbs
linux
/
tools
/
perf
/
util
/
strfilter.c
Blame
Blame
Latest commit
History
History
307 lines (267 loc) · 6.15 KB
Breadcrumbs
linux
/
tools
/
perf
/
util
/
strfilter.c
Top
File metadata and controls
Code
Blame
307 lines (267 loc) · 6.15 KB
Raw
#include "util.h" #include "string.h" #include "strfilter.h" /* Operators */ static const char *OP_and = "&"; /* Logical AND */ static const char *OP_or = "|"; /* Logical OR */ static const char *OP_not = "!"; /* Logical NOT */ #define is_operator(c) ((c) == '|' || (c) == '&' || (c) == '!') #define is_separator(c) (is_operator(c) || (c) == '(' || (c) == ')') static void strfilter_node__delete(struct strfilter_node *node) { if (node) { if (node->p && !is_operator(*node->p)) zfree((char **)&node->p); strfilter_node__delete(node->l); strfilter_node__delete(node->r); free(node); } } void strfilter__delete(struct strfilter *filter) { if (filter) { strfilter_node__delete(filter->root); free(filter); } } static const char *get_token(const char *s, const char **e) { const char *p; while (isspace(*s)) /* Skip spaces */ s++; if (*s == '\0') { p = s; goto end; } p = s + 1; if (!is_separator(*s)) { /* End search */ retry: while (*p && !is_separator(*p) && !isspace(*p)) p++; /* Escape and special case: '!' is also used in glob pattern */ if (*(p - 1) == '\\' || (*p == '!' && *(p - 1) == '[')) { p++; goto retry; } } end: *e = p; return s; } static struct strfilter_node *strfilter_node__alloc(const char *op, struct strfilter_node *l, struct strfilter_node *r) { struct strfilter_node *node = zalloc(sizeof(*node)); if (node) { node->p = op; node->l = l; node->r = r; } return node; } static struct strfilter_node *strfilter_node__new(const char *s, const char **ep) { struct strfilter_node root, *cur, *last_op; const char *e; if (!s) return NULL; memset(&root, 0, sizeof(root)); last_op = cur = &root; s = get_token(s, &e); while (*s != '\0' && *s != ')') { switch (*s) { case '&': /* Exchg last OP->r with AND */ if (!cur->r || !last_op->r) goto error; cur = strfilter_node__alloc(OP_and, last_op->r, NULL); if (!cur) goto nomem; last_op->r = cur; last_op = cur; break; case '|': /* Exchg the root with OR */ if (!cur->r || !root.r) goto error; cur = strfilter_node__alloc(OP_or, root.r, NULL); if (!cur) goto nomem; root.r = cur; last_op = cur; break; case '!': /* Add NOT as a leaf node */ if (cur->r) goto error; cur->r = strfilter_node__alloc(OP_not, NULL, NULL); if (!cur->r) goto nomem; cur = cur->r; break; case '(': /* Recursively parses inside the parenthesis */ if (cur->r) goto error; cur->r = strfilter_node__new(s + 1, &s); if (!s) goto nomem; if (!cur->r || *s != ')') goto error; e = s + 1; break; default: if (cur->r) goto error; cur->r = strfilter_node__alloc(NULL, NULL, NULL); if (!cur->r) goto nomem; cur->r->p = strndup(s, e - s); if (!cur->r->p) goto nomem; } s = get_token(e, &e); } if (!cur->r) goto error; *ep = s; return root.r; nomem: s = NULL; error: *ep = s; strfilter_node__delete(root.r); return NULL; } /* * Parse filter rule and return new strfilter. * Return NULL if fail, and *ep == NULL if memory allocation failed. */ struct strfilter *strfilter__new(const char *rules, const char **err) { struct strfilter *filter = zalloc(sizeof(*filter)); const char *ep = NULL; if (filter) filter->root = strfilter_node__new(rules, &ep); if (!filter || !filter->root || *ep != '\0') { if (err) *err = ep; strfilter__delete(filter); filter = NULL; } return filter; } static int strfilter__append(struct strfilter *filter, bool _or, const char *rules, const char **err) { struct strfilter_node *right, *root; const char *ep = NULL; if (!filter || !rules) return -EINVAL; right = strfilter_node__new(rules, &ep); if (!right || *ep != '\0') { if (err) *err = ep; goto error; } root = strfilter_node__alloc(_or ? OP_or : OP_and, filter->root, right); if (!root) { ep = NULL; goto error; } filter->root = root; return 0; error: strfilter_node__delete(right); return ep ? -EINVAL : -ENOMEM; } int strfilter__or(struct strfilter *filter, const char *rules, const char **err) { return strfilter__append(filter, true, rules, err); } int strfilter__and(struct strfilter *filter, const char *rules, const char **err) { return strfilter__append(filter, false, rules, err); } static bool strfilter_node__compare(struct strfilter_node *node, const char *str) { if (!node || !node->p) return false; switch (*node->p) { case '|': /* OR */ return strfilter_node__compare(node->l, str) || strfilter_node__compare(node->r, str); case '&': /* AND */ return strfilter_node__compare(node->l, str) && strfilter_node__compare(node->r, str); case '!': /* NOT */ return !strfilter_node__compare(node->r, str); default: return strglobmatch(str, node->p); } } /* Return true if STR matches the filter rules */ bool strfilter__compare(struct strfilter *filter, const char *str) { if (!filter) return false; return strfilter_node__compare(filter->root, str); } static int strfilter_node__sprint(struct strfilter_node *node, char *buf); /* sprint node in parenthesis if needed */ static int strfilter_node__sprint_pt(struct strfilter_node *node, char *buf) { int len; int pt = node->r ? 2 : 0; /* don't need to check node->l */ if (buf && pt) *buf++ = '('; len = strfilter_node__sprint(node, buf); if (len < 0) return len; if (buf && pt) *(buf + len) = ')'; return len + pt; } static int strfilter_node__sprint(struct strfilter_node *node, char *buf) { int len = 0, rlen; if (!node || !node->p) return -EINVAL; switch (*node->p) { case '|': case '&': len = strfilter_node__sprint_pt(node->l, buf); if (len < 0) return len; __fallthrough; case '!': if (buf) { *(buf + len++) = *node->p; buf += len; } else len++; rlen = strfilter_node__sprint_pt(node->r, buf); if (rlen < 0) return rlen; len += rlen; break; default: len = strlen(node->p); if (buf) strcpy(buf, node->p); } return len; } char *strfilter__string(struct strfilter *filter) { int len; char *ret = NULL; len = strfilter_node__sprint(filter->root, NULL); if (len < 0) return NULL; ret = malloc(len + 1); if (ret) strfilter_node__sprint(filter->root, ret); return ret; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
You can’t perform that action at this time.