Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 390345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:57:20+00:00 2026-05-12T15:57:20+00:00

I have a DTrace probe catching calls to a function, and one of the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T15:57:20+00:00Added an answer on May 12, 2026 at 3:57 pm

    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 pid provider 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 base CFStringRef pointer. Note that since a CFString can store strings internally in a variety of formats and representations, this is subject to change.

    Given the trivial test application:

    #include <CoreFoundation/CoreFoundation.h>
    
    int mungeString(CFStringRef someString)
    {
        const char* str = CFStringGetCStringPtr(someString, kCFStringEncodingMacRoman);
        if (str)
            return strlen(str);
        else
            return 0;
    }
    
    int main(int argc, char* argv[])
    {
        CFStringRef data = CFSTR("My test data");
    
        printf("%u\n", mungeString(data));
    
        return 0;
    }
    

    The following dtrace script will print the string value of the first argument, assuming it is a CFStringRef:

    #!/usr/sbin/dtrace -s
    
    /*
        Dumps a CFStringRef parameter to a function,
        assuming MacRoman or ASCII encoding.
        The C-style string is found at an offset of
        2 words past the CFStringRef pointer.
        This appears to work in 10.6 in 32- and 64-bit
        binaries, but is an implementation detail that
        is subject to change.
    
        Written by Gavin Baker <gavinb.antonym.org>
    */
    
    #pragma D option quiet
    
    /* Uncomment for LP32 */
    /* typedef long ptr_t; */
    /* Uncomment for LP64 */
    typedef long long ptr_t;
    
    pid$target::mungeString:entry
    {
        printf("Called mungeString:\n");
        printf("arg0 = 0x%p\n",arg0);
    
        this->str = *(ptr_t*)copyin(arg0+2*sizeof(ptr_t), sizeof(ptr_t));
        printf("string addr = %p\n", this->str);
        printf("string val  = %s\n", copyinstr(this->str));
    
    }
    

    And the output will be something like:

    $ sudo dtrace -s dump.d -c ./build/Debug/dtcftest 
    12
    Called mungeString:
    arg0 = 0x2030
    string addr = 1fef
    string val  = My test data
    

    Simply uncomment the right typedef depending 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have data that has this kind of structure: $input = [ { animal: 'cat',
I have a char* in my DTrace script that I know points to a
I have a sendmail funciton that works for one recipient. If I pass something
have Googled the cr*p out of this one so apologies if the answer is
Have following kernel function: private static String programSource = __kernel void sampleKernel(__global float *Y,
Have some dates in my local Oracle 11g database that are in this format:
My Leopard system has dtrace built in. I also have Java 6 installed, using
Have a look at this picture alt text http://www.abbeylegal.com/downloads/2009-04-01/web%20part%20top%20line.jpg Does anyone know what css
Have a SomeLib.pro file that contains: CONFIG += debug TEMPLATE = lib TARGET =
Have a matrix report now that has Position, Hours and Wages for a location

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.