Add breakpoints and install handlers

心已入冬 提交于 2020-01-05 04:37:08

问题


My high-level goal is something like this:

void print_backtrace() {
    void *callstack[128];
    int framesC = backtrace(callstack, sizeof(callstack));
    printf("backtrace() returned %d addresses\n", framesC);
    char** strs = backtrace_symbols(callstack, framesC);
    for(int i = 0; i < framesC; ++i) {
        if(strs[i])
            printf("%s\n", strs[i]);
        else
            break;
    }
    free(strs);
}

install_breakpoint_handler("__NSAutoreleaseNoPool", print_backtrace);

So, each time the __NSAutoreleaseNoPool function breakpoint is catched, print_backtrace should be called. (All within the same binary. I'm not trying to catch the breakpoint of separate processes.)

I guess I can somehow do this via ptrace. Is there some easy-to-use and lightweight library?

Currently I'm searching for a MacOSX solution, but cross-platform would be nice of course.


回答1:


I just found one lib (I even had used it a few years ago...): mach_override

I also found this debuglib but didn't tried.

See here for a demonstration for __NSAutoreleaseNoPool: It automatically executes print_backtrace.



来源:https://stackoverflow.com/questions/21847394/add-breakpoints-and-install-handlers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!