[lldb-dev] Issues found writing synthetic children providers for template library

Enrico Granata egranata at apple.com
Wed Sep 24 11:05:18 PDT 2014


> On Sep 22, 2014, at 2:50 PM, Erik Olofsson <erik.olofsson at hansoft.com> wrote:
> 
> Hi Enrico,
> 
> Thanks for looking into this.
> 
> On 2014-09-22, at 20:04, Enrico Granata <egranata at apple.com <mailto:egranata at apple.com>> wrote:
> 
>> Hi Erik,
>> thanks for sending out this email. It is appreciated and valuable that you took the time to write this list of problems you encountered.
>> 
>>> On Sep 20, 2014, at 3:42 PM, Erik Olofsson <erik.olofsson at hansoft.com <mailto:erik.olofsson at hansoft.com>> wrote:
>>> 
>>> Hi, 
>>> 
>>> These are some of the issues I have run into when trying to add synthetic children and summary providers with the python API.
>>> 
>>> Access to nested classes and typedefs
>>> The first problem I ran into was accessing member typedefs of a SBType, for which I have found no API. The workaround for this was to create static variables with types I needed. This practice however would not be advisable in Release builds as it bloats the executable. This brings us to the second problem.
>> 
>> You are probably correct about this. On the other hand, I don’t think I ever needed this ability. When exactly would this be useful?
>> I am of course not implying that we would not care to have this ability, just that I expect workarounds to exist.
> 
> I have need this mostly for my intrusive containers. For example I have a tree template that takes a traits type. Inside this traits type I need to get to the typedef for the node type. Also getting back to the container type from an iterator.

Fair enough. I usually get away with inspecting template arguments to figure these things out, but I can see that as being useful.
Worthy of a bugzilla.

> 
>> 
>>> 
>>> Access to class member static variables
>>> There is no way to access static member variables directly from a SBValue or SBType. The current method I use works for some cases but not all:
>>> 
>>> SBValue.CreateValueFromExpression(SBValue.get_expr_path() + ("." or "->") + StaticVarName) 
>>> 	This works for normal SBValues, but not synthetic SBValues
>>> 
>> 
>> Indeed. Known problem, but most likely very hard to solve, and not even desirable in all cases. See below, but this has been discussed already somewhere in greater detail.
>> 
>>> SBValue.CreateValueFromExpression(SBType.GetName() + "::" + StaticVarName) 
>>> 	This fails for template types as it fails to parse the template
>>> 
>>> SBValue.GetFrame().GetVariables(False, False, True, False); 
>>> 	This fails for statics not in the current compilation unit
>>> 
>>> SBValue.GetTarget().FindGlobalVariables(StaticVarName, 1024); 
>>> 	This fails because not nearly all statics are returned. Also if you specify the class, it is just ignored and you need to compare SBValue.GetName() against the SBType.GetName() + StaticVarName. But that also fails sometimes because the name in SBType and SBValue is not always formatted the same way for template (non-type) params.
>> 
>> Worth filing a bugzilla for. Or maybe Greg has some magical insight on how to find all the things.
>> 
>>> 
>>> Expressions on synthetic SBValues
>>> You cannot access synthetic SBValues in an expression in any way as far as I can tell. I have found no way to get an arbitrary type and pointer into an expression for evaluation. For simple types it works to just use the address and cast it to the correct type:  "(Type *)0xXXXXXXXX". But this does not work for template types as parsing them invariably fails (error: no type named 'X' in namespace 'X::X’).
>> 
>> You can’t. That is entirely by design. Imagine having an std::map<string,int>
>> In code, you write myMap[“key”], but synthetic children are exposed as myMap[0], as a pair<keyType,valueType>
>> If synthetic children were allowed, now you could write myMap[0] = 12, or myMap[0] = {“1”,2} or other horrible things that you should not be allowed to write, with dubious semantics at best.
>> There are a few more caveats, but long story short, it’s a tricky business, and we just don’t have enough of a compelling use case/right answers to want to delve into it
> 
> Yes, I agree you shouldn't be able to access child synthetics through expressions. The problem is that there is no way to get an synthetic SBValue into an expression in any way. The only thing needed is to access the "this" of a SBValue, not synthetic children of an arbitrary SBValue. Consider the following scenario:
> 
> 	struct Type
> 	{
> 		const char *DebugFormat();
> 	};
> 	map<Type> Map;
> 
> Getting the synthetic children of this Map returns a SBValue created with:
> 	SBValue.CreateValueFromAddress('[0]', NodeAddress, NodeType)
> 
> This synthetic value is in turn sent to a summary provider which wants to call DebugFormat:
> 	ValueObject.CreateValueFromExpression(ValueObject.get_expr_path() + ".DebugFormat()")
> 
> This works for a plain variable, but not when evaluating the synthetic child of the Map, where get_expr_path() returns [0], which gives [0].DebugFormat().
> 
> Of course this example can be worked around by casting a pointer to the type, but this also loses context. What if there are several types with the same name in different modules for example. And for my case it doesn't work for templates.
> 
> A way to support this would for example be to allow the SBValue that you call CreateValueFromExpression on to be accessed through a special variable a bit like the persistent variables. So in this case I would want to call:
> 	ValueObject.CreateValueFromExpression("$this.DebugFormat()") # $this is ValueObject
> 
> Keep in mind that my use case for this is primarily for expanding variables in the Xcode debugger.

We did chat about this a bit, and we came up with a scheme of a solution. As you noticed, the problem is that GetExpressionPath() "makes no sense” (in an expression-y sense) for synthetic children
There are mainly two kinds of synthetic children:
- ones that actually exist in inferior memory
- ones that are made up by LLDB as a result of computation

The first category of children should just return a feasible expression path, e.g. (*((MyType*)address))
Now you would be able to type (*((MyType*)address).DebugFormat() and things would work

If you actually computed something (e.g. the length of a string) but it does not exist anywhere, then you can either get a raw value back, e.g. (int)5, or actually no expression path at all - and that would indicate “hey tough luck, can’t use this”

There are a few advantages to this (apart from simplicity), mostly that since you’re modifying the actual object in inferior memory, your view of the world will be meaningful, i.e. there will be no bias between the synthetic children and the reality you just tweaked

> 
>> 
>>> 
>>> Non Type Template Params
>>> For some types I need to get non type template param values. I have found no API for this, although this can be worked around by adding static variables with the values in them.
>> 
>> This sounds like a bug. Maybe a DWARF problem. Do you have a reproduction case handy?
> 
> I think it's just a missing API. Currently you have SBType.GetTemplateArgumentType, while I would need something like: SBType.GetTemplateArgumentValue to get the value of a non-type template param. 

Bugzilla here!

> 
>> 
>>> 
>>> Access to posix thread locals
>>> I have found no way to access pthread thread locals, or reading through the GS segment of a thread from the python API, so I have to resort to calling into the target. But I guess that is no problem as long as I don't need to debug a core dump (if or when this is supported).
>>> 
>>> Suggestions
>>> To get everything working:
>>> * Add API for saving a SBValue as a persistent variable. This would allow a workaround for doing expressions on synthetic SBValues
>> 
>> This might be interesting. Worth filing a bugzilla ER for.
>> 
>>> or
>>> * You should be able to access the SBValue in the expression when calling: SBValue.CreateValueFromExpression. For example: $var, or the context of the evaluation should be as if inside a class function of the type of the SBValue, which would allow you to access the member vars directly.
>> 
>> I don’t think we’d want to go for language extensions. Doesn’t this->whatever work, if you are currently stopped in a class context?
> 
> See above, it's the case when expanding a container in Xcode, when you need to do expressions on the resulting synthetic value. Also you already have the language extension in there for persistent variables. You would only need to reserve one of them for this purpose ($this).
> 
>> 
>>> 
>>> To not have to add helpers in target executable:
>>> * Add API to access nested types and typedefs in a SBType
>>> * Add API to get static class members: SBValue.GetChildStaticMemberWithName, and SBType.GetChildStaticMemberWithName
>>> * Add API to read the from the GS segment of a thread, or support for reading pthread thread locals.
>> 
>> These seem all worth of bug/ER reports, although Greg might know more about thread local access
>> 
>>> 
>>> 
>>> Any ideas for workarounds for these issue would be appreciated. Currently I would be happy to just get it to work reliably, for which all I need is to be able to get an arbitrary SBValue into an expression. 
>> 
>> Why do you need to use an expression? For 99% of formatters, that is usually not desirable nor necessary.
>> Not desirable for performance and stability reasons. Not necessary because most often all the expression does is memory access and basic manipulations that are doable via Python anyway
> 
> I agree, I use expressions only when there is no other way. Currently I need to use expressions to get to static member variables, which is the foundation for working around all the other limitations.

That should work

> Also sometimes it's just too much work to create a formatter in Python. For example I have a case with a templated float type that lets you choose the number of bits for the mantissa and exponent. Creating code in Python to format these into a string would be a lot of work.

It is probably some chunk of work, yes.
On the other hand, the whole “run expressions” business will likely end up slowing you down, and potentially causing headaches down the road when you try to debug something in a really funky state.
It is quite discouraged to run expressions to format objects. Sure, sometimes you gotta do what you gotta do, but caveat emptor. 

> 
>> 
>>> 
>>> If I were to try to patch this myself, what do you think would be the easiest:
>>> * Fix the template parsing in expressions.
>> 
>> If you have cases of expressions that we should parse and aren’t, please file bugs
> 
> I will look into reducing the cases I have found. I have a suspicion that it's not the parsing that is at fault, but that the template types are not being included for consideration.
> 
>> 
>>> * Save SBValue as persistent variable.
>> 
>> Not a bad idea, but probably not necessary either
>> 
>>> * Access SBValue directly in expression through $var
>> 
>> There are good reasons to not do that
> 
> Taken the explanation above, are you still of the same opinion?
> 
>> 
>>> * Access the SBValue directly in expression by being evaluated in the context of type of the SBValue
>> 
>> Depending on where you’re stopped this should either work already or hardly be meaningful (if I understand what you mean)
> 
> This only applies to synthetic SBValues, which have no context in the current frame. This would inject a context when CreateValueFromExpression is called through a SBValue. SBTarget.EvaluateExpression would work exactly like before. But I can imagine this would be messy to implement.
> 
>> 
>>> 
>>> Regards,
>>> Erik
>>> 
>>> _______________________________________________
>>> lldb-dev mailing list
>>> lldb-dev at cs.uiuc.edu <mailto:lldb-dev at cs.uiuc.edu>
>>> http://lists.cs.uiuc.edu/mailman/listinfo/lldb-dev <http://lists.cs.uiuc.edu/mailman/listinfo/lldb-dev>
>> 
>> Thanks,
>> - Enrico
>> 📩 egranata@.com ☎️ 27683
>> 
>> 
> 
> Regards,
> Erik

Thanks,
- Enrico
📩 egranata@.com ☎️ 27683




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/lldb-dev/attachments/20140924/4a0e1078/attachment.html>


More information about the lldb-dev mailing list