GDB找不到源文件

时间:2016-05-23 13:21:57

标签: c++ opencv gdb shared-libraries

我正在尝试调试OpenCV代码,但 gdb 无法加载OpenCV源文件。

检查源文件是否存在:

$ls -l /home/rui/DriveSo/opencv/modules/core/src/matrix.cpp
-rw-r--r-- 1 rui rui 156046 May 11 21:46 /home/rui/DriveSo/opencv/modules/core/src/matrix.cpp

在与OpenCV Debug模式库链接后,我启动 dbg 并将其添加到源目录列表中:

$gdb DriveSo
GNU gdb (Debian 7.7.1+dfsg-5) 7.7.1
....
Reading symbols from DriveSo...done.
(gdb) directory /home/rui/DriveSo/opencv/modules/core/src
Source directories searched: /home/rui/DriveSo/opencv/modules/core/src:$cdir:$cwd
(gdb) break matrix.cpp:2349
No source file named matrix.cpp.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (matrix.cpp:2349) pending.

即使在启动会话后,gdb仍然找不到源文件:

(gdb) start
Temporary breakpoint 2 at 0x41d2cf: file ../../DriveSo.dkt/main.cpp, line 11.
Starting program: /home/rui/DriveSo/repositories/trunk/builds/build-DriveSo-Desktop_Qt_5_5_0_GCC_64bit-Debug/DriveSo
....
Temporary breakpoint 2, main (argc=1, argv=0x7fffffffe5a8) at ../../DriveSo.dkt/main.cpp:11
11 QCoreApplication::setApplicationName(QString("DriveSo"));
(gdb)

导致这种(对我而言)意外行为的原因是什么?

跟进iksajotien的建议,据我所知,可执行文件有调试信息......

$ objdump --syms DriveSo | grep debug
0000000000000000 l    d  .debug_aranges 0000000000000000              .debug_aranges
0000000000000000 l    d  .debug_info    0000000000000000              .debug_info
0000000000000000 l    d  .debug_abbrev  0000000000000000              .debug_abbrev
0000000000000000 l    d  .debug_line    0000000000000000              .debug_line
0000000000000000 l    d  .debug_str 0000000000000000              .debug_str
0000000000000000 l    d  .debug_ranges  0000000000000000              .debug_ranges

...以及由它打开的共享库......

$ lsof -p 12159 
COMMAND   PID USER   FD      TYPE             DEVICE   SIZE/OFF      NODE NAME
...
DriveSo 12159  rui  mem       REG                8,6   20289800  26740265 /usr/local/lib/libopencv_core.so.3.1.0
...

...也有调试信息。

$ objdump --syms /usr/local/lib/libopencv_core.so.3.1.0 | grep debug
0000000000000000 l    d  .debug_aranges 0000000000000000              .debug_aranges
0000000000000000 l    d  .debug_info    0000000000000000              .debug_info
0000000000000000 l    d  .debug_abbrev  0000000000000000              .debug_abbrev
0000000000000000 l    d  .debug_line    0000000000000000              .debug_line
0000000000000000 l    d  .debug_str 0000000000000000              .debug_str
0000000000000000 l    d  .debug_ranges  0000000000000000              .debug_ranges

gdb info sharing:

$gdb DriveSo
GNU gdb (Debian 7.7.1+dfsg-5) 7.7.1
...
Reading symbols from DriveSo...done.
(gdb) run
Starting program: /home/rui/DriveSo/repositories/trunk/builds/build-DriveSo-Desktop_Qt_5_5_0_GCC_64bit-Debug/DriveSo 
...
^C
Program received signal SIGINT, Interrupt.
...
(gdb) info shared
From                To                  Syms Read   Shared Object Library
0x00007ffff7ddcae0  0x00007ffff7df5130  Yes         /lib64/ld-linux-x86-64.so.2
                                        No          linux-vdso.so.1
0x00007ffff7158860  0x00007ffff794a910  Yes         /usr/local/lib/libopencv_core.so.3.1
0x00007ffff6e5d880  0x00007ffff6e63a0b  Yes         /usr/local/lib/libopencv_highgui.so.3.1
0x00007ffff5493840  0x00007ffff690ce00  Yes         /usr/local/lib/libopencv_imgproc.so.3.1
0x00007ffff50aa6f0  0x00007ffff5158bd1  Yes         /usr/local/lib/libopencv_features2d.so.3.1
0x00007ffff4db8640  0x00007ffff4e3de75  Yes         /usr/local/lib/libopencv_imgcodecs.so.3.1
0x00007ffff4b72a80  0x00007ffff4b931b0  Yes         /usr/local/lib/libopencv_videoio.so.3.1
0x00007ffff47b7d40  0x00007ffff4920f80  Yes         /usr/local/lib/libopencv_calib3d.so.3.1
...
---Type <return> to continue, or q <return> to quit---

查看 gdb源代码 solib.c在第1053行有以下内容:

   if (! ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ()))
          && so->symbols_loaded
          && !objfile_has_symbols (so->objfile))
        {
          so_missing_debug_info = 1;
          ui_out_field_string (uiout, "syms-read", "Yes (*)");
        }
      else
        ui_out_field_string (uiout, "syms-read",
                             so->symbols_loaded ? "Yes" : "No");

因此我假设已加载符号并且存在调试信息,否则库将列出是(*)

1 个答案:

答案 0 :(得分:0)

我遇到了完全相同的问题,并通过在我的makefile上附加一个rpath来解决了这个问题。

g++ -o $@ $< -L$(LIBS) -lmylib -Wl,-rpath,$(LIBS)