debugging.txt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. GRASS Debugging:
  2. At user level:
  3. Print debugging message if variable DEBUG
  4. is set to level equal or greater
  5. g.gisenv set="DEBUG=3"
  6. Levels: (recommended levels)
  7. * 0 - silence
  8. * 1 - message is printed once or few times per module
  9. * 3 - each row (raster) or line (vector)
  10. * 5 - each cell (raster) or point (vector)
  11. Further hints:
  12. > How can I force gcc, to complain about missing header file?
  13. '-Wimplicit-function-declaration'
  14. '-Werror-implicit-function-declaration'
  15. Give a warning (or error) whenever a function is used before being
  16. declared.
  17. ----------------------------------------------------
  18. Tcl/TK:
  19. A) Pure Tcl code:
  20. Typically you need to add "trace" commands to the code
  21. in question.
  22. B) To debug TCL code which uses C (including hybrid C+Tcl
  23. code, e.g. NVIZ, v.digit, run (example):
  24. GRASS:~> g.gisenv set="DEBUG=1"
  25. GRASS:~> d.text.freetype > dtf_tcl.txt
  26. then edit 'dtf_tcl.txt' to remove the "| wish &" from the end.
  27. then '. dtf_tcl.txt > dtf.tcl' to get rid of the echo-proofing chars.
  28. then 'wish < dtf.tcl' to test it.
  29. References:
  30. "Is white space significant in Tcl" http://wiki.tcl.tk/981
  31. "Tcl quoting hell" http://wiki.tcl.tk/1726
  32. "1 minute intro to tcl" http://www.pconline.com/~erc/tcl.htm
  33. NVIZ debug tip:
  34. at the start of nviz2.2_script, change the DEBUG setting to:
  35. set DEBUG 1
  36. To debug NVIZ:
  37. $ gdb $GISBASE/etc/nviz2.2/nviz
  38. > run -f $GISBASE/etc/nviz2.2/scripts/nviz2.2_script <any args>
  39. If you don't need to debug the startup, you can start NVIZ then
  40. "attach" to the process. E.g.
  41. $ gdb $GISBASE/etc/nviz2.2/nviz
  42. > attach <pid>
  43. > break Create_OS_Ctx
  44. > cont
  45. ----------------------------------------------------
  46. To make gcc less tolerant in accepting errors and code flaws, compile
  47. with (see also ../INSTALL):
  48. export CFLAGS='-g -ansi -Wall -D_BSD_SOURCE=1 -D_SVID_SOURCE=1 -D_POSIX_SOURCE=1 -D_POSIX_C_SOURCE=199506L'
  49. Also nice to emulate gcc/MacOSX compiler:
  50. CFLAGS='-fno-common'
  51. It is a good idea to use '-Wall -Werror' as gcc options. This means
  52. it treats the warnings as errors, i.e. it stops on them. So you have time
  53. to read them then.
  54. The gcc switch -Wimplicit-function-declaration (implied by -Wall) should
  55. report missing prototypes (use -Werror-implicit-function-declaration to
  56. make it an error rather than a warning).
  57. The linker switch --warn-common (e.g. LDFLAGS='-Wl,--warn-common') might
  58. be useful for catching multiply-defined symbols.
  59. Be sure to compile using debug flags (gcc -g) and don't "strip" the
  60. binaries after compiling.
  61. ----------------------------------------------------
  62. C Code debugging Software:
  63. 1) Debugger (command-line)
  64. gdb `which r.plane`
  65. r <flags> <parameters>
  66. bt full
  67. To debug in a running process, find out the process ID (PID)
  68. ps -aef
  69. and then use
  70. gdb --pid=PID
  71. to attach to running process PID. It will stop, enter 'c'
  72. to continue and so forth (bt full for backtrace).
  73. 2a) Graphical front-end for command-line debugger: ddd
  74. ddd `which r.plane`
  75. RUN -> here enter Parameters/Flags
  76. http://www.gnu.org/software/ddd/
  77. (GNU DDD is a graphical front-end for command-line debuggers such as
  78. GDB, DBX, WDB, Ladebug, JDB, XDB, the Perl debugger, the bash debugger, or the
  79. Python debugger. )
  80. See mini tutorial here:
  81. http://grass.osgeo.org/wiki/GRASS_Debugging
  82. 2b) Graphical front-end for command-line debugger: kdbg
  83. Use the menus
  84. ----------------------------------------------------
  85. Debugging on Mac OS X
  86. The 'ldd' command doesn't exist, but
  87. otool -L
  88. does almost the same job.
  89. ----------------------------------------------------
  90. Using valgrind to find memory leaks (http://valgrind.org/):
  91. * Memory note for vector modules:
  92. Support structures are not released by default because it take long time
  93. and it is much faster if it is done by system.
  94. You have to call Vect_set_release_support() before
  95. Vect_close() if you want to use valgrind.
  96. * Example (see also http://bambi.otago.ac.nz/hamish/grass/memleak/v.in.ascii/):
  97. CMD="v.in.ascii -zt z=3 in=lidaratm2_250k.txt out=lidaratm2_250k fs=,"
  98. valgrind -v --tool=addrcheck --leak-check=yes --show-reachable=yes $CMD --o
  99. * On 64bit boxed valgrind is not supported. An alternative is 'memusage':
  100. memusage -t -T v.in.ogr -o dsn=clcit2000 layer=LAB,ARC \
  101. type=centroid,boundary output=corine2000_it
  102. ----------------------------------------------------
  103. Profiling GRASS with GCC:
  104. Profiling allows the compiler to insert code that collects various
  105. statistics for later analysis. Profiling with GCC is particularly
  106. useful for quickly locating bottleneck functions, determining how
  107. much time is spent in a particular function, and how many times that
  108. function was called.
  109. To profile the entire GRASS package, the following steps should be
  110. followed for success:
  111. * Before running 'configure', both the CFLAGS and LDFLAGS environment
  112. variables need to be altered by adding the -pg flag to enable
  113. profiling. This can be accomplished by the following:
  114. CFLAGS='-pg' LDFLAGS='-pg' ./configure ...
  115. CFLAGS and LINK_FLAGS in include/Make/Platform.make can be manually
  116. edited after running 'configure', also.
  117. * 'configure' must be called with --disable-shared if you intend to
  118. profile GRASS libraries (recommended)
  119. * make GRASS as normal
  120. * In order to run GRASS without errors, lib/init must be recompiled
  121. without linking using the -pg flag. Edit include/Make/Platform.make
  122. and remove -pg from LINK_FLAGS. 'cd lib/init; make clean; make'.
  123. GRASS can now be installed as normal.
  124. When running a GRASS C module, a file called gmon.out is created when
  125. the module completes execution. This file can be analyzed with
  126. the GCC tool 'gprof'.
  127. Note that when 'make distclean' is run, the manual changes to
  128. include/Make/Platform.make are also removed.