0

I am developing/debugging a c++ code which extensively uses c++ STL vectors and blitz cpp arrays (vectors/arrays are multidimensional, upto 4D/5D arrays) I am currently using cout/print to log the outputs of inputs/outputs of functions but it is getting very tedious. To be able to print the vectors/arrays while debugging, can you suggest any options. I thought of a couple of options

(a) write template functions on c++ to print and use GDBs "call" feature. but unable to use the "call" functionality of GDB for c++ template functions but works for normal functions though. (b) Is it possible to pass c++ variables to python interface of GDB and print them ? any examples for the same ?

I googled before posting this question, but did not find any useful thread. Any help is highly appreciated (even if some links can be provided)

Thanks a lot in advance !

CutePoison
  • 131
  • 1
  • 6

1 Answers1

1

Writing code in C++ to print the array and call it from gdb is certainly an option, but it might be unreliable because the print function you write might not be accessible (the linker might have dropped it because it was not used in your c++ code, for instance). Also, remember that templates are just "recipes" and you actually need to use them in order for the compiler to generate a class/function from it.

Is it possible to pass c++ variables to python interface of GDB and print them ? any examples for the same ?

A simple answer to this is "yes". You can use the parse_and_eval function in the gdb module when you use gdb's python API. Something such as

py print(gdb.parse_and_eval('your_variable'))

would print the value of a variable called your_variable using gdb's python API. But just that would be the same as just p your_variable in gdb's regular prompt without using the python API. The real power comes when you use gdb's python API to write pretty-printers for the types you want to debug.

A pretty-printer is basically just some code that you or someone else wrote to tell gdb how to print some type in a nice way. With a pretty-printer for a type just p your_variable in gdb's prompt prints the variable in the nice way defined by your pretty-printer.

I couldn't find a pretty-printer for blitz with a quick google search and I haven't used blitz before. However, I have used another library for vectors and matrices in scientific computing called armadillo and thus faced similar problems. I have thus written some pretty printers for armadillo here that might help you in case you decide to write pretty printers for blitz.

As an illustration, below you can see how the arma::mat (a matrix of doubles) type from armadillo is printed in gdb without a pretty printer (the m1 variable, which is a 6x3 matrix of doubles)

armadillo matrix without pretty printer

Notice that we can't even see the matrix elements. They are stored in a continuous memory region pointed by the mem attribute of the arma::mat object.

Now the same matrix with the pretty printer available here.

armadillo matrix with pretty printer

That makes debugging code a lot easier.


Note: You can also write pretty printers in the guile language, but I bet python is a much more common choice.

darcamo
  • 3,294
  • 1
  • 16
  • 27