Debugging in XCode - running through code and breakpoints [closed]

会有一股神秘感。 提交于 2021-02-07 02:54:41

问题


I would like to know how to debug best in xcode. I know how to set a break point but ideally i would like the simulator to stop and then step through the code...

Not step through breakpoints but step through the code line by line so I can see where it goes, what methods are run etc...

Is this possible, if so how?


回答1:


Debug control icons in Xcode

At the bottom left of your code window in Xcode 4 you can see the debug step controls. Each has a short alt text to explain it. I'll explain in slightly more detail here. Learning to become an expert debugger is the subject of many textbooks.

The play button alt text: Continue program execution. You've probably found this already. Pressing it advances to the next breakpoint.

The jumping arrow alt text: Step over. Pressing this button repeatedly will allow you to advance through your program at the current level of scope just as your program would. It will allow you to observe the behavior of your program step by step through the current method. When the current method finishes, Step over will take you to the calling method one step up in the program stack.

The down arrow alt text: Step into. Pressing this button will follow the new method into its scope and allow you to view code in the called-method if it has been compiled for debugging. For example, clicking this on the current line of code in the above debug window will take you to the _generateSources method, which you can then progress through with Step over.

The up arrow alt text: Step out. Takes you out of the current context and into the calling method one step up in the program stack. This behaves identically to finishing a method using Step over, executing the program as normal and executing all of the lines of code in the original scope that you did not debug using Step over.

View of call stack

Click on the silhouette with the blue background to see the current call stack. The call stack will always progress all the way from your current scope to the first method that was called to launch your program. Each method you see here was called in sequence in order to reach -[HelloWorldLayer init]. When you press Step out then the current line of executing code will return to -[CCNode node], and if you have the source for it you can browse it.

To the left are the current Local variables visible from the scope of the line of code currently being executed (line 76 in this image). These variables are how you can really use the above commands like Step over. background and winSize are local variables that were defined in this scope and are currently being used. When the scope is exited they will be gone. _cmd is a pointer to the selector that is currently being invoked in Objective-C: -[HelloWorldLayer init]. You won't need this until you are an advanced debugger. self is the pointer to an object containing all of the ivars that belong to the currently executing class, HelloWorldLayer, and objc_super is a pointer to the parent class of HelloWorldLayer.




回答2:


I would add keyboard shortcuts to @Thomson Corner's answer.

  • Use 'c' to continue. 's' to step, 'n' for next, 'f' for finish. I don't like operating the debugger with the mouse. Those icons are too tiny and getting the pointer on them is a pain (once in a while). I've always been a keyboard guy and it's REALLY comfortable with the keyboard.

  • You should also try using po - it prints out variable values with type ahead suggestions. Like po _varName. Makes it easy to check if a variable is nil-valued etc.,

  • You can also change your command aliases by modifying ~/.lldbinit.

Here's the official tutorial: http://lldb.llvm.org/tutorial.html. And another tutorial: http://www.informit.com/articles/article.aspx?p=1829415&seqNum=6. What I pointed out above are the most basic debugging functions programmers mostly perform. There are more options like exception breakpoints and loading programs, but I'm pretty sure they're for another day if you are just getting started.



来源:https://stackoverflow.com/questions/12697473/debugging-in-xcode-running-through-code-and-breakpoints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!