Skip to content

Commit

Permalink
Merge pull request #3660 from davemarchevsky/davemarchevsky_sections
Browse files Browse the repository at this point in the history
bcc internals: Keep track of BPF progs using function instead of section
  • Loading branch information
davemarchevsky authored Feb 22, 2022
2 parents f96fed0 + 8323d74 commit 678197b
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 231 deletions.
89 changes: 44 additions & 45 deletions src/cc/bcc_debug.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,68 +196,67 @@ void SourceDebugger::dump() {
vector<string> LineCache = buildLineCache();

// Start to disassemble with source code annotation section by section
for (auto section : sections_)
if (!strncmp(fn_prefix_.c_str(), section.first.c_str(),
fn_prefix_.size())) {
MCDisassembler::DecodeStatus S;
MCInst Inst;
uint64_t Size;
uint8_t *FuncStart = get<0>(section.second);
uint64_t FuncSize = get<1>(section.second);
prog_func_info_.for_each_func([&](std::string func_name, FuncInfo &info) {
MCDisassembler::DecodeStatus S;
MCInst Inst;
uint64_t Size;
uint8_t *FuncStart = info.start_;
uint64_t FuncSize = info.size_;
#if LLVM_MAJOR_VERSION >= 9
unsigned SectionID = get<2>(section.second);
auto section = sections_.find(info.section_);
if (section == sections_.end()) {
errs() << "Debug Error: no section entry for section " << info.section_
<< '\n';
return;
}
unsigned SectionID = get<2>(section->second);
#endif
ArrayRef<uint8_t> Data(FuncStart, FuncSize);
uint32_t CurrentSrcLine = 0;
string func_name = section.first.substr(fn_prefix_.size());
ArrayRef<uint8_t> Data(FuncStart, FuncSize);
uint32_t CurrentSrcLine = 0;

errs() << "Disassembly of section " << section.first << ":\n"
<< func_name << ":\n";
errs() << "Disassembly of function " << func_name << "\n";

string src_dbg_str;
llvm::raw_string_ostream os(src_dbg_str);
for (uint64_t Index = 0; Index < FuncSize; Index += Size) {
string src_dbg_str;
llvm::raw_string_ostream os(src_dbg_str);
for (uint64_t Index = 0; Index < FuncSize; Index += Size) {
#if LLVM_MAJOR_VERSION >= 10
S = DisAsm->getInstruction(Inst, Size, Data.slice(Index), Index,
nulls());
S = DisAsm->getInstruction(Inst, Size, Data.slice(Index), Index, nulls());
#else
S = DisAsm->getInstruction(Inst, Size, Data.slice(Index), Index,
nulls(), nulls());
S = DisAsm->getInstruction(Inst, Size, Data.slice(Index), Index, nulls(),
nulls());
#endif
if (S != MCDisassembler::Success) {
os << "Debug Error: disassembler failed: " << std::to_string(S)
<< '\n';
break;
} else {
DILineInfo LineInfo;
if (S != MCDisassembler::Success) {
os << "Debug Error: disassembler failed: " << std::to_string(S) << '\n';
break;
} else {
DILineInfo LineInfo;

LineTable->getFileLineInfoForAddress(
LineTable->getFileLineInfoForAddress(
#if LLVM_MAJOR_VERSION >= 9
{(uint64_t)FuncStart + Index, SectionID},
{(uint64_t)FuncStart + Index, SectionID},
#else
(uint64_t)FuncStart + Index,
(uint64_t)FuncStart + Index,
#endif
CU->getCompilationDir(),
DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
LineInfo);
CU->getCompilationDir(),
DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, LineInfo);

adjustInstSize(Size, Data[Index], Data[Index + 1]);
dumpSrcLine(LineCache, LineInfo.FileName, LineInfo.Line,
CurrentSrcLine, os);
os << format("%4" PRIu64 ":", Index >> 3) << '\t';
dumpBytes(Data.slice(Index, Size), os);
adjustInstSize(Size, Data[Index], Data[Index + 1]);
dumpSrcLine(LineCache, LineInfo.FileName, LineInfo.Line, CurrentSrcLine,
os);
os << format("%4" PRIu64 ":", Index >> 3) << '\t';
dumpBytes(Data.slice(Index, Size), os);
#if LLVM_MAJOR_VERSION >= 10
IP->printInst(&Inst, 0, "", *STI, os);
IP->printInst(&Inst, 0, "", *STI, os);
#else
IP->printInst(&Inst, os, "", *STI);
IP->printInst(&Inst, os, "", *STI);
#endif
os << '\n';
}
os << '\n';
}
os.flush();
errs() << src_dbg_str << '\n';
src_dbg_fmap_[func_name] = src_dbg_str;
}
os.flush();
errs() << src_dbg_str << '\n';
src_dbg_fmap_[func_name] = src_dbg_str;
});
}

} // namespace ebpf
13 changes: 6 additions & 7 deletions src/cc/bcc_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@
*/

#include "bpf_module.h"
#include "frontends/clang/loader.h"

namespace ebpf {

class SourceDebugger {
public:
SourceDebugger(
llvm::Module *mod,
sec_map_def &sections,
const std::string &fn_prefix, const std::string &mod_src,
std::map<std::string, std::string> &src_dbg_fmap)
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)
: mod_(mod),
sections_(sections),
fn_prefix_(fn_prefix),
prog_func_info_(prog_func_info),
mod_src_(mod_src),
src_dbg_fmap_(src_dbg_fmap) {}
// Only support dump for llvm 6.x and later.
Expand Down Expand Up @@ -56,7 +55,7 @@ class SourceDebugger {
private:
llvm::Module *mod_;
const sec_map_def &sections_;
const std::string &fn_prefix_;
ProgFuncInfo &prog_func_info_;
const std::string &mod_src_;
std::map<std::string, std::string> &src_dbg_fmap_;
};
Expand Down
Loading

0 comments on commit 678197b

Please sign in to comment.