linux gdb 调试SO库

wKiom1Mu6IDyqy7yAATw8Z590VA882.jpg

为了对调用的SO库进行调试,在编译SO库时也要如上图所示加入调试信息。


首先   g++ -c -fPIC -g -o Ccalculate.o Ccalculate.cpp 是将Ccalculate.cpp生成.o文件,为生成SO文件准备。

g++ -shared -g -o libCompute.so Ccalculate.o 将点o文件生存SO共享库。注意这里在编译时都加入了-g选项。这样后面就可以使用GDB对其进行调试。


g++ main.cpp -L. -lCompute -g -o somain  main.cpp引用Compute.so库,并生成可执行文件somain。这里也加入了-g调试选项。


如下图所示成功进入SO库,进行调试。


root@ubuntu:/home/administrator/桌面/Java# gdb somain
GNU gdb (GDB) 7.5.91.20130417-cvs-ubuntu
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/administrator/桌面/Java/somain...done.
(gdb) info break
No breakpoints or watchpoints.
(gdb) l 11
6   int main()
7   {
8       printf("it is in main\n");
9       Calculate *pCalculate = new Calculate();
10          vector<double> vResult;
11          pCalculate->ComputeValue();
12          pCalculate->GetResult(vResult);
13      for(int iIndex = 0; iIndex < vResult.size(); iIndex ++)
14          {
15          cout<<"value is"<<vResult[iIndex]<<endl;
(gdb) break 10
Breakpoint 1 at 0x401559: file main.cpp, line 10.
(gdb) break 11
Breakpoint 2 at 0x401565: file main.cpp, line 11.
(gdb) break 12
Breakpoint 3 at 0x401571: file main.cpp, line 12.
(gdb) break 15
Breakpoint 4 at 0x40158d: file main.cpp, line 15.
(gdb) r
Starting program: /home/administrator/桌面/Java/somain
it is in main
Breakpoint 1, main () at main.cpp:10
10          vector<double> vResult;
(gdb) n
Breakpoint 2, main () at main.cpp:11
11          pCalculate->ComputeValue();
(gdb) s
Calculate::ComputeValue (this=0x604010) at Ccalculate.cpp:14
14      InitVector();
(gdb) list 14
9       }
10  }
11
12  void Calculate::ComputeValue()
13  {
14      InitVector();
15      for(int iINdex =0; iINdex < m_vSrcValue.size(); iINdex ++)
16      {
17          double dTemp = m_vSrcValue[iINdex] * 462 + 234/23+65*423;
18          m_vCalvalue.push_back(dTemp);
(gdb) n
15      for(int iINdex =0; iINdex < m_vSrcValue.size(); iINdex ++)
(gdb) n
17          double dTemp = m_vSrcValue[iINdex] * 462 + 234/23+65*423;
(gdb) n
18          m_vCalvalue.push_back(dTemp);
(gdb) print dTemp
$1 = 27505
(gdb) n
15      for(int iINdex =0; iINdex < m_vSrcValue.size(); iINdex ++)
(gdb)



你可能感兴趣的:(linux,so库调试)