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