Blob Blame History Raw
From ed9caea4efcffdd9f37c67b272324a87abfd20c8 Mon Sep 17 00:00:00 2001
From: Jerome Marchand <jmarchan@redhat.com>
Date: Thu, 5 Nov 2020 15:17:14 +0100
Subject: [PATCH] Fix clear() when called on an array

Fixes the following error:
Error looking up elem: -1
terminate called after throwing an instance of 'std::runtime_error'
  what():  Could not clear map with ident "@", err=-1
Aborted (core dumped)
---
 src/bpftrace.cpp | 5 +++++
 src/imap.h       | 4 ++++
 2 files changed, 9 insertions(+)

diff --git a/src/bpftrace.cpp b/src/bpftrace.cpp
index 23b65a5..fe2fb66 100644
--- a/src/bpftrace.cpp
+++ b/src/bpftrace.cpp
@@ -1147,6 +1147,11 @@ int BPFtrace::print_maps()
 int BPFtrace::clear_map(IMap &map)
 {
   std::vector<uint8_t> old_key;
+  if (map.is_array_type())
+  {
+    return zero_map(map);
+  }
+
   try
   {
     if (map.type_.IsHistTy() || map.type_.IsLhistTy() ||
diff --git a/src/imap.h b/src/imap.h
index 27d0d74..ca9f424 100644
--- a/src/imap.h
+++ b/src/imap.h
@@ -27,6 +27,10 @@ class IMap
     return map_type_ == BPF_MAP_TYPE_PERCPU_HASH ||
            map_type_ == BPF_MAP_TYPE_PERCPU_ARRAY;
   }
+  bool is_array_type()
+  {
+    return map_type_ == BPF_MAP_TYPE_PERCPU_ARRAY;
+  }
 
   // unique id of this map. Used by (bpf) runtime to reference
   // this map
-- 
2.25.4