Initial support for dtrace and systemtap
[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 </refsect2>
206
207 <refsect2 id="setlocale">
208 <title>Locale</title>
209
210 <para>
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 
214 locale.
215 </para>
216
217 <para>
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.)
224 </para>
225
226 <para>
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>.
231 </para>
232
233 </refsect2>
234
235 <refsect2>
236 <title>Traps and traces</title>
237
238 <para>
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 
245 and backtrace.
246 </para>
247
248 <para>
249 Currently, the following trap variables exist:
250 <programlisting>
251 static volatile gulong g_trap_free_size;
252 static volatile gulong g_trap_realloc_size;
253 static volatile gulong g_trap_malloc_size;
254 </programlisting>
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.
261 </para>
262 <para>
263 Note that many modern debuggers support conditional breakpoints, which achieve
264 pretty much the same. E.g. in gdb, you can do
265 <programlisting>
266 break g_malloc
267 condition 1 n_bytes == 20
268 </programlisting>
269 to break only on g_malloc() calls where the size of the allocated memory block
270 is 20. 
271 </para>
272 </refsect2>
273
274 <refsect2>
275 <title>Gdb debugging macros</title>
276
277 <para>
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.
281 </para>
282
283 <para>
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
288 gdb to pick up.
289 </para>
290
291 <para>
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.
295 </para>
296
297 <para>
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
300 <programlisting>
301 gforeach i in some_list_variable: print *(GtkWidget *)l
302 </programlisting>
303 Which would print the contents of each widget in a list of widgets.
304 </para>
305
306 </refsect2>
307
308 <refsect2>
309 <title>SystemTap</title>
310
311 <para>
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.
317 </para>
318
319 </refsect2>
320
321 <refsect2>
322 <title>Memory statistics</title>
323
324 <para>
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.
328 </para>
329
330 <para>
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.
334 </para>
335
336 </refsect2>
337 </refsect1>
338 </refentry>