[dtrace-discuss] Re: Re: DTrace: How to print string in C++

Jonathan Haslam Jonathan.Haslam at Sun.COM
Fri Dec 1 04:15:05 PST 2006


> Do you mean C++ compiler will add a "this" pointer to each function?
> Or the changes is just in dtrace script?
> As far as I know,C++ compiler will do name mangling for all functions, 
> but it will ONLY add "this" pointer to Class member functions;for 
> global functions such as Fun(),no "this" pointer is added.

Yes. This was another case of me not thinking clearly before
typing! As you say, the "this" pointer will be arg0 in the
entry to class member functions only. However, C++ has a tendency
to add hidden arguments in other places as well and,
unfortunately, you have to be aware of them when using DTrace
as we have seen here. See next comment.

> #root at zhijun-5:tmp] dtrace -s test.d -c ./tt             //tt is g++ 
> compiled
> arg0=8047b94, arg1=8047b84
> string=Jon
> arg0=20, arg1=8047b94
> string=Jon
>
>  From the output,I don't think arg0 (8047b94) seems to be a "this" 
> pointer.

You're correct - it isn't. It is a hidden argument which is a
pointer to the space allocated by the caller for the return
structure to be placed in. This is the value that I referenced
in my post yesterday that should be in the %eax register on return
(as it is with g++ here). The actual argument to the function
appears here in arg1.

All this goes to show that you have to be really careful with
argument interpretation with C++ as arguments can appear shifted
because the compiler adds arguments in. In normal runtime you
don't have to worry about this but DTrace says it as it sees it
and you have to understand what is happening under the hood.

Thanks for following through on my comments. Let me know if
there still appears to be any inconsistencies.

Cheers.

Jon.

>
>
> Thanks,
> Zhijun
>
>> So, if I actually alter my code to use a return probe it
>> does indeed fail in the way you describe - i.e. I see an
>> incorrect value in arg1. The reason as to why this happens
>> is this:
>>
>> When an object is returned in C++ we are, essentially,
>> returning a structure (a lump of memory anyway). The
>> ABI specifies that when returning a structure it is up
>> to the calling function (main() in this case) to allocate
>> some space and store the address on the stack which the
>> called function then uses. We therefore have a "hidden"
>> argument which is referenced via the stack so the return
>> object referencing is done via the stack in this case.
>>
>> The ABI does stipulate however that the a function that
>> returns a structure or union also should set %eax to contain
>> (essentially) a pointer to the structure being returned. Our
>> compiler doesn't do that in this case but g++ does. I won't go
>> into the details of why it appears not to do that here. I'll
>> log a bug on this.
>>
>> So, to recap, change my D script to use the return probe
>> and code compiled with g++ should be fine. Unfortunately,
>> it looks like this won't work with Studio compiler at the
>> minute.
>>
>> Cheers.
>>
>> Jon.
>>
>


More information about the dtrace-discuss mailing list