bpf 实现支持热插拔的内存泄露检测

This commit is contained in:
1iaan
2026-01-27 13:57:37 +00:00
parent c99867b342
commit 226f9a510f
4 changed files with 38 additions and 2 deletions

View File

@@ -28,6 +28,6 @@
<memory> <memory>
<allocator>mypool</allocator> <!-- jemalloc / malloc / mypool --> <allocator>mypool</allocator> <!-- jemalloc / malloc / mypool -->
<leakage>enable</leakage> <!-- enable/disable--> <leakage>disable</leakage> <!-- enable/disable-->
</memory> </memory>
</config> </config>

View File

@@ -84,7 +84,7 @@ void *nMalloc(size_t size, const char * filename, const char *func, int line){
return NULL; return NULL;
} }
fprintf(fp, "[+] [%s : %s : %d] %p: %ld malloc\n", filename, func, line, ptr, size); fprintf(fp, "[+] [%s:%d:%s] [%p:%ld]\n", filename, line, func, ptr, size);
fflush(fp); fflush(fp);
fclose(fp); fclose(fp);

BIN
memory/leak_detect/bpftrace Executable file

Binary file not shown.

View File

@@ -0,0 +1,36 @@
BEGIN
{
printf("Start memory leakage detect\n");
}
uprobe:/home/lian/share/9.1-kvstore/kvstore:nMalloc
{
@tmp_size[tid] = arg0;
@tmp_file[tid] = str(arg1);
@tmp_func[tid] = str(arg2);
@tmp_line[tid] = arg3;
}
uretprobe:/home/lian/share/9.1-kvstore/kvstore:nMalloc
{
$ptr = retval;
if ($ptr != 0) {
@allocs[$ptr] = ( @tmp_file[tid], @tmp_line[tid], @tmp_func[tid], @tmp_size[tid]);
}
delete(@tmp_size[tid]);
delete(@tmp_file[tid]);
delete(@tmp_func[tid]);
delete(@tmp_line[tid]);
}
uprobe:/home/lian/share/9.1-kvstore/kvstore:nFree
{
delete(@allocs[arg0]);
}
END
{
printf("\n=== Memory Leaks ===\n");
}