Fixes #2518 -- weird behaviour of lookup_or_init (#2520)
* Allow lookup_or_init to return NULL (rather than just returning from the current function)
* Fixed a couple of bad edits found when running tests. Also fixed a bug in the
test runner. Also fixed a bug in libbcc where the python signature did not match
the actual implementation.
diff --git a/tools/biotop.py b/tools/biotop.py
index 6c959f6..3181ba9 100755
--- a/tools/biotop.py
+++ b/tools/biotop.py
@@ -155,10 +155,12 @@
valp = counts.lookup_or_init(&info, &zero);
}
- // save stats
- valp->us += delta_us;
- valp->bytes += req->__data_len;
- valp->io++;
+ if (valp) {
+ // save stats
+ valp->us += delta_us;
+ valp->bytes += req->__data_len;
+ valp->io++;
+ }
start.delete(&req);
whobyreq.delete(&req);
diff --git a/tools/filetop.py b/tools/filetop.py
index ccc5a10..238048e 100755
--- a/tools/filetop.py
+++ b/tools/filetop.py
@@ -119,12 +119,14 @@
struct val_t *valp, zero = {};
valp = counts.lookup_or_init(&info, &zero);
- if (is_read) {
- valp->reads++;
- valp->rbytes += count;
- } else {
- valp->writes++;
- valp->wbytes += count;
+ if (valp) {
+ if (is_read) {
+ valp->reads++;
+ valp->rbytes += count;
+ } else {
+ valp->writes++;
+ valp->wbytes += count;
+ }
}
return 0;
diff --git a/tools/lib/ucalls.py b/tools/lib/ucalls.py
index d072af0..1175195 100755
--- a/tools/lib/ucalls.py
+++ b/tools/lib/ucalls.py
@@ -164,7 +164,9 @@
(void *)method);
#ifndef LATENCY
valp = counts.lookup_or_init(&data.method, &val);
- ++(*valp);
+ if (valp) {
+ ++(*valp);
+ }
#endif
#ifdef LATENCY
entry.update(&data, ×tamp);
@@ -189,8 +191,10 @@
return 0; // missed the entry event
}
info = times.lookup_or_init(&data.method, &zero);
- info->num_calls += 1;
- info->total_ns += bpf_ktime_get_ns() - *entry_timestamp;
+ if (info) {
+ info->num_calls += 1;
+ info->total_ns += bpf_ktime_get_ns() - *entry_timestamp;
+ }
entry.delete(&data);
return 0;
}
@@ -210,7 +214,9 @@
#endif
#ifndef LATENCY
valp = syscounts.lookup_or_init(&id, &val);
- ++(*valp);
+ if (valp) {
+ ++(*valp);
+ }
#endif
return 0;
}
@@ -227,8 +233,10 @@
}
id = e->id;
info = systimes.lookup_or_init(&id, &zero);
- info->num_calls += 1;
- info->total_ns += bpf_ktime_get_ns() - e->timestamp;
+ if (info) {
+ info->num_calls += 1;
+ info->total_ns += bpf_ktime_get_ns() - e->timestamp;
+ }
sysentry.delete(&pid);
return 0;
}
diff --git a/tools/lib/uflow.py b/tools/lib/uflow.py
index 63fab87..f904533 100755
--- a/tools/lib/uflow.py
+++ b/tools/lib/uflow.py
@@ -89,6 +89,9 @@
data.pid = bpf_get_current_pid_tgid();
depth = entry.lookup_or_init(&data.pid, &zero);
+ if (!depth) {
+ depth = &zero;
+ }
data.depth = DEPTH;
UPDATE
diff --git a/tools/lib/uobjnew.py b/tools/lib/uobjnew.py
index 85f5768..63dd80a 100755
--- a/tools/lib/uobjnew.py
+++ b/tools/lib/uobjnew.py
@@ -80,8 +80,10 @@
struct val_t *valp, zero = {};
key.size = size;
valp = allocs.lookup_or_init(&key, &zero);
- valp->total_size += size;
- valp->num_allocs += 1;
+ if (valp) {
+ valp->total_size += size;
+ valp->num_allocs += 1;
+ }
return 0;
}
"""
@@ -98,8 +100,10 @@
bpf_usdt_readarg(4, ctx, &size);
bpf_probe_read(&key.name, sizeof(key.name), (void *)classptr);
valp = allocs.lookup_or_init(&key, &zero);
- valp->total_size += size;
- valp->num_allocs += 1;
+ if (valp) {
+ valp->total_size += size;
+ valp->num_allocs += 1;
+ }
return 0;
}
"""
@@ -115,8 +119,10 @@
u64 size = 0;
bpf_usdt_readarg(1, ctx, &size);
valp = allocs.lookup_or_init(&key, &zero);
- valp->total_size += size;
- valp->num_allocs += 1;
+ if (valp) {
+ valp->total_size += size;
+ valp->num_allocs += 1;
+ }
return 0;
}
"""
@@ -128,7 +134,9 @@
bpf_usdt_readarg(1, ctx, &classptr);
bpf_probe_read(&key.name, sizeof(key.name), (void *)classptr);
valp = allocs.lookup_or_init(&key, &zero);
- valp->num_allocs += 1; // We don't know the size, unfortunately
+ if (valp) {
+ valp->num_allocs += 1; // We don't know the size, unfortunately
+ }
return 0;
}
"""
@@ -146,7 +154,9 @@
struct key_t key = { .name = "<ALL>" };
struct val_t *valp, zero = {};
valp = allocs.lookup_or_init(&key, &zero);
- valp->num_allocs += 1;
+ if (valp) {
+ valp->num_allocs += 1;
+ }
return 0;
}
"""
diff --git a/tools/lib/ustat.py b/tools/lib/ustat.py
index 1edc985..bd5b98b 100755
--- a/tools/lib/ustat.py
+++ b/tools/lib/ustat.py
@@ -94,7 +94,9 @@
u64 *valp, zero = 0;
u32 tgid = bpf_get_current_pid_tgid() >> 32;
valp = %s_%s_counts.lookup_or_init(&tgid, &zero);
- ++(*valp);
+ if (valp) {
+ ++(*valp);
+ }
return 0;
}
"""
diff --git a/tools/old/offcputime.py b/tools/old/offcputime.py
index 38d12a2..c0042ff 100755
--- a/tools/old/offcputime.py
+++ b/tools/old/offcputime.py
@@ -141,7 +141,9 @@
out:
val = counts.lookup_or_init(&key, &zero);
- (*val) += delta;
+ if (val) {
+ (*val) += delta;
+ }
return 0;
}
"""
diff --git a/tools/old/offwaketime.py b/tools/old/offwaketime.py
index 3b5bb36..42fa5ce 100755
--- a/tools/old/offwaketime.py
+++ b/tools/old/offwaketime.py
@@ -189,7 +189,9 @@
}
val = counts.lookup_or_init(&key, &zero);
- (*val) += delta;
+ if (val) {
+ (*val) += delta;
+ }
return 0;
}
"""
diff --git a/tools/old/profile.py b/tools/old/profile.py
index e308208..e6940e6 100755
--- a/tools/old/profile.py
+++ b/tools/old/profile.py
@@ -185,7 +185,9 @@
}
val = counts.lookup_or_init(&key, &zero);
- (*val)++;
+ if (val) {
+ (*val)++;
+ }
return 0;
}
"""
diff --git a/tools/old/softirqs.py b/tools/old/softirqs.py
index 3b40b1a..5708ba6 100755
--- a/tools/old/softirqs.py
+++ b/tools/old/softirqs.py
@@ -147,7 +147,7 @@
bpf_text = bpf_text.replace('STORE',
' .ip = ip, .slot = 0 /* ignore */};' +
'u64 zero = 0, *vp = dist.lookup_or_init(&key, &zero);' +
- '(*vp) += delta;')
+ 'if (vp) { (*vp) += delta; }')
if debug:
print(bpf_text)
diff --git a/tools/old/stackcount.py b/tools/old/stackcount.py
index 108c800..b60cc4c 100755
--- a/tools/old/stackcount.py
+++ b/tools/old/stackcount.py
@@ -118,7 +118,9 @@
out:
val = counts.lookup_or_init(&key, &zero);
- (*val)++;
+ if (val) {
+ (*val)++;
+ }
return 0;
}
"""
diff --git a/tools/old/wakeuptime.py b/tools/old/wakeuptime.py
index 783c7ff..a4cd521 100644
--- a/tools/old/wakeuptime.py
+++ b/tools/old/wakeuptime.py
@@ -154,7 +154,9 @@
out:
val = counts.lookup_or_init(&key, &zero);
- (*val) += delta;
+ if (val) {
+ (*val) += delta;
+ }
return 0;
}
"""
diff --git a/tools/slabratetop.py b/tools/slabratetop.py
index 101c585..7b8a421 100755
--- a/tools/slabratetop.py
+++ b/tools/slabratetop.py
@@ -92,8 +92,10 @@
struct val_t *valp, zero = {};
valp = counts.lookup_or_init(&info, &zero);
- valp->count++;
- valp->size += cachep->size;
+ if (valp) {
+ valp->count++;
+ valp->size += cachep->size;
+ }
return 0;
}
diff --git a/tools/syscount.py b/tools/syscount.py
index 6cbea11..21e788d 100755
--- a/tools/syscount.py
+++ b/tools/syscount.py
@@ -133,12 +133,16 @@
return 0;
val = data.lookup_or_init(&key, &zero);
- val->count++;
- val->total_ns += bpf_ktime_get_ns() - *start_ns;
+ if (val) {
+ val->count++;
+ val->total_ns += bpf_ktime_get_ns() - *start_ns;
+ }
#else
u64 *val, zero = 0;
val = data.lookup_or_init(&key, &zero);
- ++(*val);
+ if (val) {
+ ++(*val);
+ }
#endif
return 0;
}