• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 
19 #include "perfetto/base/logging.h"
20 #include "perfetto/ext/base/scoped_file.h"
21 #include "perfetto/profiling/deobfuscator.h"
22 #include "perfetto/trace_processor/trace_processor.h"
23 #include "tools/trace_to_text/deobfuscate_profile.h"
24 #include "tools/trace_to_text/utils.h"
25 
26 namespace perfetto {
27 namespace trace_to_text {
28 namespace {
29 
ParseFile(profiling::ProguardParser * p,FILE * f)30 bool ParseFile(profiling::ProguardParser* p, FILE* f) {
31   std::vector<std::string> lines;
32   size_t n = 0;
33   char* line = nullptr;
34   ssize_t rd = 0;
35   bool success = true;
36   do {
37     rd = getline(&line, &n, f);
38     // Do not read empty line that terminates the output.
39     if (rd > 1) {
40       // Remove newline character.
41       PERFETTO_DCHECK(line[rd - 1] == '\n');
42       line[rd - 1] = '\0';
43       success = p->AddLine(line);
44     }
45   } while (rd > 1 && success);
46   free(line);
47   return success;
48 }
49 }  // namespace
50 
DeobfuscateProfile(std::istream * input,std::ostream * output)51 int DeobfuscateProfile(std::istream* input, std::ostream* output) {
52   base::ignore_result(input);
53   base::ignore_result(output);
54   auto maybe_map = GetPerfettoProguardMapPath();
55   if (!maybe_map) {
56     PERFETTO_ELOG("No PERFETTO_PROGUARD_MAP specified.");
57     return 1;
58   }
59   base::ScopedFstream f(fopen(maybe_map->c_str(), "r"));
60   if (!f) {
61     PERFETTO_ELOG("Failed to open %s", maybe_map->c_str());
62     return 1;
63   }
64   profiling::ProguardParser parser;
65   if (!ParseFile(&parser, *f)) {
66     PERFETTO_ELOG("Failed to parse %s", maybe_map->c_str());
67     return 1;
68   }
69   std::map<std::string, profiling::ObfuscatedClass> obfuscation_map =
70       parser.ConsumeMapping();
71 
72   trace_processor::Config config;
73   std::unique_ptr<trace_processor::TraceProcessor> tp =
74       trace_processor::TraceProcessor::CreateInstance(config);
75 
76   if (!ReadTrace(tp.get(), input))
77     PERFETTO_FATAL("Failed to read trace.");
78 
79   tp->NotifyEndOfFile();
80   DeobfuscateDatabase(tp.get(), obfuscation_map,
81                       [output](const std::string& packet_proto) {
82                         WriteTracePacket(packet_proto, output);
83                       });
84   return 0;
85 }
86 
87 }  // namespace trace_to_text
88 }  // namespace perfetto
89