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/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;
}
"""