@@ -1302,22 +1302,18 @@ int BPFtrace::clear_map(const BpfMap &map)
13021302 if (!map.is_clearable ())
13031303 return zero_map (map);
13041304
1305- auto maybe_old_key = find_empty_key (map);
1306- if (!maybe_old_key.has_value ()) {
1307- return -2 ;
1308- }
1309- auto old_key (std::move (*maybe_old_key));
1310- auto key (old_key);
1305+ uint8_t *old_key = nullptr ;
1306+ auto key = std::vector<uint8_t >(map.key_size ());
13111307
13121308 // snapshot keys, then operate on them
13131309 std::vector<std::vector<uint8_t >> keys;
1314- while (bpf_get_next_key (map.fd (), old_key. data () , key.data ()) == 0 ) {
1310+ while (bpf_get_next_key (map.fd (), old_key, key.data ()) == 0 ) {
13151311 keys.push_back (key);
1316- old_key = key;
1312+ old_key = key. data () ;
13171313 }
13181314
1319- for (auto &key : keys) {
1320- int err = bpf_delete_elem (map.fd (), key .data ());
1315+ for (auto &k : keys) {
1316+ int err = bpf_delete_elem (map.fd (), k .data ());
13211317 if (err && err != -ENOENT) {
13221318 LOG (ERROR) << " failed to look up elem: " << err;
13231319 return -1 ;
@@ -1331,24 +1327,21 @@ int BPFtrace::clear_map(const BpfMap &map)
13311327int BPFtrace::zero_map (const BpfMap &map)
13321328{
13331329 uint64_t nvalues = map.is_per_cpu_type () ? ncpus_ : 1 ;
1334- auto maybe_old_key = find_empty_key (map);
1335- if (!maybe_old_key.has_value ()) {
1336- return -2 ;
1337- }
1338- auto old_key (std::move (*maybe_old_key));
1339- auto key (old_key);
1330+
1331+ uint8_t *old_key = nullptr ;
1332+ auto key = std::vector<uint8_t >(map.key_size ());
13401333
13411334 // snapshot keys, then operate on them
13421335 std::vector<std::vector<uint8_t >> keys;
1343- while (bpf_get_next_key (map.fd (), old_key. data () , key.data ()) == 0 ) {
1336+ while (bpf_get_next_key (map.fd (), old_key, key.data ()) == 0 ) {
13441337 keys.push_back (key);
1345- old_key = key;
1338+ old_key = key. data () ;
13461339 }
13471340
13481341 int value_size = map.value_size () * nvalues;
13491342 std::vector<uint8_t > zero (value_size, 0 );
1350- for (auto &key : keys) {
1351- int err = bpf_update_elem (map.fd (), key .data (), zero.data (), BPF_EXIST);
1343+ for (auto &k : keys) {
1344+ int err = bpf_update_elem (map.fd (), k .data (), zero.data (), BPF_EXIST);
13521345
13531346 if (err && err != -ENOENT) {
13541347 LOG (ERROR) << " failed to look up elem: " << err;
@@ -1367,17 +1360,14 @@ int BPFtrace::print_map(const BpfMap &map, uint32_t top, uint32_t div)
13671360 return print_map_hist (map, top, div);
13681361
13691362 uint64_t nvalues = map.is_per_cpu_type () ? ncpus_ : 1 ;
1370- auto maybe_old_key = find_empty_key (map);
1371- if (!maybe_old_key.has_value ()) {
1372- return -2 ;
1373- }
1374- auto old_key (std::move (*maybe_old_key));
1375- auto key (old_key);
1363+
1364+ uint8_t *old_key = nullptr ;
1365+ auto key = std::vector<uint8_t >(map.key_size ());
13761366
13771367 std::vector<std::pair<std::vector<uint8_t >, std::vector<uint8_t >>>
13781368 values_by_key;
13791369
1380- while (bpf_get_next_key (map.fd (), old_key. data () , key.data ()) == 0 ) {
1370+ while (bpf_get_next_key (map.fd (), old_key, key.data ()) == 0 ) {
13811371 auto value = std::vector<uint8_t >(map.value_size () * nvalues);
13821372 int err = bpf_lookup_elem (map.fd (), key.data (), value.data ());
13831373 if (err == -ENOENT) {
@@ -1391,7 +1381,7 @@ int BPFtrace::print_map(const BpfMap &map, uint32_t top, uint32_t div)
13911381
13921382 values_by_key.push_back ({ key, value });
13931383
1394- old_key = key;
1384+ old_key = key. data () ;
13951385 }
13961386
13971387 if (value_type.IsCountTy () || value_type.IsSumTy () || value_type.IsIntTy ()) {
@@ -1456,17 +1446,14 @@ int BPFtrace::print_map_hist(const BpfMap &map, uint32_t top, uint32_t div)
14561446 // would actually be stored with the key: [1, 2, 3]
14571447
14581448 uint64_t nvalues = map.is_per_cpu_type () ? ncpus_ : 1 ;
1459- auto maybe_old_key = find_empty_key (map);
1460- if (!maybe_old_key.has_value ()) {
1461- return -2 ;
1462- }
1463- auto old_key (std::move (*maybe_old_key));
1464- auto key (old_key);
1449+
1450+ uint8_t *old_key = nullptr ;
1451+ auto key = std::vector<uint8_t >(map.key_size ());
14651452
14661453 std::map<std::vector<uint8_t >, std::vector<uint64_t >> values_by_key;
14671454
14681455 const auto &map_info = resources.maps_info .at (map.name ());
1469- while (bpf_get_next_key (map.fd (), old_key. data () , key.data ()) == 0 ) {
1456+ while (bpf_get_next_key (map.fd (), old_key, key.data ()) == 0 ) {
14701457 auto key_prefix = std::vector<uint8_t >(map_info.key_type .GetSize ());
14711458 uint64_t bucket = read_data<uint64_t >(key.data () +
14721459 map_info.key_type .GetSize ());
@@ -1495,7 +1482,7 @@ int BPFtrace::print_map_hist(const BpfMap &map, uint32_t top, uint32_t div)
14951482 values_by_key[key_prefix].at (bucket) = reduce_value<uint64_t >(value,
14961483 nvalues);
14971484
1498- old_key = key;
1485+ old_key = key. data () ;
14991486 }
15001487
15011488 // Sort based on sum of counts in all buckets
@@ -1532,33 +1519,6 @@ std::optional<std::string> BPFtrace::get_watchpoint_binary_path() const
15321519 }
15331520}
15341521
1535- std::optional<std::vector<uint8_t >> BPFtrace::find_empty_key (
1536- const BpfMap &map) const
1537- {
1538- // 4.12 and above kernel supports passing NULL to BPF_MAP_GET_NEXT_KEY
1539- // to get first key of the map. For older kernels, the call will fail.
1540- auto key = std::vector<uint8_t >(map.key_size ());
1541- uint64_t nvalues = map.is_per_cpu_type () ? ncpus_ : 1 ;
1542- int value_size = map.value_size () * nvalues;
1543- auto value = std::vector<uint8_t >(value_size);
1544-
1545- if (bpf_lookup_elem (map.fd (), key.data (), value.data ()))
1546- return key;
1547-
1548- for (auto &elem : key)
1549- elem = 0xff ;
1550- if (bpf_lookup_elem (map.fd (), key.data (), value.data ()))
1551- return key;
1552-
1553- for (auto &elem : key)
1554- elem = 0x55 ;
1555- if (bpf_lookup_elem (map.fd (), key.data (), value.data ()))
1556- return key;
1557-
1558- LOG (ERROR) << " Failed to get key for map '" << map.name ();
1559- return std::nullopt ;
1560- }
1561-
15621522std::string BPFtrace::get_stack (int64_t stackid,
15631523 uint32_t nr_stack_frames,
15641524 int32_t pid,
0 commit comments