a130e66bee78668f2be845325a2125697920a10f
[platform/upstream/glib.git] / docs / reference / glib / running.sgml
1 <?xml version="1.0"?>
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
3                "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd" [
4 ]>
5 <refentry id="glib-running" revision="17 Jan 2002">
6 <refmeta>
7 <refentrytitle>Running GLib Applications</refentrytitle>
8 <manvolnum>3</manvolnum>
9 <refmiscinfo>GLib Library</refmiscinfo>
10 </refmeta>
11
12 <refnamediv>
13 <refname>Running GLib Applications</refname>
14 <refpurpose>
15 How to run and debug your GLib application
16 </refpurpose>
17 </refnamediv>
18
19 <refsect1>
20 <title>Running and debugging GLib Applications</title>
21
22 <refsect2>
23 <title>Environment variables</title>
24
25 <para> 
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>. 
28 </para>
29
30 <formalpara id="G_FILENAME_ENCODING">
31   <title><envar>G_FILENAME_ENCODING</envar></title>
32
33   <para>
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.
38   </para>
39 </formalpara>
40
41 <formalpara id="G_BROKEN_FILENAMES">
42   <title><envar>G_BROKEN_FILENAMES</envar></title>
43
44   <para>
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. 
48   </para>
49 </formalpara>
50
51 <formalpara id="G_MESSAGES_PREFIXED">
52   <title><envar>G_MESSAGES_PREFIXED</envar></title>
53
54   <para>
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>. 
58   </para>
59 </formalpara>
60
61 <formalpara id="G_DEBUG">
62   <title><envar>G_DEBUG</envar></title>
63   <para>
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.
67     <variablelist>
68       <varlistentry>
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>
75         </listitem>
76       </varlistentry>
77       <varlistentry>
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>
83         </listitem>
84       </varlistentry>
85       <varlistentry>
86         <term>gc-friendly</term>
87         <listitem>
88                 <para>
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.
95                 </para>
96         </listitem>
97       </varlistentry>
98       <varlistentry>
99         <term>resident-modules</term>
100         <listitem>
101           <para>
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
105             been unloaded.
106             This option is special in that it doesn't require GLib to be
107             configured with debugging support.
108           </para>
109         </listitem>
110       </varlistentry>
111       <varlistentry>
112         <term>bind-now-modules</term>
113         <listitem>
114           <para>
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.
119           </para>
120         </listitem>
121       </varlistentry>
122     </variablelist>
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.  
125   </para>
126 </formalpara>
127
128 <formalpara id="G_SLICE">
129         <title><envar>G_SLICE</envar></title>
130         <para>
131           This environment variable allows reconfiguration of the GSlice
132           memory allocator.
133           <variablelist>
134             <varlistentry>
135               <term>always-malloc</term>
136               <listitem>
137                 <para>
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.
147                 </para>
148               </listitem>
149             </varlistentry>
150             <varlistentry>
151               <term>debug-blocks</term>
152               <listitem>
153                 <para>
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
157                   a program halt.
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:
169                   <programlisting>
170                     void *slist = g_slist_alloc(); /* void* gives up type-safety */
171                     g_list_free (slist);           /* corruption: sizeof (GSList) != sizeof (GList) */
172                   </programlisting>
173                 </para>
174               </listitem>
175             </varlistentry>
176           </variablelist>
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.  
179         </para>
180 </formalpara>
181
182 <formalpara id="G_RANDOM_VERSION">
183   <title><envar>G_RANDOM_VERSION</envar></title>
184
185   <para>
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.  
191   </para>
192 </formalpara> 
193
194 <formalpara id="LIBCHARSET_ALIAS_DIR">
195   <title><envar>LIBCHARSET_ALIAS_DIR</envar></title>
196
197   <para>
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.
202   </para>
203 </formalpara> 
204
205 <formalpara id="TZDIR">
206   <title><envar>TZDIR</envar></title>
207
208   <para>
209     Allows to specify a nonstandard location for the timezone data files
210     that are used by the #GDateTime API. The default location is under
211     <filename>/usr/share/zoneinfo</filename>. For more information,
212     also look at the <command>tzset</command> manual page.
213   </para>
214 </formalpara>
215
216 </refsect2>
217
218 <refsect2 id="setlocale">
219 <title>Locale</title>
220
221 <para>
222 A number of interfaces in GLib depend on the current locale in which
223 an application is running. Therefore, most GLib-using applications should
224 call <function>setlocale (LC_ALL, "")</function> to set up the current 
225 locale.
226 </para>
227
228 <para>
229 On Windows, in a C program there are several locale concepts
230 that not necessarily are synchronized. On one hand, there is the
231 system default ANSI code-page, which determines what encoding is used
232 for file names handled by the C library's functions and the Win32
233 API. (We are talking about the "narrow" functions here that take
234 character pointers, not the "wide" ones.)
235 </para>
236
237 <para>
238 On the other hand, there is the C library's current locale. The
239 character set (code-page) used by that is not necessarily the same as
240 the system default ANSI code-page. Strings in this character set are
241 returned by functions like <function>strftime()</function>.
242 </para>
243
244 </refsect2>
245
246 <refsect2>
247 <title>Traps and traces</title>
248
249 <para>
250 <indexterm><primary>g_trap_free_size</primary></indexterm>
251 <indexterm><primary>g_trap_realloc_size</primary></indexterm>
252 <indexterm><primary>g_trap_malloc_size</primary></indexterm>
253 Some code portions contain trap variables that can be set during debugging 
254 time if GLib has been configured with <option>--enable-debug=yes</option>. 
255 Such traps lead to immediate code halts to examine the current program state 
256 and backtrace.
257 </para>
258
259 <para>
260 Currently, the following trap variables exist:
261 <programlisting>
262 static volatile gulong g_trap_free_size;
263 static volatile gulong g_trap_realloc_size;
264 static volatile gulong g_trap_malloc_size;
265 </programlisting>
266 If set to a size > 0, <link linkend="g-free">g_free</link>(), 
267 <link linkend="g-realloc">g_realloc</link>() and 
268 <link linkend="g-malloc">g_malloc</link>() will be intercepted if the size 
269 matches the size of the corresponding memory block. This will only work with 
270 <literal>g_mem_set_vtable (glib_mem_profiler_table)</literal> upon startup 
271 though, because memory profiling is required to match on the memory block sizes.
272 </para>
273 <para>
274 Note that many modern debuggers support conditional breakpoints, which achieve
275 pretty much the same. E.g. in gdb, you can do
276 <programlisting>
277 break g_malloc
278 condition 1 n_bytes == 20
279 </programlisting>
280 to break only on g_malloc() calls where the size of the allocated memory block
281 is 20. 
282 </para>
283 </refsect2>
284
285 <refsect2>
286 <title>Gdb debugging macros</title>
287
288 <para>
289 glib ships with a set of python macros for the gdb debugger. These includes pretty
290 printers for lists, hashtables and gobject types. It also has a backtrace filter
291 that makes backtraces with signal emissions easier to read.
292 </para>
293
294 <para>
295 To use this you need a recent enough gdb that supports python scripting. Gdb 7.0
296 should be recent enough, but branches of the "archer" gdb tree as used in Fedora 11
297 and Fedora 12 should work too. 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 for
299 gdb to pick up.
300 </para>
301
302 <para>
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.
306 </para>
307
308 <para>
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
311 <programlisting>
312 gforeach i in some_list_variable: print *(GtkWidget *)l
313 </programlisting>
314 Which would print the contents of each widget in a list of widgets.
315 </para>
316
317 </refsect2>
318
319 <refsect2>
320 <title>SystemTap</title>
321
322 <para>
323 <ulink url="http://sourceware.org/systemtap/">SystemTap</ulink> is a dynamic whole-system
324 analysis toolkit.  GLib ships with a file <filename>glib.stp</filename> which defines a
325 set of probe points, which you can hook into with custom SystemTap scripts.
326 See the files <filename>glib.stp</filename> and <filename>gobject.stp</filename> which
327 are in your shared SystemTap scripts directory.
328 </para>
329
330 </refsect2>
331
332 <refsect2>
333 <title>Memory statistics</title>
334
335 <para>
336 g_mem_profile() will output a summary g_malloc() memory usage, if memory
337 profiling has been enabled by calling 
338 <literal>g_mem_set_vtable (glib_mem_profiler_table)</literal> upon startup.
339 </para>
340
341 <para>
342 If GLib has been configured with <option>--enable-debug=yes</option>,
343 then g_slice_debug_tree_statistics() can be called in a debugger to 
344 output details about the memory usage of the slice allocator.
345 </para>
346
347 </refsect2>
348 </refsect1>
349 </refentry>