简体   繁体   English

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

[英]Can GDB reload executable with a statically linked library?

Normally when using gdb I can stop execution and rebuild the executable and restart without loosing my breakpoints.通常在使用 gdb 时,我可以停止执行并重建可执行文件并重新启动而不会丢失我的断点。 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: libtest.h:

int square(int a);

libtest.c: 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 ac contains:主文件 ac 包含:

#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.如果我将生成的文件 gdb_test 加载到 gdb 中,它是否正在运行它并不重要。 As long is gdb is open a subsequent build will fail during the link step:只要 gdb 处于打开状态,后续构建就会在链接步骤中失败:

/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.我希望能够使用 gdb,而不必重新启动它并丢失我的断点。

It is probably a matter of versions of GDB or GCC;这可能是 GDB 或 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.我的系统是 Debian/Sid/amd64。 GCC is (Debian 4.6.2-9); GCC 是 (Debian 4.6.2-9); ld = binutils = ar is GNU gold (GNU Binutils for Debian 2.22); ld = binutils = ar 是 GNU gold(Debian 2.22 的 GNU Binutils); GDB is GNU gdb (GDB) 7.3.50.20111117-cvs-debian; GDB 是 GNU gdb (GDB) 7.3.50.20111117-cvs-debian; Gnu Libc is (Debian EGLIBC 2.13-24). Gnu Libc 是 (Debian EGLIBC 2.13-24)。 Kernel is Linux version 3.1.0-1-amd64 (Debian 3.1.5-1)内核是 Linux 版本 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 ./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
  % 

/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.这不太可能与GDBld (或它们的版本)有关,并且肯定与您使用存档库无关。

Much more likely this is happening because you are using some "strange" filesystem.更可能发生这种情况是因为您使用了一些“奇怪的”文件系统。 Perhaps you are using NTFS or CIFS mount?也许您正在使用NTFSCIFS挂载? What does df . df .是什么? say?说?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM