1

I'm writing some specialized gdb scripting in python, and I have some questions about the gdb.Type class. I see a lot of methods for producing related types, like Type.const and Type.volatile, but how do I examine a Type instance, and ask "is this type const?", and "is this type volatile?"

More generally, I've got two types that are not ==. But their strings are identical. How should I be comparing two types, and how should I be programmatically determining their differences?

  • Do you have an example where the types are no ==, but their strings are identical? Because that's not what I would expect. – ssbssa Feb 20 '21 at 19:20
  • Yes, I do. The executable is a large proprietary program so I can't share the full source, but here we are talking to the python REPL: >>> m = gdb.parse_and_eval("$1") >>> ty = m.type >>> ty >>> str(ty) 'T1 *' >>> T1_ptr_type >>> str(T1_ptr_type) 'T1 *' >>> ty == T1_ptr_type False – John Aspinall Feb 21 '21 at 16:04
  • Can you create a small example with the same problem? – ssbssa Feb 21 '21 at 16:07
  • I have not been able to make a small example yet. It would help to understand what features might contribute to the symptoms. For example, some parts of the executable deal with T1 as an incomplete type (they never need to dereference the pointer), while other parts of the executable have the full definition. But I can see underlying typedefs (using Type.strip_typedefs) on both sides of the comparison, so I don't think that's the problem. – John Aspinall Feb 21 '21 at 16:16
  • In looking at the gdb sources, (which I am a long way from understanding!) it seems that types are per-objfile, which lends support to the theory that the incomplete type is contributing to the symptoms. Let's suppose that's unavoidable. Then my question is: How do i tell gdb that "I promise that T1 from *this* object file is the same as T1 from *that* object file? – John Aspinall Feb 21 '21 at 16:39
  • If you are sure the types are the same, why do you need gdb to say the same? Anyways, without an example it's very unlikely someone can help you. – ssbssa Feb 21 '21 at 17:00
  • I'm trying to write code that branches on type. For example, I have a diagnostic function that takes a T1. I want to wrap it in a layer that handles T1, T1*, std::unique_ptr, std::shared_ptr, etc. For that branching, I need to write comparisons on types. If I can't use == to compare types, what do I use? This is the original question. – John Aspinall Feb 21 '21 at 18:08
  • Use `str(T1)` to compare? – ssbssa Feb 21 '21 at 18:14
  • Yeah, that's what I'm doing now. But it's fragile, because it's quite possible to have two truly different types with the same name, if they are isolated from each other. In the C gdb code, there are methods with suggestive names like type->is_objfile_owned () and type->objfile_owner (); I'd sure like to be able to call them from python. I appreciate your help; I think I need to go off and create multiple examples with types across multiple object files (or even multiple shared libraries), and see what happens. – John Aspinall Feb 21 '21 at 18:35
  • @ssbssa For example in C++, `int main(){ struct {} a; struct {} b; }` then type of `a` and `b` are different, but `gdb.Type` objects of a and b are equal. Or static types with same name from different modules etc. (I'm not the OP, and this case is indeed *slightly* different from the OP 's case.) – user202729 Dec 07 '21 at 07:26
  • There's also `maintenance print type` but it only works from gdb console, not Python. – user202729 Dec 07 '21 at 07:31

0 Answers0