Linux GCC: debug libraries and building adjusted GCC configuration

Posted on 2011-03-12
Medium Priority
Last Modified: 2012-05-11
Hi, all!

I'm new in Linux development, starting C++ development for CentOS 5.5 in C++. I'm a WIndows programmer and used to C++ development in Visual Studio. So, I have some Linux-specific related questions and would appreciate any assistance. Time to time I'm going to post some questions.

I got Centos 5.5 in a VmWare image. So, GCC was configured by somebody, maybe in the default way. I need to adjust or rebuild it for my needs.

It seems GCC was installed via rpm packages:
rpm -qa | grep gcc


Here is my GCC configuration:
  version 4.1.2 20080704 (Red Hat 4.1.2-48)
  Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj- --with-cpu=generic --host=i386-redhat-linux
Thread model: posix

1. The first thing I faced with: there aren't any debug libraries (*.a, *.so) for default libraries.
I build a hello world application as:
COMPILE=g++ -c  "-D_GLIBCXX_DEBUG" -g -O0 -fno-inline -o "$(OUTDIR)/$(*F).o" $(CFG_INC) $<
LINK=g++  -g -o "$(OUTFILE)" $(ALL_OBJ)

ldd shows these dependences:
                linux-gate.so.1 =>  (0x00fd7000)
                libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x02839000)
                libm.so.6 => /lib/libm.so.6 (0x00c54000)
                libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x02702000)
                libc.so.6 => /lib/libc.so.6 (0x00ad9000)
                /lib/ld-linux.so.2 (0x00aba000)

All the libs are release. So, I can't step into them in the debugger.

Q1.: How to add debug default libraries and their sources to GCC installation?

Q2.: How to link properly my application in DEBUG configuration to debug libraries? In Visual Studio in debug conf. an application is linked to debug versions of std. libraries suffixed by "d". What's approach is used in Linix GCC?

Q3. What could you say about my GCC configuration shown above? Definitely, I would rebuild GCC with C, C++ turned on languages only. Any other advises about the configuration? I need c/c++, x86 32/64 bit, with debug std libs configuration.
Also, where could I find a step by step instruction how to build and install GCC configuration I need?

With best regards,
Question by:Alexey Fedorov
  • 9
  • 8
LVL 35

Expert Comment

by:Duncan Roe
ID: 35119214
Only libgcc_s.so.1 and libstdc++.so.6 are part of the gcc distribution. The others, which get used far more often, are part of glibc. You can (I have) rebuild glibc so that debugging is available, but why do you want to? (if you don't mind my asking). The libraries end up with the same names but contain debug information which makes them considerably bigger but not AFAIK much slower. Of course if you drop the optimising level to give cleaner debugging then they will be somewhat slower.
LVL 35

Accepted Solution

Duncan Roe earned 501 total points
ID: 35119268
Gcc builds with debug on by default, but the installer strips debug information. To get around that, do a full source build and install, then copy by hand those items you wish to debug.
There is no change to the link procedure.
I would leave it as_is: it's very hard to get ada back if you drop it. You need to get the srpms e.g. gcc-c++-4.1.2-48.el5.srpm. Then build using rpmbuild. Verify the installed gcc is smaller than the built one in the build directory. That will be because of debug info being stripped. Copy it. Note that gcc is a front-end to the actual compilers, which have names like cc1, cc1plus, cc1obj and so on. You need to copy these as well. They will be in a revision-dependent directory (on my system they are in /usr/libexec/gcc/i486-slackware-linux/4.4.4, but yours will be different).

Author Comment

by:Alexey Fedorov
ID: 35123793
Thanks, Duncan.

"but why do you want to?" - sometimes stepping into libs may be helpful because unmanaged C++ code is prone to memory allocation and corruption errors causing fault in libs. Also, on Windows in MS Visual Studio we have this ability without any extra effort, so I consider that as usual feature.

I understand the simplest way: to download GCC source code and build it in a separate dir. Than, copy debug libs and their sources manually into my GCC installation dirs. And, the same steps I should do to GLIBC. Am I correct?

You told "Gcc builds with debug on by default". I thought it builds debug when --enable-libstdcxx-debug is specified: http://gcc.gnu.org/onlinedocs/libstdc++/manual/debug.html  Isn't it?

Where debug libraries must be reside at, and, why I don't need to do special setting to link to them?

On WIndows for a WIn32 C++ console app in Visual Studio 2010 it looks as follow.
  dumpbin /dependents shows:
     MSVCR100.dll for release build, and

     MSVCR100D.dll for debu build.

MSVCR100 resides in WIndows/System32 folder. It exists there in different versions with different names: msvcrt.dll, msvcrt20.dll, msvcrt40.dll plus debug versions with suffix 'd'. Compiler automatically links to 'd' version in debug build configuration and to release version in release build. So, it works very clearly and gives ability to step into default stuff like "printf" in debug mode, that is convenient.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 35

Assisted Solution

by:Duncan Roe
Duncan Roe earned 501 total points
ID: 35125114
unmanaged C++ code is prone to memory allocation and corruption errors causing fault in libs - yes I am very familiar with that ;)
The idea of stepping into system libraries to address such problems is, I must confess, new to me but if it works for you then go for it. The problem with debugging the library is, of course, that the code in the library is usually fine: the bug that caused the storage arena corruption executed some time previously.
People unable to otherwise solve such problems generally use a package to help them, such as (free) Valgrind, (free) DUMA (formerly Electric Fence) or (paid for) IBM Rational Purify

Yes download gcc source code and build it in a separate dir. No need to move the source - use gdb dir command (you would need to anyway).
Building glibc is not for the faint-hearted. Best is to use the distributor's build process (i.e. get the SRPM). Be sure to run the self-tests. Be especially sure to run the self-tests if you change anything.

The page you refer to says the same as I did: The default optimizations and debug flags for a libstdc++ build are -g -O2. So debug (-g) is on by default. But the page does explain how to improve on that, as you mention (thanks for that btw - I did wonder why I couldn't debug new() :)

As per the web page, you can put debug libstdc++ in a special dir. glibc you can not. If you install a debug version then every process gets to use it. For that reason, there is no special linking.

Linux does things differently from Windows.

Author Comment

by:Alexey Fedorov
ID: 35126069
Hi, Duncan!

I've found there are debu rpm packages: glibc-debuginfo, glibc-debuginfo-common.

Assisted Solution

dcesari earned 249 total points
ID: 35126602
Concerning Q1, nowadays rpm-based distributions (at least Fedora which I use, and consequently CentOS) provide, for most library packages, a corresponding  *-debuginfo package which contain separate debugging symbols and source files for the shared libraries, these symbols are usually not used, but they are loaded by gdb, so that you can step into library functions.

So, if you wish to step into a function which is in a library belonging, e.g., to zlib package, you have to install a package like that (as root):

yum install zlib-debuginfo

then launch gdb as usual. The packages you need are probably glibc-debuginfo and gcc-debuginfo.

Before doing this you may have to enable the debuginfo repositories by setting "enabled=1" in the corresponding entries in /etc/yum.repos.d/*.repo, if they are disabled.

I anyway agree with the previous posts that it may be quite useless to step into libc functions, it is usually enough to know which is the function call that generates a segfault or whichever error.


Author Comment

by:Alexey Fedorov
ID: 35126932
Hi, Dcesari!

I turned on:
  name=Extra Packages for Enterprise Linux 5 - $basearch - Debug

   name=Extra Packages for Enterprise Linux 5 - $basearch - Source

But, couldn't find glibc, gcc debuginfo. I found them as separate roms at http://debuginfo.centos.org/5/i386/

By the way, I updated glibc from "update" repository: yum update glibc.


Author Comment

by:Alexey Fedorov
ID: 35126977
How could I find a repository by package?

Author Comment

by:Alexey Fedorov
ID: 35127053
I've found out here http://wiki.centos.org/AdditionalResources/Repositories

I need to add "debuginfo"  repository. I can do it manually in /etc/yum.repos.d/  
how it is described here: http://wiki.centos.org/AdditionalResources/Repositories/DebugInfo

I wonder how to add it via yum without manual editing the config.
LVL 35

Expert Comment

by:Duncan Roe
ID: 35127259
I run Slackware, so am not especially familiar with Yum. It looks to me from the Web pages that you may not have to manually edit the config. Instead it seems you could create a new file such as /etc/yum.repos.d/CentOS-Debug.repo. Usually the way xxx.d dirs work is that all contained files are effectively concatenated - sometimes only those files that have execute access. Do an ls -l /etc/yum.repos.d to see if the execute bit is required. You might even find that the debuginfo file is there already, without execute.
In the new file, you would have to specify enable=1 for Centos 5
LVL 35

Expert Comment

by:Duncan Roe
ID: 35127274
Actually enabled=1

Author Comment

by:Alexey Fedorov
ID: 35127275
I ended up with adding the repository.

1. Added CentOS-Debug.repo into /etc/yum.repos.d/ dir:

#Debug Info
name=CentOS-$releasever - DebugInfo
# CentOS-5

2. Excecuted:
yum --disablerepo "*" --enablerepo "debuginfo" install glibc-debuginfo glibc-debuginfo-common gcc-debuginfo

Now I compiled a test program with: "-D_GLIBCXX_DEBUG" -g -O0 -fno-inline
But, I still unable to step into default functions (for inst. wprintf). Why?

Author Comment

by:Alexey Fedorov
ID: 35127332
One more question. in MS Visual Studio there is a nice feature: at starting debugging up it shows in Output window all the loaded libs into debugged process and reports whether symbols are found or not for each lib.

How could I i view the same in SlickEdit 15 attached to gdb under Linux?
LVL 35

Expert Comment

by:Duncan Roe
ID: 35127358
What files did Yum install and where did it put them? "-D_GLIBCXX_DEBUG" is unlikely to help you because you will after all need to tell the linker to do something special if indeed Yum has put the debug libraries in some special dir. (Red Hat (and derivatives like CentOS) tend to be rather more profligate with disc space than Slackware, at least AFAICS).
LVL 35

Expert Comment

by:Duncan Roe
ID: 35127368
Sorry - I have not yet tried to use SlickEdit

Author Comment

by:Alexey Fedorov
ID: 35127487
Maybe, some gdb command to list loaded modules?
LVL 35

Expert Comment

by:Duncan Roe
ID: 35128112
info sharedlibrary

Author Closing Comment

by:Alexey Fedorov
ID: 35128587

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question