代码之家  ›  专栏  ›  技术社区  ›  yves Baumes

如何用gdb调试objective-c程序(苹果提供的pbcopy)?

  •  1
  • yves Baumes  · 技术社区  · 17 年前

    我需要调试一个objective-c程序。 在main()函数上设置断点时,我得到:

    Reading symbols from /usr/bin/pbcopy...done.
    (gdb) break main
    Function "main" not defined.
    

    从reply调用“start”时会出现相同的错误。 我怀疑在max os x上做事情的方式是不同的?

    与objective c程序等效的是什么?

    编辑:我尝试按照Lyndsey的建议在-[NSApplication run]上突破

    (gdb) file pbcopy
    Reading symbols for shared libraries ........ done
    Reading symbols from /usr/bin/pbcopy...done.
    (gdb) break -[NSApplication run]
    Breakpoint 1 at 0x35a8356
    (gdb) run
    Starting program: /usr/bin/pbcopy 
    Reading symbols for shared libraries +++++++.................................................................... done
    Breakpoint 1 at 0x95a97356
    
    helo
    ^C
    Program received signal SIGINT, Interrupt.
    0x9574eeda in read$UNIX2003 ()
    (gdb) bt
    #0  0x9574eeda in read$UNIX2003 ()
    #1  0x94d4e5b6 in _NSReadFromFileDescriptor ()
    #2  0x94d4e4b6 in -[NSConcreteFileHandle readDataOfLength:] ()
    #3  0x94d7f2fa in -[NSConcreteFileHandle readDataToEndOfFile] ()
    #4  0x00002a11 in ?? ()
    #5  0x00002736 in ?? ()
    (gdb) info breakpoints
    Num Type           Disp Enb Address    What
    1   breakpoint     keep y   0x95a97356 <-[NSApplication run]+6>
    (gdb) 
    

    这很奇怪,gdb是如何找到符号的地址的,但没有破坏它。。。 看到stacktrace,我看到了很多??让我假设触发应用程序的通常方法在这里不使用(?大假设…:/)

    2 回复  |  直到 17 年前
        1
  •  3
  •   smorgan    17 年前

    pbcopy是一个命令行工具,而不是应用程序,因此它没有理由调用[NSApplication run]。断点解析是因为它(出于任何原因)链接到AppKit框架。

    因为这是一个精简的二进制文件,所以破坏C函数名不会很好地工作。如果你真的想四处闲逛,你最好的办法就是转储程序集,并根据它找出你想在哪里中断。

        2
  •  2
  •   Lyndsey Ferguson    17 年前

    您是否使用Cocoa项目模板创建了Objective-C应用程序?如果是,哪一个?许多具有主要功能。

    你能发布你的主要功能吗?在主函数中,键入:

    int main(int argc, char *argv[]) {

        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
        int retVal = UIApplicationMain(argc, argv, nil, nil);
        [pool release];
        return retVal;
    }
    

    现在,构建您的应用程序,是否出现错误?如果没有,那么由于某种原因,您的带有main函数的代码实际上没有被编译。确保它是正确的。

    在通知应用程序运行时中断。