• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<refentry id="glib-running" revision="17 Jan 2002">
2<refmeta>
3<refentrytitle>Running GLib Applications</refentrytitle>
4<manvolnum>3</manvolnum>
5<refmiscinfo>GLib Library</refmiscinfo>
6</refmeta>
7
8<refnamediv>
9<refname>Running GLib Applications</refname>
10<refpurpose>
11How to run and debug your GLib application
12</refpurpose>
13</refnamediv>
14
15<refsect1>
16<title>Running and debugging GLib Applications</title>
17
18<refsect2>
19<title>Environment variables</title>
20
21<para>
22GLib inspects a few of environment variables in addition to standard
23variables like <envar>LANG</envar>, <envar>PATH</envar> or <envar>HOME</envar>.
24</para>
25
26<formalpara id="G_FILENAME_ENCODING">
27  <title><envar>G_FILENAME_ENCODING</envar></title>
28
29  <para>
30    This environment variable can be set to a comma-separated list of character
31    set names. GLib assumes that filenames are encoded in the first character
32    set from that list rather than in UTF-8. The special token "@locale" can be
33    used to specify the character set for the current locale.
34  </para>
35</formalpara>
36
37<formalpara id="G_BROKEN_FILENAMES">
38  <title><envar>G_BROKEN_FILENAMES</envar></title>
39
40  <para>
41    If this environment variable is set, GLib assumes that filenames are in
42    the locale encoding rather than in UTF-8. G_FILENAME_ENCODING takes
43    priority over G_BROKEN_FILENAMES.
44  </para>
45</formalpara>
46
47<formalpara id="G_MESSAGES_PREFIXED">
48  <title><envar>G_MESSAGES_PREFIXED</envar></title>
49
50  <para>
51    A list of log levels for which messages should be prefixed by the
52    program name and PID of the application. The default is to prefix
53    everything except <literal>G_LOG_LEVEL_MESSAGE</literal> and <literal>G_LOG_LEVEL_INFO</literal>.
54  </para>
55</formalpara>
56
57<formalpara id="G_DEBUG">
58  <title><envar>G_DEBUG</envar></title>
59  <para>
60    If GLib has been configured with <option>--enable-debug=yes</option>,
61    this variable can be set to a list of debug options, which cause GLib
62    to print out different types of debugging information.
63    <variablelist>
64      <varlistentry>
65        <term>fatal_warnings</term>
66        <listitem><para>Causes GLib to abort the program at the first call
67           to <link linkend="g-warning">g_warning</link>() or
68	   <link linkend="g-critical">g_critical</link>(). This option is
69           special in that it doesn't require GLib to be configured with
70           debugging support.</para>
71        </listitem>
72      </varlistentry>
73      <varlistentry>
74        <term>fatal_criticals</term>
75        <listitem><para>Causes GLib to abort the program at the first call
76           to <link linkend="g-critical">g_critical</link>(). This option is
77           special in that it doesn't require GLib to be configured with
78           debugging support.</para>
79        </listitem>
80      </varlistentry>
81      <varlistentry>
82        <term>gc-friendly</term>
83	<listitem>
84		<para>
85		  Newly allocated memory that isn't directly initialized, as well
86		  as memory being freed will be reset to 0. The point here is to
87		  allow memory checkers and similar programs that use bohem GC alike
88		  algorithms to produce more accurate results.
89		  This option is special in that it doesn't require GLib to be
90		  configured with debugging support.
91		</para>
92        </listitem>
93      </varlistentry>
94      <varlistentry>
95        <term>resident-modules</term>
96	<listitem>
97	  <para>
98	    All modules loaded by GModule will be made resident. This can be useful
99	    for tracking memory leaks in modules which are later unloaded; but it can
100	    also hide bugs where code is accessed after the module would have normally
101	    been unloaded.
102	    This option is special in that it doesn't require GLib to be
103 	    configured with debugging support.
104	  </para>
105        </listitem>
106      </varlistentry>
107      <varlistentry>
108        <term>bind-now-modules</term>
109	<listitem>
110	  <para>
111	    All modules loaded by GModule will bind their symbols at load time, even
112	    when the code uses %G_MODULE_BIND_LAZY.
113	    This option is special in that it doesn't require GLib to be
114 	    configured with debugging support.
115	  </para>
116        </listitem>
117      </varlistentry>
118    </variablelist>
119    The special value all can be used to turn on all debug options.
120    The special value help can be used to print all available options.
121  </para>
122</formalpara>
123
124<formalpara id="G_SLICE">
125	<title><envar>G_SLICE</envar></title>
126	<para>
127	  This environment variable allows reconfiguration of the GSlice
128	  memory allocator.
129	  <variablelist>
130	    <varlistentry>
131	      <term>always-malloc</term>
132	      <listitem>
133		<para>
134		  This will cause all slices allocated through g_slice_alloc() and
135		  released by g_slice_free1() to be actually allocated via direct
136		  calls to g_malloc() and g_free().
137		  This is most useful for memory checkers and similar programs that
138		  use Bohem GC alike algorithms to produce more accurate results.
139		  It can also be in conjunction with debugging features of the system's
140		  malloc implementation such as glibc's MALLOC_CHECK_=2 to debug
141		  erroneous slice allocation code, allthough <literal>debug-blocks</literal>
142		  usually is a better suited debugging tool.
143		</para>
144	      </listitem>
145	    </varlistentry>
146	    <varlistentry>
147	      <term>debug-blocks</term>
148	      <listitem>
149		<para>
150		  Using this option (present since GLib-2.13) engages extra code
151		  which performs sanity checks on the released memory slices.
152		  Invalid slice adresses or slice sizes will be reported and lead to
153		  a program halt.
154		  This option is for debugging scenarios.
155		  In particular, client packages sporting their own test suite should
156		  <emphasis>always enable this option when running tests</emphasis>.
157		  Global slice validation is ensured by storing size and address information
158		  for each allocated chunk, and maintaining a global hash table of that data.
159		  That way, multi-thread scalability is given up, and memory consumption is
160		  increased. However, the resulting code usually performs acceptably well,
161		  possibly better than with comparable memory checking carried out using
162		  external tools. An example of a memory corruption scenario that cannot be
163		  reproduced with <literal>G_SLICE=always-malloc</literal>, but will be caught
164		  by <literal>G_SLICE=debug-blocks</literal> is as follows:
165		  <programlisting>
166		    void *slist = g_slist_alloc(); /* void* gives up type-safety */
167		    g_list_free (slist);           /* corruption: sizeof (GSList) != sizeof (GList) */
168		  </programlisting>
169		</para>
170	      </listitem>
171	    </varlistentry>
172	  </variablelist>
173          The special value all can be used to turn on all options.
174          The special value help can be used to print all available options.
175	</para>
176</formalpara>
177
178<formalpara id="G_RANDOM_VERSION">
179  <title><envar>G_RANDOM_VERSION</envar></title>
180
181  <para>
182    If this environment variable is set to '2.0', the outdated
183    pseudo-random number seeding and generation algorithms from
184    GLib-2.0 are used instead of the new better ones. Use the GLib-2.0
185    algorithms only if you have sequences of numbers generated with
186    Glib-2.0 that you need to reproduce exactly.
187  </para>
188</formalpara>
189
190<formalpara id="LIBCHARSET_ALIAS_DIR">
191  <title><envar>LIBCHARSET_ALIAS_DIR</envar></title>
192
193  <para>
194    Allows to specify a nonstandard location for the
195    <filename>charset.aliases</filename> file that is used by the
196    character set conversion routines. The default location is the
197    <replaceable>libdir</replaceable> specified at compilation time.
198  </para>
199</formalpara>
200
201</refsect2>
202
203<refsect2 id="setlocale">
204<title>Locale</title>
205
206<para>
207A number of interfaces in GLib depend on the current locale in which
208an application is running. Therefore, most GLib-using applications should
209call <function>setlocale (LC_ALL, "")</function> to set up the current
210locale.
211</para>
212
213<para>
214On Windows, in a C program there are several locale concepts
215that not necessarily are synchronized. On one hand, there is the
216system default ANSI code-page, which determines what encoding is used
217for file names handled by the C library's functions and the Win32
218API. (We are talking about the "narrow" functions here that take
219character pointers, not the "wide" ones.)
220</para>
221
222<para>
223On the other hand, there is the C library's current locale. The
224character set (code-page) used by that is not necessarily the same as
225the system default ANSI code-page. Strings in this character set are
226returned by functions like <function>strftime()</function>.
227</para>
228
229</refsect2>
230
231<refsect2>
232<title>Traps and traces</title>
233
234<para>
235<indexterm><primary>g_trap_free_size</primary></indexterm>
236<indexterm><primary>g_trap_realloc_size</primary></indexterm>
237<indexterm><primary>g_trap_malloc_size</primary></indexterm>
238Some code portions contain trap variables that can be set during debugging
239time if GLib has been configured with <option>--enable-debug=yes</option>.
240Such traps lead to immediate code halts to examine the current program state
241and backtrace.
242</para>
243
244<para>
245Currently, the following trap variables exist:
246<programlisting>
247static volatile gulong g_trap_free_size;
248static volatile gulong g_trap_realloc_size;
249static volatile gulong g_trap_malloc_size;
250</programlisting>
251If set to a size > 0, <link linkend="g-free">g_free</link>(),
252<link linkend="g-realloc">g_realloc</link>() and
253<link linkend="g-malloc">g_malloc</link>() will be intercepted if the size
254matches the size of the corresponding memory block. This will only work with
255<literal>g_mem_set_vtable (glib_mem_profiler_table)</literal> upon startup
256though, because memory profiling is required to match on the memory block sizes.
257</para>
258<para>
259Note that many modern debuggers support conditional breakpoints, which achieve
260pretty much the same. E.g. in gdb, you can do
261<programlisting>
262break g_malloc
263condition 1 n_bytes == 20
264</programlisting>
265to break only on g_malloc() calls where the size of the allocated memory block
266is 20.
267</para>
268</refsect2>
269
270<refsect2>
271<title>Memory statistics</title>
272
273<para>
274g_mem_profile() will output a summary g_malloc() memory usage, if memory
275profiling has been enabled by calling
276<literal>g_mem_set_vtable (glib_mem_profiler_table)</literal> upon startup.
277</para>
278
279<para>
280If GLib has been configured with <option>--enable-debug=yes</option>,
281then g_slice_debug_tree_statistics() can be called in a debugger to
282output details about the memory usage of the slice allocator.
283</para>
284
285</refsect2>
286</refsect1>
287</refentry>
288