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 <iostream>
17#include "ecmascript/ecma_vm.h"
18#include "ecmascript/js_runtime_options.h"
19#include "ecmascript/napi/include/jsnapi.h"
20
21namespace panda::ecmascript {
22
23const std::string PC_TOOL_HELP_MSG =
24    "Usage: ark_js_heap_snapshot_tool <bin_filename> <ouput_filename>\n"
25    "\tWill decode the heapdump bin file to the target file named ouput_filename\n";
26
27std::string GetHelper()
28{
29    std::string str;
30    str.append(PC_TOOL_HELP_MSG);
31    return str;
32}
33
34int HeapSnapshotPcToolMain(const int argc, const char **argv)
35{
36    if (argc < 3) { // 3: at least have 3 arguments
37        std::cerr << GetHelper();
38        return -1;
39    }
40
41    int newArgc = argc;
42    std::string filePath = argv[argc - 2];
43    std::string outputPath = argv[argc - 1];
44    newArgc--;
45    JSRuntimeOptions runtimeOptions;
46
47    EcmaVM *vm = JSNApi::CreateEcmaVM(runtimeOptions);
48    if (vm == nullptr) {
49        std::cerr << "Cannot Create vm" << std::endl;
50        return -1;
51    }
52    std::cout << "(INFO) ark Decode: " << filePath << std::endl;
53    DFXJSNApi::GenerateHeapSnapshotByBinFile(vm, filePath, outputPath);
54    JSNApi::DestroyJSVM(vm);
55    std::cout << "(INFO) ark HeapSnapshotPcToolMain finished" << std::endl;
56    return 0;
57}
58}  // namespace panda::ecmascript
59
60
61int main([[maybe_unused]] int argc, [[maybe_unused]] char const *argv[])
62{
63    return panda::ecmascript::HeapSnapshotPcToolMain(argc, argv);
64}
65