eclipse CDT pretty-printing not working for strings - c++

I am trying to Get gdb pretty-print working in eclipse, it seems to be working for all stl elements and containers baring std::string
basically if i have a vector like:
std::vector<std::string> m_vec = {"hello" , "world"};
each element of the vector shows a string and it shows the contents like "hello" and "world".
but if i have a code like
std::string m_string = "hello world";
m_string shows up empty, even though i can do string operations on the contents. Not sure why only strings alone is causing as issue with pretty print.
Any help/pointers would be much appreciated.
Edited:
Few more details regarding the setup:
IDE: Eclipse Luna 4.4.2
Compiler: Cygwin g++ 5.4.0
Debugger: Cygwin gdb 7.10.1
phython : Cygwin phython 3.6
Update: Don't know how exactly, but windows restart solved it, can see string now in preety-print.

For adding support for gdb pretty-printing in cygwin you need the following file available:
/usr/share/gdb/auto-load/usr/bin/cygstdc++-6.dll-gdb.py
It is available in gcc-debuginfo-5.4.0-1 package in cygwin package installer.
Note: This might break once packages get updated in Cygwin repository. So you will need to again locate this file in the Cygwin package search.
Update: Check the version of python interpreter supported by gdb by running the following commands in the gdb console:
(cygwin console) $ gdb
(gdb) python
>import sys
>print(sys.version)
>end
You should see output something like this(in my case it defaults to python2.7 interpreter):
2.7.13 (default, Mar 13 2017, 20:56:15)
[GCC 5.4.0]

Related

C++ gdb pretty printing in Ubuntu 18.04 visual studio code

I am trying to make pretty printing to work on Ubuntu 18.04 from visual studio code 1.64.2.
I tried to follow instructions initially from here and then the answer by Devymex as detailed in here.
Then further digging up revealed that the gdb pretty printing itself is not working as I tried to build, make, and run my code outside of VSCode. I had gcc 7.5 preinstalled on Ubuntu 18.04 and then I installed 11.2. But nothing worked.
The code I am trying to run
#include <string>
#include <iostream>
std::string str = "hello world";
int main ()
{
std::cout << str << std::endl;
return 0;
}
The output I get while debugging with gdb
Additionally, I tried to check which pretty-printer is configured or set up by typing info pretty-printer from within (gdb). But it appears that the appropriate pretty printer is not configured.
I tried reinstalling gcc 11.2 from the source by configuring with python using both python 2.7 and python 3.6.9 using the command ./configure --with-python and ./configure --with-python3. But nothing worked!
Can anyone please help me out?
I found a solution as posted here. The gdb was not able to find the location where the python printers.py was located. The file was located under /usr/share/gcc/python/libstdcxx/v6/printers.py.
What I needed to do is create a .gdbinit file on my home directory including the following lines of code
python
import sys
sys.path.insert(0, '/usr/share/gcc/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
Next source the file with source .gdbinit and then try again the info pretty-print. All the alternate options are now available. Subsequently, gdb debugging and vscode was able to show the contents of the C++ STL containers.

Unable to use standard library debugging symbols in gdb

I am using this code as a test.
#include <sstream>
#include <iostream>
int main () {
std::stringstream ss;
ss << "This is a test\n";
std::cout << ss.str();
}
I compile with
g++ -O0 -g test.cpp. When I run the program in gdb and stop at a breakpoint on the cout line, trying to print ss or ss.str() fails.
(gdb) p ss
$1 = <incomplete type>
(gdb) p ss.str()
Couldn't find method std::stringstream::str
gdb also gives me a ton of warnings about the debug information for libstdc++ and libc not matching their respective libraries, followed by an additional warning suggesting I install separate debuginfos.
warning: the debug information found in "/usr/lib/debug/usr/lib64/libc-2.17.so.debug" does not match "/lib64/libc.so.6" (CRC mismatch).
warning: the debug information found in "/usr/lib/debug/usr/lib64/libstdc++.so.6.0.19.debug" does not match "/lib64/libstdc++.so.6" (CRC mismatch).
Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7_6.6.x86_64 libstdc++-4.8.5-36.el7_6.2.x86_64
However, these packages are already installed according to rpm -qa. I don't have permissions to attempt to reinstall these or try other suggestions from this similar question.
I also found this question and checked the debug-file-directory that gdb is using.
(gdb) show debug-file-directory
The directory where separate debug symbols are searched for is "/usr/lib/debug".
I am running RHEL7 with the included versions of g++ and gdb.
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-114.el7
The command debuginfo-install glibc-2.17-260.el7_6.6.x86_64 libstdc++-4.8.5-36.el7_6.2.x86_64 will install debugging information for those packages, not the packages themselves.
It seems that a different version of the debugging information has already been installed on the system, so you should be able to request that your system administrator installs matching package versions. (Some companies have policies against installing compilers and debuggers in production, but that does not seem to apply here.)
If you cannot get the correct debuginfo package versions installed on the system, you can download the packages from the Red Hat Customer Portal, copy it to the machine, unpack them using rpm2cpio … | cpio -id, and point GDB to the extracted debugging information. As of this writing, Red Hat does not offer a public symbol server unfortunately.
Note that packages from CentOS will not work even if they have the same name/version/release because they are not binary-identical due to different build environments.

QMake not matching regex for distro detection

I have a C++ project using QMake. I'm trying to set some compiler options based on a simple test of which Linux distro is running, but the test does not pass. My qmake file contains:
OSDISTRO = $$(cat /proc/version)
contains(OSDISTRO, "Ubuntu"): {
message(Found ubuntu)
}
I tested the regex from the command line and it works!
cat /proc/version | pcregrep "Ubuntu"
Linux version 4.18.0-20-generic (buildd#lcy01-amd64-020) (gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3)) #21~18.04.1-Ubuntu SMP Wed May 8 08:43:37 UTC 2019
Is there something special about the regex syntax in qmake? Any reason why this isn't working?
For the RegEx:
This works for me:
OSDISTRO = $$system(cat /proc/version)
contains(OSDISTRO, .*Ubuntu.*){
message("Found Ubuntu")
}
Note:
The match is case sensitive.
You can use .*[uU]buntu.* for example to match ubuntu and Ubuntu.
Explanation why your solution does not work:
The QMake function contains works with lists of values.
So, the execution of you solution will be like this:
1) First instruction OSDISTRO = $$(cat /proc/version):
QMake will execute $$system(cat /proc/version).
Then the result is splitted (by space as separator) to list of values. OSDISTRO will contain this list.
Assuming that the result is as yours. The result of the first instruction is like this:
OSDISTRO = "Linux" "version" "4.18.0-20-generic"....
2) Second instruction contains(OSDISTRO, "Ubuntu") : message(Found ubuntu):
QMake will search if the variable OSDISTRO contains the value Ubuntu and display the message Found ubuntu if success.
Here in your case, QMake will never find Ubuntu, cause the value which contains it is like this (Ubuntu 7.3.0-16ubuntu3) and QMake search only the value Ubuntu.
Hope it helps you.

Eclipse CDT prettyprint broken

I am using Ubuntu 13.10, Eclipse Kepler Service Release 1, GDB 7.6.1-ubuntu and latest CDT available through the Eclipse "install new software". I followed the instructions posted on several sites to set up prettyprinting for the STL containers.
Since it didn't work this way, following other instructions I modified the printers.py to have the len of strings maximized to 100 and formatted all raise ValueError, "..." to raise ValueError("...").
My .gdbinit file is the following:
python
import sys
sys.path.insert(0, '/home/fbence/stlPrettyPrinter')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
My settings are these:
http://fbence.web.elte.hu/setup.png
If I go to the debug button it has my configuration set as first, and it seems to use it too (otherwise I wouldn't had have to change the printers.py I think), but the debug as... is empty.
The gbd traces have messages like these:
193,677 36^error,msg="Could not get children iterator".
193,674 35^done,name="var5",numchild="0",value="{static npos = , _M_dataplus = {> = {<__\
gnu_cxx::new_allocator> = {}, }, _M_p = 0x0}}",type="std::stri\
ng",thread-id="1",displayhint="string",dynamic="1",has_more="0"
This is the output: http://fbence.web.elte.hu/eclipseproblem.png
Using gdb from the terminal results in the following output, when I want to print a simple vector:
Python Exception <class 'TypeError'> iter() returned non-iterator of type '_iterator':
$3 = std::vector of length 6, capacity 16
Obviously, my question is, how do I fix this? Without prettyprint I'd just rather debug under windows, but I don't really want to do that:)
gdb libstdc++ pretty printers are actually broken in Ubuntu 13.10 now. See this bug for example.
gdb is linked with Python3 in Ubuntu 13.10, while pretty printers scripts support only Python2 syntax. You may apply this patch to you pretty printers. It will add support of Python3. This solution worked for me though I don't use Eclipse for debugging.

Better variable exploring when debugging C++ code with Eclipse/CDT

Using Eclipse and CDT to debug C++ code the variable windows is cumbersome and not very informative for types defined in the standard template library or in boost (e.g. shared_ptr).
Just an example how this may look like for an std::vector:
bar {…}
std::_Vector_base<TSample<MyTraits>, std::allocator<TSample<MyTraits> > >
_M_impl {…}
std::allocator<TSample<MyTraits> > {…}
_M_start 0x00007ffff7fb5010
_M_finish 0x00007ffff7fd4410
_M_end_of_storage 0x00007ffff7fd5010
Even if this information about the internals of those types may be useful, in almost any cases I would expect a clearer presentation here, i.e. a list of values for the std::vector. Are there any tools, plugins or other modifications around which can do this?
EDIT
The following solutions does not work for linux. I am using ubuntu 14.04, eclipse, g++, gdb.
I cant find a package gdb-python and linux does not use mingw
You need a version of GDB capable of using python to pretty print structures. I know at least on windows using mingw that this is not provided in the default install.
Pretty Printers are python modules which tell gdb how to display a given structure. You can write your own, but there are already printers for STL available for download.
To Get Pretty Printers working on Windows (instructions should be similiar for other OS's):
Prerequisites
Make sure you have you have Python 2.7 installed and in the system path.
http://www.python.org/download/
Make sure MinGW-get is installed
http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/
Make sure you have an SVN client are installed
Installation:
Open a command Shell and type:
mingw-get install gdb-python
When its finished cd to a local directory and install the printers by typing:
svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
Open the .gdbinit (create it in a text editor if need be) and type the following replaceing "C:/directory" with the folder that you checked the printers into.
Python
import sys
sys.path.insert(0, 'C:/directory')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
Eclipse Setup
Go To Windows > Preferences > C/C++ > Debug > GDB
Where it Says GDB Debugger put the path to the python enabled GDB it will most likely be in the mingw /bin folder with a name like gdb-python27.exe
Where it says GDB Command File put the path to the .gdb init file you made earlier.
That's it, debug like normal, the stl structures should be much easier to read.
Well, gdb don't natively support STL containers. You can't say this is incorrect, since it will expose the inner workings of the STL objects, but most of the time it is not what we want, right?
If you're using gdb 7.0 you can take advantage of the pretty printers. This website http://sourceware.org/gdb/wiki/STLSupport has a pretty simple tutorial on how to set them. I copied below the part that interests you:
Check-out the latest Python libstdc++ printers to a place on your
machine. In a local directory, do:
svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
Add the following to your ~/.gdbinit. The path needs to match
where the python module above was
checked-out. So if checked out to:
/home/maude/gdb_printers/, the path
would be as written in the example:
python
import sys
sys.path.insert(0, '/home/maude/gdb_printers/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
The path should be the only element
that needs to be adjusted in the
example above. Once loaded, STL
classes that the printers support
should printed in a more
human-readable format. To print the
classes in the old style, use the /r
(raw) switch in the print command
(i.e., print /r foo). This will print
the classes as if the Python
pretty-printers were not loaded.
Since you're using eclipse cdt, don't forget to point your debug configuration to your .gdbinit file. When creating a new Debug Configuration, go to the Debugger tab and put the path to the .gdbinit file in the "GDB command file" field.
I hope that helps!
In debug view in variables list expand vector:
"vector_name" -> std::_Vector_base<"datatype"> -> _M_impl
then right click on _M_start and select "Display as array...", type its length and then click OK. Now you can expand each item of your vector.
If you have gdb support for CDT (see, for example, GDB in Eclipse), you could try this: De-referencing STL containers
Long ago I also stumbled upon your same problem. It was a pain to check the STL containers. Then I found that link and added to my .gdbinit file some of those definitions. Life was easier after that.
NOTE: My gdb version is 7.1 and adding those definitions work fine. I don't know if in newer versions of gdb they are already included.
I would like to expand on the Windows 7 response because some key steps are left out:
This is for MinGW users with Eclipse CDT
0) If you don't have python GDB, open a shell/command and use MinGW-get.exe to 'install'
Python-enabled GDB e.g.
MinGw-get.exe install gdb-python
1a) Get Python 2.7.x from http://python.org/download/ and install
1b) Make sure PYTHONPATH and PYTHONHOME are set in your environment:
PYTHONPATH should be C:\Python27\Lib (or similar)
PYTHONHOME should be C:\Python27
1c) Add PYTHONHOME to your PATH
%PYTHONHOME%;...
2a) Open a text enter, enter the following statements. Notice the 3rd line is
pointing to where the python scripts are located. See notes below about this!
python
import sys
sys.path.insert(0, 'C:/MinGW/share/gcc-4.6.1/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
2b) Save as '.gdbinit' NOTE: Windows explorer will not let you name a file that starts with
with a period from explorer. Most text edits (including Notepad) will let you. GDB init
files are like 'scripts' of GDB commands that GBD will execute upon loading.
2c) The '.gdbinit' file needs to be in the working directory of GDB (most likely this is
your projects root directory but your IDE can tell you.
3) Open your Eclipse (or other IDE) Preferences dialog. Go to the C++ Debugger sub-menu.
4) Configure Eclipse to use C:\MinGW\bin\gdb-python27.exe as the debugger and your .gdbinit as the config file.
5a) Re-create all your debug launch configurations (delete the old one and create a new one from scratch).
--OR--
5b) Edit each debug configuration and point it to the new gdb-python.exe AND point it to the.
If you run into issues:
--Don't forget to change the location to the python directory in the above python code!
This directory is created by MinGW, so don't go looking to download the pretty printers, MinGW
did it for you in step zero. Just goto your MinGW install director, the share folder,
the GCC folder (has version number) and you will find python folder. This location is what
should be in python script loaded by GDB.
--Also, the .gdbinit is a PITA, make sure its named correctly and in the working folder of GDB
which isn't necessarily where gdb-python.exe is located! Look at your GDB output when loading GDB to see if a) 'python-enabled' appears during load and that the statements in the .gdbinit are appearing.
--Finally, I had alot of issues with the system variables. If python gives you 'ImportError' then most likely you have not set PYTHONPATH or PYTHONHOME.
--The directory with 'gdb-python27' (e.g. C:\MinGW\bin') should also be on your path and if it is, it makes setting up eclipse a bit nicer because you don't need to put in absolute paths. But still, sometimes the .gbdinit needs an absoulte path. if it works you'll see output from gbd (console->gdb traces) like this on startup of debugger:
835,059 4^done
835,059 (gdb)
835,059 5-enable-pretty-printing
835,069 5^done
....
835,129 12^done
835,129 (gdb)
835,129 13source C:\MinGW\bin\.gdbinit
835,139 &"source C:\\MinGW\\bin\\.gdbinit\n"
835,142 13^done
835,142 (gdb)
I know that JDT (Java environment in eclipse) provides custom "formatters" to be applied when displaying variable values in debug views. A quick look at google for the same in CDT brings this page:
http://wiki.eclipse.org/CDT/Better_Debugging_%28GSoC_project%29
I don't know if this has been yet integrated in the main CDT line, may be you can try to right click on a variable while debugging (in the last CDT) and see if there is a custom formater entry. If not available I recomend you to add a new tracker entry in CDT tracker to ask this enhancement.