I have a DTrace probe catching calls to a function, and one of the function’s arguments is a CFStringRef. This is private structure that holds a pointer to a unicode string. But the CFStringRef is not itself a char*, so normal DTrace methods like copyinstr() just return ?cp?, which isn’t exactly helpful.
So how can I print out the string in the DTrace action?
As far as I know, there is not built-in support for this kind of thing. Usually a library would publish a probe that decodes the string for you (as Brad mentions). So since in your case you can’t modify the library, you’ll need to use the
pidprovider and hook into a user function, and decode it yourself.The solution (which is very similar to the approach you would use in C++ to dump a
std::string) is to dump out the pointer which is stored at an 2 word offset from the baseCFStringRefpointer. Note that since aCFStringcan store strings internally in a variety of formats and representations, this is subject to change.Given the trivial test application:
The following
dtracescript will print the string value of the first argument, assuming it is aCFStringRef:And the output will be something like:
Simply uncomment the right
typedefdepending on whether you are running against a 32-bit or 64-bit binary. I have tested this against both architectures on 10.6 and it works fine.