Skip to content

Instantly share code, notes, and snippets.

@drakenclimber
Last active April 4, 2019 19:46
Show Gist options
  • Save drakenclimber/f2dd7aa98b174443ebca13a16bc09d44 to your computer and use it in GitHub Desktop.
Save drakenclimber/f2dd7aa98b174443ebca13a16bc09d44 to your computer and use it in GitHub Desktop.
print libseccomp hash table
#include <stdarg.h>
#include <stdio.h>
static void print_line(int indent_level, const char *format, ...)
{
int i;
for (i = 0; i < indent_level; i++)
fprintf(stdout, " ");
va_list(args);
va_start(args, format);
vfprintf(stdout, format, args);
va_end(args);
}
#define INDENT 2
static void print_blk(int indent_level, struct bpf_blk *b_cur)
{
int blk_cnt = 0;
print_line(indent_level, "----bpf_blk---- %p\n", b_cur);
print_line(indent_level + INDENT,
"blk_cnt = %d blk_alloc = %d hash = 0x%lx\n",
b_cur->blk_cnt, b_cur->blk_alloc,
b_cur->hash);
for (blk_cnt = 0; blk_cnt < b_cur->blk_cnt; blk_cnt++) {
print_line(indent_level + INDENT,
"----instr %d----\n", blk_cnt);
print_line(indent_level + INDENT * 2, "op = 0x%x\n",
b_cur->blks[blk_cnt].op);
print_line(indent_level + INDENT * 2,
"jt type = %d hash = 0x%lx\n",
b_cur->blks[blk_cnt].jt.type,
b_cur->blks[blk_cnt].jt.tgt.hash);
print_line(indent_level + INDENT * 2,
"jf type = %d hash = 0x%lx\n",
b_cur->blks[blk_cnt].jf.type,
b_cur->blks[blk_cnt].jf.tgt.hash);
print_line(indent_level + INDENT * 2,
"k type = %d hash = 0x%lx\n",
b_cur->blks[blk_cnt].k.type,
b_cur->blks[blk_cnt].k.tgt.hash);
}
print_line(indent_level + INDENT * 2, "lvl_prv = %p lvl_nxt = %p\n",
b_cur->lvl_prv, b_cur->lvl_nxt);
print_line(indent_level + INDENT * 2, "prev = %p next = %p\n",
b_cur->prev, b_cur->next);
}
static void print_hash_bkt(int indent_level, struct bpf_hash_bkt const *hbkt)
{
if (hbkt == NULL)
return;
print_line(indent_level, "----hash bucket---- %p\n", hbkt);
print_line(indent_level + INDENT,
"blk = %p next = %p found = %d\n",
hbkt->blk, hbkt->next, hbkt->found);
if (hbkt->blk != NULL)
print_blk(indent_level + INDENT, hbkt->blk);
}
void print_htbl(struct bpf_state *state)
{
struct bpf_hash_bkt *h_iter;
unsigned int bkt;
print_line(0, "----hash table----\n");
for (bkt = 0; bkt < _BPF_HASH_SIZE; bkt++) {
if (state->htbl[bkt]) {
h_iter = state->htbl[bkt & _BPF_HASH_MASK];
print_hash_bkt(INDENT, h_iter);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment