summaryrefslogtreecommitdiff
path: root/src/maintenance/main.cc
blob: 8b66069f823e5421f95910c22d7cbd772c4eb3a6 (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// 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 "common/debug.h"
#include "compiler/compiler.h"
#include "maintenance/controller.h"
#include "db/clean_up.h"

#include <android-base/parseint.h>
#include <android-base/properties.h>
#include <android-base/logging.h>

#include <iostream>
#include <optional>

#if defined(IORAP_MAINTENANCE_MAIN)

namespace iorap::maintenance {

void Usage(char** argv) {
  std::cerr << "Usage: " << argv[0] << " <path of sqlite db>" << std::endl;
  std::cerr << "" << std::endl;
  std::cerr << "  Compile the perfetto trace for an package and activity." << std::endl;
  std::cerr << "  The info of perfetto trace is stored in the sqlite db." << std::endl;
  std::cerr << "" << std::endl;
  std::cerr << "  Optional flags:" << std::endl;
  std::cerr << "    --package $,-p $           Package name." << std::endl;
  std::cerr << "    --version $,-ve $          Package version." << std::endl;
  std::cerr << "    --activity $,-a $          Activity name." << std::endl;
  std::cerr << "    --inode-textcache $,-it $  Resolve inode->filename from textcache." << std::endl;
  std::cerr << "    --help,-h                  Print this Usage." << std::endl;
  std::cerr << "    --recompile,-r             Force re-compilation, which replace the existing compiled trace ." << std::endl;
  std::cerr << "    --purge-package,-pp        Purge all files associated with a package." << std::endl;
  std::cerr << "    --verbose,-v               Set verbosity (default off)." << std::endl;
  std::cerr << "    --output-text,-ot          Output ascii text instead of protobuf (default off)." << std::endl;
  std::cerr << "    --min_traces,-mt           The min number of perfetto traces needed "
            << "for compilation (default 1)." << std::endl;
  std::cerr << "    --exclude-dex-files,-edf   Set of exclude dex files" << std::endl;
  exit(1);
}


int Main(int argc, char** argv){
  android::base::InitLogging(argv);
  android::base::SetLogger(android::base::StderrLogger);

  if (argc == 1) {
    // Need at least 1 input file to do anything.
    Usage(argv);
  }

  std::vector<std::string> arg_input_filenames;
  std::optional<std::string> arg_package;
  std::optional<std::string> arg_purge_package;
  int arg_version = -1;
  std::optional<std::string> arg_activity;
  std::optional<std::string> arg_inode_textcache;
  bool recompile = false;
  bool enable_verbose = false;
  bool arg_output_text = false;
  uint64_t arg_min_traces = 1;
  bool exclude_dex_files = false;

  for (int arg = 1; arg < argc; ++arg) {
    std::string argstr = argv[arg];
    bool has_arg_next = (arg+1)<argc;
    std::string arg_next = has_arg_next ? argv[arg+1] : "";

    if (argstr == "--help" || argstr == "-h") {
      Usage(argv);
    } else if (argstr == "--package" || argstr == "-p") {
      if (!has_arg_next) {
        std::cerr << "Missing --package <value>" << std::endl;
        return 1;
      }
      arg_package = arg_next;
      ++arg;
    } else if (argstr == "--version" || argstr == "-ve") {
      if (!has_arg_next) {
        std::cerr << "Missing --version <value>" << std::endl;
        return 1;
      }
      int version;
      if (!android::base::ParseInt<int>(arg_next, &version)) {
        std::cerr << "Invalid --version " << arg_next << std::endl;
        return 1;
      }
      arg_version = version;
      ++arg;
    } else if (argstr == "--activity" || argstr == "-a") {
      if (!has_arg_next) {
        std::cerr << "Missing --activity <value>" << std::endl;
        return 1;
      }
      arg_activity = arg_next;
      ++arg;
    } else if (argstr == "--inode-textcache" || argstr == "-it") {
      if (!has_arg_next) {
        std::cerr << "Missing --inode-textcache <value>" << std::endl;
        return 1;
      }
      arg_inode_textcache = arg_next;
      ++arg;
    } else if (argstr == "--purge-package" || argstr == "-pp") {
      if (!has_arg_next) {
        std::cerr << "Missing --purge-package <value>" << std::endl;
        return 1;
      }
      arg_purge_package = arg_next;
      ++arg;
    } else if (argstr == "--verbose" || argstr == "-v") {
      enable_verbose = true;
    } else if (argstr == "--recompile" || argstr == "-r") {
      recompile = true;
    } else if (argstr == "--output-text" || argstr == "-ot") {
      arg_output_text = true;
    } else if (argstr == "--min_traces" || argstr == "-mt") {
      if (!has_arg_next) {
        std::cerr << "Missing --min_traces <value>" << std::endl;
        return 1;
      }
      arg_min_traces = std::stoul(arg_next);
      ++arg;
    } else if (argstr == "--exclude-dex-files" || argstr == "-edf") {
      exclude_dex_files = true;
    } else {
      arg_input_filenames.push_back(argstr);
    }
  }

  if (arg_input_filenames.empty()) {
    LOG(ERROR) << "Missing filename to a sqlite database.";
    Usage(argv);
  } else if (arg_input_filenames.size() > 1) {
    LOG(ERROR) << "More than one filename to a sqlite database.";
    Usage(argv);
  }

  std::string db_path = arg_input_filenames[0];

  if (enable_verbose) {
    android::base::SetMinimumLogSeverity(android::base::VERBOSE);

    LOG(VERBOSE) << "Verbose check";
    LOG(VERBOSE) << "Debug check: " << ::iorap::kIsDebugBuild;
  } else {
    android::base::SetMinimumLogSeverity(android::base::DEBUG);
  }

  if (arg_purge_package) {
    db::CleanUpFilesForPackage(db_path, *arg_purge_package);
    return 0;
    // Don't do any more work because SchemaModel can only be created once.
  }

  maintenance::ControllerParameters params{
    arg_output_text,
    arg_inode_textcache,
    enable_verbose,
    recompile,
    arg_min_traces,
    std::make_shared<Exec>(),
    exclude_dex_files};

  int ret_code = 0;
  if (arg_package && arg_activity) {
    ret_code = !Compile(std::move(db_path),
                        std::move(*arg_package),
                        std::move(*arg_activity),
                        arg_version,
                        params);
  } else if (arg_package) {
    ret_code = !Compile(std::move(db_path), std::move(*arg_package), arg_version, params);
  } else {
    ret_code = !Compile(std::move(db_path), params);
  }
  return ret_code;
}

} // iorap::maintenance

int main(int argc, char** argv) {
  return ::iorap::maintenance::Main(argc, argv);
}


#endif  // IORAP_MAINTENANCE_MAIN