Revision Content

﻿This is the debugging Mozilla on Linux FAQ. It details some tricks that you can use to make it easier to debug and to work around some of the problems that GDB has.

GDB has problems that Mozilla exacerbates. The most serious of these problems is GDB's memory usage when debugging Mozilla's components. GDB can grow up to several hundred meg in size while debugging Mozilla. To work around this problem, it's necessary to load each of Mozilla's component libraries only when needed. This process is described in this FAQ. You will see this problem if you have less than 256 meg of ram in your computer.

Many distributions use a version of GDB that is somewhat old. The most recent version of GDB that is available at the time that this document is being written is version 5.0. Version 5.0 is much more usable than previous version for debugging Mozilla. It includes fixes for slow symbol loading and includes better thread functionality than previous versions. However, issues with regards to gdb's high memory usage with Mozilla are not solved with the 5.0 release of GDB.

This will actually launch ddd, a graphical front-end on gdb. Redhat 6.2 doesn't install ddd by default, so you may get an error message. In this case,

[blizzard@gunhead bin]$ setenv LD_LIBRARY_PATH .

(csh), or

[blizzard@gunhead bin]$ LD_LIBRARY_PATH=.; export LD_LIBRARY_PATH

(bash), then run

[blizzard@gunhead bin]$ gdb mozilla-bin

Using GDB is well beyond the scope of this document. There is documentation available on your system if you have GDB installed in the form of info pages. You might want to use the gnome help browser to read that docuementation as many people find the info reader on Linux hard to use.
Additionally, you can use a graphical front end to GDB like ddd or insight.
Here are some other sites that have more information:

The debugger uses a lot of memory. How do I fix it?

As mentioned in the introduction of this FAQ GDB uses a lot of memory when loading Mozilla's shared libraries. The solution to this is to delay loading shared libraries until they are actually needed.
However, you need to make sure that the base libraries like libc and pthreads are loaded before you tell GDB to stop loading shared libraries. If you don't allow those libraries to be loaded then GDB will not be able to properly debug threads on Linux. Mozilla uses pthreads for its networking library so you need to be able to work in a threaded environment.
The best way to do this is to set a breakpoint in main, let the program run until main and then turn off automatic library loading. From there you can allow the program to continue running. Here's an example:

It's easy to define a function for gdb to do this for you automatically. You can add this function to the .gdbinit file in your home directory:

def prun
tbreak main
run
set auto-solib-add 0
end

How do I load shared libraries?

If you have turned off automatic shared library loading, you will have to load them when you need them. GDB has a command to load libraries while the program is running. This is the sharedlibrary command. It can be shortened to shar when used in GDB. The argument to the command is a regular expression for the libraries to be loaded.
Here's what it looks like in the 4.18 version of the debugger:

^C
Program received signal SIGINT, Interrupt.
0x404ccdeb in __sigsuspend (set=0xbf5ffbc0)
at ../sysdeps/unix/sysv/linux/sigsuspend.c:48
48 ../sysdeps/unix/sysv/linux/sigsuspend.c: No such file or directory.
Current language: auto; currently c
(gdb) shar glib
Reading symbols from /usr/lib/libglib-1.2.so.0...done.
(gdb)

Here's what it looks like in a version 5.x of the debugger ( yet unreleased ):

GDB is taking forever to load a shared library. What the hell is it doing?

Versions of GDB earlier than 5 ( yet unreleased ) use a very slow algoritm for walking the list of already loaded symbols as it adds symbols to its symbol table. This has been fixed in version 5 of GDB. There isn't a fix for older versions.

How do I set a breakpoint in a library that hasn't been loaded?

Unfortunately, there isn't a way to set a breakpoint in GDB in a library that hasn't been loaded. If you are actually intersted in setting a breakpoint in a real component when it's loaded please see the section on setting a breakpoint when a component is loaded.
If you are completely desperate and have to set a breakpoint in a library when it's loaded you can set a breakpoint on the symbol _dl_open. This function is called when a new library is loaded. You can set your breakpoint after you see that your library has been loaded.

How do I set a breakpoint when a component is loaded?

There's a faculity in XPCOM that allows you to set an environment variable that triggers the program to drop into the debugger when it loads a certain component.
You have to set the XPCOM_BREAK_ON_LOAD environment variable before you run Mozilla. You set the variable to a string that contains the names of libraries you might want to load. For example, if you wanted to stop when a library named raptor or necko is loaded, you can set the variable to raptor:necko. Here's an example:

I can't set a breakpoint. Why not?

You probably can't set a breakpoint because the library in which that breakpoint is located hasn't been loaded. If you know the library has actually been loaded into Mozilla but you can't set the breakpoint see the section on loading shared libraries. If the library hasn't been loaded yet you will have to wait until it is. If you want to set a breakpoint as soon as the library is loaded, see the section on breaking when a component is loaded and breaking on a library load.

Debian's GDB doesn't work. What do I do?

[submitted by Bruce Mitchener] Debian's unstable distribution currently uses glibc 2.1 and GDB 4.18. However, there is no package of GDB for Debian with the appropriate threads patches that will work with glibc 2.1. I was able to get this to work by getting the GDB 4.18 RPM from Red Hat's rawhide server and installing that. It has all of the patches necessary for debugging threaded software. These fixes are expected to be merged into GDB, which will fix the problem for Debian Linux.

How do I display an nsString?

You can call the ToNewCString() method on the nsString in question. It leaks a little memory but it shouldn't hurt anything if you only do it a few times in one gdb session. ( from akkana@netscape.com )

(gdb) p string.ToNewCString()

Another method (via bent) is the following (replace n with: the returned length of your string):

(gdb) p string.Length()
$1 = n
(gdb) x/ns string.BeginReading()

How do I determine the concrete type of an object pointed to by an interface pointer?

You can determine the concrete type of any object pointed to by an XPCOM interface pointer by looking at the mangled name of the symbol for the object's vtable:

(If you're using gcc 3.x, the output is slightly different from the above (which is for gcc 2.9x), but just pay attention to the vtable symbol (which in this case would be _ZTV14nsRootBoxFrame) rather than the mangled name of the first virtual function, since some classes may not override the first virtual function (although they usually do, since it's QueryInterface).)
Note that you won't get anything useful if the shared library containing the implementation of the object you're looking at is not loaded. See "How do I load shared libraries?" and "How do I see what libraries I already have loaded?".

"run" or "prun" in GDB fails with "error in loading shared libraries."

Attempting to run mozilla-bin inside GDB fails with an error message similar to the following:

Your LD_LIBRARY_PATH is probably being reset by your .cshrc or .profile. From the GDB manual:
*Warning:* GDB runs your program using the shell indicated by your 'SHELL' environment variable if it exists (or '/bin/sh' if not). If your 'SHELL' variable names a shell that runs an initialization file -- such as '.cshrc' for C-shell, or '.bashrc' for BASH--any variables you set in that file affect your program. You may wish to move setting of environment variables to files that are only run when you sign on, such as '.login' or '.profile'.

How do I pass arguments over to the app using GDB/ddd?

You should now be able to convey arguments using ./mozilla -g your-list-of-arguments For example you can call the mozilla script like this if you want to launch mail at startup: ./mozilla -g -mail

How do I pass arguments in prun?

Set the arguments in GDB before calling prun. Here's an example on how to do that:

Mozilla is aborting. Where do I set a breakpoint to find out where it is exiting?

On Linux there are two possible symbols that are causing this. They are PR_ASSERT() and NS_ASSERTION(). To catch the program before it exits to see where it's asserting you can stop at two places:

(gdb) b abort
(gdb) b exit

I keep getting a SIG32 in the debugger. How do I fix it?

If you are getting a SIG32 while trying to debug Mozilla you might have turned off shared library loading before the pthreads library was loaded, e.g. by having set auto-solib-add 0 in your .gdbinit file.
In this case you can either:

Alternatively the problem might lie in your pthread library. If this library has its symbols stripped, then GDB can't hook into thread events, and you end up with SIG32 signals. You can check if your libpthread is stripped by checking the output of file /lib/libpthread* for 'stripped'. To fix this problem for Gentoo Linux, you can re-emerge glibc after adding "nostrip" to your FEATURES in /etc/make.conf.

How can I debug race conditions and/or how can I make something different happen at NS_ASSERTION time?

{{ mediawiki.external('submitted by Dan Mosedale') }}
Since Linux is unable to generate useful core files for multi-threaded applications, tracking down race-conditions that don't show up under the debugger can be a bit tricky. Unless you've given the --enable-crash-on-assert switch to configure, you can now change the behavior of NS_ASSERTION (actually nsDebug::Break) using the XPCOM_DEBUG_BREAK environment variable. See the corresponding article for details on its possible values.

What performance tools do I have on Linux?

The amazing Jim Nance has written a tool called jprof that uses sampling to do performance analysis. Other tools are listed on the performance tools page.
Unfortunately, there aren't tools like Quantify available for Linux. You will have to go run {{ mediawiki.external('quantify.html Quantify') }} on {{ mediawiki.external('solaris.html Solaris') }} for that.

How do I run the debugger in emacs/xemacs?

Emacs and XEmacs contain modes for doing visual debugging that many programmers use. However, you might want to set up environment variables to make sure that when running the debugger and Mozilla they know where to load symbols from and where to find components.
The easiest way to set up those environment variables is to use the run-mozilla.sh script that's in the dist/bin directory of your build. This script will set up the environment that you need to run the editor, shell or debugger. Another trick is to use the script to run /bin/bash ( or your favorite shell ) to set up a shell that has the right setup and then you can run any commands you want inside it.

I'm feeling brave (or my old gdb isn't working at all). How can I try out gdb 5.0?

gdb 5 used to work for me, but now Mozilla won't start. What can I do?

A recent threading change (see bug 57051 for defails) caused a problem on some systems: Mozilla would get partway through its initialization, then die before showing a window.
A recent change to gdb has fixed this problem. Download and build [ http://sources.redhat.com/insight/ the latest version of Insight], or if you don't want a GUI, the latest version of gdb.

Mozilla crashes at startup when I run it under Valgrind. What can I do?

GTK 2 builds of Mozilla bring in ORBit, which is sensitive to the memory alignment used by malloc(). The system implementation aligns to 8 bytes, while Valgrind aligns to 4 bytes by default. This causes ORBit to crash.
To run Mozilla under Valgrind, it is recommended that you use the --alignment=8 option to Valgrind.

How do I get useful stack traces inside system libraries?

Many Linux distributions provide separate packages with debugging information for system libraries that you can install if you want gdb, valgrind, profiling tools, etc., to give useful stack traces for stacks that go through system libraries.

Fedora 8

On Fedora, you need to enable the debuginfo repositories, since the packages with debug symbols are provided in separate repositories. It is best to enable them permanently, since then you'll get the updates to the debug symbols when you get security updates for the packages. The best way I know to do this is simply to edit /etc/yum.repos.d/fedora.repo and fedora-updates.repo to change the enabled=0 line in the debuginfo section to enabled=1. Doing this will require dealing with the conflict (and redoing the change, I think) when upgrading to a new distribution version.

Once you do this, you can install debuginfo packages with yum or other package management tools. Installing a roughly complete set for debugging Mozilla can be done using: