【发布时间】:2011-06-27 07:29:47
【问题描述】:
我用这个代码用 gdb 测试了一些东西(这是一个错误的代码,我只是为了测试目的使用它):
#import <Foundation/Foundation.h>
int main (int argc, char **argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int i = 1;
NSLog(@"Hi GDB, i is %@", i);
[pool release];
return 0;
}
然后我编译它:gcc -Wall -g -framework Foundation testGdb.m -o testGdb,然后我运行它:
gdb ./a.out
GNU gdb 6.3.50-20050815 (Apple version gdb-1518) (Sat Feb 12 02:52:12 UTC 2011)
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"...Reading symbols for shared libraries ..... done
(gdb) run
Starting program: /Users/Tarek/Desktop/a.out
Reading symbols for shared libraries .++++....................... done
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x000000000000002a
0x00007fff82d7a0a3 in objc_msgSend_fixup ()
(gdb) bt
#0 0x00007fff82d7a0a3 in objc_msgSend_fixup ()
#1 0x0000000000000000 in ?? ()
(gdb)
奇怪的是 (#1) 中没有名字,正确的输出通常会打印 NSLog(崩溃的原因)和 main。
感谢您帮助理解这种奇怪的行为。
【问题讨论】:
-
您有调试符号可用于您的代码,但不适用于共享库。
-
为什么会这样?你能给我解释一下原因吗?谢谢。
-
@funnycoder 默认情况下,从所有共享库中删除调试符号以节省空间
strip(1)。如果有可能,请使用“-g”标志自己编译它们。要检查是否存在调试符号,请使用nm -a /path/to/library -
我不明白你的意思!!我说的是一个简单的程序(不是库),我只是与 Foundation Framawork 链接。当我运行 nm 命令时,所有符号都存在。
-
我无法准确解释原因,但这与编译器和运行时如何优化 x86_64 上的 Objective-C 2.0 中的某些方法调用有关 — 使用了一个内部 vtable,而不是传统的 Objective-C 消息调度机制。出于某种原因,方法修复代码会阻止调试器重建堆栈跟踪。如果你用
-arch i386编译你的代码,它不使用vtables,你将能够得到回溯。
标签: objective-c debugging macos gdb backtrace