From 15ae44201045722477158ecab89335b979199d89 Mon Sep 17 00:00:00 2001 From: wengchangcheng Date: Tue, 26 Apr 2022 15:32:52 +0800 Subject: [PATCH] Descriptor: add file stream for heap dump details: add file stream for heap dump issue: https://gitee.com/openharmony/ark_js_runtime/issues/I54S6V Signed-off-by: wengchangcheng Change-Id: I45435df049d5e67480b5d8332d3e54895f15f796 --- BUILD.gn | 1 + ecmascript/tooling/interface/file_stream.cpp | 89 ++++++++++++++++++++ ecmascript/tooling/interface/file_stream.h | 48 +++++++++++ ecmascript/tooling/interface/stream.h | 4 +- 4 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 ecmascript/tooling/interface/file_stream.cpp create mode 100644 ecmascript/tooling/interface/file_stream.h diff --git a/BUILD.gn b/BUILD.gn index af6f0ea33d..2f1a63c373 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -487,6 +487,7 @@ ecma_source = [ if (!is_mingw) { ecma_source += [ "ecmascript/napi/dfx_jsnapi.cpp" ] + ecma_source += [ "ecmascript/tooling/interface/file_stream.cpp" ] } if (enable_test_stub) { diff --git a/ecmascript/tooling/interface/file_stream.cpp b/ecmascript/tooling/interface/file_stream.cpp new file mode 100644 index 0000000000..c6e16c8181 --- /dev/null +++ b/ecmascript/tooling/interface/file_stream.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * 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 "ecmascript/tooling/interface/file_stream.h" + +#include +#include + +#include "ecmascript/ecma_macros.h" +#include "libpandabase/utils/logger.h" + +namespace panda::ecmascript { +FileStream::FileStream(const std::string &fileName) +{ + Initialize(fileName); +} + +FileStream::~FileStream() +{ + EndOfStream(); +} + +void FileStream::EndOfStream() +{ + if (fileStream_.fail()) { + return; + } + + fileStream_.close(); +} + +void FileStream::Initialize(const std::string &fileName) +{ + // check file name + std::pair realPath = FilePathValid(fileName); + if (!realPath.first) { + LOG_ECMA(ERROR) << "FileStream: check file path failed"; + return; + } + + fileStream_.open(realPath.second.c_str(), std::ios::out); + if (fileStream_.fail()) { + LOG_ECMA(ERROR) << "FileStream: open file failed"; + } +} + +std::pair FileStream::FilePathValid(const std::string &fileName) +{ + if (fileName.empty() || fileName.size() > PATH_MAX) { + return std::make_pair(false, ""); + } + char resolvedPath[PATH_MAX] = {0}; + auto result = realpath(fileName.c_str(), resolvedPath); + if (result == resolvedPath || errno == ENOENT) { + return std::make_pair(true, std::string(resolvedPath)); + } + return std::make_pair(false, ""); +} + +// Writes the chunk of data into the stream +bool FileStream::WriteChunk(char* data, int size) +{ + if (fileStream_.fail()) { + return false; + } + + std::string str; + str.resize(size); + for (int i = 0; i < size; ++i) { + str[i] = data[i]; + } + + fileStream_ << str; + + return true; +} +} \ No newline at end of file diff --git a/ecmascript/tooling/interface/file_stream.h b/ecmascript/tooling/interface/file_stream.h new file mode 100644 index 0000000000..b5e2390be0 --- /dev/null +++ b/ecmascript/tooling/interface/file_stream.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * 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. + */ + +#ifndef ECMASCRIPT_TOOLING_INTERFACE_FILE_STREAM_H +#define ECMASCRIPT_TOOLING_INTERFACE_FILE_STREAM_H + +#include + +#include "ecmascript/tooling/interface/stream.h" + +namespace panda::ecmascript { +class FileStream : public Stream { + FileStream(const std::string &fileName); + ~FileStream() override; + + void EndOfStream() override; + + // Get chunk's size + int GetSize() override + { + const static int fileChunkSize = 10240; + return fileChunkSize; + } + + // Writes the chunk of data into the stream + bool WriteChunk(char* data, int size) override; + +private: + void Initialize(const std::string &fileName); + std::pair FilePathValid(const std::string &fileName); + + std::fstream fileStream_; +}; +} // namespace panda::ecmascript::tooling + +#endif // ECMASCRIPT_TOOLING_INTERFACE_FILE_STREAM_H diff --git a/ecmascript/tooling/interface/stream.h b/ecmascript/tooling/interface/stream.h index 63b9fe371a..3c19d8be20 100644 --- a/ecmascript/tooling/interface/stream.h +++ b/ecmascript/tooling/interface/stream.h @@ -16,7 +16,7 @@ #ifndef ECMASCRIPT_TOOLING_INTERFACE_STREAM_H #define ECMASCRIPT_TOOLING_INTERFACE_STREAM_H -namespace panda::ecmascript::tooling { +namespace panda::ecmascript { class Stream { public: virtual ~Stream() = default; @@ -29,6 +29,6 @@ public: // Writes the chunk of data into the stream virtual bool WriteChunk (char* data, int size) = 0; }; -} // namespace panda::ecmascript::tooling +} // namespace panda::ecmascript #endif // ECMASCRIPT_TOOLING_INTERFACE_STREAM_H -- Gitee