Author Topic: How can watch the contents of std::containers?  (Read 345 times)

Offline chameleon

  • Multiple posting newcomer
  • *
  • Posts: 34
How can watch the contents of std::containers?
« on: May 15, 2020, 11:06:57 am »
I am not sure that it is a Code::Blocks question or off-topic.

I work with g++/gdb and I cannot debug my programs without placing std::cout all over my code.

The reason is that gdb shows the internal structure of e.g. std::vector instead of what I want: the contained elements as array.
So, given the start vector pointer and the end pointer, I cannot debug. I must inspect the memory which is a NO. Better std::cout for logging.
I give and a screenshot with meaningless values.


Also, 'this' and other pointers are given with their memory pointer value which is almost useless. I want the contents.
Here there is a fast workaround to place extra watch *this but why Code::Blocks does not give both by default? Not for 'this' only. For every pointer.
« Last Edit: May 15, 2020, 11:09:43 am by chameleon »

Offline hongwenjun

  • Multiple posting newcomer
  • *
  • Posts: 13
Re: How can watch the contents of std::containers?
« Reply #1 on: May 15, 2020, 01:07:04 pm »
Please search for gdbinit in the MinGW directory.

Debugger setting  -->  default -->  Initialization command

source $(TARGET_COMPILER_DIR)etc\gdbinit



New result


Offline chameleon

  • Multiple posting newcomer
  • *
  • Posts: 34
Re: How can watch the contents of std::containers?
« Reply #2 on: May 15, 2020, 03:40:05 pm »
Thanks pal.
I really understand that, I am doing something wrong.

I cannot make your solution work.

I use this binary of Mingw64:
http://winlibs.com/ the 10.1 version.

There is no etc\gdbinit
I will appreciate if you can be a little bit more accurate, based on my distribution of mingw64.

Thanks.

PS: I really understand that my post is off-topic too.

Offline chameleon

  • Multiple posting newcomer
  • *
  • Posts: 34
Re: How can watch the contents of std::containers?
« Reply #3 on: May 15, 2020, 04:29:31 pm »
It seems that I run a GDB without Python support.
So, there is no solution.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12625
    • Travis build status
Re: How can watch the contents of std::containers?
« Reply #4 on: May 15, 2020, 06:27:27 pm »
There is a solution - install gdb with python. But it will be partial solution. :(
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline cacb

  • Regular
  • ***
  • Posts: 425
Re: How can watch the contents of std::containers?
« Reply #5 on: May 15, 2020, 06:38:03 pm »
This is interesting information. Does a similar solution for debugging std::vector and other containers exist under linux and g++? I have seen similar issues as described and would appreciate info on how to improve debugging with containers.

Offline hongwenjun

  • Multiple posting newcomer
  • *
  • Posts: 13
Re: How can watch the contents of std::containers?
« Reply #6 on: May 16, 2020, 02:30:30 am »
Code: [Select]
// map::crbegin/crend
#include <iostream>
#include <map>

int main ()
{
  std::map<char,int> mymap;

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

  std::cout << "mymap backwards:";
  for (auto rit = mymap.crbegin(); rit != mymap.crend(); ++rit)
    std::cout << " [" << rit->first << ':' << rit->second << ']';
  std::cout << '\n';

  return 0;
}

Codeblocks is just an IDE, helping users to run compiler and debugger commands

Code: [Select]
--------------  Debug  GNU GCC Compiler)---------------

[ 50.0%] g++.exe -Wall -fexceptions -g -std=c++11  main.cpp -o main.o
[100.0%] g++.exe  -o 234.exe  main.o   

-------------- GNU gdb (GDB) 8.1 ------------------------

[debug]Command-line: C:\CodeBlocks\MinGW\bin\gdb.exe -nx -fullname -quiet  -args R:/www/234/bin/Debug/234.exe
[debug]Working dir : R:\www\234

C:\CodeBlocks\MinGW\bin\gdb.exe -nx -fullname -quiet  -args R:/www/234/bin/Debug/234.exe


[debug]> set prompt >>>>>>cb_gdb:


[debug]Reading symbols from R:/www/234/bin/Debug/234.exe...done.
[debug](gdb) >>>>>>cb_gdb:
[debug]> show version
[debug]GNU gdb (GDB) 8.1

[debug]>>>>>>cb_gdb:
[debug]> set confirm off

GNU gdb (GDB) 8.1

[debug]>>>>>>cb_gdb:
[debug]> set width 0
[debug]>>>>>>cb_gdb:
[debug]> set height 0
[debug]>>>>>>cb_gdb:
[debug]> set breakpoint pending on
[debug]>>>>>>cb_gdb:
[debug]> set print asm-demangle on
[debug]>>>>>>cb_gdb:
[debug]> set unwindonsignal on
[debug]>>>>>>cb_gdb:
[debug]> set print elements 200
[debug]>>>>>>cb_gdb:
[debug]> set new-console on
[debug]>>>>>>cb_gdb:
[debug]> set disassembly-flavor att
[debug]>>>>>>cb_gdb:
[debug]> catch throw
[debug]Catchpoint 1 (throw)
[debug]>>>>>>cb_gdb:
[debug]> source C:\CodeBlocks\MinGW\etc\gdbinit

[debug]Traceback (most recent call last):
[debug]  File "<string>", line 4, in <module>
[debug]  File "c:\codeblocks\mingw\share\gdb/python/../../gcc-8.1.0/python/libstdcxx/v6/printers.py", line 1606, in register_libstdcxx_printers
[debug]    gdb.printing.register_pretty_printer(obj, libstdcxx_printer)
[debug]  File "c:\codeblocks\mingw\share\gdb/python/gdb/printing.py", line 152, in register_pretty_printer
[debug]    printer.name)
[debug]RuntimeError: pretty-printer already registered: libstdc++-v6
[debug]C:\CodeBlocks\MinGW\etc\gdbinit:6: Error in sourced command file:
[debug]Error while executing Python code.
[debug]>>>>>>cb_gdb:>>>>>>cb_gdb:
[debug]> directory R:/www/234/

Error while executing Python code.

[debug]Source directories searched: R:/www/234;$cdir;$cwd
[debug]>>>>>>cb_gdb:
[debug]> break "R:/www/234/main.cpp:11"
[debug]Breakpoint 2 at 0x4015a3: file R:\www\234\main.cpp, line 11.
[debug]>>>>>>cb_gdb:
[debug]> run
[debug]Starting program: R:\www\234\bin\Debug\234.exe

PID:2996

[debug][New Thread 2996.0x1308]
[debug]Breakpoint 2, main () at R:\www\234\main.cpp:11
[debug]R:\www\234\main.cpp:11:151:beg:0x4015a3
[debug]>>>>>>cb_gdb:

R:\www\234\main.cpp:11

[debug]> show language
[debug]The current source language is "auto; currently c++".
[debug]>>>>>>cb_gdb:
[debug]> info locals
[debug]mymap = std::map with 2 elements = {[97 'a'] = 200, [98 'b'] = 100}
[debug]>>>>>>cb_gdb:
[debug]> info args
[debug]No arguments.
[debug]>>>>>>cb_gdb:



Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2730
Re: How can watch the contents of std::containers?
« Reply #7 on: May 17, 2020, 08:38:21 pm »
This is interesting information. Does a similar solution for debugging std::vector and other containers exist under linux and g++? I have seen similar issues as described and would appreciate info on how to improve debugging with containers.
Pretty print python scripts for gdb work on windows like on linux.. The same way, just the paths have to be correct...
if the scripts are not present on your system you can download them from here:
https://github.com/gcc-mirror/gcc/tree/master/libstdc%2B%2B-v3/python

Offline chameleon

  • Multiple posting newcomer
  • *
  • Posts: 34
Re: How can watch the contents of std::containers?
« Reply #8 on: May 26, 2020, 02:17:40 pm »
I tried this, and finally I have a solution.

The basic idea is:
Try latest mingw64 distribution for compilation for availability of latest c++2a support.
Try Code::Blocks mingw64 distribution for debugging with an older gdb.
Do the above, the same time.

- Download latest mingw64 g++(10.1) and gdb(9.1) for windows from here: www.winlibs.com (sorry for ad, but I use that version of mingw64).
- This version has gdb(9.1) compiled without python support.
- Place that mingw64 distribution on c:\program files\mingw64
- Download latest Code::Blocks with mingw64 which has g++(8.1) and gdb(8.1).
- This version has gdb(8.1) compiled with python support.
- Place that mingw64 distribution on c:\program files\mingw
- Use for compilation g++(10.1) from c:\program files\mingw64 distribution
- Use for debugging gdb(8.1) from c:\program files\mingw distribution, but first place "source $(TARGET_COMPILER_DIR)etc\gdbinit" on debugger initialization commands,as previous posts stated.