Write a Lua decoder#

How-to · upatch authors

A decoder turns a snapshot of one map into the record that shirudo exports: the body of an OTLP log record, or a list of OTLP metrics. It is a Lua function that the BPF source declares and the build signs into the upatch, so it has the same signer as the program whose data it reads. This page shows how to declare one, write it, check it and run it.

Before you begin#

Declare the decoder#

  1. Decide on the signal. A log decoder returns a table that becomes the body of one log record per snapshot. A metric decoder returns OTLP metrics with names, units and data points of its choosing.

  2. Include decoder.h after vmlinux.h and declare a manifest in .rodata. kernel-obs declares this one, through a macro, for watch_stat, a per-CPU array whose one value holds two __u64 counters, checked and matched:

    #include "decoder.h"
    
    const struct decoder_manifest watch_stat_decoder SEC(".rodata") = {
            .magic = DECODER_MANIFEST_MAGIC,
            .version = UPATCH_DECODER_VERSION,
            .language = UPATCH_DECODER_LANG_LUA,
            .api_version = 1,
            .input_format = UPATCH_DECODER_INPUT_MAP_V1,
            .export_signal = UPATCH_DECODER_EXPORT_METRIC,
            .upatch = "kernel-obs",
            .map = "watch_stat",
            .source = "decoders/watch_stat.lua",
            .entrypoint = "decode",
    };
    

    The builder finds manifests by their magic in the object’s .rodata, so declare the manifest const. It also passes over a manifest with an empty map, source or entrypoint, a string field without a terminating NUL, or a zero language, api_version, input_format or export_signal, without an error, so check the result as step 6 shows. Set the fields as follows:

    Field

    Value

    magic, version

    The constants shown. With any other value the builder does not recognize the manifest and builds the upatch without it.

    language, api_version, input_format

    The constants shown. Any other value fails the build with unsupported decoder ABI for '<upatch>/<map>'.

    export_signal

    UPATCH_DECODER_EXPORT_LOG or UPATCH_DECODER_EXPORT_METRIC.

    upatch

    Empty, or the upatch name, which the field holds up to 63 characters. In a package that is the source’s base name. Leave it out for a longer name: one of 64 characters or more leaves no room for the terminating NUL. The decoder belongs to the upatch whose signed data carries it either way.

    map

    The name of a map of the object, at most 15 characters. One decoder per map.

    source

    The Lua file, at most 63 characters, relative to the directory of the .bpf.c file, or to the current directory when the input is a .bpf.o. No leading /, no . or .. component, no backslash. Not empty, at most 16 MiB.

    entrypoint

    The function to call, at most 31 characters.

Write the function#

  1. Create the Lua file. The chunk either defines a global function with the entrypoint’s name, returns a function, or returns a table that has one under that name. shirudo calls it with one argument, the input table:

    Field

    Content

    schema

    1

    upatch, map

    the upatch and map names

    startTimeUnixNano

    when the upatch was loaded, as a decimal string

    timeUnixNano

    the time of the snapshot, as a decimal string

    data.name, data.type

    the map’s name and type, such as percpu_array

    data.max_entries, data.n_entries

    the map’s capacity and the entries read

    data.key_size, data.value_size, data.n_cpus

    sizes in bytes, and the number of CPUs for a per-CPU map

    data.truncated

    whether the read stopped at the entry limit

    data.entries[i].key_hex, .value_hex

    a directly read entry in hex. A per-CPU value holds one 8-byte aligned slot per CPU.

    data.serialized, data.serialized_pages, data.serialized_bytes

    whether a serializer produced the snapshot, and its size

    data.pages[i]

    one serializer page: page_index, cursor, next_cursor, done, flags, chunks, bytes and data_hex

    Lua numbers cannot hold a 64-bit value exactly, so shirudo passes timestamps as decimal strings, and these host functions exchange 64-bit values as decimal strings too:

    Function

    Returns

    shirudo.page_slots(page, size)

    the page’s payload cut into slots of size bytes, in order, respecting chunk boundaries

    shirudo.fixed_records(pages, size)

    the slots of all pages, indexed by each page’s cursor plus the slot’s position

    slot:u8(off), slot:u16(off), slot:u32(off)

    a little-endian integer at off, 0 when out of range

    slot:u64(off)

    a little-endian 64-bit value as a decimal string

    slot:cstring(off, len)

    the bytes from off up to the first NUL, at most len

    shirudo.u64_add(a, b), shirudo.u64_sub(a, b)

    exact sum and difference of two decimal strings. The sum stops at the largest value and the difference at 0.

    shirudo.hex_u64(hex, off)

    the little-endian 64-bit value at byte off of a hex string, as a decimal string

    The watch_stat decoder adds up both counters over all CPUs and returns one monotonic sum with a data point for each:

    -- SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
    -- Copyright (c) 2026 Authors of shirudo
    
    local function metric_point(input, value, outcome)
    	return {
    		startTimeUnixNano = input.startTimeUnixNano,
    		timeUnixNano = input.timeUnixNano,
    		asInt = value,
    		attributes = {
    			{key = "outcome", value = {stringValue = outcome}},
    		},
    	}
    end
    
    function decode(input)
    	local data = input.data
    	local entry = data.entries and data.entries[1]
    	if not entry then
    		error("missing watch counter entry")
    	end
    	local stride = math.ceil((data.value_size or 16) / 8) * 8
    	local cpus = data.n_cpus or 1
    	local checked, matched = "0", "0"
    	for cpu = 0, cpus - 1 do
    		local base = cpu * stride
    		checked = shirudo.u64_add(checked,
    			shirudo.hex_u64(entry.value_hex, base))
    		matched = shirudo.u64_add(matched,
    			shirudo.hex_u64(entry.value_hex, base + 8))
    	end
    	return {
    		{name = "kernel_obs.watch_attempts", unit = "1",
    			description = "socket-level destination checks against the watch list",
    			sum = {
    				aggregationTemporality = 2,
    				isMonotonic = true,
    				dataPoints = {
    					metric_point(input, checked, "checked"),
    					metric_point(input, matched, "matched"),
    				},
    			}},
    	}
    end
    
  2. Return the record. A log decoder returns a table. With --otel it becomes the log record’s body: a table whose keys are 1 to n becomes an array, any other table a key-value list, a whole number up to 2^53 an intValue and any other number a doubleValue. A metric decoder returns an array of OTLP Metric objects, exactly as they appear in the metrics array of a scopeMetrics entry. A string or a key that is not valid UTF-8 is exported with U+FFFD in place of each bad byte and of each NUL.

    To turn a timestamp a serializer took on the boot-time clock into wall clock time, subtract its age from timeUnixNano. The kernel-obs decoders do it with the time the serializer ran, which their page header carries:

    shirudo.u64_sub(input.timeUnixNano, shirudo.u64_sub(boot_now, ts))
    

The decoder runs in a restricted LuaJIT state:

  • It is loaded as source text. LuaJIT does not verify bytecode, so a precompiled chunk is refused.

  • The JIT compiler is off, so that the instruction count sees every instruction. One snapshot may take 1,000,000 virtual machine instructions, loading the chunk included, and 32 MiB of memory, and the result may nest 32 levels deep. pcall catches the error the limit raises, but from then on every instruction raises it again.

  • io, os, package, require, debug, jit, ffi, dofile, loadfile, load, loadstring, module, collectgarbage, newproxy, xpcall and print are removed, and so are the pattern functions string.find, string.match, string.gmatch and string.gsub, whose backtracking on a recorded string the instruction count never sees. The rest of the base library and the string, table, math and bit libraries remain.

  • shirudo reads the decoder from the archived copy of the upatch, /var/lib/shirudo/upatches/<name>.upatch, opens it as sealed state and runs it only when its bytes are the payload loaded, as the digest on the pin directory records. A decoder that did not arrive inside the signed artifact of the loaded upatch never runs.

An error raised in the decoder drops that snapshot with a message on standard error, its control characters replaced. It never produces a partial record.

Build and check it#

  1. Build the upatch as usual. The builder reads each manifest, resolves source and embeds the file in the signed DCDB section. It refuses the build when a manifest names another upatch, a map the object does not have, a second decoder for the same map, or a source it cannot read.

  2. Confirm that the decoder is in the artifact:

    operator$ shirudo upatch info <file>.upatch
    

    <file> is your upatch. The Decoders section lists each decoder with its target as <upatch>/<map>, <upatch> empty where the manifest leaves it out, the language and API version, the input format shirudo.map.v1, the export signal, and the source file with its entry point and size.

Run it on a target#

  1. Load the upatch on the target, then run the decoder against the live map without draining it:

    target# shirudo-runtime upatch map <upatch> <map> --json --peek
    

    shirudo-runtime is the command in the test VM, as Test a upatch on a target explains. <upatch> is the upatch name and <map> the map’s. The command prints what the decoder returned as one JSON document. A failure prints one of upatch map: decoder load failed: <message>, upatch map: decoder entrypoint '<name>' not found, upatch map: decoder failed: <message> or upatch map: decoder encode failed: <message>, and the instruction limit shows as decoder instruction limit exceeded.

  2. Send one snapshot to a collector:

    target# shirudo-runtime upatch map <upatch> <map> --peek --otel \
        --otel-endpoint=<url>
    

    <url> is the collector’s OTLP/HTTP base URL. A log decoder’s record goes to <url>/v1/logs, a metric decoder’s metrics to <url>/v1/metrics.

A map without a decoder is exported over OTLP as a log record whose body lists its entries or serializer pages in hex, and --json reports No data available for it. An unsigned per-CPU integer map without a decoder is classified as a metric and then fails to export over OTLP with the same message, so give such a map a metric decoder.

Next steps#