IOS & Max_ OS _X Debugging Magic

Source: Internet
Author: User
Tags gdb debugger

IOS: http://developer.apple.com/library/ios/#technotes/tn2239/_index.html

Mac OS X: http://developer.apple.com/library/ios/#technotes/tn2004/tn2124.html

========================================================== ======================================

Debugging Objective-C with gdb

Introduction

When using Xcode on Mac OS X, the debugger underneath the covers is the GNU gdb debugger.

There are a number of extensions to gdb to support debugging Objective-C applications. Here are a few tips that may be helpful.

Setting breakpoints on Objective-C methods Normally you wocould set a breakpoint from within Xcode by clicking in the gutter to the left of the source code where you want debugger to stop.

If you are running gdb from Terminal you need to set breakpoints manually. For example if you wanted to set a breakpoint on the digitKeyClicked: method in the calculator you cocould do this:

(Gdb) break digitKeyClicked:

If gdb find multiple methods in different classes for the name you specified it will list the classes and ask you to select one or more where you want to set a breakpoint.

Alternatively, you can specify a full class and method name by doing things like:

(Gdb) break-[Controller digitKeyClicked:]

(Gdb) break + [SomeClass myFavoriteClassMethod]

You can also set breakpoints on objects that you don't have symbols for. For example, you cocould set breakpoints on NSArray methods by doing:

(Gdb) B-[NSArray objectAtIndex:]

(Gdb) B-[NSArray removeObject: atIndex:]

(Gdb) B removeObject: atIndex:

Note the use of a selector name that takes two arguments. When using selectors like this you write out the labels and colons all together with no spaces between them.

Printing Objective-C objects

From the gdb prompt you can print an Objective-C object in the same way you can print other C types by using the "print" command (or just "p" for short ). for example if you were at the gdb prompt when broken in an action method where the argument was called "sender", you cocould print the sender object by doing:

(Gdb) print sender

Which wowould return something like:

$1 = 0x134240

That's not very informative, is it? It's just printing the address in memory where the sender object resides. instead you often want to use the "print-object" (or "po" for short) command to print a description of an object, like:

(Gdb) print-object sender

Which wocould print something like:

<NSButton: 0x134240>

Indicating it's a button object.

Sending messages to Objective-C objects When you are sitting at the gdb prompt you frequently want to interact with an object dynamically by sending messages to it. for example, if you want to ask an object for its retain count you cocould do:

(Gdb) p (int) [sender retainCount]

Which wocould print the current retain count of the object. gdb frequently can't figure out what the return type of Objective-C methods are so you usually have to make an explicit cast to a type. if you leave the cast off and gdb can't figure out what the type is it will spew a message saying that it doesn't know the type and you'll have to explicitly cast return value. when in doubt about the type, you can always cast objects to type "(id )".

You can also make nested method cballs like we did in class by doing:

(Gdb) po [[sender selectedCell] title]

Where the sender was the NSMatrix which responds to the selectedCell method. the selected cell was a button cell which responds to the title method. the "po" (or print-object) command will print the result which wocould be the title of the selected or clicked button in the matrix.

Gdb knows about "self" automatically Whenever gdb is in the middle of an Objective-C method it automatically knows about "self" and you can refer to instance variables directly. for example in Tuesday's lecture we created a Controller object that had an instance variable named "_ outputField ". at any point in the middle of one of the Controller classes instance methods we cocould interact with the text field pointed to by the _ outputField instance variable. as an example, we cocould print the current contents of the text field by doing:

(Gdb) po [_ outputField stringValue]

We cocould even set the string value dynamically in gdb by doing something like:

(Gdb) p [_ outputField setStringValue: @ "123456"]

Note the use of a string constant with the "@" prefix which gdb will dynamically turn into an NSString object.

Command history and emacs key bindings gdb keeps a list of commands that you 've issued. you can use the up/down arrow keys to navigate the history of commands. additionally you can use the left/right arrow keys to move the cursor back to edit a command. finally, gdb supports most of the basic emacs key bindings for text editing. if you're familiar with emacs then this makes editing commands easier.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.