• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecmascript/dfx/hprof/rawheap_translate/metadata_parse.h"
17 #include "ecmascript/dfx/hprof/rawheap_translate/rawheap_translate.h"
18 #include "ecmascript/dfx/hprof/rawheap_translate/serializer.h"
19 
20 namespace rawheap_translate {
21 std::string RAWHEAP_TRANSLATE_HELPER =
22     "Usage: rawheap_translator <filename.rawheap> <filename.heapsnapshot>\n"
23     "at least 1 argv provide, you can also extend to include <filename.heapsnapshot>, "
24     "if output file not available, an automatic one will be generated after all.";
25 
ParseArgs(const int argc,const char ** argv,std::string & input,std::string & output)26 bool ParseArgs(const int argc, const char **argv, std::string &input, std::string &output)
27 {
28     const int minArgc = 2;  // 2: at least 1 argv provide, including <filename.rawheap>
29     const int maxArgc = 3;  // 3: also extend to include output file <filename.heapsnapshot>
30     if (argc < minArgc || argc > maxArgc) {
31         std::cout << "Input error!\n" << RAWHEAP_TRANSLATE_HELPER << std::endl;
32         return false;
33     }
34 
35     int newArgc = 1;
36     std::string rawheapPathOrVersionCheck = argv[newArgc];
37     if (rawheapPathOrVersionCheck == "--version" || rawheapPathOrVersionCheck == "-v") {
38         std::cout << VERSION.ToString() << std::endl;
39         return false;
40     }
41 
42     if (rawheapPathOrVersionCheck == "--help" || rawheapPathOrVersionCheck == "-h") {
43         std::cout << RAWHEAP_TRANSLATE_HELPER << std::endl;
44         return false;
45     }
46 
47     if (!EndsWith(rawheapPathOrVersionCheck, ".rawheap")) {
48         std::cout << "The second argument must be rawheap file!" << std::endl
49                   << RAWHEAP_TRANSLATE_HELPER << std::endl;
50         return false;
51     }
52 
53     newArgc++;
54     std::string outputPath {};
55     if (newArgc < argc) {
56         outputPath = argv[newArgc];
57         if (!EndsWith(outputPath, ".heapsnapshot")) {
58             std::cout << "The last argument must be heapsnapshot file!" << std::endl
59                       << RAWHEAP_TRANSLATE_HELPER << std::endl;
60             return false;
61         }
62     } else {
63         if (!GenerateDumpFileName(outputPath)) {
64             std::cout << "Generate dump file name failed!\n";
65             return false;
66         }
67     }
68 
69     input = rawheapPathOrVersionCheck;
70     output = outputPath;
71     return true;
72 }
73 
Main(const int argc,const char ** argv)74 int Main(const int argc, const char **argv)
75 {
76     std::string rawheapPath;
77     std::string snapshotPath;
78     if (!ParseArgs(argc, argv, rawheapPath, snapshotPath)) {
79         return 0;
80     }
81 
82     RawHeap::TranslateRawheap(rawheapPath, snapshotPath);
83     return 0;
84 }
85 
86 }  // namespace rawheap_translate
main(int argc,const char ** argv)87 int main(int argc, const char **argv)
88 {
89     return rawheap_translate::Main(argc, argv);
90 }