Author Topic: How do you show the contents in an address?  (Read 4121 times)

jlgs

  • Guest
How do you show the contents in an address?
« on: February 22, 2021, 07:59:48 am »
Hello, I have recently just started to use Code Blocks, but I am having trouble in figuring out how to debug properly. Previously, I was using the CS50 IDE (The edX course), and in said IDE, we were able to see what is the exact content/value in an address, which was very useful especially in debugging data structures like linked lists, etc.

https://imgur.com/a/SWj3BP0

An example would be the image above, I'm able to see what is the content (in this instance, 1) of the head node, which has an address of 0x4062b0.

https://imgur.com/a/ujDWaCm

However, in Code Blocks, I'm only able to see the address, but not the content within that address. Is that anyway to fix this?

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: How do you show the contents in an address?
« Reply #1 on: February 22, 2021, 09:32:48 am »
The watches window of codeblocks uses the "output" command of gdb, so you can write any valid code in the watches windo.
If you want to know the address of a variable
Code
int a;
you simply type
Code
&a
in the watches window
If your variable is a pointer
Code
int *a;
and you want to see the content where the pointer is pointing type
Code
*a
in the watches window.

For complex data structures like std::vector std::shared_ptr ecc you can use python enabled gdb to have a nice print in the watches dialog
For this you need a python enabled gdb: https://github.com/ssbssa/gdb/releases/tag/gdb-10.1.90.20201024
and load the watch scripts
http://wiki.codeblocks.org/index.php/Pretty_Printers

i really hope we can provide a nice default behavior in the next code blocks release...

Offline cacb

  • Lives here!
  • ****
  • Posts: 536
Re: How do you show the contents in an address?
« Reply #2 on: March 08, 2021, 09:47:45 pm »
I would really like to be able to learn how to use pretty printers in Code::Blocks
I checked the link http://wiki.codeblocks.org/index.php/Pretty_Printers
But it seems out of date?

I was able to do this under Kubuntu 18.04
(gdb) python print(sys.version)
3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0]

So apparently my gdb is python enabled

However the link to printers.py is dead on that page...? Where can we find an up to date printers.py that covers smart pointers and STL?


Update: looks like I got it to work under Kubuntu 18.04

I stored the following as /home/<username>/gdb_printers/pp.gdb

python
import sys
sys.path.insert(0, '/usr/share/gcc-8/python/libstdcxx/v6')
from printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end


Then in Code::Blocks
Codeblocks->Settings->GDB/CDB Debugger->Default->Debugger initialization commands

source /home/<username>/gdb_printers/pp.gdb

found some additional tips (if slightly outdated) at https://www.gonwan.com/2014/07/02/setting-up-pretty-printers-in-gdb/

Seems to work with STL vector

« Last Edit: March 08, 2021, 10:24:02 pm by cacb »

Offline cacb

  • Lives here!
  • ****
  • Posts: 536
Re: How do you show the contents in an address?
« Reply #3 on: March 08, 2021, 10:43:12 pm »
After a little bit of fiddling I also got it to work with smart pointers (which I use a lot). You can't navigate from the pointer, but you can dereference the smart pointer in the watch window, just like a raw pointer. That's progress...