• 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/rawheap_translate.h"
17 #include "ecmascript/dfx/hprof/rawheap_translate/serializer.h"
18 
19 namespace rawheap_translate {
20 std::string RAWHEAP_TRANSLATE_HELPER =
21     "Usage: rawheap_translator <filename.rawheap> <filename.heapsnapshot>\n"
22     "at least 1 argv provide, you can also extend to include <filename.heapsnapshot>, "
23     "if output file not available, an automatic one will be generated after all.";
24 
Main(const int argc,const char ** argv)25 int Main(const int argc, const char **argv)
26 {
27     // 2: at least 1 argv provide, including <filename.rawheap>
28     // 3: also extend to include output file <filename.heapsnapshot>
29     if (argc < 2 || argc > 3) {
30         LOG_ERROR("Main: input error!\n" + RAWHEAP_TRANSLATE_HELPER);
31         return -1;
32     }
33 
34     int newArgc = 1;
35     std::string rawheapPathOrVersionCheck = argv[newArgc];
36     if (rawheapPathOrVersionCheck == "-V" || rawheapPathOrVersionCheck == "-v") {
37         std::cout << VERSION[MAJOR_VERSION_INDEX]
38                   << '.' << VERSION[MINOR_VERSION_INDEX]
39                   << '.' << VERSION[BUILD_VERSION_INDEX] << std::endl;
40         return 0;
41     }
42 
43     if (!EndsWith(rawheapPathOrVersionCheck, ".rawheap")) {
44         LOG_ERROR("The second argument must be rawheap file!\n" + RAWHEAP_TRANSLATE_HELPER);
45         return -1;
46     }
47 
48     newArgc++;
49     std::string outputPath {};
50     if (newArgc < argc) {
51         outputPath = argv[newArgc];
52         if (!EndsWith(outputPath, ".heapsnapshot")) {
53             LOG_ERROR("The last argument must be heapsnapshot file!\n" + RAWHEAP_TRANSLATE_HELPER);
54             return -1;
55         }
56     } else {
57         if (!GenerateDumpFileName(outputPath)) {
58             LOG_ERROR("Generate dump file name failed!\n");
59             return -1;
60         }
61     }
62 
63     LOG_INFO("Main: start to translate rawheap!");
64     RawHeapTranslate translate;
65     if (!translate.Translate(rawheapPathOrVersionCheck)) {
66         return -1;
67     }
68 
69     LOG_INFO("Main: start to serialize!");
70     StreamWriter writer;
71     if (!writer.Initialize(outputPath)) {
72         return -1;
73     }
74 
75     HeapSnapshotJSONSerializer::Serialize(&translate, &writer);
76     LOG_INFO("Main: translate success! file save to " + outputPath);
77     return 0;
78 }
79 
80 }  // namespace rawheap_translate
main(int argc,const char ** argv)81 int main(int argc, const char **argv)
82 {
83     return rawheap_translate::Main(argc, argv);
84 }