Commit 66c549b0 authored by Alastair Robertson's avatar Alastair Robertson

ClangParser: Don't exit if kernel headers are not found

- KBuildHelper takes kdir, not kpath, as a parameter.
- Copy BCC and set has_kpath_source to false when reading
  kpath from environment variable
parent e25f0498
...@@ -4,13 +4,6 @@ BPFtrace is a high-level tracing language for Linux enhanced Berkeley Packet Fil ...@@ -4,13 +4,6 @@ BPFtrace is a high-level tracing language for Linux enhanced Berkeley Packet Fil
To learn more about BPFtrace, see the [Reference Guide](docs/reference_guide.md) and [One-Liner Tutorial](docs/tutorial_one_liners.md). To learn more about BPFtrace, see the [Reference Guide](docs/reference_guide.md) and [One-Liner Tutorial](docs/tutorial_one_liners.md).
BPFTrace depends on the kernel headers which are searched for by default in:
```bash
/lib/modules/$(uname -r)
```
The default search directory could be overridden using the environment variable BPFTRACE_KERNEL_HEADERS.
## Install ## Install
For build and install instructions, see [INSTALL.md](INSTALL.md). For build and install instructions, see [INSTALL.md](INSTALL.md).
......
...@@ -1752,3 +1752,13 @@ BPF programs that operate on many data items may hit this limit. There are a num ...@@ -1752,3 +1752,13 @@ BPF programs that operate on many data items may hit this limit. There are a num
1. Split your program over multiple probes. 1. Split your program over multiple probes.
1. Check the status of the BPF stack limit in Linux (it may be increased in the future, maybe as a tuneabe). 1. Check the status of the BPF stack limit in Linux (it may be increased in the future, maybe as a tuneabe).
1. (advanced): Run -d and examine the LLVM IR, and look for ways to optimize src/ast/codegen_llvm.cpp. 1. (advanced): Run -d and examine the LLVM IR, and look for ways to optimize src/ast/codegen_llvm.cpp.
## 2. Kernel headers not found
bpftrace requires kernel headers for certain features, which are searched for by default in:
```bash
/lib/modules/$(uname -r)
```
The default search directory can be overridden using the environment variable `BPFTRACE_KERNEL_SOURCE`.
...@@ -108,7 +108,7 @@ static bool is_dir(const std::string& path) ...@@ -108,7 +108,7 @@ static bool is_dir(const std::string& path)
static std::pair<bool, std::string> get_kernel_path_info(const std::string kdir) static std::pair<bool, std::string> get_kernel_path_info(const std::string kdir)
{ {
if (is_dir(kdir + "/build") && is_dir(kdir + "/source")) if (is_dir(kdir + "/build") && is_dir(kdir + "/source"))
return std::make_pair (true, "source"); return std::make_pair(true, "source");
return std::make_pair(false, "build"); return std::make_pair(false, "build");
} }
...@@ -159,32 +159,23 @@ void ClangParser::parse(ast::Program *program, StructMap &structs) ...@@ -159,32 +159,23 @@ void ClangParser::parse(ast::Program *program, StructMap &structs)
struct utsname utsname; struct utsname utsname;
uname(&utsname); uname(&utsname);
const char* env_kernel_modules_dir = ::getenv("BPFTRACE_KERNEL_HEADERS"); const char *kpath_env = ::getenv("BPFTRACE_KERNEL_SOURCE");
std::string kernel_modules_dir = env_kernel_modules_dir? std::string kdir = kpath_env ?
std::string(env_kernel_modules_dir): std::string(kpath_env) :
std::string("/lib/modules/") + std::string("/lib/modules/") + utsname.release;
utsname.release;
if (!is_dir(kernel_modules_dir)){
std::cerr << "WARNING: ("
<< kernel_modules_dir << ") is not a valid dir" << std::endl;
std::cerr << "Use environment variable BPFTRACE_KERNEL_HEADERS to setup"
" a valid directory for kernel headers." << std::endl;
exit(EXIT_FAILURE);
}
auto kpath_info = get_kernel_path_info(kernel_modules_dir); auto kpath_info = get_kernel_path_info(kdir);
auto kpath = env_kernel_modules_dir? auto kpath = kpath_env ?
kernel_modules_dir : kdir :
kernel_modules_dir + "/" + kpath_info.second; kdir + "/" + kpath_info.second;
bool has_kpath_source = env_kernel_modules_dir? true:kpath_info.first; bool has_kpath_source = kpath_env ? false : kpath_info.first;
std::vector<std::string> kflags; std::vector<std::string> kflags;
ebpf::DirStack dstack(kpath); ebpf::DirStack dstack(kpath);
if (dstack.ok()) if (dstack.ok())
{ {
ebpf::KBuildHelper kbuild_helper(kpath, has_kpath_source); ebpf::KBuildHelper kbuild_helper(kdir, has_kpath_source);
kbuild_helper.get_flags(utsname.machine, &kflags); kbuild_helper.get_flags(utsname.machine, &kflags);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment