Skip to content

Commit aa16385

Browse files
committed
update map cleanup process
1 parent aae446b commit aa16385

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

main.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -536,11 +536,16 @@ func displayMapContents(ctx context.Context, bpfMap *bpf.BPFMap) {
536536

537537
iter := bpfMap.Iterator()
538538
count := 0
539+
var keysToDelete [][]byte
539540

540541
for iter.Next() {
541542
keyBytes := iter.Key()
542543

543-
v, _ := bpfMap.GetValue(unsafe.Pointer(&keyBytes[0]))
544+
v, err := bpfMap.GetValue(unsafe.Pointer(&keyBytes[0]))
545+
if err != nil {
546+
slog.Warn("Failed to get value from BPF map", "error", err)
547+
continue
548+
}
544549

545550
filename := cstring(keyBytes)
546551
slog.Info(filename)
@@ -557,9 +562,9 @@ func displayMapContents(ctx context.Context, bpfMap *bpf.BPFMap) {
557562
var n int64
558563

559564
buf := bytes.NewReader(v)
560-
err := binary.Read(buf, binary.LittleEndian, &n)
561-
if err != nil {
562-
panic(err)
565+
if err := binary.Read(buf, binary.LittleEndian, &n); err != nil {
566+
slog.Warn("Failed to read timestamp from BPF map value", "filename", filename, "error", err, "value_len", len(v))
567+
continue
563568
}
564569

565570
boottime, _ := getBootTimeUnix()
@@ -578,12 +583,19 @@ func displayMapContents(ctx context.Context, bpfMap *bpf.BPFMap) {
578583
tLocal := t.Local()
579584
slog.Info(tLocal.Format(time.RFC3339))
580585

581-
bpfMap.DeleteKey(unsafe.Pointer(&keyBytes[0]))
582-
slog.Info(filename + " deleted")
586+
// Collect key for deletion after iteration
587+
keyCopy := make([]byte, len(keyBytes))
588+
copy(keyCopy, keyBytes)
589+
keysToDelete = append(keysToDelete, keyCopy)
583590
count++
584591

585592
}
586593

594+
// Delete keys after iteration to avoid corrupting the iterator
595+
for _, key := range keysToDelete {
596+
bpfMap.DeleteKey(unsafe.Pointer(&key[0]))
597+
}
598+
587599
if count == 0 {
588600
fmt.Println("No data yet...")
589601
} else {

0 commit comments

Comments
 (0)