Blob Blame History Raw
From 3f7b59660037c0d5dea785d115df25d9b95f07dc Mon Sep 17 00:00:00 2001
From: Xiaozhou Liu <liuxiaozhou@bytedance.com>
Date: Mon, 21 Jan 2019 11:23:42 +0800
Subject: [PATCH] print_log2_hist(): check and skip possible paddings (#2155)

Address issue 2154.

When a struct S is used as key to a BPF_HISTOGRAM, it is assumed that the second
member of S holds the slot. But when S is converted to python from bpf C,
a padding may be inserted as a second member. This breaks print_log2_hist().

    root@debian:~/bcc/tools# ./softirqs.py -d
    Tracing soft irq event time... Hit Ctrl-C to end.
    ^C
    Traceback (most recent call last):
      File "./softirqs.py", line 144, in <module>
        dist.print_log2_hist(label, "softirq", section_print_fn=vec_to_name)
      File "/usr/local/lib/python2.7/dist-packages/bcc/table.py", line 326, in print_log2_hist
        vals[slot] = v.value
    TypeError: list indices must be integers, not str

Fix it by skipping the possible padding. Future work would be fixing/working
around in the library where the padding is introduced.
---
 src/python/bcc/table.py | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/python/bcc/table.py b/src/python/bcc/table.py
index 6f598353..f6449de7 100644
--- a/src/python/bcc/table.py
+++ b/src/python/bcc/table.py
@@ -317,6 +317,15 @@ linear_index_max = 1025
             tmp = {}
             f1 = self.Key._fields_[0][0]
             f2 = self.Key._fields_[1][0]
+
+            # The above code assumes that self.Key._fields_[1][0] holds the
+            # slot. But a padding member may have been inserted here, which
+            # breaks the assumption and leads to chaos.
+            # TODO: this is a quick fix. Fixing/working around in the BCC
+            # internal library is the right thing to do.
+            if f2 == '__pad_1' and len(self.Key._fields_) == 3:
+                f2 = self.Key._fields_[2][0]
+
             for k, v in self.items():
                 bucket = getattr(k, f1)
                 if bucket_fn:
-- 
2.20.1