linux进程crash时,通常我们可以通过gdb+core或addr2line解析出进程crash在哪一个函数中,有时crash在动态链接库中,解析出的core可能只有一个地址,而不能知道是在哪一个函数,这时可以尝试通过/proc/进程ID/maps的方法找到crash的位置。
一个动态库的实现:
头文件sample_fun.h
#ifndef _SAMPLE_FUN_H_
#define _SAMPLE_FUN_H_
void testFun4();
#endif
源文件sample_fun.cpp
#include <stdio.h>
#include "sample_fun.h"
void testFun4()
{
int* p = NULL;
printf("%d", *p); //will crash
}
使用g++将这个源文件编译出动态库
g++ -fPIC -shared sample_fun.cpp -o libsample_fun.so
调用动态库的实现sample_main.cpp
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "sample_fun.h"
using namespace std;
#define BUFFLEN 256
void testFun1();
void testFun2();
void testFun3();
int main(int argc, char** argv)
{
int pid = getpid();
char cmdBuf[BUFFLEN] = {0x00};
sprintf(cmdBuf, "cat /proc/%d/maps > /home/nap/%d.maps", pid, pid); //save current process maps
system(cmdBuf);
testFun1();
}
void testFun1()
{
testFun2();
}
void testFun2()
{
testFun3();
}