diff --git a/src/cc/bcc_btf.cc b/src/cc/bcc_btf.cc index eaf0ee9c423c..dee2d114ce53 100644 --- a/src/cc/bcc_btf.cc +++ b/src/cc/bcc_btf.cc @@ -15,9 +15,11 @@ */ #include "bcc_btf.h" +#include #include #include "linux/btf.h" #include "libbpf.h" +#include "libbpf/src/libbpf.h" #include "libbpf/src/btf.h" #include @@ -41,11 +43,27 @@ uint32_t BTFStringTable::addString(std::string S) { return Offset; } +BTF::BTF(bool debug) : debug_(debug), btf_(nullptr), btf_ext_(nullptr) { + if (!debug) + libbpf_set_print(NULL); +} + BTF::~BTF() { btf__free(btf_); btf_ext__free(btf_ext_); } +void BTF::warning(const char *format, ...) { + va_list args; + + if (!debug_) + return; + + va_start(args, format); + vfprintf(stderr, format, args); + va_end(args); +} + // The compiler doesn't have source code for remapped files. // So we modify .BTF and .BTF.ext sections here to add these // missing line source codes. @@ -153,14 +171,14 @@ int BTF::load(uint8_t *btf_sec, uintptr_t btf_sec_size, btf = btf__new(btf_sec, btf_sec_size); } if (BCC_IS_ERR(btf)) { - fprintf(stderr, "Processing .BTF section failure\n"); + warning("Processing .BTF section failed\n"); return -1; } btf_ext = btf_ext__new(btf_ext_sec, btf_ext_sec_size); if (BCC_IS_ERR(btf_ext)) { btf__free(btf); - fprintf(stderr, "Processing .BTF.ext section failure\n"); + warning("Processing .BTF.ext section failed\n"); return -1; } @@ -189,14 +207,14 @@ int BTF::get_btf_info(const char *fname, ret = btf_ext__reloc_func_info(btf_, btf_ext_, fname, 0, func_info, func_info_cnt); if (ret) { - fprintf(stderr, ".BTF.ext reloc func_info not successful\n"); + warning(".BTF.ext reloc func_info failed\n"); return ret; } ret = btf_ext__reloc_line_info(btf_, btf_ext_, fname, 0, line_info, line_info_cnt); if (ret) { - fprintf(stderr, ".BTF.ext reloc line_info not successful\n"); + warning(".BTF.ext reloc line_info failed\n"); return ret; } diff --git a/src/cc/bcc_btf.h b/src/cc/bcc_btf.h index ffa8307e823d..5204b016cdce 100644 --- a/src/cc/bcc_btf.h +++ b/src/cc/bcc_btf.h @@ -43,6 +43,7 @@ class BTFStringTable { class BTF { public: + BTF(bool debug); ~BTF(); int load(uint8_t *btf_sec, uintptr_t btf_sec_size, uint8_t *btf_ext_sec, uintptr_t btf_ext_sec_size, @@ -62,8 +63,10 @@ class BTF { uint8_t *btf_ext_sec, uintptr_t btf_ext_sec_size, std::map &remapped_sources, uint8_t **new_btf_sec, uintptr_t *new_btf_sec_size); + void warning(const char *format, ...); private: + bool debug_; struct btf *btf_; struct btf_ext *btf_ext_; }; diff --git a/src/cc/bpf_module.cc b/src/cc/bpf_module.cc index de7e9a54cb2e..f0d399784623 100644 --- a/src/cc/bpf_module.cc +++ b/src/cc/bpf_module.cc @@ -258,7 +258,7 @@ void BPFModule::load_btf(std::map> remapped_sources["/virtual/main.c"] = mod_src_; remapped_sources["/virtual/include/bcc/helpers.h"] = helpers_h->second; - BTF *btf = new BTF(); + BTF *btf = new BTF(flags_ & DEBUG_BTF); int ret = btf->load(btf_sec, btf_sec_size, btf_ext_sec, btf_ext_sec_size, remapped_sources); if (ret) { diff --git a/src/cc/bpf_module.h b/src/cc/bpf_module.h index b372a954eea2..18c71d3d53a9 100644 --- a/src/cc/bpf_module.h +++ b/src/cc/bpf_module.h @@ -47,6 +47,8 @@ enum { DEBUG_SOURCE = 0x8, // Debug output register state on all instructions in addition to DEBUG_BPF. DEBUG_BPF_REGISTER_STATE = 0x10, + // Debug BTF. + DEBUG_BTF = 0x20, }; class TableDesc; diff --git a/src/python/bcc/__init__.py b/src/python/bcc/__init__.py index 534bc6677aa3..2c45d8c6dd26 100644 --- a/src/python/bcc/__init__.py +++ b/src/python/bcc/__init__.py @@ -51,8 +51,10 @@ def _get_num_open_probes(): DEBUG_PREPROCESSOR = 0x4 # Debug output ASM instructions embedded with source. DEBUG_SOURCE = 0x8 -#Debug output register state on all instructions in addition to DEBUG_BPF. +# Debug output register state on all instructions in addition to DEBUG_BPF. DEBUG_BPF_REGISTER_STATE = 0x10 +# Debug BTF. +DEBUG_BTF = 0x20 class SymbolCache(object): def __init__(self, pid):