linux下的C++程序有时候编译正常,但是可能会出现内存泄露、死锁等问题。
我今天的程序大概是释放了已经释放的内存,重复释放,造成服务器宕机,然后用到gdb调试的功能,就记录一下,(小菜一个,也是第一次接触C++的服务器程序)
首先需要开启:生成core文件的功能,(当程序意外退出的时候,内核会自动生成core的文件,用于差错、调试,这个文件是一个内存镜像)
ulimit -c unlimited
core文件生成位置一般是在程序所在目录,不过这个位置可以设置,一般放在/tmp/corefile文件夹内,corefile的文件名可以根据时间戳、pid、程序名字等来命名。(core.http_server_1443017912 http_server 是程序名称,1443017912是当前时间戳)这个命名办法,可以在网上找到很多。
然后宕机之后,来查看corefile是否存在,没存在 可以用ulimit -a 命令查看core的状态。
core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 31490 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited open files (-n) 65535 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 65535 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited我的查看之后是这样的。(如果不可以可以下面留言,)
当你看到corefile文件后,可以在当前目录下,执行gdb http_server core.http_server_1443017912
gdb 程序名 corefile文件名
会进入gdb模式,另外显示
Core was generated by `./http_server'. Program terminated with signal 11, Segmentation fault. #0 0x00007fe537e39eaf in ?? ()
Core was generated by `./http_server'是让你找到你的程序可执行文件的位置,然后执行,(如果你细心的话,你看到那个上面 “#0 0x00007fe537e39eaf in ?? ()” 里面in后面是问号,这里应该显示代码调试信息的代码位的。 此时就是需要执行以下你的程序的可执行文件。)
命令为 file 可执行文件的位置:如下
file /home/user/imserver/bin/http_server/./http_server
<span style="font-size:24px;color:#3333ff;">执行结果如下:</span>
<span style="font-size:24px;color:#3333ff;">然后再使用bt命令查看调用堆栈信息。</span>
(gdb) file /home/user/imserver/bin/http_server/./http_server warning: exec file is newer than core file. Reading symbols from /home/user/imserver/bin/http_server/http_server...done. (gdb) bt #0 0x00007fe537e39eaf in ?? () #1 0x00007fff4dfa6b20 in ?? () #2 0x00000000004a5f06 in CRWLock::wlock() () #3 0x000000000049f6e5 in CStrExplode::CStrExplode(char*, char) () at /root/workspace/server/src/base/util.cpp:84 #4 0x0000000000498e30 in CHttpConn::OnRead() () at /home/user/imserver/src/http_server/HttpConn.cpp:154 #5 0x000000000049414a in CHttpQuery::DispatchQuery(std::string&, std::string&, CHttpConn*) () at /home/user/imserver/src/http_server/HttpQuery.cpp:133 #6 0x0000000000499191 in CHttpConn::OnRead() () at /home/user/imserver/src/http_server/HttpConn.cpp:188 #7 0x00000000004989ab in http_conn_timer_callback(void*, unsigned char, unsigned int, void*) () at /home/user/imserver/src/http_server/HttpConn.cpp:68 #8 0x00000000004a2583 in CBaseSocket::SetSendBufSize(unsigned int) () at /root/workspace/server/src/base/BaseSocket.cpp:221 #9 0x00000000004a703c in std::_List_base<CEventDispatch::TimerItem*, std::allocator<CEventDispatch::TimerItem*> >::_List_impl::~_List_impl() () at /usr/include/c++/4.8.2/bits/stl_list.h:310 #10 0x00000000004a1085 in FindImConn(__gnu_cxx::hash_map<int, CImConn*, __gnu_cxx::hash<int>, std::equal_to<int>, std::allocator<CImConn*> >*, int) () at /root/workspace/server/src/base/imconn.cpp:17 #11 0x000000000048e068 in main () at /home/zhanglianfa/imserver/src/http_msg_server/http_msg_server.cpp:122
注:如果那里有错误,希望帮我指正,谢谢!