How to print or see method call stack in xcode?

妖精的绣舞 提交于 2019-12-31 09:45:16

问题


I working on my iPad App,

While running the code there is one method in table view.

as we know

`cellForRowAtIndexPath:`

There are several times this method can be called.

like while

 scrolling table view cells.

 Or table view reload data.

I have complex coding so I just want to see method call stack...

That from which method is calling cell for row at index path.

I have used NSLogs and breakpoints but still could not get.


回答1:


When you hit a breakpoint, select the Debug navigator in the navigator area (left side of the window):

The Debug navigator shows you a stack trace for each thread in your app. It shows you essentially the same information you get from gdb's backtrace command, but omits the return addresses (which normally aren't very helpful). Use the controls at the bottom of the navigator to hide or show all threads and to change the adjust the number of stack frames shown. I've got the slider set in the middle of its range in the image above, and the Debug navigator is omitting stack frames 2-18, which are all calls from one framework method to another, i.e. not my stuff.

Xcode 4 should be set up to display the Debug navigator automatically when you're debugging, but if not you can configure it to do so by going to Xcode->Behaviors->Edit Behaviors.... Then select the Run Pauses item from the list and set it to Show navigator Debug Navigator.




回答2:


You can print the stack trace in the NSLog by

NSLog(@"Stack trace : %@",[NSThread callStackSymbols]);

EDIT: Swift code

println("Stack trace: %@", NSThread.callStackSymbols())



回答3:


You can set a breakpoint (or pause app) and from gdb debugger write "backtrace".

You should see stack:

(gdb) backtrace
#0  0x9022f7fe in mach_msg_trap ()
#1  0x9022ecdc in mach_msg ()
#2  0x022a310a in __CFRunLoopServiceMachPort ()
#3  0x02206550 in __CFRunLoopRun ()
#4  0x02205d84 in CFRunLoopRunSpecific ()
#5  0x02205c9b in CFRunLoopRunInMode ()
#6  0x024617d8 in GSEventRunModal ()
#7  0x0246188a in GSEventRun ()
#8  0x00c0ca16 in UIApplicationMain ()
#9  0x0000270d in main (argc=1, argv=0xbfeff550) at /Users/.........m:14



回答4:


Try setting a breakpoint at the entry for

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

Then right click on the breakpoint and select "Log Stack Trace and Auto Continue" from the "Built-in Breakpoints" menu item.

This will automatically log the stack trace every time this function is entered and continue without having to step actually use the gdb console.

That was for Xcode 3.x For Xcode 4 the procedure is a bit different.

  1. Set your breakpoint.
  2. Right click on the breakpoint and select "Edit Breakpoint". (Or Command-Option Click on the breakpoint)
  3. Select "Debugger Command" fromt the "Action" popup.
  4. Set the message to "bt" (Without the quotes.)
  5. Under "Options" make sure to check "Automatically continue after evaluating".



回答5:


Just putting the Swift 4 syntax in case if some one needs

print("Stack trace: \(Thread.callStackSymbols)")



回答6:


By https://developer.apple.com/library/archive/documentation/General/Conceptual/lldb-guide/chapters/C5-Examining-The-Call-Stack.html

(lldb) thread backtrace

Happy debugging.



来源:https://stackoverflow.com/questions/9516288/how-to-print-or-see-method-call-stack-in-xcode

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