advanced swift debugging in lldb - apple inc. · advanced swift debugging in lldb debugging in a...

185
© 2014 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. #WWDC14 Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools

Upload: others

Post on 29-Jun-2020

36 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

© 2014 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

#WWDC14

Advanced Swift Debugging in LLDBDebugging in a Swift world

Session 410 Enrico Granata Debugger Engineer

Tools

Page 2: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Introduction

Page 3: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Introduction

Swift is the modern language of Cocoa

Page 4: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Introduction

Swift is the modern language of Cocoa

Your existing tools, improved

Page 5: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Introduction

Swift is the modern language of Cocoa

Your existing tools, improved

The debugger can help you explore in the context of your app

Page 6: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Introduction

Swift is the modern language of Cocoa

Your existing tools, improved

The debugger can help you explore in the context of your app• And be productive

Page 7: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Introduction

Swift is the modern language of Cocoa

Your existing tools, improved

The debugger can help you explore in the context of your app• And be productive

Swift feels awesome in LLDB

Page 8: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

What You Will Learn

Page 9: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

What You Will Learn

Swift types in LLDB

Page 10: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

What You Will Learn

Swift types in LLDB• Optional types

Page 11: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

What You Will Learn

Swift types in LLDB• Optional types

• Protocols

Page 12: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

What You Will Learn

Swift types in LLDB• Optional types

• Protocols

• Generics

Page 13: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

What You Will Learn

Swift types in LLDB• Optional types

• Protocols

• Generics

Debugging combined Swift and Objective-C

Page 14: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

What You Will Learn

Swift types in LLDB• Optional types

• Protocols

• Generics

Debugging combined Swift and Objective-C

Stepping

Page 15: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

What You Will Learn

Swift types in LLDB• Optional types

• Protocols

• Generics

Debugging combined Swift and Objective-C

Stepping

Data formatters for Swift objects

Page 16: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

What You Will Learn

Swift types in LLDB• Optional types

• Protocols

• Generics

Debugging combined Swift and Objective-C

Stepping

Data formatters for Swift objects

Name uniqueness in Swift

Page 17: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Optional Types

Page 18: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Optional Types

Optionals introduce indirection

Page 19: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Optional Types

Optionals introduce indirection• Is it there? Is it not?

Page 20: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Optional Types

Optionals introduce indirection• Is it there? Is it not?

LLDB implicitly unwraps whenever possible

Page 21: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Optional Types

Optionals introduce indirection• Is it there? Is it not?

LLDB implicitly unwraps whenever possible

nil used consistently for the no-value situation

Page 22: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

var string: String? = "Hello WWDC14 Attendees" var rect: NSRect? = NSMakeRect(0, 0, 20, 14) var url: NSURL? = nil

Optional Types

Page 23: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

var string: String? = "Hello WWDC14 Attendees" var rect: NSRect? = NSMakeRect(0, 0, 20, 14) var url: NSURL? = nil

Optional Types

string = (Swift.String?) "Hello WWDC14 Attendees"rect = (CoreGraphics.CGRect?) origin=(x=0, y=0) size=(width=20, height=14)url = (Foundation.NSURL?) nil

Page 24: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Double Optional

var optional: String? = nil var twice_optional: String?? = Optional.Some(nil)

Page 25: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Double Optional

var optional: String? = nil var twice_optional: String?? = Optional.Some(nil)

By default, propagate nil upwards

optional = (Swift.String?) niltwice_optional = (swift.String) nil

Page 26: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Raw Display(lldb) fr v -R twice_optional (Swift.String??) twice_optional = Some { Some = None {

Some = { core = {

_baseAddress = { value = 0x0000000000000000

} _countAndFlags = {

value = 0 } _owner = None {

Some = { instance_type = 0x0000000000000000

} }

} }

} }

!

Page 27: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Raw Display(lldb) fr v -R twice_optional (Swift.String??) twice_optional = Some { Some = None {

Some = { core = {

_baseAddress = { value = 0x0000000000000000

} _countAndFlags = {

value = 0 } _owner = None {

Some = { instance_type = 0x0000000000000000

} }

} }

} }

!

Page 28: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Raw Display(lldb) fr v -R twice_optional (Swift.String??) twice_optional = Some { Some = None {

Some = { core = {

_baseAddress = { value = 0x0000000000000000

} _countAndFlags = {

value = 0 } _owner = None {

Some = { instance_type = 0x0000000000000000

} }

} }

} }

!

Page 29: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Raw Display(lldb) fr v -R twice_optional (Swift.String??) twice_optional = Some { Some = None {

Some = { core = {

_baseAddress = { value = 0x0000000000000000

} _countAndFlags = {

value = 0 } _owner = None {

Some = { instance_type = 0x0000000000000000

} }

} }

} }

!

Page 30: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Raw Display(lldb) fr v -R twice_optional (Swift.String??) twice_optional = Some { Some = None {

Some = { core = {

_baseAddress = { value = 0x0000000000000000

} _countAndFlags = {

value = 0 } _owner = None {

Some = { instance_type = 0x0000000000000000

} }

} }

} }

!

Page 31: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Types

Page 32: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Types

What is a type?

Page 33: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Types

What is a type?

Lots of answers

Page 34: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Types

What is a type?

Lots of answers

a classification that determines a set of valid values and operations for data

Page 35: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Types

What is a type?

Lots of answers

a classification that determines a set of valid values and operations for data

Data can have multiple types

Page 36: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Static/Dynamic Types

Page 37: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Static/Dynamic Types

var url: AnyObject = NSURL(string: “http://www.apple.com”)

Variables have a declared (aka static) type

Page 38: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Static/Dynamic Types

var url: AnyObject = NSURL(string: “http://www.apple.com”)

Variables have a declared (aka static) type

Page 39: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Static/Dynamic Types

var url: AnyObject = NSURL(string: “http://www.apple.com”)

Variables have a declared (aka static) type

!

url.hash

Page 40: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Static/Dynamic Types

var url: AnyObject = NSURL(string: “http://www.apple.com”)

Variables have a declared (aka static) type

!

url.hash

Which “hash” gets called?

Page 41: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Static/Dynamic Types

var url: AnyObject = NSURL(string: “http://www.apple.com”)

Variables have a declared (aka static) type

!

url.hash

Which “hash” gets called?

The one that matches the runtime (aka dynamic) type

Page 42: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Dynamic Dispatchurl.hash

Page 43: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Dynamic Dispatchtype info

ivars

url.hash

Page 44: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Dynamic Dispatchtype info

ivars

super type info

foo

bar

hash

baz

url.hash

Page 45: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

hash { …

}

Dynamic Dispatchtype info

ivars

super type info

foo

bar

hash

baz

url.hash

Page 46: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

hash { …

}

Dynamic Dispatchtype info

ivars

super type info

foo

bar

hash

baz

What if the runtime can’t find an implementation?

url.hash

Page 47: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

hash { …

}

Dynamic Dispatchtype info

ivars

super type info

foo

bar

hash

baz

What if the runtime can’t find an implementation?

Ask the superclass

url.hash

Page 48: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

hash { …

}

Dynamic Dispatchtype info

ivars

super type info

foo

bar

hash

baz

What if the runtime can’t find an implementation?

Ask the superclass

url.hash

url.another

Page 49: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

hash { …

}

Dynamic Dispatchtype info

ivars

super type info

foo

bar

hash

baz

What if the runtime can’t find an implementation?

Ask the superclass

type info

ivars

url.hash

url.another

Page 50: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

hash { …

}

Dynamic Dispatchtype info

ivars

super type info

foo

bar

hash

baz

What if the runtime can’t find an implementation?

Ask the superclass

type info

ivars

super type info

foo

bar

hash

baz

url.hash

url.another

Page 51: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

super type info

foo

another

bar

hash

hash { …

}

Dynamic Dispatchtype info

ivars

super type info

foo

bar

hash

baz

What if the runtime can’t find an implementation?

Ask the superclass

type info

ivars

super type info

foo

bar

hash

baz

url.hash

url.another

Page 52: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

another { …

}

super type info

foo

another

bar

hash

hash { …

}

Dynamic Dispatchtype info

ivars

super type info

foo

bar

hash

baz

What if the runtime can’t find an implementation?

Ask the superclass

type info

ivars

super type info

foo

bar

hash

baz

url.hash

url.another

Page 53: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Dynamic Types in LLDB

2 class Base {} 34 class Derived : Base { 5 var meaning = 42 6 }78 func userbase(x: Base) { 9 println("All your base are belong to us.") 10 }

Page 54: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

useBase (aDerived)

Dynamic Types in LLDB

2 class Base {} 34 class Derived : Base { 5 var meaning = 42 6 }78 func userbase(x: Base) { 9 println("All your base are belong to us.") 10 }

Page 55: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Dynamic Types in LLDB

2 class Base {} 34 class Derived : Base { 5 var meaning = 42 6 }78 func userbase(x: Base) { 9 println("All your base are belong to us.") 10 }

Page 56: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Dynamic Types in LLDB

2 class Base {} 34 class Derived : Base { 5 var meaning = 42 6 }78 func userbase(x: Base) { 9 println("All your base are belong to us.") 10 }9 println("All your base are belong to us.")

Page 57: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Dynamic Types in LLDB

x = (WWDC14.Derived) 0x0000000100510000WWDC14.Base meaning = (Swift.Int) 42

2 class Base {} 34 class Derived : Base { 5 var meaning = 42 6 }78 func userbase(x: Base) { 9 println("All your base are belong to us.") 10 }9 println("All your base are belong to us.")

Page 58: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Protocols

Page 59: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Protocols

Protocols are types in Swift

Page 60: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Protocols

Protocols are types in Swift• Variables can have protocol types

Page 61: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Protocols

Protocols are types in Swift• Variables can have protocol types

• Protocols can be included in function signatures

Page 62: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Protocols

Protocols are types in Swift• Variables can have protocol types

• Protocols can be included in function signatures

Variables of protocol type are limited

Page 63: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Protocols

Protocols are types in Swift• Variables can have protocol types

• Protocols can be included in function signatures

Variables of protocol type are limited• To only reveal what the protocol allows

Page 64: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Protocols

Protocols are types in Swift• Variables can have protocol types

• Protocols can be included in function signatures

Variables of protocol type are limited• To only reveal what the protocol allows

LLDB opens the curtain for you

Page 65: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Protocols

Protocols are types in Swift• Variables can have protocol types

• Protocols can be included in function signatures

Variables of protocol type are limited• To only reveal what the protocol allows

LLDB opens the curtain for you• And it shows you the full value

Page 66: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Protocols

protocol Creature { func speak() } !class Cat: Creature { func speak() { println("Meow. Purr") } } class Dog: Creature { func speak() { println(“Woof!”) } }

19 func atTheZoo(creature: Creature) { 20 creature.speak()21 }

Page 67: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Protocols

19 func atTheZoo(creature: Creature) { 20 creature.speak()21 }

Page 68: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Protocols

19 func atTheZoo(creature: Creature) { 20 creature.speak()21 } 20 creature.speak()

Page 69: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Protocols

creature = (WWDC14.Dog) 0x0000000100510000happy = (WWDC14.Dog.Happiness) VeryHappy

19 func atTheZoo(creature: Creature) { 20 creature.speak()21 } 20 creature.speak()

Page 70: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Protocols

Page 71: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

ProtocolsConsole users beware

Page 72: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

ProtocolsConsole users beware

(lldb) fr v creature (Creature) creature = {

payload_data_0 = 0x0000000100510000 -> 0x00000001000081c0 direct type metadata for WWDC14.Dog + 16 payload_data_1 = 0x00007fff5fbffa10 payload_data_2 = 0x0000000100002b52 WWDC14`WWDC14.play_with_hierarchy () -> () + 66 at hierarchy.swift:20 instance_type = 0x00000001000081c0 direct type metadata for WWDC14.Dog + 16 protocol_witness_0 = 0x0000000100007510 protocol witness table for WWDC14.Dog : WWDC14.Creature

}

Page 73: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

(lldb) fr v -d r creature (WWDC14.Dog) creature = 0x0000000100510000 { happy = VeryHappy

}

ProtocolsConsole users beware

(lldb) fr v creature (Creature) creature = {

payload_data_0 = 0x0000000100510000 -> 0x00000001000081c0 direct type metadata for WWDC14.Dog + 16 payload_data_1 = 0x00007fff5fbffa10 payload_data_2 = 0x0000000100002b52 WWDC14`WWDC14.play_with_hierarchy () -> () + 66 at hierarchy.swift:20 instance_type = 0x00000001000081c0 direct type metadata for WWDC14.Dog + 16 protocol_witness_0 = 0x0000000100007510 protocol witness table for WWDC14.Dog : WWDC14.Creature

}

Page 74: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

(lldb) fr v -d r creature (WWDC14.Dog) creature = 0x0000000100510000 { happy = VeryHappy

}

ProtocolsConsole users beware

(lldb) fr v creature (Creature) creature = {

payload_data_0 = 0x0000000100510000 -> 0x00000001000081c0 direct type metadata for WWDC14.Dog + 16 payload_data_1 = 0x00007fff5fbffa10 payload_data_2 = 0x0000000100002b52 WWDC14`WWDC14.play_with_hierarchy () -> () + 66 at hierarchy.swift:20 instance_type = 0x00000001000081c0 direct type metadata for WWDC14.Dog + 16 protocol_witness_0 = 0x0000000100007510 protocol witness table for WWDC14.Dog : WWDC14.Creature

}

Page 75: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

ProtocolsConsole users beware

(lldb) fr v creature (Creature) creature = {

payload_data_0 = 0x0000000100510000 -> 0x00000001000081c0 direct type metadata for WWDC14.Dog + 16 payload_data_1 = 0x00007fff5fbffa10 payload_data_2 = 0x0000000100002b52 WWDC14`WWDC14.play_with_hierarchy () -> () + 66 at hierarchy.swift:20 instance_type = 0x00000001000081c0 direct type metadata for WWDC14.Dog + 16 protocol_witness_0 = 0x0000000100007510 protocol witness table for WWDC14.Dog : WWDC14.Creature

}

(lldb) fr v -d r creature (WWDC14.Dog) creature = 0x0000000100510000 { happy = VeryHappy

}

Page 76: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Generics

Page 77: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Generics

Swift has native support for generics

Page 78: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Generics

Swift has native support for generics

Type information passed to functions

Page 79: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Generics

Swift has native support for generics

Type information passed to functions• LLDB uses it to reconstruct code’s meaning

Page 80: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Generics1 protocol Producer { 2 typealias Element3 func produce() -> Element 4 } 56 class TheProducer: Producer {7 typealias Element = Int8 var _x: Int 9 int(_x: Int) {10 Self._x = x11 }12 func produce() -> Int { 13 return ++_x14 }15 }1617 func produce<P: Producer where P.Element == Int>(p: P, count: Int) {18 println("About to generate data")19 for i in 0..count {20 println(p.produce())21 }22 }

Page 81: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Generics1 protocol Producer { 2 typealias Element3 func produce() -> Element 4 } 56 class TheProducer: Producer {7 typealias Element = Int8 var _x: Int 9 int(_x: Int) {10 Self._x = x11 }12 func produce() -> Int { 13 return ++_x14 }15 }1617 func produce<P: Producer where P.Element == Int>(p: P, count: Int) {18 println("About to generate data")19 for i in 0..count {20 println(p.produce())21 }22 }

19 for i in 0..count {

Page 82: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Generics

p = (WWDC14.TheProducer) 0x0000000100510000_x = (Swift.Int) 2014count = (Swift.Int) 6$swift.type.P = (builtin.RawPointer) 0x100009410

1 protocol Producer { 2 typealias Element3 func produce() -> Element 4 } 56 class TheProducer: Producer {7 typealias Element = Int8 var _x: Int 9 init(_x: Int) {10 Self._x = x11 }12 func produce() -> Int { 13 return ++_x14 }15 }1617 func produce<P: Producer where P.Element == Int>(p: P, count: Int) {18 println("About to generate data")19 for i in 0..count {20 println(p.produce())21 }22 }

19 for i in 0..count {

Page 83: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Generics

p = (WWDC14.TheProducer) 0x0000000100510000_x = (Swift.Int) 2014count = (Swift.Int) 6$swift.type.P = (builtin.RawPointer) 0x100009410

1 protocol Producer { 2 typealias Element3 func produce() -> Element 4 } 56 class TheProducer: Producer {7 typealias Element = Int8 var _x: Int 9 init(_x: Int) {10 Self._x = x11 }12 func produce() -> Int { 13 return ++_x14 }15 }1617 func produce<P: Producer where P.Element == Int>(p: P, count: Int) {18 println("About to generate data")19 for i in 0..count {20 println(p.produce())21 }22 }

19 for i in 0..count {

Page 84: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Generics

p = (WWDC14.TheProducer) 0x0000000100510000_x = (Swift.Int) 2014count = (Swift.Int) 6$swift.type.P = (builtin.RawPointer) 0x100009410

1 protocol Producer { 2 typealias Element3 func produce() -> Element 4 } 56 class TheProducer: Producer {7 typealias Element = Int8 var _x: Int 9 init(_x: Int) {10 Self._x = x11 }12 func produce() -> Int { 13 return ++_x14 }15 }1617 func produce<P: Producer where P.Element == Int>(p: P, count: Int) {18 println("About to generate data")19 for i in 0..count {20 println(p.produce())21 }22 }

19 for i in 0..count {

Page 85: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Generics

p = (WWDC14.TheProducer) 0x0000000100510000_x = (Swift.Int) 2014count = (Swift.Int) 6$swift.type.P = (builtin.RawPointer) 0x100009410

1 protocol Producer { 2 typealias Element3 func produce() -> Element 4 } 56 class TheProducer: Producer {7 typealias Element = Int8 var _x: Int 9 init(_x: Int) {10 Self._x = x11 }12 func produce() -> Int { 13 return ++_x14 }15 }1617 func produce<P: Producer where P.Element == Int>(p: P, count: Int) {18 println("About to generate data")19 for i in 0..count {20 println(p.produce())21 }22 }

19 for i in 0..count {

Page 86: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Generics

p = (WWDC14.TheProducer) 0x0000000100510000_x = (Swift.Int) 2014count = (Swift.Int) 6$swift.type.P = (builtin.RawPointer) 0x100009410

1 protocol Producer { 2 typealias Element3 func produce() -> Element 4 } 56 class TheProducer: Producer {7 typealias Element = Int8 var _x: Int 9 init(_x: Int) {10 Self._x = x11 }12 func produce() -> Int { 13 return ++_x14 }15 }1617 func produce<P: Producer where P.Element == Int>(p: P, count: Int) {18 println("About to generate data")19 for i in 0..count {20 println(p.produce())21 }22 }

19 for i in 0..count {

Page 87: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Debugging Optimized Swift Code

Page 88: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Debugging Optimized Swift Code

Debug builds: Literal translation of your code

Page 89: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Debugging Optimized Swift Code

Debug builds: Literal translation of your code

Optimized builds: Enhanced toward a goal (speed, memory…)

Page 90: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Debugging Optimized Swift Code

Debug builds: Literal translation of your code

Optimized builds: Enhanced toward a goal (speed, memory…)

The first rule of debugging optimized code • Don’t!

Page 91: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Debugging Optimized Swift Code

Debug builds: Literal translation of your code

Optimized builds: Enhanced toward a goal (speed, memory…)

The first rule of debugging optimized code • Don’t!

Generics may be optimized for specific typesProtocols may be devirtualized

Page 92: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop

Page 93: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop

Two main cases: • ObjC frameworks in Swift apps

• Apps with ObjC and Swift source code

Page 94: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop

Two main cases: • ObjC frameworks in Swift apps

• Apps with ObjC and Swift source code

What to expect? • Variables view

• Expression evaluation

• po

Page 95: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Most native experience • Data shown in the type’s language of origin

• Formatters apply in all cases

Objective-C Interop: Variables View

3 func addStrings(x: String, y: NSString) -> NSString { 4 return x+y5 }

x = (Swift.String) "Hello,"y = (__NSCFString *) @"world"

Page 96: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop: Expressions

Page 97: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop: Expressions

Expressions see two separate worlds

Page 98: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop: Expressions

Expressions see two separate worlds• Objects in Swift frames only for Swift expressions

Page 99: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop: Expressions

Expressions see two separate worlds• Objects in Swift frames only for Swift expressions

• Two namespaces for your results

Page 100: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop: Expressions

Expressions see two separate worlds• Objects in Swift frames only for Swift expressions

• Two namespaces for your results

- $0, $1, … for Objective-C

- $R0, $R1, … for Swift

Page 101: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop: Expressions

Page 102: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop: Expressions

(lldb) f frame #0: 0x0000000100005de0 WWDC14`-[CocoaClass description](self=0x000000010040e940, _cmd=0x00007fff8c7eaf49) + 16 at CocoaClass.m:19

Page 103: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop: Expressions

(lldb) f frame #0: 0x0000000100005de0 WWDC14`-[CocoaClass description](self=0x000000010040e940, _cmd=0x00007fff8c7eaf49) + 16 at CocoaClass.m:19

Page 104: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop: Expressions

(lldb) f frame #0: 0x0000000100005de0 WWDC14`-[CocoaClass description](self=0x000000010040e940, _cmd=0x00007fff8c7eaf49) + 16 at CocoaClass.m:19

(lldb) p self CocoaClass *) $0 = 0x000000010040e940 (lldb) |

Page 105: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop: Expressions

(lldb) f frame #0: 0x0000000100005de0 WWDC14`-[CocoaClass description](self=0x000000010040e940, _cmd=0x00007fff8c7eaf49) + 16 at CocoaClass.m:19

(lldb) p self CocoaClass *) $0 = 0x000000010040e940 (lldb) |

(lldb) f frame #0: 0x0000000100004a1f WWDC14`WWDC14.play_with_usecocoa () -> () + 95 at usecocoa.swift:6

Page 106: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop: Expressions

(lldb) f frame #0: 0x0000000100005de0 WWDC14`-[CocoaClass description](self=0x000000010040e940, _cmd=0x00007fff8c7eaf49) + 16 at CocoaClass.m:19

(lldb) p self CocoaClass *) $0 = 0x000000010040e940 (lldb) |

(lldb) f frame #0: 0x0000000100004a1f WWDC14`WWDC14.play_with_usecocoa () -> () + 95 at usecocoa.swift:6

Page 107: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop: Expressions

(lldb) f frame #0: 0x0000000100005de0 WWDC14`-[CocoaClass description](self=0x000000010040e940, _cmd=0x00007fff8c7eaf49) + 16 at CocoaClass.m:19

(lldb) p self CocoaClass *) $0 = 0x000000010040e940 (lldb) |

(lldb) f frame #0: 0x0000000100004a1f WWDC14`WWDC14.play_with_usecocoa () -> () + 95 at usecocoa.swift:6

(lldb) p (Class) [$0 class] error: <REPL>:1:9: error: anonymous closure argument not contained in a closure (Class)[$0 class]

^ <REPL>:1:12: error: expected ',' separator (Class) [$0 class]

^

Page 108: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop: Expressions

Page 109: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Language can be changed

Objective-C Interop: Expressions

Page 110: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Language can be changed

Objective-C Interop: Expressions

(lldb) expr -l objc++ -- (Class)[$0 class] (Class) $2 = CocoaClass

Page 111: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Language can be changed

Objective-C Interop: Expressions

(lldb) expr -l objc++ -- (Class)[$0 class] (Class) $2 = CocoaClass

Page 112: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Language can be changed• But locals will not be available

Objective-C Interop: Expressions

(lldb) expr -l objc++ -- (Class)[$0 class] (Class) $2 = CocoaClass

Page 113: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop: po

po honors most native experience: • Swift objects display using formatters

• Objective-C objects use -description

Page 114: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop

class MyObject: NSObject { var myInt = 1 override var description: String! {

return "Hello Swift subclass. myInt = \(myInt)" }

}

Page 115: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop

class MyObject: NSObject { var myInt = 1 override var description: String! {

return "Hello Swift subclass. myInt = \(myInt)" }

}po uses formatters—ignores description

Page 116: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop

class MyObject: NSObject { var myInt = 1 override var description: String! {

return "Hello Swift subclass. myInt = \(myInt)" }

}po uses formatters—ignores description

(lldb) po object 0x0000000100700ea0 (ObjectiveC.NSObject = {}, myInt = 1)

Page 117: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop

class MyObject: NSObject { var myInt = 1 override var description: String! {

return "Hello Swift subclass. myInt = \(myInt)" }

}po uses formatters—ignores description

What if I want to use my description property?

(lldb) po object 0x0000000100700ea0 (ObjectiveC.NSObject = {}, myInt = 1)

Page 118: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop

(lldb) po object 0x0000000100700ea0 (ObjectiveC.NSObject = {}, myInt = 1)

(lldb) expr -l objc++ -O -- (id)0x0000000100700ea0

Page 119: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop

(lldb) po object 0x0000000100700ea0 (ObjectiveC.NSObject = {}, myInt = 1)

(lldb) expr -l objc++ -O -- (id)0x0000000100700ea0

Page 120: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop

(lldb) po object 0x0000000100700ea0 (ObjectiveC.NSObject = {}, myInt = 1)

(lldb) expr -l objc++ -O -- (id)0x0000000100700ea0

Page 121: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop

(lldb) po object 0x0000000100700ea0 (ObjectiveC.NSObject = {}, myInt = 1)

(lldb) expr -l objc++ -O -- (id)0x0000000100700ea0

Page 122: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop

(lldb) po object 0x0000000100700ea0 (ObjectiveC.NSObject = {}, myInt = 1)

(lldb) expr -l objc++ -O -- (id)0x0000000100700ea0

Page 123: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Objective-C Interop

(lldb) po object 0x0000000100700ea0 (ObjectiveC.NSObject = {}, myInt = 1)

(lldb) expr -l objc++ -O -- (id)0x0000000100700ea0

Hello Swift subclass. myInt = 1

Page 124: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Stepping

Stepping around Swift code • Protocols

• Closures

Page 125: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

8 func useCreature(c: Creature) {9 c.speak()10 }

Stepping: Protocols

Page 126: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

8 func useCreature(c: Creature) {9 c.speak()10 }

Stepping: Protocols

9 c.speak()

Page 127: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

8 func useCreature(c: Creature) {9 c.speak()10 }

Stepping: Protocols

9 c.speak() class Cat: Creature { func speak() { println("Meow. Purr.") }

Page 128: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

8 func useCreature(c: Creature) {9 c.speak()10 }

Stepping: Protocols

9 c.speak() class Cat: Creature { func speak() { println("Meow. Purr.") }

func useCreature(c: Creature) { c.speak() }

Page 129: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

8 func useCreature(c: Creature) {9 c.speak()10 }

Stepping: Protocols

Stepping inside protocol implementations involves a layer of dynamic dispatch (“protocol witness”)

Stepping out of the implementation steps out of the witness

9 c.speak() class Cat: Creature { func speak() { println("Meow. Purr.") }

func useCreature(c: Creature) { c.speak() }

Page 130: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

13 takeClosure(3) {14 return $0 < 515 }

Stepping: Breakpoint In a Closure

Page 131: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

13 takeClosure(3) {14 return $0 < 515 }

Stepping: Breakpoint In a Closure

14 return $0 < 5

Page 132: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

13 takeClosure(3) {14 return $0 < 515 }

Stepping: Breakpoint In a Closure

14 return $0 < 5

Page 133: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

13 takeClosure(3) {14 return $0 < 515 }

Stepping: Breakpoint In a Closure

14 return $0 < 5

Page 134: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

13 takeClosure(3) {14 return $0 < 515 }

Stepping: Breakpoint In a Closure

14 return $0 < 5

Page 135: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Data Formatters for Swift Objects

Page 136: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Data Formatters for Swift Objects

LLDB data formatters improve data display • Hide implementation details

• Focus on what matters

Page 137: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Data Formatters for Swift Objects

LLDB data formatters improve data display • Hide implementation details

• Focus on what matters

LLDB formats Swift library types automatically

Page 138: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Data Formatters for Swift Objects

LLDB data formatters improve data display • Hide implementation details

• Focus on what matters

LLDB formats Swift library types automatically

You can roll your own • Just like for C++/Objective-C

Page 139: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Data Formatters for Swift Objects15 struct Address { 16 var name: String17 var city: String18 var zip: Int19 var state: State20 }

Page 140: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Data Formatters for Swift Objects

(lldb)po enrico { Name = "Enrico Granata" City = "Mountain View" Zip = 94043 State = California }

15 struct Address { 16 var name: String17 var city: String18 var zip: Int19 var state: State20 }

Page 141: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Data Formatters for Swift Objects

(lldb)po enrico { Name = "Enrico Granata" City = "Mountain View" Zip = 94043 State = California } (lldb) type summary add -s "${var.name} \n ${var.city} \n ${var.zip}, ${var.state}" WWDC14.Address

15 struct Address { 16 var name: String17 var city: String18 var zip: Int19 var state: State20 }

Page 142: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

(lldb) type summary add -s "${var.name} \n ${var.city} \n ${var.zip}, ${var.state}" WWDC14.Address

15 struct Address { 16 var name: String17 var city: String18 var zip: Int19 var state: State20 }

Data Formatters for Swift Objects

(lldb)po enrico { Name = "Enrico Granata" City = "Mountain View" Zip = 94043 State = California }

Page 143: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

(lldb) type summary add -s "${var.name} \n ${var.city} \n ${var.zip}, ${var.state}" WWDC14.Address

15 struct Address { 16 var name: String17 var city: String18 var zip: Int19 var state: State20 }

Data Formatters for Swift Objects

(lldb)po enrico { Name = "Enrico Granata" City = "Mountain View" Zip = 94043 State = California }

Page 144: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

(lldb) type summary add -s "${var.name} \n ${var.city} \n ${var.zip}, ${var.state}" WWDC14.Address

15 struct Address { 16 var name: String17 var city: String18 var zip: Int19 var state: State20 }

Data Formatters for Swift Objects

(lldb)po enrico { Name = "Enrico Granata" City = "Mountain View" Zip = 94043 State = California }

Page 145: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

(lldb) type summary add -s "${var.name} \n ${var.city} \n ${var.zip}, ${var.state}" WWDC14.Address

15 struct Address { 16 var name: String17 var city: String18 var zip: Int19 var state: State20 }

Data Formatters for Swift Objects

(lldb)po enrico { Name = "Enrico Granata" City = "Mountain View" Zip = 94043 State = California }

Page 146: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

(lldb) type summary add -s "${var.name} \n ${var.city} \n ${var.zip}, ${var.state}" WWDC14.Address

15 struct Address { 16 var name: String17 var city: String18 var zip: Int19 var state: State20 }

Data Formatters for Swift Objects

(lldb)po enrico { Name = "Enrico Granata" City = "Mountain View" Zip = 94043 State = California }

Page 147: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

15 struct Address { 16 var name: String17 var city: String18 var zip: Int19 var state: State20 }

Data Formatters for Swift Objects

(lldb)po enrico { Name = “Enrico Granata” City = “Mountain View” Zip = 94043 State = California }

(lldb)po enrico "Enrico Granata" "Mountain View" 94043, California

Page 148: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Data Formatters for Swift Objects

Page 149: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Data Formatters for Swift Objects

Caveats

Page 150: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Data Formatters for Swift Objects

Caveats• Type name must be fully qualified (include module)

Page 151: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Data Formatters for Swift Objects

Caveats• Type name must be fully qualified (include module)

• In Python, use SBValue.GetSummary()

Page 152: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Data Formatters for Swift Objects

Caveats• Type name must be fully qualified (include module)

• In Python, use SBValue.GetSummary()

- Except for enums!

Page 153: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

UniquenessMyApp

Page 154: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Uniqueness

Foo.framework

MyClassWithANiceName

MyApp

Page 155: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Uniqueness

Foo.framework

MyClassWithANiceName

MyApp

Bar.framework

MyClassWithANiceName

Page 156: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Uniqueness

Page 157: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Uniqueness

Swift provides uniqueness • Of function overloads

• Of classes in different frameworks

Page 158: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Uniqueness

Swift provides uniqueness • Of function overloads

• Of classes in different frameworks

Mangled names are the way

Page 159: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Uniqueness

Module2.swiftclass MyClass {…}

Module1.swiftclass MyClass {…}

Page 160: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Uniqueness

Module2.swiftclass MyClass {…}

Swift Compiler

Module1.swiftclass MyClass {…}

Page 161: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Uniqueness

Module2.swiftclass MyClass {…}

Swift Compiler

Module1.swiftclass MyClass {…}

Page 162: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Uniqueness

Module2.swiftclass MyClass {…}

Swift Compiler

Module1_TtC7Module17MyClass

Module1.swiftclass MyClass {…}

Page 163: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Uniqueness

Swift Compiler

Module1_TtC7Module17MyClass

Module2_TtC7Module27MyClass

Module1.swiftclass MyClass {…}

Module2.swiftclass MyClass {…}

Page 164: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Uniqueness

Page 165: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Uniqueness

What if you encounter a mangled name?

Page 166: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Uniqueness

What if you encounter a mangled name?

Enter swift-demangle!

Page 167: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Uniqueness

What if you encounter a mangled name?

Enter swift-demangle!$ xcrun swift-demangle _TF5MyApp6myFuncFTSiSi_TSS_

Page 168: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Uniqueness

What if you encounter a mangled name?

Enter swift-demangle!$ xcrun swift-demangle _TF5MyApp6myFuncFTSiSi_TSS_

_TF5MyApp6myFuncFTSiSi_TSS_ ---> MyApp.myFunc (Swift.Int, Swift.Int) -> (Swift.String)

Page 169: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Modules

MyApp.swift

Page 170: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Modules

MyApp.swift

Swift Compiler

Page 171: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Modules

Swift CompilerMyApp.swift

Page 172: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Modules

Swift Compiler

MyApp.swift

MyApp Module

MyApp.app

Page 173: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Modules

Swift Compiler

MyApp.swift LLDB

Swift Compiler

MyApp Module

MyApp.app

Page 174: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Modules

Swift Compiler

MyApp.swift LLDB

Swift CompilerMyApp Module

MyApp.app

Page 175: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Modules

Modules store the compiler’s truth • No need to reconstruct types from DWARF

• No loss of information

Swift Compiler

MyApp.swift LLDB

Swift CompilerMyApp Module

MyApp.app

Page 176: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Modules

Modules store the compiler’s truth • No need to reconstruct types from DWARF

• No loss of information

LLDB can see types and functions your program doesn’t use • Yes, generics too!

Swift Compiler

MyApp.swift LLDB

Swift CompilerMyApp Module

MyApp.app

Page 177: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Summary

Page 178: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Summary

Choose your language

Page 179: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Summary

Choose your language

LLDB provides helpful investigation tools

Page 180: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Summary

Choose your language

LLDB provides helpful investigation tools

We talked about: • Swift types in LLDB

• Stepping

• Data formatters

• Modules

Page 181: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Summary

Choose your language

LLDB provides helpful investigation tools

We talked about: • Swift types in LLDB

• Stepping

• Data formatters

• Modules

Your feedback matters!

Page 182: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

More Information

Dave DeLong Developer Tools Evangelist [email protected]

Documentation Apple Developer Forums http://devforums.apple.com

LLDB Website http://lldb.llvm.org

Page 183: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Related Sessions

• Debugging in Xcode 6 Marina Wednesday 10:15AM

• Introduction to LLDB and the Swift REPL Mission Thursday 10:15AM

• Advanced Swift Presidio Thursday 11:30AM

Page 184: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction

Labs

• Swift Lab Tools Lab A Friday 9:00AM

• LLDB and Xcode Debugging Lab Tools Lab B Friday 9:00AM

Page 185: Advanced Swift Debugging in LLDB - Apple Inc. · Advanced Swift Debugging in LLDB Debugging in a Swift world Session 410 Enrico Granata Debugger Engineer Tools. Introduction. Introduction