Skip to content

errors when storing comm in map key #234

@brendangregg

Description

@brendangregg

Test program, testkey:

#!/usr/bin/python

from __future__ import print_function
from bcc import BPF
from time import sleep

# load BPF program
b = BPF(text = """
#include <uapi/linux/ptrace.h>
#include <linux/sched.h>

struct key_t {
    u32 pid;
    char name[TASK_COMM_LEN];
};
BPF_HASH(counts, struct key_t);

int trace_count(struct pt_regs *ctx) {
    u32 pid = bpf_get_current_pid_tgid();
    struct key_t key = {};
    u64 zero = 0, *val;
    key.pid = pid;
    if (bpf_get_current_comm(&key.name, sizeof(key.name)) == 0) {
        val = counts.lookup_or_init(&key, &zero);
        (*val)++;
    }
    return 0;
}
""")
b.attach_kprobe(event_re='^[sS]y[sS]_read', fn_name="trace_count")
sleep(1)
counts = b.get_table("counts")
for k, v in sorted(counts.items(), key=lambda counts: counts[1].value):
    print("%-6d %-16s %8d" % (k.pid, k.name, v.value))

This gets:

bpf: Permission denied
0: (85) call 14
1: (b7) r1 = 0
2: (63) *(u32 *)(r10 -8) = r1
3: (7b) *(u64 *)(r10 -16) = r1
4: (7b) *(u64 *)(r10 -24) = r1
5: (7b) *(u64 *)(r10 -32) = r1
6: (63) *(u32 *)(r10 -24) = r0
7: (bf) r1 = r10
8: (07) r1 += -24
9: (47) r1 |= 4
10: (b7) r2 = 16
11: (85) call 16
R1 type=inv expected=fp
[...]

But if I swap the pid and name members in the key around (ie, name is first):

# ./testkey 
1936028263 112                     1
1735355497 109                     1
1735355497 109                     1
1936291442 115                     3
7955819 116                     3
1936291442 115                     3
1936261234 109                     6
1768383593 115                     7
1936261230 99                      7
1768383593 115                     7
1936291442 114                    12
1936291442 114                    14
1668248176 114                   128
Segmentation fault

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions