1 /*
2  * Copyright (c) 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "bpf_module.h"
18 #include "frontends/clang/loader.h"
19 
20 namespace ebpf {
21 
22 class SourceDebugger {
23  public:
SourceDebugger(llvm::Module * mod,sec_map_def & sections,ProgFuncInfo & prog_func_info,const std::string & mod_src,std::map<std::string,std::string> & src_dbg_fmap)24   SourceDebugger(llvm::Module *mod, sec_map_def &sections,
25                  ProgFuncInfo &prog_func_info, const std::string &mod_src,
26                  std::map<std::string, std::string> &src_dbg_fmap)
27       : mod_(mod),
28         sections_(sections),
29         prog_func_info_(prog_func_info),
30         mod_src_(mod_src),
31         src_dbg_fmap_(src_dbg_fmap) {}
32 // Only support dump for llvm 6.x and later.
33 //
34 // The llvm 5.x, but not earlier versions, also supports create
35 // a dwarf context for source debugging based
36 // on a set of in-memory sections with slightly different interfaces.
37 // FIXME: possibly to support 5.x
38 //
39 #if LLVM_VERSION_MAJOR >= 6
40   void dump();
41 
42  private:
43   void adjustInstSize(uint64_t &Size, uint8_t byte0, uint8_t byte1);
44   std::vector<std::string> buildLineCache();
45   void dumpSrcLine(const std::vector<std::string> &LineCache,
46                    const std::string &FileName, uint32_t Line,
47                    uint32_t &CurrentSrcLine, llvm::raw_ostream &os);
48   void getDebugSections(
49       llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> &DebugSections);
50 #else
dump()51   void dump() {
52   }
53 #endif
54 
55  private:
56   llvm::Module *mod_;
57   const sec_map_def &sections_;
58   ProgFuncInfo &prog_func_info_;
59   const std::string &mod_src_;
60   std::map<std::string, std::string> &src_dbg_fmap_;
61 };
62 
63 }  // namespace ebpf
64