为什么GDB不能识别我的C程序的语言?

时间:2016-02-14 20:25:07

标签: c debugging gdb

我正在从一本书中的一个例子开始,作者写了一个简短的,有缺陷的C程序来演示使用GDB进行调试。

#include <stdio.h>

int main (void)
{
    const int data[5] = {1, 2, 3, 4, 5};
    int i, sum;

    for (i = 0; i >= 0; ++i)
        sum += data[i];

    printf ("sum = %i\n", sum);

    return 0;
}

使用gcc -g program.c进行编译后,他运行gdb ./a.out并从gdb中调用run。当它遇到程序中的缺陷时,他会打电话,

(gdb) print data
$1 = {1, 2, 3, 4, 5}

当我尝试相同的时候,我明白了,

(gdb) print data
$1 = 0x100000f80
Current language:  auto; currently minimal

我做了一些挖掘,这听起来像我只是被显示内存地址与“当前语言”下面的消息有关。

我编译并运行了上面的C程序,

❯ gcc -g program.c
❯ gdb ./a.out
Reading symbols for shared libraries .. done
(gdb) run
Starting program: /Users/ivan/Development/books/c/programming-in-c/a.out

GDB然后输出一系列关于不支持的“间接”引用的行,

Reading symbols for shared libraries +..............bfd_mach_o_scan_read_symtab_symbol: symbol "_bzero" is unsupported 'indirect' reference: setting to undefined
bfd_mach_o_scan_read_symtab_symbol: symbol "_memccpy" is unsupported 'indirect' reference: setting to undefined
bfd_mach_o_scan_read_symtab_symbol: symbol "_memchr" is unsupported 'indirect' reference: setting to undefined
bfd_mach_o_scan_read_symtab_symbol: symbol "_memcmp" is unsupported 'indirect' reference: setting to undefined
bfd_mach_o_scan_read_symtab_symbol: symbol "_memcpy" is unsupported 'indirect' reference: setting to undefined
bfd_mach_o_scan_read_symtab_symbol: symbol "_memmove" is unsupported 'indirect' reference: setting to undefined
bfd_mach_o_scan_read_symtab_symbol: symbol "_memset" is unsupported 'indirect' reference: setting to undefined
bfd_mach_o_scan_read_symtab_symbol: symbol "_strchr" is unsupported 'indirect' reference: setting to undefined
bfd_mach_o_scan_read_symtab_symbol: symbol "_strcmp" is unsupported 'indirect' reference: setting to undefined
bfd_mach_o_scan_read_symtab_symbol: symbol "_strncmp" is unsupported 'indirect' reference: setting to undefined

...然后是关于无法找到目标文件的警告(所有这些都与ssl / tls相关):

...warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Objects/coretls.build/coretls.build/Objects-normal/x86_64/system_coretls_vers.o" - no debug information available for "system_coretls_vers.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_ciphersuites.a(tls_ciphersuites.o)" - no debug information available for "tls_ciphersuites.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_handshake.a(sslSession.o)" - no debug information available for "sslSession.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_handshake.a(sslCipherSpecs.o)" - no debug information available for "sslCipherSpecs.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_handshake.a(sslDigests.o)" - no debug information available for "sslDigests.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_handshake.a(sslCrypto.o)" - no debug information available for "sslCrypto.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_handshake.a(sslChangeCipher.o)" - no debug information available for "sslChangeCipher.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_handshake.a(sslCert.o)" - no debug information available for "sslCert.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_handshake.a(sslKeyExchange.o)" - no debug information available for "sslKeyExchange.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_handshake.a(sslAlertMessage.o)" - no debug information available for "sslAlertMessage.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_handshake.a(sslHandshake.o)" - no debug information available for "sslHandshake.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_handshake.a(sslHandshakeFinish.o)" - no debug information available for "sslHandshakeFinish.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_handshake.a(tls1Callouts.o)" - no debug information available for "tls1Callouts.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_handshake.a(sslHandshakeHello.o)" - no debug information available for "sslHandshakeHello.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_handshake.a(tls_handshake.o)" - no debug information available for "tls_handshake.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_handshake.a(ssl3Callouts.o)" - no debug information available for "ssl3Callouts.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_record.a(tls1RecordCallouts.o)" - no debug information available for "tls1RecordCallouts.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_record.a(ssl3RecordCallouts.o)" - no debug information available for "ssl3RecordCallouts.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_record.a(tls_record.o)" - no debug information available for "tls_record.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_record.a(sslRc4Cipher.o)" - no debug information available for "sslRc4Cipher.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_record.a(sslMemory.o)" - no debug information available for "sslMemory.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_record.a(sslUtils.o)" - no debug information available for "sslUtils.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_record.a(symCipherParams.o)" - no debug information available for "symCipherParams.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_record.a(sslNullCipher.o)" - no debug information available for "sslNullCipher.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_record.a(symCipher.o)" - no debug information available for "symCipher.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_record.a(tls_digest.o)" - no debug information available for "tls_digest.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_record.a(tls_hashhmac.o)" - no debug information available for "tls_hashhmac.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_record.a(tls_hmac.o)" - no debug information available for "tls_hmac.c".
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_stream_parser.a(tls_stream_parser.o)" - no debug information available for "tls_stream_parser.c".
....................... done

...然后最终它停在该计划的预期(有缺陷)线上:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x0000000100006000
0x0000000100000f1e in main () at prog.18.4.c:9
9                       sum += data[i];

我在OS X Yosemite上,使用以下版本的gcc和gdb:

❯ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin14.5.0
Thread model: posix

❯ gdb --version
GNU gdb 6.3.50-20050815 (Apple version gdb-1824) (Wed Feb  6 22:51:23 UTC 2013)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".

我需要做些什么才能让GDB将其识别为C程序?

1 个答案:

答案 0 :(得分:1)

您必须使用-g选项编译和链接程序,以包含调试信息,如函数和变量名,源语言和位置......

相关问题