Linux 如何调试';软件中so文件的一部分是什么?

Linux 如何调试';软件中so文件的一部分是什么?,linux,visual-studio,debugging,dll,shared-libraries,Linux,Visual Studio,Debugging,Dll,Shared Libraries,所以我有这个软件,它有多个.So文件。对于其中一个so文件,我对代码做了一些更改,但我希望看到so文件中存在的文件的逐行执行 在使用VisualStudio时,为了提供参考,我们可以将代码附加到使用dll文件的进程,然后一旦附加了代码,我们就在要调试的文件中放置断点,然后逐步转到下一行 如何在Linux机器上实现同样的功能?有什么能帮我的吗 我有一个使用so文件的软件,该so文件在其构建过程中有多个cpp文件,我想调试该so文件中的一个文件。如果.so有调试信息,它只是一个小问题,您只需使用调试

所以我有这个软件,它有多个.So文件。对于其中一个so文件,我对代码做了一些更改,但我希望看到so文件中存在的文件的逐行执行

在使用VisualStudio时,为了提供参考,我们可以将代码附加到使用dll文件的进程,然后一旦附加了代码,我们就在要调试的文件中放置断点,然后逐步转到下一行

如何在Linux机器上实现同样的功能?有什么能帮我的吗


我有一个使用so文件的软件,该so文件在其构建过程中有多个cpp文件,我想调试该so文件中的一个文件。

如果.so有调试信息,它只是一个小问题,您只需使用调试器运行您的应用程序,并执行您提到的相同操作,放置一些断点。您有许多调试器和调试选项。例如,您可以从控制台运行gdb,请求执行应用程序:

>gdb yourAppPath
或附加到正在运行的进程:

>gdb
(gdb) attach runningProcessId
一旦进入gdb,您就可以在任何地方放置断点(您应该阅读gdb文档)

另一种选择是为gdb(即:kgdb)使用GUI前端。此外,linux上可用的大多数IDE都与gdb进行了良好的图形集成

现在,如果库(.so)文件中没有调试符号,则应该使用编译器的调试信息生成选项重新编译它们。如果您使用的是gcc,它将是'-g'

如果库是由您的发行版提供的,那么大多数库都有单独的调试符号包,一旦您安装了这些包,这些符号将可用,gdb应该自动加载它们。例如,在OpenSuse中,您有包含glibc符号的glibc包和glibc debuginfo包

下面是在我的一个项目上使用gdb的一个简单示例:

首先,我使用搜索路径调用gdb来搜索所有库,因为它们不在系统路径中。如果您的库已“安装”,因此可以从系统路径访问它们,则无需指定LD_LIBRARY_路径

>LD_LIBRARY_PATH=.:~/projects/asdstoolkit2/asdscore/:~/projects/asdstoolkit2/asdscrypto:~/projects/asdstoolkit2/asdsnet:~/projects/asdstoolkit2/codb/codb gdb ../clibrarian/clibrarian 

#output

Copyright (C) 2020 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-suse-linux".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://bugs.opensuse.org/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ../clibrarian/clibrarian...
(gdb) 

然后您可以列出一个方法源代码:(您可以点击tab键自动完成)


请不要完全重写问题。读者可能会感到非常困惑。附加到进程后,如何向文件添加断点,因为“so”在其环境中包含多个cpp文件。您要查找的方法是在库中还是在应用程序中并不重要。在那个阶段几乎是一样的。您可以使用'list'命令,如'list Class::method'。我将用一个简单的例子更新答案,但您应该阅读gdb文档或使用前端。
(gdb)start
Temporary breakpoint 1 at 0x407aa9: file ../../librarian/clibrarian/main.cpp, line 16.
Starting program: /home/pablo/projects/build-librarian-Desktop-Debug/clibrarian/clibrarian 
Missing separate debuginfos, use: zypper install glibc-debuginfo-2.32-1.1.x86_64
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Temporary breakpoint 1, main (argc=1, argv=0x7fffffffdbf8) at ../../librarian/clibrarian/main.cpp:16
16          qputenv("QT_FORCE_STDERR_LOGGING", QByteArray("1"));
Missing separate debuginfos, use: zypper install krb5-debuginfo-1.18.2-3.2.x86_64 libQt5Core5-debuginfo-5.15.1-2.1.x86_64 libQt5Gui5-debuginfo-5.15.1-2.1.x86_64 libQt5Network5-debuginfo-5.15.1-2.1.x86_64 libQt5Sql5-debuginfo-5.15.1-2.1.x86_64 libX11-6-debuginfo-1.6.12-1.1.x86_64 libXau6-debuginfo-1.0.9-1.7.x86_64 libbrotlicommon1-debuginfo-1.0.9-1.1.x86_64 libbrotlidec1-debuginfo-1.0.9-1.1.x86_64 libbz2-1-debuginfo-1.0.8-2.20.x86_64 libcom_err2-debuginfo-1.45.6-1.19.x86_64 libcurl4-debuginfo-7.72.0-1.2.x86_64 libdouble-conversion3-debuginfo-3.1.5-3.4.x86_64 libexiv2-27-debuginfo-0.27.3-2.1.x86_64 libexpat1-debuginfo-2.2.10-1.1.x86_64 libffi8-debuginfo-3.3.git30-1.13.x86_64 libfontconfig1-debuginfo-2.13.1-2.8.x86_64 libfreetype6-debuginfo-2.10.2-1.3.x86_64 libgcc_s1-debuginfo-10.2.1+git583-1.2.x86_64 libglib-2_0-0-debuginfo-2.64.6-1.1.x86_64 libglvnd-debuginfo-1.3.2-2.1.x86_64 libgmp10-debuginfo-6.2.0-3.3.x86_64 libgnutls30-debuginfo-3.6.15-1.1.x86_64 libgpg-error0-debuginfo-1.39-1.1.x86_64 libgraphite2-3-debuginfo-1.3.14-1.2.x86_64 libharfbuzz0-debuginfo-2.7.2-1.1.x86_64 libhogweed6-debuginfo-3.6-1.5.x86_64 libicu67-debuginfo-67.1-2.3.x86_64 libidn12-debuginfo-1.36-1.2.x86_64 libidn2-0-debuginfo-2.3.0-3.2.x86_64 libjpeg8-debuginfo-8.2.2-60.2.x86_64 libldap-2_4-2-debuginfo-2.4.53-57.2.x86_64 liblz4-1-debuginfo-1.9.2-2.1.x86_64 liblzma5-debuginfo-5.2.5-1.16.x86_64 libmodman1-debuginfo-2.0.1-18.10.x86_64 libnettle8-debuginfo-3.6-1.5.x86_64 libopenssl1_1-debuginfo-1.1.1g-2.13.x86_64 libp11-kit0-debuginfo-0.23.20-2.1.x86_64 libpcre2-16-0-debuginfo-10.35-1.4.x86_64 libpng16-16-debuginfo-1.6.37-1.7.x86_64 libpodofo0_9_6-debuginfo-0.9.6-4.8.x86_64 libproxy1-debuginfo-0.4.15-9.1.x86_64 libsasl2-3-debuginfo-2.1.27-3.5.x86_64 libssh4-debuginfo-0.9.5-1.1.x86_64 libstdc++6-debuginfo-10.2.1+git583-1.2.x86_64 libsystemd0-debuginfo-246.6-1.1.x86_64 libtag1-debuginfo-1.11.2~git20190725.79bc9ccf-2.3.x86_64 libtasn1-6-debuginfo-4.16.0-1.6.x86_64 libtiff5-debuginfo-4.1.0-2.4.x86_64 libunistring2-debuginfo-0.9.10-2.8.x86_64 libz1-debuginfo-1.2.11-16.1.x86_64 libzip5-debuginfo-1.7.3-1.2.x86_64 libzstd1-debuginfo-1.4.5-2.4.x86_64

(gdb) list AIcon::AIcon
file: "aicon.cpp", line number: 12, symbol: "AIcon::AIcon()"
7       #include <QTextStream>
8       extern QStringList AIcon_google_catnames;
9       extern QList<QList<GoogleIcon>* > AIcon_google_Cats;
10      static QStringList AIcon_texts=QStringList()<<QString();
11      static QStringList AIcon_files=QStringList()<<QString();
12      AIcon::AIcon()
13      {
14          m_itype=Icon_Null;
15          m_icon=0;
16      }
file: "aicon.cpp", line number: 17, symbol: "AIcon::AIcon(GoogleIcon)"
12      AIcon::AIcon()
13      {
14          m_itype=Icon_Null;
15          m_icon=0;
16      }
17      AIcon::AIcon(GoogleIcon i)
18      {
19          m_itype=Icon_Google;
20          m_icon=i;
21      }
file: "aicon.cpp", line number: 22, symbol: "AIcon::AIcon(QString const&, bool)"
17      AIcon::AIcon(GoogleIcon i)
18      {
19          m_itype=Icon_Google;
20          m_icon=i;
21      }
22      AIcon::AIcon(const QString &text,bool ti)
23      {
24          if (ti)
25              setTextIcon(text);
26          else
file: "aicon.cpp", line number: 29, symbol: "AIcon::AIcon(AI18n::CountryCode)"
24          if (ti)
25              setTextIcon(text);
26          else
27              setFileIcon(text);
28      }
29      AIcon::AIcon(AI18n::CountryCode cc)
30      {
31          setFlagIcon(cc);
32      }
33      AIcon::AIcon(AI18n::LanguageCode cc)
file: "aicon.cpp", line number: 33, symbol: "AIcon::AIcon(AI18n::LanguageCode)"
28      }
29      AIcon::AIcon(AI18n::CountryCode cc)
30      {
31          setFlagIcon(cc);
32      }
33      AIcon::AIcon(AI18n::LanguageCode cc)
34      {
35          setLanguageIcon(cc);
36      }
37

(gdb) break 35
Breakpoint 2 at 0x7ffff7f04651: file aicon.cpp, line 35.
(gdb) continue