forked from johnno1962/SwiftTrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwiftSwizzle.swift
542 lines (468 loc) · 20.1 KB
/
SwiftSwizzle.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//
// SwiftSwizzle.swift
// SwiftTrace
//
// Created by John Holdsworth on 20/04/2020.
// Copyright © 2020 John Holdsworth. All rights reserved.
//
// Repo: https://github.com/johnno1962/SwiftTrace
// $Id: //depot/SwiftTrace/SwiftTrace/SwiftSwizzle.swift#53 $
//
// Mechanics of Swizzling Swift
// ============================
//
import Foundation
#if SWIFT_PACKAGE
import SwiftTraceGuts
#endif
extension SwiftTrace {
public typealias SIMP = SwiftMeta.SIMP
/**
Hook to intercept all trace output
*/
public static var logOutput: (String, UnsafeRawPointer?, Int) -> () = {
print($0, terminator: "")
_ = ($1, $2) // self, indent
}
/** Used for real time filtering */
static var includeFilter: NSRegularExpression?
static var excludeFilter: NSRegularExpression?
static var filterGeneration = 0
/** Used to gather order in which methods are called */
static var firstCalled: Swizzle?
static var lastCalled: Swizzle?
@objc open class var traceFilterInclude: String? {
get { return includeFilter?.pattern }
set(pattern) {
includeFilter = pattern != nil && pattern != "" ?
NSRegularExpression(regexp: pattern!) : nil
filterGeneration += 1
}
}
@objc open class var traceFilterExclude: String? {
get { return excludeFilter?.pattern }
set(pattern) {
excludeFilter = pattern != nil && pattern != "" ?
NSRegularExpression(regexp: pattern!) : nil
filterGeneration += 1
}
}
/// Map trampolines back to the original implementation swizzled
/// - Parameter trampoline: a function entry point
/// - Returns: original implementation swizzled
open class func swizzled(forTrampoline: UnsafeMutableRawPointer)
-> UnsafeMutableRawPointer? {
var implementation = forTrampoline
while let swizzle = findSwizzleOf(implementation) as? Swizzle {
implementation = autoBitCast(swizzle.implementation)
}
return implementation != forTrampoline ? implementation : nil
}
/**
Instances used to store information about a patch on a method
*/
open class Swizzle: NSObject {
/** string representing Swift or Objective-C method to user */
public let signature: String
/** trace that resulted in this Swizzle */
let trace: SwiftTrace
/** pointer to original function implementing method */
open var implementation: IMP
/** vtable slot patched for unpatching */
var vtableSlot: UnsafeMutablePointer<SIMP>?
/** Original objc method swizzled */
let objcMethod: Method?
let objcClass: AnyClass?
/** Total time spent in this method */
var totalElapsed: TimeInterval = 0.0
/** Number of times this method has beeen called */
var invocationCount = 0
/** This Sizzle has been swizzled */
var reSwizzled = false
/** Closure that can be called instead of original implementation */
public let nullImplmentation: nullImplementationType?
/** lazy calculation of shouldTrace */
var currentGeneration = 0
var currentShouldTrace = true
/** Used to gather linked list of call order */
var nextCalled: Swizzle?
/** is this method involved in Lifetime tracing? */
var isLifetime: Bool { return false }
/** Always trace allocations & deallocations unless explicitly filtered out */
open func notFilteredOut() -> Bool {
if currentGeneration != SwiftTrace.filterGeneration {
currentGeneration = SwiftTrace.filterGeneration
currentShouldTrace =
SwiftTrace.includeFilter?.matches(signature) != false &&
SwiftTrace.excludeFilter?.matches(signature) != true
}
return currentShouldTrace
}
/**
Class used to create a specific "Invocation" of the "Swizzle" on entry
*/
open var invocationFactory: Invocation.Type {
return defaultInvocationFactory
}
/**
The inner invocation instance on the stack of the current thread.
*/
open func invocation() -> Invocation! {
return Invocation.current
}
/**
designated initialiser
- parameter name: string representing method being traced
- parameter vtableSlot: pointer to vtable slot patched
- parameter objcMethod: pointer to original Method patched
- parameter replaceWith: implementation to replace that of class
*/
public required init?(name signature: String,
vtableSlot: UnsafeMutablePointer<SIMP>? = nil,
objcMethod: Method? = nil, objcClass: AnyClass? = nil,
original: OpaquePointer? = nil,
replaceWith: nullImplementationType? = nil) {
self.trace = SwiftTrace.lastSwiftTrace
self.signature = signature
self.vtableSlot = vtableSlot
self.objcMethod = objcMethod
self.objcClass = objcClass
if let vtableSlot = vtableSlot {
implementation = autoBitCast(vtableSlot.pointee)
}
else if let objcMethod = objcMethod {
implementation = method_getImplementation(objcMethod)
} else {
implementation = original!
}
nullImplmentation = replaceWith
}
/** Called from assembly code on entry to Swizzled method */
static var onEntry: @convention(c) (_ swizzle: Swizzle, _ returnAddress: UnsafeRawPointer,
_ stackPointer: UnsafeMutablePointer<UInt64>) -> IMP? = {
(swizzle, returnAddress, stackPointer) -> IMP? in
let threadLocal = ThreadLocal.current()
let invocation = swizzle.invocationFactory
.init(stackDepth: threadLocal.invocationStack.count, swizzle: swizzle,
returnAddress: returnAddress, stackPointer: stackPointer)
invocation.saveLevelsTracing = threadLocal.levelsTracing
threadLocal.invocationStack.append(invocation)
swizzle.onEntry(stack: &invocation.entryStack.pointee)
if invocation.shouldDecorate {
threadLocal.levelsTracing -= 1
}
return swizzle.nullImplmentation != nil ?
autoBitCast(swizzle.nullImplmentation) : swizzle.implementation
}
/** Called from assembly code when Patched method returns */
static var onExit: @convention(c) () -> UnsafeRawPointer = {
let threadLocal = ThreadLocal.current()
let invocation = threadLocal.invocationStack.last!
invocation.exitStack.pointee.frame.lr = invocation.returnAddress
invocation.swizzle.onExit(stack: &invocation.exitStack.pointee)
threadLocal.levelsTracing = invocation.saveLevelsTracing
return threadLocal.invocationStack.removeLast().returnAddress
}
/**
Return a unique pointer to a trampoline that will callback
the oneEntry() and onExit() method in this class
*/
open lazy var forwardingImplementation: SIMP = {
/* create trampoline */
let impl = imp_implementationForwardingToTracer(autoBitCast(self),
autoBitCast(Swizzle.onEntry), autoBitCast(Swizzle.onExit))
trace.activeSwizzles[impl] = self // track Swizzles by trampoline and retain them
var previousTrace: SwiftTrace? = trace.previousSwiftTrace
while previousTrace != nil {
if let previous = previousTrace!.activeSwizzles[implementation] {
previous.reSwizzled = true
}
previousTrace = previousTrace!.previousSwiftTrace
}
return autoBitCast(impl)
}()
/**
method called before trampoline enters the target "Swizzle"
*/
open func onEntry(stack: inout EntryStack) {
if let invocation = invocation() {
_ = objcAdjustStret(invocation: invocation, isReturn: false,
intArgs: &invocation.entryStack.pointee.intArg1)
if nextCalled == nil && lastCalled != self {
lastCalled?.nextCalled = self
lastCalled = self
if firstCalled == nil {
firstCalled = self
}
}
let shouldPrint = invocation.shouldDecorate && notFilteredOut()
if shouldPrint || isLifetime,
let decorated = entryDecorate(stack: &stack), shouldPrint {
ThreadLocal.current().caller()?.subLogged = true
let indent = String(repeating: SwiftTrace.traceIndent,
count: invocation.stackDepth)
logOutput("\(subLogging() ? "\n" : "")\(indent)\(decorated)",
autoBitCast(invocation.swiftSelf), invocation.stackDepth)
}
}
}
/**
decorate funcition signature with argument values
*/
open func entryDecorate(stack: inout EntryStack) -> String? {
return signature
}
/**
Is this method being called from one in the middle of logging
*/
open func subLogging() -> Bool {
return ThreadLocal.current().caller()?.subLogged == true
}
/**
method called after trampoline exits the target "Swizzle"
*/
open func onExit(stack: inout ExitStack) {
if let invocation = invocation() {
let elapsed = Invocation.usecTime() - invocation.timeEntered
let shouldPrint = invocation.shouldDecorate && notFilteredOut()
if shouldPrint || isLifetime,
let returnValue = exitDecorate(stack: &stack), shouldPrint {
logOutput("""
\(invocation.subLogged ? """
\n\(String(repeating: " ",
count: invocation.stackDepth))<-
""" : objcMethod != nil ? " ->" : "") \
\(returnValue)\(String(format: SwiftTrace.timeFormat,
elapsed * 1000.0))\(subLogging() ? "" : "\n")
""", autoBitCast(invocation.swiftSelf), invocation.stackDepth)
}
totalElapsed += elapsed
invocationCount += 1
}
}
/**
Provide the return value
*/
open func exitDecorate(stack: inout ExitStack) -> String? {
return signature
}
/**
Use NSMethodSignature to know argument types of method
*/
lazy var methodSignature: Any? = {
return method_getSignature(self.objcMethod!)
}()
/**
Special handling of methods that return a struct that
doesn't fit into registers on x86_64 architectures.
Argument register for `self` is offset by one.
*/
open func objcAdjustStret(invocation: Invocation, isReturn: Bool,
intArgs: UnsafePointer<intptr_t>) -> Bool {
// Is method returning a struct?
// If so there is an implicit argument which is the address
// to write the struct into (even if the registers are used.)
#if arch(arm64)
return false
#else
guard objcMethod != nil && !isReturn else { return false }
let returnType = methodSignature == nil ? "UNDECODABLE" :
String(cString: sig_returnType(methodSignature!))
let isStret = returnType.hasPrefix("{") &&
returnType.hasSuffix("}") && returnType[.end-4] != "="
if isStret && !isReturn {
invocation.swiftSelf = intArgs[1]
}
return isStret
#endif
}
/**
Remove this swizzle
*/
open func remove() {
if let vtableSlot = vtableSlot {
vtableSlot.pointee = autoBitCast(implementation)
}
else if let objcMethod = objcMethod {
method_setImplementation(objcMethod, implementation)
}
}
/**
Remove all patches recursively
*/
open func removeAll() {
(SwiftTrace.originalSwizzle(for: implementation) ?? self).remove()
}
/** find "self" for the current invocation */
open func getSelf<T>(as: T.Type = T.self) -> T {
return autoBitCast(invocation().swiftSelf)
}
/** find Class for the current invocation */
open func getClass() -> AnyClass {
let id: AnyObject = autoBitCast(invocation().swiftSelf)
return object_isClass(id) ? autoBitCast(id) : object_getClass(id)!
}
/** pointer to memory for return of struct */
open func structReturn<T>(as: T.Type = T.self) -> UnsafeMutablePointer<T> {
return UnsafeMutablePointer(cast: invocation().structReturn!)
}
/** convert arguments & return results to a specifi type */
open func rebind<IN,OUT>(_ pointer: UnsafeMutablePointer<IN>,
to: OUT.Type = OUT.self) -> UnsafeMutablePointer<OUT> {
return UnsafeMutablePointer(cast: pointer)
}
/**
Represents a specific call to a member function on the "ThreadLocal" stack
*/
public class Invocation {
/** Time call was started */
public let timeEntered: Double
/** Number of calls above this on the stack of the current thread */
public let stackDepth: Int
/** "Swizzle" related to this call */
public let swizzle: Swizzle
/** signature with arguments substituted in */
public var decorated: String?
/** arguments parsed out of invocation */
public var arguments = [Any]()
/** Original return address of call to trampoline */
public let returnAddress: UnsafeRawPointer
/** levelsTracing on entry for restore */
public var saveLevelsTracing = 0
/** Offset through argument frame of saved registers */
public var intArgumentOffset = 0
/** Offset through return value frame of saved registers */
public var floatArgumentOffset = 0
/** Has a trace taken place during this invocation */
public var subLogged = false
/** This invocation qualifies for tracing */
lazy public var shouldDecorate: Bool = {
let threadLocal = ThreadLocal.current()
if threadLocal.describing {
return false
}
if threadLocal.levelsTracing > 0 && !swizzle.reSwizzled {
return true
}
if (swizzle.trace.instanceFilter == nil ||
swizzle.trace.instanceFilter == swiftSelf) &&
(swizzle.trace.classFilter == nil ||
swizzle.trace.classFilter === swizzle.getClass()) {
ThreadLocal.current().levelsTracing = swizzle.trace.subLevels
return true
}
return false
}()
/** Architecture depenent place on stack where arguments stored */
public let entryStack: UnsafeMutablePointer<EntryStack>
public var exitStack: UnsafeMutablePointer<ExitStack> {
return autoBitCast(entryStack)
}
/** copy of struct return register in case function throws */
public var structReturn: UnsafeMutableRawPointer? = nil
/** "self" for method invocations */
public var swiftSelf: intptr_t
/** for use relaying data from entry to exit */
public var userInfo: Any?
public var numberLive = 0
/**
micro-second precision time.
*/
static public func usecTime() -> Double {
var tv = timeval()
gettimeofday(&tv, nil)
return Double(tv.tv_sec) + Double(tv.tv_usec)/1_000_000.0
}
/**
designated initialiser
- parameter stackDepth: number of calls that have been made on the stack
- parameter swizzle: associated Swizzle instance
- parameter returnAddress: adress in process trampoline was called from
- parameter stackPointer: stack pointer of thread with saved registers
*/
public required init(stackDepth: Int, swizzle: Swizzle, returnAddress: UnsafeRawPointer,
stackPointer: UnsafeMutablePointer<UInt64>) {
timeEntered = Invocation.usecTime()
self.stackDepth = stackDepth
self.swizzle = swizzle
self.returnAddress = returnAddress
self.entryStack = autoBitCast(stackPointer)
self.swiftSelf = swizzle.objcMethod != nil ?
entryStack.pointee.intArg1 : entryStack.pointee.swiftSelf
self.structReturn = UnsafeMutableRawPointer(bitPattern: entryStack.pointee.structReturn)
}
/**
The inner invocation instance on the current thread.
*/
public static var current: Invocation! {
return ThreadLocal.current().invocationStack.last
}
}
/**
Class implementing thread local storage to arrange a call stack
*/
public class ThreadLocal {
private static var keyVar: pthread_key_t = 0
private static var pthreadKey: pthread_key_t = {
let ret = pthread_key_create(&keyVar, {
#if os(Linux) || os(Android)
Unmanaged<ThreadLocal>.fromOpaque($0!).release()
#else
Unmanaged<ThreadLocal>.fromOpaque($0).release()
#endif
})
if ret != 0 {
NSLog("Could not pthread_key_create: %s", strerror(ret))
}
return keyVar
}()
/**
The stack of Invocations logged on this thread
*/
public var invocationStack = [Invocation]()
/**
currently describing an instance
*/
public var describing = false
/**
currently describing an instance
*/
public var levelsTracing = 0
/**
Returns an instance of ThreadLocal specific to the current thread
*/
static public func current() -> ThreadLocal {
let keyVar = ThreadLocal.pthreadKey
if let existing = pthread_getspecific(keyVar) {
return Unmanaged<ThreadLocal>.fromOpaque(existing).takeUnretainedValue()
}
else {
let unmanaged = Unmanaged.passRetained(ThreadLocal())
let ret = pthread_setspecific(keyVar, unmanaged.toOpaque())
if ret != 0 {
NSLog("Could not pthread_setspecific: %s", strerror(ret))
}
return unmanaged.takeUnretainedValue()
}
}
static public func whileDescribing(block: () -> ()) {
let threadLocal = current()
let saveDescribing = threadLocal.describing
threadLocal.describing = true
block()
threadLocal.describing = saveDescribing
}
public func caller() -> Invocation? {
var caller = invocationStack.count - 2
while caller >= 0 {
let invocation = invocationStack[caller]
if invocation.swizzle.notFilteredOut() {
return invocation
}
caller -= 1
}
return nil
}
}
}
}