Skip to content

Commit

Permalink
improve probes lookup by dropping use of regex
Browse files Browse the repository at this point in the history
Ref: #526
  • Loading branch information
mmarchini committed Apr 18, 2019
1 parent 0f636c9 commit 9c7f9e5
Showing 1 changed file with 52 additions and 13 deletions.
65 changes: 52 additions & 13 deletions src/bpftrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,64 @@ int BPFtrace::add_probe(ast::Probe &p)

std::set<std::string> BPFtrace::find_wildcard_matches(const std::string &prefix, const std::string &func, std::istream &symbol_name_stream)
{
// Turn glob into a regex
auto regex_str = "(" + std::regex_replace(func, std::regex("\\*"), "[^\\s]*") + ")";
if (prefix != "")
regex_str = prefix + ":" + regex_str;
regex_str = "^" + regex_str;
std::regex func_regex(regex_str);
std::smatch match;
bool start_wildcard = func[0] == '*';
bool end_wildcard = func[func.length() - 1] == '*';

// TODO(mmarchini): turn this into a helper function. Add tests
std::vector<std::string> tokens;
size_t next = start_wildcard ? 1 : 0;
size_t end = func.length();
while (next < end) {
size_t found = func.find('*', next);
std::string token;
if (found == end) {
token = func.substr(next, end - next);
next = end;
} else if (found == std::string::npos) {
token = func.substr(next, end - next);
next = end;
} else {
token = func.substr(next, found - next);
next = found + 1;
}
tokens.push_back(token);
}

std::string line;
std::set<std::string> matches;
while (std::getline(symbol_name_stream, line))
{
if (std::regex_search(line, match, func_regex))
{
assert(match.size() == 2);
// skip the ".part.N" kprobe variants, as they can't be traced:
if (std::strstr(match.str(1).c_str(), ".part.") == NULL)
matches.insert(match[1]);
size_t next = 0;
if (!prefix.empty()) {
if (line.find(prefix, next) != std::string::npos)
continue;
next += prefix.length();
}

if (!start_wildcard)
if (line.find(tokens[0], next) != next)
continue;

bool cool = true;
for (std::string token : tokens) {
size_t found = line.find(token, next);
if (found == std::string::npos) {
cool = false;
break;
}

next = found + token.length();
}
if (!cool) continue;

if (!end_wildcard)
if (line.length() != next)
continue;

if (line.find(".part.") != std::string::npos)
continue;

matches.insert(line);
}
return matches;
}
Expand Down

0 comments on commit 9c7f9e5

Please sign in to comment.