��������������������������� ���������������������������

��������

����������������������������������������������������������������������������������� ���������������������������������������������������������������������������������������������� ������������������������������������������������������������������������������������������� �������������������������������

��������������������������������������������������������������������������������������� �����������������������������������������������������������������������

�������������������������������������������������������������������������������������� ��������������������������������������������

& watchpoints

's graphical

◼ LLDB

��������������

“ ������������������������������������������������������������������������������������ ��������������������������� ”������������������

���������������������������� ◼ Assertions, Logging, and Debug configuration

◼ Static analysis & runtime memory tools

◼ Unit testing in xcode ���������������� ◼ Emphasis on LLDB as our debugging foundation

◼ Tips and tricks to stramline your the debuggin experience

◼ Use LLDB as an extensible investigative tool

“�������������”������������

“��’���������������������������������������������������’���������������������’�������������� ��������’����������������������������������� ”������������������

��������������������������������������������������� ���������������������������������������������������������������������������������� ����������������������������������������������������������������������������

◼ The term debugging is widely attributed to U.S. Navy Admiral Grace Hopper, often called Amazing Grace.

������������������������������������������������������������������������������������� ����������������������������������������������������������������������������������� ����������������������������������������������������������������������������������� ��������������������������������������������������������������������������������������

��������������������������������������������������������������������������������� �������������������������������������������������������������������������������������� �������������������������������������������������������������

�����������

“������������������������������������������������������������������������������������������� ����������������”������������������

◼ Xcode provides two ; the legacy GDB, part of GNU GCC project, and the newer LLDB, part of the LLVM project.

�����������������������������������������������������������’�������������������������������� ������������������������������������������������������������������������������������������ �������������������������������������������������������������������������������������������� �����������������������

������������������������������������� ����������������������������������’���������������������������������������������������������� ���������������������������������������������������������������������������������������������� ��������������������������������������� ��������������������������“�”����������

������������������������ ����������������������������������������������� ����������������������������������������������� ���������������������������������� �������������������������������������������� ����������������������������������������������� ����������������������������������������������� �������������������������‘��������������’����‘������’��������������� ����������������������������������

�����

������������������������������������������������������������������������������������������ ������������������������������

���������������������������������������������������������������

����������������������

��������������������������������������������������������

������������������������������

◼ Note the combined use of a set of nested method calls with a casted print statement, this yields a pretty-formatted rendition of the rect, making it easier to debug.

����������������� ◼ To add or remove a : "Command + Backslash" (⌘\) ◼ To stop: “Command + Period” (⌘.) ◼ To pause/continue: “Command + Right” (⌘→) ◼ Continue to current line: “Command + Control + ” (⌘c)

◼ Risky bidness: try dragging instruction pointer!

◼ Set your own hotkeys... here are some more I find useful:

+ ����� ������������������������ +++� �����������/������� + �������������������� ++� ���������������� +� ���������������� +� ������������� ++] ������������ ++� ������������������� +� �������� +� ����������������� ++� ������������ ++� ����������������  +→← �������������������������� + ���������������������������������� +� ����������� +� ������������ +�/�/�/�/� �������������������� +� ������������� + ���� ���������������������������

����������������������� ����������������������������������������������������������������������������������� �������������’�������������������������������������������������������������������������� �����������������������������

������������������������ ������� ���������������������������������������������������������������������������������������� “�������”���������������������������

◼ There are built-in lldb formatters for system libraries, STL, core-foundation

◼ There system is plug and play-able (more on this later...)

������������������������������������������������������������������������������������� �������������������������������������������������

��������������� ��������������������������������������������������������������������������������������������� “������������”�

�������������� ����������������������

������������������������������������������������������������

◼ Note “Step Over” maintains a stack, so when you step over a function with a breakpoint inside it, you can click continue to jump out. ����������������������� ����������������“���������������”����������������������������������������������� ������������������������������������������������������������������������������������� �������������������������������������������������������������������������������������� ��������������������������������������������������������������������������������������� �����������������������������������������������������

����������������������������������������������������������������������������������������� �������������������������������������������������������������������������������

Gotcha: On using stringWithFormat ���������������������������������‘�������������������������������������’�������������� ���������������������������������������������������������������

����������������������������������������������������������������

�����’�������������������������������������������������������������������������������� ������������������������������������������

������������������������������������������������������������ ����������������������������������

������������������������������������������������������������������’���������������������� �����������������

��������������������������������������������������������������������������������� �������������������

��������������������������������������������������������������������

�������→������������������

Conditional Log ������“���”��������������������������������

�������������������������������������������

Set Breakpoints in Runtime ������“���”��������������������������������

������������������������������������������������������

��������������������� ������������������������������������������������������������������������������������ �����������������������������’��������������������������������������������������������������� ������������������������������������������������������������������������������������������ �����������������������������������������������������������������������������

����������������������������������������������������������������������������������������� �������������������������������������������������������������������������������������������� ������������������������������������������������������������������������������������� ���������������������������������������’��������������������������������������������� ������������������������

◼ used to break each time a new exception is raised. Common use, to catch uncaught exception:

** * ����������� ��� ��� �� �������� ���������’ ����������������’� ������ �’-[ __��������� ������������� �] � �����(��) ������ ������(�)

��������������������� ���������������������������������������’������������������������������������������ ������������������������������������������������������������������������������� ���������������

◼ Symbolic breakpoints are useful when you want to stop in a piece of code that you don’t have source code for.

Type 1: (class or instance) Methods with arguments

◼ Used to break on every call to a defined method:

���������������������������������������������������������������������������������� ������������������������������������������������������������������������������������������ ������ �������������������������������������������������������������������������������� ������������� �������������“��������������������”������������������������������������� �������������������������������������������������������������������������������������� ������������������������������������“�������������������������������������”������������ �������������������������������������������������������������������������������������������

-[���������������� �����������] -[__��������� ������������� �]

���������������������������������������������������������������������������������������� �������������������������������������������������������� ���������������������������������

����������������������������������������������������������������������������������������� ���������������’����������

�������������������� ������������������ �������������������������������������

◼ Adding conditions are useful here, e.g. [NSThread isMainThread]

◼ The debugger will now break whenever any piece of code in your app sends this very message – no matter if it' s your code or code from a third party. Imagine trying to debug a third-party library for which you only have a binary, or something in the iOS frameworks. Without access to the code, you can’t set a breakpoint based on file names or line numbers, because you don’t know them.

◼ Spaceing matters! Seems that for Symbolic breakpoint, “- [UIViewController viewDidLoad]”. should be “-[UIViewController viewDidLoad]”, no blank between “-“ and “[“ or it won’t break.

��������������������������������������������������������������������������������������� �������������������������������������������������������������������������������������������

Class properties ������������������������������������������������’������������������������������������������� �������������������

�����������������������������������������������������

◼ Use the home button to break, by putting a breakpoint on the symbol:

���������������������������

Advanced Symbols ���������������������������������������������������������������������������������������� ����������������������������������������������������������������������

������������������� ������������������������������������������������������������������������������������� ��������������������������������������������������������������������������������������� ��������������������������������������������������������������������������������

1. In the breakpoint navigator, Control-click the breakpoint. 2. From the shortcut menu, choose Share Breakpoint.

������������������������������������������������������������������������������ ��������������������������

◼ After you share a breakpoint, it appears under a shared breakpoints group in the breakpoint navigator.

������������������������������������������������������������������������������������� �������

����

“�����������������������������������������������”���������������

��������������������� ◼ LLVM (formerly the Low Level Virtual Machine) is a infrastructure written in C++ ◼ Designed for compile-time, link-time, run-time, and "idle-time" optimization of programs written in arbitrary programming languages. ◼ Was originally implemented for C and C++, the language-agnostic design (and the success) of LLVM has since spawned a wide variety of front ends...

◼ LLDB (Apple's Lower Level DeBugger) is a ground up replacement for gdb in XCode 5 ◼ Apparently offers ton’s of improvements (like the expression parser) and requires fewer explicit casting (is that a sick joke?)

������������������������������������������������������������������������������������ �������������������������������������������������������������������’������������������� ��������������������������������� ����������������������������������������������������������������������������������������� �������������

������������������������������������������������������������������������������������� ������������������������������

���������������������������������� ���������������������������������������������� �������������������������������������������������������� ������������������������������������������ ���������������������������������������� ���������������������������������������������������� …

�������������������������������������������������������������������������“������������”�� ������������������������������

����’�����������������������������������������������������������������������

��������������� ����������������������������������������������������������������������������������� ��������

◼ At a source Line

�������������������������������������������������������������������������������������� ������

���������������������������������������������

�����������������

◼ At a method

�������������������������������������������������������������������������������������� ��������������������������������������������������������������������������������� �����������������������������������������������

��������������������������������������������������

����������������������������������������������

◼ Whenever any object recieve a selector

������������������������������������������

������������������������������������������������������������������������������

������������������������������������������������

������������������

��������������������� �����������������������

�����������

�����������������������������

��������������� �������������������� ��������������� ����������������������������������

�����������������������������

�����������������

����������������������������������

���������������������� ����������������������� �������������������������������� ������������������������

Command abbreviations save time ��������������������������������������������������������

�������������������

�������������������������

�������������������������� �������

◼ Breakpoint commands run each time a breakpoint is hit

�������������� �������� ���� ��� ������

�����������������������������������������������������������������������������������������

��������������������������� ����������������������������� ���������������������������������

◼ Note: here you have to use the $ sign to make the variable persist �������������������� ���������������’����������������������������������������������������������

◼ All is not lost we can do some sluthing with lldb

��������������������������������������������������������������������������������������� �������������

������������������� �������������������������������������������

����������������������������������������������������������������������������������������� ����

��������� ��������������������������������������������������������������������������������� ���������������������������� ������������������������������������������������������������������ ������������������������������������������������������������ ����������������������������������������������������� ����������������������������������������������������������� ��������������������������������������������������������������������� �������������������������������������������������������������� ��������������������������������������������������������������������������� �������������������������������������������������������������� ����������������������������������������������������������� �������������������������������������������������������������������� ������������������������������������������������������������������� ����������������������������������������������������������������� ���������������������������������������������������������������� �������������������������������������������������������� �����������������������������������������������������

�������������������������������������������������������������������������������� ���� ��������������������������������������������������������������������������� ������������ ���������������������������������������������������������������������� ���������������� �����������

◼ Combine this with “Automatically Continue” and some particularly hot or confusing code path, and you can then see console output of where your code was hit, interspersed with any other output you may have, for example from NSLog.

������������������������’��������������������������������������������������������������� �������������������������������������������������

������������’�������������“����”�������������������������������������������������������� ����������������������������������������������������������������������������������������� ��������������������������������������������������������������������“�����������”� ��������������������������������������������

��������������������������� ��������������������������

����������������������������������������������������������������������

◼ ���� ��� ���*** ����� ����� ���� �����

������������������������������������������������������������������������������

������������������������

����������������������������������������������������

�����������������������������������������������������������������������

�������������������� ����������������������������� �������������������������� �������������������������� �������������������������� ��������������������������

����������������������������

����������������������������������������������������������������������������������� ��������������������� �����������������������������������������������������������������������

���������������������������������������������������������������������

����������������

����������������������������������� ���������������������������������������

������������������������������������������������������������������������������������������� ����������������������������������

����������������������������������� ���������������������������������������

����������� ������������������������������������������������������������������������������� �������� ��������������������������������������������������������������������

��������������������������������������������������������� ������������������������������� �������������������������������������������

����������������������������������������������������������������������������� �������� �������������������������������������������������������������������� �������������������� �����������������������������

��������������������������������������������� ������������������������������������������ �������������������������������� ����������������������������������������������������������� ���������������� ����������������������������������������������������������������������� ���� ����������������������������������’����������������������������������� ����� ����������������������������������

◼ To set the watchpoint, you need to be paused in the debugger within a stack frame that has the variable you want to watch in scope.

�������������������������� ���������������������������������������������������������� ������������� ��������������������������������������������������������������������������� ��� �����������������������������������������������������������������������

◼ To use the GUI, navigate to the Variables View, expose the variable you want to watch, and right- click (control-click) on the name of it. In the menu that appears, select “Watch _button1ClickCount”.

������������������������������������������������������������������������������

�������������������������������������������� ������������������������������������������������������������������������������������� ������������������������������������������������������������ ������������������������������������������ �����������������

�����������������������

����������������������������

����������������� �������������������������������������������

������������������� ��������������������

��������������������������������������

��������������������������������

◼ By default, a watchpoint watches for “write” occurrences on the variable you specify. This means that when the variable’s value is set, the watchpoint will hit. ◼ Watchpoints are not saved between executions of your program, so if you need to set a watchpoint repeatedly you should save the command for setting it in another file and paste it into the debugger console when appropriate.

◼ Try this: set a breakpoint that hits early in the program’s execution where the variable you want to watch is in scope, and make the action of that breakpoint an LLDB command to set the watchpoint.

���������������������������������������

◼ 4 on intel mac, 2 on arm ��������� ���������������������������������������������������������������������������������� ������������������������������������������������������������������������������������� ������������

��������������������

��������������������������������������

��������������������������������������

�������������������������� � ������������������������������������������������������� �

����������������������������������������������������������������������������������“��� �������������������������������������”����������������������������������������������� ��������������������������������������������������������������������������������������� ������������

◼ You can basically return anything that has visual representation (Strings, Attributed strings, images, bezier paths, views, urls, etc…) and see them while debugging.

����������������������������������������������������������������������������������

1. Run as little code as possible (creating a caching mechanism is recommended)

2. Avoid changing your application's internal state

������������������������� ��������������������������������������������������������� ���������������������������������������������������������������������������������������������� �����������������������������������������������������

������������������������������������������������������������������������������������� ����������������������������

����������������������� ������������������������������������������������������������������������������������������ ��������������������������������������������������������

◼ All the features of the debugger are available as classes in the Python interpreter.

◼ Python functions can be used to create new LLDB command interpreter commands, which will work like all the natively defined lldb commands. ◼ You start the Python shell from the LLDB prompt by typing script. The prompt changes from (lldb) to >>>. ◼ Though powerful, the python scripting bridge is rather poorly documented

Invoking the Python Script ���’����������������������������������������������

��������������������������������������������������������������������������������������� �������������������������������������������������������������������������������������������� ������������������������������������������������������������������������������������������� ������������������������������������������������������������������������������

◼ Within the script editor, you can access the LLDB frame using the lldb.frame Python variable.

������������������������������������������������������������������������

������������������������“�”�

����������������������������������������������������������������������������������������� ������������������������������������������������������������������������

����������������������������� �������������������������������������������������� ������������������������������������������������������������������������� ��������������������

�������������������������������������������������������������������������������� �������������������������������������������

��������������������������������������������������������������� ���������������������������������������������������������������������������������� �������������������������������

�������������������������������������

Automate the import ������������������������������������������������������������’������������������������������� ������������������������“���������”��������������������������������������������������

����������������������������������������

�������������������������������������

◼ Remember to do an “import lldb” at the top of your script!

Example 1 - say �������������������������������������������������������������������������������������������� ������������������������������������������������������������������������������������ ����������������������������������������������������������������������������������� �������������������������������

�����������������“����“��� �����������������������������������“����������”��������������

Example 2 - caps ������������������������������������������������������������������������������������ ������������������������������������������������������������������������������������� ��������������������������������������

������������������������������������������

��������������������������������� ��������� ��������������������������� � ����������������������������������������������� �

���������� � ����������������������������������� ���������������������������������������������� ��������� �

�������������������������������������������������������������������������������

����’���������������������������

������������� ��������������������������������������������������������������������������� �������������������������������������������������������������� ��� ������������������������������������������������������������ ��� �������������������������������� ���� ������

���������������������������������������������� �������������������� ����������������������������������������� �������������������������� ����������������������������������������������

��������������������������������������������������������������

�����������������������������������

Example 3 - ql ���’��������������������������������������������������������������������������� ����������������������������������������������������

������������������������������������������������������������������� ����������������������������������������������������������������������������������

���’�����������������������������������������������������������������������������������

◼ Debugging images not accessible from the Xcode GUI.

◼ Debugging views that are not on screen or obscured by another view.

◼ Saving images for screenshots or to watch a view change over time.

◼ Opening text in a proper editor without having to copy/paste from the console.

��������������

1. Create a .lldbinit file to your home directory (or append the lines to your existing .lldbinit). 2. Update the path to lldb_quick_look.py in the .lldbinit file to match the script's location on your machine.

�����������������������

������������������������������������� ������������������������������������������������������� ��������������������������������������������������������� ����������������������

���������������

“������������������������������������������������������������������������������������������ ������’�������������������������������������������������������������������������”�����������

����� �������������������������

����������������������������������������������������������������������������������

����������������������������������������������

�������������������� ��������������������������������������������������������������������������������������������� ��������������������������������������������������������������������������������������������� �����������������������������

������������������������������������������� ������������������������������������������������

◼ The “-i false” flag about means don’t ignore breakpoints, which lldb does by default. ������������������� ����������������������������������

���������������������

��������������������������

������������������� ������������������������������������������������������������������������������������������ ���������������������������������������������������������������

����������������������������������������������������������

������������������������������������������������������������������ �������������������������������������������������������������������� ������������������������������������������������������������������������������ ������������������������������������������������������������������������������������� ��������������������������������������������������������������������

������������������������������ �������������������������������������������

������������������������������������������������������������������� ����������������������������������������������������������������������������������

������������ ������������������������������������������

�������������������������������� ���������������������������� ������������������� ������������������������ ������������������������������� ����������������������������������������������� ������������������������������������������������������������ ����

◼ Please add more! ���������������������������� “����’������������������”������������������������������������������������������ �����������������������������’���������������������������������������������������� �������������������������������������������������������������������������������������� ����������������������������������

◼ The commands must be in a /** block */, /*! block */, or prefixed with ///.

◼ The commands work with the @ (headerdoc style) or the \ ( style) prefix. (I.e. @b and \b both do the same thing.) ◼ The commands must come before the item they are describing. (I.e. if you are trying to document a property, the comment must come before the @property text.)

�������������������������������������������

�� ���������������������

�������������� ����� ������������� ��������

��������������������������������

������������������������ ������������������������

���������������������������������� ��

◼ You can add documentation to classes, functions, properties, and variables.

◼ All of these commands appear in dark green text to signify that they are valid commands, except for @returns. ◼ You may need to build your project (or restart Xcode) before the latest changes to your documentation appear. ◼ Your inline apple documentation will also display in the help inspector and autocomplete.

����’�����������������

��������������������

����������������������������������������������������������������������������������� ����������������������������������������������������������������

�������������������������������������

�����������������������

����������������������������������������

������������������������������������������

��������������������������������������

���������������� ������������������ �������������������������� �������������������� �������������� �������������������������� ���������������� �������������������������� ���������������� ���������������� �������������� ���������������������� ������������ �������������� ������������������ ���������������� ���������������������� ����������������������

�������������������� �������������������� ����������������������

������ ������������ ����������������������� �������������������� ������������������ ��������� ����������������

������������������������ ��������������������������� ��� �������������������

◼ My personal favorite is how easy it is to add documentation to a property

������������������������ �������������������������������������������

������’�������������������������������������������������������������������������� ���������������’����������������������������������������������������������������������� ���������������������������������������������������������������������

Warnings when the documentation types don' t match the signature! ��������������������������������������������������������������������������������������� ���������������������������������������������������������������������������’��������� �������������������������������������������������������������

◼ These warnings are off by default, so pass -Wdocumentation flag (in custom compiler warning flags) to enable warnings on documentation comments.

����������������������������������������������������������������������

��������������� ����������������������������������������������������������

������������������������������������������������������������

◼ My workflow is: click project in navigator → hotkey to show in finder → hot key to alfred or DTerm ���������������������������� ���’�����������������������������������������

◼ numberWithXXX be gone!

����������������� ����������’�’�������������� ������������������������� ���������������������������������� ������������������������������� �������������������������� ������������������������ ���������������������������

������������������

������������������ �������������������������������������������������������������� ������������������������������������������������ ��������������������������������� ���������������”��������”�

◼ the literal syntax creates immutable arrays by default.

������������������������������������������������������������������������������������� �����������������������������������

������������������������������������������������������������������������������� �������������

��������������������

���������������������������������� ��������������� ��������������� ����������� ������������������������������� ����������������������������������

��������� ������������������������

����������������������������� ��������������������������������������

����������������� ���������������������������������������������������������������������������������������� ��������������������������������������������������������������������� ������������

��������������������������� ����������������������������

��������������������������������������������������������������������������������������������� ������������������������������������������������������������������������������������������ �������������� ��������������������������������������������������������������������� ������������������������������������������������������������������������������������ �������

������������������������������������� ��������������������������������������������������������������������������������������� ����������������������������������������

◼ Show only variables view on break

◼ Show Build tab when there is new issue

◼ Switch to assistant view on a step into

����������

“ ������������������������������������������������������������������������������ ”���������� �����

����������������������������������������������������������������������������������� �������������’�������������������������������������������������������������������������� ��������������������������������������������������������������������������������������������� ������������������������������������������������������������������������������������������ ���������������������������������������������������������������������������������������� ��������������������������������������������������������������������������������������� ������������

������� ������’��������������������������������������������������������������������������������� �������������������������������������������������������������������������������������� ���������������������������������������

������������ ������������������������������������������������������������������������������������� ������������������������������������������������������������������������������������� ��������������������������������������������������������������������������������������� ��������������������������������������������������������������������������������������� ��������������������������������������������������������������������������������� ���������������������������������������������������������������������������������������� �����������������������������������������������������������������������������

��������������������������������������������������������������������������������������� ����������������������������������������������������������������������������������������� ������������������������������������������������������������������������������������� �����������������������������������������

������������ ������������������������������������������������������������������������������������������ ������������������������������������������������������������������������������ ������������������������

�������������������������������������������������������������������������������������������� ���������������������������������������������������������������������������������� ����������������������������������������������������

�������

“�����������������������������������’������������������������������������������������� ����������������������������������”�����������’�����

•���������������������������� •����������������� •��������������������� •�������������������������� •����������������������������

������������

���������

•�������������������������������� •�������������� •������������������������ •������������������������������ •������������������� •��������������������������������������� •������������������������������������ •��������������������

������������

•�������������������������������������� •������������������������������������������� •���� ��������

����������������������

•������������������������������������ •���������������������������������� •����������������������������

���������

•������������������������������ •�������������������������� •������������������������������� •�������������������������

�������������

•���������������������������� •��������������������������������� •����������������’����������� •������������������������ •������������������������������������������ •����������Mathematica�������