Running GLib Applications
Running GLib Applications — How to run and debug your GLib application
Running and debugging GLib Applications
The runtime behaviour of GLib applications can be influenced by a number of environment variables.
GLib reads standard environment variables like
GLib consults the environment variables
XDG_RUNTIME_DIR for the various XDG directories.
For more information, see the XDG basedir spec.
This environment variable can be set to a comma-separated list of character
set names. GLib assumes that filenames are encoded in the first character
set from that list rather than in UTF-8. The special token "@locale" can be
used to specify the character set for the current locale.
A list of log levels for which messages should be prefixed by the
program name and PID of the application. The default is to prefix
The possible values are
You can also use the special values
This environment variable only affects the default log handler,
A space-separated list of log domains for which informational
and debug messages should be printed. By default, these
messages are not printed.
You can also use the special value
This environment variable only affects the default log handler,
Causes GLib to abort the program at the first call to g_warning() or g_critical().
Causes GLib to abort the program at the first call to g_critical().
Newly allocated memory that isn't directly initialized, as well as memory being freed will be reset to 0. The point here is to allow memory checkers and similar programs that use Boehm GC alike algorithms to produce more accurate results.
All modules loaded by GModule will be made resident. This can be useful for tracking memory leaks in modules which are later unloaded; but it can also hide bugs where code is accessed after the module would have normally been unloaded.
All modules loaded by GModule will bind their symbols at load time, even when the code uses %G_MODULE_BIND_LAZY.
The special value all can be used to turn on all debug options. The special value help can be used to print all available options.
This will cause all slices allocated through
g_slice_alloc() and released by g_slice_free1() to be actually
allocated via direct calls to g_malloc() and g_free().
This is most useful for memory checkers and similar programs that
use Boehm GC alike algorithms to produce more accurate results.
It can also be in conjunction with debugging features of the system's
malloc() implementation such as glibc's MALLOC_CHECK_=2 to debug
erroneous slice allocation code, although
Using this option (present since GLib 2.13) engages extra code which performs sanity checks on the released memory slices. Invalid slice addresses or slice sizes will be reported and lead to a program halt. This option is for debugging scenarios. In particular, client packages sporting their own test suite should always enable this option when running tests. Global slice validation is ensured by storing size and address information for each allocated chunk, and maintaining a global hash table of that data. That way, multi-thread scalability is given up, and memory consumption is increased. However, the resulting code usually performs acceptably well, possibly better than with comparable memory checking carried out using external tools.
An example of a memory corruption scenario that cannot be
void *slist = g_slist_alloc (); /* void* gives up type-safety */ g_list_free (slist); /* corruption: sizeof (GSList) != sizeof (GList) */
The special value all can be used to turn on all options. The special value help can be used to print all available options.
If this environment variable is set to '2.0', the outdated
pseudo-random number seeding and generation algorithms from
GLib 2.0 are used instead of the newer, better ones. You should
only set this variable if you have sequences of numbers that were
generated with Glib 2.0 that you need to reproduce exactly.
Allows to specify a nonstandard location for the
charset.aliases file that is used by the
character set conversion routines. The default location is the
libdir specified at compilation time.
Allows to specify a nonstandard location for the timezone data files
that are used by the #GDateTime API. The default location is under
/usr/share/zoneinfo. For more information,
also look at the tzset manual page.
A number of interfaces in GLib depend on the current locale in which
an application is running. Therefore, most GLib-using applications should
setlocale (LC_ALL, "") to set up the current
On Windows, in a C program there are several locale concepts that not necessarily are synchronized. On one hand, there is the system default ANSI code-page, which determines what encoding is used for file names handled by the C library's functions and the Win32 API. (We are talking about the "narrow" functions here that take character pointers, not the "wide" ones.)
On the other hand, there is the C library's current locale. The
character set (code-page) used by that is not necessarily the same as
the system default ANSI code-page. Strings in this character set are
returned by functions like
glib ships with a set of python macros for the gdb debugger. These includes pretty printers for lists, hashtables and gobject types. It also has a backtrace filter that makes backtraces with signal emissions easier to read.
To use this you need a recent enough gdb that supports python scripting. Gdb 7.0 should be recent enough, but branches of the "archer" gdb tree as used in Fedora 11 and Fedora 12 should work too. You then need to install glib in the same prefix as gdb so that the python gdb autoloaded files get installed in the right place for gdb to pick up.
General pretty printing should just happen without having to do anything special. To get the signal emission filtered backtrace you must use the "new-backtrace" command instead of the standard one.
There is also a new command called gforeach that can be used to apply a command on each item in a list. E.g. you can do
gforeach i in some_list_variable: print *(GtkWidget *)l
Which would print the contents of each widget in a list of widgets.
SystemTap is a dynamic whole-system
analysis toolkit. GLib ships with a file
glib.stp which defines a
set of probe points, which you can hook into with custom SystemTap scripts.
See the files
are in your shared SystemTap scripts directory.
g_mem_profile() will output a summary g_malloc() memory usage, if memory
profiling has been enabled by calling
g_mem_set_vtable (glib_mem_profiler_table) upon startup.
If GLib has been configured with
then g_slice_debug_tree_statistics() can be called in a debugger to
output details about the memory usage of the slice allocator.