2

I got this error, do I need to use a specific syntax to use an enum in LLDB ?

(lldb) expr NSString *$text = [[NSString alloc] initWithData:$data encoding:NSUTF8StringEncoding]
error: use of undeclared identifier 'NSUTF8StringEncoding'
error: 1 errors parsing expression

Note: data is valid in my context and of the right type

Edit when using the actual instead of the enum I get

expr NSString *$text = [[NSString alloc] initWithData:$data encoding:(NSStringEncoding)4]
error: no known method '-initWithData:encoding:'; cast the message send to the method's return type
error: 1 errors parsing expression

I have tried with and without the cast to (NSStringEncoding)

Solution to use the value

expr NSString *$text = (NSString *)[[NSString alloc] initWithData:$data encoding:4]

Since I was using -initWithData:encoding: (with a type of NSUInteger for encoding) lldb could not infer the return type of the whole method so I had to cast the return type as well.

3 Answers 3

5

There's a mismatch between the debug information for enums and lldb's mode of searching for symbols that makes it very expensive to search for a particular enum value if the enum name is not specified. At present, if some other action hasn't caused the enum debug info to be brought in, lldb won't find it. Doubly unfortunately the NSStringEncoding enum is an anonymous enum, so it isn't possible to specify a value by name::value.

If you are using Xcode 7.x, however, you can import the foundation module into the compiler lldb uses for expression parsing with:

(lldb) expr @import Foundation

and then lldb will find this enum value (and many other things like macros and more accurate method types.)

Note, if you find this useful and you are using Xcode, you can make this happen automatically by setting a symbolic breakpoint on main, adding an "action" with this debugger command, and setting it to auto-continue.

Once the feature has had a little more time out in the wild, we'll probably do this automatically.

1
  • Awesome! Thanks for the detailed explanation and tips. Oct 16, 2015 at 1:13
1

NSUTF8StringEncoding is from NSUInteger enum and it equals 4:

typedef NSUInteger NSStringEncoding;
...
 NSUTF8StringEncoding = 4,

So for lldb command you can pass smth like this:

(lldb) po [[NSString alloc] initWithData:$data encoding:4]
1
  • Thanks for the answer I have just figured this, is there actually a way to use the enum instead of its value directly? Oct 15, 2015 at 11:24
1

Refer to this nice blog on using lldb debugger command.

Your solution is to directly pass enum value for NSUTF8StringEncoding like this:

expr NSString *$text = (NSString *)[[NSString alloc] initWithData:data encoding:4]
6
  • Thanks for the answer I have just figured this, is there actually a way to use the enum instead of its value directly? Oct 15, 2015 at 11:24
  • Well I don't think. I would love to see such an option although!
    – Abhinav
    Oct 15, 2015 at 11:25
  • expr NSString *$text = [[NSString alloc] initWithData:$data encoding:(NSUInteger)4] error: no known method '-initWithData:encoding:'; cast the message send to the method's return type error: 1 errors parsing expression do you get this as well? Oct 15, 2015 at 11:27
  • 1
    Try this expr NSString *$text = (NSString *)[[NSString alloc] initWithData:data encoding:(NSUInteger)4] .
    – Abhinav
    Oct 15, 2015 at 11:32
  • Great, it works, lldb was actually complaining about the return type of the whole function, thank you @Abhinav Oct 15, 2015 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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