Comments (9)
@mrpre you can prevent bpf_printk() from using global variables by defining BPF_NO_GLOBAL_DATA before including bpf_helpers.h header.
BCC implementation uses suboptimal implementation that requires on the stack variables and their implementation (which libbpf will do if BPF_NO_GLOBAL_DATA is defined)
from libbpf-bootstrap.
How about we add minimal_legacy
example which will define BPF_NO_GLOBAL_DATA (to avoid global variables for bpf_printk()) and do the map as you shown above?
from libbpf-bootstrap.
Your kernel is too old for this example.
To get it work, you need to remove my_pid
(you can use a map instead) and bpf_printk
.
from libbpf-bootstrap.
@chenhengqi
It seems that bpf_printk
with constant string works well under BCC.
So how BCC process the constant string used by bpf_printk
? Does BCC replace or remove .rodata
sections and use another way to to make it work ?
from libbpf-bootstrap.
@anakryiko Thanks a lot, it works after I remove my_pid
and add #define BPF_NO_GLOBAL_DATA
in bpf code.
from libbpf-bootstrap.
From the README: minimal is just that – a minimal practical BPF application example. It doesn't use or require BPF CO-RE, so should run on quite old kernels.
Obviously, my Ubuntu 18.04 kernel 4.15.0-180 is too quite an old a kernel. For someone just starting out with bpf, it would be helpful to have an example on how to avoid the global variable (my_pid) by using a map.
from libbpf-bootstrap.
From the README:
minimal is just that – a minimal practical BPF application example. It doesn't use or require BPF CO-RE, so should run on quite old kernels.
Obviously, my Ubuntu 18.04 kernel 4.15.0-180 is too quite an old a kernel. For someone just starting out with bpf, it would be helpful to have an example on how to avoid the global variable (my_pid) by using a map.
For anyone else running across this, I was able to change this to use an array. my_pid becomes this:
// Create an array with 1 entry instead of a global variable
// which does not work with older kernels
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 1);
__type(key, u32);
__type(value, pid_t);
} my_pid SEC(".maps");
The tracepoint hook now contains this:
SEC("tp/syscalls/sys_enter_write")
int handle_tp(void *ctx)
{
u32 index = 0;
pid_t pid = bpf_get_current_pid_tgid() >> 32;
pid_t *user_pid = bpf_map_lookup_elem(&my_pid, &index);
if (user_pid)
{
if (*user_pid == pid)
bpf_printk("BPF triggered from PID %d.\n", pid);
}
return 0;
}
and the user-space program sets it's pid into the first element of the array after the the skeleton load:
/* ensure BPF program only handles write() syscalls from our process */
pid = getpid();
bpf_map__update_elem(skel->maps.my_pid, &index, sizeof(index), &pid, sizeof(pid_t), BPF_ANY);
where index is defined unsigned index = 0;
from libbpf-bootstrap.
How about we add
minimal_legacy
example which will define BPF_NO_GLOBAL_DATA (to avoid global variables for bpf_printk()) and do the map as you shown above?
PR #84 adds this example.
from libbpf-bootstrap.
We can close this now that PR #84 has been merged.
from libbpf-bootstrap.
Related Issues (20)
- Rust: How to attach a single bpf program to multiple network interfaces? HOT 3
- Make failed HOT 2
- Make error - poisoned strlcpy HOT 1
- Android xmake fails HOT 10
- add xdp synproxy in c example? HOT 2
- run fentry examples in Loongarch cpu failed HOT 2
- error: unable to create target: 'No available targets are compatible with triple "bpf"' HOT 1
- Does libbpf-bootstrap support sk_msg/stream_parser & sk_msg/stream_parser hook ?
- example uprobe no output HOT 4
- (ARM64) Rust Example Tracecon Does Not Compile HOT 2
- [Android] Failed to find valid kernel BTF HOT 19
- How to read arbitrary len bytes using helper bpf_skb_load_bytes()? HOT 2
- To compile the Rust example in libbpf-bootstrap for use on the Android platform or arm64 , what should I do? HOT 1
- when compile the Rust example in libbpf-bootstrap for target arm64 ,why build.rs use vmlinux.h is x86 ? HOT 8
- unknown func bpf_kallsyms_lookup_name#179 HOT 1
- Can you please add a bpf_for() example? HOT 1
- minimal_ns doesn't compile HOT 1
- Compilation failed, missing a header file? HOT 6
- make failed under `examples/c` with `gimli = "^0.28"` HOT 10
- error: could not compile `gimli` (lib) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from libbpf-bootstrap.