1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
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 HIEBPF_KERNEL_SYMBOL_INFO_H_
17 #define HIEBPF_KERNEL_SYMBOL_INFO_H_
18 
19 #include <string>
20 #include <vector>
21 
22 namespace OHOS {
23 namespace Developtools {
24 namespace Hiebpf {
25 const std::string KALLSYMS_PATH = "/proc/kallsyms";
26 constexpr uint32_t DEFAULT_BUFF_SIZE = 4 * 1024 * 1024;
27 
28 class KernelSymbolInfo {
29 public:
30     KernelSymbolInfo() = delete;
31     ~KernelSymbolInfo() = delete;
32 
33     static uint32_t GetSymbolData(std::vector<uint8_t> &buf);
34 
35 private:
36     struct SymbolItem {
37         uint64_t value_ = 0;
38         uint32_t size_ = 0;
39         std::string name_ = "";
40         char type_ = 0;
41     };
42 
43     static void GetSymbol(const std::string &line, SymbolItem &symbol);
44     static void GetBinary(const std::vector<SymbolItem> &symbolItems,
45                              uint64_t vaddrStart,
46                              uint64_t vaddrEnd,
47                              uint32_t strTabLen,
48                              std::vector<uint8_t> &buf);
49 };
50 } // namespace Hiebpf
51 } // namespace Developtools
52 } // namespace OHOS
53 #endif // HIEBPF_KERNEL_SYMBOL_INFO_H_