GDB 可以使用静态链接库重新加载可执行文件吗?

发布于 2024-12-23 04:24:19 字数 879 浏览 0 评论 0原文

通常,当使用 gdb 时,我可以停止执行并重建可执行文件并重新启动,而不会丢失断点。当我使用具有静态链接库的可执行文件尝试此操作时,我收到一条错误,指出我无法在构建过程中打开可执行文件。

一个具体的例子:

库文件:

libtest.h:

int square(int a);

libtest.c:

int square(int a) {
  return a * a;
}

库是用以下内容编译的:

gcc -g -c libfile.c
ar rcs libtest.a libfile.o

主文件 ac 包含:

#include <stdio.h>
#include <stdlib.h>
#include "libfile.h"

int main() {
  printf( "2 squared is %d\n", square(2) );
  return 0;
}

项目是这样编译和链接的:

gcc -g -c a.c
gcc a.o -g --static -L. -ltest -o gdb_test

如果我将生成的文件 gdb_test 加载到 gdb 中,那没关系如果它正在运行,则不会。只要 gdb 打开,后续构建就会在链接步骤中失败:

/usr/bin/ld: cannot open output file gdb_test: Permission denied

有解决办法吗?我希望能够使用 gdb,而不必重新启动它并丢失我的断点。

Normally when using gdb I can stop execution and rebuild the executable and restart without loosing my breakpoints. When I try this with an executable that has a statically linked library I get an error stating that I cannot open the executable file during the build.

A concrete example:

The library files:

libtest.h:

int square(int a);

libtest.c:

int square(int a) {
  return a * a;
}

The library is compiled with:

gcc -g -c libfile.c
ar rcs libtest.a libfile.o

Main file a.c contains:

#include <stdio.h>
#include <stdlib.h>
#include "libfile.h"

int main() {
  printf( "2 squared is %d\n", square(2) );
  return 0;
}

The project is compiled and linked like this:

gcc -g -c a.c
gcc a.o -g --static -L. -ltest -o gdb_test

If I load the resulting file gdb_test into gdb it doesn't matter if it is running it not. As long is gdb is open a subsequent build will fail during the link step:

/usr/bin/ld: cannot open output file gdb_test: Permission denied

Is there a way around this? I would like to be able to work with gdb without having to restart it and loose my breakpoints.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌︼了一个春 2024-12-30 04:24:20

这可能是 GDB 或 GCC 版本的问题;它对我有用:

   % gcc -g -c libtest.c
  gcc -g -c libtest.c
   % ar rcs libtest.a libtest.o
  ar rcs libtest.a libtest.o
   % gcc -g -c -Wall a.c
  gcc -g -c -Wall a.c
   % gcc -Wall -g a.o -L. -ltest -o gdb_test
  gcc -Wall -g a.o -L. -ltest -o gdb_test
   % ./gdb_test
  2 squared is 4
   % gdb ./gdb_test
  GNU gdb (GDB) 7.3.50.20111117-cvs-debian
  Copyright (C) 2011 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/basile/tmp/gdb_test...done.
  (gdb) r
  r
  Starting program: /home/basile/tmp/gdb_test 
  2 squared is 4
  [Inferior 1 (process 12271) exited normally]
  (gdb) quit
  quit
   % gcc -v
  gcc -v
  Using built-in specs.
  COLLECT_GCC=/usr/bin/gcc-4.6.real
  COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
  Target: x86_64-linux-gnu
  Configured with: ../src/configure -v --with-pkgversion='Debian 4.6.2-9' 
  --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs 
  --enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr 
  --program-suffix=-4.6 --enable-shared --enable-linker-build-id 
  --with-system-zlib --libexecdir=/usr/lib --without-included-gettext 
  --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 
  --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug 
  --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc 
  --with-arch-32=i586
  --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu 
  --host=x86_64-linux-gnu --target=x86_64-linux-gnu
  Thread model: posix
  gcc version 4.6.2 (Debian 4.6.2-9) 

我的系统是 Debian/Sid/amd64。 GCC 是(Debian 4.6.2-9); ld = binutils = ar 是 GNU 黄金(Debian 2.22 的 GNU Binutils); GDB 是 GNU gdb (GDB) 7.3.50.20111117-cvs-debian; Gnu Libc 是(Debian EGLIBC 2.13-24)。内核是 Linux 版本 3.1.0-1-amd64 (Debian 3.1.5-1)

我可以从 gdb 内部重新编译该程序并运行它:

  % gdb ./gdb_test 
 gdb ./gdb_test 
 GNU gdb (GDB) 7.3.50.20111117-cvs-debian
 Copyright (C) 2011 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/basile/tmp/gdb_test...done.
 (gdb) shell  gcc -g -c -Wall a.c
 shell  gcc -g -c -Wall a.c
 (gdb) r
 r
 Starting program: /home/basile/tmp/gdb_test 
 2 squared is 4
 [Inferior 1 (process 12335) exited normally]
 (gdb) shell gcc -Wall -g a.o -L. -ltest -o gdb_test

 shell gcc -Wall -g a.o -L. -ltest -o gdb_test
 (gdb) 
 (gdb) r
 r
 `/home/basile/tmp/gdb_test' has changed; re-reading symbols.
 Starting program: /home/basile/tmp/gdb_test 
 2 squared is 4
 [Inferior 1 (process 12346) exited normally]
 (gdb) quit
 quit
  % 

It is probably a matter of versions of GDB or GCC; it works for me:

   % gcc -g -c libtest.c
  gcc -g -c libtest.c
   % ar rcs libtest.a libtest.o
  ar rcs libtest.a libtest.o
   % gcc -g -c -Wall a.c
  gcc -g -c -Wall a.c
   % gcc -Wall -g a.o -L. -ltest -o gdb_test
  gcc -Wall -g a.o -L. -ltest -o gdb_test
   % ./gdb_test
  2 squared is 4
   % gdb ./gdb_test
  GNU gdb (GDB) 7.3.50.20111117-cvs-debian
  Copyright (C) 2011 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/basile/tmp/gdb_test...done.
  (gdb) r
  r
  Starting program: /home/basile/tmp/gdb_test 
  2 squared is 4
  [Inferior 1 (process 12271) exited normally]
  (gdb) quit
  quit
   % gcc -v
  gcc -v
  Using built-in specs.
  COLLECT_GCC=/usr/bin/gcc-4.6.real
  COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
  Target: x86_64-linux-gnu
  Configured with: ../src/configure -v --with-pkgversion='Debian 4.6.2-9' 
  --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs 
  --enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr 
  --program-suffix=-4.6 --enable-shared --enable-linker-build-id 
  --with-system-zlib --libexecdir=/usr/lib --without-included-gettext 
  --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 
  --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug 
  --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc 
  --with-arch-32=i586
  --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu 
  --host=x86_64-linux-gnu --target=x86_64-linux-gnu
  Thread model: posix
  gcc version 4.6.2 (Debian 4.6.2-9) 

My system is Debian/Sid/amd64. GCC is (Debian 4.6.2-9); ld = binutils = ar is GNU gold (GNU Binutils for Debian 2.22); GDB is GNU gdb (GDB) 7.3.50.20111117-cvs-debian; Gnu Libc is (Debian EGLIBC 2.13-24). Kernel is Linux version 3.1.0-1-amd64 (Debian 3.1.5-1)

And I am able to recompile the program from inside gdb and to run it:

  % gdb ./gdb_test 
 gdb ./gdb_test 
 GNU gdb (GDB) 7.3.50.20111117-cvs-debian
 Copyright (C) 2011 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/basile/tmp/gdb_test...done.
 (gdb) shell  gcc -g -c -Wall a.c
 shell  gcc -g -c -Wall a.c
 (gdb) r
 r
 Starting program: /home/basile/tmp/gdb_test 
 2 squared is 4
 [Inferior 1 (process 12335) exited normally]
 (gdb) shell gcc -Wall -g a.o -L. -ltest -o gdb_test

 shell gcc -Wall -g a.o -L. -ltest -o gdb_test
 (gdb) 
 (gdb) r
 r
 `/home/basile/tmp/gdb_test' has changed; re-reading symbols.
 Starting program: /home/basile/tmp/gdb_test 
 2 squared is 4
 [Inferior 1 (process 12346) exited normally]
 (gdb) quit
 quit
  % 
壹場煙雨 2024-12-30 04:24:20

/usr/bin/ld:无法打开输出文件 gdb_test:权限被拒绝

这不太可能与 GDBld (或它们的版本),并且肯定与您对存档库的使用无关。

发生这种情况的可能性更大,因为您正在使用一些“奇怪的”文件系统。也许您正在使用 NTFSCIFS 挂载? df . 说什么?

/usr/bin/ld: cannot open output file gdb_test: Permission denied

This is unlikely to be related to either GDB or ld (or their versions), and is most certainly not related to your use of archive library.

Much more likely this is happening because you are using some "strange" filesystem. Perhaps you are using NTFS or CIFS mount? What does df . say?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文