2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
3 "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd" [
5 <refentry id="glib-running" revision="17 Jan 2002">
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 GLib inspects a few of environment variables in addition to standard
27 variables like <envar>LANG</envar>, <envar>PATH</envar> or <envar>HOME</envar>.
30 <formalpara id="G_FILENAME_ENCODING">
31 <title><envar>G_FILENAME_ENCODING</envar></title>
34 This environment variable can be set to a comma-separated list of character
35 set names. GLib assumes that filenames are encoded in the first character
36 set from that list rather than in UTF-8. The special token "@locale" can be
37 used to specify the character set for the current locale.
41 <formalpara id="G_BROKEN_FILENAMES">
42 <title><envar>G_BROKEN_FILENAMES</envar></title>
45 If this environment variable is set, GLib assumes that filenames are in
46 the locale encoding rather than in UTF-8. G_FILENAME_ENCODING takes
47 priority over G_BROKEN_FILENAMES.
51 <formalpara id="G_MESSAGES_PREFIXED">
52 <title><envar>G_MESSAGES_PREFIXED</envar></title>
55 A list of log levels for which messages should be prefixed by the
56 program name and PID of the application. The default is to prefix
57 everything except <literal>G_LOG_LEVEL_MESSAGE</literal> and <literal>G_LOG_LEVEL_INFO</literal>.
61 <formalpara id="G_DEBUG">
62 <title><envar>G_DEBUG</envar></title>
64 If GLib has been configured with <option>--enable-debug=yes</option>,
65 this variable can be set to a list of debug options, which cause GLib
66 to print out different types of debugging information.
69 <term>fatal_warnings</term>
70 <listitem><para>Causes GLib to abort the program at the first call
71 to <link linkend="g-warning">g_warning</link>() or
72 <link linkend="g-critical">g_critical</link>(). This option is
73 special in that it doesn't require GLib to be configured with
74 debugging support.</para>
78 <term>fatal_criticals</term>
79 <listitem><para>Causes GLib to abort the program at the first call
80 to <link linkend="g-critical">g_critical</link>(). This option is
81 special in that it doesn't require GLib to be configured with
82 debugging support.</para>
86 <term>gc-friendly</term>
89 Newly allocated memory that isn't directly initialized, as well
90 as memory being freed will be reset to 0. The point here is to
91 allow memory checkers and similar programs that use bohem GC alike
92 algorithms to produce more accurate results.
93 This option is special in that it doesn't require GLib to be
94 configured with debugging support.
99 <term>resident-modules</term>
102 All modules loaded by GModule will be made resident. This can be useful
103 for tracking memory leaks in modules which are later unloaded; but it can
104 also hide bugs where code is accessed after the module would have normally
106 This option is special in that it doesn't require GLib to be
107 configured with debugging support.
112 <term>bind-now-modules</term>
115 All modules loaded by GModule will bind their symbols at load time, even
116 when the code uses %G_MODULE_BIND_LAZY.
117 This option is special in that it doesn't require GLib to be
118 configured with debugging support.
123 The special value all can be used to turn on all debug options.
124 The special value help can be used to print all available options.
128 <formalpara id="G_SLICE">
129 <title><envar>G_SLICE</envar></title>
131 This environment variable allows reconfiguration of the GSlice
135 <term>always-malloc</term>
138 This will cause all slices allocated through g_slice_alloc() and
139 released by g_slice_free1() to be actually allocated via direct
140 calls to g_malloc() and g_free().
141 This is most useful for memory checkers and similar programs that
142 use Bohem GC alike algorithms to produce more accurate results.
143 It can also be in conjunction with debugging features of the system's
144 malloc implementation such as glibc's MALLOC_CHECK_=2 to debug
145 erroneous slice allocation code, allthough <literal>debug-blocks</literal>
146 usually is a better suited debugging tool.
151 <term>debug-blocks</term>
154 Using this option (present since GLib-2.13) engages extra code
155 which performs sanity checks on the released memory slices.
156 Invalid slice adresses or slice sizes will be reported and lead to
158 This option is for debugging scenarios.
159 In particular, client packages sporting their own test suite should
160 <emphasis>always enable this option when running tests</emphasis>.
161 Global slice validation is ensured by storing size and address information
162 for each allocated chunk, and maintaining a global hash table of that data.
163 That way, multi-thread scalability is given up, and memory consumption is
164 increased. However, the resulting code usually performs acceptably well,
165 possibly better than with comparable memory checking carried out using
166 external tools. An example of a memory corruption scenario that cannot be
167 reproduced with <literal>G_SLICE=always-malloc</literal>, but will be caught
168 by <literal>G_SLICE=debug-blocks</literal> is as follows:
170 void *slist = g_slist_alloc(); /* void* gives up type-safety */
171 g_list_free (slist); /* corruption: sizeof (GSList) != sizeof (GList) */
177 The special value all can be used to turn on all options.
178 The special value help can be used to print all available options.
182 <formalpara id="G_RANDOM_VERSION">
183 <title><envar>G_RANDOM_VERSION</envar></title>
186 If this environment variable is set to '2.0', the outdated
187 pseudo-random number seeding and generation algorithms from
188 GLib-2.0 are used instead of the new better ones. Use the GLib-2.0
189 algorithms only if you have sequences of numbers generated with
190 Glib-2.0 that you need to reproduce exactly.
194 <formalpara id="LIBCHARSET_ALIAS_DIR">
195 <title><envar>LIBCHARSET_ALIAS_DIR</envar></title>
198 Allows to specify a nonstandard location for the
199 <filename>charset.aliases</filename> file that is used by the
200 character set conversion routines. The default location is the
201 <replaceable>libdir</replaceable> specified at compilation time.
207 <refsect2 id="setlocale">
208 <title>Locale</title>
211 A number of interfaces in GLib depend on the current locale in which
212 an application is running. Therefore, most GLib-using applications should
213 call <function>setlocale (LC_ALL, "")</function> to set up the current
218 On Windows, in a C program there are several locale concepts
219 that not necessarily are synchronized. On one hand, there is the
220 system default ANSI code-page, which determines what encoding is used
221 for file names handled by the C library's functions and the Win32
222 API. (We are talking about the "narrow" functions here that take
223 character pointers, not the "wide" ones.)
227 On the other hand, there is the C library's current locale. The
228 character set (code-page) used by that is not necessarily the same as
229 the system default ANSI code-page. Strings in this character set are
230 returned by functions like <function>strftime()</function>.
236 <title>Traps and traces</title>
239 <indexterm><primary>g_trap_free_size</primary></indexterm>
240 <indexterm><primary>g_trap_realloc_size</primary></indexterm>
241 <indexterm><primary>g_trap_malloc_size</primary></indexterm>
242 Some code portions contain trap variables that can be set during debugging
243 time if GLib has been configured with <option>--enable-debug=yes</option>.
244 Such traps lead to immediate code halts to examine the current program state
249 Currently, the following trap variables exist:
251 static volatile gulong g_trap_free_size;
252 static volatile gulong g_trap_realloc_size;
253 static volatile gulong g_trap_malloc_size;
255 If set to a size > 0, <link linkend="g-free">g_free</link>(),
256 <link linkend="g-realloc">g_realloc</link>() and
257 <link linkend="g-malloc">g_malloc</link>() will be intercepted if the size
258 matches the size of the corresponding memory block. This will only work with
259 <literal>g_mem_set_vtable (glib_mem_profiler_table)</literal> upon startup
260 though, because memory profiling is required to match on the memory block sizes.
263 Note that many modern debuggers support conditional breakpoints, which achieve
264 pretty much the same. E.g. in gdb, you can do
267 condition 1 n_bytes == 20
269 to break only on g_malloc() calls where the size of the allocated memory block
275 <title>Gdb debugging macros</title>
278 glib ships with a set of python macros for the gdb debugger. These includes pretty
279 printers for lists, hashtables and gobject types. It also has a backtrace filter
280 that makes backtraces with signal emissions easier to read.
284 To use this you need a recent enough gdb that supports python scripting. Gdb 7.0
285 should be recent enough, but branches of the "archer" gdb tree as used in Fedora 11
286 and Fedora 12 should work too. You then need to install glib in the same prefix as
287 gdb so that the python gdb autoloaded files get installed in the right place for
292 General pretty printing should just happen without having to do anything special.
293 To get the signal emission filtered backtrace you must use the "new-backtrace" command
294 instead of the standard one.
298 There is also a new command called gforeach that can be used to apply a command
299 on each item in a list. E.g. you can do
301 gforeach i in some_list_variable: print *(GtkWidget *)l
303 Which would print the contents of each widget in a list of widgets.
309 <title>SystemTap</title>
312 <ulink url="http://sourceware.org/systemtap/">SystemTap</ulink> is a dynamic whole-system
313 analysis toolkit. GLib ships with a file <filename>glib.stp</filename> which defines a
314 set of probe points, which you can hook into with custom SystemTap scripts.
315 See the files <filename>glib.stp</filename> and <filename>gobject.stp</filename> which
316 are in your shared SystemTap scripts directory.
322 <title>Memory statistics</title>
325 g_mem_profile() will output a summary g_malloc() memory usage, if memory
326 profiling has been enabled by calling
327 <literal>g_mem_set_vtable (glib_mem_profiler_table)</literal> upon startup.
331 If GLib has been configured with <option>--enable-debug=yes</option>,
332 then g_slice_debug_tree_statistics() can be called in a debugger to
333 output details about the memory usage of the slice allocator.