views:

740

answers:

2

Hi everyone, I need to debug an objective-c program. When setting a breakpoint on main() function , I've got:

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

Invoking "start" from reply the same error. I suspect the way of doing things is different on max os x ?

What is the equivalent with objective c program?

BTW how to break on exit() function?

Edit: I try breaking on -[NSApplication run] as proposed by Lyndsey

(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)

That's quite weird , how does gdb find the address of the symbol but is not breaking at it... BTW it seems GDB change the address of breakpoint after loading the dynamic library. But it doesn't hurt me, as I suppose that ld needs to do relocating. Seeing the stacktrace, I see many ?? which let me suppose the usual way of triggering application is not use here (?? big supposition ...:/)

+1  A: 

Did you create your Objective-C application using a Cocoa Project Template? If so, which one? Many have the main function.

Can you post your main function? In your main function, type:

int main(int argc, char *argv[]) {
          #error Yes, I'm being compiled!

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

Now, build your application, did you get an error? If not, then for some reason your code with the main function is not actually being compiled. Make sure that it is.

Edit May 14, 2009 @ 5:22 EST: You may want to use b -[NSApplication run] to break when the application is being told to run.

Lyndsey Ferguson
Actually it is not a project of mine . I try to launch gdb on "pbcopy" which is a tool provided by Apple in Mac OS X 10.5. I know it is implemented as an objective c program looking at debug symbol with nm. But I can't found something related to the start of the program .. :/
yves Baumes
+2  A: 

pbcopy is a command-line tool, not an application, so there's no reason that it would ever call [NSApplication run]. The breakpoint resolves because it (for whatever reason) links to the AppKit framework.

Since this is a stripped binary, breaking on C function names isn't going to work very well. If you really want to poke around your best bet is to dump the assembly and figure out where you want to break based on that.

If you explain what you are trying to accomplish overall, you might get better suggestions on how to proceed.

smorgan