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

Robin Fu Zhijun.Fu at Sun.COM
Sun Dec 3 03:30:24 PST 2006


Hi,Jon,
I totally agree with you,and thanks for your kindly explanations:)
I also suppose that "hiden argument" issue exists in C as well as in 
C++, which I have
 verified using a similar D script.So I think perhaps we should take 
special care with
 functions using struct(or class object) as parameters or return value, 
in which case
 compiler may add hiden parameters to our functions.


Cheers,
Zhijun


Jonathan Haslam wrote:
>
>> 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.
>


More information about the dtrace-discuss mailing list