Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to print std::map value in gdb

I have a std::map< std::string, std::string> cont;

I want to see cont[ "some_key" ] in gdb. When I'm trying

p cont[ "some_ket" ]

I'm getting this message: One of the arguments you tried to pass to operator[] could not be converted to what the function wants.

I'm using GNU gdb Red Hat Linux (6.3.0.0-1.162.el4rh). Thanks

like image 230
Davit Siradeghyan Avatar asked Apr 21 '10 12:04

Davit Siradeghyan


People also ask

What does print do in GDB?

The usual way to examine data in your program is with the print command (abbreviated p ), or its synonym inspect . It evaluates and prints the value of an expression of the language your program is written in (see section Using GDB with Different Languages).

Which command is used to determine the variable in GDB?

The ptype [ARG] command will print the type. Show activity on this post. This question may be related: vtable in polymorphic class of C++ using gdb: (gdb) help set print object Set printing of object's derived type based on vtable info.

What is std::map in C++?

std::map is a sorted associative container that contains key-value pairs with unique keys. Keys are sorted by using the comparison function Compare . Search, removal, and insertion operations have logarithmic complexity. Maps are usually implemented as red-black trees.


3 Answers

The latest gdb has python support baked in so one could easily write a function to print out the contents of any stl structure. However you'd have to learn the API and write the script. Luckily gcc 4.5 will ship with the needed python scripts to get gdb to intelligently handle stl data structures.

EDIT: you don't have to wait for GCC 4.5 (which by the way has already been released), you can just grab the code from SVN.

like image 80
deft_code Avatar answered Oct 18 '22 23:10

deft_code


You can write your own dump functions and call them:

(gdb) call dump(m)

see this thread: http://www.mail-archive.com/[email protected]/msg02109.html

I'm curious about the GDB helper macros.

like image 39
Eddy Pronk Avatar answered Oct 18 '22 23:10

Eddy Pronk


Gdb doesn't understand C++ operator overloading.

like image 1
Marcelo Cantos Avatar answered Oct 18 '22 22:10

Marcelo Cantos