1/*
2 * Copyright (c) 2023 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#ifndef DFX_DWARF_SECTION_H
16#define DFX_DWARF_SECTION_H
17
18#include <cinttypes>
19#include <memory>
20#include <unordered_map>
21#include "dwarf_define.h"
22#include "dfx_errors.h"
23#include "dfx_memory.h"
24#include "dfx_regs.h"
25#include "unwind_context.h"
26
27namespace OHOS {
28namespace HiviewDFX {
29class DwarfSection {
30public:
31    explicit DwarfSection(std::shared_ptr<DfxMemory> memory);
32    virtual ~DwarfSection() = default;
33
34    bool LinearSearchEntry(uintptr_t pc, struct UnwindTableInfo uti, struct UnwindEntryInfo& uei);
35    bool SearchEntry(uintptr_t pc, struct UnwindTableInfo uti, struct UnwindEntryInfo& uei);
36    bool Step(uintptr_t pc, uintptr_t fdeAddr, std::shared_ptr<RegLocState> rs);
37
38    const uint16_t& GetLastErrorCode() { return lastErrorData_.GetCode(); }
39    const uint64_t& GetLastErrorAddr() { return lastErrorData_.GetAddr(); }
40
41protected:
42    bool GetCieOrFde(uintptr_t &addr, FrameDescEntry &fdeInfo);
43    void ParseCieOrFdeHeader(uintptr_t& ptr, FrameDescEntry& fdeInfo, bool& isCieEntry);
44    bool ParseFde(uintptr_t fdeAddr, uintptr_t fdePtr, FrameDescEntry &fdeInfo);
45    bool FillInFde(uintptr_t ptr, FrameDescEntry &fdeInfo);
46    bool ParseCie(uintptr_t cieAddr, uintptr_t ciePtr, CommonInfoEntry &cieInfo);
47    bool FillInCie(uintptr_t ptr, CommonInfoEntry &cieInfo);
48
49protected:
50    std::shared_ptr<DfxMemory> memory_;
51    UnwindErrorData lastErrorData_;
52    uint32_t cie32Value_ = 0;
53    uint64_t cie64Value_ = 0;
54private:
55    std::unordered_map<uintptr_t, FrameDescEntry> fdeEntries_;
56    std::unordered_map<uintptr_t, CommonInfoEntry> cieEntries_;
57};
58} // nameapace HiviewDFX
59} // nameapace OHOS
60#endif
61