Initial commit
[platform/upstream/glib2.0.git] / docs / reference / glib / html / glib-running.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html>
3 <head>
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.75.2">
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.13 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
13 <link rel="chapter" href="glib.html" title="GLib Overview">
14 <link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals">
15 <link rel="chapter" href="glib-core.html" title="GLib Core Application Support">
16 <link rel="chapter" href="glib-utilities.html" title="GLib Utilities">
17 <link rel="chapter" href="glib-data-types.html" title="GLib Data Types">
18 <link rel="chapter" href="tools.html" title="GLib Tools">
19 <link rel="index" href="api-index-full.html" title="Index">
20 <link rel="index" href="api-index-deprecated.html" title="Index of deprecated symbols">
21 <link rel="index" href="api-index-2-2.html" title="Index of new symbols in 2.2">
22 <link rel="index" href="api-index-2-4.html" title="Index of new symbols in 2.4">
23 <link rel="index" href="api-index-2-6.html" title="Index of new symbols in 2.6">
24 <link rel="index" href="api-index-2-8.html" title="Index of new symbols in 2.8">
25 <link rel="index" href="api-index-2-10.html" title="Index of new symbols in 2.10">
26 <link rel="index" href="api-index-2-12.html" title="Index of new symbols in 2.12">
27 <link rel="index" href="api-index-2-14.html" title="Index of new symbols in 2.14">
28 <link rel="index" href="api-index-2-16.html" title="Index of new symbols in 2.16">
29 <link rel="index" href="api-index-2-18.html" title="Index of new symbols in 2.18">
30 <link rel="index" href="api-index-2-20.html" title="Index of new symbols in 2.20">
31 <link rel="index" href="api-index-2-22.html" title="Index of new symbols in 2.22">
32 <link rel="index" href="api-index-2-24.html" title="Index of new symbols in 2.24">
33 </head>
34 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
35 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
36 <td><a accesskey="p" href="glib-compiling.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
37 <td><a accesskey="u" href="glib.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
38 <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
39 <th width="100%" align="center">GLib Reference Manual</th>
40 <td><a accesskey="n" href="glib-changes.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
41 </tr></table>
42 <div class="refentry" title="Running GLib Applications">
43 <a name="glib-running"></a><div class="titlepage"></div>
44 <div class="refnamediv"><table width="100%"><tr>
45 <td valign="top">
46 <h2><span class="refentrytitle">Running GLib Applications</span></h2>
47 <p>Running GLib Applications — 
48 How to run and debug your GLib application
49 </p>
50 </td>
51 <td valign="top" align="right"></td>
52 </tr></table></div>
53 <div class="refsect1" title="Running and debugging GLib Applications">
54 <a name="id492288"></a><h2>Running and debugging GLib Applications</h2>
55 <div class="refsect2" title="Environment variables">
56 <a name="id489536"></a><h3>Environment variables</h3>
57 <p> 
58 GLib inspects a few of environment variables in addition to standard
59 variables like <code class="envar">LANG</code>, <code class="envar">PATH</code> or <code class="envar">HOME</code>. 
60 </p>
61 <p title="G_FILENAME_ENCODING"><a name="G_FILENAME_ENCODING"></a><b><code class="envar">G_FILENAME_ENCODING</code>. </b>
62     This environment variable can be set to a comma-separated list of character
63     set names. GLib assumes that filenames are encoded in the first character 
64     set from that list rather than in UTF-8. The special token "@locale" can be
65     used to specify the character set for the current locale.
66   </p>
67 <p title="G_BROKEN_FILENAMES"><a name="G_BROKEN_FILENAMES"></a><b><code class="envar">G_BROKEN_FILENAMES</code>. </b>
68     If this environment variable is set, GLib assumes that filenames are in 
69     the locale encoding rather than in UTF-8. G_FILENAME_ENCODING takes
70     priority over G_BROKEN_FILENAMES. 
71   </p>
72 <p title="G_MESSAGES_PREFIXED"><a name="G_MESSAGES_PREFIXED"></a><b><code class="envar">G_MESSAGES_PREFIXED</code>. </b>
73     A list of log levels for which messages should be prefixed by the 
74     program name and PID of the application. The default is to prefix
75     everything except <code class="literal">G_LOG_LEVEL_MESSAGE</code> and <code class="literal">G_LOG_LEVEL_INFO</code>. 
76   </p>
77 <p title="G_DEBUG"><a name="G_DEBUG"></a><b><code class="envar">G_DEBUG</code>. </b>
78     If GLib has been configured with <code class="option">--enable-debug=yes</code>,
79     this variable can be set to a list of debug options, which cause GLib
80     to print out different types of debugging information.
81     </p>
82 <div class="variablelist"><table border="0">
83 <col align="left" valign="top">
84 <tbody>
85 <tr>
86 <td><p><span class="term">fatal_warnings</span></p></td>
87 <td><p>Causes GLib to abort the program at the first call
88            to <a class="link" href="glib-Message-Logging.html#g-warning" title="g_warning()">g_warning</a>() or
89            <a class="link" href="glib-Message-Logging.html#g-critical" title="g_critical()">g_critical</a>(). This option is 
90            special in that it doesn't require GLib to be configured with 
91            debugging support.</p></td>
92 </tr>
93 <tr>
94 <td><p><span class="term">fatal_criticals</span></p></td>
95 <td><p>Causes GLib to abort the program at the first call
96            to <a class="link" href="glib-Message-Logging.html#g-critical" title="g_critical()">g_critical</a>(). This option is 
97            special in that it doesn't require GLib to be configured with 
98            debugging support.</p></td>
99 </tr>
100 <tr>
101 <td><p><span class="term">gc-friendly</span></p></td>
102 <td><p>
103                   Newly allocated memory that isn't directly initialized, as well
104                   as memory being freed will be reset to 0. The point here is to
105                   allow memory checkers and similar programs that use bohem GC alike
106                   algorithms to produce more accurate results.
107                   This option is special in that it doesn't require GLib to be
108                   configured with debugging support.
109                 </p></td>
110 </tr>
111 <tr>
112 <td><p><span class="term">resident-modules</span></p></td>
113 <td><p>
114             All modules loaded by GModule will be made resident. This can be useful
115             for tracking memory leaks in modules which are later unloaded; but it can
116             also hide bugs where code is accessed after the module would have normally
117             been unloaded.
118             This option is special in that it doesn't require GLib to be
119             configured with debugging support.
120           </p></td>
121 </tr>
122 <tr>
123 <td><p><span class="term">bind-now-modules</span></p></td>
124 <td><p>
125             All modules loaded by GModule will bind their symbols at load time, even
126             when the code uses %G_MODULE_BIND_LAZY.
127             This option is special in that it doesn't require GLib to be
128             configured with debugging support.
129           </p></td>
130 </tr>
131 </tbody>
132 </table></div>
133 <p title="G_DEBUG">
134     The special value all can be used to turn on all debug options. 
135     The special value help can be used to print all available options.  
136   </p>
137 <p title="G_SLICE"><a name="G_SLICE"></a><b><code class="envar">G_SLICE</code>. </b>
138           This environment variable allows reconfiguration of the GSlice
139           memory allocator.
140           </p>
141 <div class="variablelist"><table border="0">
142 <col align="left" valign="top">
143 <tbody>
144 <tr>
145 <td><p><span class="term">always-malloc</span></p></td>
146 <td><p>
147                   This will cause all slices allocated through g_slice_alloc() and
148                   released by g_slice_free1() to be actually allocated via direct
149                   calls to g_malloc() and g_free().
150                   This is most useful for memory checkers and similar programs that
151                   use Bohem GC alike algorithms to produce more accurate results.
152                   It can also be in conjunction with debugging features of the system's
153                   malloc implementation such as glibc's MALLOC_CHECK_=2 to debug
154                   erroneous slice allocation code, allthough <code class="literal">debug-blocks</code>
155                   usually is a better suited debugging tool.
156                 </p></td>
157 </tr>
158 <tr>
159 <td><p><span class="term">debug-blocks</span></p></td>
160 <td>
161 <p>
162                   Using this option (present since GLib-2.13) engages extra code
163                   which performs sanity checks on the released memory slices.
164                   Invalid slice adresses or slice sizes will be reported and lead to
165                   a program halt.
166                   This option is for debugging scenarios.
167                   In particular, client packages sporting their own test suite should
168                   <span class="emphasis"><em>always enable this option when running tests</em></span>.
169                   Global slice validation is ensured by storing size and address information
170                   for each allocated chunk, and maintaining a global hash table of that data.
171                   That way, multi-thread scalability is given up, and memory consumption is
172                   increased. However, the resulting code usually performs acceptably well,
173                   possibly better than with comparable memory checking carried out using
174                   external tools. An example of a memory corruption scenario that cannot be
175                   reproduced with <code class="literal">G_SLICE=always-malloc</code>, but will be caught
176                   by <code class="literal">G_SLICE=debug-blocks</code> is as follows:
177                   </p>
178 <pre class="programlisting">
179                     void *slist = g_slist_alloc(); /* void* gives up type-safety */
180                     g_list_free (slist);           /* corruption: sizeof (GSList) != sizeof (GList) */
181                   </pre>
182 <p>
183                 </p>
184 </td>
185 </tr>
186 </tbody>
187 </table></div>
188 <p title="G_SLICE">
189           The special value all can be used to turn on all options. 
190           The special value help can be used to print all available options.  
191         </p>
192 <p title="G_RANDOM_VERSION"><a name="G_RANDOM_VERSION"></a><b><code class="envar">G_RANDOM_VERSION</code>. </b>
193     If this environment variable is set to '2.0', the outdated
194     pseudo-random number seeding and generation algorithms from
195     GLib-2.0 are used instead of the new better ones. Use the GLib-2.0
196     algorithms only if you have sequences of numbers generated with
197     Glib-2.0 that you need to reproduce exactly.  
198   </p>
199 <p title="LIBCHARSET_ALIAS_DIR"><a name="LIBCHARSET_ALIAS_DIR"></a><b><code class="envar">LIBCHARSET_ALIAS_DIR</code>. </b>
200     Allows to specify a nonstandard location for the 
201     <code class="filename">charset.aliases</code> file that is used by the
202     character set conversion routines. The default location is the 
203     <em class="replaceable"><code>libdir</code></em> specified at compilation time.
204   </p>
205 </div>
206 <hr>
207 <div class="refsect2" title="Locale">
208 <a name="setlocale"></a><h3>Locale</h3>
209 <p>
210 A number of interfaces in GLib depend on the current locale in which
211 an application is running. Therefore, most GLib-using applications should
212 call <code class="function">setlocale (LC_ALL, "")</code> to set up the current 
213 locale.
214 </p>
215 <p>
216 On Windows, in a C program there are several locale concepts
217 that not necessarily are synchronized. On one hand, there is the
218 system default ANSI code-page, which determines what encoding is used
219 for file names handled by the C library's functions and the Win32
220 API. (We are talking about the "narrow" functions here that take
221 character pointers, not the "wide" ones.)
222 </p>
223 <p>
224 On the other hand, there is the C library's current locale. The
225 character set (code-page) used by that is not necessarily the same as
226 the system default ANSI code-page. Strings in this character set are
227 returned by functions like <code class="function">strftime()</code>.
228 </p>
229 </div>
230 <hr>
231 <div class="refsect2" title="Traps and traces">
232 <a name="id492704"></a><h3>Traps and traces</h3>
233 <p>
234
235
236
237 Some code portions contain trap variables that can be set during debugging 
238 time if GLib has been configured with <code class="option">--enable-debug=yes</code>. 
239 Such traps lead to immediate code halts to examine the current program state 
240 and backtrace.
241 </p>
242 <p>
243 Currently, the following trap variables exist:
244 </p>
245 <pre class="programlisting">
246 static volatile gulong g_trap_free_size;
247 static volatile gulong g_trap_realloc_size;
248 static volatile gulong g_trap_malloc_size;
249 </pre>
250 <p>
251 If set to a size &gt; 0, <a class="link" href="glib-Memory-Allocation.html#g-free" title="g_free ()">g_free</a>(), 
252 <a class="link" href="glib-Memory-Allocation.html#g-realloc" title="g_realloc ()">g_realloc</a>() and 
253 <a class="link" href="glib-Memory-Allocation.html#g-malloc" title="g_malloc ()">g_malloc</a>() will be intercepted if the size 
254 matches the size of the corresponding memory block. This will only work with 
255 <code class="literal">g_mem_set_vtable (glib_mem_profiler_table)</code> upon startup 
256 though, because memory profiling is required to match on the memory block sizes.
257 </p>
258 <p>
259 Note that many modern debuggers support conditional breakpoints, which achieve
260 pretty much the same. E.g. in gdb, you can do
261 </p>
262 <pre class="programlisting">
263 break g_malloc
264 condition 1 n_bytes == 20
265 </pre>
266 <p>
267 to break only on g_malloc() calls where the size of the allocated memory block
268 is 20. 
269 </p>
270 </div>
271 <hr>
272 <div class="refsect2" title="Gdb debugging macros">
273 <a name="id457417"></a><h3>Gdb debugging macros</h3>
274 <p>
275 glib ships with a set of python macros for the gdb debugger. These includes pretty
276 printers for lists, hashtables and gobject types. It also has a backtrace filter
277 that makes backtraces with signal emissions easier to read.
278 </p>
279 <p>
280 To use this you need a recent enough gdb that supports python scripting. Gdb 7.0
281 should be recent enough, but branches of the "archer" gdb tree as used in Fedora 11
282 and Fedora 12 should work too. You then need to install glib in the same prefix as
283 gdb so that the python gdb autoloaded files get installed in the right place for
284 gdb to pick up.
285 </p>
286 <p>
287 General pretty printing should just happen without having to do anything special.
288 To get the signal emission filtered backtrace you must use the "new-backtrace" command
289 instead of the standard one.
290 </p>
291 <p>
292 There is also a new command called gforeach that can be used to apply a command
293 on each item in a list. E.g. you can do
294 </p>
295 <pre class="programlisting">
296 gforeach i in some_list_variable: print *(GtkWidget *)l
297 </pre>
298 <p>
299 Which would print the contents of each widget in a list of widgets.
300 </p>
301 </div>
302 <hr>
303 <div class="refsect2" title="Memory statistics">
304 <a name="id457452"></a><h3>Memory statistics</h3>
305 <p>
306 g_mem_profile() will output a summary g_malloc() memory usage, if memory
307 profiling has been enabled by calling 
308 <code class="literal">g_mem_set_vtable (glib_mem_profiler_table)</code> upon startup.
309 </p>
310 <p>
311 If GLib has been configured with <code class="option">--enable-debug=yes</code>,
312 then g_slice_debug_tree_statistics() can be called in a debugger to 
313 output details about the memory usage of the slice allocator.
314 </p>
315 </div>
316 </div>
317 </div>
318 <div class="footer">
319 <hr>
320           Generated by GTK-Doc V1.13</div>
321 </body>
322 </html>