我正在尝试跟踪每次Redis调用malloc(实际上是redis中的zmalloc)时使用的物理地址。我的想法是通过进程名称匹配遍历proc / cmdline来确认PID,然后获取/ proc / PID / pagemapto计算物理地址。所以我的第一个尝试是在zmalloc函数中添加get PID函数。
void *zmalloc(const char* action,size_t size) {
void *ptr = malloc(size+PREFIX_SIZE);
/***********************************************/
unsigned long virt_addr = (unsigned long)ptr;
unsigned long phy_addr, pfn;
//find out pid of "redis-server"
int result=get_pid("redis-server"); //
printf("pid:%d\n",result);
/*************************************************/
if (!ptr) zmalloc_oom_handler(size);
#ifdef HAVE_MALLOC_SIZE
update_zmalloc_stat_alloc(zmalloc_size(ptr));
return ptr;
#else
*((size_t*)ptr) = size;
update_zmalloc_stat_alloc(size+PREFIX_SIZE);
return (char*)ptr+PREFIX_SIZE;
#endif
}
获取pid函数是:
int get_pid(char* pname)
{
const char* directory = "/proc";
size_t taskNameSize = 1024;
char* taskName = (char*)calloc(1, taskNameSize);
DIR* dir = opendir(directory);
if (dir)
{
struct dirent* de = 0;
while ((de = readdir(dir)) != 0)
{
if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
continue;
int pid = -1;
int res = sscanf(de->d_name, "%d", &pid);
if (res == 1)
{
// we have a valid pid
// open the cmdline file to determine what's the name of the process running
char cmdline_file[1024] = {0};
sprintf(cmdline_file, "%s/%d/cmdline", directory, pid);
FILE* cmdline = fopen(cmdline_file, "r");
if (getline(&taskName, &taskNameSize, cmdline) > 0)
{
// is it the process we care about?
if (strstr(taskName, pname) != 0)
{
#ifdef DEBUG_MODE
fprintf(stdout, "A %s process, with PID %d, has been detected.\n", pname, pid);
#endif
free(taskName);
return pid;
}
}
fclose(cmdline);
}
}
closedir(dir);
return -1;
}
free(taskname);
return -1;
}
但是它不能在make test
的所有步骤上都起作用。屏幕输出为
......
pid:12843
pid:12843
pid:12843
pid:12843
pid:12843
pid:12843
pid:12843
pid:12843
pid:12843
pid:12843
pid:12843
pid:12843
pid:12843
pid:12843
pid:12843
pid:12843
pid:12843
pid:12
[exception]: Executing test client: could not set permissions for file "./tests/tmp/server.rdb-startup-test.12826.8/dump.rdb": no such file or directory.
could not set permissions for file "./tests/tmp/server.rdb-startup-test.12826.8/dump.rdb": no such file or directory
while executing
"file attributes [file join $server_path dump.rdb] -permissions 0222"
(file "tests/integration/rdb.tcl" line 73)
invoked from within
"source $path"
(procedure "execute_tests" line 4)
invoked from within
"execute_tests $data"
(procedure "test_client_main" line 10)
invoked from within
"test_client_main $::test_server_port "
Killing still running Redis server 12844
Killing still running Redis server 12843
Killing still running Redis server 12845
Killing still running Redis server 12847
Killing still running Redis server 12850
Killing still running Redis server 12848
Killing still running Redis server 12856
Killing still running Redis server 12855
Killing still running Redis server 12859
Killing still running Redis server 12862
Killing still running Redis server 12864
Killing still running Redis server 12865
Killing still running Redis server 12863
Killing still running Redis server 12866
Killing still running Redis server 12867
Killing still running Redis server 12868
...........
此错误的更多信息是
=== REDIS BUG REPORT START: Cut & paste starting from here ===
13122:M 26 Feb 2019 09:49:29.182 # Redis 5.0.0 crashed by signal: 11
13122:M 26 Feb 2019 09:49:29.182 # Crashed running the instruction at: 0x7f49c7cb270c
13122:M 26 Feb 2019 09:49:29.182 # Accessing address: (nil)
13122:M 26 Feb 2019 09:49:29.182 # Failed assertion: <no assertion failed> (<no file>:0)
------ STACK TRACE ------
EIP:
/lib64/libc.so.6(getdelim+0x2c)[0x7f49c7cb270c]
Backtrace:
src/redis-server(logStackTrace+0x29)[0x469689]
src/redis-server(sigsegvHandler+0xac)[0x469d2c]
/lib64/libpthread.so.0(+0xf5d0)[0x7f49c801f5d0]
/lib64/libc.so.6(getdelim+0x2c)[0x7f49c7cb270c]
src/redis-server(get_pid+0xd4)[0x4301f4]
src/redis-server(zmalloc+0x27)[0x4302d7]
src/redis-server(createObject+0x1b)[0x43aa4b]
src/redis-server(createSharedObjects+0x4fa)[0x427e2a]
src/redis-server(initServer+0xf4)[0x42cc24]
src/redis-server(main+0x3c3)[0x4207c3]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7f49c7c653d5]
src/redis-server[0x420ae3]
------ INFO OUTPUT ------
感谢您的回复和帮助!