diff --git a/docs/reference_guide.md b/docs/reference_guide.md index 64a07acf1235..ef088b2c6d5a 100644 --- a/docs/reference_guide.md +++ b/docs/reference_guide.md @@ -767,7 +767,7 @@ Examples in situ: Syntax: ```*val map.lookup_or_init(&key, &zero)``` -Lookup the key in the map, and return a pointer to its value if it exists, else initialize the key's value to the second argument. This is often used to initialize values to zero. +Lookup the key in the map, and return a pointer to its value if it exists, else initialize the key's value to the second argument. This is often used to initialize values to zero. If the key cannot be inserted (e.g. the map is full) then NULL is returned. Examples in situ: [search /examples](https://github.com/iovisor/bcc/search?q=lookup_or_init+path%3Aexamples&type=Code), diff --git a/docs/tutorial_bcc_python_developer.md b/docs/tutorial_bcc_python_developer.md index 82e1bbe83d1d..60a0dfb0bfbf 100644 --- a/docs/tutorial_bcc_python_developer.md +++ b/docs/tutorial_bcc_python_developer.md @@ -559,7 +559,9 @@ int count(struct pt_regs *ctx) { bpf_probe_read(&key.c, sizeof(key.c), (void *)PT_REGS_PARM1(ctx)); // could also use `counts.increment(key)` val = counts.lookup_or_init(&key, &zero); - (*val)++; + if (val) { + (*val)++; + } return 0; }; """) @@ -678,7 +680,9 @@ int count_sched(struct pt_regs *ctx, struct task_struct *prev) { // could also use `stats.increment(key);` val = stats.lookup_or_init(&key, &zero); - (*val)++; + if (val) { + (*val)++; + } return 0; } ``` diff --git a/examples/cpp/LLCStat.cc b/examples/cpp/LLCStat.cc index 2e9d628bdbfb..b1a36aedcb39 100644 --- a/examples/cpp/LLCStat.cc +++ b/examples/cpp/LLCStat.cc @@ -43,7 +43,9 @@ int on_cache_miss(struct bpf_perf_event_data *ctx) { u64 zero = 0, *val; val = miss_count.lookup_or_init(&key, &zero); - (*val) += ctx->sample_period; + if (val) { + (*val) += ctx->sample_period; + } return 0; } @@ -54,7 +56,9 @@ int on_cache_ref(struct bpf_perf_event_data *ctx) { u64 zero = 0, *val; val = ref_count.lookup_or_init(&key, &zero); - (*val) += ctx->sample_period; + if (val) { + (*val) += ctx->sample_period; + } return 0; } diff --git a/examples/cpp/TCPSendStack.cc b/examples/cpp/TCPSendStack.cc index 183529ef0452..7c41670896d2 100644 --- a/examples/cpp/TCPSendStack.cc +++ b/examples/cpp/TCPSendStack.cc @@ -39,7 +39,9 @@ int on_tcp_send(struct pt_regs *ctx) { u64 zero = 0, *val; val = counts.lookup_or_init(&key, &zero); - (*val)++; + if (val) { + (*val)++; + } return 0; } diff --git a/examples/cpp/UseExternalMap.cc b/examples/cpp/UseExternalMap.cc index d0cf445c65cc..724a12f98b4d 100644 --- a/examples/cpp/UseExternalMap.cc +++ b/examples/cpp/UseExternalMap.cc @@ -56,7 +56,9 @@ int on_sched_switch(struct tracepoint__sched__sched_switch *args) { __builtin_memcpy(&key.prev_comm, args->prev_comm, 16); __builtin_memcpy(&key.next_comm, args->next_comm, 16); val = counts.lookup_or_init(&key, &zero); - (*val)++; + if (val) { + (*val)++; + } return 0; } diff --git a/examples/lua/offcputime.lua b/examples/lua/offcputime.lua index 09c704f6d065..73bd435095bf 100755 --- a/examples/lua/offcputime.lua +++ b/examples/lua/offcputime.lua @@ -65,7 +65,9 @@ int oncpu(struct pt_regs *ctx, struct task_struct *prev) { key.stack_id = stack_traces.get_stackid(ctx, stack_flags); val = counts.lookup_or_init(&key, &zero); - (*val) += delta; + if (val) { + (*val) += delta; + } return 0; } ]] diff --git a/examples/lua/task_switch.lua b/examples/lua/task_switch.lua index 1c0aaa8b7755..92c1f276e696 100755 --- a/examples/lua/task_switch.lua +++ b/examples/lua/task_switch.lua @@ -33,7 +33,9 @@ int count_sched(struct pt_regs *ctx, struct task_struct *prev) { key.prev_pid = prev->pid; val = stats.lookup_or_init(&key, &zero); - (*val)++; + if (val) { + (*val)++; + } return 0; } ]] diff --git a/examples/networking/distributed_bridge/tunnel.c b/examples/networking/distributed_bridge/tunnel.c index 0bd99829ac1d..1d5681fb0fa2 100644 --- a/examples/networking/distributed_bridge/tunnel.c +++ b/examples/networking/distributed_bridge/tunnel.c @@ -38,7 +38,9 @@ int handle_ingress(struct __sk_buff *skb) { struct vni_key vk = {ethernet->src, *ifindex, 0}; struct host *src_host = mac2host.lookup_or_init(&vk, &(struct host){tkey.tunnel_id, tkey.remote_ipv4, 0, 0}); - lock_xadd(&src_host->rx_pkts, 1); + if (src_host) { + lock_xadd(&src_host->rx_pkts, 1); + } bpf_clone_redirect(skb, *ifindex, 1/*ingress*/); } else { bpf_trace_printk("ingress invalid tunnel_id=%d\n", tkey.tunnel_id); diff --git a/examples/networking/tunnel_monitor/monitor.c b/examples/networking/tunnel_monitor/monitor.c index 630e4a682153..787be1f771ec 100644 --- a/examples/networking/tunnel_monitor/monitor.c +++ b/examples/networking/tunnel_monitor/monitor.c @@ -126,12 +126,14 @@ ip: ; swap_ipkey(&key); struct counters zleaf = {0}; struct counters *leaf = stats.lookup_or_init(&key, &zleaf); - if (is_ingress) { - lock_xadd(&leaf->rx_pkts, 1); - lock_xadd(&leaf->rx_bytes, skb->len); - } else { - lock_xadd(&leaf->tx_pkts, 1); - lock_xadd(&leaf->tx_bytes, skb->len); + if (leaf) { + if (is_ingress) { + lock_xadd(&leaf->rx_pkts, 1); + lock_xadd(&leaf->rx_bytes, skb->len); + } else { + lock_xadd(&leaf->tx_pkts, 1); + lock_xadd(&leaf->tx_bytes, skb->len); + } } return 1; } diff --git a/examples/networking/vlan_learning/vlan_learning.c b/examples/networking/vlan_learning/vlan_learning.c index 3774d74ba7bc..ad08c1184900 100644 --- a/examples/networking/vlan_learning/vlan_learning.c +++ b/examples/networking/vlan_learning/vlan_learning.c @@ -33,10 +33,12 @@ int handle_phys2virt(struct __sk_buff *skb) { int out_ifindex = leaf->out_ifindex; struct ifindex_leaf_t zleaf = {0}; struct ifindex_leaf_t *out_leaf = egress.lookup_or_init(&out_ifindex, &zleaf); - // to capture potential configuration changes - out_leaf->out_ifindex = skb->ifindex; - out_leaf->vlan_tci = skb->vlan_tci; - out_leaf->vlan_proto = skb->vlan_proto; + if (out_leaf) { + // to capture potential configuration changes + out_leaf->out_ifindex = skb->ifindex; + out_leaf->vlan_tci = skb->vlan_tci; + out_leaf->vlan_proto = skb->vlan_proto; + } // pop the vlan header and send to the destination bpf_skb_vlan_pop(skb); bpf_clone_redirect(skb, leaf->out_ifindex, 0); diff --git a/examples/tracing/mallocstacks.py b/examples/tracing/mallocstacks.py index 1d887e86dafd..051ee3f926a2 100755 --- a/examples/tracing/mallocstacks.py +++ b/examples/tracing/mallocstacks.py @@ -47,7 +47,9 @@ // could also use `calls.increment(key, size);` u64 zero = 0, *val; val = calls.lookup_or_init(&key, &zero); - (*val) += size; + if (val) { + (*val) += size; + } return 0; }; """) diff --git a/examples/tracing/strlen_count.py b/examples/tracing/strlen_count.py index eab67109f719..c122d647a5d2 100755 --- a/examples/tracing/strlen_count.py +++ b/examples/tracing/strlen_count.py @@ -34,7 +34,9 @@ bpf_probe_read(&key.c, sizeof(key.c), (void *)PT_REGS_PARM1(ctx)); // could also use `counts.increment(key)` val = counts.lookup_or_init(&key, &zero); - (*val)++; + if (val) { + (*val)++; + } return 0; }; """) diff --git a/examples/tracing/task_switch.c b/examples/tracing/task_switch.c index 7192ad02ceda..83f4ff1823f6 100644 --- a/examples/tracing/task_switch.c +++ b/examples/tracing/task_switch.c @@ -16,6 +16,8 @@ int count_sched(struct pt_regs *ctx, struct task_struct *prev) { // could also use `stats.increment(key);` val = stats.lookup_or_init(&key, &zero); - (*val)++; + if (val) { + (*val)++; + } return 0; } diff --git a/src/cc/frontends/clang/b_frontend_action.cc b/src/cc/frontends/clang/b_frontend_action.cc index 5671869002f4..cb1f5b1cd7d8 100644 --- a/src/cc/frontends/clang/b_frontend_action.cc +++ b/src/cc/frontends/clang/b_frontend_action.cc @@ -782,7 +782,6 @@ bool BTypeVisitor::VisitCallExpr(CallExpr *Call) { txt += "if (!leaf) {"; txt += " " + update + ", " + arg0 + ", " + arg1 + ", BPF_NOEXIST);"; txt += " leaf = " + lookup + ", " + arg0 + ");"; - txt += " if (!leaf) return 0;"; txt += "}"; txt += "leaf;})"; } else if (memb_name == "increment") { diff --git a/src/python/bcc/libbcc.py b/src/python/bcc/libbcc.py index 792558f400e0..4c992dda9481 100644 --- a/src/python/bcc/libbcc.py +++ b/src/python/bcc/libbcc.py @@ -89,7 +89,7 @@ lib.bpf_attach_socket.argtypes = [ct.c_int, ct.c_int] lib.bcc_func_load.restype = ct.c_int lib.bcc_func_load.argtypes = [ct.c_void_p, ct.c_int, ct.c_char_p, ct.c_void_p, - ct.c_size_t, ct.c_char_p, ct.c_uint, ct.c_int, ct.c_char_p, ct.c_uint] + ct.c_size_t, ct.c_char_p, ct.c_uint, ct.c_int, ct.c_char_p, ct.c_uint, ct.c_char_p] _RAW_CB_TYPE = ct.CFUNCTYPE(None, ct.py_object, ct.c_void_p, ct.c_int) _LOST_CB_TYPE = ct.CFUNCTYPE(None, ct.py_object, ct.c_ulonglong) lib.bpf_attach_kprobe.restype = ct.c_int diff --git a/tests/cc/test_bpf_table.cc b/tests/cc/test_bpf_table.cc index a45c7bc6bc53..39ebf89dd946 100644 --- a/tests/cc/test_bpf_table.cc +++ b/tests/cc/test_bpf_table.cc @@ -182,7 +182,9 @@ TEST_CASE("test bpf stack table", "[bpf_stack_table]") { int stack_id = stack_traces.get_stackid(ctx, BPF_F_REUSE_STACKID); int zero = 0, *val; val = id.lookup_or_init(&zero, &stack_id); - (*val) = stack_id; + if (val) { + (*val) = stack_id; + } return 0; } @@ -233,7 +235,9 @@ TEST_CASE("test bpf stack_id table", "[bpf_stack_table]") { int stack_id = stack_traces.get_stackid(ctx, BPF_F_USER_STACK); int zero = 0, *val; val = id.lookup_or_init(&zero, &stack_id); - (*val) = stack_id; + if (val) { + (*val) = stack_id; + } return 0; } diff --git a/tests/lua/test_clang.lua b/tests/lua/test_clang.lua index f3d395ec52c0..8843b74b22a0 100644 --- a/tests/lua/test_clang.lua +++ b/tests/lua/test_clang.lua @@ -244,7 +244,9 @@ int kprobe__finish_task_switch(struct pt_regs *ctx, struct task_struct *prev) { key.prev_pid = prev->pid; val = stats.lookup_or_init(&key, &zero); - (*val)++; + if (val) { + (*val)++; + } return 0; } ]]} diff --git a/tests/python/test_clang.py b/tests/python/test_clang.py index 36f0a1b09a62..69b35f6a6ad4 100755 --- a/tests/python/test_clang.py +++ b/tests/python/test_clang.py @@ -397,7 +397,9 @@ def test_task_switch(self): key.prev_pid = prev->pid; val = stats.lookup_or_init(&key, &zero); - (*val)++; + if (val) { + (*val)++; + } return 0; } """) diff --git a/tests/python/test_license.py b/tests/python/test_license.py index f0c6b1dca2a9..61faf52f784b 100755 --- a/tests/python/test_license.py +++ b/tests/python/test_license.py @@ -43,7 +43,9 @@ class TestLicense(unittest.TestCase): bpf_get_current_comm(&(key.comm), 16); val = counts.lookup_or_init(&key, &zero); // update counter - (*val)++; + if (val) { + (*val)++; + } return 0; } """ diff --git a/tests/python/test_lru.py b/tests/python/test_lru.py index 1139040d377b..34ba9b5ab9d8 100644 --- a/tests/python/test_lru.py +++ b/tests/python/test_lru.py @@ -27,7 +27,9 @@ def test_lru_percpu_hash(self): u32 key=0; u32 value = 0, *val; val = stats.lookup_or_init(&key, &value); - *val += 1; + if (val) { + *val += 1; + } return 0; } """ diff --git a/tests/python/test_percpu.py b/tests/python/test_percpu.py index 39a3bbc4373d..86497d0fb67c 100755 --- a/tests/python/test_percpu.py +++ b/tests/python/test_percpu.py @@ -30,7 +30,9 @@ def test_u64(self): u32 key=0; u64 value = 0, *val; val = stats.lookup_or_init(&key, &value); - *val += 1; + if (val) { + *val += 1; + } return 0; } """ @@ -60,7 +62,9 @@ def test_u32(self): u32 key=0; u32 value = 0, *val; val = stats.lookup_or_init(&key, &value); - *val += 1; + if (val) { + *val += 1; + } return 0; } """ @@ -94,8 +98,10 @@ def test_struct_custom_func(self): u32 key=0; counter value = {0,0}, *val; val = stats.lookup_or_init(&key, &value); - val->c1 += 1; - val->c2 += 1; + if (val) { + val->c1 += 1; + val->c2 += 1; + } return 0; } """ diff --git a/tests/python/test_stat1.c b/tests/python/test_stat1.c index f7ecb93c18d5..f3fa1cefa00c 100644 --- a/tests/python/test_stat1.c +++ b/tests/python/test_stat1.c @@ -48,8 +48,10 @@ int on_packet(struct __sk_buff *skb) { } struct IPLeaf zleaf = {0}; struct IPLeaf *leaf = stats.lookup_or_init(&key, &zleaf); - lock_xadd(&leaf->rx_pkts, rx); - lock_xadd(&leaf->tx_pkts, tx); + if (leaf) { + lock_xadd(&leaf->rx_pkts, rx); + lock_xadd(&leaf->tx_pkts, tx); + } } EOP: diff --git a/tests/python/test_trace2.c b/tests/python/test_trace2.c index 4c18a8607a6e..76412a5a1d59 100644 --- a/tests/python/test_trace2.c +++ b/tests/python/test_trace2.c @@ -8,6 +8,9 @@ BPF_HASH(stats, struct Ptr, struct Counters, 1024); int count_sched(struct pt_regs *ctx) { struct Ptr key = {.ptr = PT_REGS_PARM1(ctx)}; struct Counters zleaf = {0}; - stats.lookup_or_init(&key, &zleaf)->stat1++; + struct Counters *val = stats.lookup_or_init(&key, &zleaf); + if (val) { + val->stat1++; + } return 0; } diff --git a/tests/python/test_trace2.py b/tests/python/test_trace2.py index 5e9805ae58bb..06cce8b7dfc6 100755 --- a/tests/python/test_trace2.py +++ b/tests/python/test_trace2.py @@ -19,7 +19,10 @@ struct Counters zleaf; memset(&zleaf, 0, sizeof(zleaf)); - stats.lookup_or_init(&key, &zleaf)->stat1++; + struct Counters *val = stats.lookup_or_init(&key, &zleaf); + if (val) { + val->stat1++; + } return 0; } """ diff --git a/tests/python/test_trace3.c b/tests/python/test_trace3.c index 10d91d08992d..235ae3ace2bb 100644 --- a/tests/python/test_trace3.c +++ b/tests/python/test_trace3.c @@ -48,6 +48,8 @@ int probe_blk_update_request(struct pt_regs *ctx) { u64 zero = 0; u64 *val = latency.lookup_or_init(&index, &zero); - lock_xadd(val, 1); + if (val) { + lock_xadd(val, 1); + } return 0; } diff --git a/tests/python/test_trace4.py b/tests/python/test_trace4.py index 6836047587f8..63c543b83f09 100755 --- a/tests/python/test_trace4.py +++ b/tests/python/test_trace4.py @@ -14,11 +14,17 @@ def setUp(self): typedef struct { u64 val; } Val; BPF_HASH(stats, Key, Val, 3); int hello(void *ctx) { - stats.lookup_or_init(&(Key){1}, &(Val){0})->val++; + Val *val = stats.lookup_or_init(&(Key){1}, &(Val){0}); + if (val) { + val->val++; + } return 0; } int goodbye(void *ctx) { - stats.lookup_or_init(&(Key){2}, &(Val){0})->val++; + Val *val = stats.lookup_or_init(&(Key){2}, &(Val){0}); + if (val) { + val->val++; + } return 0; } """) diff --git a/tests/python/test_trace_maxactive.py b/tests/python/test_trace_maxactive.py index b0d4d68eb762..4455e9efc47d 100755 --- a/tests/python/test_trace_maxactive.py +++ b/tests/python/test_trace_maxactive.py @@ -14,11 +14,17 @@ def setUp(self): typedef struct { u64 val; } Val; BPF_HASH(stats, Key, Val, 3); int hello(void *ctx) { - stats.lookup_or_init(&(Key){1}, &(Val){0})->val++; + Val *val = stats.lookup_or_init(&(Key){1}, &(Val){0}); + if (val) { + val->val++; + } return 0; } int goodbye(void *ctx) { - stats.lookup_or_init(&(Key){2}, &(Val){0})->val++; + Val *val = stats.lookup_or_init(&(Key){2}, &(Val){0}); + if (val) { + val->val++; + } return 0; } """) diff --git a/tests/python/test_tracepoint.py b/tests/python/test_tracepoint.py index 3bc576a84d55..006516645784 100755 --- a/tests/python/test_tracepoint.py +++ b/tests/python/test_tracepoint.py @@ -29,7 +29,9 @@ def test_tracepoint(self): u64 val = 0; u32 pid = args->next_pid; u64 *existing = switches.lookup_or_init(&pid, &val); - (*existing)++; + if (existing) { + (*existing)++; + } return 0; } """ @@ -53,8 +55,10 @@ def test_tracepoint_data_loc(self): char fn[64]; u32 pid = args->pid; struct value_t *existing = execs.lookup_or_init(&pid, &val); - TP_DATA_LOC_READ_CONST(fn, filename, 64); - __builtin_memcpy(existing->filename, fn, 64); + if (existing) { + TP_DATA_LOC_READ_CONST(fn, filename, 64); + __builtin_memcpy(existing->filename, fn, 64); + } return 0; } """ diff --git a/tests/wrapper.sh.in b/tests/wrapper.sh.in index 90b63ec7d667..41b3521138ac 100755 --- a/tests/wrapper.sh.in +++ b/tests/wrapper.sh.in @@ -8,7 +8,7 @@ name=$1; shift kind=$1; shift cmd=$1; shift -PYTHONPATH=@CMAKE_BINARY_DIR@/src/python +PYTHONPATH=@CMAKE_BINARY_DIR@/src/python/bcc-python LD_LIBRARY_PATH=@CMAKE_BINARY_DIR@:@CMAKE_BINARY_DIR@/src/cc ns=$name diff --git a/tools/biotop.py b/tools/biotop.py index 6c959f671e80..3181ba9d6a5e 100755 --- a/tools/biotop.py +++ b/tools/biotop.py @@ -155,10 +155,12 @@ def signal_ignore(signal_value, frame): 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 ccc5a1079bb4..238048ea85ac 100755 --- a/tools/filetop.py +++ b/tools/filetop.py @@ -119,12 +119,14 @@ def signal_ignore(signal_value, frame): 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 d072af09c8a2..117519523c31 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 63fab877d071..f904533d1bd3 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 85f576812f14..63dd80ac9173 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 = "" }; 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 1edc985655f8..bd5b98b689ef 100755 --- a/tools/lib/ustat.py +++ b/tools/lib/ustat.py @@ -94,7 +94,9 @@ def _generate_functions(self): 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 38d12a251f6d..c0042ffc063e 100755 --- a/tools/old/offcputime.py +++ b/tools/old/offcputime.py @@ -141,7 +141,9 @@ def signal_ignore(signal, frame): 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 3b5bb36c8518..42fa5ce275ed 100755 --- a/tools/old/offwaketime.py +++ b/tools/old/offwaketime.py @@ -189,7 +189,9 @@ def signal_ignore(signal, frame): } 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 e308208eeb31..e6940e678e6b 100755 --- a/tools/old/profile.py +++ b/tools/old/profile.py @@ -185,7 +185,9 @@ def positive_nonzero_int(val): } 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 3b40b1acf781..5708ba69fc92 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 108c800075cc..b60cc4c22403 100755 --- a/tools/old/stackcount.py +++ b/tools/old/stackcount.py @@ -118,7 +118,9 @@ def signal_ignore(signal, frame): 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 783c7ffbbfd7..a4cd521d655c 100644 --- a/tools/old/wakeuptime.py +++ b/tools/old/wakeuptime.py @@ -154,7 +154,9 @@ def signal_ignore(signal, frame): 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 101c585684a8..7b8a421675d5 100755 --- a/tools/slabratetop.py +++ b/tools/slabratetop.py @@ -92,8 +92,10 @@ def signal_ignore(signal, frame): 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 6cbea11620c8..21e788ddf8e8 100755 --- a/tools/syscount.py +++ b/tools/syscount.py @@ -133,12 +133,16 @@ def handle_errno(errstr): 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; }