iovisor / bcc

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

memleak reports asm/types.h file not found #3731

Open cydrain opened 2 years ago

cydrain commented 2 years ago

I build bcc from source successfully, but when I run memleak, it reports this error:

❯ ./memleak -p 3698
In file included from <built-in>:2:
In file included from /virtual/include/bcc/bpf.h:12:
In file included from include/linux/types.h:6:
include/uapi/linux/types.h:5:10: fatal error: 'asm/types.h' file not found
#include <asm/types.h>
         ^~~~~~~~~~~~~
1 error generated.
Traceback (most recent call last):
  File "./memleak", line 427, in <module>
    bpf = BPF(text=bpf_source)
  File "/usr/lib/python3/dist-packages/bcc/__init__.py", line 479, in __init__
    raise Exception("Failed to compile BPF module %s" % (src_file or "<text>"))
Exception: Failed to compile BPF module <text>

And actually this file "asm/types.h" exists in the default include directory:

❯ ll /usr/include/x86_64-linux-gnu/asm/types.h
-rw-r--r-- 1 root root 152 Nov  5 19:22 /usr/include/x86_64-linux-gnu/asm/types.h

My system is ubuntu 18.04. Can someone help to solve this issue?

chenhengqi commented 2 years ago

Do you have kernel-devel or kernel-headers packages installed ?

chenhengqi commented 2 years ago

Try this:

$ sudo ln -s /usr/include/x86_64-linux-gnu/asm /usr/include/asm
cydrain commented 2 years ago

Hi @chenhengqi , I have tried both of your suggestions, it does not work.

netedwardwu commented 2 years ago
include/uapi/linux/types.h:5:10: fatal error: 'asm/types.h' file not found
#include <asm/types.h>
         ^~~~~~~~~~~~~

It's should be types.h in Linux kernel source instead of gnu header /usr/include/x86_64-linux-gnu Maybe you should make sure you have types.h in your kernel source which BPF references.

yonghong-song commented 2 years ago

@cydrain As folks already mentioned, the path for types.h (/usr/include/x86_64-linux-gnu/asm/types.h) is not correct. You should install kernel-devel package. The headers should be in build/arch/x86/include/generated/uapi/asm/types.h.

cydrain commented 2 years ago

Hi @yonghong-song , I have "types.h" under this directory, is it ok ?

❯ pwd
/usr/src/linux-headers-5.4.0-90-generic/arch/x86/include/generated/uapi/asm
❯ ll types.h
-rw-r--r-- 1 root root 31 Oct 22 16:46 types.h
yonghong-song commented 2 years ago

Looks like your kernel src is in a different location. Maybe you can try BCC_KERNEL_SOURCE env variable? Please take a look at the following patch for details

commit 11f3a2739be5c1659c8459fd1b20a99bbd3c35ae
Author: Joel Fernandes <joelaf@google.com>
Date:   Thu Feb 1 20:59:19 2018 -0500

    clang/loader: Add support for specifying absolute path of kernel sources

    Many developers have kernel sources at an specific location. Allow
    clang to build from there by allowing to specify a single absolute
    path to the kernel sources through a new env var BCC_KERNEL_SOURCE.
cydrain commented 2 years ago

Hi @yonghong-song , after specify BCC_KERNEL_SOURCE, it reports another error.

❯ export BCC_KERNEL_SOURCE=/usr/src/linux-headers-5.4.0-90-generic/arch/x86
❯ ./hello_world.py
modprobe: ERROR: could not insert 'kheaders': Operation not permitted
Unable to find kernel headers. Try rebuilding kernel with CONFIG_IKHEADERS=m (module) or installing the kernel development package for your running kernel version.
<built-in>:1:10: fatal error: './include/linux/kconfig.h' file not found
#include "./include/linux/kconfig.h"
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
Traceback (most recent call last):
  File "./hello_world.py", line 12, in <module>
    BPF(text='int kprobe__sys_clone(void *ctx) { bpf_trace_printk("Hello, World!\\n"); return 0; }').trace_print()
  File "/usr/lib/python3/dist-packages/bcc/__init__.py", line 479, in __init__
    raise Exception("Failed to compile BPF module %s" % (src_file or "<text>"))
Exception: Failed to compile BPF module <text>

❯ grep CONFIG_IKHEADERS /boot/config-5.4.0-90-generic
131:CONFIG_IKHEADERS=m
ismhong commented 2 years ago

Hi @cydrain

export BCC_KERNEL_SOURCE=/usr/src/linux-headers-5.4.0-90-generic/arch/x86

The BCC_KERNEL_SOURCE should point to the root of kernel header, in your case, it should be something like below without ARCH path.

export BCC_KERNEL_SOURCE=/usr/src/linux-headers-5.4.0-90-generic/

Could you please take a shoot? Thank you.

incapdns commented 2 years ago

Try this:

$ sudo ln -s /usr/include/x86_64-linux-gnu/asm /usr/include/asm

Solved, thanks @chenhengqi!