GDB Printers: Unintialized variable in gdb

Da Viper yerimyah1 at gmail.com
Thu Aug 26 13:07:18 BST 2021


The gdb Xmethods support may help, from
https://sourceware.org/gdb/current/onlinedocs/gdb/Xmethods-In-Python.html

The problem is the inlined method to implement uses  a lot of private
classes, variables and method that has been optimised out, can can be
expensive to reimplement


(gdb) x 0
0x0:    Cannot access memory at address 0x0

which can also be used by python

the variable sometimes may contain some data that is randomly assigned to
it, so gdb just returns that


The  SIGSENV segmentation fault happens when i try to call a method before
the variable is actually initialised.
( this would also happen in raw gdb without printers).

there are two ways i thought of solving the problem

either i catch the  segfault signal in python and return the method is not
callable

or i make sure the variable is initialised before calling the method


if the case of the former, i am not sure how to catch a SIGSENV signal

i tried
`set unwindonsignal on`
 https://sourceware.org/gdb/onlinedocs/gdb/Calling.html
and catch gdb error in the printers like this

```
   try:
       #  gdb.parse_and_eval(call method here)
   except gdb.error:
         return "<not callable>"
```
this works in gdb cli but not in an Ide as it still segfaults
(maybe it sets unwidowsignal  off internall not sure )


On Tue, 24 Aug 2021 at 17:34, Ralf Habacker via Kde-finance-apps <
kde-finance-apps at kde.org> wrote:

> Am 24.08.21 um 17:27 schrieb Da Viper via Kde-finance-apps:
>
> There is a QDateTime  printer that requires calling a method  to get
> useful values.
>
> The problem is when I call the method from gdb the printer crashes because
> the variable does not exist yet thus the method does not exist in memory.
>
> The gdb Xmethods support may help, from https://sourceware.org/gdb/current/onlinedocs/gdb/Xmethods-In-Python.html
> *Xmethods* are additional methods or replacements for existing
> methods of a C++ class.  This feature is useful for those cases
> where a method defined in C++ source code could be inlined or
> optimized out by the compiler, making it unavailable to GDB.
> For such cases, one can define an xmethod to serve as a replacement
> for the method defined in the C++ source code.  GDB will
> then invoke the xmethod, instead of the C++ method, to
> evaluate expressions.
>
>
> Is there a way to check if a variable has been initialised ?
>
> In gdb access to invalid variables is catched
>
> (gdb) x 0
> 0x0:    Cannot access memory at address 0x0
>
> which can also be used by python
>
> (gdb) python gdb.execute('x 0')
> Traceback (most recent call last):
>   File "<string>", line 1, in <module>
> gdb.MemoryError: Cannot access memory at address 0x0
> 0x0:    Error while executing Python code.
>
> Wrapping this code with a try/catch block should do this, e.g.
>
> (gdb) python
> >try:
> >    gdb.execute('x 0')
> >except:
> >    print('exception')
> >end
> 0x0:    exception
>
> Regards
>
> Ralf
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kde-finance-apps/attachments/20210826/bdcb39c1/attachment.htm>


More information about the Kde-finance-apps mailing list