iovisor / ply

Dynamic Tracing in Linux
GNU General Public License v2.0
969 stars 90 forks source link

Question: clear up map? #51

Closed Leo-Yan closed 4 years ago

Leo-Yan commented 4 years ago

Hi there,

Before we can clear map with assignment 'nil', seems now the latest code base doesn't support 'nil' anymore. So e.g. below program cannot work.

kprobe:kmem_cache_alloc_node {                
        s[cpu,kpid] = stack();                
}                                             

kretprobe:kmem_cache_alloc_node {             
        @[retval] = s[cpu,kpid];              
        s[cpu,kpid] = nil;                    
}                                             

kprobe:kmem_cache_free {                      
        @[arg1] = nil;                        
}                                             

If I change 'nil' to 0, the program can run successfully. But the assignment for the sentence "@[retval] = s[cpu,kpid]" also cannot work as expected, I observe the result as below:

{ -281474254472384 }: { -281474254472000 }: { -281474231713792 }: { -281474177160960 }: { -281474148187360 }: { -281474147794048 }: { -281474147622912 }: { -281474132892352 }: { -281474132891456 }: { -281474132887872 }: { -281474132887424 }: { -281474132886528 }:

Thanks for the suggestion!

wkz commented 4 years ago

You're quite right. In version 2 the syntax changed to delete map[key], but I have forgotten to update the manual. I assume you're looking to track where live objects where allocated from? Here is a script that should get the job done:

k:kmem_cache_alloc_node {
    from[cpu,kpid] = stack();
}

kr:kmem_cache_alloc_node {
    if (from[cpu, kpid]) {
        live[retval] = from[cpu,kpid];
        delete from[cpu,kpid];
    }
}

k:kmem_cache_free {
    delete live[arg1];
}
Leo-Yan commented 4 years ago

Thank you for the detailed explanation, I verified at my side, the new script works pretty well!

Update: sorry I didn't answer your question. This program is purposed to track memory leaking; it uses map to record the pointer with stack (who is using it) and the map is deleted when free the memory block.