Documentation/4.3/Developers/Tutorials/Debug Instructions

From Slicer Wiki
Jump to: navigation, search
Home < Documentation < 4.3 < Developers < Tutorials < Debug Instructions


For the latest Slicer documentation, visit the read-the-docs.


Background

The executable Slicer application Slicer-Superbuild/Slicer-build/Slicer (or Slicer.exe) is the launcher of the real application binary Slicer-Superbuild/Slicer-build/bin/SlicerApp-real.

Linux

Analyze a segmentation fault

$ ulimit -c unlimited
$ ./Slicer
... make it crash
$ ./Slicer --gdb ./bin/SlicerApp-real
(gdb) core core
(gdb) backtrace
...

GDB debug with launch arguments

The Slicer app launcher provides options to start other programs with the Slicer environment settings.

  • --launch <executable> [<parameters>]: executes an arbitrary program. For example, Slicer --launch /usr/bin/gnome-terminal starts gnome-terminal (then run GDB directly on SlicerQT-real)
  • --gdb: runs GDB then executes SlicerQT-real from within the debugger environment.

GDB debug by attaching to running process [RECOMMENDED]

1) Running Slicer with the following command line argument will allow you to easily obtain the associated PID:

$ ./Slicer --attach-process

This will bring up a window with the PID before loading any modules, which is also helpful for debugging the loading process.

2) Then, you can attach the process to gdb using the following command:

$ gdb --pid $PIDABOVE

3) Finally type the following gdb command

(gdb) continue

If not using the --attach-process, the PID could be obtain using the following command:

$ ps -Afww | grep SlicerApp-real


Ubuntu 10.10 and above - How to enable ptracing

In Ubuntu 10.10, Ubuntu introduced a patch to disallow ptracing of non-child processes by non-root users -ie. only a process which is a parent of another process can ptrace it for normal users. You can temporarily disable this restriction by:

$ echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

To permanently allow it to edit /etc/sysctl.d/10-ptrace.conf and change the line:

  • kernel.yama.ptrace_scope = 1

to read:

  • kernel.yama.ptrace_scope = 0

See http://askubuntu.com/questions/41629/after-upgrade-gdb-wont-attach-to-process

GDB debug by using exec-wrapper

An alternative approach is to use a wrapper script to emulate the functionality of the app launcher. This will allow you to use gdb or a gdb-controlling program such as an IDE, in order to interactively debug directly from GDB without attaching.

The general idea of the wrapper is to set all of the appropriate environment variables as they would be set by the app launcher. From SlicerLauncherSettings:

  • [LibraryPath] contents should be in LD_LIBRARY_PATH
  • [Paths] contents should be in PATH
  • [EnvironmentVariables] should each be set

See this example script

Now, start gdb and do the following:

(gdb) set exec-wrapper ./WrapSlicer4 
(gdb) exec-file ./bin/SlicerQT-real
(gdb) run

Since VTK and ITK include many multithreaded filters, by default you will see lots of messages like the following from gdb during rendering and processing:

[New Thread 0x7fff8378f700 (LWP 20510)]
[Thread 0x7fff8b0aa700 (LWP 20506) exited]

These can be turned off with this command:

set print thread-events off

GDB debug in CodeLite IDE

Linux debugging with CodeLite IDE

Windows

For VisualStudio it's also possible to run the IDE with the correct environment to debug slicer.

  • Start VisualStudio by using the launcher. Run: Slicer.exe --VisualStudio
  • When Visual Studio is started, open the ...\Slicer-build\Slicer.sln solution
  • Set the SlicerApp-real as StartUp project
  • Run Slicer in debug mode by the Start Debugging command (F5).

Note that because CMake re-creates the solution file from within the build process, Visual Studio will sometimes need to stop and reload the project, requiring manual button pressing on your part (just press Yes or OK whenever you are asked). To avoid this, you can use a script to complete the build process and then re-start the Visual Studio.

Experimental: Using Cygwin to re-build

If you use cygwin, you can create simple shell scripts, for example in /usr/local/bin, which these several steps at once when run from the Slicer4-superbuild directory:

  • s4build (slicer4 build)
(cd Slicer-build; cmake.exe -VV --debug-output . && ./Slicer.exe --VisualStudio Slicer.sln /out buildlog.txt /build)
(cd Slicer-build; ./Slicer.exe --VisualStudio Slicer.sln)
  • s4sbuild (slicer4 super build)
(cmake.exe -VV --debug-output . && ./Slicer-build/Slicer.exe --VisualStudio Slicer.sln /out buildlog.txt /build)
(cd Slicer-build; ./Slicer.exe --VisualStudio Slicer.sln)

The s4build command re-runs cmake and then runs the build of Slicer only. When this completes, it launches visual studio with slicer loaded for debugging.

s4sbuild is similar, but it runs the superbuild process so that all the dependency libraries are updated. This takes longer, but is sometimes needed when there has been a change to CTK or other tools.

Note the buildlog.txt files for checking if anything went wrong.

Mac (Xcode)

Xcode-scheme-Screen Shot 2012-10-05 at 10.13.30 AM.png

As of slicer 4.2, we do not support building using Xcode, however if you build slicer using traditional unix Makefiles you can still debug using the powerful source debugging features of Xcode.

Note: You need to set up a dummy project in Xcode just to enable some of the options. This means that Xcode will create a MacOS code template, but you won't use it directly (instead you will point to the code you build with cmake/make).

Attaching to a running process

You can use the Attach to Process option in the Xcode Product menu for debugging. This will allow you to get browsable stack traces for crashes.

Steps:

  1. Start Slicer
  2. Start Xcode
  3. Use the Product->Attach to Process... menu

Setting Breakpoints

To set a breakpoint in code that is not crashing, you can set it via the command line interface. For the lldb debugger, first attach to the process and break the program. Then at the (lldb) prompt, stop at a method as follows:

breakpoint set -M vtkMRMLScene::Clear

then you can single step and/or set other breakpoints in that file.

Alternatively, you can use the Product->Debug->Create a Symbolic Breakpoint... option. This is generally preferable to the lldb level debugging, since it shows up in the Breakpoints pane and can be toggled/deleted with the GUI.

Debugging the Startup Process

To debug startup issues you can set up a 'Scheme' and select the Slicer.app in your Slicer-build directory (see screen shot). You can set up multiple schemes with different command line arguments and executables (for example to debug tests).

Since you are using a dummy project but want to debug the slicer application, you need to do the following:

  1. Start Xcode and open dummy project
  2. Pick Product->Edit Scheme...
  3. Select the Run section
  4. Select the Info tab
  5. Pick Slicer.app as the Executable
  6. Click OK
  7. Now the Run button will start Slicer for debugging

Note that you can create multiple schemes, each various command line options to pass to slicer so you can easily get back to a debugging environment.

Also on the mac, the Instruments tool is very useful for profiling.

Debugging a Test

Once VisualStudio is open with the Slicer environment loaded, it is possible to run and debug tests. To run all tests, build the RUN_TESTS project.

  1. To debug a test, find its project:
    1. Libs/MRML/Core tests are in the MRMLCoreCxxTests project
    2. CLI tests are in CLI_NAMETest project (e.g. ThresholdScalarVolumeTest)
    3. Loadable module tests are in qSlicerLOADABLE_NAMECxxTests project (e.g. qSlicerVolumeRenderingCxxTests)
    4. Module logic tests are in MODULE_NAMELogicCxxTests project (e.g. VolumeRenderingLogicCxxTests)
    5. Module widgets tests are in MODULE_NAMEWidgetsCxxTests project (e.g. VolumesWidgetsCxxTests)
  2. Go to the project debugging properties (right click -> Properties, then Configuration Properties/Debugging)
  3. In Command Arguments, type the name of the test (e.g. vtkMRMLSceneImportTest for project MRMLCoreCxxTests)
  4. If the test takes argument(s), enter the argument(s) after the test name in Command Arguments (e.g. vtkMRMLSceneImportTest C:\Path\To\Slicer4\Libs\MRML\Core\Testing\vol_and_cube.mrml)
    1. You can see what arguments are passed by the dashboards by looking at the test details in CDash.
    2. Most VTK and Qt tests support the -I argument, it allows the test to be run in "interactive" mode. It doesn't exit at the end of the test.
  5. Make the project Set As StartUp Project
  6. Start Debugging (F5)