iOS & Max_OS_X Debugging Magic

來源:互聯網
上載者:User

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 would set a breakpoint from within Xcode by clicking in the gutter to the left of the source code where you want the 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 could 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 could 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 could print the sender object by doing:

(gdb) print sender

which would 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 would 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 could do:

(gdb) p (int)[sender retainCount]

which would 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 the return value. When in doubt about the type, you can always cast objects to type “(id)”.

You can also make nested method calls 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 would 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 could interact with the text field pointed to by the _outputField instance variable. As an example, we could print the current contents of the text field by doing:

(gdb) po [_outputField stringValue]

We could 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.

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.