Gdb pretty prints with direct function calls

I am trying to use the pretty printed features of GDB to show my own C ++ matrix class.

The class is quite standard, which you can find anywhere. This is a template parameterized by the type, and can be accessed using a type C entry, such as mat [i] [j]. This first implicitly returns another "Slice" class, representing a row or column that can again access the [] operator to retrieve the data. The class itself uses a simple C array for storage, but it implements some tricks on it, such as the option to pre-allocate a larger matrix, enable non-zero startup using a step, etc. The class does not have a built-in print interface, and I cannot change it, or a link to my own code is easy.

Custom functions make it difficult to render direct data access code in Python. But is it necessary? In general: why should print reproduce data access logic? Can't I just use C ++ calls and use the [] operators to print the i, jth element? The fact that the Slice class is temporary in GDB during such a request further complicates this.

I am also pretty new to python and GDB scripting. I tried to hack examples to replace data access with gdb.execute calls, but I have no idea how to access the object name from the to_string function, so I can use something like gdb.execute (??? + '[ ] + str (i) + ']', False, True).

Interestingly, this is the most effective way to do this.

+5
2

++ [] i, j- ?

() , gdb.parse_and_eval (docs), :

  • "" ( , , )
  • , , ,
  • , , (, malloc), , , , .
+5

python, GDB. print_matrix.

(gdb) define print_matrix
Type commands for definition of "print_matrix".
End with a line saying just "end".
>set $s_arr = $arg0
>set $i=0
>while($i < $arg1)
 >p (int [][$arg2]) *($s_arr + $i)
 >set $i = $i + 1
 >end
>end
(gdb) print_matrix arr 10 10
$90 = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}
$91 = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}
$92 = {{2, 3, 4, 5, 6, 7, 8, 9, 10, 11}}
$93 = {{3, 4, 5, 6, 7, 8, 9, 10, 11, 12}}
$94 = {{4, 5, 6, 7, 8, 9, 10, 11, 12, 13}}
$95 = {{5, 6, 7, 8, 9, 10, 11, 12, 13, 14}}
$96 = {{6, 7, 8, 9, 10, 11, 12, 13, 14, 15}}
$97 = {{7, 8, 9, 10, 11, 12, 13, 14, 15, 16}}
$98 = {{8, 9, 10, 11, 12, 13, 14, 15, 16, 17}}
$99 = {{9, 10, 11, 12, 13, 14, 15, 16, 17, 18}}
(gdb)

script -x

gdb -x <commands file name> binary.out
+1

All Articles