Coder Social home page Coder Social logo

yulingtianxia / blocktracker Goto Github PK

View Code? Open in Web Editor NEW
78.0 6.0 10.0 756 KB

Tracking block args of Objective-C method based on BlockHook

Home Page: http://yulingtianxia.com/blog/2018/03/31/Track-Block-Arguments-of-Objective-C-Method/

License: MIT License

Objective-C 72.96% Ruby 1.67% C 25.37%
blocktracker carthage cocoapods blockhook hookblock

blocktracker's Introduction

BlockTracker

CI Status Carthage compatible GitHub release Twitter Follow

BlockTracker

BlockTracker can track block arguments of a method. It's based on BlockHook.

๐Ÿ“š Article

่ฟฝ่ธช Objective-C ๆ–นๆณ•ไธญ็š„ Block ๅ‚ๆ•ฐๅฏน่ฑก

๐ŸŒŸ Features

  • Easy to use.
  • Keep your code clear.
  • Let you modify return value and arguments.
  • Trace all block args of method.
  • Trace all NSMallocBlock.
  • Self-managed trackers.
  • Support CocoaPods & Carthage.

๐Ÿ”ฎ Example

The sample project "BlockTrackerSample" just only support iOS platform.

๐Ÿ’ How to use

Track blocks in method

You can track blocks in arguments. This method returns a BTTracker instance for more control. You can stop a BTTracker when you don't want to track it anymore.

__unused BTTracker *tracker = [self bt_trackBlockArgOfSelector:@selector(performBlock:) callback:^(BHInvocation * _Nonnull invocation) {
    switch (invocation.mode) {
        case BlockHookModeBefore:
            NSLog(@"Before block:%@, mangleName:%@", invocation.token.block, invocation.token.mangleName);
            break;
        case BlockHookModeAfter:
            NSLog(@"After block:%@, mangleName:%@", invocation.token.block, invocation.token.mangleName);
            objc_setAssociatedObject(invocation.token, @"invoked", @YES, OBJC_ASSOCIATION_RETAIN);
            break;
        case BlockHookModeDead:
            NSLog(@"Block Dead! mangleName:%@", invocation.token.mangleName);
            BOOL invoked = [objc_getAssociatedObject(invocation.token, @"invoked") boolValue];
            if (!invoked) {
                NSLog(@"Block Not Invoked Before Dead! %@", invocation.token.mangleName);
            }
            break;
        default:
            break;
    }
}];
    
// invoke blocks
NSString *word = @"I'm a block";
[self performBlock:^{
    NSLog(@"%@", word);
}];
// stop tracker in future
//    [tracker stop];
// blocks will die

- (void)performBlock:(void(^)(void))block {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), block);
}

@end

Here is the log:

Hook Block Arg mangleName:__42-[BlockTrackerSampleTests testTrackMethod]_block_invoke_2, in selector:performBlock:
Before block:<__NSMallocBlock__: 0x600000c71aa0>, mangleName:__42-[BlockTrackerSampleTests testTrackMethod]_block_invoke_2
I'm a block
After block:<__NSMallocBlock__: 0x600000c71aa0>, mangleName:__42-[BlockTrackerSampleTests testTrackMethod]_block_invoke_2
Block Dead! mangleName:__42-[BlockTrackerSampleTests testTrackMethod]_block_invoke_2

Track a batch of blocks.

setMallocBlockCallback(^(BHInvocation * _Nonnull invocation) {
    switch (invocation.mode) {
        case BlockHookModeBefore: {
            NSLog(@"Before block:%@, mangleName:%@", invocation.token.block, invocation.token.mangleName);
            break;
        }
        case BlockHookModeAfter: {
            NSLog(@"After block:%@, mangleName:%@", invocation.token.block, invocation.token.mangleName);
            objc_setAssociatedObject(invocation.token, @"invoked", @YES, OBJC_ASSOCIATION_RETAIN);
            break;
        }
        case BlockHookModeDead: {
            NSLog(@"Block Dead! mangleName:%@", invocation.token.mangleName);
            BOOL invoked = [objc_getAssociatedObject(invocation.token, @"invoked") boolValue];
            if (!invoked) {
                NSLog(@"Block Not Invoked Before Dead! %@", invocation.token.mangleName);
            }
            break;
        }
        default:
            break;
    }
});

๐Ÿ“ฒ Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate BlockTracker into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
target 'MyApp' do
	pod 'BlockTracker'
end

You need replace "MyApp" with your project's name.

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate BlockTracker into your Xcode project using Carthage, specify it in your Cartfile:

github "yulingtianxia/BlockTracker"

Run carthage update to build the framework and drag the built BlockTrackerKit.framework into your Xcode project.

Manual

Just drag source files in BlockTracker folder to your project.

โค๏ธ Contributed

  • If you need help or you'd like to ask a general question, open an issue.
  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป Author

yulingtianxia, [email protected]

๐Ÿ‘ฎ๐Ÿป License

BlockTracker is available under the MIT license. See the LICENSE file for more info.

blocktracker's People

Contributors

yulingtianxia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

blocktracker's Issues

Great Work

Please show article as soon as possible.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.