GDB: How do we extract values from an std::tuple

Finch_Powers

How do we extract/print individual values in an std::tuple?

Here is a sample program in a file named test.cc.

#include <tuple>
#include <iostream>

 using namespace std;

 int main() {
     auto t = make_tuple(111, 222);
     cout << std::get<0>(t) << endl
          << std::get<1>(t) << endl;
     return 0;
 }

Compile it

g++ --std=c++11 -g test.cc

Run it in gdb

gdb --args ./a.out
...
(gdb) start
Temporary breakpoint 1 at 0x400836: file test.cc, line 7.
Starting program: /home/fmlheureux/a.out

Temporary breakpoint 1, main () at test.cc:7
7           auto t = make_tuple(111, 222);
(gdb) n
9                << std::get<1>(t) << endl;
(gdb) p t
$1 = std::tuple containing = {[1] = 111, [2] = 222}

The last command printed the tuple as a whole. How can I extract individual values? My naives attempts fail.

(gdb) p get<0>(t)
No symbol "get<0>" in current context.
(gdb) p std::get<0>(t)
No symbol "get<0>" in namespace "std".
Tom Tromey

Unfortunately, the pretty-printing code in gdb is a display-only feature -- so while it helps display a tuple in a nice way, it doesn't let you further access it.

The normal problem with something like t.get<0> is that these tiny accessor methods are usually completely optimized away by the compiler -- so there is no copy to call. And, while gdb has an "xmethod" feature that can be used to supply gdb-side Python implementations of these accessors, info xmethods shows (for me at least) that nobody has done this for std::tuple yet.

So then you're pretty much left with just one option: inspecting the implementation. So, start by printing the raw tuple:

(gdb) p/r t
$3 = {<std::_Tuple_impl<0ul, int, int>> = {<std::_Tuple_impl<1ul, int>> = {<std::_Head_base<1ul, int, false>> = {
        _M_head_impl = 222}, <No data fields>}, <std::_Head_base<0ul, int, false>> = {_M_head_impl = 111}, <No data fields>}, <No data fields>}

Here you can see the "real" structure of the tuple, and access the fields directly:

(gdb) print ((std::_Head_base<1ul, int, false>) t)._M_head_impl
$7 = 222

This cast to an intermediate type is kind of a pain, eh? It's needed to make gdb choose the correct _M_head_impl field. If this is something you plan to do a lot I'd suggest writing that xmethod. Or you could also easily write a Python convenience function to automate the access; this kind of introspection is a bit simpler to do with the Python API.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I extract these values from an object

From Dev

How to extract integer from list with tuple in Elixir?

From Dev

How does this code extract arguments from tuple?

From Dev

How can we extract values from URL when redirect to a Android app?

From Dev

How to extract values from xml file and do mathematical operations?

From Dev

How do i extract and combine values from a dict

From Dev

How do I change values in gdb

From Dev

How do I search for a tuple of values in pandas?

From Dev

How do I search for a tuple of values in pandas?

From Dev

Extract tuple from list

From Dev

How to extract tuple values in pandas dataframe for use of matplotlib?

From Dev

How to make a tuple of lvalue references from a tuple of values

From Dev

How do I move String values from an array to a tuple without copying?

From Dev

How do I get all values from one position in a tuple in a pandas dataframe column?

From Dev

how do i extract the last element of a tuple/bag in Pig?

From Java

How to return multiple values from a function in Kotlin like we do in Swift?

From Dev

How do we get two random values from min and max but with a gap of 500?

From Dev

How do we separate values fetched from findvalues of XML::LibXMl by a delimiter?

From Dev

How do we get multiple values from a single django orm query?

From Dev

std::vector<std::tuple in for - cannot get values

From Dev

How do I extract part of a tuple that's duplicate as key to a dictionary, and have the second part of the tuple as value?

From Dev

How do i extract values with beautifulsoup?

From Dev

How can we extract the main verb from a sentence?

From Dev

Swift: How can we extract a subview from xib in Xcode?

From Dev

Swift: How can we extract a subview from xib in Xcode?

From Dev

How to extract date , month and year from the date we pass to javascript?

From Dev

Unable to extract in GDB the values where FPE is occuring

From Dev

How do we start a thread from a servlet?

From Dev

How Do We Get a Looper from an Executor?

Related Related

  1. 1

    How do I extract these values from an object

  2. 2

    How to extract integer from list with tuple in Elixir?

  3. 3

    How does this code extract arguments from tuple?

  4. 4

    How can we extract values from URL when redirect to a Android app?

  5. 5

    How to extract values from xml file and do mathematical operations?

  6. 6

    How do i extract and combine values from a dict

  7. 7

    How do I change values in gdb

  8. 8

    How do I search for a tuple of values in pandas?

  9. 9

    How do I search for a tuple of values in pandas?

  10. 10

    Extract tuple from list

  11. 11

    How to extract tuple values in pandas dataframe for use of matplotlib?

  12. 12

    How to make a tuple of lvalue references from a tuple of values

  13. 13

    How do I move String values from an array to a tuple without copying?

  14. 14

    How do I get all values from one position in a tuple in a pandas dataframe column?

  15. 15

    how do i extract the last element of a tuple/bag in Pig?

  16. 16

    How to return multiple values from a function in Kotlin like we do in Swift?

  17. 17

    How do we get two random values from min and max but with a gap of 500?

  18. 18

    How do we separate values fetched from findvalues of XML::LibXMl by a delimiter?

  19. 19

    How do we get multiple values from a single django orm query?

  20. 20

    std::vector<std::tuple in for - cannot get values

  21. 21

    How do I extract part of a tuple that's duplicate as key to a dictionary, and have the second part of the tuple as value?

  22. 22

    How do i extract values with beautifulsoup?

  23. 23

    How can we extract the main verb from a sentence?

  24. 24

    Swift: How can we extract a subview from xib in Xcode?

  25. 25

    Swift: How can we extract a subview from xib in Xcode?

  26. 26

    How to extract date , month and year from the date we pass to javascript?

  27. 27

    Unable to extract in GDB the values where FPE is occuring

  28. 28

    How do we start a thread from a servlet?

  29. 29

    How Do We Get a Looper from an Executor?

HotTag

Archive