[lldb-dev] Cast SBValue?

Jim Ingham jingham at apple.com
Thu Sep 23 16:21:30 PDT 2010


On Sep 23, 2010, at 2:16 PM, Greg Clayton wrote:

> It currently isn't in there, though it could easily be added. We should probably add it as:
> 
> class SBValue {
> ...
> 
> 	SBValue Cast (SBType type);
> };
> 
> You would need to check the SBValue object that is returned to make sure the casting happened correctly since we couldn't cast a "struct foo" to a "int *".

SBType is definitely the most convenient way to specify the target type, and will work in most cases.  In some C++ cases you might have two instances of a given type as base-classes in the class of the SBValue you're casting from.  So at some point we will need to come up with a way to specify that.  But this is fine to start.

> 
> Then we would need to be add a FindTypes to SBModule for a lookup in a specific module, and to SBTarget for a lookup in all modules in a target:
> 
> class SBModule {
> ...
> 	// Lookup a type in this modules
> 	SBTypeList FindTypes (const char *type_name, uint32_t max_num_matches);
> };
> 
> 
> class SBTarget {
> ...
> 
> 	// Lookup a type in all modules within a target
> 	SBTypeList FindTypes (const char *type_name, uint32_t max_num_matches);
> };
> 
> 

The other interface that would be really handy here is finding a type starting from a given SBSymbolContext.  For instance if you're stopped at a given frame, and you often want to the type that would be visible to code in the function for that frame.  You could do this from the SBFrame, but you can pull the SBSymbolContext from the frame, and the symbol context is more general.

This should probably also be hung off the SBTarget, since the search should go outward from the function, to the containing compilation unit, to the containing module then as a Hail Mary play all the other modules in the target.  

So:

class SBTarget {
...

	// Lookup a type in all modules within a target
	SBTypeList FindTypes (const char *type_name, SBSymbolContext &context, uint32_t max_num_matches);
};


> We need to have the extra "uint32_t max_num_matches" to work around the GCC one definition rule where you might get a type defined in each compile unit in your binary and if you lookup say "size_t", you might get thousands of matches. If you specify "max_num_matches = 1", then you can stop as soon as you find just one match.

In the case of the SymbolContext search, the search would stop if you found a match that would actually be visible to the given symbol context, and only provide more than one match if you didn't find a real match.

> 
> The final code would look something like:
> 
> SBValue void_ptr_value (...);
> SBModule module (frame.GetModule());
> SBTypeList type_list (module.FindTypes ("MyType *", 1));
> if (type_list.GetSize())
> {
>    // We were able to find a type that matches
>    SBValue casted_value (void_ptr_value.Cast (type_list.GetTypeAtIndex (0)));
>    // Now check cast to make sure it worked
>    if (casted_value.IsValid())
>    {
>        // The cast worked!!
>    }
> }
> 
> This isn't in the public API just yet, but it can easily be added.
> 

Yeah, this looks useful.

Jim

> Greg Clayton
> 
> 
> On Sep 23, 2010, at 3:04 AM, arvid.picciani at nokia.com wrote:
> 
>> Hi,
>> 
>> I see no way of casting an SBValue to a different type. quite often you have void pointers flying around, that are actually pointers to a class instance.
>> Casting inside an expression is not an option, since we'd prefer having it work on coredumps. 
>> Is there some API i missed?
>> _______________________________________________
>> lldb-dev mailing list
>> lldb-dev at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/lldb-dev
> 
> 
> _______________________________________________
> lldb-dev mailing list
> lldb-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/lldb-dev





More information about the lldb-dev mailing list