iovisor / bcc

BCC - Tools for BPF-based Linux IO analysis, networking, monitoring, and more
Apache License 2.0
20.48k stars 3.87k forks source link

How would one compile files in /bcc/examples/cpp? #2949

Closed anmathew closed 4 years ago

anmathew commented 4 years ago

How would one compile files in bcc/examples/cpp?

root@BCCTestHost:~/bcc/examples/cpp# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.4 LTS
Release:        18.04
Codename:       bionic

# uname -r
4.15.0-101-generic

Trying with HelloWorld.cc:

root@BCCTestHost:~/bcc/examples/cpp# g++ HelloWorld.cc
HelloWorld.cc:11:10: fatal error: bcc_version.h: No such file or directory
 #include "bcc_version.h"
          ^~~~~~~~~~~~~~~
compilation terminated.

I found the files in /usr/include/bcc folder:

root@BCCTestHost:~/bcc/examples/cpp# locate bcc_version.h
/root/bcc/build/src/cc/bcc_version.h
/root/bcc/src/cc/bcc_version.h.in
/usr/include/bcc/bcc_version.h

So, I tried, below and it blew further up:

root@BCCTestHost:~/bcc/examples/cpp# g++ HelloWorld.cc  -I /usr/include/bcc
In file included from /usr/include/bcc/BPF.h:25:0,
                 from HelloWorld.cc:12:
/usr/include/bcc/BPFTable.h:325:54: error: field 'trace' has incomplete type 'bpf_stack_build_id [127]'
   struct bpf_stack_build_id trace[BPF_MAX_STACK_DEPTH];
                                                      ^
In file included from /usr/include/bcc/BPFTable.h:30:0,
                 from /usr/include/bcc/BPF.h:25,
                 from HelloWorld.cc:12:
/usr/include/bcc/bcc_syms.h:81:38: note: forward declaration of 'struct bpf_stack_build_id'
                               struct bpf_stack_build_id *trace,
                                      ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/bcc/BPF.h:25:0,
                 from HelloWorld.cc:12:
/usr/include/bcc/BPFTable.h: In constructor 'ebpf::BPFSkStorageTable::BPFSkStorageTable(const ebpf::TableDesc&)':
/usr/include/bcc/BPFTable.h:442:22: error: 'BPF_MAP_TYPE_SK_STORAGE' was not declared in this scope
     if (desc.type != BPF_MAP_TYPE_SK_STORAGE)
                      ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/bcc/BPFTable.h:442:22: note: suggested alternative: 'BPF_MAP_TYPE_STACK_TRACE'
     if (desc.type != BPF_MAP_TYPE_SK_STORAGE)
                      ^~~~~~~~~~~~~~~~~~~~~~~
                      BPF_MAP_TYPE_STACK_TRACE
/usr/include/bcc/BPFTable.h: In constructor 'ebpf::BPFCgStorageTable::BPFCgStorageTable(const ebpf::TableDesc&)':
/usr/include/bcc/BPFTable.h:471:22: error: 'BPF_MAP_TYPE_CGROUP_STORAGE' was not declared in this scope
     if (desc.type != BPF_MAP_TYPE_CGROUP_STORAGE)
                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/bcc/BPFTable.h:471:22: note: suggested alternative: 'BPF_MAP_TYPE_CGROUP_ARRAY'
     if (desc.type != BPF_MAP_TYPE_CGROUP_STORAGE)
                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~
                      BPF_MAP_TYPE_CGROUP_ARRAY
/usr/include/bcc/BPFTable.h: In constructor 'ebpf::BPFPercpuCgStorageTable::BPFPercpuCgStorageTable(const ebpf::TableDesc&)':
/usr/include/bcc/BPFTable.h:497:22: error: 'BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE' was not declared in this scope
     if (desc.type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/bcc/BPFTable.h:497:22: note: suggested alternative: 'BPF_MAP_TYPE_PERCPU_ARRAY'
     if (desc.type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                      BPF_MAP_TYPE_PERCPU_ARRAY

Could you please point me towards the correct direction?

Thanks!

yonghong-song commented 4 years ago

Top level cmake/make should build examples, you can follow the instruction in INSTALL.md, which has more detailed information about how to build.

anmathew commented 4 years ago

Thank you, following the instructions built and put the binaries in build/examples/cpp - this was a little bit magical as I don't feign to be a cmake expert.

If you could please show how to compile HelloWorld.cc standalone using g++ or clang, I think that would help me immensely as a bcc cpp starter. I am positive there would be others who may benefit in the future.

willfindlay commented 4 years ago

@anmathew The following command works on my system: g++ -I/usr/include/bcc -lbcc_bpf -lbcc HelloWorld.cc -o HelloWorld

anmathew commented 4 years ago

Thank you @willfindlay.

Unfortunately, does not work for me This is what I get on a brand new focal vm:

Virtual-Machine:~/git/bcc/examples/cpp$ g++ -I/usr/include/bcc -lbcc_bpf -lbcc HelloWorld.cc -o HelloWorld
/usr/bin/ld: /tmp/ccv8WGOt.o: in function `main':
HelloWorld.cc:(.text+0x4e): undefined reference to `ebpf::bpf_module_rw_engine_enabled()'
/usr/bin/ld: HelloWorld.cc:(.text+0x12b): undefined reference to `ebpf::BPF::init(std::__cxx11::basic_string, std::allocator > const&, std::vector, std::allocator >, std::allocator, std::allocator > > > const&, std::vector > const&)'
/usr/bin/ld: HelloWorld.cc:(.text+0x214): undefined reference to `ebpf::BPF::get_syscall_fnname(std::__cxx11::basic_string, std::allocator > const&)'
/usr/bin/ld: HelloWorld.cc:(.text+0x294): undefined reference to `ebpf::BPF::attach_kprobe(std::__cxx11::basic_string, std::allocator > const&, std::__cxx11::basic_string, std::allocator > const&, unsigned long, bpf_probe_attach_type, int)'
/usr/bin/ld: HelloWorld.cc:(.text+0x3c5): undefined reference to `ebpf::BPF::detach_kprobe(std::__cxx11::basic_string, std::allocator > const&, bpf_probe_attach_type)'
/usr/bin/ld: HelloWorld.cc:(.text+0x4d0): undefined reference to `ebpf::BPF::~BPF()'
/usr/bin/ld: HelloWorld.cc:(.text+0x62f): undefined reference to `ebpf::BPF::~BPF()'
/usr/bin/ld: /tmp/ccv8WGOt.o: in function `ebpf::BPF::BPF(unsigned int, ebpf::TableStorage*, bool, std::__cxx11::basic_string, std::allocator > const&, bool)':
HelloWorld.cc:(.text._ZN4ebpf3BPFC2EjPNS_12TableStorageEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEb[_ZN4ebpf3BPFC5EjPNS_12TableStorageEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEb]+0x8a): undefined reference to `ebpf::BPFModule::BPFModule(unsigned int, ebpf::TableStorage*, bool, std::__cxx11::basic_string, std::allocator > const&, bool, char const*)'
collect2: error: ld returned 1 exit status

I've started using gobpf instead, as it seems to work right-off the bat after minimal preparatory steps.

anmathew commented 4 years ago

Ok, these seem to work for me:

$ g++ HelloWorld.cc  -I ~/git/bcc/build/src/cc -I ~/git/bcc/src/cc/api/ -I ~/git/bcc/src/cc/ -lbcc
$ g++ HelloWorld.cc  -I ~/git/bcc/build/src/cc -I ~/git/bcc/src/cc/api/ -I ~/git/bcc/src/cc/ -lbcc -lbcc_bpf

Thanks!

QGB commented 1 year ago

@anmathew not work for me

QGB commented 1 year ago
/usr/bin/c++ -I/usr/lib/llvm-6.0/include/../tools/clang/include -I/home/qgb/github/ProcMon-for-Linux/build/_deps/bcc-build/src/cc -I/home/qgb/github/ProcMon-for-Linux/build/_deps/bcc-src/src/cc -I/home/qgb/github/ProcMon-for-Linux/build/_deps/bcc-sr
     ...: c/src/cc/api -I/home/qgb/github/ProcMon-for-Linux/build/_deps/bcc-src/src/cc/libbpf/include/uapi  -std=gnu++14  /home/qgb/github/ProcMon-for-Linux/build/_deps/bcc-src/examples/cpp/HelloWorld.cc -lbcc -lbcc_bpf
/tmp/ccON6AaU.o: In function `ebpf::BPF::BPF(unsigned int, ebpf::TableStorage*, bool, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)':
HelloWorld.cc:(.text._ZN4ebpf3BPFC2EjPNS_12TableStorageEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEb[_ZN4ebpf3BPFC5EjPNS_12TableStorageEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEb]+0x86): undefined reference to `ebpf::BPFModule::BPFModule(unsigned int, ebpf::TableStorage*, bool, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool, char const*)'
collect2: error: ld returned 1 exit status
QGB commented 1 year ago

successful build command:

!/usr/bin/c++ \
-I/home/qgb/github/ProcMon-for-Linux/build/src \
-I/home/qgb/github/ProcMon-for-Linux/build/_deps/bcc-src/src/cc/api \
-I/home/qgb/github/ProcMon-for-Linux/build/_deps/bcc-src/src/cc \
-I/usr/lib/llvm-6.0/include/../tools/clang/include -I/home/qgb/github/ProcMon-for-Linux/build/_deps/bcc-src/src/cc -I/home/qgb/github/ProcMon-for-Linux/build/_deps/bcc-src/src/cc/api -I/home/qgb/github/ProcMon-for-Linux/build/_deps/bcc-src/src/cc/libbpf/include/uapi -I/home/qgb/github/ProcMon-for-Linux/build/_deps/bcc-src/tests/python/include \
-g -fno-omit-frame-pointer -DELPP_THREAD_SAFE -std=gnu++1z \
/home/qgb/github/ProcMon-for-Linux/src/storage/sqlite3_storage_engine.cpp \
/home/qgb/github/ProcMon-for-Linux/src/logging/easylogging++.cc \
/home/qgb/github/ProcMon-for-Linux/src/common/cli_utils.cpp \
/home/qgb/github/ProcMon-for-Linux/src/tracer/ebpf/syscall_schema.cpp  \
/home/qgb/github/ProcMon-for-Linux/src/display/column.cpp \
/home/qgb/github/ProcMon-for-Linux/src/storage/storage_proxy.cpp \
/home/qgb/github/ProcMon-for-Linux/build/src/storage/CMakeFiles/storage-static.dir/mock_storage_engine.cpp.o \
/home/qgb/github/ProcMon-for-Linux/build/src/display/CMakeFiles/display-static.dir/event_formatter.cpp.o \
/home/qgb/github/ProcMon-for-Linux/build/src/tracer/CMakeFiles/tracer-static.dir/ebpf/ebpf_tracer_engine.cpp.o \
/home/qgb/github/ProcMon-for-Linux/build/src/configuration/CMakeFiles/configuration-static.dir/procmon_configuration.cpp.o  \
/home/qgb/github/ProcMon-for-Linux/build/src/display/CMakeFiles/display-static.dir/screen.cpp.o  \
/home/qgb/github/ProcMon-for-Linux/src/qgb.cpp \
-lsqlite3 -lpanel -lncurses -lpthread -lelf \
-lbcc -lbcc_bpf -lbcc -lelf  -lz -lm -lstdc++fs \
/home/qgb/github/ProcMon-for-Linux/build/_deps/bcc-build/src/cc/libbcc.a \
/home/qgb/github/ProcMon-for-Linux/build/_deps/bcc-build/src/cc/frontends/b/libb_frontend.a \
/home/qgb/github/ProcMon-for-Linux/build/_deps/bcc-build/src/cc/frontends/clang/libclang_frontend.a \
-Wl,--whole-archive \
/usr/lib/llvm-6.0/lib/libclangFrontend.a \
/usr/lib/llvm-6.0/lib/libclangSerialization.a \
/usr/lib/llvm-6.0/lib/libclangDriver.a \
/usr/lib/llvm-6.0/lib/libclangParse.a \
/usr/lib/llvm-6.0/lib/libclangSema.a \
/usr/lib/llvm-6.0/lib/libclangCodeGen.a \
/usr/lib/llvm-6.0/lib/libclangAnalysis.a \
/usr/lib/llvm-6.0/lib/libclangRewrite.a \
/usr/lib/llvm-6.0/lib/libclangEdit.a \
/usr/lib/llvm-6.0/lib/libclangAST.a \
/usr/lib/llvm-6.0/lib/libclangLex.a \
/usr/lib/llvm-6.0/lib/libclangBasic.a \
/usr/lib/llvm-6.0/lib/libLLVMBPFDisassembler.a \
/usr/lib/llvm-6.0/lib/libLLVMBPFAsmParser.a \
/usr/lib/llvm-6.0/lib/libLLVMCoroutines.a \
/usr/lib/llvm-6.0/lib/libLLVMCoverage.a \
/usr/lib/llvm-6.0/lib/libLLVMX86CodeGen.a \
/usr/lib/llvm-6.0/lib/libLLVMX86Desc.a \
/usr/lib/llvm-6.0/lib/libLLVMX86Info.a \
/usr/lib/llvm-6.0/lib/libLLVMMCDisassembler.a \
/usr/lib/llvm-6.0/lib/libLLVMX86AsmPrinter.a \
/usr/lib/llvm-6.0/lib/libLLVMX86Utils.a \
/usr/lib/llvm-6.0/lib/libLLVMGlobalISel.a \
/usr/lib/llvm-6.0/lib/libLLVMLTO.a \
/usr/lib/llvm-6.0/lib/libLLVMPasses.a \
/usr/lib/llvm-6.0/lib/libLLVMipo.a \
/usr/lib/llvm-6.0/lib/libLLVMVectorize.a \
/usr/lib/llvm-6.0/lib/libLLVMInstrumentation.a \
/usr/lib/llvm-6.0/lib/libLLVMOption.a \
/usr/lib/llvm-6.0/lib/libLLVMObjCARCOpts.a \
/usr/lib/llvm-6.0/lib/libLLVMMCJIT.a \
/usr/lib/llvm-6.0/lib/libLLVMExecutionEngine.a \
/usr/lib/llvm-6.0/lib/libLLVMRuntimeDyld.a \
/usr/lib/llvm-6.0/lib/libLLVMLinker.a \
/usr/lib/llvm-6.0/lib/libLLVMIRReader.a \
/usr/lib/llvm-6.0/lib/libLLVMAsmParser.a \
/usr/lib/llvm-6.0/lib/libLLVMDebugInfoDWARF.a \
/usr/lib/llvm-6.0/lib/libLLVMBPFCodeGen.a \
/usr/lib/llvm-6.0/lib/libLLVMSelectionDAG.a \
/usr/lib/llvm-6.0/lib/libLLVMBPFDesc.a \
/usr/lib/llvm-6.0/lib/libLLVMBPFInfo.a \
/usr/lib/llvm-6.0/lib/libLLVMBPFAsmPrinter.a \
/usr/lib/llvm-6.0/lib/libLLVMAsmPrinter.a \
/usr/lib/llvm-6.0/lib/libLLVMDebugInfoCodeView.a \
/usr/lib/llvm-6.0/lib/libLLVMDebugInfoMSF.a \
/usr/lib/llvm-6.0/lib/libLLVMCodeGen.a \
/usr/lib/llvm-6.0/lib/libLLVMTarget.a \
/usr/lib/llvm-6.0/lib/libLLVMScalarOpts.a \
/usr/lib/llvm-6.0/lib/libLLVMInstCombine.a \
/usr/lib/llvm-6.0/lib/libLLVMTransformUtils.a \
/usr/lib/llvm-6.0/lib/libLLVMBitWriter.a \
/usr/lib/llvm-6.0/lib/libLLVMAnalysis.a \
/usr/lib/llvm-6.0/lib/libLLVMProfileData.a \
/usr/lib/llvm-6.0/lib/libLLVMObject.a \
/usr/lib/llvm-6.0/lib/libLLVMMCParser.a \
/usr/lib/llvm-6.0/lib/libLLVMMC.a \
/usr/lib/llvm-6.0/lib/libLLVMBitReader.a \
/usr/lib/llvm-6.0/lib/libLLVMCore.a \
/usr/lib/llvm-6.0/lib/libLLVMBinaryFormat.a \
/usr/lib/llvm-6.0/lib/libLLVMSupport.a \
-lrt -ldl -ltinfo -lpthread -lm \
/usr/lib/llvm-6.0/lib/libLLVMDemangle.a \
-Wl,--no-whole-archive -lelf \
/home/qgb/github/ProcMon-for-Linux/build/_deps/bcc-build/src/cc/libbcc_bpf.a \
/home/qgb/github/ProcMon-for-Linux/build/_deps/bcc-build/src/cc/api/libapi-static.a \
/home/qgb/github/ProcMon-for-Linux/build/_deps/bcc-build/src/cc/usdt/libusdt-static.a \
/home/qgb/github/ProcMon-for-Linux/build/_deps/bcc-build/src/cc/libbcc-loader-static.a \
-lelf -lz