让gdb再次搜索符号文件

时间:2015-06-26 09:12:16

标签: debugging gdb debug-symbols

我的发行版(Debian)将调试文件发送到单独的包中。所以经常发生的事情是我在#include <stdio.h> #include <conio.h> #include <iostream> using namespace std; class point { private: int x,y; public: point(int ox =0, int oy =0) { cout << " Make object" << this << endl; cout << " Using default constructor \n"; x = ox, y = oy; } point(const point &p) { cout << " Make object" << this << endl; cout << " Using copy constructor \n"; x = p.x, y = p.y; } void move(int dx, int dy); void display(); }; point fct(point a); int main() { point a(5,2); a.display(); point b = fct (a); b.display(); getch(); return 0; } void point::move(int dx, int dy) { x += dx, y += dy; } void point::display() { cout << "Coordinates :" << x << " " << y << "\n"; } point fct(point a) { point b=a; //b.move(2,3); return b; } 中运行一个程序直到它崩溃,以获得错误报告的可用回溯。但gdb相当无用,缺少符号信息 - 因为我没有安装相应的bt包。

如果我现在安装软件包,有没有办法让-dbg再次搜索符号文件,而不会丢失我当前的回溯?

2 个答案:

答案 0 :(得分:4)

有一个技巧可以让gdb再次尝试读取符号文件:

(gdb) nosharedlibrary
(gdb) sharedlibrary

第一个命令告诉它忘记它拥有的所有符号信息,第二个命令告诉它重新读取它。

答案 1 :(得分:1)

我将建议使用gdb using System; using System.IO; using System.Linq; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var buffer = new byte[1024]; int pos = 0; using (var fileIn = new FileStream(@"c:\test.txt", FileMode.Open, FileAccess.Read)) using (var fileOut = new FileStream(@"c:\test.txt.binary", FileMode.Create, FileAccess.Write)) while((pos = fileIn.Read(buffer,0,buffer.Length)) > 0) foreach (var value in buffer.Take(pos).Select(x => Convert.ToString(x, 2).PadLeft(8, '0'))) fileOut.Write(value.Select(x => (byte)x).ToArray(), 0, 8); } } } 命令的替代方法,可能它适合您。

这是gcore描述:

gcore

所以我有一个导致崩溃的程序:

(gdb) help gcore
Save a core file with the current state of the debugged process.
Argument is optional filename.  Default filename is 'core.<process_id>'

我用调试信息编译了它。但是,我将带有调试信息的可执行文件放在存档中,并且测试使用剥离版本。

所以它在gdb下崩溃了:

include <iostream>

int f()
{
  time_t curr_ts = time(0);
  std::cout << "Before crash " << curr_ts << std::endl;
  int * ptr = 0;
  *ptr = *ptr +1 ;
  std::cout << "After crash " << curr_ts << std::endl;
  return *ptr;
}

int main()
{

  std::cout << "Before f() " << std::endl;
  f();
  std::cout << "After f() "  << std::endl;
  return 0;
}

我只使用$ gdb ./a.out Reading symbols from ./a.out...(no debugging symbols found)...done. (gdb) r Starting program: /home/crash/a.out Before f() Before crash 1435322344 Program received signal SIGSEGV, Segmentation fault. 0x000000000040097d in ?? () (gdb) bt #0 0x000000000040097d in ?? () #1 0x00000000004009e0 in ?? () #2 0x000000314981ed1d in __libc_start_main () from /lib64/libc.so.6 #3 0x00000000004007f9 in ?? () #4 0x00007fffffffde58 in ?? () #5 0x000000000000001c in ?? () #6 0x0000000000000001 in ?? () #7 0x00007fffffffe1a9 in ?? () #8 0x0000000000000000 in ?? () (gdb) gcore crash2.core Saved corefile crash2.core 生成核心文件并保留gdb。然后我从存档中获得带有调试符号的版本,我可以看到所有符号:

gcore

更新

如果您$ gdb ./a.out ./crash2.core Reading symbols from ./a.out...done. warning: exec file is newer than core file. [New LWP 15215] Core was generated by `/home/crash/a.out'. Program terminated with signal SIGSEGV, Segmentation fault. #0 0x000000000040097d in f () at main.cpp:8 8 *ptr = *ptr +1 ; (gdb) bt #0 0x000000000040097d in f () at main.cpp:8 #1 0x00000000004009e0 in main () at main.cpp:17 (gdb) info locals curr_ts = 1435322344 ptr = 0x0 ,您至少会看到此set backtrace past-main on。如果您只分析核心文件(可能甚至没有保存在那里)保存机智__libc_start_main,则不会打印上面__libc_start_main以上的内容:

gcore

但如果我使用我的测试程序(包含调试信息)重现$ gdb ./a.out crash2.core Reading symbols from ./a.out...done. warning: exec file is newer than core file. [New LWP 15215] Core was generated by `/home/crash/a.out'. Program terminated with signal SIGSEGV, Segmentation fault. #0 0x000000000040097d in f () at main.cpp:8 8 *ptr = *ptr +1 ; (gdb) set backtrace past-main on (gdb) bt #0 0x000000000040097d in f () at main.cpp:8 #1 0x00000000004009e0 in main () at main.cpp:17 #2 0x000000314981ed1d in __libc_start_main () from /lib64/libc.so.6 Backtrace stopped: Cannot access memory at address 0x4007d0 (gdb) 下的崩溃,我可以看到所有内容(请参阅gdb&amp;&amp; set backtrace past-main on):

set backtrace past-entry on