Automatically adding Enter/Exit Function Logs to a Project

后端 未结 5 914
攒了一身酷
攒了一身酷 2020-11-29 04:42

I have a 3rd party source code that I have to investigate. I want to see in what order the functions are called but I don\'t want to waste my time typing:

pr         


        
5条回答
  •  情话喂你
    2020-11-29 05:24

    You said "nor do I want to touch any source file"... fair game if you let a script do it for you?

    Run this on all your .cpp files

    sed 's/^{/{ENTRY/'
    

    So that it transforms them into this:

    void foo()
    {ENTRY
      // code here
    }
    

    Put this in a header that can be #included by every unit:

    #define ENTRY EntryRaiiObject obj ## __LINE__ (__FUNCTION__);
    
    struct EntryRaiiObject {
      EntryRaiiObject(const char *f) : f_(f) { printf("Entered into %s", f_); }
      ~EntryRaiiObject() { printf("Exited from %s", f_); }
      const char *f_;
    };
    

    You may have to get fancier with the sed script. You can also put the ENTRY macro anywhere else you want to probe, like some deeply nested inner scope of a function.

提交回复
热议问题