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 namespace ebpf {
17 
18 class SourceDebugger {
19  public:
SourceDebugger(llvm::Module * mod,std::map<std::string,std::tuple<uint8_t *,uintptr_t>> & sections,const std::string & fn_prefix,const std::string & mod_src,std::map<std::string,std::string> & src_dbg_fmap)20   SourceDebugger(
21       llvm::Module *mod,
22       std::map<std::string, std::tuple<uint8_t *, uintptr_t>> &sections,
23       const std::string &fn_prefix, const std::string &mod_src,
24       std::map<std::string, std::string> &src_dbg_fmap)
25       : mod_(mod),
26         sections_(sections),
27         fn_prefix_(fn_prefix),
28         mod_src_(mod_src),
29         src_dbg_fmap_(src_dbg_fmap) {}
30 // Only support dump for llvm 6.x and later.
31 //
32 // The llvm 5.x, but not earlier versions, also supports create
33 // a dwarf context for source debugging based
34 // on a set of in-memory sections with slightly different interfaces.
35 // FIXME: possibly to support 5.x
36 //
37 #if LLVM_MAJOR_VERSION >= 6
38   void dump();
39 
40  private:
41   void adjustInstSize(uint64_t &Size, uint8_t byte0, uint8_t byte1);
42   std::vector<std::string> buildLineCache();
43   void dumpSrcLine(const std::vector<std::string> &LineCache,
44                    const std::string &FileName, uint32_t Line,
45                    uint32_t &CurrentSrcLine, llvm::raw_ostream &os);
46   void getDebugSections(
47       llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> &DebugSections);
48 #else
dump()49   void dump() {
50   }
51 #endif
52 
53  private:
54   llvm::Module *mod_;
55   const std::map<std::string, std::tuple<uint8_t *, uintptr_t>> &sections_;
56   const std::string &fn_prefix_;
57   const std::string &mod_src_;
58   std::map<std::string, std::string> &src_dbg_fmap_;
59 };
60 
61 }  // namespace ebpf
62