1

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 ...:/)

yves Baumes
  • 8,836
  • 7
  • 45
  • 74
  • Are you writing your code in XCode? – Marc W May 14 '09 at 20:32
  • Your question does not make sense to me. "Of course" the main function doesn't exist? Why doesn't it exist? – Chuck May 14 '09 at 20:36
  • I update my question with more information – yves Baumes May 14 '09 at 20:43
  • 2
    The real question here isn't "how do I debug an objective-c program", it's "how do I debug a stripped binary I don't have source for?" The answer to that depends a lot on what you mean by "debug". What are you actually trying to accomplish? – smorgan May 14 '09 at 23:56
  • I was trying to answer this question: http://stackoverflow.com/questions/816302/unable-to-have-pbcopy-clipboard-inside-screen . As I tried a shot with gdb I was surprised to see that the gdb's start command didn't work, and I was wondering why, and how make it work with Obj-C program (assuming this tool is written in Obj-C). Google didn't help me, so I posted my question here. I had in mind that a Obj-C program keep its whole debug symbol table as it needs it at runtime - but I am newbie in Obj-C and I maybe wrong... – yves Baumes May 15 '09 at 01:03
  • About the real goal of my question, I was trying to see the parameters pass to the -[NSPasteboard addTypes:owner:] function when launched from a screen session. I didn't took the time for that then, as I find another answer for Masi's question. My own question was really about how to use gdb with Obj-C program. – yves Baumes May 15 '09 at 01:07
  • In most ways you use it the same as you would with any other program, with the addition of various extra things like the syntax for printing NSObject subclasses or breaking on Objective-C messages. Objective-C classes do have a fair amount of information available at runtime (although not full debug information by any means), but that doesn't apply to regular C functions. If you want information about specific techniques for debugging on OS X, see http://developer.apple.com/technotes/tn2004/tn2124.html – smorgan May 15 '09 at 02:56

2 Answers2

3

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
  • 20,228
  • 3
  • 47
  • 55
2

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
  • 5,306
  • 1
  • 29
  • 46
  • 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 May 14 '09 at 21:04