1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <title>Running GLib Applications</title>
6 <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
7 <link rel="home" href="index.html" title="GLib Reference Manual">
8 <link rel="up" href="glib.html" title="GLib Overview">
9 <link rel="prev" href="glib-compiling.html" title="Compiling GLib Applications">
10 <link rel="next" href="glib-changes.html" title="Changes to GLib">
11 <meta name="generator" content="GTK-Doc V1.18 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
14 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
15 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
16 <td><a accesskey="p" href="glib-compiling.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
17 <td><a accesskey="u" href="glib.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
18 <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
19 <th width="100%" align="center">GLib Reference Manual</th>
20 <td><a accesskey="n" href="glib-changes.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
22 <div class="refentry">
23 <a name="glib-running"></a><div class="titlepage"></div>
24 <div class="refnamediv"><table width="100%"><tr>
26 <h2><span class="refentrytitle">Running GLib Applications</span></h2>
27 <p>Running GLib Applications —
28 How to run and debug your GLib application
31 <td valign="top" align="right"></td>
33 <div class="refsect1">
34 <a name="id576854"></a><h2>Running and debugging GLib Applications</h2>
35 <div class="refsect2">
36 <a name="id570449"></a><h3>Environment variables</h3>
38 The runtime behaviour of GLib applications can be influenced by a
39 number of environment variables.
41 <p><b>Standard variables. </b>
42 GLib reads standard environment variables like <code class="envar">LANG</code>,
43 <code class="envar">PATH</code>, <code class="envar">HOME</code>, <code class="envar">TMPDIR</code>,
44 <code class="envar">TZ</code> and <code class="envar">LOGNAME</code>.
46 <p><b>XDG directories. </b>
47 GLib consults the environment variables <code class="envar">XDG_DATA_HOME</code>,
48 <code class="envar">XDG_DATA_DIRS</code>, <code class="envar">XDG_CONFIG_HOME</code>,
49 <code class="envar">XDG_CONFIG_DIRS</code>, <code class="envar">XDG_CACHE_HOME</code> and
50 <code class="envar">XDG_RUNTIME_DIR</code> for the various XDG directories.
51 For more information, see the <a class="ulink" href="http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html" target="_top">XDG basedir spec</a>.
53 <p><a name="G_FILENAME_ENCODING"></a><b><code class="envar">G_FILENAME_ENCODING</code>. </b>
54 This environment variable can be set to a comma-separated list of character
55 set names. GLib assumes that filenames are encoded in the first character
56 set from that list rather than in UTF-8. The special token "@locale" can be
57 used to specify the character set for the current locale.
59 <p><a name="G_BROKEN_FILENAMES"></a><b><code class="envar">G_BROKEN_FILENAMES</code>. </b>
60 If this environment variable is set, GLib assumes that filenames are in
61 the locale encoding rather than in UTF-8. G_FILENAME_ENCODING takes
62 priority over G_BROKEN_FILENAMES.
64 <p><a name="G_MESSAGES_PREFIXED"></a><b><code class="envar">G_MESSAGES_PREFIXED</code>. </b>
65 A list of log levels for which messages should be prefixed by the
66 program name and PID of the application. The default is to prefix
67 everything except <code class="literal">G_LOG_LEVEL_MESSAGE</code> and
68 <code class="literal">G_LOG_LEVEL_INFO</code>.
69 The possible values are
70 <code class="literal">error</code>,
71 <code class="literal">warning</code>,
72 <code class="literal">critical</code>,
73 <code class="literal">message</code>,
74 <code class="literal">info</code> and
75 <code class="literal">debug</code>.
76 You can also use the special values
77 <code class="literal">all</code> and
78 <code class="literal">help</code>.
80 This environment variable only affects the default log handler,
81 g_log_default_handler().
83 <p><a name="G_MESSAGES_DEBUG"></a><b><code class="envar">G_MESSAGES_DEBUG</code>. </b>
84 A space-separated list of log domains for which informational
85 and debug messages should be printed. By default, these
86 messages are not printed.
88 You can also use the special value <code class="literal">all</code>.
90 This environment variable only affects the default log handler,
91 g_log_default_handler().
93 <p><a name="G-DEBUG:CAPS"></a><b><code class="envar">G_DEBUG</code>. </b>
94 This environment variable can be set to a list of debug options,
95 which cause GLib to print out different types of debugging information.
97 <div class="variablelist"><table border="0">
98 <col align="left" valign="top">
101 <td><p><span class="term">fatal-warnings</span></p></td>
102 <td><p>Causes GLib to abort the program at the first call
103 to g_warning() or g_critical().</p></td>
106 <td><p><span class="term">fatal-criticals</span></p></td>
107 <td><p>Causes GLib to abort the program at the first call
108 to g_critical().</p></td>
111 <td><p><span class="term">gc-friendly</span></p></td>
112 <td><p>Newly allocated memory that isn't directly initialized,
113 as well as memory being freed will be reset to 0. The point here is
114 to allow memory checkers and similar programs that use Boehm GC alike
115 algorithms to produce more accurate results.</p></td>
118 <td><p><span class="term">resident-modules</span></p></td>
119 <td><p>All modules loaded by GModule will be made resident.
120 This can be useful for tracking memory leaks in modules which are
121 later unloaded; but it can also hide bugs where code is accessed
122 after the module would have normally been unloaded.</p></td>
125 <td><p><span class="term">bind-now-modules</span></p></td>
126 <td><p>All modules loaded by GModule will bind their symbols
127 at load time, even when the code uses %G_MODULE_BIND_LAZY.</p></td>
132 The special value all can be used to turn on all debug options.
133 The special value help can be used to print all available options.
135 <p><a name="G_SLICE"></a><b><code class="envar">G_SLICE</code>. </b>
136 This environment variable allows reconfiguration of the GSlice
139 <div class="variablelist"><table border="0">
140 <col align="left" valign="top">
143 <td><p><span class="term">always-malloc</span></p></td>
144 <td><p>This will cause all slices allocated through
145 g_slice_alloc() and released by g_slice_free1() to be actually
146 allocated via direct calls to g_malloc() and g_free().
147 This is most useful for memory checkers and similar programs that
148 use Boehm GC alike algorithms to produce more accurate results.
149 It can also be in conjunction with debugging features of the system's
150 malloc() implementation such as glibc's MALLOC_CHECK_=2 to debug
151 erroneous slice allocation code, although
152 <code class="literal">debug-blocks</code> is usually a better suited debugging
156 <td><p><span class="term">debug-blocks</span></p></td>
158 <p>Using this option (present since GLib 2.13) engages
159 extra code which performs sanity checks on the released memory
160 slices. Invalid slice adresses or slice sizes will be reported and
161 lead to a program halt. This option is for debugging scenarios.
162 In particular, client packages sporting their own test suite should
163 <span class="emphasis"><em>always enable this option when running tests</em></span>.
164 Global slice validation is ensured by storing size and address
165 information for each allocated chunk, and maintaining a global
166 hash table of that data. That way, multi-thread scalability is
167 given up, and memory consumption is increased. However, the
168 resulting code usually performs acceptably well, possibly better
169 than with comparable memory checking carried out using external
171 <p>An example of a memory corruption scenario that cannot be
172 reproduced with <code class="literal">G_SLICE=always-malloc</code>, but will
173 be caught by <code class="literal">G_SLICE=debug-blocks</code> is as follows:
175 <pre class="programlisting">
176 void *slist = g_slist_alloc (); /* void* gives up type-safety */
177 g_list_free (slist); /* corruption: sizeof (GSList) != sizeof (GList) */
184 The special value all can be used to turn on all options.
185 The special value help can be used to print all available options.
187 <p><a name="G_RANDOM_VERSION"></a><b><code class="envar">G_RANDOM_VERSION</code>. </b>
188 If this environment variable is set to '2.0', the outdated
189 pseudo-random number seeding and generation algorithms from
190 GLib 2.0 are used instead of the newer, better ones. You should
191 only set this variable if you have sequences of numbers that were
192 generated with Glib 2.0 that you need to reproduce exactly.
194 <p><a name="LIBCHARSET_ALIAS_DIR"></a><b><code class="envar">LIBCHARSET_ALIAS_DIR</code>. </b>
195 Allows to specify a nonstandard location for the
196 <code class="filename">charset.aliases</code> file that is used by the
197 character set conversion routines. The default location is the
198 <em class="replaceable"><code>libdir</code></em> specified at compilation time.
200 <p><a name="TZDIR"></a><b><code class="envar">TZDIR</code>. </b>
201 Allows to specify a nonstandard location for the timezone data files
202 that are used by the #GDateTime API. The default location is under
203 <code class="filename">/usr/share/zoneinfo</code>. For more information,
204 also look at the <span class="command"><strong>tzset</strong></span> manual page.
208 <div class="refsect2">
209 <a name="setlocale"></a><h3>Locale</h3>
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 <code class="function">setlocale (LC_ALL, "")</code> to set up the current
217 On Windows, in a C program there are several locale concepts
218 that not necessarily are synchronized. On one hand, there is the
219 system default ANSI code-page, which determines what encoding is used
220 for file names handled by the C library's functions and the Win32
221 API. (We are talking about the "narrow" functions here that take
222 character pointers, not the "wide" ones.)
225 On the other hand, there is the C library's current locale. The
226 character set (code-page) used by that is not necessarily the same as
227 the system default ANSI code-page. Strings in this character set are
228 returned by functions like <code class="function">strftime()</code>.
232 <div class="refsect2">
233 <a name="id547573"></a><h3>Traps and traces</h3>
238 Some code portions contain trap variables that can be set during debugging
239 time if GLib has been configured with <code class="option">--enable-debug=yes</code>.
240 Such traps lead to immediate code halts to examine the current program state
244 Currently, the following trap variables exist:
246 <pre class="programlisting">
247 static volatile gulong g_trap_free_size;
248 static volatile gulong g_trap_realloc_size;
249 static volatile gulong g_trap_malloc_size;
252 If set to a size > 0, <a class="link" href="glib-Memory-Allocation.html#g-free" title="g_free ()">g_free</a>(),
253 <a class="link" href="glib-Memory-Allocation.html#g-realloc" title="g_realloc ()">g_realloc</a>() and
254 <a class="link" href="glib-Memory-Allocation.html#g-malloc" title="g_malloc ()">g_malloc</a>() will be intercepted if the size
255 matches the size of the corresponding memory block. This will only work with
256 <code class="literal">g_mem_set_vtable (glib_mem_profiler_table)</code> upon startup
257 though, because memory profiling is required to match on the memory block sizes.
260 Note that many modern debuggers support conditional breakpoints, which achieve
261 pretty much the same. E.g. in gdb, you can do
263 <pre class="programlisting">
265 condition 1 n_bytes == 20
268 to break only on g_malloc() calls where the size of the allocated memory block
273 <div class="refsect2">
274 <a name="id547656"></a><h3>Gdb debugging macros</h3>
276 glib ships with a set of python macros for the gdb debugger. These includes pretty
277 printers for lists, hashtables and gobject types. It also has a backtrace filter
278 that makes backtraces with signal emissions easier to read.
281 To use this you need a recent enough gdb that supports python scripting. Gdb 7.0
282 should be recent enough, but branches of the "archer" gdb tree as used in Fedora 11
283 and Fedora 12 should work too. You then need to install glib in the same prefix as
284 gdb so that the python gdb autoloaded files get installed in the right place for
288 General pretty printing should just happen without having to do anything special.
289 To get the signal emission filtered backtrace you must use the "new-backtrace" command
290 instead of the standard one.
293 There is also a new command called gforeach that can be used to apply a command
294 on each item in a list. E.g. you can do
296 <pre class="programlisting">
297 gforeach i in some_list_variable: print *(GtkWidget *)l
300 Which would print the contents of each widget in a list of widgets.
304 <div class="refsect2">
305 <a name="id547691"></a><h3>SystemTap</h3>
307 <a class="ulink" href="http://sourceware.org/systemtap/" target="_top">SystemTap</a> is a dynamic whole-system
308 analysis toolkit. GLib ships with a file <code class="filename">glib.stp</code> which defines a
309 set of probe points, which you can hook into with custom SystemTap scripts.
310 See the files <code class="filename">glib.stp</code> and <code class="filename">gobject.stp</code> which
311 are in your shared SystemTap scripts directory.
315 <div class="refsect2">
316 <a name="id547727"></a><h3>Memory statistics</h3>
318 g_mem_profile() will output a summary g_malloc() memory usage, if memory
319 profiling has been enabled by calling
320 <code class="literal">g_mem_set_vtable (glib_mem_profiler_table)</code> upon startup.
323 If GLib has been configured with <code class="option">--enable-debug=yes</code>,
324 then g_slice_debug_tree_statistics() can be called in a debugger to
325 output details about the memory usage of the slice allocator.
332 Generated by GTK-Doc V1.18</div>