<chapter id="gtk-running">
  <title>Running and debugging GTK Applications</title>
  <sect1>
    <title>Environment variables</title>
    <para>
      GTK inspects a number of environment variables in addition to
      standard variables like <literal>LANG</literal>,
      <literal>PATH</literal>, <literal>HOME</literal> or
      <literal>DISPLAY</literal>; mostly to determine paths to look for
      certain files. The <link linkend="x11-envar">X11</link>,
      <link linkend="wayland-envar">Wayland</link>,
      <link linkend="win32-envar">Windows</link> and
      <link linkend="broadway-envar">Broadway</link> GDK backends use
      some additional environment variables.
    </para>
    <sect2 id="GTK_Debug-Options">
      <title>GTK_DEBUG</title>
      <para>
        This variable can be set to a list of debug options, which cause
        GTK to print out different types of debugging information. Some
        of these options are only available when GTK has been configured
        with <literal>-Ddebug=true</literal>.
      </para>
      <variablelist spacing="compact">
        <varlistentry>
          <term>
            actions
          </term>
          <listitem>
            <para>
              Actions and menu models
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            builder
          </term>
          <listitem>
            <para>
              GtkBuilder support
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            geometry
          </term>
          <listitem>
            <para>
              Size allocation
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            icontheme
          </term>
          <listitem>
            <para>
              Icon themes
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            keybindings
          </term>
          <listitem>
            <para>
              Keybindings
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            modules
          </term>
          <listitem>
            <para>
              Loading of modules
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            printing
          </term>
          <listitem>
            <para>
              Printing support
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            size-request
          </term>
          <listitem>
            <para>
              Size requests
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            text
          </term>
          <listitem>
            <para>
              Text widget internals
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            tree
          </term>
          <listitem>
            <para>
              Tree widget internals
            </para>
          </listitem>
        </varlistentry>
      </variablelist>
      <para>
        A number of keys are influencing behavior instead of just
        logging:
      </para>
      <variablelist spacing="compact">
        <varlistentry>
          <term>
            interactive
          </term>
          <listitem>
            <para>
              Open the <link linkend="interactive-debugging">interactive
              debugger</link>
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            no-css-cache
          </term>
          <listitem>
            <para>
              Bypass caching for CSS style properties
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            touchscreen
          </term>
          <listitem>
            <para>
              Pretend the pointer is a touchscreen device
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            updates
          </term>
          <listitem>
            <para>
              Visual feedback about window updates
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            resize
          </term>
          <listitem>
            <para>
              Highlight resizing widgets
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            layout
          </term>
          <listitem>
            <para>
              Show layout borders
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            snapshot
          </term>
          <listitem>
            <para>
              Include debug render nodes in the generated snapshots
            </para>
          </listitem>
        </varlistentry>
      </variablelist>
      <para>
        The special value <literal>all</literal> can be used to turn on
        all debug options. The special value <literal>help</literal> can
        be used to obtain a list of all supported debug options.
      </para>
    </sect2>
    <sect2 id="gtk-path">
      <title>GTK_PATH</title>
      <para>
        Specifies a list of directories to search when GTK is looking
        for dynamically loaded objects such as input method modules and
        print backends. If the path to the dynamically loaded object is
        given as an absolute path name, then GTK loads it directly.
        Otherwise, GTK goes in turn through the directories in
        <literal>GTK_PATH</literal>, followed by the directory
        <literal>.gtk-4.0</literal> in the user’s home directory,
        followed by the system default directory, which is
        <literal>libdir/gtk-4.0/modules</literal>. (If
        <literal>GTK_EXE_PREFIX</literal> is defined,
        <literal>libdir</literal> is
        <literal>$GTK_EXE_PREFIX/lib</literal>. Otherwise it is the
        libdir specified when GTK was configured, usually
        <literal>/usr/lib</literal>, or
        <literal>/usr/local/lib</literal>.)
      </para>
      <para>
        For each directory in this list, GTK actually looks in a
        subdirectory <literal>directory/version/host/type</literal>.
        Where <literal>version</literal> is derived from the version of
        GTK (use
        <literal>pkg-config --variable=gtk_binary_version gtk4</literal>
        to determine this from a script), <literal>host</literal> is the
        architecture on which GTK was built. (use
        <literal>pkg-config --variable=gtk_host gtk4</literal> to
        determine this from a script), and <literal>type</literal> is a
        directory specific to the type of modules; currently it can be
        <literal>modules</literal>, <literal>immodules</literal> or
        <literal>printbackends</literal>, corresponding to the types of
        modules mentioned above. Either <literal>version</literal>,
        <literal>host</literal>, or both may be omitted. GTK looks first
        in the most specific directory, then in directories with fewer
        components. The components of <literal>GTK_PATH</literal> are
        separated by the <quote>:</quote> character on Linux and Unix,
        and the <quote>;</quote> character on Windows.
      </para>
      <para>
        Note that this environment variable is read by GTK 2.x and GTK
        3.x too, which makes it unsuitable for setting it system-wide
        (or session-wide), since doing so will cause applications using
        different GTK versions to see incompatible modules.
      </para>
    </sect2>
    <sect2>
      <title>GTK_IM_MODULE</title>
      <para>
        Specifies an IM module to use in preference to the one
        determined from the locale. If this isn’t set and you are
        running on the system that enables <literal>XSETTINGS</literal>
        and has a value in <literal>Gtk/IMModule</literal>, that will be
        used for the default IM module. This also can be a
        colon-separated list of input-methods, which GTK will try in
        turn until it finds one available on the system.
      </para>
    </sect2>
    <sect2>
      <title>GTK_MEDIA</title>
      <para>
        Specifies what backend to load for <link linkend="GtkMediaFile"><type>GtkMediaFile</type></link>. The possible
        values depend on what options GTK was built with, and can
        include <quote>gstreamer</quote>, <quote>ffmpeg</quote> and
        <quote>none</quote>. If set to <quote>none</quote>, media
        playback will be unavailable. The special value
        <quote>help</quote> can be used to obtain a list of all
        supported media backends.
      </para>
    </sect2>
    <sect2>
      <title>GTK_EXE_PREFIX</title>
      <para>
        If set, GTK uses <literal>$GTK_EXE_PREFIX/lib</literal> instead
        of the libdir configured when GTK was compiled.
      </para>
    </sect2>
    <sect2>
      <title>GTK_DATA_PREFIX</title>
      <para>
        If set, GTK uses <literal>$GTK_DATA_PREFIX</literal> instead of
        the prefix configured when GTK was compiled.
      </para>
    </sect2>
    <sect2>
      <title>GTK_THEME</title>
      <para>
        If set, makes GTK use the named theme instead of the theme that
        is specified by the gtk-theme-name setting. This is intended
        mainly for easy debugging of theme issues.
      </para>
      <para>
        It is also possible to specify a theme variant to load, by
        appending the variant name with a colon, like this:
        <literal>GTK_THEME=Adwaita:dark</literal>.
      </para>
      <para>
        The following environment variables are used by GdkPixbuf, GDK
        or Pango, not by GTK itself, but we list them here for
        completeness nevertheless.
      </para>
    </sect2>
    <sect2>
      <title>GDK_PIXBUF_MODULE_FILE</title>
      <para>
        Specifies the file listing the GdkPixbuf loader modules to load.
        This environment variable overrides the default value
        <literal>libdir/gtk-4.0/4.0.0/loaders.cache</literal>
        (<literal>libdir</literal> is the sysconfdir specified when GTK
        was configured, usually <literal>/usr/lib</literal>.)
      </para>
      <para>
        The <literal>loaders.cache</literal> file is generated by the
        <literal>gdk-pixbuf-query-loaders</literal> utility.
      </para>
    </sect2>
    <sect2>
      <title>GDK_DEBUG</title>
      <para>
        This variable can be set to a list of debug options, which cause
        GDK to print out different types of debugging information. Some
        of these options are only available when GTK has been configured
        with <literal>-Ddebug=true</literal>.
      </para>
      <variablelist spacing="compact">
        <varlistentry>
          <term>
            cursor
          </term>
          <listitem>
            <para>
              Information about cursor objects (only win32)
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            eventloop
          </term>
          <listitem>
            <para>
              Information about event loop operation (mostly Quartz)
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            misc
          </term>
          <listitem>
            <para>
              Miscellaneous information
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            frames
          </term>
          <listitem>
            <para>
              Information about the frame clock
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            settings
          </term>
          <listitem>
            <para>
              Information about xsettings
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            selection
          </term>
          <listitem>
            <para>
              Information about selections
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            clipboard
          </term>
          <listitem>
            <para>
              Information about clipboards
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            dnd
          </term>
          <listitem>
            <para>
              Information about drag-and-drop
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            opengl
          </term>
          <listitem>
            <para>
              Information about OpenGL
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            vulkan
          </term>
          <listitem>
            <para>
              Information about Vulkan
            </para>
          </listitem>
        </varlistentry>
      </variablelist>
      <para>
        A number of options affect behavior instead of logging:
      </para>
      <variablelist spacing="compact">
        <varlistentry>
          <term>
            nograbs
          </term>
          <listitem>
            <para>
              Turn off all pointer and keyboard grabs
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            gl-disable
          </term>
          <listitem>
            <para>
              Disable OpenGL support
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            gl-software
          </term>
          <listitem>
            <para>
              Force OpenGL software rendering
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            gl-texture-rect
          </term>
          <listitem>
            <para>
              Use the OpenGL texture rectangle extension, if available
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            gl-legacy
          </term>
          <listitem>
            <para>
              Use a legacy OpenGL context
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            gl-gles
          </term>
          <listitem>
            <para>
              Use a GLES OpenGL context
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            vulkan-disable
          </term>
          <listitem>
            <para>
              Disable Vulkan support
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            vulkan-validate
          </term>
          <listitem>
            <para>
              Load the Vulkan validation layer, if available
            </para>
          </listitem>
        </varlistentry>
      </variablelist>
      <para>
        The special value <literal>all</literal> can be used to turn on
        all debug options. The special value <literal>help</literal> can
        be used to obtain a list of all supported debug options.
      </para>
    </sect2>
    <sect2 id="GSK-Debug-Options">
      <title>GSK_DEBUG</title>
      <para>
        This variable can be set to a list of debug options, which cause
        GSK to print out different types of debugging information. Some
        of these options are only available when GTK has been configured
        with <literal>-Ddebug=true</literal>.
      </para>
      <variablelist spacing="compact">
        <varlistentry>
          <term>
            renderer
          </term>
          <listitem>
            <para>
              General renderer information
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            cairo
          </term>
          <listitem>
            <para>
              cairo renderer information
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            opengl
          </term>
          <listitem>
            <para>
              OpenGL renderer information
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            shaders
          </term>
          <listitem>
            <para>
              Shaders
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            surface
          </term>
          <listitem>
            <para>
              Surfaces
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            vulkan
          </term>
          <listitem>
            <para>
              Vulkan renderer information
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            fallback
          </term>
          <listitem>
            <para>
              Information about fallbacks
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            glyphcache
          </term>
          <listitem>
            <para>
              Information about glyph caching
            </para>
          </listitem>
        </varlistentry>
      </variablelist>
      <para>
        A number of options affect behavior instead of logging:
      </para>
      <variablelist spacing="compact">
        <varlistentry>
          <term>
            diff
          </term>
          <listitem>
            <para>
              Show differences
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            geometry
          </term>
          <listitem>
            <para>
              Show borders
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            full-redraw
          </term>
          <listitem>
            <para>
              Force full redraws for every frame
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            sync
          </term>
          <listitem>
            <para>
              Sync after each frame
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            vulkan-staging-image
          </term>
          <listitem>
            <para>
              Use a staging image for Vulkan texture upload
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            vulkan-staging-buffer
          </term>
          <listitem>
            <para>
              Use a staging buffer for Vulkan texture upload
            </para>
          </listitem>
        </varlistentry>
      </variablelist>
      <para>
        The special value <literal>all</literal> can be used to turn on
        all debug options. The special value <literal>help</literal> can
        be used to obtain a list of all supported debug options.
      </para>
    </sect2>
    <sect2>
      <title>GDK_BACKEND</title>
      <para>
        If set, selects the GDK backend to use. Selecting a backend
        requires that GTK is compiled with support for that backend. The
        following backends can be selected, provided they are included
        in the GDK libraries you are using:
      </para>
      <variablelist spacing="compact">
        <varlistentry>
          <term>
            quartz
          </term>
          <listitem>
            <para>
              Selects the native Quartz backend
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            win32
          </term>
          <listitem>
            <para>
              Selects the native backend for Microsoft Windows
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            x11
          </term>
          <listitem>
            <para>
              Selects the native backend for connecting to X11 servers
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            broadway
          </term>
          <listitem>
            <para>
              Selects the Broadway backend for display in web browsers
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            wayland
          </term>
          <listitem>
            <para>
              Selects the Wayland backend for connecting to Wayland
              compositors
            </para>
          </listitem>
        </varlistentry>
      </variablelist>
      <para>
        This environment variable can contain a comma-separated list of
        backend names, which are tried in order. The list may also
        contain a *, which means: try all remaining backends. The
        special value <literal>help</literal> can be used to make GDK
        print out a list of all available backends. For more information
        about selecting backends, see the <link linkend="gdk-display-manager-get"><function>gdk_display_manager_get()</function></link>
        function.
      </para>
    </sect2>
    <sect2>
      <title>GDK_VULKAN_DEVICE</title>
      <para>
        This variable can be set to the index of a Vulkan device to
        override the default selection of the device that is used for
        Vulkan rendering. The special value <literal>list</literal> can
        be used to obtain a list of all Vulkan devices.
      </para>
    </sect2>
    <sect2>
      <title>GSK_RENDERER</title>
      <para>
        If set, selects the GSK renderer to use. The following renderers
        can be selected, provided they are included in the GTK library
        you are using and the GDK backend supports them:
      </para>
      <variablelist spacing="compact">
        <varlistentry>
          <term>
            help
          </term>
          <listitem>
            <para>
              Prints information about available options
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            broadway
          </term>
          <listitem>
            <para>
              Selects the Broadway-backend specific renderer
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            cairo
          </term>
          <listitem>
            <para>
              Selects the fallback Cairo renderer
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            gl
          </term>
          <listitem>
            <para>
              Selects the default OpenGL renderer
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            vulkan
          </term>
          <listitem>
            <para>
              Selects the Vulkan renderer
            </para>
          </listitem>
        </varlistentry>
      </variablelist>
    </sect2>
    <sect2>
      <title>GTK_CSD</title>
      <para>
        The default value of this environment variable is 1. If changed
        to 0, this disables the default use of client-side decorations
        on GTK windows, thus making the window manager responsible for
        drawing the decorations of windows that do not have a custom
        titlebar widget.
      </para>
      <para>
        CSD is always used for windows with a custom titlebar widget
        set, as the WM should not draw another titlebar or other
        decorations around the custom one.
      </para>
    </sect2>
    <sect2>
      <title>GTK_A11Y</title>
      <para>
        If set, selects the accessibility backend to use. The following
        backends can be selected, provided they are included in the GTK
        library you are using:
      </para>
      <variablelist spacing="compact">
        <varlistentry>
          <term>
            help
          </term>
          <listitem>
            <para>
              Prints information about available options
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            atspi
          </term>
          <listitem>
            <para>
              Selects the AT-SPI accessibility backend
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            test
          </term>
          <listitem>
            <para>
              Selects the test backend
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term>
            none
          </term>
          <listitem>
            <para>
              Disables the accessibility backend
            </para>
          </listitem>
        </varlistentry>
      </variablelist>
      <para>
        The <literal>test</literal> accessibility backend is recommended
        for test suites and remote continuous integration pipelines.
      </para>
    </sect2>
    <sect2>
      <title>XDG_DTA_HOME, XDG_DATA_DIRS</title>
      <para>
        GTK uses these environment variables to locate icon themes and
        MIME information. For more information, see the
        <ulink url="https://freedesktop.org/Standards/icon-theme-spec">Icon
        Theme Specification</ulink> the
        <ulink url="https://freedesktop.org/Standards/shared-mime-info-spec">Shared
        MIME-Info Database</ulink> and the
        <ulink url="https://freedesktop.org/Standards/basedir-spec">Base
        Directory Specification</ulink>.
      </para>
    </sect2>
    <sect2>
      <title>DESKTOP_STARTUP_ID</title>
      <para>
        GTK uses this environment variable to provide startup
        notification according to the
        <ulink url="https://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt">Startup
        Notification Spec</ulink>. Following the specification, GTK
        unsets this variable after reading it (to keep it from leaking
        to child processes). So, if you need its value for your own
        purposes, you have to read it before calling <link linkend="gtk-init"><function>gtk_init()</function></link>.
      </para>
    </sect2>
  </sect1>
  <sect1 id="interactive-debugging">
    <title>Interactive debugging</title>
    <figure>
      <title>The inspector</title>
      <mediaobject>
        <imageobject>
          <imagedata fileref="inspector.png" />
        </imageobject>
        <textobject><phrase>The inspector</phrase></textobject>
      </mediaobject>
    </figure>
    <para>
      GTK includes an interactive debugger, called the GTK Inspector,
      which lets you explore the widget tree of any GTK application at
      runtime, as well as tweak the theme and trigger visual debugging
      aids. You can easily try out changes at runtime before putting
      them into the code.
    </para>
    <para>
      Note that the GTK inspector can only show GTK internals. It can
      not understand the application-specific logic of a GTK
      application. Also, the fact that the GTK inspector is running in
      the application process limits what it can do. It is meant as a
      complement to full-blown debuggers and system tracing facilities
      such as DTrace, not as a replacement.
    </para>
    <para>
      To enable the GTK inspector, you can use the Control-Shift-I or
      Control-Shift-D keyboard shortcuts, or set the
      <literal>GTK_DEBUG=interactive</literal> environment variable.
    </para>
    <para>
      There are a few more environment variables that can be set to
      influence how the inspector renders its UI.
      <literal>GTK_INSPECTOR_DISPLAY</literal> and
      <literal>GTK_INSPECTOR_RENDERER</literal> determine the GDK
      display and the GSK renderer that the inspector is using.
    </para>
    <para>
      In some situations, it may be inappropriate to give users access
      to the GTK inspector. The keyboard shortcuts can be disabled with
      the <literal>enable-inspector-keybinding</literal> key in the
      <literal>org.gtk.Settings.Debug</literal> GSettings schema.
    </para>
  </sect1>
  <sect1 id="profiling">
    <title>Profiling</title>
    <para>
      GTK supports profiling with sysprof. It exports timing information
      about frameclock phases and various characteristics of
      GskRenderers in a format that can be displayed by sysprof or GNOME
      Builder.
    </para>
    <para>
      A simple way to capture data is to run your application under the
      <literal>sysprof-cli</literal> wrapper, which will write profiling
      data to a file called <literal>capture.syscap</literal>.
    </para>
    <para>
      When launching the application from sysprof, it will set the
      <literal>SYSPROF_TRACE_FD</literal> environment variable to point
      GTK at a file descriptor to write profiling data to.
    </para>
  </sect1>
</chapter>
