• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright 2020-2023 Google LLC
5 //
6 // Licensed under the Apache License v2.0 with LLVM Exceptions (the
7 // "License"); you may not use this file except in compliance with the
8 // License.  You may obtain a copy of the License at
9 //
10 //     https://llvm.org/LICENSE.txt
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
18 // Author: Maria Teguiani
19 // Author: Giuliano Procida
20 
21 #ifndef STG_BTF_READER_H_
22 #define STG_BTF_READER_H_
23 
24 #include <cstddef>
25 #include <cstdint>
26 #include <map>
27 #include <optional>
28 #include <string>
29 #include <string_view>
30 #include <unordered_map>
31 #include <vector>
32 
33 #include <linux/btf.h>
34 #include "graph.h"
35 #include "reader_options.h"
36 
37 namespace stg {
38 namespace btf {
39 
40 // BTF Specification: https://www.kernel.org/doc/html/latest/bpf/btf.html
41 class Structs {
42  public:
43   explicit Structs(Graph& graph);
44   Id Process(std::string_view data);
45 
46  private:
47   struct MemoryRange {
48     const char* start;
49     const char* limit;
50     bool Empty() const;
51     template <typename T> const T* Pull(size_t count = 1);
52   };
53 
54   Graph& graph_;
55 
56   MemoryRange string_section_;
57 
58   std::optional<Id> void_;
59   std::optional<Id> variadic_;
60   std::unordered_map<uint32_t, Id> btf_type_ids_;
61   std::map<std::string, Id> btf_symbols_;
62 
63   Id GetVoid();
64   Id GetVariadic();
65   Id GetIdRaw(uint32_t btf_index);
66   Id GetId(uint32_t btf_index);
67   Id GetParameterId(uint32_t btf_index);
68 
69   Id BuildTypes(MemoryRange memory);
70   void BuildOneType(const btf_type* t, uint32_t btf_index,
71                     MemoryRange& memory);
72   Id BuildSymbols();
73   std::vector<Id> BuildMembers(
74       bool kflag, const btf_member* members, size_t vlen);
75   Enumeration::Enumerators BuildEnums(
76       bool is_signed, const struct btf_enum* enums, size_t vlen);
77   Enumeration::Enumerators BuildEnums64(
78       bool is_signed, const struct btf_enum64* enums, size_t vlen);
79   std::vector<Id> BuildParams(const struct btf_param* params, size_t vlen);
80   Id BuildEnumUnderlyingType(size_t size, bool is_signed);
81   std::string GetName(uint32_t name_off);
82 };
83 
84 Id ReadFile(Graph& graph, const std::string& path, ReadOptions options);
85 
86 }  // namespace btf
87 }  // namespace stg
88 
89 #endif  // STG_BTF_READER_H_
90