linux crash在动态库中,通过map查找crash位置

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();
}
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值