eunomia-bpf / bpftime

Userspace eBPF runtime for Observability, Network & General Extensions Framework
https://eunomia.dev/bpftime/
MIT License
815 stars 75 forks source link

[BUG] undefined reference to `shm_open'. `-lrt` missed ? #73

Closed liyan-ah closed 12 months ago

liyan-ah commented 12 months ago

Describe the bug

make release reports error.

To Reproduce

cd bpftime && make release

Expected behavior

Screenshots

gmake[3]: Entering directory '/home/pdliyan/bpftime/build'
[ 95%] Linking CXX executable bpftime_daemon
../runtime/libruntime.a(bpftime_shm_internal.cpp.o): In function `bpftime_remove_global_shm':
bpftime_shm_internal.cpp:(.text+0x4223): undefined reference to `shm_unlink'
../runtime/libruntime.a(bpftime_shm_internal.cpp.o): In function `bpftime::bpftime_shm::bpftime_shm(char const*, bpftime::shm_open_type)':
bpftime_shm_internal.cpp:(.text+0xc9dd): undefined reference to `shm_unlink'
../runtime/libruntime.a(bpftime_shm_internal.cpp.o): In function `boost::interprocess::shared_memory_object::shared_memory_object(boost::interprocess::open_only_t, char const*, boost::interprocess::mode_t)':
bpftime_shm_internal.cpp:(.text._ZN5boost12interprocess20shared_memory_objectC2ENS0_11open_only_tEPKcNS0_6mode_tE[_ZN5boost12interprocess20shared_memory_objectC5ENS0_11open_only_tEPKcNS0_6mode_tE]+0xa1): undefined reference to `shm_open'
../runtime/libruntime.a(bpftime_shm_internal.cpp.o): In function `void boost::interprocess::ipcdetail::managed_open_or_create_impl<boost::interprocess::shared_memory_object, 16ul, true, false>::priv_open_or_create<char const*, boost::interprocess::ipcdetail::create_open_func<boost::interprocess::ipcdetail::basic_managed_memory_impl<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0ul>, 0ul>, boost::interprocess::iset_index, 16ul> > >(boost::interprocess::ipcdetail::create_enum_t, char const* const&, unsigned long, boost::interprocess::mode_t, void const*, boost::interprocess::permissions const&, boost::interprocess::ipcdetail::create_open_func<boost::interprocess::ipcdetail::basic_managed_memory_impl<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0ul>, 0ul>, boost::interprocess::iset_index, 16ul> >)':
bpftime_shm_internal.cpp:(.text._ZN5boost12interprocess9ipcdetail27managed_open_or_create_implINS0_20shared_memory_objectELm16ELb1ELb0EE19priv_open_or_createIPKcNS1_16create_open_funcINS1_25basic_managed_memory_implIcNS0_15rbtree_best_fitINS0_12mutex_familyENS0_10offset_ptrIvlmLm0EEELm0EEENS0_10iset_indexELm16EEEEEEEvNS1_13create_enum_tERKT_mNS0_6mode_tEPKvRKNS0_11permissionsET0_[_ZN5boost12interprocess9ipcdetail27managed_open_or_create_implINS0_20shared_memory_objectELm16ELb1ELb0EE19priv_open_or_createIPKcNS1_16create_open_funcINS1_25basic_managed_memory_implIcNS0_15rbtree_best_fitINS0_12mutex_familyENS0_10offset_ptrIvlmLm0EEELm0EEENS0_10iset_indexELm16EEEEEEEvNS1_13create_enum_tERKT_mNS0_6mode_tEPKvRKNS0_11permissionsET0_]+0x138): undefined reference to `shm_open'
bpftime_shm_internal.cpp:(.text._ZN5boost12interprocess9ipcdetail27managed_open_or_create_implINS0_20shared_memory_objectELm16ELb1ELb0EE19priv_open_or_createIPKcNS1_16create_open_funcINS1_25basic_managed_memory_implIcNS0_15rbtree_best_fitINS0_12mutex_familyENS0_10offset_ptrIvlmLm0EEELm0EEENS0_10iset_indexELm16EEEEEEEvNS1_13create_enum_tERKT_mNS0_6mode_tEPKvRKNS0_11permissionsET0_[_ZN5boost12interprocess9ipcdetail27managed_open_or_create_implINS0_20shared_memory_objectELm16ELb1ELb0EE19priv_open_or_createIPKcNS1_16create_open_funcINS1_25basic_managed_memory_implIcNS0_15rbtree_best_fitINS0_12mutex_familyENS0_10offset_ptrIvlmLm0EEELm0EEENS0_10iset_indexELm16EEEEEEEvNS1_13create_enum_tERKT_mNS0_6mode_tEPKvRKNS0_11permissionsET0_]+0x295): undefined reference to `shm_open'
bpftime_shm_internal.cpp:(.text._ZN5boost12interprocess9ipcdetail27managed_open_or_create_implINS0_20shared_memory_objectELm16ELb1ELb0EE19priv_open_or_createIPKcNS1_16create_open_funcINS1_25basic_managed_memory_implIcNS0_15rbtree_best_fitINS0_12mutex_familyENS0_10offset_ptrIvlmLm0EEELm0EEENS0_10iset_indexELm16EEEEEEEvNS1_13create_enum_tERKT_mNS0_6mode_tEPKvRKNS0_11permissionsET0_[_ZN5boost12interprocess9ipcdetail27managed_open_or_create_implINS0_20shared_memory_objectELm16ELb1ELb0EE19priv_open_or_createIPKcNS1_16create_open_funcINS1_25basic_managed_memory_implIcNS0_15rbtree_best_fitINS0_12mutex_familyENS0_10offset_ptrIvlmLm0EEELm0EEENS0_10iset_indexELm16EEEEEEEvNS1_13create_enum_tERKT_mNS0_6mode_tEPKvRKNS0_11permissionsET0_]+0x448): undefined reference to `shm_open'
collect2: error: ld returned 1 exit status
gmake[3]: *** [daemon/CMakeFiles/bpftime_daemon.dir/build.make:103: daemon/bpftime_daemon] Error 1
gmake[3]: Leaving directory '/home/pdliyan/bpftime/build'
gmake[2]: *** [CMakeFiles/Makefile2:813: daemon/CMakeFiles/bpftime_daemon.dir/all] Error 2
gmake[2]: Leaving directory '/home/pdliyan/bpftime/build'
gmake[1]: *** [Makefile:136: all] Error 2
gmake[1]: Leaving directory '/home/pdliyan/bpftime/build'
make: *** [Makefile:54: release] Error 2

Desktop (please complete the following information):

Additional context

Not familiar with gmake, have to send this bug report. With -lrt flag, my local test code with shm_open works okay.

#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>

int main() {
    // 创建或打开共享内存对象
    int fd = shm_open("/my_shared_memory", O_CREAT | O_RDWR, 0666);
    if (fd == -1) {
        perror("shm_open");
        return 1;
    }

    // 设置共享内存对象的大小
    if (ftruncate(fd, 1024) == -1) {
        perror("ftruncate");
        return 1;
    }

    // 将共享内存映射到进程的地址空间
    void* ptr = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (ptr == MAP_FAILED) {
        perror("mmap");
        return 1;
    }

    // 在共享内存中写入数据
    sprintf((char*)ptr, "Hello, shared memory!");

    // 解除内存映射
    if (munmap(ptr, 1024) == -1) {
        perror("munmap");
        return 1;
    }

    // 关闭共享内存对象
    if (close(fd) == -1) {
        perror("close");
        return 1;
    }

    // 删除共享内存对象
    if (shm_unlink("/my_shared_memory") == -1) {
        perror("shm_unlink");
        return 1;
    }

    printf("okay\n");

    return 0;
}
$ gcc -o sha_e shm.c -lrt && ./sha_e
okay
Officeyutong commented 12 months ago

Which version of compiler are you using?

liyan-ah commented 12 months ago

Which version of compiler are you using?

@Officeyutong

$ gcc --version
gcc (GCC) 12.3.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ --version
g++ (GCC) 12.3.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ clang --version
clang version 16.0.6 (Red Hat 16.0.6-2.module_el8+588+6f71ce7b)
Target: x86_64-redhat-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Officeyutong commented 12 months ago

Seems that I forget to link librt, thanks for your issue! I'll fix it soon

Officeyutong commented 12 months ago

Which version of compiler are you using?

@Officeyutong

$ gcc --version
gcc (GCC) 12.3.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ --version
g++ (GCC) 12.3.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ clang --version
clang version 16.0.6 (Red Hat 16.0.6-2.module_el8+588+6f71ce7b)
Target: x86_64-redhat-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

Hey, could you please help me try https://github.com/Officeyutong/bpftime/tree/fix-issue-73 ?

liyan-ah commented 12 months ago

@Officeyutong Still shm related errors:

Consolidate compiler generated dependencies of target bpftimetool
gmake[3]: Leaving directory '/home/pdliyan/bpftime/build'
gmake[3]: Entering directory '/home/pdliyan/bpftime/build'
[ 98%] Building CXX object tools/bpftimetool/CMakeFiles/bpftimetool.dir/main.cpp.o
[ 98%] Linking CXX executable bpftimetool
../../runtime/libruntime.a(bpftime_shm_internal.cpp.o): In function `bpftime_remove_global_shm':
bpftime_shm_internal.cpp:(.text+0x4273): undefined reference to `shm_unlink'
../../runtime/libruntime.a(bpftime_shm_internal.cpp.o): In function `bpftime::bpftime_shm::bpftime_shm(char const*, bpftime::shm_open_type)':
bpftime_shm_internal.cpp:(.text+0xcf6d): undefined reference to `shm_unlink'
../../runtime/libruntime.a(bpftime_shm_internal.cpp.o): In function `boost::interprocess::shared_memory_object::shared_memory_object(boost::interprocess::open_only_t, char const*, boost::interprocess::mode_t)':
bpftime_shm_internal.cpp:(.text._ZN5boost12interprocess20shared_memory_objectC2ENS0_11open_only_tEPKcNS0_6mode_tE[_ZN5boost12interprocess20shared_memory_objectC5ENS0_11open_only_tEPKcNS0_6mode_tE]+0xa1): undefined reference to `shm_open'
../../runtime/libruntime.a(bpftime_shm_internal.cpp.o): In function `void boost::interprocess::ipcdetail::managed_open_or_create_impl<boost::interprocess::shared_memory_object, 16ul, true, false>::priv_open_or_create<char const*, boost::interprocess::ipcdetail::create_open_func<boost::interprocess::ipcdetail::basic_managed_memory_impl<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0ul>, 0ul>, boost::interprocess::iset_index, 16ul> > >(boost::interprocess::ipcdetail::create_enum_t, char const* const&, unsigned long, boost::interprocess::mode_t, void const*, boost::interprocess::permissions const&, boost::interprocess::ipcdetail::create_open_func<boost::interprocess::ipcdetail::basic_managed_memory_impl<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0ul>, 0ul>, boost::interprocess::iset_index, 16ul> >)':
bpftime_shm_internal.cpp:(.text._ZN5boost12interprocess9ipcdetail27managed_open_or_create_implINS0_20shared_memory_objectELm16ELb1ELb0EE19priv_open_or_createIPKcNS1_16create_open_funcINS1_25basic_managed_memory_implIcNS0_15rbtree_best_fitINS0_12mutex_familyENS0_10offset_ptrIvlmLm0EEELm0EEENS0_10iset_indexELm16EEEEEEEvNS1_13create_enum_tERKT_mNS0_6mode_tEPKvRKNS0_11permissionsET0_[_ZN5boost12interprocess9ipcdetail27managed_open_or_create_implINS0_20shared_memory_objectELm16ELb1ELb0EE19priv_open_or_createIPKcNS1_16create_open_funcINS1_25basic_managed_memory_implIcNS0_15rbtree_best_fitINS0_12mutex_familyENS0_10offset_ptrIvlmLm0EEELm0EEENS0_10iset_indexELm16EEEEEEEvNS1_13create_enum_tERKT_mNS0_6mode_tEPKvRKNS0_11permissionsET0_]+0x138): undefined reference to `shm_open'
bpftime_shm_internal.cpp:(.text._ZN5boost12interprocess9ipcdetail27managed_open_or_create_implINS0_20shared_memory_objectELm16ELb1ELb0EE19priv_open_or_createIPKcNS1_16create_open_funcINS1_25basic_managed_memory_implIcNS0_15rbtree_best_fitINS0_12mutex_familyENS0_10offset_ptrIvlmLm0EEELm0EEENS0_10iset_indexELm16EEEEEEEvNS1_13create_enum_tERKT_mNS0_6mode_tEPKvRKNS0_11permissionsET0_[_ZN5boost12interprocess9ipcdetail27managed_open_or_create_implINS0_20shared_memory_objectELm16ELb1ELb0EE19priv_open_or_createIPKcNS1_16create_open_funcINS1_25basic_managed_memory_implIcNS0_15rbtree_best_fitINS0_12mutex_familyENS0_10offset_ptrIvlmLm0EEELm0EEENS0_10iset_indexELm16EEEEEEEvNS1_13create_enum_tERKT_mNS0_6mode_tEPKvRKNS0_11permissionsET0_]+0x295): undefined reference to `shm_open'
bpftime_shm_internal.cpp:(.text._ZN5boost12interprocess9ipcdetail27managed_open_or_create_implINS0_20shared_memory_objectELm16ELb1ELb0EE19priv_open_or_createIPKcNS1_16create_open_funcINS1_25basic_managed_memory_implIcNS0_15rbtree_best_fitINS0_12mutex_familyENS0_10offset_ptrIvlmLm0EEELm0EEENS0_10iset_indexELm16EEEEEEEvNS1_13create_enum_tERKT_mNS0_6mode_tEPKvRKNS0_11permissionsET0_[_ZN5boost12interprocess9ipcdetail27managed_open_or_create_implINS0_20shared_memory_objectELm16ELb1ELb0EE19priv_open_or_createIPKcNS1_16create_open_funcINS1_25basic_managed_memory_implIcNS0_15rbtree_best_fitINS0_12mutex_familyENS0_10offset_ptrIvlmLm0EEELm0EEENS0_10iset_indexELm16EEEEEEEvNS1_13create_enum_tERKT_mNS0_6mode_tEPKvRKNS0_11permissionsET0_]+0x448): undefined reference to `shm_open'
collect2: error: ld returned 1 exit status
gmake[3]: *** [tools/bpftimetool/CMakeFiles/bpftimetool.dir/build.make:104: tools/bpftimetool/bpftimetool] Error 1
gmake[3]: Leaving directory '/home/pdliyan/bpftime/build'
gmake[2]: *** [CMakeFiles/Makefile2:868: tools/bpftimetool/CMakeFiles/bpftimetool.dir/all] Error 2
gmake[2]: Leaving directory '/home/pdliyan/bpftime/build'
gmake[1]: *** [Makefile:136: all] Error 2
gmake[1]: Leaving directory '/home/pdliyan/bpftime/build'
make: *** [Makefile:54: release] Error 2
liyan-ah commented 12 months ago

Seems that tools/bpftimetool missed librt as well.

liyan-ah commented 12 months ago

Same problem in runtime/unit-test and runtime/test.

Officeyutong commented 12 months ago

Same problem in runtime/unit-test and runtime/test.

I've linked librt at runtime and bpftimetool, could you please help me try again?

liyan-ah commented 12 months ago

Same problem in runtime/unit-test and runtime/test.

I've linked librt at runtime and bpftimetool, could you please help me try again?

@Officeyutong Those errors are reported with your pushes.

Besides, runtime/agent/CMakeLists.txt need -lrt.

liyan-ah commented 12 months ago

Those are my local changes, hope it will help:

[pdliyan@134dc04b695e bpftime]$ git diff daemon/CMakeLists.txt
[pdliyan@134dc04b695e bpftime]$ git st .
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
  (commit or discard the untracked or modified content in submodules)
    modified:   bpftime-verifier/ebpf-verifier (modified content)
    modified:   runtime/agent/CMakeLists.txt
    modified:   runtime/test/CMakeLists.txt
    modified:   runtime/test/bpf/Makefile
    modified:   runtime/unit-test/CMakeLists.txt
    modified:   third_party/bpftool (modified content)
    modified:   tools/bpftimetool/CMakeLists.txt

no changes added to commit (use "git add" and/or "git commit -a")
Officeyutong commented 12 months ago

Those are my local changes, hope it will help:

[pdliyan@134dc04b695e bpftime]$ git diff daemon/CMakeLists.txt
[pdliyan@134dc04b695e bpftime]$ git st .
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
  (commit or discard the untracked or modified content in submodules)
  modified:   bpftime-verifier/ebpf-verifier (modified content)
  modified:   runtime/agent/CMakeLists.txt
  modified:   runtime/test/CMakeLists.txt
  modified:   runtime/test/bpf/Makefile
  modified:   runtime/unit-test/CMakeLists.txt
  modified:   third_party/bpftool (modified content)
  modified:   tools/bpftimetool/CMakeLists.txt

no changes added to commit (use "git add" and/or "git commit -a")

I've updated CMakeLists in runtime/agent, runtime/test, runtime/unit-test, tools/bpftimetool

liyan-ah commented 12 months ago

Those are my local changes, hope it will help:

[pdliyan@134dc04b695e bpftime]$ git diff daemon/CMakeLists.txt
[pdliyan@134dc04b695e bpftime]$ git st .
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
  (commit or discard the untracked or modified content in submodules)
    modified:   bpftime-verifier/ebpf-verifier (modified content)
    modified:   runtime/agent/CMakeLists.txt
    modified:   runtime/test/CMakeLists.txt
    modified:   runtime/test/bpf/Makefile
    modified:   runtime/unit-test/CMakeLists.txt
    modified:   third_party/bpftool (modified content)
    modified:   tools/bpftimetool/CMakeLists.txt

no changes added to commit (use "git add" and/or "git commit -a")

I've updated CMakeLists in runtime/agent, runtime/test, runtime/unit-test, tools/bpftimetool

Works okay in my local environment.