17

Sometimes, when trying to print out a variable in the debugger, the following error message is displayed:

error: warning: <EXPR>:12:9: warning: initialization of variable '$__lldb_error_result' was never used; consider replacing with assignment to '_' or removing it
    var $__lldb_error_result = __lldb_tmp_error
    ~~~~^~~~~~~~~~~~~~~~~~~~
    _

error: <EXPR>:18:5: error: use of unresolved identifier '$__lldb_injected_self'
    $__lldb_injected_self.$__lldb_wrapped_expr_120(     
    ^~~~~~~~~~~~~~~~~~~~~

This is a known lldb bug (https://bugs.swift.org/browse/SR-6156), but perhaps someone knows a workaround that can be used until that bug is fixed?

1 Answer 1

42

As a workaround you can print it in the lldb debugger using:

frame variable variablename

Also possible using shortened syntax for quicker typing

fr v variablename

Since XCode 10.2 an ever simpler lldb syntax is supported:

v variable

Update - new workarounds:

Print stack addresses:

v -L variablename

po like on stack frame variable.property

 v -o variablename.property

Swift like p

e unsafeBitCast(address, to: ClassName.self)

Update2 - new workaround applicable for Swift classes being wrappers of objc classes.

Example:

v response
(HTTPURLResponse) response = 0x0000000283ba7640 {

if v works^:

e -l objc -- (int)[0x0000000283ba7640 statusCode]
(int) $2 = 404

Update 3 printing Swift arrays (here array of [Int])

v -L arrayName
0x00007ff7b753a9a8: ([Int]) arrayName = 1169 values {
0x000060000295e7b0:   [0] = 1
...

then

e unsafeBitCast(0x00007ff7b753a9a8, to: UnsafeMutablePointer<Array<Int>>.self).pointee[0].bitWidth

also possible with map

e unsafeBitCast(0x00007ff7b753a9a8, to: UnsafeMutablePointer<Array<Int>>.self).pointee.map{ $0.bitWidth }

I'd appreciate reports what is actually helpful and works. Thanks.

More information on this kind of capabilities can be found here: https://developer.apple.com/library/content/documentation/General/Conceptual/lldb-guide/chapters/C5-Examining-The-Call-Stack.html

2
  • 6
    Better than nothing, but still quite cumbersome to use... I wonder why this Swift bug (?) isn't fixed for so long 🤓
    – Serzhas
    Commented Nov 22, 2018 at 14:05
  • 2
    This doesn't resolve the problem. It's possible to print the variables in other code bases. Why doesn't it work now? And why does it work with other code bases? Commented May 1, 2020 at 8:31

Your Answer

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

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