37

I have a breakpoint that looks like this

-[UITableViewCell setSelected:]

and it works, but I cannot figure out how to get the value that is being passed in.

I have tried -[UITableViewCell setSelected:(BOOL)what] and -[UITableViewCell setSelected:what] which do not work at all.

How can I access the parameters?

If this doesn't work, I'll have to make a DebugUITableViewCell just to see what's going on, which is a hassle and touches a lot of code.

2
  • 1
    I'm not brave enough to post this a real answer as I"m just speculating, but I believe want is just not available to the debugger. You don't get the symbols for UIKit so the debugger has no idea about what the name of the argument is to that method. the runtime might be able to figure out its type, but its not the same as having the real symbol information. Your subclass trick works I guess because you are also providing that symbol info to the debugger.
    – D.C.
    Mar 20, 2013 at 0:29
  • I arrived at this question for the exact same method — without even including the method name in the search query. Table views truly are a mystery, and even more so 5 years later! 😅 Sep 19, 2018 at 13:02

7 Answers 7

35

If you debug your code on the device the parameters when you hit your breakpoint will consistently be in registers r0, r1, and r2. If you use po $r0 you'll see the object receiving setSelected. If you use po $r1 you'll get "no Objective-C description available" because that's the selector. Inspect $r2 to see if selected is being set to YES or NO. It's a similar story on i386, but I can't remember off hand which registers are used.

14
  • 1
    @darren I tested this with lldb before posting my answer, but what makes you think it wouldn't work? Mar 20, 2013 at 1:42
  • 17
    It will work fine with lldb. In fact, it's even better in lldb because (for architectures that pass arguments in registers, like arm and x86_64) $arg0, $arg1 etc are provided which alias to the correct register for that architecture. Mar 20, 2013 at 2:25
  • 13
    In lldb access the arguments starting with $arg1. For a method call the first user argument is $arg3; So if the first method argument is a NSString just po $arg3 to display it.
    – zaph
    May 10, 2013 at 16:23
  • 4
    Tried to log a CGPoint argument but p (CGPoint)$arg3 doesn't work. Oct 8, 2013 at 23:38
  • 1
    I also could not get po $arg0 to work in lldb. Tried both x86_64 and on arm64.
    – stigi
    Jan 28, 2014 at 10:09
18

In LLDB on Simulator use

p $arg3

for the first parameter.

6

You could replace -[UITableViewCell setSelected:] with your own implementation for debugging purposes. Below, UITableViewCellSetSelected will be called instead of UIKit's method.

static void (*__originalUITableViewCellSetSelected)( UITableViewCell *, SEL, BOOL ) ;
static void UITableViewCellSetSelected( UITableViewCell * self, SEL _cmd, BOOL b )
{
    // your code here... (or set a breakpoint here)
    NSLog(@"%@<%p> b=%s\n", [ self class ], self, b ? "YES" : "NO" ) ;

    (*__originalUITableViewCellSetSelected)( self, _cmd, b ) ; // call original implementation:
}

@implementation UITableViewCell (DebugIt)

+(void)load
{
    Method m = class_getInstanceMethod( [ self class ], @selector( setSelected: ) ) ;
    __originalUITableViewCellSetSelected = (void(*)(id, SEL, BOOL))method_getImplementation( m ) ;
    method_setImplementation( m, (IMP)UITableViewCellSetSelected ) ;
}

@end
2
  • although for a quick solution @aaron's answer is good. I'll leave this here for info...
    – nielsbot
    Mar 20, 2013 at 1:27
  • I think I might accept Aaron's answer because it's more on point, but this is great too. Thank you! Mar 20, 2013 at 2:53
3

For the methods without source code, the following works: Put a symbolic breakpoint so that the debugger stops at the first line of the method. Make sure the top stack frame is selected. Then:

In Objectice-C methods

  • po $arg1 prints self
  • po $arg3 prints the first argument, remaining arguments in $arg4, $arg5, etc.

In C functions the arguments start at $arg1

This works both on IOS device and simulator.

1
2

Based on -[UIApplication sendAction:toTarget:fromSender:forEvent:] symbol we can add symbolic breakpoint to check which sender has sent an action to which target.

We create symbolic breakpoint with:

  • symbol: -[UIApplication sendAction:toTarget:fromSender:forEvent:]
  • debugger command line actions:
    • po "Target"
    • po $arg4
    • po "Sender"
    • po $arg5

The output would be: "Target" <project.TargetViewController: 0x14ddb1470> "Sender" <UIButton: 0x14de86000; frame = (331 7; 49 30); opaque = NO; layer = <CALayer: 0x174237020>>

So as @Dan said, method parameters start with argument 3 (po $arg3).

0

you can use lldb command:

"image lookup -rn keyword"

keyword equals functions you wanna search, eg, show

then, you will look some output like follows,

Summary: ApplSlate`closure #2 (__C.UIBarButtonItem) -> () in ApplSlate.BaseTableViewController.showLoading(includeTabBar: Swift.Bool) -> () at BaseTableViewController.swift:98 Address: FullSlate[0x0000000100154a60] (FullSlate.__TEXT.__text + 1378752)

And you get the symbolic name is : closure #2 (__C.UIBarButtonItem) -> () in ApplSlate.BaseTableViewController.showLoading(includeTabBar: Swift.Bool) -> ()

0

Answer

Xcode's lldb get ObjC first parameter is: $arg3

->

  • lldb print first parameter value: po $arg3
    • enter image description here
  • Xcode add symbolic breakpoint condition: still use arg3
    • eg
      • Condition: (BOOL)($arg3 == NULL)
        • enter image description here

Related

  • rest parameter is: $arg4, $arg5, ...
  • $arg0 not exists
    • eg
      (lldb) po $arg0
      error: <user expression 3>:1:1: use of undeclared identifier '$arg0'
      $arg0
      ^
      
  • $arg1 is function type
    • - means Instance
      • eg
        (lldb) po $arg1
        _
        
    • + means Class
  • $arg2 is function pointer self, use SEL can parse to function name
    • eg
      (lldb) po $arg2
      8203662366
      
      (lldb) po (SEL)$arg2
      "stringByAppendingString:"
      
3
  • thanks. does this add new value that's not in the accepted answer? Jan 14, 2022 at 17:56
  • I think yes, at least add SOME extra value for Related part of my answer, for other (newbie) like me before
    – crifan
    Jan 15, 2022 at 1:51
  • Seems like more of a comment on an answer, but ok. Could you please fix the answer to use Xcode (the name of the IDE) instead of XCode (common mistake)? Thanks. Mar 22, 2022 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.