Changes

Jump to: navigation, search

Debugging Gramps

1,716 bytes added, 00:33, 26 May 2020
no edit summary
{{man tip|Please note when doing debugging,|that the document mentions <code>src/gramps.py</code> and <code>Gramps.py</code>. Since you never have both in a single Gramps setup, please use <code>src/gramps.py</code> when running Gramps 3.x and earlier, and <code>Gramps.py</code> for 4.x and newer.}} An overview on how to debug GRAMPSGramps.
== Hard crash ==
When a hard crash occurs, you typically have no idea where the crash occurs. It is important to know the line where the crash occurs. So if you are able to reproduce the crash, restart GRAMPS Gramps with the command:
python -m trace -t src/grampsGramps.py
This will usually generate vast terminal output and slow the system down for that, so redirecting output to a file is usually good:
python -m trace -t src/grampsGramps.py >/tmp/trace.out
Then check the file "/tmp/trace.out" and save if needed, or redirect to somewhere else in the earlier step.
== Add debug statements ==
GRAMPS Gramps is run with the optimize flag.
python -O grampsGramps.py
This gives you the option of adding debug statements. You can use the __debug__ variable or the assert statement for this. This allows us to add code to GRAMPS Gramps that will be printed out when GRAMPS Gramps runs without the optimize flag:
python grampsGramps.py
More info: [http://docs.python.org/reference/simple_stmts.html#the-assert-statement]
== Use the log infrastructure ==
GRAMPS Gramps has built in the python log infratructureinfrastructure. GRAMPS Gramps runs with logging level logging.DEBUG to stderrh.
More info: [[Logging system]]
Short: in your code add data to the logger with: log.warning(), log.error(), log.info()...
 Start GRAMPS Gramps with the --debug flag: python grampsGramps.py --debug="name_of_the_logger"
This is useful when working in different parts, adding info output, and selecting on the command line with --debug the logger you want to see output with.
== Use profiling ==
GRAMPS Gramps has a convenience hook to allow you to do profiling. Profiling gives an overview how many times methods are called in a code fragment and how long each one took. It helps to find performance bottlenecks.
An example usage:
Add at the top of the file you want to use profiling:
from Utils gramps.gen.utils.debug import profile
Then, suppose you want to profile a save function on one of the editors. The save is called due to a connect done in the method _connect_signals. So, change the connect to save to a new method testsave you make:
profile(self.save, *obj)
Then run GRAMPSGramps, every time you save, the profiler kicks in and prints to command line a report with the time for each function.
So in short: replace the call to a function/method to calling profile with as first parameter the function/method, and other parameters the parameters of the function/method.
That's it. See also [[GEPS_016:_Enhancing_Gramps_Processing_Speed]] for a sample of Profile Analysis.
== Use the winpdb python debugger ==
'''{{man note|Note''': there |There are some issues with the winpdb debugger. For a workaround see bug ticket: {{bug|2564}} }}
[httphttps://winpdbpypi.org/project/winpdb-reborn/ Winpdb] (Ubuntu: sudo apt-get install winpdb) is a graphical interface for the python debugger.
Start it with:
winpdb src/grampsGramps.py [[Image:Winpdb.png|thumb|right|400px|Winpdb with Gramps loaded]]
[[Image:Winpdb.png|thumb|Winpdb with GRAMPS loaded]] Now, in File menu, select 'Open Source', and open the source file you want to debug. Now all debug options are possible. Eg, go to a line in the file with the cursor, and click the ''run to line'' button. The debugger will run to that point, and left show you all defined local variables with their value, as well as the stack frame.
Try it!
 
{{-}}
== Use gdb C debugger ==
{{man note|Note |Knowledge of the C programming language required.}}
With GObject introscpectionintrospection, much more can go wrong on a lower level, causing C errors. For these gdb can be used. You should install some debug libraries like libglib2.0-0-dbg, python-gobject-dbg, ... . Then, you start in a terminal gdb in the directory where grampsGramps.py is stored with
gdb python
You just type now:
run grampsGramps.py
and then navigate gramps to the '''segmentation fault'''. After that, you see something like
Program received signal SIGSEGV, Segmentation fault.
which will print the backtrace. You are now ready to use your C knowledge to fix the bug!
 
=== Trace GObject/GTK warnings ===
GTK has several possible warning levels, which might pop up on the terminal, without visible problems in Gramps. To trace these, do the following in the :
 
gdb python
 
and at the gdb prompt indicate to stop on log output:
 
b g_log if log level < 32
r Gramps.py
 
Now Gramps will stop on serious messages, and you can use
 
bt
 
to get a backtrace of the C stack showing where the message originated and what called the function, etc.
 
You can force a hard crash on warnings by setting G_DEBUG.
 
For example:
LC_ALL=C G_DEBUG=fatal-warnings python -m trace -t Gramps.py
 
Different values possible: [https://developer.gnome.org/glib/2.60/glib-running.html]
 
=== Learn More ===
 
To learn more about debugging python and C debugging with gdb, see [https://web.archive.org/web/20180806035308/http://grapsus.net:80/blog/post/Low-level-Python-debugging-with-GDB] and [https://www.sourceware.org/gdb/onlinedocs/gdb.html].
 
;Learning C tutorials
The Python language implementation is written in the C programming language.
 
*[http://www.learn-c.org/ Learn C the free interactive C tutorial.]
*[https://learncodethehardway.org/c/ Learn C The Hard Way] ($)
 
[[Category:Developers/Reference]]
[[Category:Developers/Tutorials]]
[[Category:Developers/General]]

Navigation menu