|
2 | 2 | import logging |
3 | 3 |
|
4 | 4 | from llvmlite import ir |
5 | | -from pythonbpf.expr import eval_expr, get_base_type_and_depth, deref_to_depth, convert |
| 5 | +from pythonbpf.expr import ( |
| 6 | + eval_expr, |
| 7 | + get_base_type_and_depth, |
| 8 | + deref_to_depth, |
| 9 | + convert, |
| 10 | + get_typed_operand, |
| 11 | + holds_map_int_value, |
| 12 | +) |
| 13 | +from pythonbpf.type_deducer import ctypes_to_ir |
6 | 14 | from pythonbpf.expr.vmlinux_registry import VmlinuxHandlerRegistry |
7 | 15 | from pythonbpf.helper.helper_utils import get_char_array_ptr_and_size |
8 | 16 |
|
@@ -107,7 +115,13 @@ def _process_name_in_fval( |
107 | 115 | ): |
108 | 116 | """Process name nodes in formatted values.""" |
109 | 117 | if local_sym_tab and name_node.id in local_sym_tab: |
110 | | - _, var_type, tmp = local_sym_tab[name_node.id] |
| 118 | + sym = local_sym_tab[name_node.id] |
| 119 | + # A map-lookup local prints as its value, of the map's value type; |
| 120 | + # its pointer type alone would make a c_uint8 value look like a string. |
| 121 | + if holds_map_int_value(sym): |
| 122 | + var_type = ctypes_to_ir(sym.metadata) |
| 123 | + else: |
| 124 | + var_type = sym.ir_type |
111 | 125 | _populate_fval(var_type, name_node, fmt_parts, exprs) |
112 | 126 | elif name_node.id in compilation_context.bpf_globals: |
113 | 127 | var_type = compilation_context.bpf_globals[name_node.id].ir_type |
@@ -159,7 +173,8 @@ def _populate_fval(ftype, node, fmt_parts, exprs): |
159 | 173 | if ftype.width == 64: |
160 | 174 | fmt_parts.append("%lld") |
161 | 175 | exprs.append(node) |
162 | | - elif ftype.width == 32: |
| 176 | + elif ftype.width <= 32: |
| 177 | + # Narrower integers are promoted to int, as C varargs do. |
163 | 178 | fmt_parts.append("%d") |
164 | 179 | exprs.append(node) |
165 | 180 | else: |
@@ -229,6 +244,18 @@ def _prepare_expr_args(expr, func, compilation_context, builder, local_sym_tab): |
229 | 244 | if char_array_ptr: |
230 | 245 | return char_array_ptr |
231 | 246 |
|
| 247 | + # A map-lookup local: its value, widened per the map's value type |
| 248 | + if ( |
| 249 | + isinstance(expr, ast.Name) |
| 250 | + and local_sym_tab |
| 251 | + and expr.id in local_sym_tab |
| 252 | + and holds_map_int_value(local_sym_tab[expr.id]) |
| 253 | + ): |
| 254 | + val, ty = get_typed_operand( |
| 255 | + func, compilation_context, expr, builder, local_sym_tab |
| 256 | + ) |
| 257 | + return _handle_int_arg(val, builder, ty) |
| 258 | + |
232 | 259 | # Regular expression evaluation |
233 | 260 | val, _ = eval_expr(func, compilation_context, builder, expr, local_sym_tab) |
234 | 261 |
|
|
0 commit comments