I want to write a dtrace probe that would match a function with std::string argument and print the string's content:

void func(std::string some) {
    /* some code here */
}

I tried to implement the probe like this:

pid$target::func(std??string):entry
{
    this->str = *(uintptr_t*)copyin(arg1, sizeof(char*));
    printf("arg1 %s", copyinstr(this->str));
}

Unfortunately, this doesn't work for me, dtrace reports it detected invalid address.. Also, there is another problem here - string in libstdc++ uses copy on write, so just coping a pointer is not enough here. Does anybody know how to do it? I'm using dtrace on mac os x.

有帮助吗?

解决方案

I was able to find a working solution by myself. The probe in my question has a stupid mistake - arg0 should be copied instead of arg1. So the working probe is:

pid$target::func(*):entry 
{ 
    this->str = *((uintptr_t*)copyin(arg0, sizeof(void*)));
    printf("darg %s", copyinstr(this->str));
}

On the other hand for member function arg1 should be used:

class some {
    public:
        void func(const std::string arg) {
            std::cout << "arg " << arg << std::endl;
        }
};

Probe for function some::func should look like this:

pid$target::some??func(*):entry 
{ 
    this->str = *((uintptr_t*)copyin(arg1, sizeof(void*)));
    printf("darg %s", copyinstr(this->str));
}

This works for both libc++ and libstdc++ std::string classes. It even works if reference to string is used.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top