blob: 8306ded446aa4672aa947db2ff1f43782b7cd035 [file] [log] [blame]
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "perfetto/profiling/deobfuscator.h"
#include "perfetto/ext/base/string_splitter.h"
#include "perfetto/ext/base/optional.h"
namespace perfetto {
namespace profiling {
namespace {
base::Optional<std::pair<std::string, std::string>> ParseClass(
std::string line) {
base::StringSplitter ss(std::move(line), ' ');
if (!ss.Next()) {
PERFETTO_ELOG("Missing deobfuscated name.");
return base::nullopt;
}
std::string deobfuscated_name(ss.cur_token(), ss.cur_token_size());
if (!ss.Next() || ss.cur_token_size() != 2 ||
strncmp("->", ss.cur_token(), 2) != 0) {
PERFETTO_ELOG("Missing ->");
return base::nullopt;
}
if (!ss.Next()) {
PERFETTO_ELOG("Missing obfuscated name.");
return base::nullopt;
}
std::string obfuscated_name(ss.cur_token(), ss.cur_token_size());
if (obfuscated_name.size() == 0) {
PERFETTO_ELOG("Empty obfuscated name.");
return base::nullopt;
}
if (obfuscated_name.back() != ':') {
PERFETTO_ELOG("Expected colon.");
return base::nullopt;
}
obfuscated_name.resize(obfuscated_name.size() - 1);
if (ss.Next()) {
PERFETTO_ELOG("Unexpected data.");
return base::nullopt;
}
return std::make_pair(std::move(obfuscated_name),
std::move(deobfuscated_name));
}
base::Optional<std::pair<std::string, std::string>> ParseMember(
std::string line) {
base::StringSplitter ss(std::move(line), ' ');
if (!ss.Next()) {
PERFETTO_ELOG("Missing type name.");
return base::nullopt;
}
std::string type_name(ss.cur_token(), ss.cur_token_size());
if (!ss.Next()) {
PERFETTO_ELOG("Missing deobfuscated name.");
return base::nullopt;
}
std::string deobfuscated_name(ss.cur_token(), ss.cur_token_size());
if (!ss.Next() || ss.cur_token_size() != 2 ||
strncmp("->", ss.cur_token(), 2) != 0) {
PERFETTO_ELOG("Missing ->");
return base::nullopt;
}
if (!ss.Next()) {
PERFETTO_ELOG("Missing obfuscated name.");
return base::nullopt;
}
std::string obfuscated_name(ss.cur_token(), ss.cur_token_size());
if (ss.Next()) {
PERFETTO_ELOG("Unexpected data.");
return base::nullopt;
}
return std::make_pair(std::move(obfuscated_name),
std::move(deobfuscated_name));
}
} // namespace
bool ProguardParser::AddLine(std::string line) {
if (line.length() == 0)
return true;
bool is_member = line[0] == ' ';
if (is_member && !current_class_) {
PERFETTO_ELOG("Failed to parse proguard map. Saw member before class.");
return false;
}
if (!is_member) {
std::string obfuscated_name;
std::string deobfuscated_name;
auto opt_pair = ParseClass(std::move(line));
if (!opt_pair)
return false;
std::tie(obfuscated_name, deobfuscated_name) = *opt_pair;
auto p = mapping_.emplace(std::move(obfuscated_name),
std::move(deobfuscated_name));
if (!p.second)
return false;
current_class_ = &p.first->second;
} else {
// TODO(fmayer): Teach this to properly parse methods.
std::string obfuscated_name;
std::string deobfuscated_name;
auto opt_pair = ParseMember(std::move(line));
if (!opt_pair)
return false;
std::tie(obfuscated_name, deobfuscated_name) = *opt_pair;
auto p = current_class_->deobfuscated_fields.emplace(
std::move(obfuscated_name), std::move(deobfuscated_name));
if (!p.second)
return false;
}
return true;
}
} // namespace profiling
} // namespace perfetto