How to write a debugging helper for qtcreator?

China☆狼群 提交于 2020-08-23 08:02:06

问题


When debugging my C++ program using the glm::vec3 class with gdb, the vector classes are quite cumbersome to work with:

I've read in the manual, that it's possible to write debug helpers.
I've managed to get qtcreator to load the file (the debugger exits immediately with an error, if my python file has a syntax error).

How can I write a minimalistic debugging helper?

What I've already tried:

Here's the C++ code

#include <glm/glm.hpp>

class Foo
{
};

int main(int, char**)
{
  glm::vec3 vec3(42, 64, 256);
  Foo foo;

  (void)vec3;
  (void)foo;

  return 0;
}

This is my debugging helper:

from dumper import *

def qdump__glm__vec3(d, value):
    d.put("Yay, vec3 works :)")

def qdump__Foo(d, value):
    d.put("Yay, Foo works :)")

The vec3 code seems not to have any visible effect. For foo, it seems to do something, but instead of printing Yay, Foo works :) , qtcreator just shows <not accessible>. See the following screenshot:


回答1:


The short Answer: A Minimum example

Here's a minimum example for a debug helper:

C++ code:

class Foo
{
};

int main(int, char**)
{
  Foo foo;

  (void)foo;

  return 0;
}

The debug-helper:

from dumper import *

def qdump__Foo(d, value):
    d.putNumChild(0)
    d.putValue("Yay, Foo works :)")

The result:

Explanation

You've mixed up put and putValue. To quote from the link you've provided:

put(self, value) - Low level function to directly append to the output string. That is also the fastest way to append output.

put is a low level function and requires a very specific formating and thus may not be the optimal starting point for a minimum example.
Use instead putValue, this function can be used to print the value of a variable.

The short Answer for glm::vec3

Here's the working example for glm::vec3:

C++ code:

#include <glm/glm.hpp>

int main(int, char**)
{
  glm::vec3 vec3(42, 64, 256);

  (void)vec3;

  return 0;
}

The debug-helper:

from dumper import *

def qdump__glm__tvec3(d, value):
    d.putValue("[{0}, {1}, {2}]".format(value["x"], value["y"], value["z"]))
    d.putNumChild(3)
    if d.isExpanded():
        with Children(d):
            d.putSubItem("x", value["x"])
            d.putSubItem("y", value["y"])
            d.putSubItem("z", value["z"])

The result:

And to match your first screenshot for debugging a ray:

Explanation

The reason, vec3 doesn't show up is, that glm::vec3 is not the type, but just a typedef. glm::tvec3 is the type you are looking for:

typedef tvec3<float, highp>     highp_vec3;
// [...]
typedef highp_vec3          vec3;

So by replacing def qdump__glm__vec3(d, value): with def qdump__glm__tvec3(d, value):, gdb will be able to find your function.

To access the members themselves, for example the member x, use value["x"]. This way you can use d.putValue for a pleasing output.
For showing the members themselves in an expandable way, I've used the example from the link you've provided.




回答2:


Update 2020

As of glm 0.9.9.7 (2020), it seems that the typedef changed :

typedef vec<3, float, defaultp>     vec3;

Therefore, the debug-helper should be updated to :

from dumper import *

#debugging helper for    glm::(b|i|u|d)?vec[2-4]
def qdump__glm__vec(d, value):
    dim = value.type.templateArgument(0)
    
    d.putNumChild(dim)
    
    keys = ["x", "y", "z", "w"][0:dim]
    
    d.putValue("[" + ", ".join([str(value[key].value()) for key in keys]) + "]")

    if d.isExpanded(): 
        with Children(d):
            for key in keys:
                d.putSubItem(key, value[key])

This yields to the same result as before.



来源:https://stackoverflow.com/questions/34354573/how-to-write-a-debugging-helper-for-qtcreator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!