Solved

Linux GCC: debug libraries and building adjusted GCC configuration

Posted on 2011-03-12
18
1,210 Views
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

gcc-c++-4.1.2-48.el5
gcc-4.1.2-48.el5
libgcc-4.1.2-48.el5

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-1.4.2.0/jre --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,
Aleksey
0
Comment
Question by:Alexey Fedorov
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
18 Comments
 
LVL 34

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.
0
 
LVL 34

Accepted Solution

by:
Duncan Roe earned 167 total points
ID: 35119268
Q1.:
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.
Q2.:
There is no change to the link procedure.
Q3.:
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).
0
 

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:
     KERNEL32.dll
     MSVCR100.dll for release build, and

     KERNEL32.dll
     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.



0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Assisted Solution

by:Duncan Roe
Duncan Roe earned 167 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.
0
 

Author Comment

by:Alexey Fedorov
ID: 35126069
Hi, Duncan!

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

Assisted Solution

by:dcesari
dcesari earned 83 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.

0
 

Author Comment

by:Alexey Fedorov
ID: 35126932
Hi, Dcesari!

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

   [epel-source]
   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.


0
 

Author Comment

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

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.
0
 
LVL 34

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
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 35127274
Actually enabled=1
0
 

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
[debuginfo]
name=CentOS-$releasever - DebugInfo
# CentOS-5
baseurl=http://debuginfo.centos.org/$releasever/$basearch/
#baseurl=http://debuginfo.centos.org/$releasever/
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
protect=1
priority=1

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?
0
 

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?
0
 
LVL 34

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).
0
 
LVL 34

Expert Comment

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

Author Comment

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

Expert Comment

by:Duncan Roe
ID: 35128112
info sharedlibrary
0
 

Author Closing Comment

by:Alexey Fedorov
ID: 35128587
xxx
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

737 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