Commit 39354092 authored by Brenden Blanco's avatar Brenden Blanco

Support array and pointer types in scanf generated function

The rewriter-created sscanf and snprintf routines did not support map
structs with arrays or pointer types in them. Add such support.
Signed-off-by: default avatarBrenden Blanco <bblanco@plumgrid.com>
parent a4645289
...@@ -148,6 +148,19 @@ static void parse_type(IRBuilder<> &B, vector<Value *> *args, string *fmt, ...@@ -148,6 +148,19 @@ static void parse_type(IRBuilder<> &B, vector<Value *> *args, string *fmt,
*fmt += " "; *fmt += " ";
} }
*fmt += "}"; *fmt += "}";
} else if (ArrayType *at = dyn_cast<ArrayType>(type)) {
*fmt += "[ ";
for (size_t i = 0; i < at->getNumElements(); ++i) {
parse_type(B, args, fmt, at->getElementType(), B.CreateStructGEP(type, out, i), is_writer);
*fmt += " ";
}
*fmt += "]";
} else if (PointerType *pt = dyn_cast<PointerType>(type)) {
*fmt += "0xl";
if (is_writer)
*fmt += "x";
else
*fmt += "i";
} else if (IntegerType *it = dyn_cast<IntegerType>(type)) { } else if (IntegerType *it = dyn_cast<IntegerType>(type)) {
if (is_writer) if (is_writer)
*fmt += "0x"; *fmt += "0x";
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
# Licensed under the Apache License, Version 2.0 (the "License") # Licensed under the Apache License, Version 2.0 (the "License")
from bcc import BPF from bcc import BPF
import ctypes
from unittest import main, TestCase from unittest import main, TestCase
class TestClang(TestCase): class TestClang(TestCase):
...@@ -89,6 +90,22 @@ int foo(void *ctx) { ...@@ -89,6 +90,22 @@ int foo(void *ctx) {
self.assertEqual(l.s.a, 5) self.assertEqual(l.s.a, 5)
self.assertEqual(l.s.b, 6) self.assertEqual(l.s.b, 6)
def test_sscanf_array(self):
text = """
BPF_TABLE("hash", int, struct { u32 a[3]; u32 b; }, stats, 10);
"""
b = BPF(text=text, debug=0)
t = b.get_table("stats")
s1 = t.key_sprintf(t.Key(2))
self.assertEqual(s1, b"0x2")
s2 = t.leaf_sprintf(t.Leaf((ctypes.c_uint * 3)(1,2,3), 4))
self.assertEqual(s2, b"{ [ 0x1 0x2 0x3 ] 0x4 }")
l = t.leaf_scanf(s2)
self.assertEqual(l.a[0], 1)
self.assertEqual(l.a[1], 2)
self.assertEqual(l.a[2], 3)
self.assertEqual(l.b, 4)
def test_iosnoop(self): def test_iosnoop(self):
text = """ text = """
#include <linux/blkdev.h> #include <linux/blkdev.h>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment