2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
5 <refentry id="glib-running">
7 <refentrytitle>Running GLib Applications</refentrytitle>
8 <manvolnum>3</manvolnum>
9 <refmiscinfo>GLib Library</refmiscinfo>
13 <refname>Running GLib Applications</refname>
15 How to run and debug your GLib application
20 <title>Running and debugging GLib Applications</title>
23 <title>Environment variables</title>
26 The runtime behaviour of GLib applications can be influenced by a
27 number of environment variables.
31 <title>Standard variables</title>
34 GLib reads standard environment variables like <envar>LANG</envar>,
35 <envar>PATH</envar>, <envar>HOME</envar>, <envar>TMPDIR</envar>,
36 <envar>TZ</envar> and <envar>LOGNAME</envar>.
41 <title>XDG directories</title>
44 GLib consults the environment variables <envar>XDG_DATA_HOME</envar>,
45 <envar>XDG_DATA_DIRS</envar>, <envar>XDG_CONFIG_HOME</envar>,
46 <envar>XDG_CONFIG_DIRS</envar>, <envar>XDG_CACHE_HOME</envar> and
47 <envar>XDG_RUNTIME_DIR</envar> for the various XDG directories.
48 For more information, see the <ulink url="http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html">XDG basedir spec</ulink>.
52 <formalpara id="G_FILENAME_ENCODING">
53 <title><envar>G_FILENAME_ENCODING</envar></title>
56 This environment variable can be set to a comma-separated list of character
57 set names. GLib assumes that filenames are encoded in the first character
58 set from that list rather than in UTF-8. The special token "@locale" can be
59 used to specify the character set for the current locale.
63 <formalpara id="G_BROKEN_FILENAMES">
64 <title><envar>G_BROKEN_FILENAMES</envar></title>
67 If this environment variable is set, GLib assumes that filenames are in
68 the locale encoding rather than in UTF-8. G_FILENAME_ENCODING takes
69 priority over G_BROKEN_FILENAMES.
73 <formalpara id="G_MESSAGES_PREFIXED">
74 <title><envar>G_MESSAGES_PREFIXED</envar></title>
77 A list of log levels for which messages should be prefixed by the
78 program name and PID of the application. The default is to prefix
79 everything except <literal>G_LOG_LEVEL_MESSAGE</literal> and
80 <literal>G_LOG_LEVEL_INFO</literal>.
81 The possible values are
82 <literal>error</literal>,
83 <literal>warning</literal>,
84 <literal>critical</literal>,
85 <literal>message</literal>,
86 <literal>info</literal> and
87 <literal>debug</literal>.
88 You can also use the special values
89 <literal>all</literal> and
90 <literal>help</literal>.
93 This environment variable only affects the default log handler,
94 g_log_default_handler().
98 <formalpara id="G_MESSAGES_DEBUG">
99 <title><envar>G_MESSAGES_DEBUG</envar></title>
102 A space-separated list of log domains for which informational
103 and debug messages should be printed. By default, these
104 messages are not printed.
107 You can also use the special value <literal>all</literal>.
110 This environment variable only affects the default log handler,
111 g_log_default_handler().
115 <formalpara id="G-DEBUG:CAPS">
116 <title><envar>G_DEBUG</envar></title>
119 This environment variable can be set to a list of debug options,
120 which cause GLib to print out different types of debugging information.
123 <term>fatal-warnings</term>
124 <listitem><para>Causes GLib to abort the program at the first call
125 to g_warning() or g_critical(). Use of this flag is not
126 recommended except when debugging.</para>
130 <term>fatal-criticals</term>
131 <listitem><para>Causes GLib to abort the program at the first call
132 to g_critical(). This flag can be useful during debugging and
137 <term>gc-friendly</term>
138 <listitem><para>Newly allocated memory that isn't directly initialized,
139 as well as memory being freed will be reset to 0. The point here is
140 to allow memory checkers and similar programs that use Boehm GC alike
141 algorithms to produce more accurate results.</para>
145 <term>resident-modules</term>
146 <listitem><para>All modules loaded by GModule will be made resident.
147 This can be useful for tracking memory leaks in modules which are
148 later unloaded; but it can also hide bugs where code is accessed
149 after the module would have normally been unloaded.</para>
153 <term>bind-now-modules</term>
154 <listitem><para>All modules loaded by GModule will bind their symbols
155 at load time, even when the code uses %G_MODULE_BIND_LAZY.</para>
159 The special value all can be used to turn on all debug options.
160 The special value help can be used to print all available options.
164 <formalpara id="G_SLICE">
165 <title><envar>G_SLICE</envar></title>
168 This environment variable allows reconfiguration of the GSlice
172 <term>always-malloc</term>
173 <listitem><para>This will cause all slices allocated through
174 g_slice_alloc() and released by g_slice_free1() to be actually
175 allocated via direct calls to g_malloc() and g_free().
176 This is most useful for memory checkers and similar programs that
177 use Boehm GC alike algorithms to produce more accurate results.
178 It can also be in conjunction with debugging features of the system's
179 malloc() implementation such as glibc's MALLOC_CHECK_=2 to debug
180 erroneous slice allocation code, although
181 <literal>debug-blocks</literal> is usually a better suited debugging
186 <term>debug-blocks</term>
187 <listitem><para>Using this option (present since GLib 2.13) engages
188 extra code which performs sanity checks on the released memory
189 slices. Invalid slice addresses or slice sizes will be reported and
190 lead to a program halt. This option is for debugging scenarios.
191 In particular, client packages sporting their own test suite should
192 <emphasis>always enable this option when running tests</emphasis>.
193 Global slice validation is ensured by storing size and address
194 information for each allocated chunk, and maintaining a global
195 hash table of that data. That way, multi-thread scalability is
196 given up, and memory consumption is increased. However, the
197 resulting code usually performs acceptably well, possibly better
198 than with comparable memory checking carried out using external
200 <para>An example of a memory corruption scenario that cannot be
201 reproduced with <literal>G_SLICE=always-malloc</literal>, but will
202 be caught by <literal>G_SLICE=debug-blocks</literal> is as follows:
204 void *slist = g_slist_alloc (); /* void* gives up type-safety */
205 g_list_free (slist); /* corruption: sizeof (GSList) != sizeof (GList) */
206 </programlisting></para>
210 The special value all can be used to turn on all options.
211 The special value help can be used to print all available options.
215 <formalpara id="G_RANDOM_VERSION">
216 <title><envar>G_RANDOM_VERSION</envar></title>
219 If this environment variable is set to '2.0', the outdated
220 pseudo-random number seeding and generation algorithms from
221 GLib 2.0 are used instead of the newer, better ones. You should
222 only set this variable if you have sequences of numbers that were
223 generated with Glib 2.0 that you need to reproduce exactly.
227 <formalpara id="LIBCHARSET_ALIAS_DIR">
228 <title><envar>LIBCHARSET_ALIAS_DIR</envar></title>
231 Allows to specify a nonstandard location for the
232 <filename>charset.aliases</filename> file that is used by the
233 character set conversion routines. The default location is the
234 <replaceable>libdir</replaceable> specified at compilation time.
238 <formalpara id="TZDIR">
239 <title><envar>TZDIR</envar></title>
242 Allows to specify a nonstandard location for the timezone data files
243 that are used by the #GDateTime API. The default location is under
244 <filename>/usr/share/zoneinfo</filename>. For more information,
245 also look at the <command>tzset</command> manual page.
249 <formalpara id="G_ENABLE_DIAGNOSTIC">
250 <title><envar>G_ENABLE_DIAGNOSTIC</envar></title>
253 If set to a non-zero value, this environment variable enables
254 diagnostic messages, like deprecation messages for GObject properties
261 <refsect2 id="setlocale">
262 <title>Locale</title>
265 A number of interfaces in GLib depend on the current locale in which
266 an application is running. Therefore, most GLib-using applications should
267 call <function>setlocale (LC_ALL, "")</function> to set up the current
272 On Windows, in a C program there are several locale concepts
273 that not necessarily are synchronized. On one hand, there is the
274 system default ANSI code-page, which determines what encoding is used
275 for file names handled by the C library's functions and the Win32
276 API. (We are talking about the "narrow" functions here that take
277 character pointers, not the "wide" ones.)
281 On the other hand, there is the C library's current locale. The
282 character set (code-page) used by that is not necessarily the same as
283 the system default ANSI code-page. Strings in this character set are
284 returned by functions like <function>strftime()</function>.
290 GLib ships with a set of Python macros for the GDB debugger. These includes pretty
291 printers for lists, hashtables and GObject types. It also has a backtrace filter
292 that makes backtraces with signal emissions easier to read.
296 To use this you need a version of GDB that supports Python scripting; anything
297 from 7.0 should be fine. You then need to install GLib in the same prefix as
298 GDB so that the Python GDB autoloaded files get installed in the right place
303 General pretty printing should just happen without having to do anything special.
304 To get the signal emission filtered backtrace you must use the "new-backtrace" command
305 instead of the standard one.
309 There is also a new command called gforeach that can be used to apply a command
310 on each item in a list. E.g. you can do
312 gforeach i in some_list_variable: print *(GtkWidget *)l
314 Which would print the contents of each widget in a list of widgets.
318 <title>SystemTap</title>
321 <ulink url="http://sourceware.org/systemtap/">SystemTap</ulink> is a dynamic whole-system
322 analysis toolkit. GLib ships with a file <filename>libglib-2.0.so.*.stp</filename> which defines a
323 set of probe points, which you can hook into with custom SystemTap scripts.
324 See the files <filename>libglib-2.0.so.*.stp</filename>, <filename>libgobject-2.0.so.*.stp</filename>
325 and <filename>libgio-2.0.so.*.stp</filename> which
326 are in your shared SystemTap scripts directory.
332 <title>Memory statistics</title>
335 g_mem_profile() will output a summary g_malloc() memory usage, if memory
336 profiling has been enabled by calling
337 <literal>g_mem_set_vtable (glib_mem_profiler_table)</literal> upon startup.
341 If GLib has been configured with <option>--enable-debug=yes</option>,
342 then g_slice_debug_tree_statistics() can be called in a debugger to
343 output details about the memory usage of the slice allocator.