• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef ELF_FACTORY_H
17 #define ELF_FACTORY_H
18 
19 #include <memory>
20 #include <mutex>
21 #include "dfx_define.h"
22 #include "dfx_elf.h"
23 #include "dfx_map.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 class ElfFactory {
28 public:
29     virtual ~ElfFactory() = default;
30     virtual std::shared_ptr<DfxElf> Create() = 0;
31 };
32 
33 class RegularElfFactory : public ElfFactory {
34 public:
RegularElfFactory(const std::string & filePath)35     explicit RegularElfFactory(const std::string& filePath) : filePath_(filePath) {}
36     ~RegularElfFactory() override = default;
37     std::shared_ptr<DfxElf> Create() override;
38 private:
39     std::string filePath_;
40 };
41 
42 #if defined(ENABLE_MINIDEBUGINFO)
43 class MiniDebugInfoFactory : public ElfFactory {
44 public:
MiniDebugInfoFactory(const GnuDebugDataHdr & gnuDebugDataHdr)45     explicit MiniDebugInfoFactory(const GnuDebugDataHdr& gnuDebugDataHdr) : gnuDebugDataHdr_(gnuDebugDataHdr) {}
46     ~MiniDebugInfoFactory() override = default;
47     std::shared_ptr<DfxElf> Create() override;
48 private:
49     bool XzDecompress(const uint8_t *src, size_t srcLen, std::vector<uint8_t>& out);
50     GnuDebugDataHdr gnuDebugDataHdr_{};
51 };
52 #endif
53 
54 class CompressHapElfFactory : public ElfFactory {
55 public:
CompressHapElfFactory(std::string filePath,std::shared_ptr<DfxMap> prevMap)56     explicit CompressHapElfFactory(std::string filePath, std::shared_ptr<DfxMap> prevMap)
57         : filePath_(std::move(filePath)), prevMap_(prevMap) {}
58     ~CompressHapElfFactory() override = default;
59     std::shared_ptr<DfxElf> Create() override;
60 private:
61     bool VerifyElf(int fd, size_t& elfSize);
62     const std::string filePath_;
63     std::shared_ptr<DfxMap> prevMap_;
64 };
65 
66 class VdsoElfFactory : public ElfFactory {
67 public:
VdsoElfFactory(uint64_t begin,size_t size,pid_t pid)68     explicit VdsoElfFactory(uint64_t begin, size_t size, pid_t pid) : begin_(begin), size_(size), pid_(pid) {}
69     ~VdsoElfFactory() override = default;
70     std::shared_ptr<DfxElf> Create() override;
71 private:
72     VdsoElfFactory() = delete;
73     MAYBE_UNUSED uint64_t begin_ = 0;
74     MAYBE_UNUSED size_t size_ = 0;
75     MAYBE_UNUSED pid_t pid_ = 0;
76 };
77 } // namespace HiviewDFX
78 } // namespace OHOS
79 #endif
80