Skip to content

Commit c58483a

Browse files
Copilotr41k0u
andcommitted
Fix documentation: correct comm() usage, XDP types, copyright year, and add uv support
Co-authored-by: r41k0u <76248539+r41k0u@users.noreply.github.com>
1 parent 2d8c6c1 commit c58483a

File tree

9 files changed

+82
-41
lines changed

9 files changed

+82
-41
lines changed

docs/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ This directory contains the Sphinx documentation for PythonBPF.
88

99
Install the documentation dependencies:
1010

11+
**Using uv (recommended):**
12+
```bash
13+
uv pip install -r requirements.txt
14+
# Or install the optional docs dependencies
15+
uv pip install pythonbpf[docs]
16+
```
17+
18+
**Using pip:**
1119
```bash
1220
pip install -r requirements.txt
1321
# Or install the optional docs dependencies

docs/api/index.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ Ring buffer for efficient event delivery.
327327
### Process Information
328328

329329
* `pid()` - Get current process ID
330-
* `comm()` - Get current process command name
330+
* `comm(buf)` - Get current process command name (requires buffer parameter)
331331
* `uid()` - Get current user ID
332332

333333
### Time
@@ -450,7 +450,8 @@ def track_exec(ctx: c_void_p) -> c_int64:
450450
event = Event()
451451
event.timestamp = ktime()
452452
event.pid = pid()
453-
event.comm = comm()
453+
# Note: comm() requires a buffer parameter
454+
# comm(event.comm) # Fills event.comm with process name
454455

455456
events.output(event)
456457
return c_int64(0)

docs/conf.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
1414

1515
project = 'PythonBPF'
16-
copyright = '2024, r41k0u, varun-r-mallya'
16+
copyright = '2024-2026, r41k0u, varun-r-mallya'
1717
author = 'r41k0u, varun-r-mallya'
1818
release = '0.1.8'
1919
version = '0.1.8'
@@ -90,9 +90,6 @@
9090
'titles_only': False
9191
}
9292

93-
# Add any paths that contain custom static files (such as style sheets)
94-
html_static_path = ['_static']
95-
9693
# -- Options for autodoc -----------------------------------------------------
9794

9895
autodoc_default_options = {

docs/getting-started/installation.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,14 @@ The `llvm` package provides `llc`, the LLVM compiler that is used to compile LLV
4343

4444
### From PyPI (Recommended)
4545

46-
The easiest way to install PythonBPF is using pip:
46+
The easiest way to install PythonBPF is using uv or pip:
4747

48+
**Using uv (recommended):**
49+
```bash
50+
uv pip install pythonbpf pylibbpf
51+
```
52+
53+
**Using pip:**
4854
```bash
4955
pip install pythonbpf pylibbpf
5056
```
@@ -73,6 +79,13 @@ source .venv/bin/activate # On Windows: .venv\Scripts\activate
7379

7480
3. Install in development mode:
7581

82+
**Using uv (recommended):**
83+
```bash
84+
uv pip install -e .
85+
uv pip install pylibbpf
86+
```
87+
88+
**Using pip:**
7689
```bash
7790
pip install -e .
7891
pip install pylibbpf
@@ -88,6 +101,14 @@ make install
88101

89102
If you want to build the documentation locally:
90103

104+
**Using uv (recommended):**
105+
```bash
106+
uv pip install pythonbpf[docs]
107+
# Or from the repository root:
108+
uv pip install -e .[docs]
109+
```
110+
111+
**Using pip:**
91112
```bash
92113
pip install pythonbpf[docs]
93114
# Or from the repository root:
@@ -100,6 +121,12 @@ Some examples require access to kernel data structures. To use these features, y
100121

101122
1. Install additional dependencies:
102123

124+
**Using uv (recommended):**
125+
```bash
126+
uv pip install ctypeslib2
127+
```
128+
129+
**Using pip:**
103130
```bash
104131
pip install ctypeslib2
105132
```
@@ -151,8 +178,8 @@ If you get errors about `llc` or `clang` not being found:
151178
If Python can't find the `pythonbpf` module:
152179

153180
* Make sure you've activated your virtual environment
154-
* Verify installation with `pip list | grep pythonbpf`
155-
* Try reinstalling: `pip install --force-reinstall pythonbpf`
181+
* Verify installation with `uv pip list | grep pythonbpf` or `pip list | grep pythonbpf`
182+
* Try reinstalling: `uv pip install --force-reinstall pythonbpf` or `pip install --force-reinstall pythonbpf`
156183

157184
## Next Steps
158185

docs/getting-started/quickstart.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,14 @@ Let's make a more interesting program that tracks which processes are being crea
120120

121121
```python
122122
from pythonbpf import bpf, section, bpfglobal, BPF, trace_pipe
123-
from pythonbpf.helper import pid, comm
123+
from pythonbpf.helper import pid
124124
from ctypes import c_void_p, c_int64
125125

126126
@bpf
127127
@section("tracepoint/syscalls/sys_enter_execve")
128128
def track_exec(ctx: c_void_p) -> c_int64:
129129
process_id = pid()
130-
process_name = comm()
131-
print(f"Process {process_name} (PID: {process_id}) is starting")
130+
print(f"Process with PID: {process_id} is starting")
132131
return c_int64(0)
133132

134133
@bpf
@@ -145,7 +144,6 @@ trace_pipe()
145144
This program uses BPF helper functions:
146145

147146
* `pid()` - Gets the current process ID
148-
* `comm()` - Gets the current process command name
149147

150148
Run it with `sudo python3 track_exec.py` and watch processes being created!
151149

@@ -182,12 +180,11 @@ def trace_open(ctx: c_void_p) -> c_int64:
182180
For network packet processing:
183181

184182
```python
185-
from ctypes import c_uint32
183+
from pythonbpf.helper import XDP_PASS
186184

187185
@section("xdp")
188-
def xdp_pass(ctx: c_void_p) -> c_uint32:
189-
# XDP_PASS = 2
190-
return c_uint32(2)
186+
def xdp_pass(ctx: c_void_p) -> c_int64:
187+
return XDP_PASS
191188
```
192189

193190
## Best Practices

docs/user-guide/decorators.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,13 @@ def trace_open_return(ctx):
108108
For network packet processing at the earliest point:
109109

110110
```python
111-
from ctypes import c_uint32
111+
from pythonbpf.helper import XDP_PASS
112+
from ctypes import c_void_p, c_int64
112113

113114
@section("xdp")
114-
def xdp_prog(ctx: c_void_p) -> c_uint32:
115-
# XDP_PASS = 2, XDP_DROP = 1, XDP_ABORTED = 0
116-
return c_uint32(2)
115+
def xdp_prog(ctx: c_void_p) -> c_int64:
116+
# XDP_PASS, XDP_DROP, XDP_ABORTED constants available from pythonbpf.helper
117+
return XDP_PASS
117118
```
118119

119120
#### TC (Traffic Control)
@@ -257,7 +258,8 @@ def track_processes(ctx: c_void_p) -> c_int64:
257258
event = ProcessEvent()
258259
event.timestamp = ktime()
259260
event.pid = pid()
260-
event.comm = comm()
261+
# Note: comm() requires a buffer parameter
262+
# comm(event.comm) # Fills event.comm with process name
261263

262264
events.output(event)
263265
return c_int64(0)

docs/user-guide/helpers.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,19 @@ from pythonbpf.helper import comm
3939
@bpf
4040
@section("tracepoint/syscalls/sys_enter_execve")
4141
def trace_exec(ctx: c_void_p) -> c_int64:
42-
process_name = comm()
42+
# comm requires a buffer to fill
43+
process_name = str(16)
44+
comm(process_name)
4345
print(f"Executing: {process_name}")
4446
return c_int64(0)
4547
```
4648

47-
**Returns:** `str(16)` - The command name of the current task
49+
**Parameters:**
50+
* `buf` - Buffer to fill with the process command name
51+
52+
**Returns:** `c_int64` - 0 on success, negative on error
4853

49-
**Note:** The returned string is limited to 16 characters (TASK_COMM_LEN).
54+
**Note:** The buffer should be at least 16 bytes (TASK_COMM_LEN) to hold the full command name.
5055

5156
#### uid()
5257

@@ -406,17 +411,16 @@ trace_pipe()
406411

407412
```python
408413
from pythonbpf import bpf, section, bpfglobal, BPF, trace_pipe
409-
from pythonbpf.helper import pid, comm, uid
414+
from pythonbpf.helper import pid, uid
410415
from ctypes import c_void_p, c_int64
411416

412417
@bpf
413418
@section("tracepoint/syscalls/sys_enter_execve")
414419
def track_exec(ctx: c_void_p) -> c_int64:
415420
process_id = pid()
416-
process_name = comm()
417421
user_id = uid()
418422

419-
print(f"User {user_id} started {process_name} (PID: {process_id})")
423+
print(f"User {user_id} started process (PID: {process_id})")
420424
return c_int64(0)
421425

422426
@bpf
@@ -477,7 +481,7 @@ for cpu, count in map_obj.items():
477481

478482
```python
479483
from pythonbpf import bpf, section, bpfglobal, BPF, trace_pipe
480-
from pythonbpf.helper import random, pid, comm
484+
from pythonbpf.helper import random, pid
481485
from ctypes import c_void_p, c_int64
482486

483487
@bpf
@@ -486,8 +490,7 @@ def sample_opens(ctx: c_void_p) -> c_int64:
486490
# Sample 5% of events
487491
if (random() % 100) < 5:
488492
process_id = pid()
489-
process_name = comm()
490-
print(f"Sampled: {process_name} ({process_id}) opening file")
493+
print(f"Sampled: PID {process_id} opening file")
491494

492495
return c_int64(0)
493496

docs/user-guide/maps.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ def log_exec(ctx: c_void_p) -> c_int64:
215215
event = ProcessEvent()
216216
event.timestamp = ktime()
217217
event.pid = pid()
218-
event.comm = comm()
218+
# Note: comm() requires a buffer parameter
219+
# comm(event.comm) # Fills event.comm with process name
219220
events.output(event)
220221
return c_int64(0)
221222

docs/user-guide/structs.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,11 @@ def capture_event(ctx: c_void_p) -> c_int64:
119119
# Set fields
120120
event.timestamp = ktime()
121121
event.pid = pid()
122-
event.comm = comm()
122+
# Note: comm() requires a buffer parameter to fill
123+
# comm(event.comm) # Fills event.comm with process name
123124

124125
# Use the struct
125-
print(f"Process {event.comm} with PID {event.pid}")
126+
print(f"Process with PID {event.pid}")
126127

127128
return c_int64(0)
128129
```
@@ -204,7 +205,8 @@ def trace_fork(ctx: c_void_p) -> c_int64:
204205
event = ProcessEvent()
205206
event.timestamp = ktime()
206207
event.pid = pid()
207-
event.comm = comm()
208+
# Note: comm() requires a buffer parameter
209+
# comm(event.comm) # Fills event.comm with process name
208210

209211
# Send to userspace
210212
events.output(event)
@@ -265,7 +267,8 @@ Assign values to fields:
265267
event = Event()
266268
event.timestamp = ktime()
267269
event.pid = pid()
268-
event.comm = comm()
270+
# Note: comm() requires a buffer parameter
271+
# comm(event.comm) # Fills event.comm with process name
269272
```
270273

271274
### String Fields
@@ -285,8 +288,8 @@ def example(ctx: c_void_p) -> c_int64:
285288
# Assign string value
286289
msg.text = "Hello from BPF"
287290

288-
# Use helper to get process name
289-
msg.text = comm()
291+
# Use helper to get process name (requires buffer)
292+
# comm(msg.text) # Fills msg.text with process name
290293

291294
return c_int64(0)
292295
```
@@ -316,6 +319,7 @@ class MyStruct:
316319
```python
317320
from pythonbpf import bpf, struct, map, section
318321
from pythonbpf.maps import RingBuffer
322+
from pythonbpf.helper import ktime, XDP_PASS
319323
from ctypes import c_void_p, c_int64, c_uint8, c_uint16, c_uint32, c_uint64
320324

321325
@bpf
@@ -336,15 +340,15 @@ def packets() -> RingBuffer:
336340

337341
@bpf
338342
@section("xdp")
339-
def capture_packets(ctx: c_void_p) -> c_uint32:
343+
def capture_packets(ctx: c_void_p) -> c_int64:
340344
pkt = PacketEvent()
341345
pkt.timestamp = ktime()
342346
# Parse packet data from ctx...
343347

344348
packets.output(pkt)
345349

346350
# XDP_PASS
347-
return c_uint32(2)
351+
return XDP_PASS
348352
```
349353

350354
### Process Lifecycle Tracking
@@ -377,7 +381,8 @@ def track_fork(ctx: c_void_p) -> c_int64:
377381
info = ProcessLifecycle()
378382
info.pid = process_id
379383
info.start_time = ktime()
380-
info.comm = comm()
384+
# Note: comm() requires a buffer parameter
385+
# comm(info.comm) # Fills info.comm with process name
381386

382387
process_info.update(process_id, info)
383388

0 commit comments

Comments
 (0)