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

时间:2011-12-28 10:46:47

标签: c gdb static-libraries

通常在使用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

主文件a.c包含:

#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而不必重新启动它并松开我的断点。

2 个答案:

答案 0 :(得分:1)

这可能是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 gold(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
  % 

答案 1 :(得分:1)

  

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

这不太可能与GDBld(或其版本)相关,并且肯定与您使用归档库无关。

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