Git Product home page Git Product logo

gdb_armadillo_helpers's Introduction

GDB Armadillo helpers

This included pretty printers for armadillo vectors, matrices and cubes, as well as a few xmethods.

It also includes a pretty printer for std::complex<int> and std::complex<double> to make the output nicer.

Using these pretty printers

Clone this repository to some folder and add the code below to the .gdbinit in your home folder

source /path_where_you_cloned/gdb_armadillo_helpers/gdb_helpers/gdb_armadillo_printers.py
source /path_where_you_cloned/gdb_armadillo_helpers/gdb_helpers/gdb_std_complex_printer.py

After that just use p some_variable in gdb to see the result nicely formatted using gdb native format for arrays. This means that it will work better if you have set print array on in your .gdbinit file.

Note: This also works inside CLion and possible in other IDEs.

Screenshots

Consider the variables below

std::complex<int> c1(1, -3);
std::complex<double> c2(1.5, -3.3);
std::complex<double> c3(1.5, 3.3);

arma::mat m1{{1.1, 2.2, 3}, //
               {4, 5, 6},     //
               {7, 8, 9},     //
               {10, 11, 12},  //
               {13, 14, 15},  //
               {16, 17, 18}};

arma::cx_mat m2{{1.1 - 1.1j, 2.2 - 7.7j, 3},
                {4, 5, 6},
                {7, 8, 9},
                {10, 11, 12},
                {13, 14, 15},
                {16, 17, 18}};

When print the complex numbers without the pretty printers we get

printing complex numbers without pretty printers

With the pretty printers we get (notice the complex numbers are shown both in rectangular form and in polar form)

printing complex numbers with pretty printers

For the armadillo variables, without the pretty printers we get

printing m1 without pretty printers

printing m2 without pretty printers

You can see some internal data and the dimension of the matrix, but not the elements. It is possible to see the elements using gdb support for creating arrays and the fact that the stored elements are pointed by the mem variable. That is, use the command

p (*m1.mem)@6@3

While this works, it requires manually specifying the dimensions and it is not directly accessible. On the other hand, with pretty printers we get (notice that the dimensions is also shown and how arma::cx_mat uses the pretty printer for complex numbers)

printing m1 with pretty printers

printing m2 with pretty printers

This will even work in IDEs, such as CLion, as shown below

printing m1 with pretty printers_in_clion

printing m2 with pretty printers_in_clion

Note: The dimensions in the printing for matrices were swapped on purpose. That is, the first dimension indicates the column index, while the second one indicates the row index.

Configuration

You can set the value of the arma-show-content parameter to enable (default) or disable the armadillo pretty printers to show the elements. At any time, if you are only interested in vec/mat/cube dimension, the use set arma-show-content off in gdb and the armadillo pretty printers will only display the dimensions. Set the value to on to print the elements again.

Likewise, if you don't want the polar form of complex numbers to be shown you can run in gdb the command set complex-show-polar off.

Note: The pretty printers are also affected by gdb's native configuration for arrays, such as set print array on/off and set print elements SOME_NUMBER.

XMethods

XMethods are a feature of GDB python API that allow the re-implementation of C++ methods in Python in order for GDB to use. These C++ methods might not be available due to being inlined, optimized out, or simply because there is no inferior running (you are debugging from a core file, for instance).

The currently implemented xmethods are:

  • min
  • max
  • size
  • empty
  • at (linear indexing for vectors, matrices and cubes, as well as 2D index for matrices and 3D indexing for cubes)
  • slice (for cubes)

In order to have them available, add the code below to the .gdbinit in your home folder

source /path_where_you_cloned/gdb_armadillo_helpers/gdb_helpers/gdb_armadillo_xmethods.py

Custom 'print-numpy-array' gdb command and 'get_array' python function

Add the code below to the .gdbinit in your home folder

source /path_where_you_cloned/gdb_armadillo_helpers/gdb_helpers/gdb_armadillo_to_numpy.py

Now you can call the print-numpy-array gdb command passing the name(s) of any variables in the current scope which are armadillo types. Note that you can complete the variable names with TAB.

If you need more power, you can start the python interactive terminal from gdb with the the pi command (python-interactive). Once you are in the python terminal inside gdb, use the code below to get a numpy array from a variable called m.

m = get_array(gdb.parse_and_eval('m'))

# Just the name is also enough
m = get_array('m')

From there you can manipulate the numpy array as you want (print, compute the norm, etc). You can even run the python interactive terminal from gdb (the pi command), import matplotlib and plot the values in the matrix.

Note: Changes to this numpy array are not propagated in any way to the original m variable.

gdb_armadillo_helpers's People

Contributors

bkaangorur avatar darcamo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

gdb_armadillo_helpers's Issues

Add support for vscode and vscode-R

Hi @darcamo thanks a lot for this great tool!

I wonder how to make use it inside vscode, do you have any experiences regarding this?

Specifically, I eventually plan to use the pretty printing when debugging C++ code that lives inside an R package. The standard tool for interfacing R and C++ is Rcpp for which the strongest editor/debugger support currently comes in from of vscode (it already allows visual inspections similar to CLion); see https://github.com/REditorSupport/vscode-R that is the vscode-extension.

I'd like to try that out and make it work, do you think:

  1. it is possible directly?
  2. is there a lot to do to make this work? (I am willing to try and possibly PR here some changes along the way, if it is something relevant)

What do you think?

Error during the initialization of debugger

When I try to use gdb_armadillo_helpers, I encounter with some errors. First of all, I am getting this error:

File "C:/MinGW/etc/gdb_armadillo_helpers/gdb_helpers/gdb_armadillo_printers.py", line 30 return f"arma-show-content is set to {svalue}" ^ SyntaxError: invalid syntax File "C:/MinGW/etc/gdb_armadillo_helpers/gdb_helpers/gdb_std_complex_printer.py", line 29 return f"complex-show-polar is set to {svalue}" ^ SyntaxError: invalid syntax

Assuming that it can be a typo, I changed all return f"..." as return "..." in gdb_armadillo_printers.py and gdb_std_complex_printer.py. After that I started to get the following error:

Traceback (most recent call last): File "C:/MinGW/etc/gdb_armadillo_helpers/gdb_helpers/gdb_armadillo_printers.py", line 33, in <module> arma_show_content = ShowArmaContentParameter() File "C:/MinGW/etc/gdb_armadillo_helpers/gdb_helpers/gdb_armadillo_printers.py", line 19, in __init__ super().__init__("arma-show-content", gdb.COMMAND_NONE, TypeError: super() takes at least 1 argument (0 given) Traceback (most recent call last): File "C:/MinGW/etc/gdb_armadillo_helpers/gdb_helpers/gdb_std_complex_printer.py", line 32, in <module> complex_show_polar = ShowComplexNumberInPolar() File "C:/MinGW/etc/gdb_armadillo_helpers/gdb_helpers/gdb_std_complex_printer.py", line 18, in __init__ super().__init__("complex-show-polar", gdb.COMMAND_NONE, TypeError: super() takes at least 1 argument (0 given)

So, I cannot enable gdb_armadillo_helpers.

Python 2.7 Compat

It seems like there was a PR to make this work in python2 by removing f-strings, but I think one was missed. in gdb_std_complex_printer.py:29

Just thought I'd point it out in case you would like to fix this, since it's just a 1-charecter change.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.