Skip to content

Commit d9dfb61

Browse files
Add remaining docstrings to complete documentation coverage
Co-authored-by: varun-r-mallya <[email protected]>
1 parent cdf4f3e commit d9dfb61

File tree

10 files changed

+70
-0
lines changed

10 files changed

+70
-0
lines changed

pythonbpf/debuginfo/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Debug information generation for BPF programs (DWARF/BTF)."""
2+
13
from .dwarf_constants import * # noqa: F403
24
from .dtypes import * # noqa: F403
35
from .debug_info_generator import DebugInfoGenerator

pythonbpf/debuginfo/debug_info_generator.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,31 @@
88

99

1010
class DebugInfoGenerator:
11+
"""
12+
Generator for DWARF/BTF debug information in LLVM IR modules.
13+
14+
This class provides methods to create debug metadata for BPF programs,
15+
including types, structs, globals, and compilation units.
16+
"""
17+
1118
def __init__(self, module):
19+
"""
20+
Initialize the debug info generator.
21+
22+
Args:
23+
module: LLVM IR module to attach debug info to
24+
"""
1225
self.module = module
1326
self._type_cache = {} # Cache for common debug types
1427

1528
def generate_file_metadata(self, filename, dirname):
29+
"""
30+
Generate file metadata for debug info.
31+
32+
Args:
33+
filename: Name of the source file
34+
dirname: Directory containing the source file
35+
"""
1636
self.module._file_metadata = self.module.add_debug_info(
1737
"DIFile",
1838
{ # type: ignore
@@ -24,6 +44,15 @@ def generate_file_metadata(self, filename, dirname):
2444
def generate_debug_cu(
2545
self, language, producer: str, is_optimized: bool, is_distinct: bool
2646
):
47+
"""
48+
Generate debug compile unit metadata.
49+
50+
Args:
51+
language: DWARF language code (e.g., DW_LANG_C11)
52+
producer: Compiler/producer string
53+
is_optimized: Whether the code is optimized
54+
is_distinct: Whether the compile unit should be distinct
55+
"""
2756
self.module._debug_compile_unit = self.module.add_debug_info(
2857
"DICompileUnit",
2958
{ # type: ignore
@@ -83,6 +112,16 @@ def create_array_type(self, base_type: Any, count: int) -> Any:
83112

84113
@staticmethod
85114
def _compute_array_size(base_type: Any, count: int) -> int:
115+
"""
116+
Compute the size of an array in bits.
117+
118+
Args:
119+
base_type: The base type of the array
120+
count: Number of elements in the array
121+
122+
Returns:
123+
Total size in bits
124+
"""
86125
# Extract size from base_type if possible
87126
# For simplicity, assuming base_type has a size attribute
88127
return getattr(base_type, "size", 32) * count

pythonbpf/debuginfo/dtypes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
"""Debug information types and constants."""
2+
13
import llvmlite.ir as ir
24

35

46
class DwarfBehaviorEnum:
7+
"""DWARF module flag behavior constants for LLVM."""
58
ERROR_IF_MISMATCH = ir.Constant(ir.IntType(32), 1)
69
WARNING_IF_MISMATCH = ir.Constant(ir.IntType(32), 2)
710
OVERRIDE_USE_LARGEST = ir.Constant(ir.IntType(32), 7)

pythonbpf/decorators.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def section(name: str):
4141
A decorator function that marks the function with the section name
4242
"""
4343
def wrapper(fn):
44+
"""Decorator that sets the section name on the function."""
4445
fn._section = name
4546
return fn
4647

pythonbpf/functions/func_registry_handlers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Registry for statement handler functions."""
2+
13
from typing import Dict
24

35

@@ -11,6 +13,7 @@ def register(cls, stmt_type):
1113
"""Register a handler for a specific statement type."""
1214

1315
def decorator(handler):
16+
"""Decorator that registers the handler."""
1417
cls._handlers[stmt_type] = handler
1518
return handler
1619

pythonbpf/functions/functions_pass.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,20 @@
2525

2626
@dataclass
2727
class LocalSymbol:
28+
"""
29+
Represents a local variable in a BPF function.
30+
31+
Attributes:
32+
var: LLVM IR alloca instruction for the variable
33+
ir_type: LLVM IR type of the variable
34+
metadata: Optional metadata (e.g., struct type name)
35+
"""
2836
var: ir.AllocaInstr
2937
ir_type: ir.Type
3038
metadata: Any = None
3139

3240
def __iter__(self):
41+
"""Support tuple unpacking of LocalSymbol."""
3342
yield self.var
3443
yield self.ir_type
3544
yield self.metadata
@@ -692,6 +701,7 @@ def infer_return_type(func_node: ast.FunctionDef):
692701
found_type = None
693702

694703
def _expr_type(e):
704+
"""Helper function to extract type from an expression."""
695705
if e is None:
696706
return "None"
697707
if isinstance(e, ast.Constant):

pythonbpf/helper/bpf_helper_handler.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525

2626
class BPFHelperID(Enum):
27+
"""Enumeration of BPF helper function IDs."""
2728
BPF_MAP_LOOKUP_ELEM = 1
2829
BPF_MAP_UPDATE_ELEM = 2
2930
BPF_MAP_DELETE_ELEM = 3
@@ -260,6 +261,11 @@ def bpf_perf_event_output_handler(
260261
local_sym_tab=None,
261262
struct_sym_tab=None,
262263
):
264+
"""
265+
Emit LLVM IR for bpf_perf_event_output helper function call.
266+
267+
This allows sending data to userspace via a perf event array.
268+
"""
263269
if len(call.args) != 1:
264270
raise ValueError(
265271
f"Perf event output expects exactly one argument, got {len(call.args)}"
@@ -310,6 +316,7 @@ def handle_helper_call(
310316

311317
# Helper function to get map pointer and invoke handler
312318
def invoke_helper(method_name, map_ptr=None):
319+
"""Helper function to look up and invoke a registered handler."""
313320
handler = HelperHandlerRegistry.get_handler(method_name)
314321
if not handler:
315322
raise NotImplementedError(

pythonbpf/helper/helper_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def register(cls, helper_name):
2626
"""Decorator to register a handler function for a helper"""
2727

2828
def decorator(func):
29+
"""Decorator that registers the handler function."""
2930
cls._handlers[helper_name] = func
3031
return func
3132

pythonbpf/maps/maps_pass.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def is_map(func_node):
4343

4444

4545
class BPFMapType(Enum):
46+
"""Enumeration of BPF map types."""
4647
UNSPEC = 0
4748
HASH = 1
4849
ARRAY = 2

pythonbpf/maps/maps_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Registry for BPF map processor functions."""
2+
13
from collections.abc import Callable
24
from typing import Any
35

@@ -12,6 +14,7 @@ def register(cls, map_type_name):
1214
"""Decorator to register a processor function for a map type"""
1315

1416
def decorator(func):
17+
"""Decorator that registers the processor function."""
1518
cls._processors[map_type_name] = func
1619
return func
1720

0 commit comments

Comments
 (0)