Inspección del contenido del contenedor estándar (std:: map) con gdb


Supongamos que tenemos algo como esto:

#include <map>
int main(){
    std::map<int,int> m;
    m[1] = 2;
    m[2] = 4;
    return 0;
}

Me gustaría poder inspeccionar el contenido del mapa que ejecuta el programa desde gdb.
Si intento usar el operador subíndice obtengo:

(gdb) p m[1]
Attempt to take address of value not located in memory.

El uso del método find no produce mejores resultados:

(gdb) p m.find(1)
Cannot evaluate function -- may be inlined

¿Hay alguna manera de lograr esto?

Author: Paolo Tedesco, 2009-01-09

6 answers

Creo que no lo hay, al menos no si su fuente está optimizada, etc. Sin embargo, hay algunas macros para gdb que pueden inspeccionar contenedores STL para usted:

Http://sourceware.org/ml/gdb/2008-02/msg00064.html

Sin embargo, no uso esto, por lo que YMMV

 33
Author: jpalecek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-01-09 10:31:00

Las respuestas existentes a esta pregunta son muy desactualizadas. Con un GCC y GDB recientes simplemente Funciona TM gracias al soporte integrado de Python en GDB 7.x y las bonitas impresoras libstdc++ que vienen con GCC.

Para el ejemplo del OP obtengo:

(gdb) print m
$1 = std::map with 2 elements = {[1] = 2, [2] = 4}

Si no funciona automáticamente para usted, vea el primer punto en la página STL Support de la wiki de GDB.

También puede escribir impresoras Python pretty para sus propios tipos, consulte Pretty Impresión en el manual GDB.

 78
Author: Jonathan Wakely,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-11 00:26:00

El stl-views.gdb solía ser la mejor respuesta que había, pero ya no.

Esto aún no está integrado en la línea principal GDB, pero esto es lo que obtienes usando la rama' archer-tromey-python ' :

(gdb) list
1   #include <map>
2   int main(){
3       std::map<int,int> m;
4       m[1] = 2;
5       m[2] = 4;
6       return 0;
7   }
(gdb) break 6
Breakpoint 1 at 0x8048274: file map.cc, line 6.
(gdb) run

Breakpoint 1, main () at map.cc:6
6       return 0;
(gdb) print m
$1 = std::map with 2 elements = {
  [1] = 2,
  [2] = 4
}
(gdb) quit
 20
Author: Employed Russian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-01-11 22:52:36

Siempre está lo obvio: Definir su propia función de prueba... Llámalo desde gdb. Por ejemplo:

#define SHOW(X) cout << # X " = " << (X) << endl

void testPrint( map<int,int> & m, int i )
{
  SHOW( m[i] );
  SHOW( m.find(i)->first );
}

int
main()
{
    std::map<int,int> m;
    m[1] = 2;
    m[2] = 4;
    return 0;  // Line 15.
}

Y:

....
Breakpoint 1 at 0x400e08: file foo.C, line 15.
(gdb) run
Starting program: /tmp/z/qD 

Breakpoint 1, main () at qD.C:15
(gdb) call testPrint( m, 2)
m[i] = 4
(*m.find(i)).first = 2
(gdb) 
 19
Author: Mr.Ree,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-01-10 00:10:33

Intente Des-Referenciar Contenedores STL: en esta página: http://www.yolinux.com/TUTORIALS/GDB-Commands.html

 12
Author: anand,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-01-09 11:57:57

Puede sortear el segundo problema (Cannot evaluate function -- may be inlined) asegurándose de que su compilador utiliza información de depuración DWARF-2 (o 3 o 4) cuando compila su programa. DWARF-2 incluye información de inserción, por lo que debería poder usar cualquiera de los métodos descritos para acceder a los elementos de su contenedor std::map.

Para compilar con DWARF-2 debug info, agregue la bandera -gdwarf-2 a su comando compile.

 0
Author: Dan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-02-03 21:37:21