Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: chameleon on May 15, 2020, 11:06:57 am

Title: How can watch the contents of std::containers?
Post by: chameleon 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.
Title: Re: How can watch the contents of std::containers?
Post by: hongwenjun 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

(https://i.loli.net/2020/05/15/A2XHyKOG4klFzcx.png)

New result

(https://i.loli.net/2020/05/15/xMz5bByWdNGpSR6.png)
Title: Re: How can watch the contents of std::containers?
Post by: chameleon 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.
Title: Re: How can watch the contents of std::containers?
Post by: chameleon on May 15, 2020, 04:29:31 pm
It seems that I run a GDB without Python support.
So, there is no solution.
Title: Re: How can watch the contents of std::containers?
Post by: oBFusCATed on May 15, 2020, 06:27:27 pm
There is a solution - install gdb with python. But it will be partial solution. :(
Title: Re: How can watch the contents of std::containers?
Post by: cacb 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.
Title: Re: How can watch the contents of std::containers?
Post by: hongwenjun on May 16, 2020, 02:30:30 am
Code
// 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
--------------  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:


Title: Re: How can watch the contents of std::containers?
Post by: BlueHazzard 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
Title: Re: How can watch the contents of std::containers?
Post by: chameleon 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.
Title: Re: How can watch the contents of std::containers?
Post by: ollydbg on February 10, 2022, 02:12:37 pm
Releases brechtsanders/winlibs_mingw (https://github.com/brechtsanders/winlibs_mingw/releases)

It looks like winlibs' GDB has python support now, see this issue: the selected build of GDB does not support Python scripting. it cannot be used in qt creator.  Issue 2 brechtsanders/winlibs_mingw (https://github.com/brechtsanders/winlibs_mingw/issues/2)